Tunneler/Game.cpp

158 lines
3.8 KiB
C++
Raw Normal View History

2023-10-28 12:42:23 +02:00
//
// Created by bruno on 27.10.2023.
//
#include "Game.h"
#include <SDL.h>
2023-12-19 17:53:47 +01:00
#include <SDL_image.h>
2023-10-28 12:42:23 +02:00
2023-12-19 17:53:47 +01:00
int Game::logicalWidth = 160;
int Game::logicalHeight = 100;
int Game::renderWidth = 1280;
int Game::renderHeight = 800;
2023-10-28 12:42:23 +02:00
bool Game::isRunning = true;
SDL_Event Game::event;
SDL_Window* Game::window;
SDL_Renderer* Game::renderer;
2023-12-19 17:53:47 +01:00
SDL_Surface* Game::windowSurface;
SDL_Rect* Game::windowRect;
2023-10-28 12:42:23 +02:00
2023-12-19 17:53:47 +01:00
Game::Game(){
windowRect = new SDL_Rect;
}
Game::~Game(){
delete windowRect; // Deallocate memory
}
2023-10-28 12:42:23 +02:00
int Game::init(){
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
2023-12-19 17:53:47 +01:00
windowRect->w = logicalWidth;
windowRect->h = logicalHeight;
2023-10-28 12:42:23 +02:00
// Create the SDL window and renderer
window = SDL_CreateWindow("Pixel Art Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, renderWidth, renderHeight, SDL_WINDOW_RESIZABLE);
if (!window) {
SDL_Log("Unable to create window: %s", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
SDL_Log("Unable to create renderer: %s", SDL_GetError());
return 1;
}
2023-12-19 17:53:47 +01:00
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;
}
2023-10-28 12:42:23 +02:00
// Set the logical scaling
SDL_RenderSetLogicalSize(renderer, logicalWidth, logicalHeight);
2023-12-19 17:53:47 +01:00
//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);
}
2023-10-28 12:42:23 +02:00
return 0;
}
int Game::processInput(){
event;
return 0;
}
int Game::render(){
// Render the checkerboard pattern
for (int x = 0; x < logicalWidth; x++) {
for (int y = 0; y < logicalHeight; y++) {
if ((x + y) % 2 == 0) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Black
} else {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // White
}
SDL_RenderDrawPoint(renderer, x, y);
}
}
2023-12-19 17:53:47 +01:00
for(int i = 0; i < playerCount; i++){
players[i].render(renderer);
}
2023-10-28 12:42:23 +02:00
return 0;
}
int Game::update(){
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
isRunning = false;
}
else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
renderWidth = event.window.data1;
renderHeight = event.window.data2;
SDL_RenderSetLogicalSize(renderer, logicalWidth, logicalHeight);
}
else if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP){
processInput();
}
}
// Clear the renderer
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderClear(renderer);
// Your game rendering code goes here, using the logical resolution
this->render();
// Present the renderer
SDL_RenderPresent(renderer);
return 0;
}
2023-12-19 17:53:47 +01:00
int Game::quit(){
// Cleanup and exit
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
2023-10-28 12:42:23 +02:00
int Game::run(){
2023-12-19 17:53:47 +01:00
int initStatus = this->init();
if(initStatus != 0){
return initStatus;
}
2023-10-28 12:42:23 +02:00
while (isRunning) {
2023-12-19 17:53:47 +01:00
int updateStatus = this->update();
if(updateStatus != 0){
return updateStatus;
}
2023-10-28 12:42:23 +02:00
}
2023-12-19 17:53:47 +01:00
int quitStatus = this->quit();
if(quitStatus != 0){
return quitStatus;
}
2023-10-28 12:42:23 +02:00
return 0;
}