ESPHome 2025.9.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 {
6namespace sdl {
7
8void Sdl::setup() {
9 SDL_Init(SDL_INIT_VIDEO);
10 this->window_ = SDL_CreateWindow(App.get_name().c_str(), this->pos_x_, this->pos_y_, this->width_, this->height_,
11 this->window_options_);
12 this->renderer_ = SDL_CreateRenderer(this->window_, -1, SDL_RENDERER_SOFTWARE);
13 SDL_RenderSetLogicalSize(this->renderer_, this->width_, this->height_);
14 this->texture_ =
15 SDL_CreateTexture(this->renderer_, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STATIC, this->width_, this->height_);
16 SDL_SetTextureBlendMode(this->texture_, SDL_BLENDMODE_BLEND);
17}
19 this->do_update_();
21 return;
22 SDL_Rect rect{this->x_low_, this->y_low_, this->x_high_ + 1 - this->x_low_, this->y_high_ + 1 - this->y_low_};
23 this->x_low_ = this->width_;
24 this->y_low_ = this->height_;
25 this->x_high_ = 0;
26 this->y_high_ = 0;
27 this->redraw_(rect);
28}
29
30void Sdl::redraw_(SDL_Rect &rect) {
31 SDL_RenderCopy(this->renderer_, this->texture_, &rect, &rect);
32 SDL_RenderPresent(this->renderer_);
33}
34
35void Sdl::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
36 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
37 SDL_Rect rect{x_start, y_start, w, h};
38 if (this->rotation_ != display::DISPLAY_ROTATION_0_DEGREES || bitness != display::COLOR_BITNESS_565 || big_endian) {
39 Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
40 } else {
41 auto stride = x_offset + w + x_pad;
42 auto data = ptr + (stride * y_offset + x_offset) * 2;
43 SDL_UpdateTexture(this->texture_, &rect, data, stride * 2);
44 }
45 this->redraw_(rect);
46}
47
48void Sdl::draw_pixel_at(int x, int y, Color color) {
49 if (!this->get_clipping().inside(x, y))
50 return;
51
52 SDL_Rect rect{x, y, 1, 1};
54 SDL_UpdateTexture(this->texture_, &rect, &data, 2);
56 this->x_low_ = x;
58 this->y_low_ = y;
59 if (x > this->x_high_)
60 this->x_high_ = x;
61 if (y > this->y_high_)
62 this->y_high_ = y;
63}
64
65void Sdl::process_key(uint32_t keycode, bool down) {
66 auto callback = this->key_callbacks_.find(keycode);
67 if (callback != this->key_callbacks_.end())
68 callback->second(down);
69}
70
71void Sdl::loop() {
72 SDL_Event e;
73 if (SDL_PollEvent(&e)) {
74 switch (e.type) {
75 case SDL_QUIT:
76 exit(0);
77
78 case SDL_MOUSEBUTTONDOWN:
79 case SDL_MOUSEBUTTONUP:
80 if (e.button.button == 1) {
81 this->mouse_x = e.button.x;
82 this->mouse_y = e.button.y;
83 this->mouse_down = e.button.state != 0;
84 }
85 break;
86
87 case SDL_MOUSEMOTION:
88 if (e.motion.state & 1) {
89 this->mouse_x = e.button.x;
90 this->mouse_y = e.button.y;
91 this->mouse_down = true;
92 } else {
93 this->mouse_down = false;
94 }
95 break;
96
97 case SDL_KEYDOWN:
98 ESP_LOGD(TAG, "keydown %d", e.key.keysym.sym);
99 this->process_key(e.key.keysym.sym, true);
100 break;
101
102 case SDL_KEYUP:
103 ESP_LOGD(TAG, "keyup %d", e.key.keysym.sym);
104 this->process_key(e.key.keysym.sym, false);
105 break;
106
107 case SDL_WINDOWEVENT:
108 switch (e.window.event) {
109 case SDL_WINDOWEVENT_SIZE_CHANGED:
110 case SDL_WINDOWEVENT_EXPOSED:
111 case SDL_WINDOWEVENT_RESIZED: {
112 SDL_Rect rect{0, 0, this->width_, this->height_};
113 this->redraw_(rect);
114 break;
115 }
116 default:
117 break;
118 }
119 break;
120
121 default:
122 ESP_LOGV(TAG, "Event %d", e.type);
123 break;
124 }
125 }
126}
127
128} // namespace sdl
129} // namespace esphome
130#endif
uint8_t h
Definition bl0906.h:2
const std::string & get_name() const
Get the name of this Application set by pre_setup().
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:715
DisplayRotation rotation_
Definition display.h:680
void setup() override
SDL_Renderer * renderer_
Definition sdl_esphome.h:60
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
void process_key(uint32_t keycode, bool down)
SDL_Texture * texture_
Definition sdl_esphome.h:62
void redraw_(SDL_Rect &rect)
void loop() override
void draw_pixel_at(int x, int y, Color color) override
void update() override
SDL_Window * window_
Definition sdl_esphome.h:61
std::map< int32_t, CallbackManager< void(bool)> > key_callbacks_
Definition sdl_esphome.h:67
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:135
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Application App
Global storage of Application pointer - only one Application can exist.
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6