This commit is contained in:
Bruno Rybársky 2023-12-19 17:53:47 +01:00
parent 580526df63
commit 2b9da98547
8 changed files with 277 additions and 26 deletions

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

@ -4,18 +4,28 @@
#include "Game.h"
#include <SDL.h>
#include <SDL_image.h>
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;
}

41
Game.h

@ -7,34 +7,47 @@
#include <SDL.h>
#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<Player> players;
int playerCount = 1;
std::vector<std::string> 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;
};

51
Player.cpp Normal file

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

51
Player.h Normal file

@ -0,0 +1,51 @@
//
// Created by bruno on 28.10.2023.
//
#ifndef TUNELLERBUTWORSE_PLAYER_H
#define TUNELLERBUTWORSE_PLAYER_H
#include <vector>
#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<Sprite>& 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

48
Sprite.cpp Normal file

@ -0,0 +1,48 @@
//
// Created by bruno on 28.10.2023.
//
#include <SDL_image.h>
#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);
}
}

28
Sprite.h Normal file

@ -0,0 +1,28 @@
//
// Created by bruno on 28.10.2023.
//
#ifndef TUNELLERBUTWORSE_SPRITE_H
#define TUNELLERBUTWORSE_SPRITE_H
#include <SDL.h>
#include <string>
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

BIN
assets/a.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 B