luat_sdl2.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "luat_base.h"
  2. #include "luat_sdl2.h"
  3. #include "SDL2/SDL.h"
  4. #define LUAT_LOG_TAG "sdl2"
  5. #include "luat_log.h"
  6. static SDL_Window *window = NULL;
  7. static SDL_Renderer *renderer = NULL;
  8. static SDL_Texture *framebuffer = NULL;
  9. static uint32_t* fb;
  10. static luat_sdl2_conf_t sdl_conf;
  11. int luat_sdl2_init(luat_sdl2_conf_t *conf) {
  12. if (framebuffer != NULL) {
  13. LLOGD("SDL2 aready inited");
  14. return -1;
  15. }
  16. if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
  17. LLOGE("SDL_InitSubSystem failed: %s\n", SDL_GetError());
  18. return -1;
  19. }
  20. memcpy(&sdl_conf, conf, sizeof(luat_sdl2_conf_t));
  21. window = SDL_CreateWindow(conf->title == NULL ? "LuatOS" : conf->title,
  22. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  23. conf->width, conf->height, 0);
  24. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  25. framebuffer = SDL_CreateTexture(renderer,
  26. SDL_PIXELFORMAT_ARGB8888,
  27. SDL_TEXTUREACCESS_STREAMING,
  28. conf->width,
  29. conf->height);
  30. // fb = luat_heap_malloc(sizeof(uint32_t) * conf->width * conf->height);
  31. return 0;
  32. }
  33. int luat_sdl2_deinit(luat_sdl2_conf_t *conf) {
  34. SDL_DestroyTexture(framebuffer);
  35. SDL_DestroyRenderer(renderer);
  36. SDL_DestroyWindow(window);
  37. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  38. // free(fb);
  39. framebuffer = NULL;
  40. renderer = NULL;
  41. window = NULL;
  42. // fb = NULL;
  43. return 0;
  44. }
  45. void luat_sdl2_draw(size_t x1, size_t y1, size_t x2, size_t y2, uint32_t* data) {
  46. SDL_Rect r;
  47. r.x = x1;
  48. r.y = y1;
  49. r.w = x2 - x1 + 1;
  50. r.h = y2 - y1 + 1;
  51. SDL_UpdateTexture(framebuffer, &r, data, r.w * 4);
  52. }
  53. void luat_sdl2_flush(void) {
  54. if (renderer && framebuffer)
  55. {
  56. SDL_RenderCopy(renderer, framebuffer, NULL, NULL);
  57. SDL_RenderPresent(renderer);
  58. }
  59. }