ESPHome 2026.3.2
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
19
31
32void Sdl::setup() {
33 SDL_Init(SDL_INIT_VIDEO);
34 this->window_ = SDL_CreateWindow(App.get_name().c_str(), this->pos_x_, this->pos_y_, this->width_, this->height_,
35 this->window_options_);
36 this->renderer_ = SDL_CreateRenderer(this->window_, -1, SDL_RENDERER_SOFTWARE);
37 SDL_RenderSetLogicalSize(this->renderer_, this->width_, this->height_);
38 this->texture_ =
39 SDL_CreateTexture(this->renderer_, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STATIC, this->width_, this->height_);
40 SDL_SetTextureBlendMode(this->texture_, SDL_BLENDMODE_BLEND);
41}
43 this->do_update_();
45 return;
46 SDL_Rect rect{this->x_low_, this->y_low_, this->x_high_ + 1 - this->x_low_, this->y_high_ + 1 - this->y_low_};
47 this->x_low_ = this->width_;
48 this->y_low_ = this->height_;
49 this->x_high_ = 0;
50 this->y_high_ = 0;
51 this->redraw_(rect);
52}
53
54void Sdl::redraw_(SDL_Rect &rect) {
55 SDL_RenderCopy(this->renderer_, this->texture_, &rect, &rect);
56 SDL_RenderPresent(this->renderer_);
57}
58
59void Sdl::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
60 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
61 SDL_Rect rect{x_start, y_start, w, h};
62 if (this->rotation_ != display::DISPLAY_ROTATION_0_DEGREES || bitness != display::COLOR_BITNESS_565 || big_endian) {
63 Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
64 } else {
65 auto stride = x_offset + w + x_pad;
66 auto data = ptr + (stride * y_offset + x_offset) * 2;
67 SDL_UpdateTexture(this->texture_, &rect, data, stride * 2);
68 }
69 this->redraw_(rect);
70}
71
72void Sdl::draw_pixel_at(int x, int y, Color color) {
73 if (!this->get_clipping().inside(x, y))
74 return;
75
77 x = this->width_ - x - 1;
78 y = this->height_ - y - 1;
80 auto tmp = x;
81 x = this->width_ - y - 1;
82 y = tmp;
84 auto tmp = y;
85 y = this->height_ - x - 1;
86 x = tmp;
87 }
88
89 SDL_Rect rect{x, y, 1, 1};
91 SDL_UpdateTexture(this->texture_, &rect, &data, 2);
93 this->x_low_ = x;
95 this->y_low_ = y;
96 if (x > this->x_high_)
97 this->x_high_ = x;
98 if (y > this->y_high_)
99 this->y_high_ = y;
100}
101
102void Sdl::process_key(uint32_t keycode, bool down) {
103 auto callback = this->key_callbacks_.find(keycode);
104 if (callback != this->key_callbacks_.end())
105 callback->second(down);
106}
107
108void Sdl::loop() {
109 SDL_Event e;
110 if (SDL_PollEvent(&e)) {
111 switch (e.type) {
112 case SDL_QUIT:
113 exit(0);
114
115 case SDL_MOUSEBUTTONDOWN:
116 case SDL_MOUSEBUTTONUP:
117 if (e.button.button == 1) {
118 this->mouse_x = e.button.x;
119 this->mouse_y = e.button.y;
120 this->mouse_down = e.button.state != 0;
121 }
122 break;
123
124 case SDL_MOUSEMOTION:
125 if (e.motion.state & 1) {
126 this->mouse_x = e.button.x;
127 this->mouse_y = e.button.y;
128 this->mouse_down = true;
129 } else {
130 this->mouse_down = false;
131 }
132 break;
133
134 case SDL_KEYDOWN:
135 ESP_LOGD(TAG, "keydown %d", e.key.keysym.sym);
136 this->process_key(e.key.keysym.sym, true);
137 break;
138
139 case SDL_KEYUP:
140 ESP_LOGD(TAG, "keyup %d", e.key.keysym.sym);
141 this->process_key(e.key.keysym.sym, false);
142 break;
143
144 case SDL_WINDOWEVENT:
145 switch (e.window.event) {
146 case SDL_WINDOWEVENT_SIZE_CHANGED:
147 case SDL_WINDOWEVENT_EXPOSED:
148 case SDL_WINDOWEVENT_RESIZED: {
149 SDL_Rect rect{0, 0, this->width_, this->height_};
150 this->redraw_(rect);
151 break;
152 }
153 default:
154 break;
155 }
156 break;
157
158 default:
159 ESP_LOGV(TAG, "Event %d", e.type);
160 break;
161 }
162 }
163}
164
165} // namespace sdl
166} // namespace esphome
167#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:790
void setup() override
int get_width() override
int get_height() 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
int get_height_internal() override
Definition sdl_esphome.h:53
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
int get_width_internal() override
Definition sdl_esphome.h:52
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
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:138
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:137
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:136
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.
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6