mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 07:53:43 +00:00
Merge pull request #25 from Nimca0/menu
This commit is contained in:
commit
93bbd83d37
4
.gitmodules
vendored
4
.gitmodules
vendored
@ -10,3 +10,7 @@
|
||||
path = extern/SDL_mixer
|
||||
url = https://github.com/libsdl-org/SDL_mixer.git
|
||||
branch = release-2.8.x
|
||||
[submodule "extern/SDL_ttf"]
|
||||
path = extern/SDL_ttf
|
||||
url = https://github.com/libsdl-org/SDL_ttf.git
|
||||
branch = release-2.22.x
|
||||
|
||||
@ -13,10 +13,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
set(BUILD_SHARED_LIBS FALSE)
|
||||
|
||||
set(SDL2MIXER_VENDORED ON)
|
||||
set(SDL2TTF_VENDORED ON)
|
||||
|
||||
add_subdirectory(extern/SDL EXCLUDE_FROM_ALL)
|
||||
add_subdirectory(extern/SDL_image EXCLUDE_FROM_ALL)
|
||||
add_subdirectory(extern/SDL_mixer 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})
|
||||
@ -28,6 +30,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
SDL2::SDL2-static
|
||||
SDL2_image::SDL2_image-static
|
||||
SDL2_mixer::SDL2_mixer-static
|
||||
SDL2_ttf::SDL2_ttf-static
|
||||
)
|
||||
|
||||
if(CMAKE_BUILD_TYPE MATCHES "Debug")
|
||||
|
||||
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
|
||||
@ -36,9 +36,11 @@ public:
|
||||
static TextureManager* textureManager;
|
||||
static SoundManager* soundManager;
|
||||
|
||||
void refreshPlayers();
|
||||
TeamLabel getWinner() const;
|
||||
|
||||
private:
|
||||
void setWinner(TeamLabel winningTeam);
|
||||
TeamLabel getWinner();
|
||||
|
||||
int counter = 0;
|
||||
bool isRunning = false;
|
||||
|
||||
@ -13,6 +13,7 @@ public:
|
||||
~HealthComponent() {}
|
||||
|
||||
void modifyHealth(int health = -1);
|
||||
void setHealth(int health);
|
||||
int getHealth() { return this->health; }
|
||||
|
||||
void init() override;
|
||||
@ -20,6 +21,7 @@ public:
|
||||
void refreshHearts();
|
||||
void createHeartComponents(int x);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
int health;
|
||||
|
||||
27
include/PopupWindow.h
Normal file
27
include/PopupWindow.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <SDL.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <string>
|
||||
|
||||
class Game;
|
||||
enum class TeamLabel;
|
||||
|
||||
class PopupWindow {
|
||||
|
||||
public:
|
||||
PopupWindow(const char* title, const std::string& message);
|
||||
~PopupWindow();
|
||||
|
||||
void handleWinnerEvents();
|
||||
bool shouldContinue() const;
|
||||
|
||||
bool interacted;
|
||||
|
||||
void renderWinnerPopup(TeamLabel winner);
|
||||
|
||||
private:
|
||||
SDL_Renderer* renderer;
|
||||
SDL_Window* window;
|
||||
SDL_Texture* texture;
|
||||
bool continueGame;
|
||||
};
|
||||
33
src/Game.cpp
33
src/Game.cpp
@ -63,6 +63,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)
|
||||
{
|
||||
@ -139,17 +148,17 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
|
||||
//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");
|
||||
|
||||
*/
|
||||
// loading sounds
|
||||
assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
|
||||
assets->addSoundEffect("steps", "assets/sound/steps.wav");
|
||||
|
||||
//ecs implementation
|
||||
|
||||
|
||||
player1.setTeam(TeamLabel::BLUE);
|
||||
player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
|
||||
player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed
|
||||
@ -168,7 +177,6 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
player2.addComponent<HealthComponent>(5, Direction::RIGHT);
|
||||
player2.addComponent<StatEffectsComponent>();
|
||||
player2.addGroup((size_t) GroupLabel::PLAYERS);
|
||||
|
||||
}
|
||||
|
||||
void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite)
|
||||
@ -359,7 +367,24 @@ void Game::setWinner(TeamLabel winningTeam)
|
||||
this->isRunning = false;
|
||||
}
|
||||
|
||||
TeamLabel Game::getWinner()
|
||||
TeamLabel Game::getWinner() const
|
||||
{
|
||||
return this->winner;
|
||||
}
|
||||
|
||||
void Game::refreshPlayers() {
|
||||
|
||||
for(auto& p : projectiles) {
|
||||
p->destroy();
|
||||
}
|
||||
|
||||
player1.getComponent<TransformComponent>().position = Vector2D(80, 80);
|
||||
player2.getComponent<TransformComponent>().position = Vector2D(600, 500);
|
||||
|
||||
player1.getComponent<HealthComponent>().setHealth(5);
|
||||
player2.getComponent<HealthComponent>().setHealth(5);
|
||||
|
||||
isRunning = true;
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
@ -17,6 +17,12 @@ void HealthComponent::modifyHealth(int health)
|
||||
this->refreshHearts();
|
||||
}
|
||||
|
||||
void HealthComponent::setHealth(int health)
|
||||
{
|
||||
this->health = health;
|
||||
this->refreshHearts();
|
||||
}
|
||||
|
||||
void HealthComponent::refreshHearts()
|
||||
{
|
||||
// clear hearts if exist
|
||||
|
||||
85
src/PopupWindow.cpp
Normal file
85
src/PopupWindow.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
#include <iostream>
|
||||
#include <SDL_image.h>
|
||||
|
||||
#include "Entity.h"
|
||||
#include "PopupWindow.h"
|
||||
#include "TextureManager.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_DestroyRenderer(this->renderer);
|
||||
SDL_DestroyWindow(this->window);
|
||||
}
|
||||
|
||||
void PopupWindow::handleWinnerEvents() {
|
||||
|
||||
SDL_Event e;
|
||||
|
||||
while (SDL_PollEvent(&e))
|
||||
{
|
||||
if (e.type == SDL_QUIT)
|
||||
{
|
||||
continueGame = false;
|
||||
interacted = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if(e.type != SDL_KEYDOWN)
|
||||
continue;
|
||||
|
||||
switch (e.key.keysym.sym) {
|
||||
|
||||
case SDLK_q: {
|
||||
continueGame = false;
|
||||
interacted = true;
|
||||
break;
|
||||
}
|
||||
case SDLK_c: {
|
||||
continueGame = true;
|
||||
interacted = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool PopupWindow::shouldContinue() const {
|
||||
return continueGame;
|
||||
}
|
||||
|
||||
void PopupWindow::renderWinnerPopup(TeamLabel winner) {
|
||||
|
||||
SDL_RenderClear(this->renderer);
|
||||
|
||||
//Maybe use texture manager (changes need to be made that it does not use game::renderer automatically, but receives one instead)
|
||||
this->texture = winner == TeamLabel::BLUE ?
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
48
src/main.cpp
48
src/main.cpp
@ -1,12 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
|
||||
#include "Entity.h"
|
||||
#include "Game.h"
|
||||
#include "Constants.h"
|
||||
#include <ctime>
|
||||
#include "PopupWindow.h"
|
||||
|
||||
Game* game = nullptr;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
srand(time(NULL));
|
||||
bool playing = true;
|
||||
|
||||
const int frameDelay = 1000 / FPS;
|
||||
|
||||
Uint32 frameStart;
|
||||
@ -15,23 +21,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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
TeamLabel winner = game->getWinner();
|
||||
|
||||
game->clean();
|
||||
PopupWindow popupWindow("Game over", winner == TeamLabel::BLUE ?
|
||||
"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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user