texturer.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // Created by jovian on 16/07/17.
  3. //
  4. #include "texturer.h"
  5. void logSDLError(std::ostream &os, const std::string &msg) {
  6. os << msg << " error: " << SDL_GetError() << std::endl;
  7. }
  8. SDL_Texture *loadTexture(const std::string &file, SDL_Renderer *ren) {
  9. SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
  10. if (texture == nullptr) {
  11. logSDLError(std::cout, "LoadTexture");
  12. }
  13. return texture;
  14. }
  15. void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, bool centered) {
  16. int w, h;
  17. SDL_QueryTexture(tex, NULL, NULL, &w, &h);
  18. renderTexture(tex, ren, x, y, w, h, centered);
  19. }
  20. void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h, bool centered) {
  21. SDL_Rect dst;
  22. dst.x = x;
  23. dst.y = y;
  24. dst.w = w;
  25. dst.h = h;
  26. if (centered) {
  27. dst.x -= dst.w / 2;
  28. dst.y -= dst.h / 2;
  29. }
  30. SDL_RenderCopy(ren, tex, NULL, &dst);
  31. }
  32. void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, double angle, bool centered) {
  33. int w, h;
  34. SDL_QueryTexture(tex, NULL, NULL, &w, &h);
  35. renderTexture(tex, ren, x, y, angle, w, h, centered);
  36. }
  37. void
  38. renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, double angle, int w, int h, bool centered) {
  39. SDL_Rect dst;
  40. dst.x = x;
  41. dst.y = y;
  42. dst.w = w;
  43. dst.h = h;
  44. if (centered) {
  45. dst.x -= dst.w / 2;
  46. dst.y -= dst.h / 2;
  47. }
  48. SDL_RenderCopyEx(ren, tex, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  49. }