Added dummy screen

This commit is contained in:
Robert 2020-11-05 15:21:20 +01:00
parent bc2876d1b5
commit d0de17afa1
8 changed files with 79 additions and 6 deletions

View file

@ -1,7 +1,10 @@
#pragma once
#include <vector>
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<Screen*> screens;
};

View file

@ -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;
};

View file

@ -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);
};

View file

@ -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

View file

@ -1,6 +1,7 @@
#include "RenderWindow.hpp"
#include <SDL.h>
#include <stdexcept>
#include <SDL.h>
#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<Screen*>::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);
}
}

View file

@ -3,7 +3,7 @@
#include <SDL.h>
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);
}

View file

@ -1,12 +1,15 @@
#include <iostream>
#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;

View file

@ -0,0 +1,22 @@
#include "screens/DummyScreen.hpp"
#include <SDL.h>
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);
}