A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gfxutil.cc 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "gfxutil.h"
  2. #include <stdint.h>
  3. #include "log.h"
  4. namespace Swan {
  5. TexLock::TexLock(SDL_Texture *tex, SDL_Rect *rect): tex_(tex) {
  6. // We must query the texture to get a format...
  7. uint32_t format;
  8. int access, texw, texh;
  9. if (SDL_QueryTexture(tex_, &format, &access, &texw, &texh) < 0) {
  10. panic << "Failed to query texture: " << SDL_GetError();
  11. abort();
  12. }
  13. SDL_Rect lockrect = rect == NULL
  14. ? SDL_Rect{ 0, 0, texw, texh }
  15. : *rect;
  16. // ...and convert that format into masks...
  17. int bpp = 32;
  18. uint32_t rmask, gmask, bmask, amask;
  19. if (SDL_PixelFormatEnumToMasks(format, &bpp, &rmask, &gmask, &bmask, &amask) != SDL_TRUE) {
  20. panic << "Failed to get pixel mask: " << SDL_GetError();
  21. abort();
  22. }
  23. // ...and lock the texture...
  24. uint8_t *pixels;
  25. int pitch;
  26. if (SDL_LockTexture(tex_, &lockrect, (void **)&pixels, &pitch) < 0) {
  27. panic << "Failed to lock texture: " << SDL_GetError();
  28. abort();
  29. }
  30. // ...in order to create a surface.
  31. surf_.reset(SDL_CreateRGBSurfaceFrom(
  32. pixels, lockrect.w, lockrect.h,
  33. 32, pitch, rmask, gmask, bmask, amask));
  34. }
  35. }