X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCEngine.cpp;h=ac9bb8ce37cfbef6aa6badf6f3600818b7723640;hb=02cf9ac96c7ad8ba47b6ef7c5584b46656f14b8d;hp=13a54090ca201dd1480cf166d4b63cbc8de30c6c;hpb=6d93c744add4c06b85cf6c6d7e6317536a5c8ec1;p=quix0rs-blobwars.git diff --git a/src/CEngine.cpp b/src/CEngine.cpp old mode 100755 new mode 100644 index 13a5409..ac9bb8c --- a/src/CEngine.cpp +++ b/src/CEngine.cpp @@ -1,5 +1,6 @@ /* -Copyright (C) 2004 Parallel Realities +Copyright (C) 2004-2011 Parallel Realities +Copyright (C) 2011-2015 Perpendicular Dimensions This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -19,18 +20,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "headers.h" +#include +extern Graphics graphics; Engine::Engine() { - for (int i = 0 ; i < 350 ; i++) - { - keyState[i] = 0; - } - - for (int i = 0 ; i < 32 ; i++) - { - joystickState[i] = 0; - } + memset(keyState, 0, sizeof keyState); + + memset(joystickState, 0, sizeof joystickState); joyX = joyY = 0; @@ -40,7 +37,7 @@ Engine::Engine() allowJoypad = true; - strcpy(lastKeyPressed, ""); + lastKeyPressed[0] = 0; fullScreen = 0; @@ -54,7 +51,7 @@ Engine::Engine() highlightedWidget = NULL; - strcpy(message, ""); + message[0] = 0; messageTime = -1; // Development Stuff @@ -64,7 +61,7 @@ Engine::Engine() binaryBuffer = NULL; #ifdef FRAMEWORK_SDL char pakPath[PATH_MAX]; - strncpy(pakPath, PAKFULLPATH, sizeof(pakPath)); + strlcpy(pakPath, PAKFULLPATH, sizeof(pakPath)); if (CFBundleGetMainBundle() != NULL) { CFURLRef pakURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR(PAKNAME), NULL, NULL); if (pakURL != NULL) { @@ -84,11 +81,6 @@ Engine::Engine() // Cheats memset(lastKeyEvents, ' ', 25); - #if USEPAK - cheats = false; - #else - cheats = true; - #endif cheatHealth = cheatExtras = cheatFuel = cheatLevels = false; cheatBlood = cheatInvulnerable = cheatReload = cheatSpeed = cheatSkipLevel = false; @@ -101,12 +93,10 @@ void Engine::destroy() deleteWidgets(); debug(("engine: free databuffer\n")); - if (dataBuffer != NULL) - delete[] dataBuffer; + delete[] dataBuffer; debug(("engine: free binarybuffer\n")); - if (binaryBuffer != NULL) - delete[] binaryBuffer; + delete[] binaryBuffer; debug(("Clearing Define List...\n")); defineList.clear(); @@ -121,7 +111,7 @@ void Engine::clearCheatVars() bool Engine::compareLastKeyInputs() { - if (strstr(lastKeyEvents, "lockandload")) + if (strstr(lastKeyEvents, "LOCKANDLOAD")) { cheats = true; return true; @@ -150,7 +140,7 @@ void Engine::addKeyEvent() if (index == -1) { - for (int i = 0 ; i < 25 ; i++) + for (int i = 0 ; i < 24 ; i++) { lastKeyEvents[i] = lastKeyEvents[i + 1]; } @@ -167,6 +157,12 @@ void Engine::getInput() { SDL_GetMouseState(&mouseX, &mouseY); + // Scale from window coordinates to graphics coordinates + int w, h; + SDL_GetWindowSize(graphics.window, &w, &h); + mouseX = mouseX * 640 / w; + mouseY = mouseY * 480 / h; + while (SDL_PollEvent(&event)) { switch (event.type) @@ -192,7 +188,7 @@ void Engine::getInput() if (waitForButton) { - if (event.key.keysym.sym == SDLK_ESCAPE) + if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { lastButtonPressed = -1; *highlightedWidget->value = abs(*highlightedWidget->value) - 1000; @@ -201,7 +197,7 @@ void Engine::getInput() allowJoypad = false; } - if (event.key.keysym.sym == SDLK_BACKSPACE) + if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) { lastButtonPressed = -2; *highlightedWidget->value = -2; @@ -215,13 +211,13 @@ void Engine::getInput() if (waitForKey) { - if (event.key.keysym.sym == SDLK_ESCAPE) + if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { *highlightedWidget->value = -*highlightedWidget->value; } else { - *highlightedWidget->value = event.key.keysym.sym; + *highlightedWidget->value = event.key.keysym.scancode; } lastButtonPressed = -1; @@ -233,29 +229,33 @@ void Engine::getInput() return; } - keyState[event.key.keysym.sym] = 1; - strcpy(lastKeyPressed, SDL_GetKeyName(event.key.keysym.sym)); + keyState[event.key.keysym.scancode] = 1; + strlcpy(lastKeyPressed, SDL_GetKeyName(SDL_GetKeyFromScancode(event.key.keysym.scancode)), sizeof lastKeyPressed); addKeyEvent(); break; case SDL_KEYUP: - keyState[event.key.keysym.sym] = 0; + keyState[event.key.keysym.scancode] = 0; break; case SDL_JOYAXISMOTION: if (event.jaxis.axis == 0) { - joyX = event.jaxis.value; + joyX = event.jaxis.value; + int joycurX = joyX < -25000 ? -1 : joyX > 25000 ? 1 : 0; + if (joycurX != joyprevX) + joykeyX = joyprevX = joycurX; } else if (event.jaxis.axis == 1) { - joyY = event.jaxis.value; + joyY = event.jaxis.value; + int joycurY = joyY < -25000 ? -1 : joyY > 25000 ? 1 : 0; + if (joycurY != joyprevY) + joykeyY = joyprevY = joycurY; } - break; case SDL_JOYBUTTONDOWN: - if (waitForButton) { lastButtonPressed = event.jbutton.button; @@ -267,10 +267,17 @@ void Engine::getInput() } joystickState[event.jbutton.button] = 1; + joykeyFire = true; break; case SDL_JOYBUTTONUP: joystickState[event.jbutton.button] = 0; + joykeyFire = false; + break; + + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) + paused = true; break; default: @@ -279,24 +286,24 @@ void Engine::getInput() } } -int Engine::getMouseX() +int Engine::getMouseX() const { return mouseX; } -int Engine::getMouseY() +int Engine::getMouseY() const { return mouseY; } void Engine::setMouse(int x, int y) { - SDL_WarpMouse(x, y); + SDL_WarpMouseInWindow(graphics.window, x, y); } bool Engine::userAccepts() { - if ((keyState[SDLK_SPACE]) || (keyState[SDLK_ESCAPE]) || (keyState[SDLK_LCTRL]) || (keyState[SDLK_RCTRL]) || (keyState[SDLK_RETURN]) || (keyState[SDLK_LCTRL])) + if ((keyState[SDL_SCANCODE_SPACE]) || (keyState[SDL_SCANCODE_ESCAPE]) || (keyState[SDL_SCANCODE_LCTRL]) || (keyState[SDL_SCANCODE_RCTRL]) || (keyState[SDL_SCANCODE_RETURN]) || (keyState[SDL_SCANCODE_LCTRL]) || joykeyFire) { return true; } @@ -311,15 +318,16 @@ void Engine::flushInput() void Engine::clearInput() { - for (int i = 0 ; i < 350 ; i++) - keyState[i] = 0; + memset(keyState, 0, sizeof keyState); mouseLeft = mouseRight = 0; + joykeyX = joykeyY = 0; + joykeyFire = false; } void Engine::setUserHome(const char *path) { - strcpy(userHomeDirectory, path); + strlcpy(userHomeDirectory, path, sizeof userHomeDirectory); debug(("User Home = %s\n", path)); } @@ -336,18 +344,16 @@ since SDL currently provides no means to load music directly from memory */ bool Engine::unpack(const char *filename, int fileType) { + bool ret = true; + if (fileType == PAK_DATA) { - if (dataBuffer != NULL) - delete[] dataBuffer; - + delete[] dataBuffer; dataBuffer = NULL; } else { - if (binaryBuffer != NULL) - delete[] binaryBuffer; - + delete[] binaryBuffer; binaryBuffer = NULL; } @@ -376,7 +382,7 @@ bool Engine::unpack(const char *filename, int fileType) } } - if ((fileType == PAK_MUSIC) || (fileType == PAK_FONT)) + if ((fileType == PAK_MUSIC) || (fileType == PAK_FONT) || (fileType == PAK_TAGS)) { char tempPath[PATH_MAX]; @@ -384,37 +390,47 @@ bool Engine::unpack(const char *filename, int fileType) if (fileType == PAK_MUSIC) { - sprintf(tempPath, "%smusic.mod", userHomeDirectory); + snprintf(tempPath, sizeof tempPath, "%smusic.mod", userHomeDirectory); + fp = fopen(tempPath, "wb"); + } + + if (fileType == PAK_TAGS) + { + snprintf(tempPath, sizeof tempPath, "%smusic.tags", userHomeDirectory); fp = fopen(tempPath, "wb"); } if (fileType == PAK_FONT) { - sprintf(tempPath, "%sfont.ttf", userHomeDirectory); + snprintf(tempPath, sizeof tempPath, "%sfont.ttf", userHomeDirectory); fp = fopen(tempPath, "wb"); } if (!fp) { + printf("Fatal Error: could not open %s for writing: %s", tempPath, strerror(errno)); return false; } - fwrite(binaryBuffer, 1, pak.getUncompressedSize(), fp); + if (fwrite(binaryBuffer, 1, pak.getUncompressedSize(), fp) != pak.getUncompressedSize()) + { + printf("Fatal Error: could not write to %s: %s", tempPath, strerror(errno)); + ret = false; + } fclose(fp); } - debug(("unpack() : Loaded %s (%d)\n", filename, pak.getUncompressedSize())); + debug(("unpack() : Loaded %s (%d), ret: %d\n", filename, pak.getUncompressedSize(), (int)ret)); - return true; + return ret; } bool Engine::loadData(const char *filename) { - if (dataBuffer != NULL) - { - delete[] dataBuffer; - dataBuffer = NULL; - } + bool ret = true; + + delete[] dataBuffer; + dataBuffer = NULL; #if USEPAK return unpack(filename, PAK_DATA); @@ -431,15 +447,18 @@ bool Engine::loadData(const char *filename) rewind(fp); - dataBuffer = new unsigned char[fSize]; + dataBuffer = new unsigned char[fSize + 1]; - fread(dataBuffer, 1, fSize, fp); + if (fread(dataBuffer, 1, fSize, fp) != (size_t)fSize) + ret = false; + + dataBuffer[fSize] = 0; fclose(fp); - debug(("loadData() : Loaded %s (%d)\n", filename, fSize)); + debug(("loadData() : Loaded %s (%d), ret: %d\n", filename, fSize, (int)ret)); - return true; + return ret; } void Engine::reportFontFailure() @@ -462,7 +481,7 @@ void Engine::setPlayerPosition(int x, int y, int limitLeft, int limitRight, int Math::limitInt(&playerPosY, limitUp, limitDown); } -int Engine::getFrameLoop() +int Engine::getFrameLoop() const { return frameLoop; } @@ -479,7 +498,7 @@ void Engine::doTimeDifference() time2 = SDL_GetTicks(); } -float Engine::getTimeDifference() +float Engine::getTimeDifference() const { return timeDifference; } @@ -493,7 +512,7 @@ void Engine::setInfoMessage(const char *message, int priority, int type) { if (priority >= messagePriority) { - strcpy(this->message, message); + strlcpy(this->message, message, sizeof this->message); messageTime = 180; messagePriority = priority; messageType = type; @@ -727,21 +746,21 @@ int Engine::processWidgets() { int update = 0; - if (keyState[SDLK_UP]) + if (keyState[SDL_SCANCODE_UP] || joykeyY < 0) { highlightWidget(-1); update = 1; clearInput(); } - if (keyState[SDLK_DOWN]) + if (keyState[SDL_SCANCODE_DOWN] || joykeyY > 0) { highlightWidget(1); update = 1; clearInput(); } - if (keyState[SDLK_LEFT] && (highlightedWidget->type != WG_BUTTON && highlightedWidget->type != WG_JOYPAD)) + if ((keyState[SDL_SCANCODE_LEFT] || joykeyX < 0) && (highlightedWidget->type != WG_BUTTON && highlightedWidget->type != WG_JOYPAD)) { SDL_Delay(1); @@ -758,7 +777,7 @@ int Engine::processWidgets() clearInput(); } - if (keyState[SDLK_RIGHT] && (highlightedWidget->type != WG_BUTTON && highlightedWidget->type != WG_JOYPAD)) + if ((keyState[SDL_SCANCODE_RIGHT] || joykeyX > 0) && (highlightedWidget->type != WG_BUTTON && highlightedWidget->type != WG_JOYPAD)) { SDL_Delay(1); @@ -775,7 +794,7 @@ int Engine::processWidgets() clearInput(); } - if ((keyState[SDLK_RETURN]) || (keyState[SDLK_SPACE]) || (keyState[SDLK_LCTRL])) + if ((keyState[SDL_SCANCODE_RETURN]) || (keyState[SDL_SCANCODE_SPACE]) || (keyState[SDL_SCANCODE_LCTRL]) || (joykeyFire && highlightedWidget->type != WG_JOYPAD)) { if (highlightedWidget->value == NULL) { @@ -815,6 +834,39 @@ int Engine::processWidgets() clearInput(); } + if (joykeyX > 0 && highlightedWidget->type == WG_JOYPAD) + { + waitForButton = true; + waitForKey = false; + allowJoypad = true; + + if (*highlightedWidget->value > -1000) + { + *highlightedWidget->value = (-1000 - *highlightedWidget->value); + } + + clearInput(); + } + + if (joykeyX < 0 && highlightedWidget->type == WG_JOYPAD) + { + if (waitForButton) + { + lastButtonPressed = -1; + *highlightedWidget->value = abs(*highlightedWidget->value) - 1000; + } + else + { + lastButtonPressed = -2; + *highlightedWidget->value = -2; + } + + waitForButton = false; + allowJoypad = false; + highlightedWidget->redraw(); + clearInput(); + } + return update; } @@ -961,7 +1013,7 @@ int Engine::getValueOfFlagTokens(const char *realLine) char line[1024]; bool found; int value; - strcpy(line, realLine); + strlcpy(line, realLine, sizeof line); int flags = 0;