void State::Manager::Run(State& entranceState) { SDL_setenv("UBUNTU_PLATFORM_API_BACKEND", "touch_mirclient", 1); accel = ua_sensors_accelerometer_new(); ua_sensors_accelerometer_set_reading_cb(accel, tilt_cb, 0); ua_sensors_accelerometer_enable(accel); current = &entranceState; current->Enter(); //clock_t ticks = clock(); while (!quit) { PollEvent(); Tilt(); if (next) EnterNextState(); CallLoopFunction(); if (param.framerate != 0) { /**clock_t nticks = clock(); int32_t sleeptime = (CLOCKS_PER_SEC/param.framerate - (nticks-ticks)) / 1000; if (sleeptime > 0) SDL_Delay(sleeptime);*/ SDL_Delay(1); //ticks = nticks; } } current->Exit(); previous = current; current = NULL; }
Connection::Connection(const ServerId_t serverId): strangerIsTyping(false), userCount(0) { // Generate a meaningless value for omegle to track us with. srand(time(0)); std::string abtest(INIT_PACKET_ABTEST_LENGTH, 'a'); for(int i = 0; i < INIT_PACKET_ABTEST_LENGTH; ++i) { abtest[i] = RandomAlphanumericChar(); } sock.Connect(SERVERS[serverId], PORT); // Do the little init sequence. SendPacket(PID_INIT, INIT_PACKET_STRING+abtest); PacketId packetId; Packet* packet; while(PollEvent(&packetId, &packet, BLOCKING) && (packetId != PID_CONNECTED)) { if(packetId == PID_WAIT) { continue; } else if(packetId == PID_CAPTCHA) { throw CaptchaError(((CaptchaPacket*)packet)->key); } else { throw Error("Unexpected packet received while establishing connection (\""+packetId+"\")"); } } }
++h;}SDL_FillRect(e,&k,255);}int main(int a,char**b){SDL_Event d;char z[99]="L" "abyrinth ";SDL_Init(32);SDL_Surface*c=SDL_SetVideoMode(480,480,32,0);e=SDL_Cr\ eateRGBSurface(0,480,480,32,0,0,0,0);int t=SDL_GetTicks();g();while(1){if(SDL_\ PollEvent(&d)){if(d.type==12)break;else{int x=d.button.x;int y=d.button.y;if(d. button.button==1&&x<f.x+13&&x>f.x-5&&y<f.y+13&&y>f.y-5){x=((x>>3)<<3)+1;y=((y>> 3)<<3)+1;if(!p(x+2,y+2)){f.x=x;f.y=y;if(f.x==k.x&&f.y==k.y){t=SDL_GetTicks();g( );}}}}}SDL_BlitSurface(e,0,c,0);SDL_FillRect(c,&f,0);int u=SDL_GetTicks()-t;sp\ rintf(z+10,"%d",20-u/1000);SDL_WM_SetCaption(z,z);if(u>20000)break;SDL_Flip(c); SDL_Delay(20);}SDL_FreeSurface(e);SDL_Quit();return 0;}
int SDLApplication::Run() { if (!Init()) { return EXIT_FAILURE; } SDL_Event event; while (isRunning) { while (SDL_PollEvent(&event)) { PollEvent(event); } } return EXIT_SUCCESS; }
std::string Connection::PollMessage(const bool blocking) { PacketId packetId; Packet* packet; while(PollEvent(&packetId, &packet, blocking)) { if(packetId == PID_STRANGERMESSAGE) { return static_cast<StrangerMessagePacket*>(packet)->message; } else if(packetId == PID_DISCONNECT) { throw ConversationOverError(); //Not quite the most elegant use of exceptions. } } return ""; }
Bool CZmq::DiscardMsg() { if(!m_pRecvBuf) m_pRecvBuf = new OctetsStream(RCVBUF_SIZE); while (PollEvent(HEVENT_READ, 0)) { m_pRecvBuf->Clear(); Size_t iSize = (Size_t)m_pRecvBuf->Capacity(); if(!Recv(m_pRecvBuf->Begin(), iSize, 0)) return false; while (IsWaitRecv()) { m_pRecvBuf->Clear(); Size_t iSize = (Size_t)m_pRecvBuf->Capacity(); if (!Recv(m_pRecvBuf->Begin(), iSize, 0)) return false; } } return true; }
void VideoDriver_SDL::MainLoop() { uint32 cur_ticks = SDL_CALL SDL_GetTicks(); uint32 last_cur_ticks = cur_ticks; uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK; uint32 mod; int numkeys; Uint8 *keys; CheckPaletteAnim(); if (_draw_threaded) { /* Initialise the mutex first, because that's the thing we *need* * directly in the newly created thread. */ _draw_mutex = ThreadMutex::New(); if (_draw_mutex == NULL) { _draw_threaded = false; } else { _draw_mutex->BeginCritical(); _draw_continue = true; _draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread); /* Free the mutex if we won't be able to use it. */ if (!_draw_threaded) { _draw_mutex->EndCritical(); delete _draw_mutex; _draw_mutex = NULL; } else { /* Wait till the draw mutex has started itself. */ _draw_mutex->WaitForSignal(); } } } DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no "); for (;;) { uint32 prev_cur_ticks = cur_ticks; // to check for wrapping InteractiveRandom(); // randomness while (PollEvent() == -1) {} if (_exit_game) break; mod = SDL_CALL SDL_GetModState(); #if SDL_VERSION_ATLEAST(1, 3, 0) keys = SDL_CALL SDL_GetKeyboardState(&numkeys); #else keys = SDL_CALL SDL_GetKeyState(&numkeys); #endif #if defined(_DEBUG) if (_shift_pressed) #else /* Speedup when pressing tab, except when using ALT+TAB * to switch to another application */ #if SDL_VERSION_ATLEAST(1, 3, 0) if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0) #else if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0) #endif /* SDL_VERSION_ATLEAST(1, 3, 0) */ #endif /* defined(_DEBUG) */ { if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2; } else if (_fast_forward & 2) { _fast_forward = 0; } cur_ticks = SDL_CALL SDL_GetTicks(); if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) { _realtime_tick += cur_ticks - last_cur_ticks; last_cur_ticks = cur_ticks; next_tick = cur_ticks + MILLISECONDS_PER_TICK; bool old_ctrl_pressed = _ctrl_pressed; _ctrl_pressed = !!(mod & KMOD_CTRL); _shift_pressed = !!(mod & KMOD_SHIFT); /* determine which directional keys are down */ _dirkeys = #if SDL_VERSION_ATLEAST(1, 3, 0) (keys[SDL_SCANCODE_LEFT] ? 1 : 0) | (keys[SDL_SCANCODE_UP] ? 2 : 0) | (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) | (keys[SDL_SCANCODE_DOWN] ? 8 : 0); #else (keys[SDLK_LEFT] ? 1 : 0) | (keys[SDLK_UP] ? 2 : 0) | (keys[SDLK_RIGHT] ? 4 : 0) | (keys[SDLK_DOWN] ? 8 : 0); #endif if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged(); /* The gameloop is the part that can run asynchronously. The rest * except sleeping can't. */ if (_draw_mutex != NULL) _draw_mutex->EndCritical(); GameLoop(); if (_draw_mutex != NULL) _draw_mutex->BeginCritical(); UpdateWindows(); _local_palette = _cur_palette; } else { /* Release the thread while sleeping */ if (_draw_mutex != NULL) _draw_mutex->EndCritical(); CSleep(1); if (_draw_mutex != NULL) _draw_mutex->BeginCritical(); NetworkDrawChatMessage(); DrawMouseCursor(); } /* End of the critical part. */ if (_draw_mutex != NULL && !HasModalProgress()) { _draw_mutex->SendSignal(); } else { /* Oh, we didn't have threads, then just draw unthreaded */ CheckPaletteAnim(); DrawSurfaceToScreen(); } } if (_draw_mutex != NULL) { _draw_continue = false; /* Sending signal if there is no thread blocked * is very valid and results in noop */ _draw_mutex->SendSignal(); _draw_mutex->EndCritical(); _draw_thread->Join(); delete _draw_mutex; delete _draw_thread; _draw_mutex = NULL; _draw_thread = NULL; } }
void Game::Run (void) { Event event; Timer timer; bool using_vsync = this->window->IsUsingVSYNC(); uint32 frame_cap = 1000 / this->settings.target_fps; debug::InitializeOverlay(); m_flags |= RUNNING; timer.Start(); while (m_flags & RUNNING) { uint32 frame_start = timer.Ticks(); if (m_sceneStack.empty()) { m_flags &= ~RUNNING; break; } // ref count must be increased b/c scene could be removed from the collection // during the loop execution auto current_scene = m_sceneStack.back(); while (PollEvent(&event)) { if (debug::ProcessOnEvent(event)) { // debug UI requesting events - suppress from scene continue; } if (!(this->on_event_hook && this->on_event_hook(event))) { current_scene->OnEvent(event); } } delta_time dt(timer.TickDelta()); if (!(this->on_update_hook && this->on_update_hook(dt))) { current_scene->OnUpdate(dt); } debug::ProcessOnUpdate(static_cast<SDL_Window*>(*this->window.get()), dt); this->window->Clear(); if (!(this->on_render_hook && this->on_render_hook())) { current_scene->OnRender(); } debug::ProcessOnRender(); this->window->Present(); if (m_flags & PUSH_DEFERRED) { current_scene->Hibernate(); m_flags &= ~PUSH_DEFERRED; auto& new_current_scene = m_sceneStack.back(); new_current_scene->Initialize(); } else if (m_flags & POP_DEFERRED) { current_scene->Terminate(); m_flags &= ~POP_DEFERRED; auto& new_current_scene = m_sceneStack.back(); new_current_scene->Activate(); } else if (m_flags & SWAP_DEFERRED) { current_scene->Terminate(); m_flags &= ~SWAP_DEFERRED; auto& new_current_scene = m_sceneStack.back(); new_current_scene->Initialize(); } if (!using_vsync) { uint32 frame_length = timer.Ticks() - frame_start; if (frame_length < frame_cap) { SDL_Delay(frame_cap - frame_length); } } } }
void VideoDriver_Allegro::MainLoop() { uint32 cur_ticks = GetTime(); uint32 last_cur_ticks = cur_ticks; uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK; CheckPaletteAnim(); for (;;) { uint32 prev_cur_ticks = cur_ticks; // to check for wrapping InteractiveRandom(); // randomness PollEvent(); if (_exit_game) return; #if defined(_DEBUG) if (_shift_pressed) #else /* Speedup when pressing tab, except when using ALT+TAB * to switch to another application */ if (key[KEY_TAB] && (key_shifts & KB_ALT_FLAG) == 0) #endif { if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2; } else if (_fast_forward & 2) { _fast_forward = 0; } cur_ticks = GetTime(); if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) { _realtime_tick += cur_ticks - last_cur_ticks; last_cur_ticks = cur_ticks; next_tick = cur_ticks + MILLISECONDS_PER_TICK; bool old_ctrl_pressed = _ctrl_pressed; _ctrl_pressed = !!(key_shifts & KB_CTRL_FLAG); _shift_pressed = !!(key_shifts & KB_SHIFT_FLAG); /* determine which directional keys are down */ _dirkeys = (key[KEY_LEFT] ? 1 : 0) | (key[KEY_UP] ? 2 : 0) | (key[KEY_RIGHT] ? 4 : 0) | (key[KEY_DOWN] ? 8 : 0); if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged(); GameLoop(); UpdateWindows(); CheckPaletteAnim(); DrawSurfaceToScreen(); } else { CSleep(1); NetworkDrawChatMessage(); DrawMouseCursor(); DrawSurfaceToScreen(); } } }
bool dcsys_task() { enum { CMD_MENU = 1, }; int cmd = 0; Event ev; static unsigned int tick = 0; unsigned int tm = Timer() - tick; if (tm < USEC_TO_TIMER(1000000/60)) { return __dc_avail; } tick += tm; int x, y; int cJoy = 0; static int pJoy; static unsigned int repeatTime; if (__dc_avail) { dc_mouseaxis1 = 0; dc_mouseaxis2 = 0; while (PollEvent(ev)) { switch (ev.type) { case EVENT_KEYDOWN: switch (ev.key.keycode) { case KBD_S1: case KBD_S2: cmd = CMD_MENU; break; default: dckbd_keydown(ev.key.keycode); } break; case EVENT_KEYUP: dckbd_keyup(ev.key.keycode); break; case EVENT_MOUSEMOTION: dc_mouseaxis1 += ev.motion.x; dc_mouseaxis2 += ev.motion.y; break; case EVENT_MOUSEBUTTONDOWN: switch (ev.button.button) { case EVENT_BUTTON_LEFT: mousemng_buttonevent(MOUSEMNG_LEFTDOWN); break; case EVENT_BUTTON_RIGHT: mousemng_buttonevent(MOUSEMNG_RIGHTDOWN); break; } break; case EVENT_MOUSEBUTTONUP: switch (ev.button.button) { case EVENT_BUTTON_LEFT: mousemng_buttonevent(MOUSEMNG_LEFTUP); break; case EVENT_BUTTON_RIGHT: mousemng_buttonevent(MOUSEMNG_RIGHTUP); break; } break; case EVENT_JOYAXISMOTION: x = 0; y = 0; if (ev.jaxis.axis == 0) { x = ev.jaxis.value; } if (ev.jaxis.axis == 1) { y = ev.jaxis.value; } dc_mouseaxis1 += x; dc_mouseaxis2 += y; break; case EVENT_JOYBUTTONDOWN: if (ev.jbutton.button == JOY_START) cmd = CMD_MENU; if (ev.jbutton.button == JOY_RTRIGGER) __skbd_avail = !__skbd_avail; if (__skbd_avail && ev.jbutton.button == JOY_A) softkbddc_down(); if (__skbd_avail && ev.jbutton.button == JOY_B) __use_bg = !__use_bg; if (__skbd_avail) { cJoy = ev.jbutton.button & 0xffff; } else { switch (ev.jbutton.button) { case JOY_UP: dckbd_keydown(JOY1_UP); break; case JOY_DOWN: dckbd_keydown(JOY1_DOWN); break; case JOY_LEFT: dckbd_keydown(JOY1_LEFT); break; case JOY_RIGHT: dckbd_keydown(JOY1_RIGHT); break; case JOY_A: dckbd_keydown(JOY1_A); break; case JOY_B: dckbd_keydown(JOY1_B); break; case JOY_X: mousemng_buttonevent(MOUSEMNG_LEFTDOWN); break; case JOY_Y: mousemng_buttonevent(MOUSEMNG_RIGHTDOWN); break; } } break; case EVENT_JOYBUTTONUP: softkbddc_up(); if (__skbd_avail) { pJoy = 0; repeatTime = 0; } else { switch (ev.jbutton.button) { case JOY_UP: dckbd_keyup(JOY1_UP); break; case JOY_DOWN: dckbd_keyup(JOY1_DOWN); break; case JOY_LEFT: dckbd_keyup(JOY1_LEFT); break; case JOY_RIGHT: dckbd_keyup(JOY1_RIGHT); break; case JOY_A: dckbd_keyup(JOY1_A); break; case JOY_B: dckbd_keyup(JOY1_B); break; case JOY_X: mousemng_buttonevent(MOUSEMNG_LEFTUP); break; case JOY_Y: mousemng_buttonevent(MOUSEMNG_RIGHTUP); break; } } break; case EVENT_QUIT: __dc_avail = false; break; } } } if (__skbd_avail) { dc_joyinput = 0; int button = 0; if (cJoy) { repeatTime = Timer() + USEC_TO_TIMER(1000000/60*30); pJoy = cJoy; button = cJoy; } else if (repeatTime < Timer()) { button = pJoy; repeatTime = Timer() + USEC_TO_TIMER(1000000/60*10); } softkbddc_send(button); } else getJoyButton(JOYSTICKID1, &dc_joyinput); if (sys_updates & (SYS_UPDATECFG | SYS_UPDATEOSCFG)) { initsave(); sysmng_initialize(); } switch (cmd) { case CMD_MENU: sysmenu_menuopen(); break; } scrnmng_update(); softkbddc_draw(); ta_commit_frame(); return __dc_avail; }