commit 580526df63f70a8ef1d498163a2956419487fe9f Author: Bruno Rybársky Date: Sat Oct 28 12:42:23 2023 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..307dfa2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +cmake-build* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9f6de6e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.27) +project(tunellerbutworse) + +set(CMAKE_CXX_STANDARD 23) + +find_package(SDL2 REQUIRED) + +add_executable(tunellerbutworse main.cpp + Game.cpp + Game.h +) + +target_link_libraries(tunellerbutworse PRIVATE SDL2::SDL2) \ No newline at end of file diff --git a/Game.cpp b/Game.cpp new file mode 100644 index 0000000..25dbb56 --- /dev/null +++ b/Game.cpp @@ -0,0 +1,107 @@ +// +// Created by bruno on 27.10.2023. +// + +#include "Game.h" +#include + +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; +} \ No newline at end of file diff --git a/Game.h b/Game.h new file mode 100644 index 0000000..05ba767 --- /dev/null +++ b/Game.h @@ -0,0 +1,41 @@ +// +// Created by bruno on 27.10.2023. +// + +#ifndef TUNELLERBUTWORSE_GAME_H +#define TUNELLERBUTWORSE_GAME_H + + +#include + + + +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(); + +private: + int init(); + int processInput(); + int render(); + int update(); + +}; + + +#endif //TUNELLERBUTWORSE_GAME_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..6a010f1 --- /dev/null +++ b/main.cpp @@ -0,0 +1,6 @@ +#include "Game.h" + +int main() { + Game game; + return game.run(); +} \ No newline at end of file