-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSimpleSimulator.cpp
426 lines (369 loc) · 12.5 KB
/
SimpleSimulator.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
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
/*
TUIO C++ Server Demo
Copyright (c) 2005-2016 Martin Kaltenbrunner <martin@tuio.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "SimpleSimulator.h"
void SimpleSimulator::drawFrame() {
if(!running) return;
glClear(GL_COLOR_BUFFER_BIT);
char id[3];
// draw the cursors
std::list<TuioCursor*> cursorList = tuioServer->getTuioCursors();
for (std::list<TuioCursor*>::iterator iter = cursorList.begin(); iter!=cursorList.end(); iter++) {
TuioCursor *tcur = (*iter);
std::list<TuioPoint> path = tcur->getPath();
if (path.size()>0) {
TuioPoint last_point = path.front();
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 1.0);
for (std::list<TuioPoint>::iterator point = path.begin(); point!=path.end(); point++) {
glVertex3f(last_point.getScreenX(width), last_point.getScreenY(height), 0.0f);
glVertex3f(point->getScreenX(width), point->getScreenY(height), 0.0f);
last_point.update(point->getX(),point->getY());
}
glEnd();
// draw the finger tip
glColor3f(0.75, 0.75, 0.75);
std::list<TuioCursor*>::iterator joint = std::find( jointCursorList.begin(), jointCursorList.end(), tcur );
if( joint != jointCursorList.end() ) {
glColor3f(0.5, 0.5, 0.5);
}
glPushMatrix();
glTranslatef(last_point.getScreenX(width), last_point.getScreenY(height), 0.0f);
glBegin(GL_TRIANGLE_FAN);
for(double a = 0.0f; a <= 2*M_PI; a += 0.2f) {
glVertex2d(cos(a) * height/100.0f, sin(a) * height/100.0f);
}
glEnd();
glPopMatrix();
glColor3f(0.0, 0.0, 0.0);
glRasterPos2f(tcur->getScreenX(width),tcur->getScreenY(height));
sprintf(id,"%d",tcur->getCursorID());
drawString(id);
}
}
if (help) {
glColor3f(0.0, 0.0, 1.0);
glRasterPos2f(10,20);
drawString("h - toggle help display");
glRasterPos2f(10,35);
if (verbose) drawString("v - disable verbose mode");
else drawString("v - enable verbose mode");
glRasterPos2f(10,50);
drawString("r - reset session");
glRasterPos2f(10,65);
if (tuioServer->fullUpdateEnabled()) drawString("f - disable full update");
else drawString("f - enable full update");
glRasterPos2f(10,80);
if (tuioServer->periodicMessagesEnabled()) drawString("p - disable periodic messages");
else drawString("p - enable periodic messages");
glRasterPos2f(10,95);
drawString("SHIFT click - create persistent cursor");
glRasterPos2f(10,110);
drawString("CTRL click - add to cursor group");
glRasterPos2f(10,125);
if (fullscreen) drawString("F1 - exit fullscreen mode");
else drawString("F1 - enter fullscreen mode");
glRasterPos2f(10,140);
drawString("ESC - Quit");
}
SDL_GL_SwapWindow(window);
}
void SimpleSimulator::drawString(const char *str) {
if (str && strlen(str)) {
while (*str) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *str);
str++;
}
}
}
void SimpleSimulator::processEvents()
{
SDL_Event event;
while( SDL_PollEvent( &event ) ) {
switch( event.type ) {
case SDL_KEYDOWN:
if (event.key.repeat) continue;
else if( event.key.keysym.sym == SDLK_ESCAPE ){
running = false;
SDL_Quit();
} else if( event.key.keysym.sym == SDLK_F1 ){
if(fullscreen)
{
width = window_width;
height = window_height;
SDL_SetWindowFullscreen(window, SDL_FALSE);
fullscreen = false;
}
else
{
width = screen_width;
height = screen_height;
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
fullscreen = true;
}
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, (GLfloat)width, (GLfloat)height, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
} else if( event.key.keysym.sym == SDLK_f ){
fullupdate=!tuioServer->fullUpdateEnabled();
if (fullupdate) tuioServer->enableFullUpdate();
else tuioServer->disableFullUpdate();
} else if( event.key.keysym.sym == SDLK_p ){
periodic=!tuioServer->periodicMessagesEnabled();
if (periodic) tuioServer->enablePeriodicMessages();
else tuioServer->disablePeriodicMessages();
} else if( event.key.keysym.sym == SDLK_v ){
verbose = !verbose;
tuioServer->setVerbose(verbose);
} else if( event.key.keysym.sym == SDLK_h ){
help = !help;
} else if( event.key.keysym.sym == SDLK_r ){
tuioServer->resetTuioCursors();
stickyCursorList.clear();
jointCursorList.clear();
activeCursorList.clear();
}
break;
case SDL_MOUSEMOTION:
if (event.button.button == SDL_BUTTON_LEFT) mouseDragged((float)event.button.x/width, (float)event.button.y/height);
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) mousePressed((float)event.button.x/width, (float)event.button.y/height);
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT) mouseReleased((float)event.button.x/width, (float)event.button.y/height);
break;
case SDL_QUIT:
running = false;
SDL_ShowCursor(true);
SDL_Quit();
break;
}
}
}
void SimpleSimulator::mousePressed(float x, float y) {
//printf("pressed %f %f\n",x,y);
TuioCursor *match = NULL;
float distance = 0.01f;
for (std::list<TuioCursor*>::iterator iter = stickyCursorList.begin(); iter!=stickyCursorList.end(); iter++) {
TuioCursor *tcur = (*iter);
float test = tcur->getDistance(x,y);
if ((test < distance) && (test < 8.0f/width)) {
distance = test;
match = tcur;
}
}
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
if ((keystate[SDL_SCANCODE_LSHIFT]) || (keystate[SDL_SCANCODE_RSHIFT])) {
if (match!=NULL) {
std::list<TuioCursor*>::iterator joint = std::find( jointCursorList.begin(), jointCursorList.end(), match );
if( joint != jointCursorList.end() ) {
jointCursorList.erase( joint );
}
stickyCursorList.remove(match);
activeCursorList.remove(match);
tuioServer->removeTuioCursor(match);
} else {
TuioCursor *cursor = tuioServer->addTuioCursor(x,y);
stickyCursorList.push_back(cursor);
activeCursorList.push_back(cursor);
}
} else if ((keystate[SDL_SCANCODE_LCTRL]) || (keystate[SDL_SCANCODE_RCTRL])) {
if (match!=NULL) {
std::list<TuioCursor*>::iterator joint = std::find( jointCursorList.begin(), jointCursorList.end(), match );
if( joint != jointCursorList.end() ) {
jointCursorList.remove( match );
} else jointCursorList.push_back(match);
}
} else {
if (match==NULL) {
TuioCursor *cursor = tuioServer->addTuioCursor(x,y);
activeCursorList.push_back(cursor);
} else activeCursorList.push_back(match);
}
}
void SimpleSimulator::mouseDragged(float x, float y) {
//printf("dragged %f %f\n",x,y);
TuioCursor *cursor = NULL;
float distance = width;
for (std::list<TuioCursor*>::iterator iter = activeCursorList.begin(); iter!=activeCursorList.end(); iter++) {
TuioCursor *tcur = (*iter);
float test = tcur->getDistance(x,y);
if (test<distance) {
distance = test;
cursor = tcur;
}
}
if (cursor==NULL) return;
if (cursor->getTuioTime()==frameTime) return;
std::list<TuioCursor*>::iterator joint = std::find( jointCursorList.begin(), jointCursorList.end(), cursor );
if( joint != jointCursorList.end() ) {
float dx = x-cursor->getX();
float dy = y-cursor->getY();
for (std::list<TuioCursor*>::iterator iter = jointCursorList.begin(); iter!=jointCursorList.end(); iter++) {
TuioCursor *jointCursor = (*iter);
tuioServer->updateTuioCursor(jointCursor,jointCursor->getX()+dx,jointCursor->getY()+dy);
}
} else tuioServer->updateTuioCursor(cursor,x,y);
}
void SimpleSimulator::mouseReleased(float x, float y) {
//printf("released %f %f\n",x,y);
TuioCursor *cursor = NULL;
float distance = 0.01f;
for (std::list<TuioCursor*>::iterator iter = stickyCursorList.begin(); iter!=stickyCursorList.end(); iter++) {
TuioCursor *tcur = (*iter);
float test = tcur->getDistance(x,y);
if (test<distance) {
distance = test;
cursor = tcur;
}
}
if (cursor!=NULL) {
activeCursorList.remove(cursor);
return;
}
distance = 0.01f;
for (std::list<TuioCursor*>::iterator iter = activeCursorList.begin(); iter!=activeCursorList.end(); iter++) {
TuioCursor *tcur = (*iter);
float test = tcur->getDistance(x,y);
if (test<distance) {
distance = test;
cursor = tcur;
}
}
if (cursor!=NULL) {
activeCursorList.remove(cursor);
tuioServer->removeTuioCursor(cursor);
}
}
SimpleSimulator::SimpleSimulator(TuioServer *server)
: verbose (false)
, fullupdate (false)
, periodic (false)
, fullscreen (false)
, help (false)
, screen_width (1024)
, screen_height (768)
, window_width (640)
, window_height (480)
{
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
std::cerr << "SDL could not be initialized: " << SDL_GetError() << std::endl;
SDL_Quit();
exit(1);
}
TuioTime::initSession();
frameTime = TuioTime::getSessionTime();
tuioServer = server;
tuioServer->setSourceName("SimpleSimulator");
tuioServer->enableObjectProfile(false);
tuioServer->enableBlobProfile(false);
initWindow();
SDL_SetWindowTitle(window,"SimpleSimulator");
}
void SimpleSimulator::initWindow() {
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(0, &mode);
screen_width = mode.w;
screen_height= mode.h;
int videoFlags = SDL_WINDOW_OPENGL;
if( fullscreen ) {
videoFlags |= SDL_WINDOW_FULLSCREEN;
width = screen_width;
height = screen_height;
} else {
width = window_width;
height = window_height;
}
SDL_CreateWindowAndRenderer(width, height, videoFlags, &window, &renderer);
if ( window == NULL ) {
std::cerr << "Could not open window: " << SDL_GetError() << std::endl;
SDL_Quit();
}
SDL_GLContext context = SDL_GL_CreateContext(window);
if (!context) {
fprintf(stderr, "Couldn't create context: %s\n", SDL_GetError());
return;
}
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, (float)width, (float)height, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void SimpleSimulator::run() {
running=true;
while (running) {
frameTime = TuioTime::getSessionTime();
tuioServer->initFrame(frameTime);
processEvents();
tuioServer->stopUntouchedMovingCursors();
tuioServer->commitFrame();
drawFrame();
// simulate 50Hz compensating the previous processing time
int delay = 20 - (TuioTime::getSessionTime().getTotalMilliseconds() - frameTime.getTotalMilliseconds());
if (delay>0) SDL_Delay(delay);
}
}
int main(int argc, char* argv[])
{
/* if (( argc != 1) && ( argc != 3)) {
std::cout << "usage: SimpleSimulator [host] [port]\n";
return 0;
}*/
#ifndef __MACOSX__
glutInit(&argc,argv);
#else
if ((argc>1) && ((std::string(argv[1]).find("-NSDocumentRevisionsDebugMode")==0 ) || (std::string(argv[1]).find("-psn_")==0))) argc = 1;
#endif
TuioServer *server = NULL;
if( argc == 3 ) {
server = new TuioServer(argv[1],atoi(argv[2]));
} else server = new TuioServer(); // default is UDP port 3333 on localhost
// add an additional TUIO/TCP sender
OscSender *tcp_sender = NULL;
if( argc == 2 ) {
try { tcp_sender = new TcpSender(atoi(argv[1])); }
catch (std::exception e) { tcp_sender = NULL; }
} else if ( argc == 3 ) {
try { tcp_sender = new TcpSender(argv[1],atoi(argv[2])); }
catch (std::exception e) { tcp_sender = NULL; }
} else {
try { tcp_sender = new TcpSender(3333); }
catch (std::exception e) { tcp_sender = NULL; }
}
if (tcp_sender) server->addOscSender(tcp_sender);
// add an additional TUIO/WEB sender
OscSender *web_sender = NULL;
try { web_sender = new WebSockSender(8080); }
catch (std::exception e) { web_sender = NULL; }
if (web_sender) server->addOscSender(web_sender);
// add an additional TUIO/FLC sender
OscSender *flash_sender = NULL;
try { flash_sender = new FlashSender(); }
catch (std::exception e) { flash_sender = NULL; }
if (flash_sender) server->addOscSender(flash_sender);
SimpleSimulator *app = new SimpleSimulator(server);
app->run();
delete app;
delete server;
return 0;
}