/* Main loop. 1) Stores key presses, and calls call-back functions for press/release if necessary. 2) Calls GameAction 3) Calls GameRender */ int BaseEngine::MainLoop(void) { SDL_Event event; int iKeyCode; // Main loop: loop until told not to while (m_iExitWithCode==-1) { // Render stuff m_iTick = SDL_GetTicks(); // Poll for events, and handle the ones we care about. while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: //printf("Key down: %d, '%c'\n", SymbolToKeycode(event.key.keysym.sym), event.key.keysym.sym); m_pKeyStatus[SymbolToKeycode(event.key.keysym.sym)] = true; KeyDown(event.key.keysym.sym); break; case SDL_KEYUP: //printf("Key up: %d, '%c'\n", SymbolToKeycode(event.key.keysym.sym), event.key.keysym.sym); m_pKeyStatus[SymbolToKeycode(event.key.keysym.sym)] = false; KeyUp(event.key.keysym.sym); break; case SDL_MOUSEMOTION: m_iCurrentMouseX = event.motion.x; m_iCurrentMouseY = event.motion.y; MouseMoved( event.motion.x, event.motion.y ); break; case SDL_MOUSEBUTTONDOWN: m_iMouseXClickedDown = event.button.x; m_iMouseYClickedDown = event.button.y; MouseDown( event.button.button, event.button.x, event.button.y ); break; case SDL_MOUSEBUTTONUP: m_iMouseXClickedUp = event.button.x; m_iMouseYClickedUp = event.button.y; MouseUp( event.button.button, event.button.x, event.button.y ); break; case SDL_QUIT: return 0; } } // Do any updates for movements GameAction(); // Render the screen GameRender(); } return 0; }
void TimerAction(HWND hWnd){ if(is_run){ if(GameAction()) InvalidateRect(hWnd,NULL,true); else KillTimer(hWnd,1); } }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd) { WNDCLASSEX wndClass = { 0 }; wndClass.cbSize = sizeof( WNDCLASSEX ) ; wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WndProc; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndClass.hCursor = LoadCursor( NULL, IDC_ARROW ); wndClass.hbrBackground = GetStockBrush(WHITE_BRUSH); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = WINDOW_CLASS; if( !RegisterClassEx( &wndClass ) ) return -1; g_hInst = hInstance; HWND hWnd = InitInstance(hInstance, nShowCmd); if ( !hWnd) return -1; MSG msg = { 0 }; DWORD dwTick, dwPreTick = GetTickCount(); if ( !GameInit(hWnd)) return -1; while( msg.message != WM_QUIT ) { if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { dwTick = GetTickCount(); if (dwTick - dwPreTick > 40) { if (GameAction(hWnd)) GamePaint(hWnd); dwPreTick = GetTickCount(); } } } UnregisterClass(WINDOW_CLASS, wndClass.hInstance); return 0; }
GameAction game_action(GameState* gs, GameInst* origin, GameAction::action_t action, int use_id, int action_x, int action_y, int use_id2) { return GameAction(origin->id, action, gs->frame(), gs->get_level()->id(), use_id, action_x, action_y, use_id2); }
/* Main loop. 1) Stores key presses, and calls call-back functions for press/release if necessary. 2) Calls GameAction 3) Calls GameRender */ int BaseEngine::MainLoop(void) { SDL_Event event; int iKeyCode; // Main loop: loop until told not to while (m_iExitWithCode==-1) { // Render stuff m_iTick = SDL_GetTicks(); // Poll for events, and handle the ones we care about. while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: iKeyCode = event.key.keysym.sym; if ( (iKeyCode >= SDLK_FIRST) && (iKeyCode <= SDLK_LAST) ) m_pKeyStatus[iKeyCode-SDLK_FIRST] = 1; KeyDown(iKeyCode); break; case SDL_KEYUP: iKeyCode = event.key.keysym.sym; if ( (iKeyCode >= SDLK_FIRST) && (iKeyCode <= SDLK_LAST) ) { m_pKeyStatus[iKeyCode-SDLK_FIRST] = 0; } KeyUp(iKeyCode); break; case SDL_MOUSEMOTION: m_iCurrentMouseX = event.motion.x; m_iCurrentMouseY = event.motion.y; MouseMoved( event.motion.x, event.motion.y ); break; case SDL_MOUSEBUTTONDOWN: m_iMouseXClickedDown = event.button.x; m_iMouseYClickedDown = event.button.y; MouseDown( event.button.button, event.button.x, event.button.y ); break; case SDL_MOUSEBUTTONUP: m_iMouseXClickedUp = event.button.x; m_iMouseYClickedUp = event.button.y; MouseUp( event.button.button, event.button.x, event.button.y ); break; case SDL_QUIT: return 0; } } // Do any updates for movements GameAction(); // Render the screen GameRender(); } return 0; }
void PlayerInst::enqueue_io_equipment_actions(GameState* gs, bool do_stopaction) { GameView& view = gs->view(); bool mouse_within = gs->mouse_x() < gs->view().width; int rmx = view.x + gs->mouse_x(), rmy = view.y + gs->mouse_y(); int level = gs->get_level()->id(); int frame = gs->frame(); bool item_used = false; IOController& io = gs->io_controller(); Pos p(gs->mouse_x() + view.x, gs->mouse_y() + view.y); obj_id target = this->current_target; GameInst* targetted = gs->get_instance(target); if (targetted) p = Pos(targetted->x, targetted->y); // We may have already used an item eg due to auto-use of items bool used_item = false; //Item use for (int i = 0; i < 9 && !used_item; i++) { if (io.query_event(IOEvent(IOEvent::USE_ITEM_N, i))) { if (inventory().get(i).amount() > 0) { item_used = true; queued_actions.push_back( GameAction(id, GameAction::USE_ITEM, frame, level, i, p.x, p.y)); } } } if (!used_item && gs->game_settings().autouse_health_potions && core_stats().hp < AUTOUSE_HEALTH_POTION_THRESHOLD) { int item_slot = inventory().find_slot( get_item_by_name("Health Potion")); if (item_slot > -1) { queued_actions.push_back( game_action(gs, this, GameAction::USE_ITEM, item_slot)); used_item = true; } } //Item pickup GameInst* inst = NULL; if (cooldowns().can_pickup() && gs->object_radius_test(this, &inst, 1, &item_colfilter)) { ItemInst* iteminst = (ItemInst*)inst; Item& item = iteminst->item_type(); bool was_dropper = iteminst->last_held_by() == id; bool dropper_autopickup = iteminst->autopickup_held(); bool autopickup = (item.is_normal_item() && !was_dropper && !dropper_autopickup) || (was_dropper && dropper_autopickup); bool wieldable_projectile = projectile_should_autowield(equipment(), item, this->last_chosen_weaponclass); bool pickup_io = gs->key_down_state(SDLK_LSHIFT) || gs->key_down_state(SDLK_RSHIFT); if (do_stopaction || wieldable_projectile || pickup_io || autopickup) queued_actions.push_back( GameAction(id, GameAction::PICKUP_ITEM, frame, level, iteminst->id)); } }