Advertisement
Guest User

Untitled

a guest
Oct 12th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <cstdint>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. #include <SDL.h>
  7. #include <stdint.h>
  8.  
  9. static void draw(SDL_Window *gWindow, SDL_Surface *globalImage,
  10. SDL_Surface *globalScreenSurface) {
  11.  
  12. if (EXIT_SUCCESS != SDL_BlitSurface(globalImage, nullptr, globalScreenSurface, nullptr)) {
  13. std::cerr << "SDL_BlitSurface() failed. Reason: " << SDL_GetError() << std::endl;
  14. }
  15.  
  16. if (EXIT_SUCCESS != SDL_UpdateWindowSurface(gWindow)) {
  17. std::cerr << "SDL_UpdateWindowSurface() failed. Reason: " << SDL_GetError()
  18. << std::endl;
  19. }
  20. SDL_Delay(5000);
  21. }
  22.  
  23. static int32_t loadResources(SDL_Surface *globalImage) {
  24. const std::string filePath = "../resources/hello.bmp";
  25. globalImage = SDL_LoadBMP(filePath.c_str());
  26. if (globalImage == nullptr) {
  27. std::cerr << "SDL_LoadBMP() failed. Reason: " << SDL_GetError() << std::endl;
  28. return EXIT_FAILURE;
  29. }
  30. return EXIT_SUCCESS;
  31. }
  32.  
  33. static int32_t init(SDL_Window *gWindow, SDL_Surface *globalScreenSurface,
  34. SDL_Surface *globalImage) {
  35. if (EXIT_SUCCESS != SDL_Init(SDL_INIT_VIDEO)) {
  36. std::cerr << "SDL_Init() failed. Reason: " << SDL_GetError() << std::endl;
  37. return EXIT_FAILURE;
  38. }
  39. const std::string windowName = "First window";
  40. const int windowHeight = 800;
  41. const int windowWidth = 600;
  42.  
  43. gWindow = SDL_CreateWindow(windowName.c_str(),
  44. SDL_WINDOWPOS_UNDEFINED,
  45. SDL_WINDOWPOS_UNDEFINED, windowHeight, windowWidth, SDL_WINDOW_SHOWN);
  46. if (gWindow == nullptr) {
  47. std::cerr << "SDL_CreateWindow() failed. Reason: " << SDL_GetError() << std::endl;
  48. }
  49. globalScreenSurface = SDL_GetWindowSurface(gWindow);
  50. if (globalScreenSurface == nullptr) {
  51. std::cerr << "SDL_GetWindowSurface() failed. Reason: " << SDL_GetError() << std::endl;
  52. }
  53. if (EXIT_SUCCESS != loadResources(globalImage)) {
  54. std::cerr << "loadResources() failed." << std::endl;
  55. return EXIT_FAILURE;
  56. }
  57. return EXIT_SUCCESS;
  58. }
  59.  
  60. void deinit(SDL_Surface *globalImage, SDL_Window *gWindow) {
  61. SDL_FreeSurface(globalImage);
  62. if (gWindow != nullptr) {
  63. SDL_DestroyWindow(gWindow);
  64. gWindow = nullptr;
  65. }
  66. SDL_Quit();
  67. }
  68.  
  69. int32_t main([[maybe_unused]]int32_t argc, [[maybe_unused]]char *argv[]) {
  70.  
  71. SDL_Window *gWindow = nullptr;
  72. SDL_Surface *globalScreenSurface = nullptr;
  73. SDL_Surface *globalImage = nullptr;
  74.  
  75. if (EXIT_SUCCESS != init(gWindow, globalScreenSurface, globalImage)) {
  76. std::cerr << "init() failed" << std::endl;
  77. return EXIT_FAILURE;
  78. }
  79.  
  80. draw(gWindow, globalScreenSurface, globalImage);
  81.  
  82. deinit(globalImage, gWindow);
  83.  
  84. return EXIT_SUCCESS;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement