From 4d9db53de9be37a75ac34e0834724b76a8c31b7e Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Fri, 26 Jul 2019 07:10:07 +0100 Subject: [PATCH] Display interface and transfered control to render modules --- src/core/renderer.cpp | 20 ++++++++++++++++++-- src/core/renderer.hpp | 6 ++++-- src/display/display.cpp | 13 +++++++++---- src/display/display.hpp | 26 ++++++++++---------------- src/display/displayinterface.cpp | 5 +++++ src/display/displayinterface.hpp | 29 +++++++++++++++++++++++++++++ src/inferno.cpp | 13 ------------- 7 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 src/display/displayinterface.cpp create mode 100644 src/display/displayinterface.hpp diff --git a/src/core/renderer.cpp b/src/core/renderer.cpp index d9d8d25..2b8b94c 100644 --- a/src/core/renderer.cpp +++ b/src/core/renderer.cpp @@ -1,4 +1,5 @@ #include "../display/display.hpp" +#include "../pixel.hpp" #include "./renderer.hpp" Renderer::Renderer(OperationMode mode) { @@ -35,14 +36,29 @@ void Renderer::Init() { } -void Renderer::Init(Display* display) { +void Renderer::Init(DisplayInterface* display) { // Add warning if (!display) return; - m_framebuffer = display->Framebuffer; + if(!m_framebuffer) { + m_framebuffer = display->Framebuffer; + } + m_interface = display; } void Renderer::RenderProgressive() { + while (m_interface->Active) { + SDL_Event e; + while (SDL_PollEvent(&e) == SDL_TRUE) + if (e.type == SDL_QUIT) m_interface->Close(); + + for (int i = 0; i < 360000; i++) { + m_interface->SetPixelSafe(rand() % m_interface->XRes, + rand() % m_interface->YRes, + rgb888(rand() % 255, rand() % 255, rand() % 255)); + } + m_interface->Update(); + } } void Renderer::RenderSamples() { diff --git a/src/core/renderer.hpp b/src/core/renderer.hpp index 853ec25..e32836d 100644 --- a/src/core/renderer.hpp +++ b/src/core/renderer.hpp @@ -3,7 +3,7 @@ #include "../common.hpp" -class Display; +class DisplayInterface; // Function initProgressive or whatever takes a pointer to the display class Renderer { @@ -13,7 +13,8 @@ public: void SetMode(OperationMode mode, int samples = -1); void Init(); - void Init(Display* display); + + void Init(DisplayInterface* display); void Render(); void Render(uint32_t* framebuffer); @@ -22,6 +23,7 @@ private: int m_samples = -1; OperationMode m_mode = MODE_DEFAULT; + DisplayInterface* m_interface = nullptr; uint32_t* m_framebuffer = nullptr; void RenderProgressive(); diff --git a/src/display/display.cpp b/src/display/display.cpp index b3baa6d..fa67c73 100644 --- a/src/display/display.cpp +++ b/src/display/display.cpp @@ -4,10 +4,15 @@ #include "../pixel.hpp" -Display::Display() { +Display::Display() + : DisplayInterface() { } +bool Display::Init() { + return true; +} + bool Display::InitVideoDisplay(std::string title, int x, int y) { this->XRes = x; this->YRes = y; @@ -23,7 +28,7 @@ bool Display::InitVideoDisplay(std::string title, int x, int y) { return false; } - this->WindowOpen = true; + this->Active = true; m_renderer = SDL_CreateRenderer( m_window, -1, SDL_RENDERER_ACCELERATED @@ -83,13 +88,13 @@ void Display::SetFramebuffer(uint32_t* fb) { Framebuffer = fb; } -void Display::Refresh() { +void Display::Update() { SDL_UpdateTexture(m_texture, NULL, Framebuffer, this->XRes * sizeof(uint32_t)); SDL_RenderCopy(m_renderer, m_texture, NULL, NULL); SDL_RenderPresent(m_renderer); } -void Display::CloseDisplay() { +void Display::Close() { free(Framebuffer); SDL_DestroyTexture(m_texture); SDL_DestroyRenderer(m_renderer); diff --git a/src/display/display.hpp b/src/display/display.hpp index a8d8fd3..a511fec 100644 --- a/src/display/display.hpp +++ b/src/display/display.hpp @@ -5,32 +5,27 @@ #include +#include "./displayinterface.hpp" + class Pixel; -class Display { +class Display : public DisplayInterface { public: Display(); + bool Init() override; bool InitVideoDisplay(std::string title, int x, int y); - bool WindowOpen = false; - int XRes, YRes; - std::string Title; - unsigned int Scale = 1; + void SetPixel(int x, int y, Pixel p) override; + void SetPixel(int x, int y, uint32_t p) override; + void SetPixelSafe(int x, int y, Pixel p) override; + void SetPixelSafe(int x, int y, uint32_t p) override; - void SetPixel(int x, int y, Pixel p); - void SetPixel(int x, int y, uint32_t p); - void SetPixelSafe(int x, int y, Pixel p); - void SetPixelSafe(int x, int y, uint32_t p); - - uint32_t* Framebuffer; void SetFramebuffer(uint32_t* fb); - void Refresh(); + void Update() override; - // Error and warn - - void CloseDisplay(); + void Close() override; virtual ~Display(); private: @@ -38,7 +33,6 @@ private: SDL_Window* m_window; SDL_Renderer* m_renderer; SDL_Texture* m_texture; - }; #endif diff --git a/src/display/displayinterface.cpp b/src/display/displayinterface.cpp new file mode 100644 index 0000000..5013081 --- /dev/null +++ b/src/display/displayinterface.cpp @@ -0,0 +1,5 @@ +#include "./displayinterface.hpp" + +DisplayInterface::DisplayInterface() { + +} diff --git a/src/display/displayinterface.hpp b/src/display/displayinterface.hpp new file mode 100644 index 0000000..d7589dd --- /dev/null +++ b/src/display/displayinterface.hpp @@ -0,0 +1,29 @@ +#ifndef INFERNO_DISPLAY_DISPLAYINTERFACE_H_ +#define INFERNO_DISPLAY_DISPLAYINTERFACE_H_ + +#include "../common.hpp" + +class Pixel; + +class DisplayInterface { +public: + DisplayInterface(); + + bool Active = false; + int XRes, YRes; + std::string Title; + unsigned int Scale = 1; + + uint32_t* Framebuffer; + + virtual void SetPixel(int x, int y, Pixel p) = 0; + virtual void SetPixel(int x, int y, uint32_t p) = 0; + virtual void SetPixelSafe(int x, int y, Pixel p) = 0; + virtual void SetPixelSafe(int x, int y, uint32_t p) = 0; + + virtual bool Init() = 0; + virtual void Update() = 0; + virtual void Close() = 0; +}; + +#endif diff --git a/src/inferno.cpp b/src/inferno.cpp index 8bebefd..55b3f18 100644 --- a/src/inferno.cpp +++ b/src/inferno.cpp @@ -51,19 +51,6 @@ void InfernoEngine::Ready() { void InfernoEngine::Render() { m_renderer->Render(m_display->Framebuffer); - while (m_display->WindowOpen) { - SDL_Event e; - while (SDL_PollEvent(&e) == SDL_TRUE) - if (e.type == SDL_QUIT) m_display->CloseDisplay(); - - for (int i = 0; i < 360000; i++) { - m_display->SetPixelSafe(rand() % m_display->XRes, - rand() % m_display->YRes, - rgb888(rand() % 255, rand() % 255, rand() % 255)); - } - - m_display->Refresh(); - } } std::string InfernoEngine::LastError() {