Q009

Untitled

Mar 12th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. //////////////
  2. // main.cpp //
  3. //////////////
  4.  
  5. #include "engine.h"
  6.  
  7. /* SOME OTHER STUFF HERE */
  8. texture *stuffAndThings[] =
  9. {
  10. newTexture("texture1.bmp", SCR_W, SCR_H),
  11. newTexture("up.bmp", SCR_W, SCR_H),
  12. newTexture("down.bmp", SCR_W, SCR_H),
  13. newTexture("left.bmp", SCR_W, SCR_H),
  14. newTexture("right.bmp", SCR_W, SCR_H)
  15. };
  16.  
  17. int main(int numArgs, char **args)
  18. {
  19. newTexture("texture1.bmp", SCR_W, SCR_H);
  20. newTexture("up.bmp", SCR_W, SCR_H);
  21. newTexture("down.bmp", SCR_W, SCR_H);
  22. newTexture("left.bmp", SCR_W, SCR_H);
  23. newTexture("right.bmp", SCR_W, SCR_H);
  24.  
  25. if(init() && initMedia()) SDL_Delay(5000);
  26. quit();
  27. printf("\n\nThe program has ended..\n"); fflush(stdin); getchar();
  28. return 0;
  29. }
  30.  
  31. /////////////////
  32. // texture.cpp //
  33. /////////////////
  34.  
  35. #include "engine.h"
  36.  
  37. SDL_Texture *texError = NULL;
  38. std::vector<texture*> textures;
  39.  
  40. texture::texture(const char *_path, int w, int h) : path(_path), t(NULL)
  41. {
  42. r.w = w;
  43. r.h = h ? h : w;
  44. r.x = r.y = 0;
  45. }
  46.  
  47. texture::~texture() { if(t != texError) SDL_DestroyTexture(t); }
  48.  
  49. void texture::draw()
  50. {
  51. if(!t) return;
  52. SDL_RenderCopy(renderer, t, NULL, &r);
  53. }
  54.  
  55. SDL_Texture *openImage(const char *path)
  56. {
  57. if(!path) return NULL;
  58. log("Texture: %s\n", path);
  59. SDL_Surface *s = IMG_Load(path);
  60. if(!s)
  61. {
  62. log("Failed to open image file: %s\n", IMG_GetError());
  63. return NULL;
  64. }
  65. SDL_Texture *t = SDL_CreateTextureFromSurface(renderer, s);
  66. SDL_FreeSurface(s);
  67. if(!t)
  68. {
  69. log("SDL_CreateTextureFromSurface failed: %s\n", SDL_GetError());
  70. t = NULL;
  71. }
  72.  
  73. return t;
  74. }
  75.  
  76. texture *newTexture(const char *path, int w, int h)
  77. {
  78. texture *_newTexture = new texture(path, w, h);
  79. for(size_t i = 0; i < textures.size(); ++i)
  80. if(!textures[i])
  81. {
  82. textures[i] = _newTexture;
  83. return _newTexture;
  84. }
  85.  
  86. textures.push_back(_newTexture);
  87. return _newTexture;
  88. }
  89.  
  90. bool loadTextures()
  91. {
  92. if(!(texError = openImage("texerror.bmp"))) return false;
  93. for(size_t i = 0; i < textures.size(); ++i) textures[i]->t = openImage(textures[i]->path);
  94. return true;
  95. }
  96.  
  97. void freeTextures()
  98. {
  99. for(size_t i = 0; i < textures.size(); ++i)
  100. {
  101. delete textures[i];
  102. textures[i] = NULL;
  103. }
  104. SDL_DestroyTexture(texError);
  105. texError = NULL;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment