107 lines
2.6 KiB
C++
107 lines
2.6 KiB
C++
|
//
|
||
|
// Created by bruno on 27.10.2023.
|
||
|
//
|
||
|
|
||
|
#include "Game.h"
|
||
|
#include <SDL.h>
|
||
|
|
||
|
unsigned int Game::logicalWidth = 160;
|
||
|
unsigned int Game::logicalHeight = 100;
|
||
|
unsigned int Game::renderWidth = 1280;
|
||
|
unsigned int Game::renderHeight = 800;
|
||
|
|
||
|
bool Game::isRunning = true;
|
||
|
|
||
|
SDL_Event Game::event;
|
||
|
SDL_Window* Game::window;
|
||
|
SDL_Renderer* Game::renderer;
|
||
|
|
||
|
|
||
|
int Game::init(){
|
||
|
// Initialize SDL
|
||
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||
|
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
// 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;
|
||
|
}
|
||
|
|
||
|
// Set the logical scaling
|
||
|
SDL_RenderSetLogicalSize(renderer, logicalWidth, logicalHeight);
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
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::run(){
|
||
|
this->init();
|
||
|
|
||
|
while (isRunning) {
|
||
|
this->update();
|
||
|
}
|
||
|
|
||
|
// Cleanup and exit
|
||
|
SDL_DestroyRenderer(renderer);
|
||
|
SDL_DestroyWindow(window);
|
||
|
SDL_Quit();
|
||
|
return 0;
|
||
|
}
|