· 4 years ago · Sep 02, 2021, 11:06 PM
1#include <SDL2/SDL.h>
2#include <SDL2/SDL_image.h>
3#include <SDL2/SDL_timer.h>
4
5int main(int argc, char *argv[])
6{
7
8 // retutns zero on success else non-zero
9 if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
10 printf("error initializing SDL: %s\n", SDL_GetError());
11 }
12 SDL_Window* win = SDL_CreateWindow("GAME", // creates a window
13 SDL_WINDOWPOS_CENTERED,
14 SDL_WINDOWPOS_CENTERED,
15 1000, 1000, 0);
16
17 // triggers the program that controls
18 // your graphics hardware and sets flags
19 Uint32 render_flags = SDL_RENDERER_ACCELERATED;
20
21 // creates a renderer to render our images
22 SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
23
24 // creates a surface to load an image into the main memory
25 SDL_Surface* surface;
26
27 // please provide a path for your image
28 surface = IMG_Load("path");
29
30 // loads image to our graphics hardware memory.
31 SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, surface);
32
33 // clears main-memory
34 SDL_FreeSurface(surface);
35
36 // let us control our image position
37 // so that we can move it with our keyboard.
38 SDL_Rect dest;
39
40 // connects our texture with dest to control position
41 SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h);
42
43 // adjust height and width of our image box.
44 dest.w /= 6;
45 dest.h /= 6;
46
47 // sets initial x-position of object
48 dest.x = (1000 - dest.w) / 2;
49
50 // sets initial y-position of object
51 dest.y = (1000 - dest.h) / 2;
52
53 // controls annimation loop
54 int close = 0;
55
56 // speed of box
57 int speed = 300;
58
59 // annimation loop
60 while (!close) {
61 SDL_Event event;
62
63 // Events management
64 while (SDL_PollEvent(&event)) {
65 switch (event.type) {
66
67 case SDL_QUIT:
68 // handling of close button
69 close = 1;
70 break;
71
72 case SDL_KEYDOWN:
73 // keyboard API for key pressed
74 switch (event.key.keysym.scancode) {
75 case SDL_SCANCODE_W:
76 case SDL_SCANCODE_UP:
77 dest.y -= speed / 30;
78 break;
79 case SDL_SCANCODE_A:
80 case SDL_SCANCODE_LEFT:
81 dest.x -= speed / 30;
82 break;
83 case SDL_SCANCODE_S:
84 case SDL_SCANCODE_DOWN:
85 dest.y += speed / 30;
86 break;
87 case SDL_SCANCODE_D:
88 case SDL_SCANCODE_RIGHT:
89 dest.x += speed / 30;
90 break;
91 default:
92 break;
93 }
94 }
95 }
96
97 // right boundary
98 if (dest.x + dest.w > 1000)
99 dest.x = 1000 - dest.w;
100
101 // left boundary
102 if (dest.x < 0)
103 dest.x = 0;
104
105 // bottom boundary
106 if (dest.y + dest.h > 1000)
107 dest.y = 1000 - dest.h;
108
109 // upper boundary
110 if (dest.y < 0)
111 dest.y = 0;
112
113 // clears the screen
114 SDL_RenderClear(rend);
115 SDL_RenderCopy(rend, tex, NULL, &dest);
116
117 // triggers the double buffers
118 // for multiple rendering
119 SDL_RenderPresent(rend);
120
121 // calculates to 60 fps
122 SDL_Delay(1000 / 60);
123 }
124
125 // destroy texture
126 SDL_DestroyTexture(tex);
127
128 // destroy renderer
129 SDL_DestroyRenderer(rend);
130
131 // destroy window
132 SDL_DestroyWindow(win);
133
134 // close SDL
135 SDL_Quit();
136
137 return 0;
138}
139