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:
parent
fa480f916f
commit
0a1747d651
4
.gitmodules
vendored
4
.gitmodules
vendored
@ -14,3 +14,7 @@
|
|||||||
path = extern/zlib
|
path = extern/zlib
|
||||||
url = https://github.com/madler/zlib.git
|
url = https://github.com/madler/zlib.git
|
||||||
branch = master
|
branch = master
|
||||||
|
[submodule "extern/SDL_ttf"]
|
||||||
|
path = extern/SDL_ttf
|
||||||
|
url = https://github.com/libsdl-org/SDL_ttf.git
|
||||||
|
branch = release-2.22.x
|
||||||
|
|||||||
@ -12,8 +12,11 @@ set(PROJECT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
|
|||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
set(BUILD_SHARED_LIBS FALSE)
|
set(BUILD_SHARED_LIBS FALSE)
|
||||||
|
|
||||||
|
set(SDL2TTF_VENDORED ON)
|
||||||
|
|
||||||
add_subdirectory(extern/SDL EXCLUDE_FROM_ALL)
|
add_subdirectory(extern/SDL EXCLUDE_FROM_ALL)
|
||||||
add_subdirectory(extern/SDL_image 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)
|
file(GLOB_RECURSE SOURCES ${PROJECT_SOURCE_DIR}/src/*.cpp)
|
||||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||||
@ -24,6 +27,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
|
|||||||
SDL2::SDL2main
|
SDL2::SDL2main
|
||||||
SDL2::SDL2-static
|
SDL2::SDL2-static
|
||||||
SDL2_image::SDL2_image-static
|
SDL2_image::SDL2_image-static
|
||||||
|
SDL2_ttf::SDL2_ttf-static
|
||||||
)
|
)
|
||||||
|
|
||||||
file(COPY ${PROJECT_SOURCE_DIR}/assets DESTINATION ${PROJECT_BINARY_DIR})
|
file(COPY ${PROJECT_SOURCE_DIR}/assets DESTINATION ${PROJECT_BINARY_DIR})
|
||||||
BIN
assets/Player1Victory.png
Normal file
BIN
assets/Player1Victory.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.3 KiB |
BIN
assets/Player2Victory.png
Normal file
BIN
assets/Player2Victory.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.3 KiB |
BIN
assets/VictoryBackup.aseprite
Normal file
BIN
assets/VictoryBackup.aseprite
Normal file
Binary file not shown.
BIN
assets/chicken_gentleman_spritesheet.png
Normal file
BIN
assets/chicken_gentleman_spritesheet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
BIN
assets/chicken_wizzard_spritesheet.png
Normal file
BIN
assets/chicken_wizzard_spritesheet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
BIN
assets/iconImage.bmp
Normal file
BIN
assets/iconImage.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.1 KiB |
1
extern/SDL_ttf
vendored
Submodule
1
extern/SDL_ttf
vendored
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 4a318f8dfaa1bb6f10e0c5e54052e25d3c7f3440
|
||||||
@ -27,7 +27,9 @@ public:
|
|||||||
static std::vector<ColliderComponent*> colliders;
|
static std::vector<ColliderComponent*> colliders;
|
||||||
static AssetManager* assets;
|
static AssetManager* assets;
|
||||||
|
|
||||||
bool getWinner();
|
bool getWinner() const;
|
||||||
|
|
||||||
|
void refreshPlayers();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
|
|||||||
@ -19,6 +19,8 @@ public:
|
|||||||
void createAllHearts();
|
void createAllHearts();
|
||||||
void createHeartComponents(int x);
|
void createHeartComponents(int x);
|
||||||
|
|
||||||
|
void setHealth(int health) {this->health = health;}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|||||||
34
include/PopupWindow.h
Normal file
34
include/PopupWindow.h
Normal 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
|
||||||
38
src/Game.cpp
38
src/Game.cpp
@ -47,6 +47,15 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
|||||||
return;
|
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);
|
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||||
if (!renderer)
|
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);
|
map->loadMap("assets/SDL_map_test.txt", 25, 20);
|
||||||
|
|
||||||
//adding textures to the library in AssetManager
|
//adding textures to the library in AssetManager
|
||||||
|
/*
|
||||||
assets->addTexture("player1", "assets/chicken_neutral_knight.png");
|
assets->addTexture("player1", "assets/chicken_neutral_knight.png");
|
||||||
assets->addTexture("player2", "assets/chicken_neutral.png");
|
assets->addTexture("player2", "assets/chicken_neutral.png");
|
||||||
assets->addTexture("egg", "assets/egg.png");
|
assets->addTexture("egg", "assets/egg.png");
|
||||||
|
*/
|
||||||
|
|
||||||
//ecs implementation
|
//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
|
player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
|
||||||
|
|
||||||
enemy.addComponent<TransformComponent>(600, 500, 2);
|
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<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<ColliderComponent>("enemy", 0.8f);
|
||||||
enemy.addComponent<HealthComponent>(5, &manager, false);
|
enemy.addComponent<HealthComponent>(5, &manager, false);
|
||||||
enemy.addGroup((size_t)GroupLabel::ENEMIES);
|
enemy.addGroup((size_t)GroupLabel::ENEMIES);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& tiles(manager.getGroup((size_t)GroupLabel::MAP));
|
auto& tiles(manager.getGroup((size_t)GroupLabel::MAP));
|
||||||
@ -259,6 +267,26 @@ bool Game::running() const
|
|||||||
return isRunning;
|
return isRunning;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::getWinner() {
|
bool Game::getWinner() const {
|
||||||
return this->winner;
|
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
76
src/PopupWindow.cpp
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
26
src/main.cpp
26
src/main.cpp
@ -1,10 +1,14 @@
|
|||||||
|
#include <iostream>
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
#include "Constants.h"
|
#include "Constants.h"
|
||||||
|
#include "PopupWindow.h"
|
||||||
|
|
||||||
Game* game = nullptr;
|
Game* game = nullptr;
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
bool playing = true;
|
||||||
|
|
||||||
const int frameDelay = 1000 / FPS;
|
const int frameDelay = 1000 / FPS;
|
||||||
|
|
||||||
Uint32 frameStart;
|
Uint32 frameStart;
|
||||||
@ -13,8 +17,9 @@ int main(int argc, char* argv[])
|
|||||||
game = new Game();
|
game = new Game();
|
||||||
|
|
||||||
game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false);
|
game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false);
|
||||||
while (game->running())
|
while(playing) {
|
||||||
{
|
std::cout << "djslfsldkfj" << std::endl;
|
||||||
|
while (game->running()) {
|
||||||
frameStart = SDL_GetTicks();
|
frameStart = SDL_GetTicks();
|
||||||
|
|
||||||
game->handleEvents();
|
game->handleEvents();
|
||||||
@ -23,11 +28,24 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
frameTime = SDL_GetTicks() - frameStart;
|
frameTime = SDL_GetTicks() - frameStart;
|
||||||
|
|
||||||
if (frameDelay > frameTime)
|
if (frameDelay > frameTime) {
|
||||||
{
|
|
||||||
SDL_Delay(frameDelay - frameTime);
|
SDL_Delay(frameDelay - frameTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bool winner = game->getWinner();
|
||||||
|
|
||||||
|
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();
|
game->clean();
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user