From 1c7d28a2fc93699ab955ea66203d0c36bba9d977 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Fri, 18 Nov 2016 17:51:39 +0800 Subject: [PATCH] Migrated to SDL2 Option "nomousegrab" seems broken We might want to go back to keycode instead of scancode for some stuff (debug keys maybe, things like q or g which have a meaning linked to the letter itself) The resolutions detection code seems buggy and should be redone While editing a new user name or any other field, events like SDL_QUIT are ignored (will open an issue about that) --- CMakeLists.txt | 56 +-------- Source/Game.cpp | 82 ++++++------- Source/Game.h | 18 ++- Source/GameDraw.cpp | 16 +-- Source/GameTick.cpp | 249 ++++++++++++++++++-------------------- Source/Globals.cpp | 2 +- Source/Input.cpp | 38 +++--- Source/Input.h | 5 +- Source/OpenGL_Windows.cpp | 184 ++++++++++++++++------------ Source/Settings.cpp | 75 +++++------- 10 files changed, 337 insertions(+), 388 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30aef8c..81a7d67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ project(lugaru) cmake_minimum_required(VERSION 3.5) +INCLUDE(FindPkgConfig) + set(SRCDIR "${CMAKE_CURRENT_SOURCE_DIR}/Source") set(DEPDIR "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies") @@ -38,7 +40,6 @@ endif(APPLE) if(APPLE) # Save our sanity; Set all available libraries to internal by default - set(LUGARU_FORCE_INTERNAL_SDL True) set(LUGARU_FORCE_INTERNAL_GLU True) set(LUGARU_FORCE_INTERNAL_JPEG True) set(LUGARU_FORCE_INTERNAL_PNG True) @@ -206,49 +207,7 @@ if (NOT OPENAL_FOUND OR LUGARU_FORCE_INTERNAL_OPENAL) endif (NOT LUGARU_HAS_INTERNAL_OPENAL) endif (NOT OPENAL_FOUND OR LUGARU_FORCE_INTERNAL_OPENAL) -option (LUGARU_FORCE_INTERNAL_SDL "Force internal libSDL, even if there's a system version" False) -if (NOT LUGARU_FORCE_INTERNAL_SDL) - find_package(SDL) -else(NOT LUGARU_FORCE_INTERNAL_SDL) - set(SDL_FOUND False) -endif (NOT LUGARU_FORCE_INTERNAL_SDL) - -if (NOT SDL_FOUND) - message(STATUS "Using internal copy of SDL") - set(LUGARU_MISSING_DEPS "${LUGARU_MISSING_DEPS} SDL") - set(SDLDIR "${DEPDIR}/SDL12") - set(SDL_INCLUDE_DIR "${SDLDIR}/include") - set(SDL_LIBRARY "") - if (APPLE) - set(LUGARU_HAS_INTERNAL_SDL True) - set(SDL_LIBRARY - ${SDLDIR}/lib/macosx/libSDL-1.2.0.dylib - ${SDLDIR}/lib/macosx/libSDLmain-osx.a - ) - endif (APPLE) - - if (WIN32) - set(LUGARU_HAS_INTERNAL_SDL True) - if (MSVC) - set(SDL_LIBRARY - ${SDLDIR}/lib/win32/msvc2008/SDLmain.lib - ${SDLDIR}/lib/win32/msvc2008/SDL.lib - ) - endif (MSVC) - if (MINGW) - set(SDL_LIBRARY - "mingw32" - ${SDLDIR}/lib/win32/mingw/libSDLmain.a - ${SDLDIR}/lib/win32/mingw/libSDL.dll.a - "-mwindows" - ) - endif (MINGW) - endif (WIN32) - - if (NOT LUGARU_HAS_INTERNAL_SDL) - message(ERROR "We don't have a prebuilt SDL for this platform.") - endif (NOT LUGARU_HAS_INTERNAL_SDL) -endif (NOT SDL_FOUND) +PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) option (LUGARU_FORCE_INTERNAL_PNG "Force internal libPNG, even if there's a system version" False) if (NOT LUGARU_FORCE_INTERNAL_PNG) @@ -453,13 +412,13 @@ include_directories( ${ZLIB_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLU_INCLUDE_DIR} - ${SDL_INCLUDE_DIR} + ${SDL2_INCLUDE_DIRS} ${VORBISFILE_INCLUDE_DIR} ${OGG_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/Source ) -set(LUGARU_LIBS ${OPENAL_LIBRARY} ${PNG_LIBRARY} ${JPEG_LIBRARY} ${ZLIB_LIBRARIES} ${SDL_LIBRARY} ${OPENGL_glu_LIBRARY} ${VORBISFILE_LIBRARY} ${OGG_LIBRARY} ${PLATFORM_LIBS}) +set(LUGARU_LIBS ${OPENAL_LIBRARY} ${PNG_LIBRARY} ${JPEG_LIBRARY} ${ZLIB_LIBRARIES} ${SDL2_LIBRARIES} ${OPENGL_glu_LIBRARY} ${VORBISFILE_LIBRARY} ${OGG_LIBRARY} ${PLATFORM_LIBS}) add_executable(lugaru ${LUGARU_SRCS} ${LUGARU_H}) @@ -485,11 +444,9 @@ endif(APPLE) if(WIN32) install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/lugaru.exe DESTINATION ${CMAKE_INSTALL_PREFIX}) if(MSVC) - install(FILES ${SDLDIR}/lib/win32/msvc2008/SDL.dll DESTINATION ${CMAKE_INSTALL_PREFIX}) install(FILES ${OPENALDIR}/lib/win32/msvc2008/OpenAL32.dll DESTINATION ${CMAKE_INSTALL_PREFIX}) endif(MSVC) if(MINGW) - install(FILES ${SDLDIR}/lib/win32/mingw/SDL.dll DESTINATION ${CMAKE_INSTALL_PREFIX}) install(FILES ${OPENALDIR}/lib/win32/mingw/OpenAL32.dll DESTINATION ${CMAKE_INSTALL_PREFIX}) endif(MINGW) else(WIN32) @@ -499,9 +456,6 @@ else(WIN32) install(FILES ${SRCDIR}/mac-res/Info.plist DESTINATION ${APPS_ROOT}/Contents) endif(APPLE) install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/lugaru DESTINATION ${CMAKE_INSTALL_PREFIX}) - if(APPLE AND LUGARU_HAS_INTERNAL_SDL) - install(FILES ${SDLDIR}/lib/macosx/libSDL-1.2.0.dylib DESTINATION ${CMAKE_INSTALL_PREFIX}) - endif(APPLE AND LUGARU_HAS_INTERNAL_SDL) endif(WIN32) if(NOT APPLE) diff --git a/Source/Game.cpp b/Source/Game.cpp index 7e671e0..1601442 100644 --- a/Source/Game.cpp +++ b/Source/Game.cpp @@ -123,11 +123,9 @@ bool scoreadded = 0; int numchallengelevels = 0; bool console = false; -char consoletext[15][256] = {}; -int consolechars[15] = {}; +std::string consoletext[15] = {}; bool chatting = 0; -char displaytext[15][256] = {}; -int displaychars[15] = {}; +std::string displaytext[15] = {}; float displaytime[15] = {}; float displayblinkdelay = 0; bool displayblink = 0; @@ -154,120 +152,110 @@ void Game::fireSound(int sound) emit_sound_at(sound); } -void Game::inputText(char* str, int* charselected, int* nb_chars) +void Game::inputText(string& str, int* charselected) { SDL_Event evenement; if (!waiting) { + SDL_StartTextInput(); waiting = true; - SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); - SDL_EnableUNICODE(true); } while (SDL_PollEvent(&evenement)) { - switch (evenement.type) { + case SDL_TEXTEDITING: + /* FIXME - We should handle this for complete input method support */ + break; + case SDL_TEXTINPUT: + str.insert(*charselected, evenement.text.text); + (*charselected) += strlen(evenement.text.text); + break; case SDL_KEYDOWN: if (evenement.key.keysym.sym == SDLK_ESCAPE) { - for (int i = 0; i < 255; i++) - str[i] = 0; - *nb_chars = 0; + str.clear(); *charselected = 0; waiting = false; } else if (evenement.key.keysym.sym == SDLK_BACKSPACE) { - if ((*charselected) != 0) { - for (int i = (*charselected) - 1; i < 255; i++) - str[i] = str[i + 1]; - str[255] = 0; + if ((*charselected) > 0) { (*charselected)--; - (*nb_chars)--; + str.erase(*charselected, 1); } } else if (evenement.key.keysym.sym == SDLK_DELETE) { - if ((*charselected) < (*nb_chars)) { - for (int i = (*charselected); i < 255; i++) - str[i] = str[i + 1]; - str[255] = 0; - (*nb_chars)--; + if ((*charselected) < str.size()) { + str.erase(*charselected, 1); } } else if (evenement.key.keysym.sym == SDLK_HOME) { (*charselected) = 0; } else if (evenement.key.keysym.sym == SDLK_END) { - (*charselected) = (*nb_chars); + (*charselected) = str.size(); } else if (evenement.key.keysym.sym == SDLK_LEFT) { if ((*charselected) != 0) (*charselected)--; } else if (evenement.key.keysym.sym == SDLK_RIGHT) { - if ((*charselected) < (*nb_chars)) + if ((*charselected) < str.size()) (*charselected)++; } else if (evenement.key.keysym.sym == SDLK_RETURN) { waiting = false; - } else if (evenement.key.keysym.unicode >= 32 && evenement.key.keysym.unicode < 127 && (*nb_chars) < 60) { - for (int i = 255; i >= (*charselected) + 1; i--) - str[i] = str[i - 1]; - str[*charselected] = evenement.key.keysym.unicode; - (*charselected)++; - (*nb_chars)++; } break; } } if (!waiting) { - SDL_EnableKeyRepeat(0, 0); // disable key repeat - SDL_EnableUNICODE(false); + SDL_StopTextInput(); } } int setKeySelected_thread(void* data) { using namespace Game; - int keycode = -1; + int scancode = -1; SDL_Event evenement; - while (keycode == -1) { + while (scancode == -1) { SDL_WaitEvent(&evenement); switch (evenement.type) { case SDL_KEYDOWN: - keycode = evenement.key.keysym.sym; + scancode = evenement.key.keysym.scancode; break; case SDL_MOUSEBUTTONDOWN: - keycode = SDLK_LAST + evenement.button.button; + scancode = SDL_NUM_SCANCODES + evenement.button.button; break; default: break; } } - if (keycode != SDLK_ESCAPE) { + if (scancode != SDL_SCANCODE_ESCAPE) { fireSound(); switch (keyselect) { case 0: - forwardkey = keycode; + forwardkey = scancode; break; case 1: - backkey = keycode; + backkey = scancode; break; case 2: - leftkey = keycode; + leftkey = scancode; break; case 3: - rightkey = keycode; + rightkey = scancode; break; case 4: - crouchkey = keycode; + crouchkey = scancode; break; case 5: - jumpkey = keycode; + jumpkey = scancode; break; case 6: - drawkey = keycode; + drawkey = scancode; break; case 7: - throwkey = keycode; + throwkey = scancode; break; case 8: - attackkey = keycode; + attackkey = scancode; break; case 9: - consolekey = keycode; + consolekey = scancode; break; default: break; @@ -283,7 +271,7 @@ void Game::setKeySelected() { waiting = true; printf("launch thread\n"); - SDL_Thread* thread = SDL_CreateThread(setKeySelected_thread, NULL); + SDL_Thread* thread = SDL_CreateThread(setKeySelected_thread, NULL, NULL); if ( thread == NULL ) { fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError()); waiting = false; diff --git a/Source/Game.h b/Source/Game.h index 87c6c2e..f5b4207 100644 --- a/Source/Game.h +++ b/Source/Game.h @@ -135,11 +135,9 @@ extern bool scoreadded; extern int numchallengelevels; extern bool console; -extern char consoletext[15][256]; -extern int consolechars[15]; +extern std::string consoletext[15]; extern bool chatting; -extern char displaytext[15][256]; -extern int displaychars[15]; +extern std::string displaytext[15]; extern float displaytime[15]; extern float displayblinkdelay; extern bool displayblink; @@ -178,7 +176,7 @@ int checkcollide(XYZ startpoint, XYZ endpoint, int what); void fireSound(int sound = fireendsound); void setKeySelected(); -void inputText(char* str, int* charselected, int* nb_chars); +void inputText(std::string& str, int* charselected); void flash(); } @@ -190,7 +188,15 @@ void flash(); static __forceinline void swap_gl_buffers(void) { - SDL_GL_SwapBuffers(); + extern SDL_Window *sdlwindow; + SDL_GL_SwapWindow(sdlwindow); + + // try to limit this to 60fps, even if vsync fails. + Uint32 now; + static Uint32 frameticks = 0; + const Uint32 endticks = (frameticks + 16); + while ((now = SDL_GetTicks()) < endticks) { /* spin. */ } + frameticks = now; } extern "C" { diff --git a/Source/GameDraw.cpp b/Source/GameDraw.cpp index c7b0ca1..76eadb1 100644 --- a/Source/GameDraw.cpp +++ b/Source/GameDraw.cpp @@ -1448,12 +1448,10 @@ int Game::DrawGLScene(StereoSide side) } for (i = 0; i < 15; i++) if ((i != 0 || chatting) && displaytime[i] < 4) - for (j = 0; j < displaychars[i]; j++) { + for (j = 0; j < displaytext[i].size(); j++) { glColor4f(1, 1, 1, 4 - displaytime[i]); - if (j < displaychars[i]) { - sprintf (string, "%c", displaytext[i][j]); - text->glPrint(30 + j * 10, 30 + i * 20 + (screenheight - 330), string, 0, 1, screenwidth, screenheight); - } + sprintf (string, "%c", displaytext[i][j]); + text->glPrint(30 + j * 10, 30 + i * 20 + (screenheight - 330), string, 0, 1, screenwidth, screenheight); } } @@ -2038,12 +2036,10 @@ int Game::DrawGLScene(StereoSide side) text->glPrint(30 + (float)(consoleselected) * 10 - offset * 10, 30, string, 0, 1, 1024, 768); } for (i = 0; i < 15; i++) - for (j = 0; j < consolechars[i]; j++) { + for (j = 0; j < consoletext[i].size(); j++) { glColor4f(1, 1, 1, 1 - (float)(i) / 16); - if (j < consolechars[i]) { - sprintf (string, "%c", consoletext[i][j]); - text->glPrint(30 + j * 10 - offset * 10, 30 + i * 20, string, 0, 1, 1024, 768); - } + sprintf (string, "%c", consoletext[i][j]); + text->glPrint(30 + j * 10 - offset * 10, 30 + i * 20, string, 0, 1, 1024, 768); } } } diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index 84a10da..122471a 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -2890,7 +2890,7 @@ void doDebugKeys() { float headprop, bodyprop, armprop, legprop; if (debugmode) { - if (Input::isKeyPressed(SDLK_h)) { + if (Input::isKeyPressed(SDL_SCANCODE_H)) { Person::players[0]->damagetolerance = 200000; Person::players[0]->damage = 0; Person::players[0]->burnt = 0; @@ -2898,18 +2898,18 @@ void doDebugKeys() Person::players[0]->superpermanentdamage = 0; } - if (Input::isKeyPressed(SDLK_j)) { + if (Input::isKeyPressed(SDL_SCANCODE_J)) { environment++; if (environment > 2) environment = 0; Setenvironment(environment); } - if (Input::isKeyPressed(SDLK_c)) { + if (Input::isKeyPressed(SDL_SCANCODE_C)) { cameramode = 1 - cameramode; } - if (Input::isKeyPressed(SDLK_x) && !Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_X) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { if (Person::players[0]->num_weapons > 0) { if (weapons[Person::players[0]->weaponids[0]].getType() == sword) weapons[Person::players[0]->weaponids[0]].setType(staff); @@ -2920,7 +2920,7 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_x) && Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_X) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { int closest = findClosestPlayer(); if (closest >= 0) { if (Person::players[closest]->num_weapons) { @@ -2941,7 +2941,7 @@ void doDebugKeys() } } - if (Input::isKeyDown(SDLK_u)) { + if (Input::isKeyDown(SDL_SCANCODE_U)) { int closest = findClosestPlayer(); if (closest >= 0) { Person::players[closest]->yaw += multiplier * 50; @@ -2950,9 +2950,9 @@ void doDebugKeys() } - if (Input::isKeyPressed(SDLK_o) && !Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_O) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { int closest = findClosestPlayer(); - if (Input::isKeyDown(SDLK_LCTRL)) + if (Input::isKeyDown(SDL_SCANCODE_LCTRL)) closest = 0; if (closest >= 0) { @@ -2977,7 +2977,7 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_o) && Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_O) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { int closest = findClosestPlayer(); if (closest >= 0) { if (Person::players[closest]->creature == wolftype) { @@ -3045,13 +3045,13 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_b) && !Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_B) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { slomo = 1 - slomo; slomodelay = 1000; } - if (((Input::isKeyPressed(SDLK_i) && !Input::isKeyDown(SDLK_LSHIFT)))) { + if (((Input::isKeyPressed(SDL_SCANCODE_I) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT)))) { int closest = -1; float closestdist = std::numeric_limits::max(); @@ -3105,7 +3105,7 @@ void doDebugKeys() } } - if (((Input::isKeyPressed(SDLK_i) && Input::isKeyDown(SDLK_LSHIFT)))) { + if (((Input::isKeyPressed(SDL_SCANCODE_I) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)))) { int closest = findClosestPlayer(); XYZ flatfacing2, flatvelocity2; XYZ blah; @@ -3211,7 +3211,7 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_f)) { + if (Input::isKeyPressed(SDL_SCANCODE_F)) { Person::players[0]->onfire = 1 - Person::players[0]->onfire; if (Person::players[0]->onfire) { Person::players[0]->CatchFire(); @@ -3222,7 +3222,7 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_n) && !Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyPressed(SDL_SCANCODE_N) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) { //if(!Person::players[0]->skeleton.free)Person::players[0]->damage+=500; Person::players[0]->RagDoll(0); //Person::players[0]->spurt=1; @@ -3231,7 +3231,7 @@ void doDebugKeys() emit_sound_at(whooshsound, Person::players[0]->coords, 128.); } - if (Input::isKeyPressed(SDLK_n) && Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyPressed(SDL_SCANCODE_N) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) { for (int i = 0; i < objects.numobjects; i++) { if (objects.type[i] == treeleavestype) { objects.scale[i] *= .9; @@ -3239,7 +3239,7 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_m) && Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_M) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { editorenabled = 1 - editorenabled; if (editorenabled) { Person::players[0]->damagetolerance = 100000; @@ -3254,7 +3254,7 @@ void doDebugKeys() } //skip level - if (whichlevel != -2 && Input::isKeyPressed(SDLK_k) && Input::isKeyDown(SDLK_LSHIFT) && !editorenabled) { + if (whichlevel != -2 && Input::isKeyPressed(SDL_SCANCODE_K) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !editorenabled) { targetlevel++; if (targetlevel > numchallengelevels - 1) targetlevel = 0; @@ -3263,20 +3263,20 @@ void doDebugKeys() } if (editorenabled) { - if (Input::isKeyPressed(SDLK_DELETE) && Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_DELETE) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { int closest = findClosestPlayer(); if (closest >= 0) { Person::players.erase(Person::players.begin()+closest); } } - if (Input::isKeyPressed(SDLK_DELETE) && Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyPressed(SDL_SCANCODE_DELETE) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) { int closest = findClosestObject(); if (closest >= 0) objects.position[closest].y -= 500; } - if (Input::isKeyPressed(SDLK_m) && Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_M) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { //drawmode++; //if(drawmode>2)drawmode=0; if (objects.numobjects < max_objects - 1) { @@ -3303,7 +3303,7 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_p) && Input::isKeyDown(SDLK_LSHIFT) && !Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyPressed(SDL_SCANCODE_P) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) { Person::players.push_back(shared_ptr(new Person())); Person::players.back()->scale = .2 * 5 * Person::players[0]->scale; @@ -3440,7 +3440,7 @@ void doDebugKeys() Person::players.back()->loaded = 1; } - if (Input::isKeyPressed(SDLK_p) && Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_P) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { if (Person::players.back()->numwaypoints < 90) { Person::players.back()->waypoints[Person::players.back()->numwaypoints] = Person::players[0]->coords; Person::players.back()->waypointtype[Person::players.back()->numwaypoints] = editorpathtype; @@ -3448,7 +3448,7 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_p) && Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyPressed(SDL_SCANCODE_P) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) { if (numpathpoints < 30) { bool connected, alreadyconnected; connected = 0; @@ -3480,17 +3480,17 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_PERIOD)) { + if (Input::isKeyPressed(SDL_SCANCODE_PERIOD)) { pathpointselected++; if (pathpointselected >= numpathpoints) pathpointselected = -1; } - if (Input::isKeyPressed(SDLK_COMMA) && !Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_COMMA) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { pathpointselected--; if (pathpointselected <= -2) pathpointselected = numpathpoints - 1; } - if (Input::isKeyPressed(SDLK_COMMA) && Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_COMMA) && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { if (pathpointselected != -1) { numpathpoints--; pathpoint[pathpointselected] = pathpoint[numpathpoints]; @@ -3513,7 +3513,7 @@ void doDebugKeys() } } - if (Input::isKeyPressed(SDLK_LEFT) && Input::isKeyDown(SDLK_LSHIFT) && !Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyPressed(SDL_SCANCODE_LEFT) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) { editortype--; if (editortype == treeleavestype || editortype == 10) editortype--; @@ -3521,7 +3521,7 @@ void doDebugKeys() editortype = firetype; } - if (Input::isKeyPressed(SDLK_RIGHT) && Input::isKeyDown(SDLK_LSHIFT) && !Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyPressed(SDL_SCANCODE_RIGHT) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) { editortype++; if (editortype == treeleavestype || editortype == 10) editortype++; @@ -3529,44 +3529,44 @@ void doDebugKeys() editortype = 0; } - if (Input::isKeyDown(SDLK_LEFT) && !Input::isKeyDown(SDLK_LSHIFT) && !Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyDown(SDL_SCANCODE_LEFT) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) { editoryaw -= multiplier * 100; if (editoryaw < -.01) editoryaw = -.01; } - if (Input::isKeyDown(SDLK_RIGHT) && !Input::isKeyDown(SDLK_LSHIFT) && !Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyDown(SDL_SCANCODE_RIGHT) && !Input::isKeyDown(SDL_SCANCODE_LSHIFT) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) { editoryaw += multiplier * 100; } - if (Input::isKeyDown(SDLK_UP) && !Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyDown(SDL_SCANCODE_UP) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) { editorsize += multiplier; } - if (Input::isKeyDown(SDLK_DOWN) && !Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyDown(SDL_SCANCODE_DOWN) && !Input::isKeyDown(SDL_SCANCODE_LCTRL)) { editorsize -= multiplier; if (editorsize < .1) editorsize = .1; } - if (Input::isKeyPressed(SDLK_LEFT) && Input::isKeyDown(SDLK_LSHIFT) && Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyPressed(SDL_SCANCODE_LEFT) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) { mapradius -= multiplier * 10; } - if (Input::isKeyPressed(SDLK_RIGHT) && Input::isKeyDown(SDLK_LSHIFT) && Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyPressed(SDL_SCANCODE_RIGHT) && Input::isKeyDown(SDL_SCANCODE_LSHIFT) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) { mapradius += multiplier * 10; } - if (Input::isKeyDown(SDLK_UP) && Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyDown(SDL_SCANCODE_UP) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) { editorpitch += multiplier * 100; } - if (Input::isKeyDown(SDLK_DOWN) && Input::isKeyDown(SDLK_LCTRL)) { + if (Input::isKeyDown(SDL_SCANCODE_DOWN) && Input::isKeyDown(SDL_SCANCODE_LCTRL)) { editorpitch -= multiplier * 100; if (editorpitch < -.01) editorpitch = -.01; } - if (Input::isKeyPressed(SDLK_DELETE) && objects.numobjects && Input::isKeyDown(SDLK_LSHIFT)) { + if (Input::isKeyPressed(SDL_SCANCODE_DELETE) && objects.numobjects && Input::isKeyDown(SDL_SCANCODE_LSHIFT)) { int closest = findClosestObject(); if (closest >= 0) objects.DeleteObject(closest); @@ -6296,10 +6296,7 @@ void MenuTick() mainmenu = 5; else mainmenu = 1; - for (int j = 0; j < 255; j++) { - displaytext[0][j] = 0; - } - displaychars[0] = 0; + displaytext[0].clear(); displayselected = 0; entername = 0; } @@ -6373,7 +6370,7 @@ void MenuTick() } } - if (Input::isKeyDown(SDLK_q) && Input::isKeyDown(SDLK_LMETA)) { + if (Input::isKeyDown(SDL_SCANCODE_Q) && Input::isKeyDown(SDL_SCANCODE_LGUI)) { tryquit = 1; if (mainmenu == 3) { SaveSettings(); @@ -6383,9 +6380,9 @@ void MenuTick() OPENAL_SetFrequency(channels[stream_menutheme], 22050); if (entername) { - inputText(displaytext[0], &displayselected, &displaychars[0]); + inputText(displaytext[0], &displayselected); if (!waiting) { // the input as finished - if (displaychars[0]) { // with enter + if (!displaytext[0].empty()) { // with enter accountactive = Account::add(string(displaytext[0])); mainmenu = 8; @@ -6394,10 +6391,7 @@ void MenuTick() fireSound(firestartsound); - for (int i = 0; i < 255; i++) { - displaytext[0][i] = 0; - } - displaychars[0] = 0; + displaytext[0].clear(); displayselected = 0; } @@ -6435,8 +6429,8 @@ void Game::Tick() keyboardfrozen = false; Input::Tick(); - if (Input::isKeyPressed(SDLK_F6)) { - if (Input::isKeyDown(SDLK_LSHIFT)) + if (Input::isKeyPressed(SDL_SCANCODE_F6)) { + if (Input::isKeyDown(SDL_SCANCODE_LSHIFT)) stereoreverse = true; else stereoreverse = false; @@ -6447,16 +6441,16 @@ void Game::Tick() printf("Stereo unreversed\n"); } - if (Input::isKeyDown(SDLK_F7)) { - if (Input::isKeyDown(SDLK_LSHIFT)) + if (Input::isKeyDown(SDL_SCANCODE_F7)) { + if (Input::isKeyDown(SDL_SCANCODE_LSHIFT)) stereoseparation -= 0.001; else stereoseparation -= 0.010; printf("Stereo decreased increased to %f\n", stereoseparation); } - if (Input::isKeyDown(SDLK_F8)) { - if (Input::isKeyDown(SDLK_LSHIFT)) + if (Input::isKeyDown(SDL_SCANCODE_F8)) { + if (Input::isKeyDown(SDL_SCANCODE_LSHIFT)) stereoseparation += 0.001; else stereoseparation += 0.010; @@ -6464,7 +6458,7 @@ void Game::Tick() } - if (Input::isKeyPressed(SDLK_TAB) && tutoriallevel) { + if (Input::isKeyPressed(SDL_SCANCODE_TAB) && tutoriallevel) { if (tutorialstage != 51) tutorialstagetime = tutorialmaxtime; emit_sound_np(consolefailsound, 128.); @@ -6505,7 +6499,7 @@ void Game::Tick() LoadMenu(); } //escape key pressed - if (Input::isKeyPressed(SDLK_ESCAPE) && + if (Input::isKeyPressed(SDL_SCANCODE_ESCAPE) && (gameon || mainmenu == 0 || (mainmenu >= 3 && mainmenu != 8 && !(mainmenu == 7 && entername)))) { selected = -1; if (mainmenu == 0 && !winfreeze) @@ -6566,7 +6560,7 @@ void Game::Tick() leveltime += multiplier; //keys - if (Input::isKeyPressed(SDLK_v) && debugmode) { + if (Input::isKeyPressed(SDL_SCANCODE_V) && debugmode) { freeze = 1 - freeze; if (freeze) { OPENAL_SetFrequency(OPENAL_ALL, 0.001); @@ -6577,12 +6571,10 @@ void Game::Tick() chatting = 1; if (chatting) { - inputText(displaytext[0], &displayselected, &displaychars[0]); + inputText(displaytext[0], &displayselected); if (!waiting) { - if (displaychars[0]) { - for (int j = 0; j < 255; j++) - displaytext[0][j] = 0; - displaychars[0] = 0; + if (!displaytext[0].empty()) { + displaytext[0].clear(); displayselected = 0; } chatting = 0; @@ -6609,20 +6601,15 @@ void Game::Tick() if (console) freeze = 1; - if (console && !Input::isKeyDown(SDLK_LMETA)) { - inputText(consoletext[0], &consoleselected, &consolechars[0]); + if (console && !Input::isKeyDown(SDL_SCANCODE_LGUI)) { + inputText(consoletext[0], &consoleselected); if (!waiting) { - if (consolechars[0] > 0) { - consoletext[0][consolechars[0]] = '\0'; + if (!consoletext[0].empty()) { cmd_dispatch(consoletext[0]); for (int k = 14; k >= 1; k--) { - for (int j = 0; j < 255; j++) - consoletext[k][j] = consoletext[k - 1][j]; - consolechars[k] = consolechars[k - 1]; + consoletext[k] = consoletext[k - 1]; } - for (int j = 0; j < 255; j++) - consoletext[0][j] = 0; - consolechars[0] = 0; + consoletext[0].clear(); consoleselected = 0; } } @@ -6636,7 +6623,7 @@ void Game::Tick() - if (Input::isKeyDown(SDLK_q) && Input::isKeyDown(SDLK_LMETA)) { + if (Input::isKeyDown(SDL_SCANCODE_Q) && Input::isKeyDown(SDL_SCANCODE_LGUI)) { tryquit = 1; if (mainmenu == 3) { SaveSettings(); @@ -6653,10 +6640,10 @@ void Game::Tick() else oldwinfreeze++; - if ((Input::isKeyPressed(jumpkey) || Input::isKeyPressed(SDLK_SPACE)) && !campaign) + if ((Input::isKeyPressed(jumpkey) || Input::isKeyPressed(SDL_SCANCODE_SPACE)) && !campaign) if (winfreeze) winfreeze = 0; - if ((Input::isKeyDown(SDLK_ESCAPE)) && !campaign && gameon) { + if ((Input::isKeyDown(SDL_SCANCODE_ESCAPE)) && !campaign && gameon) { if (console) { console = false; freeze = 0; @@ -6869,29 +6856,29 @@ void Game::Tick() viewer.y += multiplier * 4; if (Input::isKeyDown(crouchkey)) viewer.y -= multiplier * 4; - if ( Input::isKeyPressed(SDLK_1) || - Input::isKeyPressed(SDLK_2) || - Input::isKeyPressed(SDLK_3) || - Input::isKeyPressed(SDLK_4) || - Input::isKeyPressed(SDLK_5) || - Input::isKeyPressed(SDLK_6) || - Input::isKeyPressed(SDLK_7) || - Input::isKeyPressed(SDLK_8) || - Input::isKeyPressed(SDLK_9) || - Input::isKeyPressed(SDLK_0) || - Input::isKeyPressed(SDLK_MINUS)) { + if ( Input::isKeyPressed(SDL_SCANCODE_1) || + Input::isKeyPressed(SDL_SCANCODE_2) || + Input::isKeyPressed(SDL_SCANCODE_3) || + Input::isKeyPressed(SDL_SCANCODE_4) || + Input::isKeyPressed(SDL_SCANCODE_5) || + Input::isKeyPressed(SDL_SCANCODE_6) || + Input::isKeyPressed(SDL_SCANCODE_7) || + Input::isKeyPressed(SDL_SCANCODE_8) || + Input::isKeyPressed(SDL_SCANCODE_9) || + Input::isKeyPressed(SDL_SCANCODE_0) || + Input::isKeyPressed(SDL_SCANCODE_MINUS)) { int whichend; - if (Input::isKeyPressed(SDLK_1)) whichend = 1; - if (Input::isKeyPressed(SDLK_2)) whichend = 2; - if (Input::isKeyPressed(SDLK_3)) whichend = 3; - if (Input::isKeyPressed(SDLK_4)) whichend = 4; - if (Input::isKeyPressed(SDLK_5)) whichend = 5; - if (Input::isKeyPressed(SDLK_6)) whichend = 6; - if (Input::isKeyPressed(SDLK_7)) whichend = 7; - if (Input::isKeyPressed(SDLK_8)) whichend = 8; - if (Input::isKeyPressed(SDLK_9)) whichend = 9; - if (Input::isKeyPressed(SDLK_0)) whichend = 0; - if (Input::isKeyPressed(SDLK_MINUS)) + if (Input::isKeyPressed(SDL_SCANCODE_1)) whichend = 1; + if (Input::isKeyPressed(SDL_SCANCODE_2)) whichend = 2; + if (Input::isKeyPressed(SDL_SCANCODE_3)) whichend = 3; + if (Input::isKeyPressed(SDL_SCANCODE_4)) whichend = 4; + if (Input::isKeyPressed(SDL_SCANCODE_5)) whichend = 5; + if (Input::isKeyPressed(SDL_SCANCODE_6)) whichend = 6; + if (Input::isKeyPressed(SDL_SCANCODE_7)) whichend = 7; + if (Input::isKeyPressed(SDL_SCANCODE_8)) whichend = 8; + if (Input::isKeyPressed(SDL_SCANCODE_9)) whichend = 9; + if (Input::isKeyPressed(SDL_SCANCODE_0)) whichend = 0; + if (Input::isKeyPressed(SDL_SCANCODE_MINUS)) whichend = -1; if (whichend != -1) { participantfocus[whichdialogue][indialogue] = whichend; @@ -6921,27 +6908,27 @@ void Game::Tick() } } //TODO: should these be KeyDown or KeyPressed? - if ( Input::isKeyDown(SDLK_KP1) || - Input::isKeyDown(SDLK_KP2) || - Input::isKeyDown(SDLK_KP3) || - Input::isKeyDown(SDLK_KP4) || - Input::isKeyDown(SDLK_KP5) || - Input::isKeyDown(SDLK_KP6) || - Input::isKeyDown(SDLK_KP7) || - Input::isKeyDown(SDLK_KP8) || - Input::isKeyDown(SDLK_KP9) || - Input::isKeyDown(SDLK_KP0)) { + if ( Input::isKeyDown(SDL_SCANCODE_KP_1) || + Input::isKeyDown(SDL_SCANCODE_KP_2) || + Input::isKeyDown(SDL_SCANCODE_KP_3) || + Input::isKeyDown(SDL_SCANCODE_KP_4) || + Input::isKeyDown(SDL_SCANCODE_KP_5) || + Input::isKeyDown(SDL_SCANCODE_KP_6) || + Input::isKeyDown(SDL_SCANCODE_KP_7) || + Input::isKeyDown(SDL_SCANCODE_KP_8) || + Input::isKeyDown(SDL_SCANCODE_KP_9) || + Input::isKeyDown(SDL_SCANCODE_KP_0)) { int whichend; - if (Input::isKeyDown(SDLK_KP1)) whichend = 1; - if (Input::isKeyDown(SDLK_KP2)) whichend = 2; - if (Input::isKeyDown(SDLK_KP3)) whichend = 3; - if (Input::isKeyDown(SDLK_KP4)) whichend = 4; - if (Input::isKeyDown(SDLK_KP5)) whichend = 5; - if (Input::isKeyDown(SDLK_KP6)) whichend = 6; - if (Input::isKeyDown(SDLK_KP7)) whichend = 7; - if (Input::isKeyDown(SDLK_KP8)) whichend = 8; - if (Input::isKeyDown(SDLK_KP9)) whichend = 9; - if (Input::isKeyDown(SDLK_KP0)) whichend = 0; + if (Input::isKeyDown(SDL_SCANCODE_KP_1)) whichend = 1; + if (Input::isKeyDown(SDL_SCANCODE_KP_2)) whichend = 2; + if (Input::isKeyDown(SDL_SCANCODE_KP_3)) whichend = 3; + if (Input::isKeyDown(SDL_SCANCODE_KP_4)) whichend = 4; + if (Input::isKeyDown(SDL_SCANCODE_KP_5)) whichend = 5; + if (Input::isKeyDown(SDL_SCANCODE_KP_6)) whichend = 6; + if (Input::isKeyDown(SDL_SCANCODE_KP_7)) whichend = 7; + if (Input::isKeyDown(SDL_SCANCODE_KP_8)) whichend = 8; + if (Input::isKeyDown(SDL_SCANCODE_KP_9)) whichend = 9; + if (Input::isKeyDown(SDL_SCANCODE_KP_0)) whichend = 0; participantfacing[whichdialogue][indialogue][whichend] = facing; } if (indialogue >= numdialogueboxes[whichdialogue]) { @@ -6957,17 +6944,17 @@ void Game::Tick() yaw = dialoguecamerayaw[whichdialogue][indialogue]; pitch = dialoguecamerapitch[whichdialogue][indialogue]; if (dialoguetime > 0.5) - if ( Input::isKeyPressed(SDLK_1) || - Input::isKeyPressed(SDLK_2) || - Input::isKeyPressed(SDLK_3) || - Input::isKeyPressed(SDLK_4) || - Input::isKeyPressed(SDLK_5) || - Input::isKeyPressed(SDLK_6) || - Input::isKeyPressed(SDLK_7) || - Input::isKeyPressed(SDLK_8) || - Input::isKeyPressed(SDLK_9) || - Input::isKeyPressed(SDLK_0) || - Input::isKeyPressed(SDLK_MINUS) || + if ( Input::isKeyPressed(SDL_SCANCODE_1) || + Input::isKeyPressed(SDL_SCANCODE_2) || + Input::isKeyPressed(SDL_SCANCODE_3) || + Input::isKeyPressed(SDL_SCANCODE_4) || + Input::isKeyPressed(SDL_SCANCODE_5) || + Input::isKeyPressed(SDL_SCANCODE_6) || + Input::isKeyPressed(SDL_SCANCODE_7) || + Input::isKeyPressed(SDL_SCANCODE_8) || + Input::isKeyPressed(SDL_SCANCODE_9) || + Input::isKeyPressed(SDL_SCANCODE_0) || + Input::isKeyPressed(SDL_SCANCODE_MINUS) || Input::isKeyPressed(attackkey)) { indialogue++; if (indialogue < numdialogueboxes[whichdialogue]) { @@ -7059,8 +7046,8 @@ void Game::Tick() static bool respawnkeydown; if (!editorenabled && (whichlevel != -2 && - (Input::isKeyDown(SDLK_z) && - Input::isKeyDown(SDLK_LMETA) && + (Input::isKeyDown(SDL_SCANCODE_Z) && + Input::isKeyDown(SDL_SCANCODE_LGUI) && debugmode) || (Input::isKeyDown(jumpkey) && !respawnkeydown && @@ -8104,7 +8091,7 @@ void Game::Tick() } } - if (Input::isKeyPressed(SDLK_F1)) + if (Input::isKeyPressed(SDL_SCANCODE_F1)) Screenshot(); } diff --git a/Source/Globals.cpp b/Source/Globals.cpp index ea466e2..1cb6dbf 100644 --- a/Source/Globals.cpp +++ b/Source/Globals.cpp @@ -74,7 +74,7 @@ int numsounds = 0; Terrain terrain; float sps = 0; -SDL_Surface *sdlscreen; +SDL_Window *sdlwindow; int kTextureSize = 0; int detail = 0; diff --git a/Source/Input.cpp b/Source/Input.cpp index 0598790..88970f9 100644 --- a/Source/Input.cpp +++ b/Source/Input.cpp @@ -25,47 +25,48 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern bool keyboardfrozen; -bool keyDown[SDLK_LAST + 6]; -bool keyPressed[SDLK_LAST + 6]; +bool keyDown[SDL_NUM_SCANCODES + 6]; +bool keyPressed[SDL_NUM_SCANCODES + 6]; void Input::Tick() { SDL_PumpEvents(); - Uint8 *keyState = SDL_GetKeyState(NULL); - for (int i = 0; i < SDLK_LAST; i++) { + int numkeys; + const Uint8 *keyState = SDL_GetKeyboardState(&numkeys); + for (int i = 0; i < numkeys; i++) { keyPressed[i] = !keyDown[i] && keyState[i]; keyDown[i] = keyState[i]; } Uint8 mb = SDL_GetMouseState(NULL, NULL); for (int i = 1; i < 6; i++) { - keyPressed[SDLK_LAST + i] = !keyDown[SDLK_LAST + i] && (mb & SDL_BUTTON(i)); - keyDown[SDLK_LAST + i] = (mb & SDL_BUTTON(i)); + keyPressed[SDL_NUM_SCANCODES + i] = !keyDown[SDL_NUM_SCANCODES + i] && (mb & SDL_BUTTON(i)); + keyDown[SDL_NUM_SCANCODES + i] = (mb & SDL_BUTTON(i)); } } bool Input::isKeyDown(int k) { - if (keyboardfrozen || k >= SDLK_LAST + 6) // really useful? check that. + if (keyboardfrozen || k >= SDL_NUM_SCANCODES + 6) // really useful? check that. return false; return keyDown[k]; } bool Input::isKeyPressed(int k) { - if (keyboardfrozen || k >= SDLK_LAST + 6) + if (keyboardfrozen || k >= SDL_NUM_SCANCODES + 6) return false; return keyPressed[k]; } const char* Input::keyToChar(unsigned short i) { - if (i < SDLK_LAST) - return SDL_GetKeyName(SDLKey(i)); - else if (i == SDLK_LAST + SDL_BUTTON_LEFT) + if (i < SDL_NUM_SCANCODES) + return SDL_GetScancodeName(SDL_Scancode(i)); + else if (i == MOUSEBUTTON1) return "mouse1"; - else if (i == SDLK_LAST + SDL_BUTTON_RIGHT) + else if (i == MOUSEBUTTON2) return "mouse2"; - else if (i == SDLK_LAST + SDL_BUTTON_MIDDLE) + else if (i == MOUSEBUTTON3) return "mouse3"; else return "unknown"; @@ -73,8 +74,8 @@ const char* Input::keyToChar(unsigned short i) unsigned short Input::CharToKey(const char* which) { - for (unsigned short i = 0; i < SDLK_LAST; i++) { - if (!strcasecmp(which, SDL_GetKeyName(SDLKey(i)))) + for (unsigned short i = 0; i < SDL_NUM_SCANCODES; i++) { + if (!strcasecmp(which, SDL_GetScancodeName(SDL_Scancode(i)))) return i; } if (!strcasecmp(which, "mouse1")) { @@ -83,10 +84,13 @@ unsigned short Input::CharToKey(const char* which) if (!strcasecmp(which, "mouse2")) { return MOUSEBUTTON2; } - return SDLK_LAST; + if (!strcasecmp(which, "mouse3")) { + return MOUSEBUTTON3; + } + return SDL_NUM_SCANCODES; } Boolean Input::MouseClicked() { - return isKeyPressed(SDLK_LAST + SDL_BUTTON_LEFT); + return isKeyPressed(SDL_NUM_SCANCODES + SDL_BUTTON_LEFT); } diff --git a/Source/Input.h b/Source/Input.h index 2972ca6..e28b587 100644 --- a/Source/Input.h +++ b/Source/Input.h @@ -28,8 +28,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Game.h" /**> CONSTANT DECLARATIONS <**/ -#define MOUSEBUTTON1 SDLK_LAST+SDL_BUTTON_LEFT -#define MOUSEBUTTON2 SDLK_LAST+SDL_BUTTON_RIGHT +#define MOUSEBUTTON1 (SDL_NUM_SCANCODES + SDL_BUTTON_LEFT) +#define MOUSEBUTTON2 (SDL_NUM_SCANCODES + SDL_BUTTON_RIGHT) +#define MOUSEBUTTON3 (SDL_NUM_SCANCODES + SDL_BUTTON_MIDDLE) /**> FUNCTION PROTOTYPES <**/ class Input diff --git a/Source/OpenGL_Windows.cpp b/Source/OpenGL_Windows.cpp index 09081c4..5c32c25 100644 --- a/Source/OpenGL_Windows.cpp +++ b/Source/OpenGL_Windows.cpp @@ -89,18 +89,11 @@ extern float slomofreq; #include "win-res/resource.h" #endif +extern SDL_Window *sdlwindow; + using namespace std; SDL_Rect **resolutions = NULL; -static SDL_Rect rect_1024_768 = { 0, 0, 1024, 768 }; -static SDL_Rect rect_800_600 = { 0, 0, 800, 600 }; -static SDL_Rect rect_640_480 = { 0, 0, 640, 480 }; -static SDL_Rect *hardcoded_resolutions[] = { - &rect_1024_768, - &rect_800_600, - &rect_640_480, - NULL -}; Boolean SetUp (); void DoUpdate (); @@ -236,45 +229,46 @@ void initGL() static void toggleFullscreen() { - if (!SDL_WM_ToggleFullScreen(SDL_GetVideoSurface())) { - SDL_Surface* screen = SDL_GetVideoSurface(); - Uint32 flags = screen->flags; - screen = SDL_SetVideoMode(0, 0, 0, flags ^ SDL_FULLSCREEN); - if (!screen) - screen = SDL_SetVideoMode(0, 0, 0, flags); - if (!screen) - exit(1); - //reload opengl state - initGL(); - Texture::reloadAll(); - if (text) - text->BuildFont(); - if (firstload) { - screentexture = 0; - LoadScreenTexture(); - } - screentexture2 = 0; + Uint32 flags = SDL_GetWindowFlags(sdlwindow); + if (flags & SDL_WINDOW_FULLSCREEN) { + flags &= ~SDL_WINDOW_FULLSCREEN; + } else { + flags |= SDL_WINDOW_FULLSCREEN; } + SDL_SetWindowFullscreen(sdlwindow, flags); } -static void sdlEventProc(const SDL_Event &e) +static SDL_bool sdlEventProc(const SDL_Event &e) { switch (e.type) { - case SDL_MOUSEMOTION: - deltah += e.motion.xrel; - deltav += e.motion.yrel; + case SDL_QUIT: + return SDL_FALSE; + + case SDL_WINDOWEVENT: + if (e.window.event == SDL_WINDOWEVENT_CLOSE) { + return SDL_FALSE; + } break; - case SDL_KEYDOWN: - if ((e.key.keysym.sym == SDLK_g) && - (e.key.keysym.mod & KMOD_CTRL) && - !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN) ) { - SDL_WM_GrabInput( ((SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON) ? SDL_GRAB_OFF : SDL_GRAB_ON) ); - } else if ( (e.key.keysym.sym == SDLK_RETURN) && (e.key.keysym.mod & KMOD_ALT) ) { - toggleFullscreen(); - } + case SDL_MOUSEMOTION: + deltah += e.motion.xrel; + deltav += e.motion.yrel; + break; + + case SDL_KEYDOWN: + if ((e.key.keysym.scancode == SDL_SCANCODE_G) && + (e.key.keysym.mod & KMOD_CTRL)) { + SDL_bool mode = SDL_TRUE; + if ((SDL_GetWindowFlags(sdlwindow) & SDL_WINDOW_FULLSCREEN) == 0) + mode = (SDL_GetWindowGrab(sdlwindow) ? SDL_FALSE : SDL_TRUE); + SDL_SetWindowGrab(sdlwindow, mode); + SDL_SetRelativeMouseMode(mode); + } else if ( (e.key.keysym.scancode == SDL_SCANCODE_RETURN) && (e.key.keysym.mod & KMOD_ALT) ) { + toggleFullscreen(); + } break; } + return SDL_TRUE; } @@ -297,6 +291,8 @@ Boolean SetUp () DefaultSettings(); + const int displayIdx = 0; // !!! FIXME: other monitors? + if (!SDL_WasInit(SDL_INIT_VIDEO)) if (SDL_Init(SDL_INIT_VIDEO) == -1) { fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError()); @@ -316,15 +312,24 @@ Boolean SetUp () return false; } - SDL_Rect **res = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_OPENGL); - if ( (res == NULL) || (res == ((SDL_Rect **) - 1)) || (res[0] == NULL) || (res[0]->w < 640) || (res[0]->h < 480) ) - res = hardcoded_resolutions; + int count = 0; + const int nummodes = SDL_GetNumDisplayModes(displayIdx); + for (int i = 0; i < nummodes; i++) + { + SDL_DisplayMode mode; + if (SDL_GetDisplayMode(displayIdx, i, &mode) == -1) + continue; + if ((mode.w < 640) || (mode.h < 480)) + continue; // sane lower limit. + count++; + } - // reverse list (it was sorted biggest to smallest by SDL)... - int count; - for (count = 0; res[count]; count++) { - if ((res[count]->w < 640) || (res[count]->h < 480)) - break; // sane lower limit. + if (count == 0) { + const std::string error = "No suitable video resolutions found."; + cerr << error << endl; + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Lugaru init failed!", error.c_str(), NULL); + SDL_Quit(); + return false; } static SDL_Rect *resolutions_block = NULL; @@ -338,7 +343,15 @@ Boolean SetUp () resolutions[count--] = NULL; for (int i = 0; count >= 0; i++, count--) { - memcpy(&resolutions_block[count], res[i], sizeof (SDL_Rect)); + /* FIXME - Pretty sure this should use nummodes and not count */ + SDL_DisplayMode mode; + if (SDL_GetDisplayMode(displayIdx, i, &mode) == -1) + continue; + if ((mode.w < 640) || (mode.h < 480)) + continue; // sane lower limit. + resolutions_block[count].x = resolutions_block[count].y = 0; + resolutions_block[count].w = mode.w; + resolutions_block[count].h = mode.h; resolutions[count] = &resolutions_block[count]; } @@ -348,51 +361,68 @@ Boolean SetUp () printf(" %d x %d\n", (int) resolutions[i]->w, (int) resolutions[i]->h); } - Uint32 sdlflags = SDL_OPENGL; + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1); + Uint32 sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; if (!cmdline("windowed")) - sdlflags |= SDL_FULLSCREEN; - - SDL_WM_SetCaption("Lugaru", "Lugaru"); + sdlflags |= SDL_WINDOW_FULLSCREEN; + if (!cmdline("nomousegrab")) + sdlflags |= SDL_WINDOW_INPUT_GRABBED; - SDL_ShowCursor(0); + sdlwindow = SDL_CreateWindow("Lugaru", SDL_WINDOWPOS_CENTERED_DISPLAY(displayIdx), SDL_WINDOWPOS_CENTERED_DISPLAY(displayIdx), + kContextWidth, kContextHeight, sdlflags); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1); -#if SDL_VERSION_ATLEAST(1, 2, 10) - SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, vblsync); -#endif - - if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL) { - fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError()); + if (!sdlwindow) { + fprintf(stderr, "SDL_CreateWindow() failed: %s\n", SDL_GetError()); fprintf(stderr, "forcing 640x480...\n"); kContextWidth = 640; kContextHeight = 480; - if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL) { - fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError()); + sdlwindow = SDL_CreateWindow("Lugaru", SDL_WINDOWPOS_CENTERED_DISPLAY(displayIdx), SDL_WINDOWPOS_CENTERED_DISPLAY(displayIdx), + kContextWidth, kContextHeight, sdlflags); + if (!sdlwindow) { + fprintf(stderr, "SDL_CreateWindow() failed: %s\n", SDL_GetError()); fprintf(stderr, "forcing 640x480 windowed mode...\n"); - sdlflags &= ~SDL_FULLSCREEN; - if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL) { - fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError()); + sdlflags &= ~SDL_WINDOW_FULLSCREEN; + sdlwindow = SDL_CreateWindow("Lugaru", SDL_WINDOWPOS_CENTERED_DISPLAY(displayIdx), SDL_WINDOWPOS_CENTERED_DISPLAY(displayIdx), + kContextWidth, kContextHeight, sdlflags); + + if (!sdlwindow) { + fprintf(stderr, "SDL_CreateWindow() failed: %s\n", SDL_GetError()); return false; } } } - int dblbuf = 0; - if ((SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &dblbuf) == -1) || (!dblbuf)) { - fprintf(stderr, "Failed to get double buffered GL context!\n"); + SDL_GLContext glctx = SDL_GL_CreateContext(sdlwindow); + if (!glctx) { + fprintf(stderr, "SDL_GL_CreateContext() failed: %s\n", SDL_GetError()); SDL_Quit(); return false; } + SDL_GL_MakeCurrent(sdlwindow, glctx); + if (!lookup_all_glsyms()) { + fprintf(stderr, "Missing required OpenGL functions.\n"); SDL_Quit(); return false; } - if (!cmdline("nomousegrab")) - SDL_WM_GrabInput(SDL_GRAB_ON); + int dblbuf = 0; + if ((SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &dblbuf) == -1) || (!dblbuf)) + { + fprintf(stderr, "Failed to get a double-buffered context.\n"); + SDL_Quit(); + return false; + } + + if (SDL_GL_SetSwapInterval(-1) == -1) // try swap_tear first. + SDL_GL_SetSwapInterval(1); + + SDL_ShowCursor(0); + SDL_SetWindowGrab(sdlwindow, SDL_TRUE); + SDL_SetRelativeMouseMode(SDL_TRUE); initGL(); @@ -575,7 +605,7 @@ void CleanUp (void) static bool IsFocused() { - return ((SDL_GetAppState() & SDL_APPINPUTFOCUS) != 0); + return ((SDL_GetWindowFlags(sdlwindow) & SDL_WINDOW_INPUT_FOCUS) != 0); } @@ -713,11 +743,10 @@ int main(int argc, char **argv) if (!waiting) { // message pump while ( SDL_PollEvent( &e ) ) { - if ( e.type == SDL_QUIT ) { + if (!sdlEventProc(e)) { gDone = true; break; } - sdlEventProc(e); } } @@ -731,12 +760,7 @@ int main(int argc, char **argv) } // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking' - SDL_ActiveEvent evt; - SDL_WaitEvent((SDL_Event*)&evt); - if (evt.type == SDL_ACTIVEEVENT && evt.gain == 1) - gameFocused = true; - else if (evt.type == SDL_QUIT) - gDone = true; + SDL_WaitEvent(0); } } diff --git a/Source/Settings.cpp b/Source/Settings.cpp index 9f73038..38bf9b2 100644 --- a/Source/Settings.cpp +++ b/Source/Settings.cpp @@ -55,17 +55,17 @@ void DefaultSettings() vblsync = 1; debugmode = 0; - crouchkey = SDLK_LSHIFT; - jumpkey = SDLK_SPACE; - leftkey = SDLK_a; - forwardkey = SDLK_w; - backkey = SDLK_s; - rightkey = SDLK_d; - drawkey = SDLK_e; - throwkey = SDLK_q; + crouchkey = SDL_SCANCODE_LSHIFT; + jumpkey = SDL_SCANCODE_SPACE; + leftkey = SDL_SCANCODE_A; + forwardkey = SDL_SCANCODE_W; + backkey = SDL_SCANCODE_S; + rightkey = SDL_SCANCODE_D; + drawkey = SDL_SCANCODE_E; + throwkey = SDL_SCANCODE_Q; attackkey = MOUSEBUTTON1; - consolekey = SDLK_BACKQUOTE; - chatkey = SDLK_t; + consolekey = SDL_SCANCODE_GRAVE; + chatkey = SDL_SCANCODE_T; } void SaveSettings() @@ -138,27 +138,27 @@ void SaveSettings() opstream << "\nVolume:\n"; opstream << volume; opstream << "\nForward key:\n"; - opstream << Input::keyToChar(forwardkey); + opstream << forwardkey; opstream << "\nBack key:\n"; - opstream << Input::keyToChar(backkey); + opstream << backkey; opstream << "\nLeft key:\n"; - opstream << Input::keyToChar(leftkey); + opstream << leftkey; opstream << "\nRight key:\n"; - opstream << Input::keyToChar(rightkey); + opstream << rightkey; opstream << "\nJump key:\n"; - opstream << Input::keyToChar(jumpkey); + opstream << jumpkey; opstream << "\nCrouch key:\n"; - opstream << Input::keyToChar(crouchkey); + opstream << crouchkey; opstream << "\nDraw key:\n"; - opstream << Input::keyToChar(drawkey); + opstream << drawkey; opstream << "\nThrow key:\n"; - opstream << Input::keyToChar(throwkey); + opstream << throwkey; opstream << "\nAttack key:\n"; - opstream << Input::keyToChar(attackkey); + opstream << attackkey; opstream << "\nConsole key:\n"; - opstream << Input::keyToChar(consolekey); + opstream << consolekey; opstream << "\nChat key:\n"; - opstream << Input::keyToChar(chatkey); + opstream << chatkey; opstream << "\nDamage bar:\n"; opstream << showdamagebar; opstream << "\nStereoMode:\n"; @@ -259,38 +259,27 @@ bool LoadSettings() } else if ( !strncmp(setting, "Volume", 6) ) { ipstream >> volume; } else if ( !strncmp(setting, "Forward key", 11) ) { - ipstream.getline( string, sizeof(string) ); - forwardkey = Input::CharToKey(string); + ipstream >> forwardkey; } else if ( !strncmp(setting, "Back key", 8) ) { - ipstream.getline( string, sizeof(string) ); - backkey = Input::CharToKey(string); + ipstream >> backkey; } else if ( !strncmp(setting, "Left key", 8) ) { - ipstream.getline( string, sizeof(string) ); - leftkey = Input::CharToKey(string); + ipstream >> leftkey; } else if ( !strncmp(setting, "Right key", 9) ) { - ipstream.getline( string, sizeof(string) ); - rightkey = Input::CharToKey(string); + ipstream >> rightkey; } else if ( !strncmp(setting, "Jump key", 8) ) { - ipstream.getline( string, sizeof(string) ); - jumpkey = Input::CharToKey(string); + ipstream >> jumpkey; } else if ( !strncmp(setting, "Crouch key", 10) ) { - ipstream.getline( string, sizeof(string) ); - crouchkey = Input::CharToKey(string); + ipstream >> crouchkey; } else if ( !strncmp(setting, "Draw key", 8) ) { - ipstream.getline( string, sizeof(string) ); - drawkey = Input::CharToKey(string); + ipstream >> drawkey; } else if ( !strncmp(setting, "Throw key", 9) ) { - ipstream.getline( string, sizeof(string) ); - throwkey = Input::CharToKey(string); + ipstream >> throwkey; } else if ( !strncmp(setting, "Attack key", 10) ) { - ipstream.getline( string, sizeof(string) ); - attackkey = Input::CharToKey(string); + ipstream >> attackkey; } else if ( !strncmp(setting, "Console key", 11) ) { - ipstream.getline( string, sizeof(string) ); - consolekey = Input::CharToKey(string); + ipstream >> consolekey; } else if ( !strncmp(setting, "Chat key", 8) ) { - ipstream.getline( string, sizeof(string) ); - chatkey = Input::CharToKey(string); + ipstream >> chatkey; } else if ( !strncmp(setting, "Damage bar", 10) ) { ipstream >> showdamagebar; } else if ( !strncmp(setting, "StereoMode", 10) ) { -- 2.39.5