logo

bytemedia

Home to byte-level sounds, images, videos, … git clone https://hacktivis.me/git/bytemedia.git

SDL_YUV_visualisation.c (1566B)


  1. // Copyright © 2020 Haelwenn (lanodan) Monnier <https://hacktivis.me/>
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Inspired by IBNIZ: http://pelulamu.net/ibniz/
  4. #include <SDL2/SDL.h>
  5. #define WIDTH 512
  6. uint32_t pixels[WIDTH][WIDTH];
  7. struct
  8. {
  9. SDL_Window *s;
  10. SDL_Texture *o;
  11. SDL_Renderer *r;
  12. } sdl;
  13. int
  14. main(void)
  15. {
  16. SDL_Init(SDL_INIT_VIDEO);
  17. sdl.s = SDL_CreateWindow("SDL YUV", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, WIDTH, SDL_WINDOW_RESIZABLE);
  18. sdl.r = SDL_CreateRenderer(sdl.s, -1, 0);
  19. SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
  20. SDL_RenderSetLogicalSize(sdl.r, WIDTH, WIDTH);
  21. uint32_t format = SDL_PIXELFORMAT_YUY2;
  22. sdl.o = SDL_CreateTexture(sdl.r, format, SDL_TEXTUREACCESS_STREAMING, WIDTH, WIDTH);
  23. while(1) {
  24. SDL_Event event;
  25. while (SDL_PollEvent(&event)) {
  26. if(event.type == SDL_QUIT) goto quit;
  27. if((event.type == SDL_KEYDOWN)
  28. && (event.key.keysym.sym==SDLK_ESCAPE)
  29. ) goto quit;
  30. }
  31. uint32_t t = SDL_GetTicks();
  32. for(uint32_t y = 0; y < WIDTH; y++)
  33. for(uint32_t x = 0; x < WIDTH; x++)
  34. {
  35. //pixels[x][y] = (t|2)<<(x*y/2); // Mostly green with some colors; Christmas
  36. //pixels[x][y] = (x|t)*(y|t);
  37. //pixels[x][y] = 0x00FF0055;
  38. //pixels[x][y] = ((x/2)<<24&0xFF000000);
  39. pixels[x][y] = ((x/2)<<8&0x0000FF00);
  40. }
  41. SDL_UpdateTexture(sdl.o, NULL, &pixels, WIDTH*SDL_BYTESPERPIXEL(format));
  42. SDL_RenderClear(sdl.r);
  43. SDL_RenderCopy(sdl.r, sdl.o, NULL, NULL);
  44. SDL_RenderPresent(sdl.r);
  45. }
  46. quit:
  47. SDL_DestroyRenderer(sdl.r);
  48. SDL_DestroyWindow(sdl.s);
  49. SDL_Quit();
  50. return 0;
  51. }