ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
sdl_esphome.cpp
Go to the documentation of this file.
1#ifdef USE_HOST
2#include "sdl_esphome.h"
4
5namespace esphome::sdl {
6
18
30
31void Sdl::setup() {
32 SDL_Init(SDL_INIT_VIDEO);
33 this->window_ = SDL_CreateWindow(App.get_name().c_str(), this->pos_x_, this->pos_y_, this->width_, this->height_,
34 this->window_options_);
35 this->renderer_ = SDL_CreateRenderer(this->window_, -1, SDL_RENDERER_SOFTWARE);
36 SDL_RenderSetLogicalSize(this->renderer_, this->width_, this->height_);
37 this->texture_ =
38 SDL_CreateTexture(this->renderer_, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STATIC, this->width_, this->height_);
39 SDL_SetTextureBlendMode(this->texture_, SDL_BLENDMODE_BLEND);
40}
42 this->do_update_();
44 return;
45 SDL_Rect rect{this->x_low_, this->y_low_, this->x_high_ + 1 - this->x_low_, this->y_high_ + 1 - this->y_low_};
46 this->x_low_ = this->width_;
47 this->y_low_ = this->height_;
48 this->x_high_ = 0;
49 this->y_high_ = 0;
50 this->redraw_(rect);
51}
52
53void Sdl::redraw_(SDL_Rect &rect) {
54 SDL_RenderCopy(this->renderer_, this->texture_, &rect, &rect);
55 SDL_RenderPresent(this->renderer_);
56}
57
58void Sdl::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
59 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
60 SDL_Rect rect{x_start, y_start, w, h};
61 if (this->rotation_ != display::DISPLAY_ROTATION_0_DEGREES || bitness != display::COLOR_BITNESS_565 || big_endian) {
62 Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
63 } else {
64 auto stride = x_offset + w + x_pad;
65 auto data = ptr + (stride * y_offset + x_offset) * 2;
66 SDL_UpdateTexture(this->texture_, &rect, data, stride * 2);
67 }
68 this->redraw_(rect);
69}
70
71void Sdl::draw_pixel_at(int x, int y, Color color) {
72 if (!this->get_clipping().inside(x, y))
73 return;
74
76 x = this->width_ - x - 1;
77 y = this->height_ - y - 1;
79 auto tmp = x;
80 x = this->width_ - y - 1;
81 y = tmp;
83 auto tmp = y;
84 y = this->height_ - x - 1;
85 x = tmp;
86 }
87
88 SDL_Rect rect{x, y, 1, 1};
90 SDL_UpdateTexture(this->texture_, &rect, &data, 2);
92 this->x_low_ = x;
94 this->y_low_ = y;
95 if (x > this->x_high_)
96 this->x_high_ = x;
97 if (y > this->y_high_)
98 this->y_high_ = y;
99}
100
101void Sdl::process_key(uint32_t keycode, bool down) {
102 auto callback = this->key_callbacks_.find(keycode);
103 if (callback != this->key_callbacks_.end())
104 callback->second(down);
105}
106
107void Sdl::loop() {
108 SDL_Event e;
109 if (SDL_PollEvent(&e)) {
110 switch (e.type) {
111 case SDL_QUIT:
112 exit(0);
113
114 case SDL_MOUSEBUTTONDOWN:
115 case SDL_MOUSEBUTTONUP:
116 if (e.button.button == 1) {
117 this->mouse_x = e.button.x;
118 this->mouse_y = e.button.y;
119 this->mouse_down = e.button.state != 0;
120 }
121 break;
122
123 case SDL_MOUSEMOTION:
124 if (e.motion.state & 1) {
125 this->mouse_x = e.button.x;
126 this->mouse_y = e.button.y;
127 this->mouse_down = true;
128 } else {
129 this->mouse_down = false;
130 }
131 break;
132
133 case SDL_KEYDOWN:
134 ESP_LOGD(TAG, "keydown %d", e.key.keysym.sym);
135 this->process_key(e.key.keysym.sym, true);
136 break;
137
138 case SDL_KEYUP:
139 ESP_LOGD(TAG, "keyup %d", e.key.keysym.sym);
140 this->process_key(e.key.keysym.sym, false);
141 break;
142
143 case SDL_WINDOWEVENT:
144 switch (e.window.event) {
145 case SDL_WINDOWEVENT_SIZE_CHANGED:
146 case SDL_WINDOWEVENT_EXPOSED:
147 case SDL_WINDOWEVENT_RESIZED: {
148 SDL_Rect rect{0, 0, this->width_, this->height_};
149 this->redraw_(rect);
150 break;
151 }
152 default:
153 break;
154 }
155 break;
156
157 default:
158 ESP_LOGV(TAG, "Event %d", e.type);
159 break;
160 }
161 }
162}
163
164} // namespace esphome::sdl
165#endif
uint8_t h
Definition bl0906.h:2
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
constexpr const char * c_str() const
Definition string_ref.h:73
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:766
DisplayRotation rotation_
Definition display.h:789
void setup() override
int get_width() override
int get_height() override
SDL_Renderer * renderer_
Definition sdl_esphome.h:59
void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
int get_height_internal() override
Definition sdl_esphome.h:52
void process_key(uint32_t keycode, bool down)
SDL_Texture * texture_
Definition sdl_esphome.h:61
void redraw_(SDL_Rect &rect)
void loop() override
void draw_pixel_at(int x, int y, Color color) override
int get_width_internal() override
Definition sdl_esphome.h:51
void update() override
SDL_Window * window_
Definition sdl_esphome.h:60
std::map< int32_t, CallbackManager< void(bool)> > key_callbacks_
Definition sdl_esphome.h:66
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:134
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:137
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:136
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:135
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6