diff --git a/include/RenderWindow.hpp b/include/RenderWindow.hpp new file mode 100644 index 0000000..4c6e326 --- /dev/null +++ b/include/RenderWindow.hpp @@ -0,0 +1,17 @@ +#pragma once + +struct SDL_Window; +struct SDL_Renderer; + +class RenderWindow +{ +public: + RenderWindow(int width, int height); + ~RenderWindow(); + + void Run(); + +private: + SDL_Window* window; + SDL_Renderer* renderer; +}; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fe8d040..8968ed6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable(fourier "main.cpp" -) + "RenderWindow.cpp" + ) target_include_directories(fourier PRIVATE ${CMAKE_SOURCE_DIR}/include diff --git a/src/RenderWindow.cpp b/src/RenderWindow.cpp new file mode 100644 index 0000000..eb01e06 --- /dev/null +++ b/src/RenderWindow.cpp @@ -0,0 +1,42 @@ +#include "RenderWindow.hpp" +#include +#include + +RenderWindow::RenderWindow(int width, int height) +{ + if (SDL_CreateWindowAndRenderer(width, height, SDL_WINDOW_SHOWN, &window, &renderer)) + { + throw std::runtime_error(SDL_GetErrorMsg((char*)malloc(512), 511)); + } +} + +RenderWindow::~RenderWindow() +{ + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); +} + +void RenderWindow::Run() +{ + SDL_Event event; + bool isOpen = true; + + while (isOpen) + { + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_WINDOWEVENT) + { + switch (event.window.event) + { + case SDL_WINDOWEVENT_CLOSE: isOpen = false; break; + } + } + } + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + SDL_RenderPresent(renderer); + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 185e8ed..ab30eaa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,40 +1,20 @@ #include -#include +#include "RenderWindow.hpp" int main(int argc, char** argv) { - SDL_Window* window; - SDL_Renderer* renderer; - - if (SDL_CreateWindowAndRenderer(800, 800, SDL_WINDOW_SHOWN, &window, &renderer)) + RenderWindow* window; + try { - std::cerr << "Failed to initialize Window and Renderer" << std::endl; + window = new RenderWindow(800, 800); + } + catch (const std::runtime_error& e) + { + std::cerr << e.what() << std::endl; return -1; } - SDL_Event event; - bool isOpen = true; - while (isOpen) - { - while (SDL_PollEvent(&event)) - { - if (event.type == SDL_WINDOWEVENT) - { - switch (event.window.event) - { - case SDL_WINDOWEVENT_CLOSE: isOpen = false; break; - } - } - } - - SDL_SetRenderDrawColor(renderer, 20, 40, 210, 255); - SDL_RenderClear(renderer); - - SDL_RenderPresent(renderer); - } - - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); + window->Run(); return 0; } \ No newline at end of file