sdl_helper.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <SDL2/SDL.h>
  2. uint32_t pixels[S3L_RESOLUTION_X * S3L_RESOLUTION_Y];
  3. SDL_Window *window;
  4. SDL_Renderer *renderer;
  5. SDL_Texture *textureSDL;
  6. SDL_Surface *screenSurface;
  7. SDL_Event event;
  8. void sdlInit(void)
  9. {
  10. window = SDL_CreateWindow("model viewer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN);
  11. renderer = SDL_CreateRenderer(window,-1,0);
  12. textureSDL = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y);
  13. screenSurface = SDL_GetWindowSurface(window);
  14. }
  15. void sdlEnd(void)
  16. {
  17. SDL_DestroyTexture(textureSDL);
  18. SDL_DestroyRenderer(renderer);
  19. SDL_DestroyWindow(window);
  20. }
  21. void sdlUpdate(void)
  22. {
  23. SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));
  24. SDL_RenderClear(renderer);
  25. SDL_RenderCopy(renderer,textureSDL,NULL,NULL);
  26. SDL_RenderPresent(renderer);
  27. }
  28. void clearScreen(void)
  29. {
  30. memset(pixels,200,S3L_RESOLUTION_X * S3L_RESOLUTION_Y * sizeof(uint32_t));
  31. }
  32. static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue)
  33. {
  34. uint8_t *p = ((uint8_t *) pixels) + (y * S3L_RESOLUTION_X + x) * 4 + 1;
  35. *p = blue;
  36. ++p;
  37. *p = green;
  38. ++p;
  39. *p = red;
  40. }
  41. void sampleTexture(const uint8_t *tex, int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b)
  42. {
  43. u = S3L_wrap(u,TEXTURE_W);
  44. v = S3L_wrap(v,TEXTURE_H);
  45. const uint8_t *t = tex + (v * TEXTURE_W + u) * 4;
  46. *r = *t;
  47. t++;
  48. *g = *t;
  49. t++;
  50. *b = *t;
  51. }