/** * @return whether window initialization succeeded * @param title char* string with window title * * Initializes the game window */ bool SpringApp::InitWindow(const char* title) { unsigned int sdlInitFlags = SDL_INIT_VIDEO | SDL_INIT_TIMER; #ifdef WIN32 // the crash reporter should be catching the errors sdlInitFlags |= SDL_INIT_NOPARACHUTE; #endif if ((SDL_Init(sdlInitFlags) == -1)) { LOG_L(L_FATAL, "Could not initialize SDL: %s", SDL_GetError()); return false; } PrintAvailableResolutions(); WindowManagerHelper::SetCaption(title); if (!SetSDLVideoMode()) { LOG_L(L_FATAL, "Failed to set SDL video mode: %s", SDL_GetError()); return false; } RestoreWindowPosition(); if (cmdline->IsSet("minimise")) { globalRendering->active = false; SDL_WM_IconifyWindow(); } glClearColor(0.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SDL_GL_SwapBuffers(); return true; }
/** * @return whether window initialization succeeded * @param title char* string with window title * * Initializes the game window */ bool SpringApp::InitWindow (const char* title) { if ((SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)) { handleerror(NULL,"Could not initialize SDL.","ERROR",MBF_OK|MBF_EXCL); return false; } // Sets window manager properties SDL_WM_SetIcon(SDL_LoadBMP("spring.bmp"),NULL); SDL_WM_SetCaption(title, title); return SetSDLVideoMode (); }
/** * @return whether window initialization succeeded * @param title char* string with window title * * Initializes the game window */ bool SpringApp::InitWindow (const char* title) { unsigned int sdlInitFlags = SDL_INIT_VIDEO | SDL_INIT_TIMER; #ifdef WIN32 // the crash reporter should be catching the errors sdlInitFlags |= SDL_INIT_NOPARACHUTE; #endif if ((SDL_Init(sdlInitFlags) == -1)) { handleerror(NULL,"Could not initialize SDL.","ERROR",MBF_OK|MBF_EXCL); return false; } // Sets window manager properties SDL_WM_SetIcon(SDL_LoadBMP("spring.bmp"),NULL); SDL_WM_SetCaption(title, title); if (!SetSDLVideoMode ()) return false; return true; }
/** * @param argc argument count * @param argv array of argument strings * * Executes the application * (contains main game loop) */ int SpringApp::Run (int argc, char *argv[]) { INIT_SYNCIFY; CheckCmdLineFile (argc, argv); cmdline = BaseCmd::initialize(argc,argv); if (!Initialize ()) return -1; #ifdef WIN32 SDL_EventState (SDL_SYSWMEVENT, SDL_ENABLE); #endif SDL_Event event; bool done=false; std::map<int, int> toUnicode; // maps keysym.sym to keysym.unicode Uint8 scrollWheelSpeed = configHandler.GetInt("ScrollWheelSpeed",25); while (!done) { ENTER_UNSYNCED; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_VIDEORESIZE: screenWidth = event.resize.w; screenHeight = event.resize.h; #ifndef WIN32 // HACK We don't want to break resizing on windows (again?), // so someone should test this very well before enabling it. SetSDLVideoMode(); #endif InitOpenGL(); break; case SDL_QUIT: done = true; break; case SDL_MOUSEMOTION: if(mouse) mouse->MouseMove(event.motion.x,event.motion.y); break; case SDL_MOUSEBUTTONDOWN: if (mouse) { if (event.button.button == SDL_BUTTON_WHEELUP) mouse->currentCamController->MouseWheelMove(scrollWheelSpeed); else if (event.button.button == SDL_BUTTON_WHEELDOWN) mouse->currentCamController->MouseWheelMove(-scrollWheelSpeed); else mouse->MousePress(event.button.x,event.button.y,event.button.button); } break; case SDL_MOUSEBUTTONUP: if (mouse) mouse->MouseRelease(event.button.x,event.button.y,event.button.button); break; case SDL_KEYDOWN: { int i = event.key.keysym.sym; UpdateSDLKeys (); if(activeController) { int j = tolower(event.key.keysym.unicode); // Don't translate 0-9 because with ctrl that maps to weird unicode characters (eg same as esc) if (j > SDLK_FIRST && j <= SDLK_DELETE && (i < SDLK_0 || i > SDLK_9)) { // With control+letter, the unicode field is 1-26, so convert it back to ascii. if (keys[SDLK_LCTRL] && j >= 1 && j <= 26) j += SDLK_a - 1; // Store the unicode value of this sym because SDL is too stupid // to do Unicode translation for SDL_KEYUP events. toUnicode[i] = j; i = j; } else if (i == SDLK_RSHIFT) i = SDLK_LSHIFT; else if (i == SDLK_RCTRL) i = SDLK_LCTRL; else if (i == SDLK_RMETA) i = SDLK_LMETA; else if (i == SDLK_RALT) i = SDLK_LALT; activeController->KeyPressed(i,1); #ifndef NEW_GUI if(activeController->userWriting) { i = event.key.keysym.unicode; if (i >= SDLK_SPACE && i <= SDLK_DELETE) if(activeController->ignoreNextChar || activeController->ignoreChar==char(i)) activeController->ignoreNextChar=false; else activeController->userInput+=char(i); } #endif } #ifdef NEW_GUI i = event.key.keysym.unicode; if (i > SDLK_FIRST && i <= SDLK_DELETE) /* HACK */ GUIcontroller::Character(char(i)); #endif break; } case SDL_KEYUP: { int i = event.key.keysym.sym; UpdateSDLKeys(); if (activeController) { // Retrieve the Unicode value stored in the KEYDOWN event (if any). std::map<int, int>::const_iterator j = toUnicode.find(i); if (j != toUnicode.end()) i = j->second; else if (i == SDLK_RSHIFT) i = SDLK_LSHIFT; else if (i == SDLK_RCTRL) i = SDLK_LCTRL; else if (i == SDLK_RMETA) i = SDLK_LMETA; else if (i == SDLK_RALT) i = SDLK_LALT; activeController->KeyReleased(i); } break; } #ifdef WIN32 case SDL_SYSWMEVENT: { SDL_SysWMmsg *msg = event.syswm.msg; if (msg->msg == 0x020B) { // WM_XBUTTONDOWN, beats me why it isn't defined by default if (msg->wParam & 0x20) // MK_XBUTTON1 mouse->MousePress (LOWORD(msg->lParam),HIWORD(msg->lParam), 4); if (msg->wParam & 0x40) // MK_XBUTTON2 mouse->MousePress (LOWORD(msg->lParam),HIWORD(msg->lParam), 5); } break; } #endif } } if (globalQuit) break; if (!Draw() && active) break; } ENTER_MIXED; // Shutdown Shutdown(); return 0; }
/** * @param argc argument count * @param argv array of argument strings * * Executes the application * (contains main game loop) */ int SpringApp::Run (int argc, char *argv[]) { INIT_SYNCIFY; CheckCmdLineFile (argc, argv); cmdline = BaseCmd::initialize(argc,argv); if (!Initialize ()) return -1; #ifdef WIN32 SDL_EventState (SDL_SYSWMEVENT, SDL_ENABLE); #endif SDL_Event event; bool done = false; while (!done) { ENTER_UNSYNCED; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_VIDEORESIZE: { screenWidth = event.resize.w; screenHeight = event.resize.h; #ifndef WIN32 // HACK We don't want to break resizing on windows (again?), // so someone should test this very well before enabling it. SetSDLVideoMode(); #endif InitOpenGL(); activeController->ResizeEvent(); break; } case SDL_VIDEOEXPOSE: { // re-initialize the stencil glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); SDL_GL_SwapBuffers(); glClear(GL_STENCIL_BUFFER_BIT); SDL_GL_SwapBuffers(); SetupViewportGeometry(); break; } case SDL_QUIT: { done = true; break; } case SDL_ACTIVEEVENT: { if (event.active.state & SDL_APPACTIVE) { gu->active = !!event.active.gain; } break; } case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: case SDL_SYSWMEVENT: { mouseInput->HandleSDLMouseEvent (event); break; } case SDL_KEYDOWN: { int i = event.key.keysym.sym; const bool isRepeat = !!keys[i]; UpdateSDLKeys (); if (activeController) { if (i <= SDLK_DELETE) { i = tolower(i); } else if (i == SDLK_RSHIFT) { i = SDLK_LSHIFT; } else if (i == SDLK_RCTRL) { i = SDLK_LCTRL; } else if (i == SDLK_RMETA) { i = SDLK_LMETA; } else if (i == SDLK_RALT) { i = SDLK_LALT; } if (keyBindings) { const int fakeMetaKey = keyBindings->GetFakeMetaKey(); if (fakeMetaKey >= 0) { keys[SDLK_LMETA] |= keys[fakeMetaKey]; } } activeController->KeyPressed(i,isRepeat); #ifndef NEW_GUI if (activeController->userWriting){ // use unicode for printed characters i = event.key.keysym.unicode; if ((i >= SDLK_SPACE) && (i <= SDLK_DELETE)) if (activeController->ignoreNextChar || activeController->ignoreChar == char(i)) { activeController->ignoreNextChar = false; } else { activeController->userInput += char(i); } } #endif } break; } case SDL_KEYUP: { int i = event.key.keysym.sym; UpdateSDLKeys(); if (activeController) { if (i <= SDLK_DELETE) { i = tolower(i); } else if (i == SDLK_RSHIFT) { i = SDLK_LSHIFT; } else if (i == SDLK_RCTRL) { i = SDLK_LCTRL; } else if (i == SDLK_RMETA) { i = SDLK_LMETA; } else if (i == SDLK_RALT) { i = SDLK_LALT; } if (keyBindings) { const int fakeMetaKey = keyBindings->GetFakeMetaKey(); if (fakeMetaKey >= 0) { keys[SDLK_LMETA] |= keys[fakeMetaKey]; } } activeController->KeyReleased(i); } break; } } } if (globalQuit) break; if (!Update()) break; } ENTER_MIXED; // Shutdown Shutdown(); return 0; }
bool SpringApp::MainEventHandler(const SDL_Event& event) { switch (event.type) { case SDL_VIDEORESIZE: { GML_MSTMUTEX_LOCK(sim); // MainEventHandler Watchdog::ClearTimer(WDT_MAIN, true); globalRendering->viewSizeX = event.resize.w; globalRendering->viewSizeY = event.resize.h; #ifndef WIN32 // HACK We don't want to break resizing on windows (again?), // so someone should test this very well before enabling it. SetSDLVideoMode(); #endif InitOpenGL(); activeController->ResizeEvent(); break; } case SDL_VIDEOEXPOSE: { GML_MSTMUTEX_LOCK(sim); // MainEventHandler Watchdog::ClearTimer(WDT_MAIN, true); // re-initialize the stencil glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); SDL_GL_SwapBuffers(); glClear(GL_STENCIL_BUFFER_BIT); SDL_GL_SwapBuffers(); SetupViewportGeometry(); break; } case SDL_QUIT: { gu->globalQuit = true; break; } case SDL_ACTIVEEVENT: { Watchdog::ClearTimer(WDT_MAIN, true); //! deactivate sounds and other if (event.active.state & (SDL_APPACTIVE | (globalRendering->fullScreen ? SDL_APPINPUTFOCUS : 0))) { globalRendering->active = !!event.active.gain; if (ISound::IsInitialized()) { sound->Iconified(!event.active.gain); } } //! update keydown table if (event.active.gain) { keyInput->Update(event.key.keysym.unicode, ((keyBindings != NULL)? keyBindings->GetFakeMetaKey(): -1)); } //! unlock mouse if (mouse && mouse->locked) { mouse->ToggleMiddleClickScroll(); } //! release all keyboard keys if ((event.active.state & (SDL_APPACTIVE | SDL_APPINPUTFOCUS)) && !event.active.gain) { for (boost::uint16_t i = 1; i < SDLK_LAST; ++i) { if (keyInput->IsKeyPressed(i)) { SDL_Event event; event.type = event.key.type = SDL_KEYUP; event.key.state = SDL_RELEASED; event.key.keysym.sym = (SDLKey)i; event.key.keysym.unicode = i; //event.keysym.mod =; //event.keysym.scancode =; SDL_PushEvent(&event); } } } //! simulate mouse release to prevent hung buttons if ((event.active.state & (SDL_APPACTIVE | SDL_APPMOUSEFOCUS)) && !event.active.gain) { for (int i = 1; i <= NUM_BUTTONS; ++i) { if (mouse && mouse->buttons[i].pressed) { SDL_Event event; event.type = event.button.type = SDL_MOUSEBUTTONUP; event.button.state = SDL_RELEASED; event.button.which = 0; event.button.button = i; event.button.x = -1; event.button.y = -1; SDL_PushEvent(&event); } } //! and make sure to un-capture mouse if(SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON) SDL_WM_GrabInput(SDL_GRAB_OFF); } break; } case SDL_KEYDOWN: { const boost::uint16_t sym = keyInput->GetNormalizedKeySymbol(event.key.keysym.sym); const bool isRepeat = !!keyInput->GetKeyState(sym); keyInput->Update(event.key.keysym.unicode, ((keyBindings != NULL)? keyBindings->GetFakeMetaKey(): -1)); if (activeController) { activeController->KeyPressed(sym, isRepeat); if (activeController->userWriting){ // use unicode for printed characters const boost::uint16_t usym = keyInput->GetCurrentKeyUnicodeChar(); if ((usym >= SDLK_SPACE) && (usym <= 255)) { CGameController* ac = activeController; if (ac->ignoreNextChar || (ac->ignoreChar == (char)usym)) { ac->ignoreNextChar = false; } else { if (usym < 255 && (!isRepeat || ac->userInput.length() > 0)) { const int len = (int)ac->userInput.length(); const char str[2] = { (char)usym, 0 }; ac->writingPos = std::max(0, std::min(len, ac->writingPos)); ac->userInput.insert(ac->writingPos, str); ac->writingPos++; } } } } activeController->ignoreNextChar = false; } break; } case SDL_KEYUP: { keyInput->Update(event.key.keysym.unicode, ((keyBindings != NULL)? keyBindings->GetFakeMetaKey(): -1)); if (activeController) { activeController->KeyReleased(keyInput->GetNormalizedKeySymbol(event.key.keysym.sym)); } break; } default: break; } return false; }