Advertisement
Guest User

Untitled

a guest
Jul 24th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <stdio.h>
  3. #include <string>
  4.  
  5. const int SCREEN_HEIGHT = 768;
  6. const int SCREEN_WIDTH = 1024;
  7.  
  8. bool init();
  9. bool loadMedia();
  10. void close();
  11.  
  12. SDL_Surface* loadSurface(std::string path);
  13.  
  14. SDL_Surface* gScreenSurface = NULL;
  15. SDL_Surface* gStretchedSurface = NULL;
  16. SDL_Window* gWindow = NULL;
  17.  
  18. int main(int argc, char* args[])
  19. {
  20. if (!init())
  21. {
  22. printf("SDL couldn't initialize");
  23. }
  24. else
  25. {
  26. if (!loadMedia())
  27. {
  28. printf("couldn't load media");
  29. }
  30. else
  31. {
  32. bool quit = false;
  33. SDL_Event e;
  34. while (!quit)
  35. {
  36. while (SDL_PollEvent(&e) != 0)
  37. {
  38. if (e.type == SDL_QUIT)
  39. {
  40. quit = true;
  41. }
  42. }
  43.  
  44. //apply stretched image
  45. SDL_Rect stretchRect;
  46. stretchRect.x = 0;
  47. stretchRect.y = 0;
  48. stretchRect.w = SCREEN_WIDTH;
  49. stretchRect.h = SCREEN_HEIGHT;
  50. SDL_BlitScaled(gStretchedSurface, NULL, gScreenSurface, &stretchRect);
  51. SDL_UpdateWindowSurface(gWindow);
  52. }
  53. }
  54. }
  55.  
  56. close();
  57. return 0;
  58. }
  59. bool init()
  60. {
  61. bool success = true;
  62. if (SDL_Init(SDL_INIT_VIDEO) < 0)
  63. {
  64. printf("SDL Couldn't initialize.");
  65. success = false;
  66. }
  67. else
  68. {
  69. gWindow = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  70. if (gWindow == NULL)
  71. {
  72. printf("SDL Window failure.");
  73. success = false;
  74. }
  75. else
  76. {
  77. gScreenSurface = SDL_GetWindowSurface(gWindow);
  78. //SDL_Delay(100000);
  79. }
  80. }
  81. return success;
  82. }
  83. bool loadMedia()
  84. {
  85. bool success = true;
  86. gStretchedSurface = loadSurface("stretch.bmp");
  87. if (gStretchedSurface == NULL)
  88. {
  89. printf("Couldnt load screen surface");
  90. success = false;
  91. }
  92.  
  93. return success;
  94. }
  95. SDL_Surface* loadSurface(std::string path)
  96. {
  97. //final optimized Image
  98. SDL_Surface* optimizedSurface = NULL;
  99. //load image at specified path
  100. SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
  101. if (loadedSurface == NULL)
  102. {
  103. printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
  104. }
  105. else
  106. {
  107. //convert surface to screen format
  108. optimizedSurface = SDL_ConvertSurface(loadedSurface, gScreenSurface->format, NULL);
  109. if (optimizedSurface == NULL)
  110. {
  111. printf("Unable to optimize image %s! SDL error: ", path.c_str(), SDL_GetError());
  112. }
  113. //get rid of old loaded surface
  114. SDL_FreeSurface(loadedSurface);
  115. }
  116. return optimizedSurface;
  117. }
  118. void close()
  119. {
  120. SDL_FreeSurface(gStretchedSurface);
  121. gStretchedSurface = nullptr;
  122. SDL_DestroyWindow(gWindow);
  123. gWindow = nullptr;
  124. SDL_Quit();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement