0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

replayability added,

popup window for victory added,
png files for new skins and popups added
This commit is contained in:
Markus 2024-01-29 15:04:44 -06:00
parent fa480f916f
commit 0a1747d651
15 changed files with 192 additions and 23 deletions

4
.gitmodules vendored
View File

@ -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

View File

@ -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})

BIN
assets/Player1Victory.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

BIN
assets/Player2Victory.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
assets/iconImage.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

1
extern/SDL_ttf vendored Submodule

@ -0,0 +1 @@
Subproject commit 4a318f8dfaa1bb6f10e0c5e54052e25d3c7f3440

View File

@ -27,7 +27,9 @@ public:
static std::vector<ColliderComponent*> colliders;
static AssetManager* assets;
bool getWinner();
bool getWinner() const;
void refreshPlayers();
private:
int counter = 0;

View File

@ -19,6 +19,8 @@ public:
void createAllHearts();
void createHeartComponents(int x);
void setHealth(int health) {this->health = health;}
private:

34
include/PopupWindow.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef SDL_MINIGAME_POPUPWINDOW_H
#define SDL_MINIGAME_POPUPWINDOW_H
#include <SDL.h>
#include <SDL_ttf.h>
#include <string>
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

View File

@ -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<TransformComponent>(600, 500, 2);
enemy.addComponent<SpriteComponent>("assets/chicken_spritesheet.png", true);
enemy.addComponent<SpriteComponent>("assets/chicken_wizzard_spritesheet.png", true);
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0));
enemy.addComponent<ColliderComponent>("enemy", 0.8f);
enemy.addComponent<HealthComponent>(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;
}
}
void Game::refreshPlayers() {
for(auto& p : projectiles)
p->destroy();
player.getComponent<TransformComponent>().position.x = 80;
player.getComponent<TransformComponent>().position.y = 80;
player.getComponent<HealthComponent>().setHealth(5);
enemy.getComponent<HealthComponent>().setHealth(5);
player.getComponent<HealthComponent>().createAllHearts();
enemy.getComponent<HealthComponent>().createAllHearts();
isRunning = true;
update();
}

76
src/PopupWindow.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <iostream>
#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();
}
}

View File

@ -1,10 +1,14 @@
#include <iostream>
#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;
}