From 572aa86c44a4824e7928f6f0c9e468a34ddb9272 Mon Sep 17 00:00:00 2001 From: Christian Himmler Date: Mon, 28 Dec 2020 01:17:20 +0100 Subject: [PATCH 1/2] Added CMake, Fixed Memory Leak, Fixed Score --- .gitignore | 85 ++++++++++++++++++++++++++++++++++++++++++ .gitmodules | 3 ++ .vscode/settings.json | 7 ++++ CMakeLists.txt | 27 ++++++++++++++ Makefile | 31 --------------- cmake/sdl2 | 1 + include/entity.h | 4 +- include/ground.h | 4 +- include/groundtile.h | 4 +- include/player.h | 7 ++-- include/renderwindow.h | 6 +-- src/entity.cpp | 16 ++++---- src/ground.cpp | 4 +- src/main.cpp | 40 +++++++++++--------- src/player.cpp | 18 +++++---- src/renderwindow.cpp | 9 +++-- 16 files changed, 186 insertions(+), 80 deletions(-) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .vscode/settings.json create mode 100644 CMakeLists.txt delete mode 100644 Makefile create mode 160000 cmake/sdl2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e66b1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,85 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# CMake +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +build/ + +# VS Code +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..14d180e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "cmake/sdl2"] + path = cmake/sdl2 + url = https://gitlab.com/aminosbh/sdl2-cmake-modules.git diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..172969a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools", + "files.associations": { + "iosfwd": "cpp", + "string": "cpp" + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..fbcf98e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,27 @@ +cmake_minimum_required(VERSION 3.19) + +project(cursor-custodian VERSION 0.1.0 LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2) + +# Find SDL2, SDL2_image and SDL2_ttf libraries +find_package(SDL2 REQUIRED) +find_package(SDL2_image REQUIRED) +find_package(SDL2_ttf REQUIRED) +find_package(SDL2_mixer REQUIRED) + +# Build game +add_compile_options(-Wall -Wextra -pedantic -Werror) +include_directories(include) + +FILE(GLOB_RECURSE SOURCES src/**.cpp) +add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES}) + +file(COPY res DESTINATION ${CMAKE_BINARY_DIR}) + +# Link SDL2::Main, SDL2::Image and SDL2::TTF to our project +target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2::Image SDL2::TTF SDL2::Mixer) \ No newline at end of file diff --git a/Makefile b/Makefile deleted file mode 100644 index 1426926..0000000 --- a/Makefile +++ /dev/null @@ -1,31 +0,0 @@ -# set compiler -CC = g++ -# include files -INCLUDE = -I ./include -#compilers flags for compiling object files -CFLAGSO = -std=c++14 -Wall -m64 -O3 -c ${INCLUDE} -# libraries -LIBS = -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer -#compilers flags for compiling binary file -CFLAGSB = -s ${LIBS} - -default: objCompile - mkdir -p ./make/bin - ${CC} ./make/build/*.o -o ./make/bin/main ${CFLAGSB} - cp -r ./res ./make/bin/res - -objCompile: - mkdir -p ./make/build - ${CC} ./src/*.cpp ${CFLAGSO} - # laymans way to move object files to make/build folder - mv *.o ./make/build -windows: winObjCompile - mkdir -p ./make/bin - ${CC} ./make/build/*.o -o ./make/bin/main ${CFLAGSB} -mwindows - cp -r ./res ./make/bin/res - -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 diff --git a/cmake/sdl2 b/cmake/sdl2 new file mode 160000 index 0000000..ad006a3 --- /dev/null +++ b/cmake/sdl2 @@ -0,0 +1 @@ +Subproject commit ad006a3daae65a612ed87415037e32188b81071e diff --git a/include/entity.h b/include/entity.h index a902a05..a871889 100644 --- a/include/entity.h +++ b/include/entity.h @@ -1,6 +1,6 @@ #pragma once -#include -#include +#include +#include #include class Entity diff --git a/include/ground.h b/include/ground.h index d0c2fe4..5f6489e 100644 --- a/include/ground.h +++ b/include/ground.h @@ -1,6 +1,6 @@ #pragma once -#include -#include +#include +#include #include #include "groundtile.h" diff --git a/include/groundtile.h b/include/groundtile.h index 1bd18c8..d2e12d9 100644 --- a/include/groundtile.h +++ b/include/groundtile.h @@ -1,6 +1,6 @@ #pragma once -#include -#include +#include +#include #include #include "entity.h" diff --git a/include/player.h b/include/player.h index 6c07ecc..fd526c2 100644 --- a/include/player.h +++ b/include/player.h @@ -1,6 +1,6 @@ #pragma once -#include -#include +#include +#include #include #include @@ -17,7 +17,8 @@ class Player : public Entity { const char* getScore(); const char* getHighscore(); int getScoreInt(); - int isDead(); + int getDeadType(); + bool isDead(); void reset(); private: float velocityX, velocityY; diff --git a/include/renderwindow.h b/include/renderwindow.h index f2d42b1..82f4651 100644 --- a/include/renderwindow.h +++ b/include/renderwindow.h @@ -1,7 +1,7 @@ #pragma once -#include -#include -#include +#include +#include +#include #include "entity.h" diff --git a/src/entity.cpp b/src/entity.cpp index 2218412..9b845b5 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -1,11 +1,11 @@ -#include -#include +#include +#include #include #include "entity.h" -Entity::Entity(float p_x, float p_y, std::vector p_tex) -:x(p_x), y(p_y), tex(p_tex) +Entity::Entity(float p_x, float p_y, std::vector p_tex) + : x(p_x), y(p_y), tex(p_tex) { currentFrame.x = 0; currentFrame.y = 0; @@ -17,8 +17,8 @@ Entity::Entity(float p_x, float p_y, std::vector p_tex) } } -Entity::Entity(float p_x, float p_y, SDL_Texture* p_tex) -:x(p_x), y(p_y) +Entity::Entity(float p_x, float p_y, SDL_Texture *p_tex) + : x(p_x), y(p_y) { tex.push_back(p_tex); currentFrame.x = 0; @@ -86,7 +86,7 @@ void Entity::setAnimOffsetY(int p_index, int p_value) animOffsetsY[p_index] = p_value; } -SDL_Texture* Entity::getTex(int p_index) +SDL_Texture *Entity::getTex(int p_index) { return tex.at(p_index); } @@ -96,7 +96,7 @@ SDL_Rect Entity::getCurrentFrame() return currentFrame; } -void Entity::setTex(SDL_Texture* p_tex) +void Entity::setTex(SDL_Texture *p_tex) { tex[0] = p_tex; } \ No newline at end of file diff --git a/src/ground.cpp b/src/ground.cpp index cac84b1..c20e74d 100644 --- a/src/ground.cpp +++ b/src/ground.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include diff --git a/src/main.cpp b/src/main.cpp index 6ef4c7f..0ee7c6c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,10 +4,10 @@ #endif -#include -#include -#include -#include +#include +#include +#include +#include #include #include #include @@ -42,8 +42,8 @@ TTF_Font* font32_outline; TTF_Font* font24; TTF_Font* font16; -SDL_Color white = { 255, 255, 255 }; -SDL_Color black = { 0, 0, 0 }; +SDL_Color white = { 255, 255, 255, 255 }; +SDL_Color black = { 0, 0, 0, 255 }; Mix_Chunk* jumpSfx; Mix_Chunk* fallSfx; @@ -85,7 +85,7 @@ bool init() font32_outline = TTF_OpenFont("res/fonts/cocogoose.ttf", 32); font24 = TTF_OpenFont("res/fonts/cocogoose.ttf", 24); font16 = TTF_OpenFont("res/fonts/cocogoose.ttf", 16); - TTF_SetFontOutline(font32_outline, 3); + TTF_SetFontOutline(font32_outline, 3); jumpSfx = Mix_LoadWAV("res/sounds/jump.wav"); fallSfx = Mix_LoadWAV("res/sounds/fall.wav"); @@ -106,6 +106,7 @@ void reset() player.reset(); ground.reset(); } + void gameLoop() { SDL_Event event; @@ -129,14 +130,14 @@ void gameLoop() } else { - if (event.button.button == SDL_BUTTON_LEFT && player.isDead() == ALIVE) + if (event.button.button == SDL_BUTTON_LEFT && player.getDeadType() == ALIVE) { if (player.jump()) { Mix_PlayChannel(-1, jumpSfx, 0); } } - else if (player.isDead() != ALIVE) + else if (player.isDead()) { Mix_PlayChannel(-1, clickSfx, 0); reset(); @@ -147,6 +148,7 @@ void gameLoop() } } } + if (mainMenu) { if (SDL_GetTicks() < 2500) @@ -171,21 +173,21 @@ void gameLoop() } else { - if (player.isDead() != CURSOR_DEATH) + if (player.getDeadType() != CURSOR_DEATH) { ground.update(player.getScoreInt()); } - if (player.isDead() == ALIVE) + if (!player.isDead()) { player.update(ground); } else if (!playedDeathSFX) { - if (player.isDead() == CURSOR_DEATH) + if (player.getDeadType() == CURSOR_DEATH) { Mix_PlayChannel(-1, hitSfx, 0); } - else if (player.isDead() == HOLE_DEATH) + else if (player.getDeadType() == HOLE_DEATH) { Mix_PlayChannel(-1, fallSfx, 0); } @@ -205,24 +207,28 @@ void gameLoop() window.render(0, 65, highscoreBox); window.render(65, 64, player.getHighscore(), font16, white); - if (player.isDead() != ALIVE) + if (player.isDead()) { window.render(deathOverlay); - if (player.isDead() == CURSOR_DEATH) + + if (player.getDeadType() == CURSOR_DEATH) { window.renderCenter(0, -24, "The cursor is deadly to the player...", font24, white); } - else if (player.isDead() == HOLE_DEATH) + + else if (player.getDeadType() == HOLE_DEATH) { window.renderCenter(0, -24, "The hole had no bottom...", font24, white); } + window.renderCenter(0, 12, "Click to retry.", font16, white); + } window.display(); } } -int main(int argc, char* args[]) +int main() { #ifdef __EMSCRIPTEN__ emscripten_set_main_loop(gameLoop, 0, 1); diff --git a/src/player.cpp b/src/player.cpp index e4c316e..776ba82 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include @@ -140,15 +140,15 @@ void Player::update(Ground& ground) const char* Player::getScore() { - std::string s = std::to_string(score); - s = "DISTANCE: " + s; + static std::string s; + s = "DISTANCE: " + std::to_string(score); return s.c_str(); } const char* Player::getHighscore() { - std::string s = std::to_string(highscore); - s = "BEST: " + s; + static std::string s; + s = "BEST: " + std::to_string(highscore); return s.c_str(); } @@ -157,11 +157,15 @@ int Player::getScoreInt() return score; } -int Player::isDead() +int Player::getDeadType() { return dead; } +bool Player::isDead() { + return dead != ALIVE; +} + void Player::reset() { setX(SCREEN_WIDTH/2 - getWidth()/2); diff --git a/src/renderwindow.cpp b/src/renderwindow.cpp index c011474..b7d5fe8 100644 --- a/src/renderwindow.cpp +++ b/src/renderwindow.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #include "renderwindow.h" @@ -103,6 +103,7 @@ void RenderWindow::render(float p_x, float p_y, const char* p_text, TTF_Font* fo SDL_RenderCopy(renderer, message, &src, &dst); SDL_FreeSurface(surfaceMessage); + SDL_DestroyTexture(message); } void RenderWindow::renderCenter(float p_x, float p_y, const char* p_text, TTF_Font* font, SDL_Color textColor) @@ -124,6 +125,7 @@ void RenderWindow::renderCenter(float p_x, float p_y, const char* p_text, TTF_Fo SDL_RenderCopy(renderer, message, &src, &dst); SDL_FreeSurface(surfaceMessage); + SDL_DestroyTexture(message); } void RenderWindow::display() @@ -134,4 +136,5 @@ void RenderWindow::display() void RenderWindow::cleanUp() { SDL_DestroyWindow(window); + SDL_DestroyRenderer(renderer); } \ No newline at end of file From 92f6064e32ff712744b2827569d9ac2a5b70e035 Mon Sep 17 00:00:00 2001 From: Christian Himmler Date: Mon, 28 Dec 2020 22:10:40 +0100 Subject: [PATCH 2/2] Added cmake pipeline for emscripten --- CMakeLists.txt | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fbcf98e..acc92e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,13 +9,22 @@ set(CMAKE_CXX_EXTENSIONS OFF) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2) # Find SDL2, SDL2_image and SDL2_ttf libraries -find_package(SDL2 REQUIRED) -find_package(SDL2_image REQUIRED) -find_package(SDL2_ttf REQUIRED) -find_package(SDL2_mixer REQUIRED) +if( ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten") + set(USE_FLAGS "-s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS=['png'] -s USE_SDL_TTF=2 -s USE_SDL_MIXER=2 -s USE_FREETYPE=1 --preload-file res") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${USE_FLAGS}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${USE_FLAGS}") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${USE_FLAGS}") + set(CMAKE_EXECUTABLE_SUFFIX .html) +else() + find_package(SDL2 REQUIRED) + find_package(SDL2_image REQUIRED) + find_package(SDL2_ttf REQUIRED) + find_package(SDL2_mixer REQUIRED) + set(SDL2_LIBRARIES SDL2::Main SDL2::Image SDL2::TTF SDL2::Mixer) +endif() # Build game -add_compile_options(-Wall -Wextra -pedantic -Werror) +add_compile_options(-Wall -Wextra) include_directories(include) FILE(GLOB_RECURSE SOURCES src/**.cpp) @@ -24,4 +33,4 @@ add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES}) file(COPY res DESTINATION ${CMAKE_BINARY_DIR}) # Link SDL2::Main, SDL2::Image and SDL2::TTF to our project -target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2::Image SDL2::TTF SDL2::Mixer) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES}) \ No newline at end of file