int main() { char command[128] = { 0 }; clock_t flag = clock(), flagNow, flagDiff; srand((unsigned int)time(NULL)); newMatrix(&map, HEIGHT, WIDTH); newMatrix(&rendered, HEIGHT, WIDTH); sprintf(command, "mode con:cols=%d lines=%d", 2 * WIDTH + 5, 4+HEIGHT); system(command); SetConsoleOutputCP(949); initilize(); eventInitilize(); for (;;) { do { flagNow = clock(); flagDiff = ((flagNow - flag) * 1000 / CLOCKS_PER_SEC) % 10000; if (flagDiff > timeInterval) { eventLoop(); flag = flagNow; } } while (!kbhit()); eventKeyPress(getKeyInput()); switch (state) { case INIT: eventGameInit(); render(&rendered, &map); if(autoState) state = GAME; break; case GAME: break; case DEAD: eventGameOver(); if(autoState) state = INIT; break; default: break; } } }
bool Window::handleEvent() { SDL_Event event; while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: case SDL_MOUSEMOTION: input.setLastMouseEvent(Chrono::getCurMillis()); input.setMousePos(Vec2i(event.button.x, event.button.y)); } switch(event.type) { case SDL_QUIT: return false; case SDL_MOUSEBUTTONDOWN: handleMouseDown(event); break; case SDL_MOUSEBUTTONUP: { MouseButton b = Input::getMouseButton(event.button.button); input.setMouseState(b, false); eventMouseUp(event.button.x, event.button.y, b); break; } case SDL_MOUSEMOTION: input.setMouseState(MouseButton::LEFT, event.motion.state & SDL_BUTTON_LMASK); input.setMouseState(MouseButton::RIGHT, event.motion.state & SDL_BUTTON_RMASK); input.setMouseState(MouseButton::MIDDLE, event.motion.state & SDL_BUTTON_MMASK); eventMouseMove(event.motion.x, event.motion.y, input.getMouseState()); break; case SDL_KEYDOWN: /* handle ALT+Return */ if(event.key.keysym.sym == SDLK_RETURN && (event.key.keysym.mod & (KMOD_LALT | KMOD_RALT))) { toggleFullscreen(); } else { eventKeyDown(Key(event.key.keysym)); eventKeyPress(static_cast<char>(event.key.keysym.unicode)); } break; case SDL_KEYUP: eventKeyUp(Key(event.key.keysym)); break; case SDL_ACTIVEEVENT: if(event.active.state & SDL_APPACTIVE){ if(event.active.gain){ // got focus eventResize(ssRestored); }else{ // lost focus (minimized) eventResize(ssMinimized); } } break; } } return true; }