SDL_Scancode GetKey(EventHandlers *handlers) { SDL_Scancode k = SDL_SCANCODE_UNKNOWN; do { EventPoll(handlers, SDL_GetTicks()); k = KeyGetPressed(&handlers->keyboard); } while (k == SDL_SCANCODE_UNKNOWN); return k; }
SDL_Scancode EventWaitKeyOrText(EventHandlers *handlers) { SDL_Scancode k = SDL_SCANCODE_UNKNOWN; do { EventPoll(handlers, SDL_GetTicks()); k = KeyGetPressed(&handlers->keyboard); } while (k == SDL_SCANCODE_UNKNOWN && handlers->keyboard.Typed[0] == '\0'); return k; }
static void EditCampaign(void) { int xc = 0, yc = 0; int xcOld, ycOld; Mission scrap; memset(&scrap, 0, sizeof scrap); gCampaign.seed = 0; Setup(1); SDL_EnableKeyRepeat(0, 0); Uint32 ticksNow = SDL_GetTicks(); sTicksElapsed = 0; ticksAutosave = AUTOSAVE_INTERVAL_SECONDS * 1000; for (;;) { Uint32 ticksThen = ticksNow; ticksNow = SDL_GetTicks(); sTicksElapsed += ticksNow - ticksThen; if (sTicksElapsed < 1000 / FPS_FRAMELIMIT * 2) { SDL_Delay(1); debug(D_VERBOSE, "Delaying 1 ticksNow %u elapsed %u\n", ticksNow, sTicksElapsed); continue; } debug(D_MAX, "Polling for input\n"); EventPoll(&gEventHandlers, SDL_GetTicks()); int c = KeyGetPressed(&gEventHandlers.keyboard); int m = MouseGetPressed(&gEventHandlers.mouse); debug(D_MAX, "Handling input\n"); HandleInputResult result = HandleInput( c, m, &xc, &yc, &xcOld, &ycOld, &scrap); if (result.Done) { break; } if (result.Redraw || result.RemakeBg || sJustLoaded) { sJustLoaded = false; debug(D_MAX, "Drawing UI\n"); Display(&gGraphicsDevice, yc, result); if (result.WillDisplayAutomap) { GetKey(&gEventHandlers); } } debug(D_MAX, "End loop\n"); SDL_Delay(10); } }
int main(int argc, char *argv[]) { UNUSED(argc); UNUSED(argv); if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) != 0) { fprintf(stderr, "Could not initialise SDL: %s\n", SDL_GetError()); return -1; } if (SDLNet_Init() == -1) { fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); } if (!SDL_SetVideoMode(320, 200, 0, 0)) { fprintf(stderr, "Could not set video mode: %s\n", SDL_GetError()); SDL_Quit(); exit(-1); } ConfigLoadDefault(&gConfig); ConfigLoad(&gConfig, GetConfigFilePath(CONFIG_FILE)); EventInit(&gEventHandlers, NULL, false); NetInputClient client; NetInputClientInit(&client); NetInputClientConnect(&client, 0x7F000001); // localhost printf("Press esc to exit\n"); Uint32 ticksNow = SDL_GetTicks(); Uint32 ticksElapsed = 0; for (;;) { Uint32 ticksThen = ticksNow; ticksNow = SDL_GetTicks(); ticksElapsed += ticksNow - ticksThen; if (ticksElapsed < 1000 / FPS_FRAMELIMIT) { SDL_Delay(1); debug(D_VERBOSE, "Delaying 1 ticksNow %u elapsed %u\n", ticksNow, ticksElapsed); continue; } EventPoll(&gEventHandlers, SDL_GetTicks()); int cmd = GetOnePlayerCmd( &gEventHandlers, &gConfig.Input.PlayerKeys[0], false, INPUT_DEVICE_KEYBOARD, 0); if (cmd) { printf("Sending %s + %s\n", CmdStr(cmd & (CMD_LEFT | CMD_RIGHT | CMD_UP | CMD_DOWN)), CmdStr(cmd & (CMD_BUTTON1 | CMD_BUTTON2 | CMD_BUTTON3 | CMD_BUTTON4 | CMD_ESC))); NetInputClientSend(&client, cmd); } // Check keyboard escape if (KeyIsPressed(&gEventHandlers.keyboard, SDLK_ESCAPE)) { break; } ticksElapsed -= 1000 / FPS_FRAMELIMIT; } NetInputClientTerminate(&client); EventTerminate(&gEventHandlers); SDLNet_Quit(); SDL_Quit(); return 0; }