This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_press.cpp~
executable file
·357 lines (297 loc) · 9.78 KB
/
key_press.cpp~
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL, standard IO, and strings
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <string>
#include <SerialStream.h>
using namespace LibSerial;
SerialStream serialPort;
//Screen dimension constants
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
//Key press surfaces constants
enum KeyPressSurfaces
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_MANUAL,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_FRONT,
KEY_PRESS_SURFACE_BACK,
KEY_PRESS_SURFACE_ROLLL,
KEY_PRESS_SURFACE_ROLLR,
KEY_PRESS_SURFACE_TOTAL
};
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//Loads individual image
SDL_Surface* loadSurface( std::string path );
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The images that correspond to a keypress
SDL_Surface* gKeyPressSurfaces[ KEY_PRESS_SURFACE_TOTAL ];
//Current displayed image
SDL_Surface* gCurrentSurface = NULL;
//automatic / manual mode
bool manual = true;
bool init()
{
//Initialization flag
bool success = true;
serialPort.Open("/dev/ttyUSB0");
serialPort.SetBaudRate(SerialStreamBuf::BAUD_9600);
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize png loading
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags)) {
printf("SDL_image couldn't initialize! SDL_image error: %s\n", IMG_GetError());
success = false;
} else {
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load default surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] = loadSurface( "imgs/auto.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] == NULL )
{
printf( "Failed to load default image!\n" );
success = false;
}
//Load manual surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_MANUAL ] = loadSurface( "imgs/manual.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_MANUAL ] == NULL )
{
printf( "Failed to load manual image!\n" );
success = false;
}
//Load up surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ] = loadSurface( "imgs/VU.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ] == NULL )
{
printf( "Failed to load up image!\n" );
success = false;
}
//Load down surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ] = loadSurface( "imgs/VD.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ] == NULL )
{
printf( "Failed to load down image!\n" );
success = false;
}
//Load left surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] = loadSurface( "imgs/HL.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] == NULL )
{
printf( "Failed to load left image!\n" );
success = false;
}
//Load right surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ] = loadSurface( "imgs/HR.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ] == NULL )
{
printf( "Failed to load right image!\n" );
success = false;
}
//Load front surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_FRONT ] = loadSurface( "imgs/LF.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_FRONT ] == NULL )
{
printf( "Failed to load front image!\n" );
success = false;
}
//Load back surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_BACK ] = loadSurface( "imgs/LB.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_BACK ] == NULL )
{
printf( "Failed to load back image!\n" );
success = false;
}
//Load roll_left surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_ROLLL ] = loadSurface( "imgs/RL.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_ROLLL ] == NULL )
{
printf( "Failed to load roll-left image!\n" );
success = false;
}
//Load roll_right surface
gKeyPressSurfaces[ KEY_PRESS_SURFACE_ROLLR ] = loadSurface( "imgs/RR.png" );
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_ROLLR ] == NULL )
{
printf( "Failed to load roll-right image!\n" );
success = false;
}
return success;
}
void close()
{
//Deallocate surfaces
for( int i = 0; i < KEY_PRESS_SURFACE_TOTAL; ++i )
{
SDL_FreeSurface( gKeyPressSurfaces[ i ] );
gKeyPressSurfaces[ i ] = NULL;
}
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
SDL_Surface* loadSurface( std::string path )
{
// the final optimized image
SDL_Surface* optimizedSurface = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
} else {
//Convert surface to screen format
optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL);
if (optimizedSurface == NULL) {
printf("unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
return optimizedSurface;
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//Set default current surface
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//User presses a key
else if( e.type == SDL_KEYDOWN )
{
if (e.key.keysym.sym == SDLK_ESCAPE) {
manual = !manual;
if (manual)
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_MANUAL ];
else
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
serialPort << "MM" << std::endl;
} else if (manual){
//Select surfaces based on key press
switch( e.key.keysym.sym )
{
case SDLK_UP:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ];
serialPort << "VU" << std::endl;
break;
case SDLK_DOWN:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ];
serialPort << "VD" << std::endl;
break;
case SDLK_LEFT:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ];
serialPort << "HL" << std::endl;
break;
case SDLK_RIGHT:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ];
serialPort << "HR" << std::endl;
break;
case SDLK_a:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_ROLLL ];
serialPort << "RL" << std::endl;
break;
case SDLK_d:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_ROLLR ];
serialPort << "RR" << std::endl;
break;
case SDLK_w:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_FRONT ];
serialPort << "LF" << std::endl;
break;
case SDLK_s:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_BACK ];
serialPort << "LB" << std::endl;
break;
default:
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
serialPort << "OT" << std::endl;
break;
}
}
}
}
//Apply the current image stretched
SDL_Rect stretchRect;
stretchRect.x = 0;
stretchRect.y = 0;
stretchRect.w = SCREEN_WIDTH;
stretchRect.h = SCREEN_HEIGHT;
SDL_BlitScaled( gCurrentSurface, NULL, gScreenSurface, &stretchRect );
//SDL_BlitSurface( gCurrentSurface, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
}
}
}
//Free resources and close SDL
close();
serialPort.Close();
return 0;
}