luat_lcd_sdl2.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "luat_base.h"
  2. #include "luat_lcd.h"
  3. #include "luat_sdl2.h"
  4. #include "luat_malloc.h"
  5. static uint32_t* fb;
  6. static inline uint32_t luat_color_565to8888(luat_color_t color);
  7. static int sdl2_init(luat_lcd_conf_t* conf) {
  8. luat_sdl2_conf_t sdl2_conf = {
  9. .width = conf->w,
  10. .height = conf->h
  11. };
  12. luat_sdl2_init(&sdl2_conf);
  13. fb = luat_heap_malloc(sizeof(uint32_t) * conf->w * conf->h);
  14. luat_lcd_clear(conf, WHITE);
  15. // printf("ARGB8888 0xFFFF %08X\n", luat_color_565to8888(0xFFFF));
  16. // printf("ARGB8888 0X001F %08X\n", luat_color_565to8888(0X001F));
  17. // printf("ARGB8888 0xF800 %08X\n", luat_color_565to8888(0xF800));
  18. // printf("ARGB8888 0x0CE0 %08X\n", luat_color_565to8888(0x0CE0));
  19. return 0;
  20. }
  21. const luat_lcd_opts_t lcd_opts_sdl2 = {
  22. .name = "sdl2",
  23. .init = sdl2_init,
  24. };
  25. typedef struct luat_color_rgb565swap
  26. {
  27. uint16_t blue : 5;
  28. uint16_t green : 6;
  29. uint16_t red : 5;
  30. }luat_color_rgb565swap_t;
  31. typedef struct luat_color_argb8888
  32. {
  33. uint8_t blue;
  34. uint8_t green;
  35. uint8_t red;
  36. uint8_t alpha;
  37. }luat_color_argb8888_t;
  38. static inline uint32_t luat_color_565to8888(luat_color_t color) {
  39. luat_color_rgb565swap_t tmp;
  40. memcpy(&tmp, &color, sizeof(luat_color_rgb565swap_t));
  41. luat_color_argb8888_t dst = {
  42. .alpha = 0xFF,
  43. .red = (tmp.red * 263 + 7) >> 5,
  44. .green = (tmp.green * 259 + 3) >> 6,
  45. .blue = (tmp.blue *263 + 7) >> 5
  46. };
  47. uint32_t t;
  48. memcpy(&t, &dst, sizeof(luat_color_argb8888_t));
  49. //printf("ARGB8888 %08X\n", t);
  50. return t;
  51. }
  52. int luat_lcd_draw(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t* color_p) {
  53. size_t rw = x2 - x1 + 1;
  54. size_t rh = y2 - y1 + 1;
  55. uint32_t *tmp = fb;
  56. for (size_t i = 0; i < rh; i++)
  57. {
  58. for (size_t j = 0; j < rw; j++)
  59. {
  60. *tmp = luat_color_565to8888(*color_p);
  61. tmp ++;
  62. color_p ++;
  63. }
  64. }
  65. luat_sdl2_draw(x1, y1, x2, y2, fb);
  66. luat_sdl2_flush();
  67. return 0;
  68. }