DWORD WINAPI netGameLoop(void *param) { int len, c, x, y; Psquare p; TnetGameSettings *b; char *m, *u, *a; hostent *h; char buf[256]; if(!param){ buf[0]=(BYTE)C_INIT1; buf[1]=NETGAME_VERSION; wr(buf, 2); c=rd1(); if(c!=C_INIT2) goto le; c=rd1(); if(c<1) goto le; netGameVersion=c; SetForegroundWindow(hWin); h= gethostbyaddr((char*)&netGameIP.sin_addr, 4, netGameIP.sin_family); a= inet_ntoa(netGameIP.sin_addr); if(msg1(MB_YESNO|MB_ICONQUESTION, lng(868, "Do you want to play with %s [%s] ?"), (h && h->h_name) ? h->h_name : "???", a ? a : "???")!=IDYES){ wr1(C_INIT_DENY); goto le; } buf[0]=(BYTE)C_INFO; buf[1]=sizeof(TnetGameSettings); b= (TnetGameSettings*)(buf+2); b->width=(char)width; b->height=(char)height; b->begin= (player==1); b->rule5=(char)ruleFive; b->cont=(char)continuous; if(netGameVersion<2) b->rule5=b->cont=0; wr(buf, 2+sizeof(TnetGameSettings)); if(rd1()!=C_INFO_OK) goto le; b->begin= !b->begin; } else{ buf[0]=(BYTE)C_INIT2; buf[1]=NETGAME_VERSION; wr(buf, 2); c=rd1(); if(c==C_BUSY) wrLog(lng(874, "The other player is already playing with someone else")); if(c!=C_INIT1) goto le; c=rd1(); if(c<1) goto le; netGameVersion=c; wrLog(lng(871, "Connection established. Waiting for response...")); c=rd1(); if(c==C_INIT_DENY) wrLog(lng(870, "The other player doesn't want to play with you !")); if(c!=C_INFO) goto le; len=rd1(); b= (TnetGameSettings*)buf; memset(buf, 0, sizeof(TnetGameSettings)); if(len<2 || rd(buf, len)<0) goto le; wr1(C_INFO_OK); } SendMessage(hWin, WM_COMMAND, 992, (LPARAM)b); undoRequest=0; for(;;){ x=rd1(); switch(x){ case C_MSG: //message len=rd2(); if(len<=0) goto le; u=new char[2*len]; if(rd(u, 2*len)>=0){ m=new char[len+1]; WideCharToMultiByte(CP_ACP, 0, (WCHAR*)u, len, m, len, 0, 0); m[len]='\0'; wrLog("---> %s", m); delete[] m; SetWindowPos(logDlg, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_ASYNCWINDOWPOS|SWP_NOACTIVATE|SWP_SHOWWINDOW); } delete[] u; break; case C_UNDO_REQUEST: y=rd2(); if(y<=0 || y>moves) goto le; if(undoRequest){ //both players pressed undo simultaneously if(undoRequest<0) undoRequest=y; amax(undoRequest, y); postUndo(); } else{ c=msg1(MB_YESNO|MB_ICONQUESTION, lng(876, "The other player wants to UNDO the last move.\r\nDo you agree ?")); if(c==IDYES){ undoRequest=y; wr1(C_UNDO_YES); postUndo(); } else{ wr1(C_UNDO_NO); } } break; case C_NEW_REQUEST: if(undoRequest){ if(undoRequest<0){ //both players pressed NewGame simultaneously postNewGame(); } else{ postUndo(); } } else{ c=msg1(MB_YESNO|MB_ICONQUESTION, lng(877, "The other player wants to start a NEW game.\r\nDo you agree ?")); if(c==IDYES){ undoRequest=-1; wr1(C_NEW_YES); postNewGame(); } else{ wr1(C_NEW_NO); } } break; case C_UNDO_YES: if(undoRequest<=0) goto le; postUndo(); break; case C_UNDO_NO: if(undoRequest>0){ msglng(878, "Sorry, the other player does not allow to UNDO your move."); undoRequest=0; } break; case C_NEW_YES: if(undoRequest>=0) goto le; postNewGame(); break; case C_NEW_NO: if(undoRequest<0){ msglng(879, "Sorry, the other player does not allow to start a NEW game."); undoRequest=0; } break; default: //move or error if(x<0 || x>=width){ show(logDlg); goto le; } while(finished && (getTickCount()-lastTick<5000 || saveLock)){ Sleep(200); } if(finished) SendMessage(hWin, WM_COMMAND, 101, 0); y=rd1(); if(y<0 || y>=height) goto le; p=Square(x, y); p->time=rd4(); PostMessage(hWin, WM_COMMAND, 991, (LPARAM)p); } } le: EnterCriticalSection(&netLock); netGameDone(); LeaveCriticalSection(&netLock); return 0; }
static void proc_l1bi5(mux_ctx_t ctx) { union { struct dc_s bin[1]; struct dqbi5_s bi5[2]; } buf[1]; ssize_t nrd; int fd = ctx->infd; /* read a probe */ if (UNLIKELY((nrd = read(fd, buf->bi5, sizeof(buf->bi5))) <= 0)) { return; } else if (UNLIKELY(nrd == sizeof(*buf->bi5))) { /* only one record then, just go for bi5 format, * make use of the tick compressor and dupe the record * then just proceed normally */ memcpy(buf->bi5 + 1, buf->bi5 + 0, sizeof(*buf->bi5)); } else if (UNLIKELY((size_t)nrd < sizeof(buf->bi5))) { /* oooh incomplete innit? */ return; } /* the only thing we can make assumptions about is the timestamp * we check the two stamps in bi5 and compare their distance */ { uint32_t ts0 = be32toh(buf->bi5[0].ts); uint32_t ts1 = be32toh(buf->bi5[1].ts); if (ts1 - ts0 > 60/*min*/ * 60/*sec*/ * 1000/*msec*/) { /* definitely old_fmt */ goto old_fmt; } /* quickly polish the probe */ buf->bi5[0].ts = ts0; buf->bi5[0].ap = be32toh(buf->bi5[0].ap); buf->bi5[0].bp = be32toh(buf->bi5[0].bp); buf->bi5[0].aq.i = be32toh(buf->bi5[0].aq.i); buf->bi5[0].bq.i = be32toh(buf->bi5[0].bq.i); buf->bi5[1].ts = ts1; buf->bi5[1].ap = be32toh(buf->bi5[1].ap); buf->bi5[1].bp = be32toh(buf->bi5[1].bp); buf->bi5[1].aq.i = be32toh(buf->bi5[1].aq.i); buf->bi5[1].bq.i = be32toh(buf->bi5[1].bq.i); } /* re-use the probe data */ write_tick_bi5(ctx, buf->bi5 + 0); /* main loop */ do { write_tick_bi5(ctx, buf->bi5 + 1); } while (rd1bi5(fd, buf->bi5 + 1)); return; old_fmt: /* polish the probe */ buf->bin->ts = be64toh(buf->bin->ts); buf->bin->ap.i = be64toh(buf->bin->ap.i); buf->bin->bp.i = be64toh(buf->bin->bp.i); buf->bin->aq.i = be64toh(buf->bin->aq.i); buf->bin->bq.i = be64toh(buf->bin->bq.i); /* main loop */ do { write_tick(ctx, buf->bin); } while (rd1(fd, buf->bin)); return; }
int main() { // Create the main window sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Hello World - SFML"); CollisionRectangle r1(glm::vec2(5,5), glm::vec2(100, 40)), r2(glm::vec2(200, 100), glm::vec2(50, 120)); r1.setOrigin(glm::vec2(50, 20)); r2.setOrigin(glm::vec2(10, 30)); r1.setRotation(DEG2RAD(138.f)); bool choose = false; bool r1Col(false), r2Col(false); // Start the main loop while (App.isOpen()) { // Process events sf::Event Event; while (App.pollEvent(Event)) { // Close window : exit if (Event.type == sf::Event::Closed || (Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Escape)) App.close(); if (Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Space) { choose = !choose; } if (Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Return) { r1Col = r1.checkCollision(r2); r2Col = r2.checkCollision(r1); std::cout<<"\n"; } if (Event.type == sf::Event::MouseWheelMoved) { if (choose) r1.rotate(DEG2RAD(Event.mouseWheel.delta)*5.f); else r2.rotate(DEG2RAD(Event.mouseWheel.delta)); } } if (choose) r1.setPosition(glm::vec2(sf::Mouse::getPosition(App).x, sf::Mouse::getPosition(App).y)); else r2.setPosition(glm::vec2(sf::Mouse::getPosition(App).x, sf::Mouse::getPosition(App).y)); // Clear screen, and fill it with blue App.clear(sf::Color::Black); sf::ConvexShape rd1(4), rd2(4); rd1.setOutlineColor(sf::Color::Blue); rd1.setFillColor(sf::Color(0.f, 0.f, 0.f, 0.f)); rd1.setOutlineThickness(2.f); rd2.setOutlineColor(sf::Color::Blue); rd2.setFillColor(sf::Color(0.f, 0.f, 0.f, 0.f)); rd2.setOutlineThickness(2.f); r1Col = r1.checkCollision(r2); r2Col = r2.checkCollision(r1); if (r1Col || r1.isPointInside(glm::vec2(0.f, 0.f))) rd1.setOutlineColor(sf::Color::Green); if (r2Col) rd2.setOutlineColor(sf::Color::Green); for (int i = 0; i < 4; i++) { rd1.setPoint(i, sf::Vector2f(r1.getVertex(i).x, r1.getVertex(i).y)); rd2.setPoint(i, sf::Vector2f(r2.getVertex(i).x, r2.getVertex(i).y)); } App.draw(rd1); App.draw(rd2); // Display the content of the window on screen App.display(); } return 0; }