From d0de17afa13c8d7a678d3721cd1e5672f9d27d54 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 5 Nov 2020 15:21:20 +0100 Subject: [PATCH] Added dummy screen --- include/RenderWindow.hpp | 11 ++++++++++- include/Screen.hpp | 4 ++-- include/screens/DummyScreen.hpp | 12 ++++++++++++ src/CMakeLists.txt | 2 +- src/RenderWindow.cpp | 25 ++++++++++++++++++++++++- src/Screen.cpp | 5 ++++- src/main.cpp | 4 ++++ src/screens/DummyScreen.cpp | 22 ++++++++++++++++++++++ 8 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 include/screens/DummyScreen.hpp create mode 100644 src/screens/DummyScreen.cpp diff --git a/include/RenderWindow.hpp b/include/RenderWindow.hpp index 4c6e326..9a61aa2 100644 --- a/include/RenderWindow.hpp +++ b/include/RenderWindow.hpp @@ -1,7 +1,10 @@ #pragma once +#include + struct SDL_Window; struct SDL_Renderer; +class Screen; class RenderWindow { @@ -9,9 +12,15 @@ public: RenderWindow(int width, int height); ~RenderWindow(); + void AddScreen(Screen* screen); + void RemoveScreen(Screen* screen); + void Run(); -private: +public: SDL_Window* window; SDL_Renderer* renderer; + +private: + std::vector screens; }; \ No newline at end of file diff --git a/include/Screen.hpp b/include/Screen.hpp index ca06e11..1827505 100644 --- a/include/Screen.hpp +++ b/include/Screen.hpp @@ -2,7 +2,7 @@ struct SDL_Renderer; struct SDL_Texture; -struct SDL_Rect { int x, y, w, h }; +struct SDL_Rect; class Screen { @@ -14,6 +14,6 @@ public: virtual void Render(SDL_Renderer* renderer) = 0; protected: - SDL_Rect screenSpace; + SDL_Rect* screenSpace; SDL_Texture* texture; }; \ No newline at end of file diff --git a/include/screens/DummyScreen.hpp b/include/screens/DummyScreen.hpp new file mode 100644 index 0000000..f52717b --- /dev/null +++ b/include/screens/DummyScreen.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "../Screen.hpp" + +class DummyScreen : public Screen +{ +public: + DummyScreen(SDL_Renderer* renderer, int x, int y, int w, int h); + + void Update(); + void Render(SDL_Renderer* renderer); +}; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c904840..8d07ce6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,7 @@ add_executable(fourier "main.cpp" "RenderWindow.cpp" - "Screen.cpp") + "Screen.cpp" "screens/DummyScreen.cpp") target_include_directories(fourier PRIVATE ${CMAKE_SOURCE_DIR}/include diff --git a/src/RenderWindow.cpp b/src/RenderWindow.cpp index eb01e06..7a0ec04 100644 --- a/src/RenderWindow.cpp +++ b/src/RenderWindow.cpp @@ -1,6 +1,7 @@ #include "RenderWindow.hpp" -#include #include +#include +#include "Screen.hpp" RenderWindow::RenderWindow(int width, int height) { @@ -16,6 +17,22 @@ RenderWindow::~RenderWindow() SDL_DestroyWindow(window); } +void RenderWindow::AddScreen(Screen* screen) +{ + screens.push_back(screen); +} + +void RenderWindow::RemoveScreen(Screen* screen) +{ + for (std::vector::iterator it = screens.begin(); it != screens.end();) + { + if (*it == screen) + it = screens.erase(it); + else + it++; + } +} + void RenderWindow::Run() { SDL_Event event; @@ -34,9 +51,15 @@ void RenderWindow::Run() } } + for (Screen* screen : screens) + screen->Update(); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); + for (Screen* screen : screens) + screen->Render(renderer); + SDL_RenderPresent(renderer); } } \ No newline at end of file diff --git a/src/Screen.cpp b/src/Screen.cpp index 06bbe0b..b954000 100644 --- a/src/Screen.cpp +++ b/src/Screen.cpp @@ -3,7 +3,7 @@ #include Screen::Screen(SDL_Renderer* renderer, int x, int y, int w, int h) : - screenSpace{ x, y, w, h } + screenSpace(new SDL_Rect{ x, y, w, h }) { texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, w, h); if (texture == nullptr) @@ -14,5 +14,8 @@ Screen::Screen(SDL_Renderer* renderer, int x, int y, int w, int h) : Screen::~Screen() { + delete screenSpace; + screenSpace = nullptr; + SDL_DestroyTexture(texture); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ab30eaa..8fc2bef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,15 @@ #include #include "RenderWindow.hpp" +#include "screens/DummyScreen.hpp" int main(int argc, char** argv) { RenderWindow* window; + DummyScreen* dScreen; try { window = new RenderWindow(800, 800); + dScreen = new DummyScreen(window->renderer, 100, 100, 600, 600); } catch (const std::runtime_error& e) { @@ -14,6 +17,7 @@ int main(int argc, char** argv) return -1; } + window->AddScreen(dScreen); window->Run(); return 0; diff --git a/src/screens/DummyScreen.cpp b/src/screens/DummyScreen.cpp new file mode 100644 index 0000000..bef682d --- /dev/null +++ b/src/screens/DummyScreen.cpp @@ -0,0 +1,22 @@ +#include "screens/DummyScreen.hpp" +#include + +DummyScreen::DummyScreen(SDL_Renderer* renderer, int x, int y, int w, int h) : + Screen(renderer, x, y, w, h) +{ +} + +void DummyScreen::Update() +{ + // Do nothing +} + +void DummyScreen::Render(SDL_Renderer* renderer) +{ + SDL_SetRenderTarget(renderer, texture); + SDL_SetRenderDrawColor(renderer, 20, 40, 210, 255); + SDL_RenderClear(renderer); + + SDL_SetRenderTarget(renderer, NULL); + SDL_RenderCopy(renderer, texture, NULL, screenSpace); +} \ No newline at end of file