ESPHome  2024.11.1
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::loop() {
65  SDL_Event e;
66  if (SDL_PollEvent(&e)) {
67  switch (e.type) {
68  case SDL_QUIT:
69  exit(0);
70 
71  case SDL_MOUSEBUTTONDOWN:
72  case SDL_MOUSEBUTTONUP:
73  if (e.button.button == 1) {
74  this->mouse_x = e.button.x;
75  this->mouse_y = e.button.y;
76  this->mouse_down = e.button.state != 0;
77  }
78  break;
79 
80  case SDL_MOUSEMOTION:
81  if (e.motion.state & 1) {
82  this->mouse_x = e.button.x;
83  this->mouse_y = e.button.y;
84  this->mouse_down = true;
85  } else {
86  this->mouse_down = false;
87  }
88  break;
89 
90  case SDL_WINDOWEVENT:
91  switch (e.window.event) {
92  case SDL_WINDOWEVENT_SIZE_CHANGED:
93  case SDL_WINDOWEVENT_EXPOSED:
94  case SDL_WINDOWEVENT_RESIZED: {
95  SDL_Rect rect{0, 0, this->width_, this->height_};
96  this->redraw_(rect);
97  break;
98  }
99  default:
100  break;
101  }
102  break;
103 
104  default:
105  ESP_LOGV(TAG, "Event %d", e.type);
106  break;
107  }
108  }
109 }
110 
111 } // namespace sdl
112 } // namespace esphome
113 #endif
void setup() override
Definition: sdl_esphome.cpp:8
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
uint16_t x_high_
Definition: sdl_esphome.h:49
uint16_t x
Definition: tt21100.cpp:17
SDL_Window * window_
Definition: sdl_esphome.h:45
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:44
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:666
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:46
uint16_t y_high_
Definition: sdl_esphome.h:50
void loop() override
Definition: sdl_esphome.cpp:64