-
Notifications
You must be signed in to change notification settings - Fork 21
/
hello6_keyboard.c
187 lines (165 loc) · 5.11 KB
/
hello6_keyboard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* hello6_keyboard.c - Move the sprite using the arrow keys or WASD
*/
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_image.h>
#define WINDOW_WIDTH (640)
#define WINDOW_HEIGHT (480)
// speed in pixels/second
#define SPEED (300)
int main(void)
{
// attempt to initialize graphics and timer system
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
{
printf("error initializing SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window* win = SDL_CreateWindow("Hello, CS50!",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT,0);
if (!win)
{
printf("error creating window: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// create a renderer, which sets up the graphics hardware
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
if (!rend)
{
printf("error creating renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// load the image into memory using SDL_image library function
SDL_Surface* surface = IMG_Load("resources/hello.png");
if (!surface)
{
printf("error creating surface\n");
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// load the image data into the graphics hardware's memory
SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, surface);
SDL_FreeSurface(surface);
if (!tex)
{
printf("error creating texture: %s\n", SDL_GetError());
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// struct to hold the position and size of the sprite
SDL_Rect dest;
// get and scale the dimensions of texture
SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h);
dest.w /= 4;
dest.h /= 4;
// start sprite in center of screen
float x_pos = (WINDOW_WIDTH - dest.w) / 2;
float y_pos = (WINDOW_HEIGHT - dest.h) / 2;
float x_vel = 0;
float y_vel = 0;
// keep track of which inputs are given
int up = 0;
int down = 0;
int left = 0;
int right = 0;
// set to 1 when window close button is pressed
int close_requested = 0;
// animation loop
while (!close_requested)
{
// process events
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
close_requested = 1;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.scancode)
{
case SDL_SCANCODE_W:
case SDL_SCANCODE_UP:
up = 1;
break;
case SDL_SCANCODE_A:
case SDL_SCANCODE_LEFT:
left = 1;
break;
case SDL_SCANCODE_S:
case SDL_SCANCODE_DOWN:
down = 1;
break;
case SDL_SCANCODE_D:
case SDL_SCANCODE_RIGHT:
right = 1;
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.scancode)
{
case SDL_SCANCODE_W:
case SDL_SCANCODE_UP:
up = 0;
break;
case SDL_SCANCODE_A:
case SDL_SCANCODE_LEFT:
left = 0;
break;
case SDL_SCANCODE_S:
case SDL_SCANCODE_DOWN:
down = 0;
break;
case SDL_SCANCODE_D:
case SDL_SCANCODE_RIGHT:
right = 0;
break;
}
break;
}
}
// determine velocity
x_vel = y_vel = 0;
if (up && !down) y_vel = -SPEED;
if (down && !up) y_vel = SPEED;
if (left && !right) x_vel = -SPEED;
if (right && !left) x_vel = SPEED;
// update positions
x_pos += x_vel / 60;
y_pos += y_vel / 60;
// collision detection with bounds
if (x_pos <= 0) x_pos = 0;
if (y_pos <= 0) y_pos = 0;
if (x_pos >= WINDOW_WIDTH - dest.w) x_pos = WINDOW_WIDTH - dest.w;
if (y_pos >= WINDOW_HEIGHT - dest.h) y_pos = WINDOW_HEIGHT - dest.h;
// set the positions in the struct
dest.y = (int) y_pos;
dest.x = (int) x_pos;
// clear the window
SDL_RenderClear(rend);
// draw the image to the window
SDL_RenderCopy(rend, tex, NULL, &dest);
SDL_RenderPresent(rend);
// wait 1/60th of a second
SDL_Delay(1000/60);
}
// clean up resources before exiting
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
}