Advertisement
Guest User

imageTEst.c

a guest
Dec 31st, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #define SDL_MAIN_HANDLED
  2. #include <stdio.h>
  3. #include<stdlib.h>
  4. #include"../include/SDL2/SDL.h"
  5. #include"../include/SDL2/SDL_timer.h"
  6. #include "../include/SDL2/SDL_image.h"
  7.  
  8.  
  9.  
  10. int main(void)
  11. {
  12. FILE *logs = fopen("logs.txt","a");
  13. // attempt to initialize graphics and timer system
  14. if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
  15. {
  16. fprintf(logs,"error initializing SDL: %s\n", SDL_GetError());
  17. return 1;
  18. }
  19.  
  20. SDL_Window* win = SDL_CreateWindow("Hello, CS50!",
  21. SDL_WINDOWPOS_CENTERED,
  22. SDL_WINDOWPOS_CENTERED,
  23. 640, 480, 0);
  24. if (!win)
  25. {
  26. fprintf(logs,"error creating window: %s\n", SDL_GetError());
  27. SDL_Quit();
  28. return 1;
  29. }
  30.  
  31. // create a renderer, which sets up the graphics hardware
  32. Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
  33. SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
  34. if (!rend)
  35. {
  36. fprintf(logs,"error creating renderer: %s\n", SDL_GetError());
  37. SDL_DestroyWindow(win);
  38. SDL_Quit();
  39. return 1;
  40. }
  41.  
  42. // load the image into memory using SDL_image library function
  43. SDL_Surface* surface = IMG_Load("res/r.png");
  44. if (!surface)
  45. {
  46. fprintf(logs,"error creating surface: %s\n", SDL_GetError());
  47. SDL_DestroyRenderer(rend);
  48. SDL_DestroyWindow(win);
  49. SDL_Quit();
  50. return 1;
  51. }
  52.  
  53. // load the image data into the graphics hardware's memory
  54. SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, surface);
  55. SDL_FreeSurface(surface);
  56. if (!tex)
  57. {
  58. fprintf(logs,"error creating texture: %s\n", SDL_GetError());
  59. SDL_DestroyRenderer(rend);
  60. SDL_DestroyWindow(win);
  61. SDL_Quit();
  62. return 1;
  63. }
  64.  
  65. // clear the window
  66. SDL_RenderClear(rend);
  67.  
  68. // draw the image to the window
  69. SDL_RenderCopy(rend, tex, NULL, NULL);
  70. SDL_RenderPresent(rend);
  71.  
  72. // wait a few seconds
  73. SDL_Delay(5000);
  74.  
  75. // clean up resources before exiting
  76. fclose(logs);
  77. SDL_DestroyTexture(tex);
  78. SDL_DestroyRenderer(rend);
  79. SDL_DestroyWindow(win);
  80. SDL_Quit();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement