From b4e1c2b9ab9e2df1bba750932f762e183a3ca30c Mon Sep 17 00:00:00 2001 From: Keith Rosenberg Date: Sun, 15 Nov 2020 12:02:17 -0500 Subject: [PATCH] Fix issue with Distance text accessing invalid memory Fix score functions returning local variables that become inaccessible Remove unused variables triggering compiler warnings Add .gitignore to keep emscripten-generated files out Add emscripten compile to Makefile --- .gitignore | 9 +++++++++ Makefile | 6 +++++- README.md | 12 ++++++++++++ include/player.h | 4 ++-- src/groundtile.cpp | 1 - src/main.cpp | 10 +++++++--- src/player.cpp | 10 +++++----- src/renderwindow.cpp | 2 +- 8 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f088357 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# IDE Files +.vscode/ + +# emscripten-generated files +index.data +index.html +index.js +index.wasm + diff --git a/Makefile b/Makefile index 1426926..77b8d7b 100644 --- a/Makefile +++ b/Makefile @@ -28,4 +28,8 @@ winObjCompile: mkdir -p ./make/build ${CC} ./src/*.cpp ${CFLAGSO} -mwindows # laymans way to move object files to make/build folder - mv *.o ./make/build \ No newline at end of file + mv *.o ./make/build + +web: + em++ -std=c++14 -Wall src/main.cpp src/entity.cpp src/renderwindow.cpp src/player.cpp src/ground.cpp src/groundtile.cpp -I include -O2 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS=\['png'\] -s USE_SDL_TTF=2 -s USE_SDL_MIXER=2 --preload-file res -o index.html + diff --git a/README.md b/README.md index 3497012..b11a5ee 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,25 @@ After installing the dev packages of SDL2 for your distribution, execute the fol g++ -c src/*.cpp -std=c++14 -O3 -Wall -m64 -I include && mkdir -p bin/release && g++ *.o -o bin/release/main -s -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer ``` The compiled binary ``main`` is located in ``./bin``. For it to run, you must copy the ``./res`` folder to its directory. + ### Web Install [emscripten](https://emscripten.org/docs/getting_started/downloads.html) and execute the following command in the project's root directory: ``` emcc src/main.cpp src/entity.cpp src/renderwindow.cpp src/player.cpp src/ground.cpp src/groundtile.cpp -I include -O2 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s \"SDL2_IMAGE_FORMATS=['png']\" -s USE_SDL_TTF=2 -s USE_SDL_MIXER=2 --preload-file res -o index.html ``` + +You can also just run `make web` to achieve the same compilation. + The compiled ``.js``, ``.wasm``, ``.data``, and ``.html`` files are located in the project's root. +You can use `python` to run a server on localhost port 8000 like so: + +``` +python -m SimpleHTTPServer +``` + +and visit [http://localhost:8000](http://localhost:8000) to run the game in the browser. + ## Contributing Pull requests are welcome! For major refactors, please open an issue first to discuss what you would like to improve. Feel free to create a fork of this repository or use the code for any other noncommercial purposes. diff --git a/include/player.h b/include/player.h index 6c07ecc..0979c91 100644 --- a/include/player.h +++ b/include/player.h @@ -14,8 +14,8 @@ class Player : public Entity { float distanceFromCursor(); bool jump(); void update(Ground& ground); - const char* getScore(); - const char* getHighscore(); + std::string getScore(); + std::string getHighscore(); int getScoreInt(); int isDead(); void reset(); diff --git a/src/groundtile.cpp b/src/groundtile.cpp index 3af9e90..198c0df 100644 --- a/src/groundtile.cpp +++ b/src/groundtile.cpp @@ -1,6 +1,5 @@ #include "groundtile.h" -const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 480; GroundTile::GroundTile(SDL_Texture* p_tex, int p_index) diff --git a/src/main.cpp b/src/main.cpp index 6ef4c7f..249f144 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -200,10 +200,14 @@ void gameLoop() window.render(ground.getTile(i)); } window.render(25, 30, arrow); - window.render(62, 20, player.getScore(), font32_outline, black); - window.render(65, 23, player.getScore(), font32, white); + + std::string distanceScore = player.getScore(); + std::string highScore = player.getHighscore(); + + window.render(62, 20, distanceScore.c_str(), font32_outline, black); + window.render(65, 23, distanceScore.c_str(), font32, white); window.render(0, 65, highscoreBox); - window.render(65, 64, player.getHighscore(), font16, white); + window.render(65, 64, highScore.c_str(), font16, white); if (player.isDead() != ALIVE) { diff --git a/src/player.cpp b/src/player.cpp index e4c316e..ce0afe0 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "player.h" #include "entity.h" @@ -11,7 +12,6 @@ const float GRAVITY = 0.09f; const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 480; -const int ALIVE = 0; const int CURSOR_DEATH = 1; const int HOLE_DEATH = 2; @@ -138,18 +138,18 @@ void Player::update(Ground& ground) } -const char* Player::getScore() +std::string Player::getScore() { std::string s = std::to_string(score); s = "DISTANCE: " + s; - return s.c_str(); + return s; } -const char* Player::getHighscore() +std::string Player::getHighscore() { std::string s = std::to_string(highscore); s = "BEST: " + s; - return s.c_str(); + return s; } int Player::getScoreInt() diff --git a/src/renderwindow.cpp b/src/renderwindow.cpp index c011474..119f9f8 100644 --- a/src/renderwindow.cpp +++ b/src/renderwindow.cpp @@ -86,7 +86,7 @@ void RenderWindow::render(SDL_Texture* p_tex) void RenderWindow::render(float p_x, float p_y, const char* p_text, TTF_Font* font, SDL_Color textColor) { - SDL_Surface* surfaceMessage = TTF_RenderText_Blended( font, p_text, textColor); + SDL_Surface* surfaceMessage = TTF_RenderText_Blended(font, p_text, textColor); SDL_Texture* message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); SDL_Rect src;