TinyGPU
Loading...
Searching...
No Matches
DisplayDriverSDL.h
Go to the documentation of this file.
1#pragma once
2#include <SDL.h>
3
4#include <cstdint>
5#include <vector>
6
8
9namespace tinygpu {
10
11/**
12 * @brief Display class for rendering a Surface<RGB565> using the SDL2 library.
13 * This class can be used to display the contents of a TinyGPU Surface<RGB565>
14 * on a desktop environment for testing and debugging purposes.
15 */
16template <typename RGB_T = RGB565>
17class DisplayDriverSDL : public DisplayDriver<RGB_T> {
18 public:
19 DisplayDriverSDL(size_t width, size_t height) : w_(width), h_(height) {}
20 DisplayDriverSDL(const ISurface<RGB_T>& surface)
21 : w_(surface.width()), h_(surface.height()) {}
22
23 bool begin() override {
24 SDL_Init(SDL_INIT_VIDEO);
25 if (window_ == nullptr) {
26 window_ =
27 SDL_CreateWindow("RGB565 Display", SDL_WINDOWPOS_UNDEFINED,
28 SDL_WINDOWPOS_UNDEFINED, w_, h_, SDL_WINDOW_SHOWN);
29 if (window_ == nullptr) return false;
30 renderer_ = SDL_CreateRenderer(window_, -1, SDL_RENDERER_ACCELERATED);
31 if (renderer_ == nullptr) return false;
32 texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGB565,
33 SDL_TEXTUREACCESS_STREAMING, w_, h_);
34 if (texture_ == nullptr) return false;
35 }
36 return true;
37 }
38
39 public:
40 bool writeData(ISurface<RGB_T>& surface) override {
41 setAddressWindow(0, 0, surface.width(), surface.height());
42 if (surface.width() != w_ || surface.height() != h_) {
43 return false;
44 }
45 SDL_Rect dstRect = {static_cast<int>(x_), static_cast<int>(y_),
46 static_cast<int>(w_), static_cast<int>(h_)};
47 SDL_UpdateTexture(texture_, NULL, surface.data(),
48 surface.width() * sizeof(RGB_T));
49 SDL_RenderClear(renderer_);
50 SDL_RenderCopy(renderer_, texture_, NULL, &dstRect);
51 SDL_RenderPresent(renderer_);
52 return true;
53 }
54
55 void end() override {
56 SDL_DestroyTexture(texture_);
57 SDL_DestroyRenderer(renderer_);
58 SDL_DestroyWindow(window_);
59 SDL_Quit();
60 texture_ = nullptr;
61 renderer_ = nullptr;
62 window_ = nullptr;
63 }
64
65 protected:
70 SDL_Window* window_ = nullptr;
73
74 protected:
75 bool setAddressWindow(size_t x, size_t y, size_t w, size_t h) override {
76 x_ = x;
77 y_ = y;
78 w_ = w;
79 h_ = h;
80 return true;
81 }
82};
83
84} // namespace tinygpu
Display class for rendering a Surface<RGB565> using the SDL2 library. This class can be used to displ...
Definition: DisplayDriverSDL.h:17
size_t w_
Definition: DisplayDriverSDL.h:68
SDL_Texture * texture_
Definition: DisplayDriverSDL.h:72
size_t y_
Definition: DisplayDriverSDL.h:67
DisplayDriverSDL(size_t width, size_t height)
Definition: DisplayDriverSDL.h:19
void end() override
Definition: DisplayDriverSDL.h:55
SDL_Window * window_
Definition: DisplayDriverSDL.h:70
size_t x_
Definition: DisplayDriverSDL.h:66
DisplayDriverSDL(const ISurface< RGB_T > &surface)
Definition: DisplayDriverSDL.h:20
size_t h_
Definition: DisplayDriverSDL.h:69
SDL_Renderer * renderer_
Definition: DisplayDriverSDL.h:71
bool begin() override
Definition: DisplayDriverSDL.h:23
bool setAddressWindow(size_t x, size_t y, size_t w, size_t h) override
Definition: DisplayDriverSDL.h:75
bool writeData(ISurface< RGB_T > &surface) override
Definition: DisplayDriverSDL.h:40
Abstract base class for display drivers.
Definition: DisplayDriver.h:17
RGB color stored in 16-bit RGB565 format.
Definition: RGB565.h:13
Definition: AVIWriter.h:9