root / TUIO_CPP / SimpleSimulator.cpp @ 91

View | Annotate | Download (7 KB)

1
/*
2
        TUIO C++ Server Demo - part of the reacTIVision project
3
        http://reactivision.sourceforge.net/
4
5
        Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu>
6
7
    This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 2 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
*/
21
22
#include "SimpleSimulator.h"
23
void SimpleSimulator::drawFrame() {
24
        
25
        if(!running) return;
26
        glClear(GL_COLOR_BUFFER_BIT);
27
        char id[3];
28
        
29
        // draw the cursors
30
        std::list<TuioCursor*> cursorList = tuioServer->getTuioCursors();
31
        for (std::list<TuioCursor*>::iterator tuioCursor = cursorList.begin(); tuioCursor!=cursorList.end(); tuioCursor++) {
32
                std::list<TuioPoint> path = (*tuioCursor)->getPath();
33
                if (path.size()>0) {
34
                        
35
                        TuioPoint last_point = path.front();
36
                        glBegin(GL_LINES);
37
                        glColor3f(0.0, 0.0, 1.0);
38
                        
39
                        for (std::list<TuioPoint>::iterator point = path.begin(); point!=path.end(); point++) {
40
                                glVertex3f(last_point.getX()*width, last_point.getY()*height, 0.0f);
41
                                glVertex3f(point->getX()*width, point->getY()*height, 0.0f);
42
                                last_point.update(point->getX(),point->getY());
43
                        }
44
                        glEnd();
45
                        
46
                        // draw the finger tip
47
                        glColor3f(0.75, 0.75, 0.75);
48
                        glPushMatrix();
49
                        glTranslatef(last_point.getX()*width, last_point.getY()*height, 0.0);
50
                        glBegin(GL_TRIANGLE_FAN);
51
                        for(double a = 0.0f; a <= 2*M_PI; a += 0.2f) {
52
                                glVertex2d(cos(a) * height/100.0f, sin(a) * height/100.0f);
53
                        }
54
                        glEnd();
55
                        glPopMatrix();
56
                        
57
                        glColor3f(0.0, 0.0, 0.0);
58
                        glRasterPos2f((*tuioCursor)->getScreenX(width),(*tuioCursor)->getScreenY(height));
59
                        sprintf(id,"%d",(*tuioCursor)->getCursorID());
60
                        drawString(id);
61
                }
62
        }
63
        
64
        SDL_GL_SwapBuffers();
65
}
66
67
void SimpleSimulator::drawString(char *str) {
68
        if (str && strlen(str)) {
69
                while (*str) {
70
                        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str);
71
                        str++;
72
                }
73
        }
