forked from tweakoz/twilight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·364 lines (288 loc) · 8.05 KB
/
main.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
///////////////////////////////////////////////////////////////////////////////
// RTPS
// Copyright 1996-2013, Michael T. Mayers
// Provided under the MIT License (see LICENSE.txt)
///////////////////////////////////////////////////////////////////////////////
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include "gl.h"
using namespace std;
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void iterate( void );
void windowreshape( GLsizei w, GLsizei h );
void menuhandler( int value );
void mousehandler( int button, int state, int x, int y );
void keyboardhandler( unsigned char key, int x, int y );
void motionhandler(int x, int y);
void drawfunc( void );
GLsizei gw, gh;
int mainwinid;
#if !defined(OSX)
#include <GL/glx.h>
#include <stdio.h>
#else
#import <Cocoa/Cocoa.h>
@interface MyOpenGLView : NSOpenGLView
@end
@implementation MyOpenGLView
-(void) drawRect: (NSRect) bounds
{
drawfunc();
}
@end
#endif
///////////////////////////////////////////////////////////////////////////
int main( int argc, char * argv[] )
{
#if 0
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 640, 480 );
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA /*| GLUT_DOUBLE*/ );
mainwinid = glutCreateWindow("Twilight 2013, courtesy of TweakoZ");
glutDisplayFunc(drawfunc);
glutReshapeFunc(windowreshape);
glutIdleFunc( iterate );
glutCreateMenu(menuhandler);
glutAddMenuEntry("FullScreen", 1);
glutAddMenuEntry("Windowed", 2);
glutAddMenuEntry("ResetPosition", 3);
glutAddMenuEntry("Quit", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMouseFunc( mousehandler );
glutKeyboardFunc( keyboardhandler );
glutMotionFunc( motionhandler );
glutMainLoop(); //glutMainLoopEvent();
#elif !defined(OSX)
Display *dpy = XOpenDisplay(getenv("DISPLAY"));
Window win;
if (argc == 2) {
win = strtoul(argv[1], NULL, 0);
} else {
printf("Drawing to root window. If you don't see background, provide ID of window to which we should draw as command line argument.\n");
win = DefaultRootWindow(dpy);
}
GLint att[] = {GLX_RGBA, None};
XVisualInfo *vi = glXChooseVisual(dpy, 0, att);
XWindowAttributes gwa;
XSetWindowAttributes swa;
XEvent xev;
swa.event_mask = ExposureMask;
XChangeWindowAttributes(dpy, win, CWEventMask, &swa);
glXMakeCurrent(dpy, win, glXCreateContext(dpy, vi, NULL, GL_TRUE));
while(1) {
XNextEvent(dpy, &xev);
if (xev.type == Expose) {
XGetWindowAttributes(dpy, win, &gwa);
gw = gwa.width;
gh = gwa.height;
drawfunc();
}
}
#else
NSApplication *app = [[NSApplication alloc] init];
NSRect mainDisplayRect = [[NSScreen mainScreen] frame];
NSWindow *fullScreenWindow = [[NSWindow alloc] initWithContentRect: mainDisplayRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
gw = mainDisplayRect.size.width;
gh = mainDisplayRect.size.height;
[fullScreenWindow setLevel:kCGDesktopWindowLevel];
[fullScreenWindow setContentView:[[MyOpenGLView alloc] init]];
[fullScreenWindow makeKeyAndOrderFront:nil];
[app run];
#endif
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void iterate( void )
{
drawfunc();
usleep(500000);
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
void windowreshape( GLsizei w, GLsizei h )
{
gw = w;
gh = h;
glViewport( 0, 0, gw, gh );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// gluPerspective( 40.0f, ((F32)gw/(F32)gh), 0.1f, 100.0f );
// gluLookAt( eyex, eyey, eyez,
// cenx, ceny, cenz,
// 0.0f, 1.0f, 0.0f );
//
// glTranslatef( 0.0f, 0.0f, 0.0f );
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
void menuhandler( int value )
{
switch (value)
{ case 1: // Go Full Screen
{ glutFullScreen();
break;
}
case 2: // Go Windowed
{ glutSetWindow( mainwinid );
glutReshapeWindow( 640, 480 );
glutPositionWindow( 0, 0 );
break;
}
case 4: // Quit
{ exit(0);
break;
}
default:
break;
}
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
void mousehandler(int button, int state, int x, int y)
{ /*if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{ movingxz = 1;
movingy = 0;
beginx = x;
beginy = y; //if(glutGetModifiers() & GLUT_ACTIVE_SHIFT)
}
//int glutGetModifiers(void)
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{ movingxz = 0;
}
if ( (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) || ((button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)&&glutGetModifiers()==GLUT_ACTIVE_SHIFT) )
{ movingy = 1;
movingxz = 0;
beginx = x;
beginy = y;
}
if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP)
{ movingy = 0;
}*/
}
///////////////////////////////////////////////////////////////
void motionhandler(int x, int y)
{ /*extern int movingxz;
extern int movingy;
extern int beginx;
extern int beginy;
extern F32 eyex;
extern F32 eyey;
extern F32 eyez;
extern F32 h, p, zoom;
extern U32 gmassn;
int deltax;
int deltay;
deltax = beginx - x;
deltay = beginy - y;
if( movingxz == 1 )
{ h += (F32) (deltax/4800.0f);
p += (F32) (deltay/4800.0f);
}
else if( movingy == 1 )
{ zoom += (F32) (deltay/2400.0f);
}
eyex = zoom * sinf( h * PI2 * DEG2RAD );
eyey = zoom * sinf( p * PI2 * DEG2RAD );
eyez = zoom * cosf( h * PI2 * DEG2RAD );*/
}
///////////////////////////////////////////////////////////////
void keyboardhandler( unsigned char key, int x, int y )
{ /*static F32 saveval;
int dx, dy;
dx = x;
dy = y;
switch( key )
{
case ' ':
re_init();
break;
case '/':
gvmass[gmassn] /= 10.0f;
break;
case '*':
gvmass[gmassn] *= 10.0f;
break;
case ']':
gpointsize *= 1.25f;
break;
case '[':
gpointsize /= 1.25f;
break;
case '}':
NPAR *= 2;
re_init();
break;
case '{':
NPAR /= 2;
re_init();
break;
case '-':
gvmass[gmassn] /= 1.1f;
break;
case '+':
gvmass[gmassn] *= 1.1f;
break;
case 's':
saveval = gvmass[gmassn];
break;
case 'l':
gvmass[gmassn] = saveval;
break;
case '0':
gvmass[gmassn] = 1.0f;
break;
case 'a':
attreptoggle[gmassn] = 1.0f;
break;
case 'r':
attreptoggle[gmassn] = -1.0f;
break;
case 'd':
attached[gmassn] = false;
break;
case 'D':
for( U32 di=0; di<4; di++ ) // detach others
{ if( di != gmassn ) attached[di] = false;
}
attached[gmassn] = true;
break;
case '1':
gmassn = 0;
break;
case '2':
gmassn = 1;
break;
case '3':
gmassn = 2;
break;
case '4':
gmassn = 3;
break;
case 'i':
if( showtext==FALSE ) showtext=TRUE;
else( showtext = FALSE );
break;
case 'h':
if( showhelp==FALSE ) showhelp = TRUE;
else showhelp = FALSE;
break;
case '.':
if( showparticles==FALSE ) showparticles = TRUE;
else showparticles = FALSE;
break;
case '>':
if( dots==FALSE ) dots = TRUE;
else dots = FALSE;
break;
case 'g':
if( nmgrav==FALSE ) nmgrav = TRUE;
else nmgrav = FALSE;
break;
}
updatetexts(); */
}
///////////////////////////////////////////////////////////////