diff --git a/.gitmodules b/.gitmodules index e1f447a..f8904e1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,3 +14,7 @@ path = extern/zlib url = https://github.com/madler/zlib.git branch = master +[submodule "extern/SDL_ttf"] + path = extern/SDL_ttf + url = https://github.com/libsdl-org/SDL_ttf.git + branch = release-2.22.x diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e5c48a..72d3221 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,11 @@ set(PROJECT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(BUILD_SHARED_LIBS FALSE) +set(SDL2TTF_VENDORED ON) + add_subdirectory(extern/SDL EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_image EXCLUDE_FROM_ALL) +add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL) file(GLOB_RECURSE SOURCES ${PROJECT_SOURCE_DIR}/src/*.cpp) add_executable(${PROJECT_NAME} ${SOURCES}) @@ -21,9 +24,10 @@ add_executable(${PROJECT_NAME} ${SOURCES}) target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_INCLUDE_DIR}) target_link_libraries(${PROJECT_NAME} PRIVATE - SDL2::SDL2main - SDL2::SDL2-static - SDL2_image::SDL2_image-static + SDL2::SDL2main + SDL2::SDL2-static + SDL2_image::SDL2_image-static + SDL2_ttf::SDL2_ttf-static ) file(COPY ${PROJECT_SOURCE_DIR}/assets DESTINATION ${PROJECT_BINARY_DIR}) \ No newline at end of file diff --git a/assets/Player1Victory.png b/assets/Player1Victory.png new file mode 100644 index 0000000..f50a386 Binary files /dev/null and b/assets/Player1Victory.png differ diff --git a/assets/Player2Victory.png b/assets/Player2Victory.png new file mode 100644 index 0000000..7a20be3 Binary files /dev/null and b/assets/Player2Victory.png differ diff --git a/assets/VictoryBackup.aseprite b/assets/VictoryBackup.aseprite new file mode 100644 index 0000000..b53cd97 Binary files /dev/null and b/assets/VictoryBackup.aseprite differ diff --git a/assets/chicken_gentleman_spritesheet.png b/assets/chicken_gentleman_spritesheet.png new file mode 100644 index 0000000..848c426 Binary files /dev/null and b/assets/chicken_gentleman_spritesheet.png differ diff --git a/assets/chicken_wizzard_spritesheet.png b/assets/chicken_wizzard_spritesheet.png new file mode 100644 index 0000000..27e8796 Binary files /dev/null and b/assets/chicken_wizzard_spritesheet.png differ diff --git a/assets/iconImage.bmp b/assets/iconImage.bmp new file mode 100644 index 0000000..6eddbef Binary files /dev/null and b/assets/iconImage.bmp differ diff --git a/extern/SDL_ttf b/extern/SDL_ttf new file mode 160000 index 0000000..4a318f8 --- /dev/null +++ b/extern/SDL_ttf @@ -0,0 +1 @@ +Subproject commit 4a318f8dfaa1bb6f10e0c5e54052e25d3c7f3440 diff --git a/include/Game.h b/include/Game.h index ed07f38..1fd3cd4 100644 --- a/include/Game.h +++ b/include/Game.h @@ -27,7 +27,9 @@ public: static std::vector colliders; static AssetManager* assets; - bool getWinner(); + bool getWinner() const; + + void refreshPlayers(); private: int counter = 0; diff --git a/include/HealthComponent.h b/include/HealthComponent.h index 7cfd2e7..2dd0737 100644 --- a/include/HealthComponent.h +++ b/include/HealthComponent.h @@ -19,6 +19,8 @@ public: void createAllHearts(); void createHeartComponents(int x); + void setHealth(int health) {this->health = health;} + private: diff --git a/include/PopupWindow.h b/include/PopupWindow.h new file mode 100644 index 0000000..e09d9b1 --- /dev/null +++ b/include/PopupWindow.h @@ -0,0 +1,34 @@ +#ifndef SDL_MINIGAME_POPUPWINDOW_H +#define SDL_MINIGAME_POPUPWINDOW_H + +#include +#include +#include + +class Game; + +class PopupWindow { + +public: + PopupWindow(const char* title, const std::string& message); + ~PopupWindow(); + + void handleWinnerEvents(); + bool shouldContinue() const; + + bool interacted; + + void renderWinnerPopup(bool winner); + +private: + SDL_Renderer* renderer; + SDL_Window* window; + SDL_Texture* texture; + bool continueGame; + + +}; + + + +#endif //SDL_MINIGAME_POPUPWINDOW_H diff --git a/src/Game.cpp b/src/Game.cpp index a979a8b..d7c580f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -47,6 +47,15 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo return; } + SDL_Surface* icon = SDL_LoadBMP("assets/iconImage.bmp"); + if(!icon) + { + std::cout << "ERROR: Couldn't create icon!" << std::endl; + return; + } + + SDL_SetWindowIcon(window, icon); + renderer = SDL_CreateRenderer(window, -1, 0); if (!renderer) { @@ -102,11 +111,11 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo map->loadMap("assets/SDL_map_test.txt", 25, 20); //adding textures to the library in AssetManager - + /* assets->addTexture("player1", "assets/chicken_neutral_knight.png"); assets->addTexture("player2", "assets/chicken_neutral.png"); assets->addTexture("egg", "assets/egg.png"); - +*/ //ecs implementation @@ -118,12 +127,11 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order enemy.addComponent(600, 500, 2); - enemy.addComponent("assets/chicken_spritesheet.png", true); + enemy.addComponent("assets/chicken_wizzard_spritesheet.png", true); enemy.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0)); enemy.addComponent("enemy", 0.8f); enemy.addComponent(5, &manager, false); enemy.addGroup((size_t)GroupLabel::ENEMIES); - } auto& tiles(manager.getGroup((size_t)GroupLabel::MAP)); @@ -259,6 +267,26 @@ bool Game::running() const return isRunning; } -bool Game::getWinner() { +bool Game::getWinner() const { return this->winner; -} \ No newline at end of file +} + +void Game::refreshPlayers() { + + for(auto& p : projectiles) + p->destroy(); + + + player.getComponent().position.x = 80; + player.getComponent().position.y = 80; + + player.getComponent().setHealth(5); + enemy.getComponent().setHealth(5); + + player.getComponent().createAllHearts(); + enemy.getComponent().createAllHearts(); + + isRunning = true; + + update(); +} diff --git a/src/PopupWindow.cpp b/src/PopupWindow.cpp new file mode 100644 index 0000000..185d069 --- /dev/null +++ b/src/PopupWindow.cpp @@ -0,0 +1,76 @@ +#include +#include "PopupWindow.h" +#include "TextureManager.h" +#include "SDL_image.h" +#include "Game.h" + +PopupWindow::PopupWindow(const char* title, const std::string &message) : +continueGame(false), interacted(false) { + this->window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 250, 0); + //font = TTF_OpenFont("assets/Trajan.ttf", 24); // Change the path and size as needed + + this->renderer = SDL_CreateRenderer(window, -1, 0); + SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255); + + //SDL_Surface* surface = TTF_RenderText_Blended(font, message.c_str(), {255, 255, 255}); + //texture = SDL_CreateTextureFromSurface(renderer, surface); + + //SDL_FreeSurface(surface); +} + +PopupWindow::~PopupWindow() { + SDL_DestroyTexture(this->texture); + SDL_DestroyWindow(this->window); + SDL_DestroyRenderer(this->renderer); +} + +void PopupWindow::handleWinnerEvents() { + + SDL_Event e; + + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT) + { + continueGame = false; + interacted = true; + } + else if (e.type == SDL_KEYDOWN) + { + if (e.key.keysym.sym == SDLK_c) + { + continueGame = true; + interacted = true; + } + else if (e.key.keysym.sym == SDLK_q) + { + continueGame = false; + interacted = true; + } + } + } +} + +bool PopupWindow::shouldContinue() const { + return continueGame; +} + +void PopupWindow::renderWinnerPopup(bool winner) { + + SDL_RenderClear(this->renderer); + + this->texture = winner ? IMG_LoadTexture(this->renderer, "assets/Player1Victory.png") + : IMG_LoadTexture(this->renderer, "assets/Player2Victory.png"); + + SDL_RenderCopy(this->renderer, this->texture, NULL, NULL); + + SDL_RenderPresent(this->renderer); + + //Error handling for debugging + const char* sdlError = SDL_GetError(); + if (*sdlError != '\0') { + std::cerr << "SDL Error: " << sdlError << std::endl; + SDL_ClearError(); + } + +} diff --git a/src/main.cpp b/src/main.cpp index 2e7a993..7cb30c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,14 @@ +#include #include "Game.h" #include "Constants.h" +#include "PopupWindow.h" Game* game = nullptr; int main(int argc, char* argv[]) { + bool playing = true; + const int frameDelay = 1000 / FPS; Uint32 frameStart; @@ -13,23 +17,37 @@ int main(int argc, char* argv[]) game = new Game(); game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false); - while (game->running()) - { - frameStart = SDL_GetTicks(); + while(playing) { + std::cout << "djslfsldkfj" << std::endl; + while (game->running()) { + frameStart = SDL_GetTicks(); - game->handleEvents(); - game->update(); - game->render(); + game->handleEvents(); + game->update(); + game->render(); - frameTime = SDL_GetTicks() - frameStart; + frameTime = SDL_GetTicks() - frameStart; - if (frameDelay > frameTime) - { - SDL_Delay(frameDelay - frameTime); - } - } + if (frameDelay > frameTime) { + SDL_Delay(frameDelay - frameTime); + } + } + bool winner = game->getWinner(); - game->clean(); + PopupWindow popupWindow("Game over",winner ? "Player1 won! Press 'C' to continue or 'Q' to quit." + : "Player2 won! Press 'C' to continue or 'Q' to quit."); + + popupWindow.renderWinnerPopup(winner); + + while (!popupWindow.interacted) { + popupWindow.handleWinnerEvents(); + SDL_Delay(10); + } + playing = popupWindow.shouldContinue(); + game->refreshPlayers(); + } + + game->clean(); return 0; } \ No newline at end of file