74
}
75
76
void SimpleSimulator::toggleFullscreen() {
77
78
        if( !window || SDL_WM_ToggleFullScreen(window)!=1 ) {
79
                std::cout << "Unable to toggle fullscreen: " << SDL_GetError() << std::endl;
80
        } else {
81
                fullscreen = !fullscreen;
82
                SDL_ShowCursor(!fullscreen);
83
        }
84
}
85
86
void SimpleSimulator::processEvents()
87
{
88
    SDL_Event event;
89
90
    while( SDL_PollEvent( &event ) ) {
91
92
        switch( event.type ) {
93
                case SDL_KEYDOWN:
94
                        if( event.key.keysym.sym == SDLK_ESCAPE ){
95
                                running = false;
96
                                SDL_ShowCursor(true);
97
                                SDL_Quit();
98
                        } else if( event.key.keysym.sym == SDLK_F1 ){
99
                                toggleFullscreen();
100
                        } else if( event.key.keysym.sym == SDLK_v ){
101
                                verbose = !verbose;        
102
                                tuioServer->setVerbose(verbose);
103
                        } 
104
                        break;
105
                                
106
                case SDL_MOUSEMOTION:
107
                        if (event.button.button & SDL_BUTTON(1)) mouseDragged((float)event.button.x/width, (float)event.button.y/height);
108
                        break;
109
                case SDL_MOUSEBUTTONDOWN:
110
                        if (event.button.button & SDL_BUTTON(1)) mousePressed((float)event.button.x/width, (float)event.button.y/height);
111
                        break;
112
                case SDL_MOUSEBUTTONUP:
113
                        if (event.button.button & SDL_BUTTON(1)) mouseReleased((float)event.button.x/width, (float)event.button.y/height);
114
                        break;
115
                case SDL_QUIT:
116
                        running = false;
117
                        SDL_ShowCursor(true);
118
                        SDL_Quit();
119
                        break;
120
        }
121
    }
122
}
123
124
void SimpleSimulator::mousePressed(float x, float y) {
125
        //printf("clicked %i %i\n",x,y);
126
        
127
        TuioCursor *match = NULL;
128
        for (std::list<TuioCursor*>::iterator tuioCursor = stickyCursorList.begin(); tuioCursor!=stickyCursorList.end(); tuioCursor++) {
129
                if ((*tuioCursor)->getDistance(x,y) < (5.0f/width)) {
130
                        match = (*tuioCursor);
131
                        break;
132
                }
133
        }
134
        
135
        Uint8 *keystate = SDL_GetKeyState(NULL);
136
        if ((keystate[SDLK_LSHIFT]) || (keystate[SDLK_RSHIFT]))  {
137
                        
138
                if (match!=NULL) {
139
                        stickyCursorList.remove(match);
140
                        tuioServer->removeTuioCursor(match);
141
                        cursor = NULL;
142
                } else {
143
                        cursor = tuioServer->addTuioCursor(x,y);
144
                        stickyCursorList.push_back(cursor);
145
                }
146
        } else {
147
                if (match!=NULL) {
148
                        cursor=match;
149
                } else {
150
                        cursor = tuioServer->addTuioCursor(x,y);
151
                }
152
        }        
153
}
154
155
void SimpleSimulator::mouseDragged(float x, float y) {
156
        //printf("dragged %i %i\n",x,y);
157
        if (cursor==NULL) return;
158
        if (cursor->getTuioTime()==currentTime) return;
159
        tuioServer->updateTuioCursor(cursor,x,y);
160
}
161
162
void SimpleSimulator::mouseReleased(float x, float y) {
163
        //printf("released\n");
164
        if (cursor==NULL) return;
165
        
166
        TuioCursor *match = NULL;
167
        std::list<TuioCursor*> cursorList = tuioServer->getTuioCursors();
168
        for (std::list<TuioCursor*>::iterator tuioCursor = stickyCursorList.begin(); tuioCursor!=stickyCursorList.end(); tuioCursor++) {
169
                if ((*tuioCursor)->getDistance(x,y) < (5.0f/width)) {
170
                        match = (*tuioCursor);
171
                        break;
172
                }
173
        }
174
        
175
        if (match==NULL) {
176
                tuioServer->removeTuioCursor(cursor);
177
        }
178
        cursor = NULL;
179
}
180
181
SimpleSimulator::SimpleSimulator(char* host, int port) {
182
183
        verbose = false;
184
        fullscreen = false;
185
        window_width = 640;
186
        window_height = 480;
187
        screen_width = 1024;
188
        screen_height = 768;
189
        
190
        cursor = NULL;
191
        if ((strcmp(host,"default")==0) && (port==0)) tuioServer = new TuioServer();
192
        else tuioServer = new TuioServer(host, port);
193
        currentTime = TuioTime::getSessionTime();
194
        
195
        //tuioServer->enablePeriodicMessages();
196
197
        if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
198
                std::cerr << "SDL could not be initialized: " <<  SDL_GetError() << std::endl;
199
                SDL_Quit();
200
                exit(1);
201
        }
202
        
203
        int videoFlags = SDL_OPENGL | SDL_DOUBLEBUF;
204
        if( fullscreen ) {
205
                videoFlags |= SDL_FULLSCREEN;
206
                SDL_ShowCursor(false);
207
                width = screen_width;
208
                height = screen_height;
209
        } else {
210
                width = window_width;
211
                height = window_height;
212
        }
213
        
214
        window = SDL_SetVideoMode(width, height, 32, videoFlags);
215
        if ( window == NULL ) {
216
                std::cerr << "Could not open window: " << SDL_GetError() << std::endl;
217
                SDL_Quit();
218
                exit(1);
219
        }
220
221
        glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
222
        glViewport(0, 0, width, height);
223
        glMatrixMode(GL_PROJECTION);        
224
        glLoadIdentity();
225
        gluOrtho2D(0, (float)width,  (float)height, 0);
226
        glMatrixMode(GL_MODELVIEW);
227
        glLoadIdentity();
228
        SDL_WM_SetCaption("SimpleSimulator", NULL);
229
}
230
231
void SimpleSimulator::run() {
232
        running=true;
233
        while (running) {
234
                currentTime = TuioTime::getSessionTime();
235
                tuioServer->initFrame(currentTime);
236
                processEvents();
237
                tuioServer->stopUntouchedMovingCursors();
238
                tuioServer->commitFrame();
239
                drawFrame();
240
                SDL_Delay(20);
241
        } 
242
}
243
244
int main(int argc, char* argv[])
245
{
246
        if (( argc != 1) && ( argc != 3)) {
247
                std::cout << "usage: SimpleSimulator [host] [port]\n";
248
                return 0;
249
        }
250
251
#ifndef __MACOSX__
252
        glutInit(&argc,argv);
253
#endif
254
        
255
        SimpleSimulator *app;
256
        if( argc == 3 ) {
257
                app = new SimpleSimulator(argv[1],atoi(argv[2]));
258
        } else app = new SimpleSimulator("default",0);
259
        
260
        app->run();
261
        delete(app);
262
263
        return 0;
264
}
265
266