ESPHome  2024.9.2
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_, 0);
13  this->renderer_ = SDL_CreateRenderer(this->window_, -1, SDL_RENDERER_SOFTWARE);
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  ESP_LOGD(TAG, "Setup Complete");
18 }
19 void Sdl::update() {
20  this->do_update_();
21  if ((this->x_high_ < this->x_low_) || (this->y_high_ < this->y_low_))
22  return;
23  SDL_Rect rect{this->x_low_, this->y_low_, this->x_high_ + 1 - this->x_low_, this->y_high_ + 1 - this->y_low_};
24  this->x_low_ = this->width_;
25  this->y_low_ = this->height_;
26  this->x_high_ = 0;
27  this->y_high_ = 0;
28  SDL_RenderCopy(this->renderer_, this->texture_, &rect, &rect);
29  SDL_RenderPresent(this->renderer_);
30 }
31 
32 void Sdl::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
33  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
34  SDL_Rect rect{x_start, y_start, w, h};
35  if (this->rotation_ != display::DISPLAY_ROTATION_0_DEGREES || bitness != display::COLOR_BITNESS_565 || big_endian) {
36  display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
37  x_pad);
38  } else {
39  auto stride = x_offset + w + x_pad;
40  auto data = ptr + (stride * y_offset + x_offset) * 2;
41  SDL_UpdateTexture(this->texture_, &rect, data, stride * 2);
42  }
43  SDL_RenderCopy(this->renderer_, this->texture_, &rect, &rect);
44  SDL_RenderPresent(this->renderer_);
45 }
46 
47 void Sdl::draw_pixel_at(int x, int y, Color color) {
48  SDL_Rect rect{x, y, 1, 1};
50  SDL_UpdateTexture(this->texture_, &rect, &data, 2);
51  if (x < this->x_low_)
52  this->x_low_ = x;
53  if (y < this->y_low_)
54  this->y_low_ = y;
55  if (x > this->x_high_)
56  this->x_high_ = x;
57  if (y > this->y_high_)
58  this->y_high_ = y;
59 }
60 
61 void Sdl::loop() {
62  SDL_Event e;
63  if (SDL_PollEvent(&e)) {
64  switch (e.type) {
65  case SDL_QUIT:
66  exit(0);
67 
68  case SDL_MOUSEBUTTONDOWN:
69  case SDL_MOUSEBUTTONUP:
70  if (e.button.button == 1) {
71  this->mouse_x = e.button.x;
72  this->mouse_y = e.button.y;
73  this->mouse_down = e.button.state != 0;
74  }
75  break;
76 
77  case SDL_MOUSEMOTION:
78  if (e.motion.state & 1) {
79  this->mouse_x = e.button.x;
80  this->mouse_y = e.button.y;
81  this->mouse_down = true;
82  } else {
83  this->mouse_down = false;
84  }
85  break;
86 
87  default:
88  ESP_LOGV(TAG, "Event %d", e.type);
89  break;
90  }
91  }
92 }
93 
94 } // namespace sdl
95 } // namespace esphome
96 #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:48
uint16_t x
Definition: tt21100.cpp:17
SDL_Window * window_
Definition: sdl_esphome.h:44
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:32
uint8_t h
Definition: bl0906.h:209
uint16_t y
Definition: tt21100.cpp:18
SDL_Renderer * renderer_
Definition: sdl_esphome.h:43
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:659
void update() override
Definition: sdl_esphome.cpp:19
void draw_pixel_at(int x, int y, Color color) override
Definition: sdl_esphome.cpp:47
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
SDL_Texture * texture_
Definition: sdl_esphome.h:45
uint16_t y_high_
Definition: sdl_esphome.h:49
virtual void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorOrder order, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Given an array of pixels encoded in the nominated format, draw these into the display&#39;s buffer...
Definition: display.cpp:54
void loop() override
Definition: sdl_esphome.cpp:61