#include "cinsim/CInsim.h" //Headers for CInsim library #include //Basic C++ streams, needed for console output CInsim insim; //CInsim object, handles the communication with LFS int main() { int retVal = 0; struct IS_VER verPack; //LFS can tell us info about it's version and InSim version, store that info in this struct. //Try to connect to LFS retVal = insim.init("127.0.0.1", //LFS will run on local machine (word)29999, //InSim will accept connection on port 29999 "Sample InSim app", //Short name for our app, will appear in LFS "", //No admin password &verPack, //Pointer to IS_VERSION packet '!', //InSim command character ISF_MCI, //We're writing a local InSim app 1000, //Receive updates every 1000 msecs 0); //No UDP today //Could we connect to LFS? if(retVal < 0) //We could not { //Tell the user and exit std::cerr << "Error connecting to LFS!" << std::endl; return -1; } //All OK, report version of LFS and InSim std::cout << "Connected to LFS " << verPack.Version << ", InSim ver. " << verPack.InSimVer << std::endl; //Let's create a button that will be displayed in LFS. struct IS_BTN sampleBtn; memset(&sampleBtn, 0, sizeof(sampleBtn)); //Make sure there is nothing in the memory we created the button struct in. sampleBtn.Size = sizeof(struct IS_BTN); //Size of the packet sampleBtn.Type = ISP_BTN; //Type of the button sampleBtn.ReqI = 1; sampleBtn.UCID = 0; sampleBtn.ClickID = 1; //We can identify the button by this number sampleBtn.BStyle = ISB_LIGHT | ISB_CLICK; //Type of the button, add ISB_CLICK to make it clickable sampleBtn.L = 5; //Left offset sampleBtn.T = 5; //Top offset sampleBtn.H = 10; //Height of the button sampleBtn.W = 50; //Width of the button sprintf(sampleBtn.Text, "%s", "Don't you dare clicking me!"); //Text that will appear in the button insim.send_packet(&sampleBtn); //Send a packet instructing LFS to create our button through InSim //Now enter the main loop which will receive packets from LFS int recvErr = 0; int packetType = 0; bool canRun = true; //Run the loop as long as this is true while(canRun) { //Get the next packet in the buffer recvErr = insim.next_packet(); if(recvErr < 0) //Something went wrong { //Tell the user and exit std::cerr << "Error receiving packets from LFS!" << std::endl; return -1; } //We have a packet, check it's type packetType = insim.peek_packet(); //Handle each type of packet switch(packetType) { case ISP_BTC: //A button was clicked in LFS struct IS_BTC* btc = (struct IS_BTC*)insim.get_packet(); //We know what type of packet we got, so read it if(btc->ClickID == 1) //Our button was clicked { canRun = false; //Exit the loop } break; //Remember to have this after EVERY case: statement!!! } } //Successful exit insim.isclose(); //I forgot to add this line in the video, but connection to InSim should be always properly closed return 0; } /** * Please note that you need CInsim library to successfully compile this source. * For further info regarding packets that can be send to/received from LFS * refer to insim.h file (included in CInsim library). */