// // Created by bruno on 27.10.2023. // #include "Game.h" #include #include 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 if (SDL_Init(SDL_INIT_VIDEO) != 0) { 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); 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; } 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; } 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); } } for(int i = 0; i < playerCount; i++){ players[i].render(renderer); } 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; } 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; }