fork download
  1. // CSurface.h
  2. //------------
  3.  
  4. #ifndef C_SURFACE
  5. #define C_SURFACE
  6.  
  7. #include <SDL.h>
  8. #include <iostream>
  9. #include <string>
  10. #include <map>
  11. #include "../Main.h"
  12.  
  13. using namespace std;
  14.  
  15. extern GlobalState g_applicationState;
  16.  
  17. class CSurface
  18. {
  19. public:
  20. CSurface(void);
  21.  
  22. static bool LoadImage(string name, string file);
  23. static void FreeImage(string name);
  24. static void Unload();
  25.  
  26. static bool DrawImage(string name, int x, int y);
  27. static bool DrawImage(string name, int x, int y, int x2, int y2, int w, int h);
  28.  
  29. private:
  30. static bool Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y, SDL_Rect* rect);
  31. static map<string, SDL_Surface*> loadedSurfaces;
  32. };
  33.  
  34. #endif
  35.  
  36. // --------------------
  37. // CSurface.cpp
  38. //---------------------
  39.  
  40. #include <SDL_image.h>
  41. #include "CSurface.h"
  42.  
  43. CSurface::CSurface()
  44. {
  45.  
  46. }
  47.  
  48. bool CSurface::LoadImage(string name, string file) {
  49.  
  50. if(loadedSurfaces.find(name) == loadedSurfaces.end())
  51. {
  52. fprintf(stderr, "Attempt to load resource '%s' but is already in resources (file '%s')", name, file);
  53. return false;
  54. }
  55.  
  56. SDL_Surface* temp = NULL;
  57. SDL_Surface* result = NULL;
  58.  
  59. temp = IMG_Load(file.c_str());
  60. if(temp == NULL)
  61. {
  62. fprintf(stderr, "There was an error loading the temp surface for %s\n", file);
  63. return false;
  64. }
  65.  
  66. result = SDL_DisplayFormatAlpha(temp);
  67. SDL_FreeSurface(temp);
  68.  
  69. fprintf(stdout, "Loaded Image '%s' as '%s'\n", file, name);
  70.  
  71. loadedSurfaces[name] = result;
  72.  
  73. return true;
  74. }
  75.  
  76. void CSurface::FreeImage(string name)
  77. {
  78. if(loadedSurfaces.find(name) == loadedSurfaces.end())
  79. {
  80. fprintf(stderr, "Cannot find '%s' to free it", name);
  81. return;
  82. }
  83.  
  84. SDL_Surface* surf = loadedSurfaces[name];
  85. SDL_FreeSurface(surf);
  86. loadedSurfaces.erase(name);
  87. fprintf(stdout, "Unloaded Image '%s'\n", name);
  88. }
  89.  
  90. void CSurface::Unload()
  91. {
  92. if(loadedSurfaces.size() == 0)
  93. return;
  94.  
  95. for(map<string, SDL_Surface*>::iterator ii = loadedSurfaces.begin(); ii != loadedSurfaces.end(); ii++)
  96. {
  97. FreeImage(ii->first);
  98. }
  99. }
  100.  
  101. bool CSurface::DrawImage(string name, int x, int y) {
  102.  
  103. return Draw(g_applicationState.screen, loadedSurfaces[name], x, y, NULL);
  104. }
  105.  
  106. bool CSurface::DrawImage(string name, int x, int y, int x2, int y2, int w, int h)
  107. {
  108. SDL_Rect sourceRect;
  109.  
  110. sourceRect.x = x2;
  111. sourceRect.y = y2;
  112. sourceRect.w = w;
  113. sourceRect.h = h;
  114.  
  115. return Draw(g_applicationState.screen, loadedSurfaces[name], x, y, &sourceRect);
  116. }
  117.  
  118. bool CSurface::Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y, SDL_Rect* rect)
  119. {
  120. if(dest == NULL || src == NULL) {
  121. return false;
  122. }
  123.  
  124. SDL_Rect destinationRect;
  125.  
  126. destinationRect.x = x;
  127. destinationRect.y = y;
  128.  
  129. SDL_BlitSurface(src, rect, dest, &destinationRect);
  130.  
  131. return true;
  132. }
  133.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty