The online racing simulator
Guide: My first InSim Application
2
(34 posts, started )
I think I have the original code by TAA. Is this it maybe?
Attached files
C InSim Tutorial.zip - 21.4 KB - 977 views
Yeah that looks about right
My C# tutorial disappeared as well, so I've uploaded a new one to the post.
Thank you to all three of you.
nice
Quote from DarkTimes :Hey, wasn't sure where to post this, but someone asked me for an example of writing an OutGauge app in C++, and this is what I came up with. I'm posting it here in case it's useful to other people. Just note that I ain't a great C++ programmer, so there may be a few issues that I've not realised. Anyway it appears to work and shouldn't explode.

The code is a rehash of the pyinsim OutGauge example, which basically prints out a message whenever the shift-light comes on. I use this because it's simple, demonstrates everything you need to know, and is easily testable. It's usefulness, however, can be disputed.

Note: The OutGauge flags mask thing is explained in this thread.

#include <iostream>
#include <winsock2.h> // WinSock2 library (may need to add WS2_32.lib to project dependencies).

#define BUFFER_SIZE 512 // Receive buffer size (basically maximum packet size in UDP).
#define HOST "127.0.0.1" // Host to connect to.
#define PORT 30000 // Port to connect to the host through.

// Define types used by InSim.
typedef unsigned char byte;
typedef unsigned short word;
typedef struct
{
int X, Y, Z;
} Vec;
typedef struct
{
float X, Y, Z;
} Vector;

// Include InSim header.
#include "InSim.h"

void OutGaugePacketReceived(const OutGaugePack packet);

int main(int argc, char *argv[])
{

// Initialise WinSock version 2.2.
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
WSACleanup();
std::cerr << "Error: Failed to init WinSock" << std::endl;
return EXIT_FAILURE;
}

// Create UDP socket.
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET)
{
WSACleanup();
std::cerr << "Error: Could not create socket." << std::endl;
return EXIT_FAILURE;
}

// Bind to receive UDP packets.
sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr(HOST);
saddr.sin_port = htons(PORT);
if (bind(sock, (sockaddr *)&saddr, sizeof(sockaddr)) == SOCKET_ERROR)
{
closesocket(sock);
WSACleanup();
std::cerr << "Error: Could not connect to LFS" << std::endl;
return EXIT_FAILURE;
}

// Packet receive loop.
char recvbuf[BUFFER_SIZE];
memset(recvbuf, 0, sizeof(recvbuf)); // Set recvbuf to zero.
int bytes = 0;
do
{
bytes = recv(sock, recvbuf, BUFFER_SIZE, 0);
if (bytes > 0)
OutGaugePacketReceived((OutGaugePack &)recvbuf);
else if (bytes == 0)
std::cerr << "Error: Lost connection with LFS" << std::endl;
else
std::cerr << "Error: " << WSAGetLastError() << std::endl;
} while (bytes > 0);

// Cleanup and exit.
closesocket(sock);
WSACleanup();
return EXIT_SUCCESS;
}

void OutGaugePacketReceived(const OutGaugePack packet)
{
unsigned mask = 1 << DL_SHIFT;
if (packet.ShowLights & mask)
{
std::cout << "Shift light on!" << std::endl;
}
}


I uploaded a VS2010 project for this, with dependencies and stuff, which should compile without any issue.
Attached files
OutGauge_Project.zip - 23.2 KB - 826 views
How can i add text when a player connects and buttons in the project from the first post?
Quote from Ziroh :How can i add text when a player connects

Sending text: (you send this packet to LFS)
Quote from docs/InSim.txt :struct IS_MST // MSg Type - send to LFS to type message or command
{
byte Size; // 68
byte Type; // ISP_MST
byte ReqI; // 0
byte Zero;

char Msg[64]; // last byte must be zero
};

Detecting a new connection: (you receive this packet from LFS)
Quote from docs/InSim.txt :struct IS_NCN // New ConN
{
byte Size; // 56
byte Type; // ISP_NCN
byte ReqI; // 0 unless this is a reply to a TINY_NCN request
byte UCID; // new connection's unique id (0 = host)

char UName[24]; // username
char PName[24]; // nickname

byte Admin; // 1 if admin
byte Total; // number of connections including host
byte Flags; // bit 2 : remote
byte Sp3;
};

Nice : D
-
mata (a474130644) DELETED by Flame CZE : spam
2

FGED GREDG RDFGDR GSFDG