-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.cc
executable file
·374 lines (303 loc) · 11.4 KB
/
view.cc
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
/*
* view.cc
*
* Created on: Jan 20, 2009
* Author: root
*/
#include "simlab.h"
#ifdef GTK
#include <gtk/gtk.h>
#ifdef GL
#include <gtk/gtkgl.h>
#endif
#endif
#ifdef GL
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include <map>
#include <vector>
#include <string.h>
extern passa<4> passnext;
extern simlab data;
JNIEXPORT int run( simlab runner );
typedef struct tfour {
unsigned char one;
unsigned char two;
unsigned char tre;
} tfour;
typedef struct glstuff {
int drawtype;
int buffertype;
int stride;
simlab data;
} glstuff;
std::vector<glstuff> glbuffer;
#ifdef GTK
JNIEXPORT int thread( simlab cmd ) {
#ifdef PTHREAD
pthread_t thread;
pthread_create( &thread, NULL, threadrunner, (void*)cmd.buffer );
#else
GError* error;
g_thread_create( (GThreadFunc)cmd.buffer, (gpointer)*((int*)(&passnext.big)), false, &error );
#endif
return 0;
}
static gboolean image_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data ) {
//GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
//GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
//int w = widget->allocation.width;
//int h = widget->allocation.height;
//gdk_draw_image( widget->window, widget->style->fg_gc[GTK_WIDGET_STATE (widget)], (GdkImage*)data, 0, 0, 0, 0, w, h );
return TRUE;
}
static gboolean image_configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer dat ) {
GdkImage* img = (GdkImage*)dat;
int width = img->width;
int height = img->height;
//printf("erm %d\n", data.type );
if( data.type > 0 ) {
if( data.type == 66 ) {
for( int i = 0; i < width*height; i++ ) {
gdk_image_put_pixel( img, i%width, i/width, (unsigned int)((double*)data.buffer)[i] );
}
} else if( data.type == 32 ) {
for( int i = 0; i < width*height; i++ ) {
gdk_image_put_pixel( img, i%width, i/width, (unsigned int)((int*)data.buffer)[i] );
}
} else if( data.type == 24 ) {
for( int i = 0; i < width*height; i++ ) {
unsigned int cval = 0;
memcpy( &cval, (void*)&((tfour*)data.buffer)[i], sizeof(tfour) );
gdk_image_put_pixel( img, i%width, i/width, cval );
}
}
} else {
if( data.type == -66 ) {
c_simlab<double> & sl = *(c_simlab<double>*)data.buffer;
for( int i = 0; i < width*height; i++ ) {
gdk_image_put_pixel( img, i%width, i/width, (unsigned int)sl[i] );
}
} if( data.type == -64 ) {
c_simlab<long long> & sl = *(c_simlab<long long>*)data.buffer;
for( int i = 0; i < width*height; i++ ) {
gdk_image_put_pixel( img, i%width, i/width, (unsigned int)sl[i] );
}
} else if( data.type == -32 ) {
c_simlab<int> & sl = *(c_simlab<int>*)data.buffer;
for( int i = 0; i < width*height; i++ ) {
gdk_image_put_pixel( img, i%width, i/width, (unsigned int)sl[i] );
}
}
}
return TRUE;
}
std::map<int,simlab> timers;
gint timeout_callback(gpointer image) {
GtkImage *imgWidget = (GtkImage*)image;
GdkImage *img;
gtk_image_get_image( imgWidget, &img, NULL );
simlab & tmr = timers[(long)img];
run(tmr);
image_configure_event( NULL, NULL, img );
gtk_widget_queue_draw( (GtkWidget*)imgWidget );
return 1;
}
#ifdef GL
void drawGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f( 1.0f, 1.0f, 1.0f );
std::vector<glstuff>::iterator it = glbuffer.begin();
while( it != glbuffer.end() ) {
glInterleavedArrays( it->buffertype, 0, (GLvoid*)it->data.buffer );
glDrawArrays( it->drawtype, 0, it->data.length/it->stride );
it++;
}
}
gint gl_timeout_callback(gpointer widget) {
//GtkImage *imgWidget = (GtkImage*)image;
//GdkImage *img;
//gtk_image_get_image( imgWidget, &img, NULL );
/*gdk_draw_rectangle (pixmap,
widget->style->black_gc,
TRUE,
100, (i++)*5,8,8);*/
simlab & tmr = timers[(long)widget];
run(tmr);
//run(timers[(int)img]);
//printf("%d\n",(int)imgWidget);
//image_configure_event( NULL, NULL, img );
//gtk_image_expose();
//gtk_signal_emit_by_name( (GtkObject*)imgWidget, "expose_event" );
gtk_widget_queue_draw( (GtkWidget*)widget );
return 1;
}
static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data ) {
GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
return FALSE;
drawGL();
/*glColor3f( 1.0f, 1.0f, 1.0f );
if( data.length >= chunk.buffer ) {
//glEnableClientState( GL_VERTEX_ARRAY );
//glVertexPointer( chunk.buffer, GL_DOUBLE, 0, (GLvoid*)data.buffer );
glInterleavedArrays( GL_V3F, 0, (GLvoid*)data.buffer );
glDrawArrays( what.buffer, 0, data.length/chunk.buffer ); //data.length/3 ); //start.buffer, end.buffer );
}*/
if (gdk_gl_drawable_is_double_buffered (gldrawable))
gdk_gl_drawable_swap_buffers (gldrawable);
else
glFlush ();
gdk_gl_drawable_gl_end (gldrawable);
return TRUE;
}
static gboolean configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer data ) {
GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
int w = widget->allocation.width;
int h = widget->allocation.height;
if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext)) return FALSE;
glViewport (0, 0, (GLsizei)w, (GLsizei)h );
/*glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glGetDoublev( GL_PROJECTION_MATRIX, n );
glGetDoublev( GL_PROJECTION_MATRIX, o );
if (w > h) {
aspect = w / h;
glFrustum (-aspect, aspect, -1.0, 1.0, 1.0, 500.0);
} else {
aspect = h / w;
glFrustum (-1.0, 1.0, -aspect, aspect, 1.0, 500.0);
}
//glGetDoublev( GL_PROJECTION_MATRIX, o );
glMatrixMode (GL_MODELVIEW);*/
gdk_gl_drawable_gl_end (gldrawable);
return TRUE;
}
bool fscreen = false;
static gboolean keypress_event(GtkWidget *widget, GdkEventKey *event, gpointer data ) {
char ch = event->string[0];
if( ch == 'f' ) {
fscreen = !fscreen;
if( fscreen ) gtk_window_fullscreen( GTK_WINDOW(widget) );
else gtk_window_unfullscreen( GTK_WINDOW(widget) );
}
return TRUE;
}
static gboolean buttonpress_event(GtkWidget *widget, GdkEventKey *event, gpointer data ) {
char ch = event->string[0];
//if( ch == 'f' ) {
fscreen = !fscreen;
if( fscreen ) gtk_window_fullscreen( GTK_WINDOW(widget) );
else gtk_window_unfullscreen( GTK_WINDOW(widget) );
//}
return TRUE;
}
JNIEXPORT int addtoglbuffer( simlab buffertype, simlab drawtype, simlab stride ) {
glstuff stuff;
stuff.data = data;
stuff.drawtype = drawtype.buffer;
stuff.buffertype = buffertype.buffer;
stuff.stride = stride.buffer;
glbuffer.push_back( stuff );
return 0;
}
JNIEXPORT int surface( simlab timer, simlab time ) {
GtkWidget* window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
g_signal_connect( window, "destroy", gtk_main_quit, NULL );
GtkWidget* draw = gtk_drawing_area_new();
gtk_container_add( GTK_CONTAINER(window), draw );
GdkGLConfig* glconfig = gdk_gl_config_new_by_mode( (GdkGLConfigMode)(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE) );
gtk_widget_set_gl_capability( draw, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE );
g_signal_connect( G_OBJECT(draw), "expose_event", G_CALLBACK(expose_event), NULL);
g_signal_connect( G_OBJECT(draw), "configure_event", G_CALLBACK(configure_event), NULL);
g_signal_connect( G_OBJECT(window), "key_press_event", G_CALLBACK(keypress_event), NULL );
g_signal_connect( G_OBJECT(draw), "button_press_event", G_CALLBACK(buttonpress_event), NULL );
int tag = 0;
if( timer.type == 96 ) {
timers[(long)draw] = timer;
tag = g_timeout_add( time.buffer, gl_timeout_callback, draw );
}
gtk_widget_show( draw );
gtk_widget_show( window );
gtk_main();
if( tag != 0 ) g_source_remove( tag );
if( timer.type == 96 ) return 2;
return 0;
}
JNIEXPORT int window( simlab timer, simlab time ) {
GtkWidget* window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
g_signal_connect( window, "destroy", gtk_main_quit, NULL );
//gtk_window_set_title( GTK_WINDOW(window), (char*)name.buffer );
//gtk_widget_set_size_request( window, 800, 600 );
GtkWidget* draw = gtk_drawing_area_new();
gtk_container_add( GTK_CONTAINER(window), draw );
GdkGLConfig* glconfig = gdk_gl_config_new_by_mode( (GdkGLConfigMode)(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE) );
gtk_widget_set_gl_capability( draw, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE );
g_signal_connect( G_OBJECT(draw), "expose_event", G_CALLBACK(expose_event), NULL);
g_signal_connect( G_OBJECT(draw), "configure_event", G_CALLBACK(configure_event), NULL);
g_signal_connect( G_OBJECT(window), "key_press_event", G_CALLBACK(keypress_event), NULL );
//g_signal_connect( G_OBJECT(window), "key_release_event", G_CALLBACK(keyrelease_event), NULL );
//int tag = g_timeout_add( 10, timeout_callback, window );
int tag = 0;
if( timer.type == 96 ) {
printf("install timer %d\n", timer.buffer);
timers[(long)draw] = timer;
tag = g_timeout_add( time.buffer, gl_timeout_callback, draw );
}
gtk_widget_show( draw );
gtk_widget_show( window );
gtk_main();
if( tag != 0 ) g_source_remove( tag );
if( timer.type == 96 ) return 2;
return 0;
/*int tag = 0;
if( timer.buffer != 0 ) {
timers[(int)draw] = timer;
tag = g_timeout_add( time.buffer, gl_timeout_callback, draw );
} else if( time.buffer != 0 ) {
tag = g_timeout_add( time.buffer, gl_timeout_callback, draw );
}
gtk_widget_show( draw );
gtk_widget_show( window );
gtk_main();
if( tag != 0 ) g_source_remove( tag );*/
//return 0;
}
#endif
JNIEXPORT int image( simlab chunk, simlab chunk2, simlab timer, simlab time ) {
GtkWidget* window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
g_signal_connect( window, "destroy", gtk_main_quit, NULL );
gtk_window_set_title( GTK_WINDOW(window), (char*)"image" );//name.buffer );
//gtk_widget_set_size_request( window, 800, 600 );
//int val = chunk.buffer;
int w = chunk.buffer;
int h = chunk2.buffer;
GtkWidget* scrolled_window = gtk_scrolled_window_new( NULL, NULL );
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add( GTK_CONTAINER(window), scrolled_window );
GdkImage* img = gdk_image_new( GDK_IMAGE_NORMAL, gdk_visual_get_system(), w, h);
image_configure_event( NULL, NULL, img );
GtkWidget* imgWidget = gtk_image_new_from_image( img, NULL );
gtk_scrolled_window_add_with_viewport( (GtkScrolledWindow*)scrolled_window, imgWidget );
//g_signal_connect( G_OBJECT(scrolled_window), "expose_event", G_CALLBACK(image_expose_event), img);
//g_signal_connect( G_OBJECT(scrolled_window), "configure_event", G_CALLBACK(image_configure_event), img);
//g_signal_connect( G_OBJECT(window), "key_press_event", G_CALLBACK(keypress_event), NULL );
//g_signal_connect( G_OBJECT(window), "key_release_event", G_CALLBACK(keyrelease_event), NULL );
int tag = 0;
if( timer.type == 96 ) {
printf("install timer %d\n", timer.buffer);
timers[(long)img] = timer;
tag = g_timeout_add( time.buffer, timeout_callback, imgWidget );
}
gtk_widget_show( imgWidget );
gtk_widget_show( scrolled_window );
gtk_widget_show( window );
gtk_main();
if( tag != 0 ) g_source_remove( tag );
if( timer.type == 96 ) return 3;
else return 1;
}
#endif