-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdisplay.cpp
More file actions
602 lines (526 loc) · 14.6 KB
/
display.cpp
File metadata and controls
602 lines (526 loc) · 14.6 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <string>
#include <cmath>
#include "LTexture.h"
///////use for socket
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <sys/time.h>
using namespace std;
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//use for socket
void socket_server();
int recvtimeout(int s, char *buf, int len, int timeout);
//check if mouse in rectangle
bool checkmousepos(SDL_Rect &Rect);
//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;
//Begin image
SDL_Surface* gSurface = NULL;
//Start button image
SDL_Surface* gbuttonSurface = NULL;
//Running image
SDL_Surface* grunningSurface = NULL;
SDL_Surface* grunningSurface2 = NULL;
//sitting image
SDL_Surface* gsittingSurface = NULL;
SDL_Surface* gsittingSurface2 = NULL;
//lying image
SDL_Surface* glyingSurface = NULL;
SDL_Surface* glyingSurface2 = NULL;
//stand image
SDL_Surface* gstandSurface = NULL;
SDL_Surface* gstandSurface2 = NULL;
//walk image
SDL_Surface* gwalkSurface = NULL;
SDL_Surface* gwalkSurface2 = NULL;
SDL_Surface* gbikingSurface = NULL;
SDL_Surface* gbikingSurface2 = NULL;
SDL_Surface* gnoSurface = NULL;
SDL_Surface* gnoSurface2 = NULL;
SDL_Surface* gjogSurface = NULL;
SDL_Surface* gjogSurface2 = NULL;
SDL_Surface* gstationSurface = NULL;
SDL_Surface* gstationSurface2 = NULL;
SDL_Surface* gTurnOverSurface = NULL;
SDL_Surface* gsleepSurface = NULL;
//exit image
SDL_Surface* gexitSurface = NULL;
//Rendered texture
LTexture gTextTexture;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "Warning: Linear texture filtering not enabled!" );
}
//Create window
gWindow = SDL_CreateWindow( "Recorder", 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
{
//Create vsynced renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
success = false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
}
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load stretching surface
gSurface = loadSurface( "pic/scene.bmp" );
if( gSurface == NULL )
{
printf( "Failed to load scene image!\n" );
success = false;
}
gbuttonSurface = loadSurface( "pic/start_icon.bmp" );
if( gbuttonSurface == NULL )
{
printf( "Failed to load start_icon image!\n" );
success = false;
}
gTurnOverSurface = loadSurface( "pic/turn_over.bmp" );
if( gTurnOverSurface == NULL )
{
printf( "Failed to turn over image!\n" );
success = false;
}
gsleepSurface = loadSurface( "pic/sleep.bmp" );
if( gsleepSurface == NULL )
{
printf( "Failed to load sleep image!\n" );
success = false;
}
gstationSurface = loadSurface( "pic/moving/station.bmp" );
if( gstationSurface == NULL )
{
printf( "Failed to load station image!\n" );
success = false;
}
grunningSurface = loadSurface( "pic/moving/run.bmp" );
if( grunningSurface == NULL )
{
printf( "Failed to load running image!\n" );
success = false;
}
gnoSurface = loadSurface( "pic/moving/no.bmp" );
if( gnoSurface == NULL )
{
printf( "Failed to load no image!\n" );
success = false;
}
gsittingSurface = loadSurface( "pic/moving/sit.bmp" );
if( gsittingSurface == NULL )
{
printf( "Failed to load sitting image!\n" );
success = false;
}
glyingSurface = loadSurface( "pic/moving/lying.bmp" );
if( glyingSurface == NULL )
{
printf( "Failed to load lying image!\n" );
success = false;
}
gstandSurface = loadSurface( "pic/moving/stand.bmp" );
if( gstandSurface == NULL )
{
printf( "Failed to load stand image!\n" );
success = false;
}
gwalkSurface = loadSurface( "pic/moving/walk.bmp" );
if( gwalkSurface == NULL )
{
printf( "Failed to load walk image!\n" );
success = false;
}
gbikingSurface = loadSurface( "pic/moving/biking.bmp" );
if( gbikingSurface == NULL )
{
printf( "Failed to load biking image!\n" );
success = false;
}
gjogSurface = loadSurface( "pic/moving/jogging.bmp" );
if( gjogSurface == NULL )
{
printf( "Failed to load jogging image!\n" );
success = false;
}
gbikingSurface2 = loadSurface( "pic/nosleep/biking.bmp" );
if( gbikingSurface2 == NULL )
{
printf( "Failed to load biking image!\n" );
success = false;
}
gstationSurface2 = loadSurface( "pic/nosleep/station.bmp" );
if( gstationSurface2 == NULL )
{
printf( "Failed to load station image!\n" );
success = false;
}
grunningSurface2 = loadSurface( "pic/nosleep/run.bmp" );
if( grunningSurface2 == NULL )
{
printf( "Failed to load running image!\n" );
success = false;
}
gnoSurface2 = loadSurface( "pic/nosleep/no.bmp" );
if( gnoSurface2 == NULL )
{
printf( "Failed to load running image!\n" );
success = false;
}
gsittingSurface2 = loadSurface( "pic/nosleep/sit.bmp" );
if( gsittingSurface2 == NULL )
{
printf( "Failed to load sitting_long image!\n" );
success = false;
}
glyingSurface2 = loadSurface( "pic/nosleep/lying.bmp" );
if( glyingSurface2 == NULL )
{
printf( "Failed to load lying image!\n" );
success = false;
}
gstandSurface2 = loadSurface( "pic/nosleep/stand.bmp" );
if( gstandSurface2 == NULL )
{
printf( "Failed to load stand image!\n" );
success = false;
}
gwalkSurface2 = loadSurface( "pic/nosleep/walk.bmp" );
if( gwalkSurface2 == NULL )
{
printf( "Failed to load walk image!\n" );
success = false;
}
gjogSurface2 = loadSurface( "pic/nosleep/jogging.bmp" );
if( gjogSurface2 == NULL )
{
printf( "Failed to load jog image!\n" );
success = false;
}
gexitSurface = loadSurface( "pic/exit.bmp" );
if( gexitSurface == NULL )
{
printf( "Failed to load exit image!\n" );
success = false;
}
return success;
}
void close()
{
//Free loaded image
SDL_FreeSurface( gSurface );
gSurface = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
SDL_Surface* loadSurface( string path )
{
//The final optimized image
SDL_Surface* optimizedSurface = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = SDL_LoadBMP( 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, 0 );
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 recvtimeout(int sockfd, char *buf, int len, int timeout)
{
fd_set fds;
int n;
struct timeval tv;
// file descriptor set
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
// set timeout struct timeval
tv.tv_sec = timeout;
tv.tv_usec = 0;
// wait until timeout or recieve data
n = select(sockfd+1, &fds, NULL, NULL, &tv);
// time out
if (n == 0)
{
cout<<"time out"<<endl;
return -2;
}
// error
if (n == -1) return -1;
return recv(sockfd, buf, len, 0);
}
void socket_server()
{
SDL_Rect stretchRect;
stretchRect.x = 0;
stretchRect.y = 0;
stretchRect.w = SCREEN_WIDTH;
stretchRect.h = SCREEN_HEIGHT;
SDL_Rect exitRect;
exitRect.x = SCREEN_WIDTH*5/6;
exitRect.y = SCREEN_HEIGHT*3/4;
exitRect.w = SCREEN_WIDTH/6;
exitRect.h = SCREEN_HEIGHT/4;
SDL_BlitScaled( gSurface, NULL, gScreenSurface, &stretchRect );
SDL_BlitScaled( gexitSurface, NULL, gScreenSurface, &exitRect );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
//socket
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1024];
char recvBuff[1024];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(65431);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
int result=0;
string strtime;
strcpy(sendBuff, "Hello");
bool exit = false;
while(!exit)
{
SDL_Event e_2;
bool mouse = false;
string msg="";
bool IsTurnOver=false;
while (1) {
connfd=accept(listenfd, (struct sockaddr*)NULL, NULL);
result=0;
while(1) {
ticks=time(NULL);
if(0< (result=read(connfd,recvBuff, sizeof(recvBuff)-1))) {
strtime=ctime(&ticks);
strtime.erase(strtime.size()-1);
cout<<strtime<<": ";
recvBuff[result]='\0';
cout<<recvBuff<<endl;
msg=recvBuff;
if(msg == "no activity")
SDL_BlitScaled( gnoSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "stationary")
SDL_BlitScaled( gstationSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "standing")
SDL_BlitScaled( gstandSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "sitting")
SDL_BlitScaled( gsittingSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "lying")
SDL_BlitScaled( glyingSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "walking")
SDL_BlitScaled( gwalkSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "fast walking")
SDL_BlitScaled( grunningSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "jogging")
SDL_BlitScaled( gjogSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "biking")
SDL_BlitScaled( gbikingSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "turn over"){
SDL_BlitScaled( gTurnOverSurface, NULL, gScreenSurface, &stretchRect );
IsTurnOver=true;
}
else if(msg == "sleeping")
SDL_BlitScaled( gsleepSurface, NULL, gScreenSurface, &stretchRect );
else if(msg == "no sleeping, no activity")
SDL_BlitScaled( gnoSurface2, NULL, gScreenSurface, &stretchRect );
else if(msg == "no sleeping, stationary") {
SDL_BlitScaled( gstationSurface2, NULL, gScreenSurface, &stretchRect );
}
else if(msg == "no sleeping, standing")
SDL_BlitScaled( gstandSurface2, NULL, gScreenSurface, &stretchRect );
else if(msg == "no sleeping, sitting")
SDL_BlitScaled( gsittingSurface2, NULL, gScreenSurface, &stretchRect );
else if(msg == "no sleeping, lying")
SDL_BlitScaled( glyingSurface2, NULL, gScreenSurface, &stretchRect );
else if(msg == "no sleeping, walking")
SDL_BlitScaled( gwalkSurface2, NULL, gScreenSurface, &stretchRect );
else if(msg == "no sleeping, fast walking")
SDL_BlitScaled( grunningSurface2, NULL, gScreenSurface, &stretchRect );
else if(msg == "no sleeping, jogging")
SDL_BlitScaled( gjogSurface2, NULL, gScreenSurface, &stretchRect );
else if(msg == "no sleeping, biking")
SDL_BlitScaled( gbikingSurface2, NULL, gScreenSurface, &stretchRect );
SDL_UpdateWindowSurface( gWindow );
if (IsTurnOver){
sleep(1);
IsTurnOver=false;
}
write(connfd, sendBuff, strlen(sendBuff));
}
}
}
//Update the surface
SDL_UpdateWindowSurface( gWindow );
}
//cout<<receiveMessage<<endl;
cout<<"close Socket"<<endl;
close(connfd);
return ;
}
//set the start view
void reset_screen(SDL_Rect &buttonRect)
{
//Apply the image stretched
SDL_Rect stretchRect;
stretchRect.x = 0;
stretchRect.y = 0;
stretchRect.w = SCREEN_WIDTH;
stretchRect.h = SCREEN_HEIGHT;
SDL_BlitScaled( gSurface, NULL, gScreenSurface, &stretchRect );
SDL_BlitScaled( gbuttonSurface, NULL, gScreenSurface, &buttonRect );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
}
bool checkmousepos(SDL_Rect &Rect)
{
cout<<"checkmouse"<<endl;
int x,y;
SDL_GetMouseState( &x, &y );
cout<<"x = "<<x<<",y ="<<y<<endl;
if( x>Rect.x && x<Rect.x+Rect.w && y>Rect.y && y<Rect.y+Rect.h )
return false;
else return true;
}
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;
bool mouse = false;
//Event handler
SDL_Event e;
//Start button view point
SDL_Rect buttonRect;
buttonRect.x = SCREEN_WIDTH*2/5;
buttonRect.y = SCREEN_HEIGHT*2/3;
buttonRect.w = SCREEN_WIDTH/5;
buttonRect.h = SCREEN_HEIGHT/6;
reset_screen(buttonRect);
//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;
}
else if( e.type == SDL_MOUSEBUTTONDOWN )
{
mouse = true;
}
else if( e.type == SDL_MOUSEBUTTONUP && mouse )
{
if( !checkmousepos(buttonRect))
{
socket_server();
reset_screen(buttonRect);
}
mouse = false;
}
}
}
}
}
//Free resources and close SDL
close();
return 0;
}