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