Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //The headers
  2. #include "SDL/SDL.h"
  3. #include "SDL/SDL_image.h"
  4. #include <string>
  5.  
  6. //Screen attributes
  7. const int SCREEN_WIDTH = 640;
  8. const int SCREEN_HEIGHT = 480;
  9. const int SCREEN_BPP = 32;
  10.  
  11. //The surfaces
  12. SDL_Surface *image = NULL;
  13. SDL_Surface *screen = NULL;
  14.  
  15. //The event structure that will be used
  16. SDL_Event event;
  17.  
  18. SDL_Surface *load_image(std::string filename)
  19. {
  20. //The image that's loaded
  21. SDL_Surface* loadedImage = NULL;
  22. //The optimized image that will be used
  23. SDL_Surface* optimizedImage = NULL;
  24. //Load the image
  25. loadedImage = IMG_Load(filename.c_str());
  26. //If the image loaded
  27. if(loadedImage != NULL)
  28. {            
  29. //Create an optimized image
  30. optimizedImage = SDL_DisplayFormat(loadedImage);
  31. //Free the old image
  32. SDL_FreeSurface(loadedImage);
  33. }
  34. //Return the optimized image
  35. return optimizedImage;
  36. }
  37. void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
  38. {
  39. //Temporary rectangle to hold the offsets
  40. SDL_Rect offset;
  41.    
  42. //Get the offsets
  43. offset.x = x;
  44. offset.y = y;
  45. //Blit the surface
  46. SDL_BlitSurface(source, NULL, destination, &offset);
  47. }
  48.                          
  49. bool init()
  50. {
  51. //Initialize all SDL subsystems
  52. if(SDL_Init(SDL_INIT_EVERYTHING) == -1);
  53. {
  54. return false;
  55. }
  56.          
  57. //Set up the screen
  58. screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
  59. //If there was an error in setting up the screen
  60. if(screen == NULL)
  61. {
  62. return false;
  63. }
  64.                          
  65. //Set the window caption
  66. SDL_WM_SetCaption("Event test", NULL);
  67.                      
  68. //If everythign initialized fine
  69. return true;
  70. }
  71.                                                                                
  72. bool load_files()
  73. {
  74. //Load the image
  75. image = load_image("x.png");
  76. //If there was an error in loading the image
  77. if(image == NULL)
  78. {
  79. return false;
  80. }
  81. //if everything loads fine
  82. return true;
  83. }
  84.                                                                    
  85. void clean_up()
  86. {
  87. //free the image
  88. SDL_FreeSurface(image);
  89. //quit SDL
  90. SDL_Quit();
  91. }
  92.                                                                              
  93. int main(int argc, char* args[])
  94. {
  95. //Make sure the program waits for a quit
  96. bool quit = false;
  97. //initialize
  98. if(init() == false)
  99. {
  100. return 1;
  101. }
  102. //Load the files
  103. if(load_files() == false)
  104. {
  105. return 1;
  106. }
  107.                                                                                                          
  108. //Apply the surface to the screen
  109. apply_surface(0, 0, image, screen);
  110.                                                                                            
  111. //update the screen
  112. if(SDL_Flip(screen) == -1)
  113. {
  114. return 1;
  115. }
  116.                                                                                                                                      
  117. //While the user hasn't quit
  118. while(quit == false)
  119. {
  120. while(SDL_PollEvent(&event))
  121. {
  122. //if the user has Xed out the window
  123. if(event.type == SDL_QUIT)
  124. {
  125. //Quit the program
  126. quit = true;
  127. }
  128. }
  129. }
  130. //free the surface and quit SDL
  131. clean_up();
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement