From 2b9da98547313d60ab56a67a11999d5238960e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Tue, 19 Dec 2023 17:53:47 +0100 Subject: [PATCH] update --- CMakeLists.txt | 11 +++++++- Game.cpp | 73 +++++++++++++++++++++++++++++++++++++++++-------- Game.h | 41 +++++++++++++++++---------- Player.cpp | 51 ++++++++++++++++++++++++++++++++++ Player.h | 51 ++++++++++++++++++++++++++++++++++ Sprite.cpp | 48 ++++++++++++++++++++++++++++++++ Sprite.h | 28 +++++++++++++++++++ assets/a.png | Bin 0 -> 105 bytes 8 files changed, 277 insertions(+), 26 deletions(-) create mode 100644 Player.cpp create mode 100644 Player.h create mode 100644 Sprite.cpp create mode 100644 Sprite.h create mode 100644 assets/a.png diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f6de6e..aad6c56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,19 @@ project(tunellerbutworse) set(CMAKE_CXX_STANDARD 23) find_package(SDL2 REQUIRED) +find_package(SDL2_image REQUIRED) add_executable(tunellerbutworse main.cpp Game.cpp Game.h + Sprite.cpp + Sprite.h + Player.cpp + Player.h ) -target_link_libraries(tunellerbutworse PRIVATE SDL2::SDL2) \ No newline at end of file +target_link_libraries(tunellerbutworse PRIVATE SDL2::SDL2) + +target_link_libraries(tunellerbutworse PRIVATE SDL2_image::SDL2_image) + +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/assets/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/assets/) \ No newline at end of file diff --git a/Game.cpp b/Game.cpp index 25dbb56..6ac22cd 100644 --- a/Game.cpp +++ b/Game.cpp @@ -4,18 +4,28 @@ #include "Game.h" #include +#include -unsigned int Game::logicalWidth = 160; -unsigned int Game::logicalHeight = 100; -unsigned int Game::renderWidth = 1280; -unsigned int Game::renderHeight = 800; +int Game::logicalWidth = 160; +int Game::logicalHeight = 100; +int Game::renderWidth = 1280; +int Game::renderHeight = 800; bool Game::isRunning = true; SDL_Event Game::event; SDL_Window* Game::window; SDL_Renderer* Game::renderer; +SDL_Surface* Game::windowSurface; +SDL_Rect* Game::windowRect; +Game::Game(){ + windowRect = new SDL_Rect; +} + +Game::~Game(){ + delete windowRect; // Deallocate memory +} int Game::init(){ // Initialize SDL @@ -23,6 +33,8 @@ int Game::init(){ SDL_Log("Unable to initialize SDL: %s", SDL_GetError()); return 1; } + windowRect->w = logicalWidth; + windowRect->h = logicalHeight; // Create the SDL window and renderer window = SDL_CreateWindow("Pixel Art Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, renderWidth, renderHeight, SDL_WINDOW_RESIZABLE); @@ -37,8 +49,30 @@ int Game::init(){ return 1; } + windowSurface = SDL_GetWindowSurface( window ); + if (!windowSurface) { + SDL_Log("Unable to create window surface: %s", SDL_GetError()); + return 1; + } + + //Initialize PNG loading + int imgFlags = IMG_INIT_PNG; + if( !( IMG_Init( imgFlags ) & imgFlags ) ) + { + printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() ); + return 1; + } + // Set the logical scaling SDL_RenderSetLogicalSize(renderer, logicalWidth, logicalHeight); + + //create players + + for(int i = 0; i < playerCount; i++){ + Player temp_player = Player(0, 0, new Sprite("assets/a.png", renderer, windowRect), 20, windowRect); + players.push_back(temp_player); + } + return 0; } @@ -59,6 +93,9 @@ int Game::render(){ SDL_RenderDrawPoint(renderer, x, y); } } + for(int i = 0; i < playerCount; i++){ + players[i].render(renderer); + } return 0; } @@ -92,16 +129,30 @@ int Game::update(){ return 0; } -int Game::run(){ - this->init(); - - while (isRunning) { - this->update(); - } - +int Game::quit(){ // Cleanup and exit SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; +} + +int Game::run(){ + int initStatus = this->init(); + if(initStatus != 0){ + return initStatus; + } + + while (isRunning) { + int updateStatus = this->update(); + if(updateStatus != 0){ + return updateStatus; + } + } + + int quitStatus = this->quit(); + if(quitStatus != 0){ + return quitStatus; + } + return 0; } \ No newline at end of file diff --git a/Game.h b/Game.h index 05ba767..8873c11 100644 --- a/Game.h +++ b/Game.h @@ -7,34 +7,47 @@ #include +#include "Player.h" +#include "Sprite.h" class Game { public: - static SDL_Window* window; - static SDL_Renderer* renderer; - - // Logical resolution - static unsigned int logicalWidth; - static unsigned int logicalHeight; - - // Rendering resolution (initial window size) - static unsigned int renderWidth; - static unsigned int renderHeight; - - static bool isRunning; - - static SDL_Event event; int run(); + Game(); + ~Game(); + private: int init(); int processInput(); int render(); int update(); + int quit(); + std::vector players; + + int playerCount = 1; + + std::vector spritefiles; + + static SDL_Window* window; + static SDL_Renderer* renderer; + static SDL_Surface* windowSurface; + static SDL_Rect* windowRect; + // Logical resolution + static int logicalWidth; + static int logicalHeight; + + // Rendering resolution (initial window size) + static int renderWidth; + static int renderHeight; + + static bool isRunning; + + static SDL_Event event; }; diff --git a/Player.cpp b/Player.cpp new file mode 100644 index 0000000..561f94a --- /dev/null +++ b/Player.cpp @@ -0,0 +1,51 @@ +// +// Created by bruno on 28.10.2023. +// + +#include "Player.h" + +void Player::processInput(SDL_Event *event){ + +}; + +int Player::render(SDL_Renderer* renderer){ + this->playerSprite->moveTo(this->x, this->y); + return this->playerSprite->render(renderer); +} + +void Player::renderHUD(SDL_Renderer* renderer) { + // Determine the dimensions and positions for the HUD + SDL_Rect hudRect = { 0, screenRect.h - hudHeight, screenRect.w, hudHeight }; + + // Set the HUD background color + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderFillRect(renderer, &hudRect); + + // Set the colors for the health and energy bars + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // Red for health + SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); // Green for energy + + // Calculate the width of the health and energy bars based on player's values + int healthBarWidth = (health / maxHealth) * (160 / 2); // Half of the screen width + int energyBarWidth = (energy / maxEnergy) * (160 / 2); //TODO, replace with logicalWidth + + // Define the position and size of the health bar + SDL_Rect healthBarRect = { 10, screenRect.h - hudHeight + 10, healthBarWidth, hudHeight - 20 }; + + // Define the position and size of the energy bar + SDL_Rect energyBarRect = { screenRect.w / 2, screenRect.h - hudHeight + 10, energyBarWidth, hudHeight - 20 }; + + // Render the health bar + SDL_RenderFillRect(renderer, &healthBarRect); + + // Render the energy bar + SDL_RenderFillRect(renderer, &energyBarRect); +} + +Player::Player(int initialX, int initialY, Sprite *sprite, int hudHeight, SDL_Rect *screenRect){ + this->x = initialX; + this->y = initialY; + this->screenRect = *screenRect; + this->playerSprite = sprite; + this->hudHeight = hudHeight; +} \ No newline at end of file diff --git a/Player.h b/Player.h new file mode 100644 index 0000000..d1e3359 --- /dev/null +++ b/Player.h @@ -0,0 +1,51 @@ +// +// Created by bruno on 28.10.2023. +// + +#ifndef TUNELLERBUTWORSE_PLAYER_H +#define TUNELLERBUTWORSE_PLAYER_H + +#include +#include "Sprite.h" + +class Player { +public: + // Constructor + + Player(int initialX, int initialY, Sprite *sprite, int hudHeight, SDL_Rect *screenRect); + +// Input processing method + void processInput(SDL_Event* event); + + // Movement methods + void move(int deltaX, int deltaY); + void moveTo(int x, int y); + + // Shooting method + void shoot(); + + // Collision detection methods + bool checkCollisionWith(Sprite* otherSprite); + + bool checkCollisionWithEverything(const std::vector& entities); + + int render(SDL_Renderer* renderer); + + void renderHUD(SDL_Renderer *renderer); + +private: + // Member variables for the player's position, etc. + int x, y, direction, health, energy, maxHealth, maxEnergy, hudHeight; + + SDL_Rect screenRect; + // Add any other member variables as needed + + // Member variable for the player's sprite + Sprite *playerSprite; + // You can use the Sprite class to manage the player's graphical representation + + // Add any other private member functions as needed +}; + + +#endif //TUNELLERBUTWORSE_PLAYER_H diff --git a/Sprite.cpp b/Sprite.cpp new file mode 100644 index 0000000..e116667 --- /dev/null +++ b/Sprite.cpp @@ -0,0 +1,48 @@ +// +// Created by bruno on 28.10.2023. +// + +#include +#include "Sprite.h" + +Sprite::Sprite(std::string filename, SDL_Renderer* renderer, SDL_Rect* targetRect) { + this->targetRect = targetRect; + this->sprite = IMG_LoadTexture(renderer, filename.c_str()); + if (!sprite) { + printf("Unable to load image %s! SDL_image Error: %s\n", filename.c_str(), IMG_GetError()); + } + else { + SDL_QueryTexture(sprite, NULL, NULL, &targetRect->w, &targetRect->h); + } +} + +int Sprite::render(SDL_Renderer* renderer) { + if (renderer) { + SDL_RenderCopy(renderer, this->sprite, NULL, targetRect); + } + return 0; +} + +int Sprite::getX() const { + return targetRect->x; +} + +int Sprite::getY() const { + return targetRect->y; +} + +void Sprite::moveTo(int x, int y) { + targetRect->x = x; + targetRect->y = y; +} + +void Sprite::moveRelative(int x, int y) { + targetRect->x += x; + targetRect->y += y; +} + +Sprite::~Sprite() { + if (this->sprite) { + SDL_DestroyTexture(this->sprite); + } +} diff --git a/Sprite.h b/Sprite.h new file mode 100644 index 0000000..c146ac0 --- /dev/null +++ b/Sprite.h @@ -0,0 +1,28 @@ +// +// Created by bruno on 28.10.2023. +// + +#ifndef TUNELLERBUTWORSE_SPRITE_H +#define TUNELLERBUTWORSE_SPRITE_H + +#include +#include + +class Sprite { +public: + Sprite(std::string filename, SDL_Renderer* renderer, SDL_Rect* targetRect); + ~Sprite(); + + int render(SDL_Renderer* renderer); + + int getX() const; + int getY() const; + void moveTo(int x, int y); + void moveRelative(int x, int y); + +private: + SDL_Texture* sprite; + SDL_Rect* targetRect; +}; + +#endif //TUNELLERBUTWORSE_SPRITE_H diff --git a/assets/a.png b/assets/a.png new file mode 100644 index 0000000000000000000000000000000000000000..9a3a12d14becd127f01fffd5ec75c50b9a933c5f GIT binary patch literal 105 zcmeAS@N?(olHy`uVBq!ia0vp^EFjFm1|(O0oL2{=7>k44ofy`glX(f`D0{j%hH%VG zPDx1kalV0Nh6E7Q)f*-M_