Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. /*This source code copyrighted by Lazy Foo' Productions (2004-2015)
  2. and may not be redistributed without written permission.*/
  3.  
  4. //Using SDL, SDL_image, standard IO, and strings
  5. #include <SDL2/SDL.h>
  6. #include <SDL2/SDL_image.h>
  7. #include <stdio.h>
  8. #include <string>
  9.  
  10. //Screen dimension constants
  11. const int SCREEN_WIDTH = 640;
  12. const int SCREEN_HEIGHT = 480;
  13.  
  14. //Starts up SDL and creates window
  15. bool init();
  16.  
  17. //Loads media
  18. bool loadMedia();
  19.  
  20. //Frees media and shuts down SDL
  21. void close();
  22.  
  23. //Loads individual image as texture
  24. SDL_Texture* loadTexture( std::string path );
  25.  
  26. //The window we'll be rendering to
  27. SDL_Window* gWindow = NULL;
  28.  
  29. //The window renderer
  30. SDL_Renderer* gRenderer = NULL;
  31.  
  32. //Current displayed texture
  33. SDL_Texture* gTexture = NULL;
  34.  
  35. bool init()
  36. {
  37. //Initialization flag
  38. bool success = true;
  39.  
  40. //Initialize SDL
  41. if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  42. {
  43. printf( "SDL could not initialize! SDL Error: %sn", SDL_GetError() );
  44. success = false;
  45. }
  46. else
  47. {
  48. //Set texture filtering to linear
  49. if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
  50. {
  51. printf( "Warning: Linear texture filtering not enabled!" );
  52. }
  53.  
  54. //Create window
  55. gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  56. if( gWindow == NULL )
  57. {
  58. printf( "Window could not be created! SDL Error: %sn", SDL_GetError() );
  59. success = false;
  60. }
  61. else
  62. {
  63. //Create renderer for window
  64. gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
  65. if( gRenderer == NULL )
  66. {
  67. printf( "Renderer could not be created! SDL Error: %sn", SDL_GetError() );
  68. success = false;
  69. }
  70. else
  71. {
  72. //Initialize renderer color
  73. SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
  74.  
  75. //Initialize PNG loading
  76. int imgFlags = IMG_INIT_PNG;
  77. if( !( IMG_Init( imgFlags ) & imgFlags ) )
  78. {
  79. printf( "SDL_image could not initialize! SDL_image Error: %sn", IMG_GetError() );
  80. success = false;
  81. }
  82. }
  83. }
  84. }
  85.  
  86. return success;
  87. }
  88.  
  89. bool loadMedia()
  90. {
  91. //Loading success flag
  92. bool success = true;
  93.  
  94. //Load PNG texture
  95. gTexture = loadTexture( "texture.png" );
  96. if( gTexture == NULL )
  97. {
  98. printf( "Failed to load texture image!n" );
  99. success = false;
  100. }
  101.  
  102. return success;
  103. }
  104.  
  105. void close()
  106. {
  107. //Free loaded image
  108. SDL_DestroyTexture( gTexture );
  109. gTexture = NULL;
  110.  
  111. //Destroy window
  112. SDL_DestroyRenderer( gRenderer );
  113. SDL_DestroyWindow( gWindow );
  114. gWindow = NULL;
  115. gRenderer = NULL;
  116.  
  117. //Quit SDL subsystems
  118. IMG_Quit();
  119. SDL_Quit();
  120. }
  121.  
  122. SDL_Texture* loadTexture( std::string path )
  123. {
  124. //The final texture
  125. SDL_Texture* newTexture = NULL;
  126.  
  127. //Load image at specified path
  128. SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
  129. if( loadedSurface == NULL )
  130. {
  131. printf( "Unable to load image %s! SDL_image Error: %sn", path.c_str(), IMG_GetError() );
  132. }
  133. else
  134. {
  135. //Create texture from surface pixels
  136. newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
  137. if( newTexture == NULL )
  138. {
  139. printf( "Unable to create texture from %s! SDL Error: %sn", path.c_str(), SDL_GetError() );
  140. }
  141.  
  142. //Get rid of old loaded surface
  143. SDL_FreeSurface( loadedSurface );
  144. }
  145.  
  146. return newTexture;
  147. }
  148.  
  149. int main( int argc, char* args[] )
  150. {
  151.  
  152. SDL_Rect DestR;
  153.  
  154. DestR.x = 0;
  155. DestR.y = 0;
  156. DestR.w = 25;
  157. DestR.h = 25;
  158.  
  159. //Start up SDL and create window
  160. if( !init() )
  161. {
  162. printf( "Failed to initialize!n" );
  163. }
  164. else
  165. {
  166. //Load media
  167. if( !loadMedia() )
  168. {
  169. printf( "Failed to load media!n" );
  170. }
  171. else
  172. {
  173. //Main loop flag
  174. bool quit = false;
  175.  
  176. //Event handler
  177. SDL_Event e;
  178.  
  179. //While application is running
  180. while( !quit )
  181. {
  182. //Handle events on queue
  183. while( SDL_PollEvent( &e ) != 0 )
  184. {
  185. //User requests quit
  186. if( e.type == SDL_QUIT )
  187. {
  188. quit = true;
  189. }
  190. }
  191.  
  192. //Clear screen
  193. SDL_RenderClear( gRenderer );
  194.  
  195. SDL_RenderCopy( gRenderer, gTexture, NULL, &DestR );
  196. //Render texture to screen
  197. DestR.x += 50;
  198. DestR.y += 50;
  199. SDL_RenderCopy( gRenderer, gTexture, NULL, &DestR );
  200.  
  201. //Update screen
  202. SDL_RenderPresent( gRenderer );
  203. }
  204. }
  205. }
  206.  
  207. //Free resources and close SDL
  208. close();
  209.  
  210. return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement