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.h 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <SDL2/SDL.h>
  2. #include <ostream>
  3. #include "util.h"
  4. namespace Swan {
  5. inline std::ostream &operator<<(std::ostream &os, const SDL_Rect &rect) {
  6. os
  7. << "SDL_Rect(" << rect.x << ", " << rect.y << ", "
  8. << rect.w << ", " << rect.h << ")";
  9. return os;
  10. }
  11. class TexLock: NonCopyable {
  12. public:
  13. TexLock(SDL_Texture *tex, SDL_Rect *rect = nullptr);
  14. ~TexLock() { SDL_UnlockTexture(tex_); }
  15. int blit(SDL_Rect *destrect, SDL_Surface *srcsurf, SDL_Rect *srcrect = nullptr) {
  16. return SDL_BlitSurface(srcsurf, srcrect, surf_.get(), destrect);
  17. }
  18. private:
  19. SDL_Texture *tex_;
  20. RaiiPtr<SDL_Surface> surf_ = makeRaiiPtr<SDL_Surface>(nullptr, SDL_FreeSurface);
  21. };
  22. class TexColorMod: NonCopyable {
  23. public:
  24. TexColorMod(SDL_Texture *tex, uint8_t r, uint8_t g, uint8_t b): tex_(tex) {
  25. SDL_GetTextureColorMod(tex_, &r_, &g_, &b_);
  26. SDL_SetTextureColorMod(tex_, r, g, b);
  27. }
  28. ~TexColorMod() {
  29. SDL_SetTextureColorMod(tex_, r_, g_, b_);
  30. }
  31. private:
  32. SDL_Texture *tex_;
  33. uint8_t r_, g_, b_;
  34. };
  35. class TexAlphaMod: NonCopyable {
  36. public:
  37. TexAlphaMod(SDL_Texture *tex, uint8_t alpha): tex_(tex) {
  38. SDL_GetTextureAlphaMod(tex_, &alpha_);
  39. SDL_SetTextureAlphaMod(tex_, alpha);
  40. }
  41. ~TexAlphaMod() {
  42. SDL_SetTextureAlphaMod(tex_, alpha_);
  43. }
  44. private:
  45. SDL_Texture *tex_;
  46. uint8_t alpha_;
  47. };
  48. }