Mercurial > repos > rhope
comparison visuality.c @ 0:76568becd6d6
Rhope Alpha 2a source import
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 28 Apr 2009 23:06:07 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:76568becd6d6 |
---|---|
1 | |
2 | |
3 #include <stdio.h> | |
4 #include <math.h> | |
5 | |
6 #include "structs.h" | |
7 #include "visuality.h" | |
8 #include "interp.h" | |
9 #include "parser.h" | |
10 #include "saveload.h" | |
11 | |
12 | |
13 #define NUM_SPRITES 5 | |
14 | |
15 | |
16 FILE * outfile; | |
17 | |
18 | |
19 HGLRC hRC = NULL; | |
20 HDC hDC = NULL; | |
21 HWND hWnd = NULL; | |
22 HINSTANCE hInstance; | |
23 GLuint glyph_base; | |
24 | |
25 BOOL mouse_left_down=FALSE; | |
26 BOOL mouse_leftstart_down=FALSE; | |
27 int mouse_lastx; | |
28 int mouse_lasty; | |
29 int mouse_curx; | |
30 int mouse_cury; | |
31 int start_wire_worker=-1; | |
32 int start_wire_ionum=-2; | |
33 BOOL start_wire_isinput; | |
34 BOOL checked_mouse_pos=FALSE; | |
35 | |
36 char new_name_buf[256]; | |
37 int buf_size=0; | |
38 | |
39 extern char text_buf[256]; | |
40 extern int text_buf_size=0; | |
41 | |
42 extern BOOL execute_active; | |
43 | |
44 worker * view_workerlist; | |
45 wire * view_wirelist; | |
46 | |
47 int current_def=0; | |
48 /* | |
49 void save_program(char * filename) | |
50 { | |
51 worker * aworkerlist; | |
52 wire * awirelist; | |
53 FILE * savefile; | |
54 int def_num, version = 1; | |
55 savefile = fopen(filename, "wb"); | |
56 if(!savefile) | |
57 return; | |
58 deflist[current_def].num_workers = num_workers; | |
59 deflist[current_def].num_wires = num_wires; | |
60 fwrite(&version, 4, 1, savefile); | |
61 fwrite(&num_defs, 4, 1, savefile); | |
62 fwrite(deflist, sizeof(worker_def), num_defs, savefile); | |
63 for(def_num = 0; def_num < num_defs; ++def_num) | |
64 { | |
65 if(deflist[def_num].workerlist) | |
66 { | |
67 fwrite(&def_num, 4, 1, savefile); | |
68 //fwrite(&(deflist[def_num].num_workers), 4, 1, savefile); | |
69 fwrite(deflist[def_num].workerlist, sizeof(worker), deflist[def_num].num_workers, savefile); | |
70 //fwrite(&(deflist[def_num].num_wires), 4, 1, savefile); | |
71 fwrite(deflist[def_num].wirelist, sizeof(wire), deflist[def_num].num_wires, savefile); | |
72 } | |
73 } | |
74 def_num = -1; | |
75 fwrite(&def_num, 4, 1, savefile); | |
76 fclose(savefile); | |
77 } | |
78 | |
79 void load_program(char * filename) | |
80 { | |
81 char msg[256]; | |
82 worker * aworkerlist; | |
83 wire * awirelist; | |
84 FILE * loadfile; | |
85 int def_num, version; | |
86 loadfile = fopen(filename, "rb"); | |
87 if(!loadfile) | |
88 { | |
89 MessageBox(NULL, "Could not open file","Error",MB_OK); | |
90 return; | |
91 } | |
92 fread(&version, 4, 1, loadfile); | |
93 if(version != 1) | |
94 { | |
95 MessageBox(NULL, "Can't read files of this version.","Error",MB_OK); | |
96 return; | |
97 } | |
98 fread(&num_defs, 4, 1, loadfile); | |
99 fread(deflist, sizeof(worker_def), num_defs, loadfile); | |
100 fread(&def_num, 4, 1, loadfile); | |
101 while(def_num >= 0 && !feof(loadfile)) | |
102 { | |
103 sprintf(msg, "Reading def %X at %X", def_num, ftell(loadfile)); | |
104 MessageBox(NULL, msg, "debug",MB_OK); | |
105 deflist[def_num].workerlist = malloc((deflist[def_num].num_workers+512)*sizeof(worker)); | |
106 fread(deflist[def_num].workerlist, sizeof(worker), deflist[def_num].num_workers, loadfile); | |
107 deflist[def_num].wirelist = malloc((deflist[def_num].num_wires+1024)*sizeof(wire)); | |
108 fread(deflist[def_num].wirelist, sizeof(wire), deflist[def_num].num_wires, loadfile); | |
109 deflist[def_num].workers_to_wires_up = malloc((deflist[def_num].num_wires+1024)*sizeof(int)); | |
110 deflist[def_num].workers_to_wires_down = malloc((deflist[def_num].num_wires+1024)*sizeof(int)); | |
111 fread(&def_num, 4, 1, loadfile); | |
112 } | |
113 fclose(loadfile); | |
114 view_workerlist = deflist[0].workerlist; | |
115 view_wirelist = deflist[0].wirelist; | |
116 num_workers = deflist[0].num_workers; | |
117 num_wires = deflist[0].num_wires; | |
118 initpredefworkers(); | |
119 //sprintf(msg,"%d workers, %d wires in %s",deflist[0].num_workers,deflist[0].num_wires,deflist[0].name); | |
120 //MessageBox(NULL,msg,"visdbg",MB_OK); | |
121 }*/ | |
122 | |
123 | |
124 | |
125 BOOL keys[256]; | |
126 BOOL active = TRUE; | |
127 BOOL fullscreen=TRUE; | |
128 BOOL gameover = FALSE; | |
129 | |
130 GLfloat xrot; // X Rotation ( NEW ) | |
131 GLfloat yrot; // Y Rotation ( NEW ) | |
132 GLfloat zrot; // Z Rotation ( NEW ) | |
133 | |
134 GLuint texture[NUM_SPRITES]; // Storage For One Texture ( NEW ) | |
135 float xsizes[NUM_SPRITES] = {0.75f, 1.0f, 0.25f, 0.25f, 2.0f}; | |
136 float ysizes[NUM_SPRITES] = {1.0f, 0.5f, 0.25f, 0.25f, 2.0f}; | |
137 float xtexturefit[NUM_SPRITES] = {0.75f, 1.0f, 1.0f, 1.0f, 1.0f}; | |
138 float ytexturefit[NUM_SPRITES] = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}; | |
139 float gunxpos = 5.0f; | |
140 char spritenames[NUM_SPRITES][32] = {"rini.bmp", "zapper.bmp", "heart.bmp", "fireball.bmp", "kiss.bmp"}; | |
141 | |
142 #define SCREEN_WIDTH_REL 8.8f | |
143 #define SCREEN_HEIGHT_REL 6.6f | |
144 #define PI 3.14159265 | |
145 | |
146 int selected_worker = -1; | |
147 int selected_wire = -1; | |
148 | |
149 | |
150 int killed = 0; | |
151 | |
152 | |
153 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); | |
154 | |
155 | |
156 AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image | |
157 { | |
158 FILE *File=NULL; // File Handle | |
159 if (!Filename) // Make Sure A Filename Was Given | |
160 { | |
161 return NULL; // If Not Return NULL | |
162 } | |
163 File=fopen(Filename,"r"); // Check To See If The File Exists | |
164 if (File) // Does The File Exist? | |
165 { | |
166 fclose(File); // Close The Handle | |
167 return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer | |
168 } | |
169 return NULL; // If Load Failed Return NULL | |
170 } | |
171 /* | |
172 int LoadGLTextures() // Load Bitmaps And Convert To Textures | |
173 { | |
174 int Status=FALSE; // Status Indicator | |
175 AUX_RGBImageRec *TextureImage[NUM_SPRITES]; // Create Storage Space For The Texture | |
176 int i; | |
177 | |
178 | |
179 for(i = 0; i < NUM_SPRITES; ++i) | |
180 { | |
181 | |
182 TextureImage[i] = NULL; // Set The Pointer To NULL | |
183 // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit | |
184 if (TextureImage[i]=LoadBMP(spritenames[i])) | |
185 { | |
186 Status=TRUE; // Set The Status To TRUE | |
187 glGenTextures(1, &texture[i]); // Create The Texture | |
188 | |
189 // Typical Texture Generation Using Data From The Bitmap | |
190 glBindTexture(GL_TEXTURE_2D, texture[i]); | |
191 // Generate The Texture | |
192 glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[i]->sizeX, TextureImage[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]->data); | |
193 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering | |
194 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering | |
195 } | |
196 if (TextureImage[i]) // If Texture Exists | |
197 { | |
198 if (TextureImage[i]->data) // If Texture Image Exists | |
199 { | |
200 free(TextureImage[i]->data); // Free The Texture Image Memory | |
201 } | |
202 | |
203 free(TextureImage[i]); // Free The Image Structure | |
204 } | |
205 //xsizes[i] = 0.5f; | |
206 //ysizes[i] = 1.0f; | |
207 } | |
208 | |
209 return Status; // Return The Status | |
210 } | |
211 | |
212 */ | |
213 | |
214 GLvoid ReSizeGLScene(GLsizei width, GLsizei height) | |
215 { | |
216 if(height ==0) | |
217 height = 1; | |
218 glViewport(0,0,width,height); | |
219 | |
220 glMatrixMode(GL_PROJECTION); //Reset projection matrix | |
221 glLoadIdentity(); | |
222 | |
223 //Calculate aspect ratio | |
224 gluPerspective(45.0f, (GLfloat)width/ (GLfloat)height, 0.1f, 100.0f); | |
225 | |
226 glMatrixMode(GL_MODELVIEW); | |
227 glLoadIdentity(); //reset modelview matrix | |
228 } | |
229 | |
230 int InitGL(GLvoid) | |
231 { | |
232 //if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW ) | |
233 //{ | |
234 // return FALSE; // If Texture Didn't Load Return FALSE ( NEW ) | |
235 //} | |
236 HFONT font; | |
237 glEnable(GL_TEXTURE_2D); | |
238 glShadeModel(GL_SMOOTH); | |
239 | |
240 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //blackness | |
241 | |
242 glClearDepth(1.0f); | |
243 glEnable(GL_DEPTH_TEST); | |
244 glDepthFunc(GL_LEQUAL); | |
245 | |
246 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); | |
247 | |
248 glEnable(GL_COLOR_MATERIAL); | |
249 | |
250 | |
251 | |
252 font = CreateFont(-24, 0, 0, 0, FW_BOLD, 0, 0, 0, ANSI_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, | |
253 ANTIALIASED_QUALITY, FF_SWISS || VARIABLE_PITCH, "Arial"); | |
254 SelectObject(hDC, font); | |
255 | |
256 glyph_base = glGenLists(224); | |
257 wglUseFontOutlines(hDC, 32, 224, glyph_base, 0.0, 0.0, WGL_FONT_POLYGONS, NULL); | |
258 DeleteObject(font); | |
259 | |
260 return TRUE; | |
261 } | |
262 | |
263 void drawtriangle(double xpos, double ypos, double width, double height, double angle) | |
264 { | |
265 if(xpos != 0.0 || ypos != 0.0) | |
266 glTranslatef(xpos, ypos, 0.0); | |
267 | |
268 if(angle != 0.0) | |
269 glRotatef(angle, 0.0,0.0,1.0); | |
270 | |
271 glBegin(GL_TRIANGLES); | |
272 glVertex2f(0.0, height/2); //Top | |
273 glVertex2f(0.0 - width/2, 0.0 - height/2); //Left | |
274 glVertex2f(width/2, 0.0 - height/2); //Right | |
275 glEnd(); | |
276 | |
277 if(angle != 0.0) | |
278 glRotatef(0.0-angle, 0.0,0.0,1.0);//restore rotation without loading identity | |
279 if(xpos != 0.0 || ypos != 0.0) | |
280 glTranslatef(0.0-xpos, 0.0-ypos, 0.0); | |
281 } | |
282 | |
283 void drawrect(double xpos, double ypos, double width, double height, double angle) | |
284 { | |
285 if(xpos != 0.0 || ypos != 0.0) | |
286 glTranslatef(xpos, ypos, 0.0); | |
287 | |
288 if(angle != 0.0) | |
289 glRotatef(angle, 0.0,0.0,1.0); | |
290 | |
291 glBegin(GL_QUADS); | |
292 glVertex2f(0.0 - width/2, height/2.0); //Top Left | |
293 glVertex2f(width/2, height/2.0); //Top Right | |
294 glVertex2f(width/2, 0.0 - height/2.0); //Bottom Right | |
295 glVertex2f(0.0 - width/2, 0.0 - height/2.0); //Bottom Left | |
296 | |
297 glEnd(); | |
298 | |
299 if(angle != 0.0) | |
300 glRotatef(0.0-angle, 0.0,0.0,1.0);//restore rotation without loading identity | |
301 if(xpos != 0.0 || ypos != 0.0) | |
302 glTranslatef(0.0-xpos, 0.0-ypos, 0.0); | |
303 } | |
304 void drawshapestrip(int shape_type, double length, double ypos, double width, double height, int num) | |
305 { | |
306 double currentpos, spacing; | |
307 int i; | |
308 | |
309 spacing = length/(double)num; | |
310 currentpos = 0.0 - length/2.0 + spacing/2.0; | |
311 for(i = 0; i < num; ++i) | |
312 { | |
313 if(shape_type == 0) | |
314 drawtriangle(currentpos, ypos, width, height, 0.0); | |
315 else | |
316 drawrect(currentpos, ypos, width, height, 0.0); | |
317 currentpos += spacing; | |
318 } | |
319 } | |
320 | |
321 double get_io_xpos(worker * some_worker, int output_num, BOOL is_input) | |
322 { | |
323 double length, currentpos, spacing; | |
324 if(is_input && output_num == -1) | |
325 return some_worker->xpos + some_worker->width/2.0 + INPUT_SIZE/2.0; | |
326 if(some_worker->type == 2 && is_input) //Trapezoid | |
327 length = 2.0*(some_worker->width)/3.0; | |
328 else | |
329 length = some_worker->width; | |
330 fprintf(outfile, "Worker length: %f, worker width: %f\n"); | |
331 if(is_input) | |
332 spacing = length/(some_worker->num_inputs); | |
333 else | |
334 spacing = length/(some_worker->num_outputs); | |
335 currentpos = some_worker->xpos - length/2.0 + spacing/2.0; | |
336 return currentpos + spacing * (double)output_num; | |
337 } | |
338 | |
339 double get_io_ypos(worker * some_worker, int output_num, BOOL is_input) | |
340 { | |
341 if(is_input && output_num != -1) | |
342 return some_worker->ypos + (some_worker->height)/2.0; | |
343 else | |
344 return some_worker->ypos - ((some_worker->height)/2.0); | |
345 } | |
346 | |
347 void drawshape(int shape_type, double xpos, double ypos, double width, double height, double angle, int inputs, int outputs, char *text, BOOL selected) | |
348 { | |
349 double x, y,temp, currentpos, spacing; | |
350 double *inputx, *inputy, *outputx, *outputy; | |
351 double ioangle, hypot; | |
352 int i; | |
353 glLoadIdentity(); | |
354 glTranslatef(SCREEN_WIDTH_REL / (-2.0f) + xpos, SCREEN_HEIGHT_REL / (-2.0f) + ypos,-8.0f); | |
355 xpos = 0.0; ypos = 0.0; | |
356 glRotatef(angle, 0.0,0.0,1.0); | |
357 if(selected) | |
358 glColor3f(SELECT_RED, SELECT_GREEN, SELECT_BLUE); | |
359 else | |
360 glColor3f(BODY_RED, BODY_GREEN, BODY_BLUE); | |
361 switch(shape_type) | |
362 { | |
363 case 0: //Triangle | |
364 drawtriangle(0.0, 0.0, width, height, 0.0); | |
365 glTranslatef(0.0-width/4.0, 0.0-height/4.0,0); | |
366 | |
367 break; | |
368 case 1: //Rectangle | |
369 drawrect(0.0, 0.0, width, height, angle); | |
370 glColor3f(INPUT_RED, INPUT_GREEN, INPUT_BLUE); | |
371 if(inputs) | |
372 drawshapestrip(0, width, height/2.0 + INPUT_SIZE/2.0,INPUT_SIZE, INPUT_SIZE, inputs); | |
373 drawtriangle(xpos + width/2.0 + INPUT_SIZE/2.0, ypos-height/2.0+INPUT_SIZE/2.0, INPUT_SIZE, INPUT_SIZE, 0.0); | |
374 glColor3f(OUTPUT_RED, OUTPUT_GREEN, OUTPUT_BLUE); | |
375 if(outputs) | |
376 drawshapestrip(1, width, 0.0-(height/2.0 + OUTPUT_SIZE/2.0),OUTPUT_SIZE, OUTPUT_SIZE, outputs); | |
377 glTranslatef(0.0-width/2.0 + width/16.0, 0.0-height/3.0, 0.0); | |
378 | |
379 break; | |
380 case 2: //Trapezoid | |
381 glBegin(GL_QUADS); | |
382 glVertex2f(xpos - width/3.0 , ypos + height/2.0); //Top Left | |
383 glVertex2f(xpos + width/3.0, ypos + height/2.0); //Top Right | |
384 glVertex2f(xpos + width/2.0, ypos - height/2.0); //Bottom Right | |
385 glVertex2f(xpos - width/2.0, ypos - height/2.0); //Bottom Left | |
386 glEnd(); | |
387 glColor3f(INPUT_RED, INPUT_GREEN, INPUT_BLUE); | |
388 if(inputs) | |
389 drawshapestrip(0, 2.0*(width/3.0), height/2.0 + INPUT_SIZE/2.0,INPUT_SIZE, INPUT_SIZE, inputs); | |
390 drawtriangle(xpos + width/2.0 + INPUT_SIZE/2.0, ypos-height/2.0+INPUT_SIZE/2.0, INPUT_SIZE, INPUT_SIZE, 0.0); | |
391 glColor3f(OUTPUT_RED, OUTPUT_GREEN, OUTPUT_BLUE); | |
392 if(outputs) | |
393 drawshapestrip(1, width, 0.0-(height/2.0 + OUTPUT_SIZE/2.0),OUTPUT_SIZE, OUTPUT_SIZE, outputs); | |
394 glTranslatef(0.0-width/3.0, 0.0-height/3.0, 0.0); | |
395 break; | |
396 | |
397 case 3: //Ellipse | |
398 | |
399 x = 3.4587; | |
400 y = 2293.784; | |
401 | |
402 glBegin(GL_TRIANGLE_FAN); | |
403 glVertex2f(0.0, 0.0); | |
404 | |
405 if(inputs) | |
406 { | |
407 spacing = width/(double)inputs; | |
408 currentpos = 0.0-width/2.0 + spacing/2.0; | |
409 inputx = (double *)malloc(inputs*sizeof(double)); | |
410 inputy = (double *)malloc(inputs*sizeof(double)); | |
411 i = 0; | |
412 } | |
413 for(x = 0.0-width/2 ; x <= width/2.0+CIRCLE_STEP; x += CIRCLE_STEP) | |
414 { | |
415 y = sqrt(fabs(pow(height/2.0,2) - (pow(height/2.0,2)*pow(x, 2))/pow(width/2.0, 2))); | |
416 glVertex2f(x, y); | |
417 if(inputs && x >= currentpos) | |
418 { | |
419 inputx[i] = x; | |
420 inputy[i] = y; | |
421 ++i; | |
422 currentpos += spacing; | |
423 } | |
424 } | |
425 x -= CIRCLE_STEP; | |
426 while(x >= 0.0-width/2) | |
427 { | |
428 y = 0.0-sqrt(fabs(pow(height/2.0,2) - (pow(height/2.0,2)*pow(x, 2))/pow(width/2.0, 2))); | |
429 glVertex2f(x+xpos, y+ypos); | |
430 x -= CIRCLE_STEP; | |
431 } | |
432 | |
433 glEnd(); | |
434 | |
435 glColor3f(INPUT_RED, INPUT_GREEN, INPUT_BLUE); | |
436 for(i = 0; i < inputs; ++i) | |
437 { | |
438 ioangle = atan2(inputy[i], inputx[i]); | |
439 hypot = sqrt(pow(inputx[i],2) + pow(inputy[i], 2)) + INPUT_SIZE/2; | |
440 x = hypot * cos(ioangle); | |
441 y = hypot * sin(ioangle); | |
442 ioangle *= (180/PI); | |
443 fprintf(outfile, "oldx: %f, oldy: %f, newx: %f, newy %f, hypot %f, angle %f\n",inputx[i],inputy[i],x,y,hypot, ioangle); | |
444 drawtriangle(x,y,INPUT_SIZE,INPUT_SIZE, 90-ioangle); | |
445 } | |
446 glTranslatef(0.0-width/8.0, 0.0, 0.0); | |
447 break; | |
448 default: | |
449 break; | |
450 } | |
451 if(text) | |
452 { | |
453 glColor3f(TEXT_RED, TEXT_GREEN, TEXT_BLUE); | |
454 glPushAttrib(GL_LIST_BIT); | |
455 glListBase(glyph_base-32); | |
456 glScalef(0.25,0.25,0.25); | |
457 | |
458 glCallLists(strlen(text),GL_UNSIGNED_BYTE, text); | |
459 | |
460 glPopAttrib(); | |
461 | |
462 } | |
463 } | |
464 | |
465 #define INTX_TO_FLOAT_REL(val) (((double)val)*SCREEN_WIDTH_REL/640.0) | |
466 #define INTY_TO_FLOAT_REL(val) (((double)val)*SCREEN_HEIGHT_REL/480.0) | |
467 | |
468 //void drawshape(int shape_type, double xpos, double ypos, double width, double height, double angle, int inputs, int outputs) | |
469 int DrawGLScene(GLvoid) | |
470 { | |
471 double line_startx; | |
472 double line_starty; | |
473 double line_endx; | |
474 double line_endy; | |
475 double m,b,x,y,mousey; | |
476 int i,j; | |
477 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
478 | |
479 for(i = 0; i < num_workers; ++i) | |
480 { | |
481 if(mouse_leftstart_down | |
482 && INTX_TO_FLOAT_REL(mouse_curx) > (view_workerlist[i].xpos - view_workerlist[i].width/2.0) | |
483 && INTX_TO_FLOAT_REL(mouse_curx) < (view_workerlist[i].xpos + view_workerlist[i].width/2.0) | |
484 && INTY_TO_FLOAT_REL(mouse_cury) > (view_workerlist[i].ypos - view_workerlist[i].height/2.0) | |
485 && INTY_TO_FLOAT_REL(mouse_cury) < (view_workerlist[i].ypos + view_workerlist[i].height/2.0)) | |
486 { | |
487 view_workerlist[i].grabbed = TRUE; | |
488 selected_worker = i; | |
489 selected_wire = -1; | |
490 mouse_leftstart_down = FALSE; | |
491 } | |
492 else if(mouse_leftstart_down | |
493 && INTX_TO_FLOAT_REL(mouse_curx) > (view_workerlist[i].xpos - view_workerlist[i].width/2.0) //Check for mouse in input or output area | |
494 && INTX_TO_FLOAT_REL(mouse_curx) < (view_workerlist[i].xpos + view_workerlist[i].width/2.0) | |
495 && (INTY_TO_FLOAT_REL(mouse_cury) > (view_workerlist[i].ypos - view_workerlist[i].height/2.0 - OUTPUT_SIZE)) | |
496 && (INTY_TO_FLOAT_REL(mouse_cury) < (view_workerlist[i].ypos + view_workerlist[i].height/2.0 + INPUT_SIZE))) | |
497 { | |
498 if(INTY_TO_FLOAT_REL(mouse_cury) > view_workerlist[i].ypos && view_workerlist[i].num_inputs > 0)//is mouse above or below worker (inputs are above, outputs below)? | |
499 { | |
500 fputs("Hit on input region.\n", outfile); | |
501 for(j = 0; j < view_workerlist[i].num_inputs; ++j) | |
502 { | |
503 if(INTX_TO_FLOAT_REL(mouse_curx) > (get_io_xpos(&view_workerlist[i], j, TRUE)-INPUT_SIZE/2.0) && INTX_TO_FLOAT_REL(mouse_curx) < (get_io_xpos(&view_workerlist[i], j, TRUE)+INPUT_SIZE/2.0)) | |
504 { | |
505 fprintf(outfile, "Hit on input number %d on worker %d.\n", j, i); | |
506 mouse_leftstart_down = FALSE; | |
507 if(start_wire_worker >= 0) | |
508 { | |
509 fprintf(outfile, "Adding wire %d. output_num: %d, input_num: %d", num_wires,start_wire_ionum, j); | |
510 //Add wire | |
511 view_wirelist[num_wires].start_worker = start_wire_worker; | |
512 view_wirelist[num_wires].end_worker = i; | |
513 view_wirelist[num_wires].output_num = start_wire_ionum;; | |
514 view_wirelist[num_wires].input_num = j; | |
515 ++num_wires; | |
516 start_wire_worker = start_wire_ionum = -1; | |
517 | |
518 } | |
519 else | |
520 { | |
521 fputs("Saving information for later wire creation.\n", outfile); | |
522 start_wire_worker = i; | |
523 start_wire_ionum = j; | |
524 start_wire_isinput=TRUE; | |
525 } | |
526 break; | |
527 } | |
528 } | |
529 } | |
530 else if(view_workerlist[i].num_outputs > 0) | |
531 { | |
532 fputs("Hit on output region.\n", outfile); | |
533 for(j = 0; j < view_workerlist[i].num_outputs; ++j) | |
534 { | |
535 fprintf(outfile, "Checking mouse x(%f) for hit on output number %d with position %f\n", INTX_TO_FLOAT_REL(mouse_curx), j, get_io_xpos(&view_workerlist[i], j, FALSE)); | |
536 if(INTX_TO_FLOAT_REL(mouse_curx) > (get_io_xpos(&view_workerlist[i], j, FALSE)-OUTPUT_SIZE/2.0) && INTX_TO_FLOAT_REL(mouse_curx) < (get_io_xpos(&view_workerlist[i], j, FALSE)+OUTPUT_SIZE/2.0)) | |
537 { | |
538 fprintf(outfile, "Hit on output number %d on worker %d.\n", j, i); | |
539 mouse_leftstart_down = FALSE; | |
540 if(start_wire_worker >= 0) | |
541 { | |
542 fprintf(outfile, "Adding wire %d. output_num: %d, input_num: %d", num_wires, j, start_wire_ionum); | |
543 //Add wire | |
544 view_wirelist[num_wires].start_worker = i; | |
545 view_wirelist[num_wires].end_worker = start_wire_worker; | |
546 view_wirelist[num_wires].output_num = j; | |
547 view_wirelist[num_wires].input_num = start_wire_ionum; | |
548 if(start_wire_ionum == -1) | |
549 view_workerlist[start_wire_worker].null_input = TRUE; | |
550 ++num_wires; | |
551 start_wire_worker = start_wire_ionum = -1; | |
552 } | |
553 else | |
554 { | |
555 fputs("Saving information for later wire creation.\n", outfile); | |
556 start_wire_worker = i; | |
557 start_wire_ionum = j; | |
558 start_wire_isinput=FALSE; | |
559 } | |
560 break; | |
561 } | |
562 } | |
563 } | |
564 | |
565 } | |
566 else if(mouse_leftstart_down | |
567 && INTX_TO_FLOAT_REL(mouse_curx) > (view_workerlist[i].xpos + view_workerlist[i].width/2.0) //Check for mouse in input or output area | |
568 && INTX_TO_FLOAT_REL(mouse_curx) < (view_workerlist[i].xpos + view_workerlist[i].width/2.0+INPUT_SIZE) | |
569 && (INTY_TO_FLOAT_REL(mouse_cury) > (view_workerlist[i].ypos - view_workerlist[i].height/2.0)) | |
570 && (INTY_TO_FLOAT_REL(mouse_cury) < (view_workerlist[i].ypos - view_workerlist[i].height/2.0 + INPUT_SIZE))) | |
571 { | |
572 fprintf(outfile, "Click on NULL input at %f,%f\n", INTX_TO_FLOAT_REL(mouse_curx), INTY_TO_FLOAT_REL(mouse_cury)); | |
573 mouse_leftstart_down = FALSE; | |
574 if(start_wire_worker >= 0) | |
575 { | |
576 fprintf(outfile, "Adding wire %d. output_num: %d, input_num: %d", num_wires,start_wire_ionum, -1); | |
577 //Add wire | |
578 view_wirelist[num_wires].start_worker = start_wire_worker; | |
579 view_wirelist[num_wires].end_worker = i; | |
580 view_wirelist[num_wires].output_num = start_wire_ionum;; | |
581 view_wirelist[num_wires].input_num = -1; | |
582 view_workerlist[i].null_input = TRUE; | |
583 ++num_wires; | |
584 start_wire_worker = start_wire_ionum = -1; | |
585 | |
586 } | |
587 else | |
588 { | |
589 fputs("Saving information for later wire creation.\n", outfile); | |
590 start_wire_worker = i; | |
591 start_wire_ionum = -1; | |
592 start_wire_isinput=TRUE; | |
593 } | |
594 } | |
595 else if(mouse_leftstart_down) | |
596 { | |
597 fprintf(outfile, "Click at %f,%f, worker at %f,%f with size %f,%f\n", INTX_TO_FLOAT_REL(mouse_curx), INTY_TO_FLOAT_REL(mouse_cury), view_workerlist[i].xpos, view_workerlist[i].ypos, view_workerlist[i].width, view_workerlist[i].height); | |
598 } | |
599 | |
600 if(!mouse_left_down) | |
601 view_workerlist[i].grabbed= FALSE; | |
602 if(mouse_left_down && view_workerlist[i].grabbed && (mouse_curx != mouse_lastx || mouse_cury != mouse_lasty)) | |
603 { | |
604 view_workerlist[i].xpos += INTX_TO_FLOAT_REL(mouse_curx-mouse_lastx); | |
605 view_workerlist[i].ypos += INTY_TO_FLOAT_REL(mouse_cury-mouse_lasty); | |
606 } | |
607 drawshape(view_workerlist[i].display_type, view_workerlist[i].xpos, view_workerlist[i].ypos, view_workerlist[i].width, view_workerlist[i].height, | |
608 view_workerlist[i].angle, view_workerlist[i].num_inputs, view_workerlist[i].num_outputs, view_workerlist[i].name, selected_worker==i); | |
609 } | |
610 | |
611 | |
612 glLoadIdentity(); | |
613 glTranslatef(SCREEN_WIDTH_REL / (-2.0f), SCREEN_HEIGHT_REL / (-2.0f),-8.0f); | |
614 | |
615 glColor3f(WIRE_RED, WIRE_GREEN, WIRE_BLUE); | |
616 for(i = 0; i < num_wires; ++i) | |
617 { | |
618 fprintf(outfile, "Wire %d starts in worker %d at output %d of %d and ends in worker %d at input %d of %d\n", i, view_wirelist[i].start_worker, | |
619 view_wirelist[i].output_num, view_workerlist[view_wirelist[i].start_worker].num_outputs, view_wirelist[i].end_worker, view_wirelist[i].input_num, view_workerlist[view_wirelist[i].end_worker].num_inputs); | |
620 line_startx = get_io_xpos(&view_workerlist[view_wirelist[i].start_worker], view_wirelist[i].output_num, FALSE); | |
621 line_starty = get_io_ypos(&view_workerlist[view_wirelist[i].start_worker], view_wirelist[i].output_num, FALSE)-OUTPUT_SIZE; | |
622 line_endx = get_io_xpos(&view_workerlist[view_wirelist[i].end_worker], view_wirelist[i].input_num, TRUE); | |
623 line_endy = get_io_ypos(&view_workerlist[view_wirelist[i].end_worker], view_wirelist[i].input_num, TRUE)+OUTPUT_SIZE; | |
624 | |
625 x = INTX_TO_FLOAT_REL(mouse_curx); | |
626 mousey = INTY_TO_FLOAT_REL(mouse_cury); | |
627 if(mouse_leftstart_down) | |
628 { | |
629 //y = mx=b | |
630 | |
631 if(line_startx < line_endx) | |
632 { | |
633 if(x >= line_startx && x <= line_endx) | |
634 { | |
635 m = (line_endy-line_starty)/(line_endx-line_starty); | |
636 b = line_starty; | |
637 x -= line_startx; | |
638 y = m*x + b; | |
639 fprintf(outfile, "y: %f, m: %f, x: %f, b: %f, mousey: %f\n", y,m,x,b,mousey); | |
640 if(mousey >= (y-0.05) && mousey <= (y+0.05)) | |
641 { | |
642 selected_wire = i; | |
643 selected_worker = -1; | |
644 } | |
645 } | |
646 } | |
647 else if(line_endx > line_startx) | |
648 { | |
649 if(x <= line_startx && x >= line_endx) | |
650 { | |
651 m = (line_starty-line_endy)/(line_startx-line_endy); | |
652 b = line_endy; | |
653 y = m*x + b; | |
654 x -= line_endx; | |
655 if(mousey >= (y-0.05) && mousey <= (y+0.05)) | |
656 { | |
657 selected_wire = i; | |
658 selected_worker = -1; | |
659 } | |
660 } | |
661 } | |
662 else //avoid divide by zero error | |
663 { | |
664 if(x >= (line_startx-0.05) && x <= (line_startx+0.05)) | |
665 { | |
666 if(line_starty > line_endy) | |
667 { | |
668 if(mousey >= line_endy && mousey <= line_starty) | |
669 { | |
670 selected_wire = i; | |
671 selected_worker = -1; | |
672 } | |
673 } | |
674 else | |
675 { | |
676 if(mousey <= line_endy && mousey >= line_starty) | |
677 { | |
678 selected_wire = i; | |
679 selected_worker = -1; | |
680 } | |
681 } | |
682 } | |
683 } | |
684 } | |
685 if(i == selected_wire) | |
686 glColor3f(SELECT_RED, SELECT_GREEN, SELECT_BLUE); | |
687 | |
688 fprintf(outfile, "Start (%f,%f), End (%f, %f), Mouse(%f, %f)\n", line_startx, line_starty, line_endx, line_endy, x, mousey); | |
689 | |
690 glBegin(GL_QUADS); | |
691 glVertex2f(line_startx, line_starty); | |
692 glVertex2f(line_startx+0.1, line_starty); | |
693 glVertex2f(line_endx+0.1, line_endy); | |
694 glVertex2f(line_endx, line_endy); | |
695 glEnd(); | |
696 | |
697 if(i == selected_wire) | |
698 glColor3f(WIRE_RED, WIRE_GREEN, WIRE_BLUE); | |
699 } | |
700 mouse_leftstart_down = FALSE; | |
701 //view_workerlist[3].angle += 1.0; | |
702 checked_mouse_pos=TRUE; | |
703 | |
704 if(buf_size) | |
705 { | |
706 glColor3f(1.0, 1.0, 1.0); | |
707 glPushAttrib(GL_LIST_BIT); | |
708 glListBase(glyph_base-32); | |
709 | |
710 glScalef(0.25,0.25,0.25); | |
711 | |
712 glCallLists(buf_size,GL_UNSIGNED_BYTE, new_name_buf); | |
713 | |
714 glPopAttrib(); | |
715 } | |
716 /* | |
717 if(execute_active) | |
718 { | |
719 for(i = 0; i < num_workers; ++i) | |
720 process_worker(i); | |
721 if(!execute_active) | |
722 { | |
723 for(i = 0; i < num_datum; ++i) | |
724 { | |
725 if(data[i].type & 0x80) | |
726 free(data[i].contents); | |
727 } | |
728 } | |
729 } | |
730 */ | |
731 | |
732 mouse_lastx = mouse_curx; | |
733 mouse_lasty = mouse_cury; | |
734 | |
735 return TRUE; | |
736 } | |
737 | |
738 GLvoid KillGLWindow(GLvoid) | |
739 { | |
740 if(fullscreen) | |
741 { | |
742 ChangeDisplaySettings(NULL, 0); | |
743 ShowCursor(TRUE); | |
744 } | |
745 if(hRC) | |
746 { | |
747 glDeleteLists(glyph_base, 224); | |
748 if(!wglMakeCurrent(NULL,NULL)) | |
749 MessageBox(NULL, "Release of DC and RC failed.","SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); | |
750 | |
751 if(!wglDeleteContext(hRC)) | |
752 MessageBox(NULL, "Release Rendering Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); | |
753 hRC = NULL; | |
754 if(hDC && !ReleaseDC(hWnd,hDC)) | |
755 { | |
756 MessageBox(NULL,"Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); | |
757 hDC = NULL; | |
758 } | |
759 } | |
760 | |
761 if(hWnd && !DestroyWindow(hWnd)) | |
762 { | |
763 MessageBox(NULL, "Could not Release hWnd", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); | |
764 hWnd = NULL; | |
765 } | |
766 | |
767 if(!UnregisterClass("OpenGL", hInstance)) | |
768 { | |
769 MessageBox(NULL, "Could Not Unregister Class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); | |
770 hInstance=NULL; | |
771 } | |
772 } | |
773 | |
774 BOOL CreateGLWindow(char * title, int width, int height, int bits, BOOL fullscreenflag) | |
775 { | |
776 GLuint PixelFormat; | |
777 WNDCLASS wc; | |
778 DWORD dwExStyle; | |
779 DWORD dwStyle; | |
780 | |
781 static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be | |
782 { | |
783 sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor | |
784 1, // Version Number | |
785 PFD_DRAW_TO_WINDOW | // Format Must Support Window | |
786 PFD_SUPPORT_OPENGL | // Format Must Support OpenGL | |
787 PFD_DOUBLEBUFFER, // Must Support Double Buffering | |
788 PFD_TYPE_RGBA, // Request An RGBA Format | |
789 0, // Select Our Color Depth | |
790 0, 0, 0, 0, 0, 0, // Color Bits Ignored | |
791 0, // No Alpha Buffer | |
792 0, // Shift Bit Ignored | |
793 0, // No Accumulation Buffer | |
794 0, 0, 0, 0, // Accumulation Bits Ignored | |
795 16, // 16Bit Z-Buffer (Depth Buffer) | |
796 0, // No Stencil Buffer | |
797 0, // No Auxiliary Buffer | |
798 PFD_MAIN_PLANE, // Main Drawing Layer | |
799 0, // Reserved | |
800 0, 0, 0 // Layer Masks Ignored | |
801 }; | |
802 | |
803 RECT WindowRect; | |
804 WindowRect.left = (long)0; | |
805 WindowRect.right = (long)width; | |
806 WindowRect.top = (long)0; | |
807 WindowRect.bottom=(long)height; | |
808 | |
809 fullscreen = fullscreenflag; | |
810 | |
811 hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window | |
812 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Move, And Own DC For Window | |
813 wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages | |
814 wc.cbClsExtra = 0; // No Extra Window Data | |
815 wc.cbWndExtra = 0; // No Extra Window Data | |
816 wc.hInstance = hInstance; // Set The Instance | |
817 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon | |
818 wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer | |
819 wc.hbrBackground = NULL; // No Background Required For GL | |
820 wc.lpszMenuName = NULL; // We Don't Want A Menu | |
821 wc.lpszClassName = "OpenGL"; // Set The Class Name | |
822 | |
823 if (!RegisterClass(&wc)) // Attempt To Register The Window Class | |
824 { | |
825 MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION); | |
826 return FALSE; // Exit And Return FALSE | |
827 } | |
828 | |
829 if (fullscreen) // Attempt Fullscreen Mode? | |
830 { | |
831 DEVMODE dmScreenSettings; // Device Mode | |
832 memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared | |
833 dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure | |
834 dmScreenSettings.dmPelsWidth = width; // Selected Screen Width | |
835 dmScreenSettings.dmPelsHeight = height; // Selected Screen Height | |
836 dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel | |
837 dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; | |
838 // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. | |
839 if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) | |
840 { | |
841 | |
842 // If The Mode Fails, Offer Two Options. Quit Or Run In A Window. | |
843 if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) | |
844 { | |
845 fullscreen=FALSE; // Select Windowed Mode (Fullscreen=FALSE) | |
846 } | |
847 else | |
848 { | |
849 | |
850 // Pop Up A Message Box Letting User Know The Program Is Closing. | |
851 MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP); | |
852 return FALSE; // Exit And Return FALSE | |
853 } | |
854 } | |
855 } | |
856 if (fullscreen) // Are We Still In Fullscreen Mode? | |
857 { | |
858 dwExStyle=WS_EX_APPWINDOW; // Window Extended Style | |
859 dwStyle=WS_POPUP; // Windows Style | |
860 ShowCursor(FALSE); // Hide Mouse Pointer | |
861 } | |
862 else | |
863 { | |
864 | |
865 dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style | |
866 dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style | |
867 } | |
868 AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size | |
869 if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window | |
870 "OpenGL", // Class Name | |
871 title, // Window Title | |
872 WS_CLIPSIBLINGS | // Required Window Style | |
873 WS_CLIPCHILDREN | // Required Window Style | |
874 dwStyle, // Selected Window Style | |
875 0, 0, // Window Position | |
876 WindowRect.right-WindowRect.left, // Calculate Adjusted Window Width | |
877 WindowRect.bottom-WindowRect.top, // Calculate Adjusted Window Height | |
878 NULL, // No Parent Window | |
879 NULL, // No Menu | |
880 hInstance, // Instance | |
881 NULL))) // Don't Pass Anything To WM_CREATE | |
882 { | |
883 KillGLWindow(); // Reset The Display | |
884 MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION); | |
885 return FALSE; // Return FALSE | |
886 } | |
887 | |
888 pfd.cColorBits = bits; | |
889 | |
890 if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context? | |
891 { | |
892 KillGLWindow(); // Reset The Display | |
893 MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); | |
894 return FALSE; // Return FALSE | |
895 } | |
896 if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format? | |
897 { | |
898 KillGLWindow(); // Reset The Display | |
899 MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); | |
900 return FALSE; // Return FALSE | |
901 } | |
902 if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format? | |
903 { | |
904 KillGLWindow(); // Reset The Display | |
905 MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); | |
906 return FALSE; // Return FALSE | |
907 } | |
908 if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context? | |
909 { | |
910 KillGLWindow(); // Reset The Display | |
911 MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); | |
912 return FALSE; // Return FALSE | |
913 } | |
914 if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context | |
915 { | |
916 KillGLWindow(); // Reset The Display | |
917 MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); | |
918 return FALSE; // Return FALSE | |
919 } | |
920 ShowWindow(hWnd,SW_SHOW); // Show The Window | |
921 SetForegroundWindow(hWnd); // Slightly Higher Priority | |
922 SetFocus(hWnd); // Sets Keyboard Focus To The Window | |
923 ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen | |
924 if (!InitGL()) // Initialize Our Newly Created GL Window | |
925 { | |
926 KillGLWindow(); // Reset The Display | |
927 MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION); | |
928 return FALSE; // Return FALSE | |
929 } | |
930 return TRUE; // Success | |
931 } | |
932 | |
933 LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window | |
934 UINT uMsg, // Message For This Window | |
935 WPARAM wParam, // Additional Message Information | |
936 LPARAM lParam) // Additional Message Information | |
937 { | |
938 int i; | |
939 worker_def * def; | |
940 FILE * loadfile; | |
941 char * code; | |
942 int size; | |
943 switch (uMsg) // Check For Windows Messages | |
944 { | |
945 | |
946 case WM_ACTIVATE: // Watch For Window Activate Message | |
947 { | |
948 if (!HIWORD(wParam)) // Check Minimization State | |
949 { | |
950 active=TRUE; // Program Is Active | |
951 } | |
952 else | |
953 { | |
954 active=FALSE; // Program Is No Longer Active | |
955 } | |
956 | |
957 return 0; // Return To The Message Loop | |
958 } | |
959 case WM_SYSCOMMAND: // Intercept System Commands | |
960 { | |
961 switch (wParam) // Check System Calls | |
962 { | |
963 case SC_SCREENSAVE: // Screensaver Trying To Start? | |
964 case SC_MONITORPOWER: // Monitor Trying To Enter Powersave? | |
965 return 0; // Prevent From Happening | |
966 } | |
967 break; // Exit | |
968 } | |
969 case WM_CLOSE: // Did We Receive A Close Message? | |
970 { | |
971 PostQuitMessage(0); // Send A Quit Message | |
972 return 0; // Jump Back | |
973 } | |
974 case WM_KEYDOWN: // Is A Key Being Held Down? | |
975 { | |
976 if(wParam == VK_BACK && buf_size > 0) | |
977 --buf_size; | |
978 else if(wParam == VK_F5 && !execute_active) | |
979 { | |
980 deflist[current_def].implement_func->num_workers = num_workers; | |
981 deflist[current_def].implement_func->num_wires = num_wires; | |
982 fprintf(outfile, "Starting execution.\n"); | |
983 interp_start(-1,FALSE,0,NULL); | |
984 } | |
985 else if(wParam == VK_RETURN && buf_size > 0) | |
986 { | |
987 text_buf_size = buf_size; | |
988 memcpy(text_buf, new_name_buf, buf_size); | |
989 buf_size = 0; | |
990 if(memcmp(text_buf, "Save:", strlen("Save:")) == 0) | |
991 { | |
992 text_buf[text_buf_size]='\0'; | |
993 deflist[current_def].implement_func->num_workers = num_workers; | |
994 deflist[current_def].implement_func->num_wires = num_wires; | |
995 save_program(text_buf + strlen("Save:")); | |
996 text_buf_size = 0; | |
997 } | |
998 else if(memcmp(text_buf, "Load:", strlen("Load:")) == 0) | |
999 { | |
1000 text_buf[text_buf_size]='\0'; | |
1001 load_program(text_buf + strlen("Load:")); | |
1002 view_workerlist = deflist[0].implement_func->workerlist; | |
1003 view_wirelist = deflist[0].implement_func->wirelist; | |
1004 num_workers = deflist[0].implement_func->num_workers; | |
1005 num_wires = deflist[0].implement_func->num_wires; | |
1006 text_buf_size = 0; | |
1007 } | |
1008 else if (memcmp(text_buf, "View:", strlen("View:")) == 0) | |
1009 { | |
1010 text_buf[text_buf_size]='\0'; | |
1011 deflist[current_def].implement_func->num_workers = num_workers; | |
1012 deflist[current_def].implement_func->num_wires = num_wires; | |
1013 i = find_worker(text_buf+strlen("View:"), NULL, NULL); | |
1014 if(i < 0) | |
1015 current_def = create_worker(text_buf+strlen("View:"), 0, 0, USER_FLAG | WORKER_TYPE) - deflist; | |
1016 else | |
1017 current_def = i; | |
1018 num_workers = deflist[current_def].implement_func->num_workers; | |
1019 num_wires = deflist[current_def].implement_func->num_wires; | |
1020 view_workerlist = deflist[current_def].implement_func->workerlist; | |
1021 view_wirelist = deflist[current_def].implement_func->wirelist; | |
1022 text_buf_size = 0; | |
1023 selected_worker = -1; | |
1024 } | |
1025 else if(memcmp(text_buf, "Import:", strlen("Import:")) == 0) | |
1026 { | |
1027 deflist[current_def].implement_func->num_workers = num_workers; | |
1028 deflist[current_def].implement_func->num_wires = num_wires; | |
1029 text_buf[text_buf_size]='\0'; | |
1030 loadfile = fopen(text_buf+strlen("Import:"), "rb"); | |
1031 fseek(loadfile, 0, SEEK_END); | |
1032 size = ftell(loadfile); | |
1033 fseek(loadfile, 0, SEEK_SET); | |
1034 code = malloc(size+1); | |
1035 fread(code, 1, size, loadfile); | |
1036 parse(code, size); | |
1037 } | |
1038 } | |
1039 else if(wParam == VK_DELETE) | |
1040 { | |
1041 if(selected_worker != -1) | |
1042 { | |
1043 for(i = 0; i < num_wires; ++i) | |
1044 { | |
1045 if(view_wirelist[i].start_worker == selected_worker || view_wirelist[i].end_worker == selected_worker) | |
1046 { | |
1047 view_wirelist[i] = view_wirelist[num_wires-1]; | |
1048 --num_wires; | |
1049 --i; | |
1050 } | |
1051 | |
1052 } | |
1053 view_workerlist[selected_worker] = view_workerlist[num_workers-1]; | |
1054 for(i = 0; i < num_wires; ++i) | |
1055 { | |
1056 if(view_wirelist[i].start_worker == num_workers-1) | |
1057 view_wirelist[i].start_worker = selected_worker; | |
1058 if(view_wirelist[i].end_worker == num_workers-1) | |
1059 view_wirelist[i].end_worker = selected_worker; | |
1060 } | |
1061 --num_workers; | |
1062 selected_worker = -1; | |
1063 | |
1064 } | |
1065 } | |
1066 | |
1067 | |
1068 keys[wParam] = TRUE; // If So, Mark It As TRUE | |
1069 return 0; // Jump Back | |
1070 } | |
1071 case WM_KEYUP: // Has A Key Been Released? | |
1072 { | |
1073 keys[wParam] = FALSE; // If So, Mark It As FALSE | |
1074 return 0; // Jump Back | |
1075 } | |
1076 case WM_SIZE: // Resize The OpenGL Window | |
1077 { | |
1078 ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height | |
1079 return 0; // Jump Back | |
1080 } | |
1081 case WM_RBUTTONDOWN: | |
1082 { | |
1083 view_workerlist[num_workers].xpos = INTX_TO_FLOAT_REL(GET_X_LPARAM(lParam)); | |
1084 view_workerlist[num_workers].ypos = INTY_TO_FLOAT_REL(480-GET_Y_LPARAM(lParam)); | |
1085 | |
1086 view_workerlist[num_workers].height = 0.25; | |
1087 view_workerlist[num_workers].grabbed=FALSE; | |
1088 if(buf_size > strlen("Data:") && memcmp("Data:", new_name_buf, strlen("Data:")) == 0) | |
1089 { | |
1090 | |
1091 view_workerlist[num_workers].display_type=1;//Rectangle | |
1092 view_workerlist[num_workers].type=0;//Constant | |
1093 view_workerlist[num_workers].num_inputs=0;//Constants don't have inputs | |
1094 view_workerlist[num_workers].num_outputs=1;//Constants have one output | |
1095 view_workerlist[num_workers].null_input = FALSE; | |
1096 memcpy(view_workerlist[num_workers].name, new_name_buf+strlen("Data:"), buf_size-strlen("Data:")); | |
1097 view_workerlist[num_workers].name[buf_size-strlen("Data:")]='\0'; | |
1098 view_workerlist[num_workers].width = ((double)(buf_size-strlen("Data:"))+0.5) * INPUT_SIZE; | |
1099 ++num_workers; | |
1100 buf_size = 0; | |
1101 } | |
1102 else if(buf_size > strlen("Room:") && memcmp("Room:", new_name_buf, strlen("Room:")) == 0) | |
1103 { | |
1104 view_workerlist[num_workers].type=1;//Room | |
1105 view_workerlist[num_workers].display_type=1;//Rectangle | |
1106 view_workerlist[num_workers].num_inputs=1; | |
1107 view_workerlist[num_workers].num_outputs=1; | |
1108 view_workerlist[num_workers].null_input = FALSE; | |
1109 memcpy(view_workerlist[num_workers].name, new_name_buf+strlen("Room:"), buf_size-strlen("Room:")); | |
1110 view_workerlist[num_workers].name[buf_size-strlen("Room:")]='\0'; | |
1111 view_workerlist[num_workers].width = ((double)(buf_size-strlen("Room:"))+0.5) * INPUT_SIZE; | |
1112 ++num_workers; | |
1113 buf_size = 0; | |
1114 } | |
1115 else if(buf_size > strlen("Input:") && memcmp("Input:", new_name_buf, strlen("Input:")) == 0) | |
1116 { | |
1117 deflist[current_def].implement_func->num_workers = num_workers; | |
1118 new_name_buf[buf_size] = '\0'; | |
1119 MessageBox(NULL, new_name_buf,"Error",MB_OK); | |
1120 add_input(deflist+current_def, new_name_buf + strlen("Input:"), INTX_TO_FLOAT_REL(GET_X_LPARAM(lParam)), INTY_TO_FLOAT_REL(480-GET_Y_LPARAM(lParam))); | |
1121 num_workers = deflist[current_def].implement_func->num_workers; | |
1122 } | |
1123 else if(buf_size > strlen("Output:") && memcmp("Output:", new_name_buf, strlen("Output:")) == 0) | |
1124 { | |
1125 deflist[current_def].implement_func->num_workers = num_workers; | |
1126 new_name_buf[buf_size] = '\0'; | |
1127 add_output(deflist+current_def, new_name_buf + strlen("Output:"), INTX_TO_FLOAT_REL(GET_X_LPARAM(lParam)), INTY_TO_FLOAT_REL(480-GET_Y_LPARAM(lParam))); | |
1128 num_workers = deflist[current_def].implement_func->num_workers; | |
1129 } | |
1130 else if(buf_size > strlen("Express:") && memcmp("Express:", new_name_buf, strlen("Express:")) == 0) | |
1131 { | |
1132 deflist[current_def].implement_func->num_workers = num_workers; | |
1133 new_name_buf[buf_size] = '\0'; | |
1134 def = create_worker(new_name_buf+strlen("Express:"), 0, 0, USER_FLAG | WORKER_TYPE); | |
1135 parse_body(def, new_name_buf+strlen("Express:"), buf_size-strlen("Express:")); | |
1136 add_worker_to_def(deflist+current_def, def-deflist, INTX_TO_FLOAT_REL(GET_X_LPARAM(lParam)), INTY_TO_FLOAT_REL(480-GET_Y_LPARAM(lParam))); | |
1137 num_workers = deflist[current_def].implement_func->num_workers; | |
1138 buf_size = 0; | |
1139 } | |
1140 else | |
1141 { | |
1142 memcpy(view_workerlist[num_workers].name, new_name_buf, buf_size); | |
1143 view_workerlist[num_workers].name[buf_size] = '\0'; | |
1144 for(i = 0; i < num_defs; ++i) | |
1145 { | |
1146 fprintf(outfile, "Comparing %s with %s(%d)\n", view_workerlist[num_workers].name, deflist[i].name, i); | |
1147 if(strcmp(view_workerlist[num_workers].name, deflist[i].name)==0) | |
1148 { | |
1149 view_workerlist[num_workers].display_type=2;//Trapezoid | |
1150 view_workerlist[num_workers].type=2;//Worker | |
1151 view_workerlist[num_workers].num_inputs=deflist[i].num_inputs; | |
1152 view_workerlist[num_workers].num_outputs=deflist[i].num_outputs; | |
1153 view_workerlist[num_workers].null_input = FALSE; | |
1154 view_workerlist[num_workers].value_index = i; | |
1155 view_workerlist[num_workers].width = ((double)buf_size+0.5) * INPUT_SIZE; | |
1156 | |
1157 ++num_workers; | |
1158 buf_size = 0; | |
1159 break; | |
1160 } | |
1161 } | |
1162 if(i >= num_defs) | |
1163 { | |
1164 strcpy(new_name_buf, "I don't know a worker with that name."); | |
1165 buf_size = strlen(new_name_buf); | |
1166 } | |
1167 } | |
1168 if(view_workerlist[num_workers-1].width <= (double)view_workerlist[num_workers-1].num_outputs * OUTPUT_SIZE) | |
1169 view_workerlist[num_workers-1].width = (double)view_workerlist[num_workers-1].num_outputs * (OUTPUT_SIZE*1.1); | |
1170 if(view_workerlist[num_workers-1].width <= (double)view_workerlist[num_workers-1].num_inputs * INPUT_SIZE) | |
1171 view_workerlist[num_workers-1].width = (double)view_workerlist[num_workers-1].num_inputs * (INPUT_SIZE*1.1); | |
1172 | |
1173 | |
1174 break; | |
1175 } | |
1176 | |
1177 case WM_LBUTTONDOWN: | |
1178 { | |
1179 mouse_leftstart_down=mouse_left_down=TRUE; | |
1180 mouse_lastx = mouse_curx = GET_X_LPARAM(lParam); | |
1181 mouse_lasty = mouse_cury = 480-GET_Y_LPARAM(lParam); | |
1182 checked_mouse_pos=FALSE; | |
1183 return 0; | |
1184 } | |
1185 case WM_LBUTTONUP: | |
1186 { | |
1187 mouse_leftstart_down=mouse_left_down=FALSE; | |
1188 return 0; | |
1189 } | |
1190 case WM_MOUSEMOVE: | |
1191 { | |
1192 //If no one has checked the mouse position vars, this could cause bugs | |
1193 if(checked_mouse_pos) | |
1194 { | |
1195 mouse_lastx = mouse_curx; | |
1196 mouse_lasty = mouse_cury; | |
1197 } | |
1198 mouse_curx = GET_X_LPARAM(lParam); | |
1199 mouse_cury = 480-GET_Y_LPARAM(lParam); | |
1200 checked_mouse_pos=FALSE; | |
1201 return 0; | |
1202 } | |
1203 case WM_CHAR: | |
1204 { | |
1205 if(wParam >= 0x20) | |
1206 { | |
1207 new_name_buf[buf_size++]=wParam&0xFF; | |
1208 } | |
1209 return 0; | |
1210 } | |
1211 | |
1212 } | |
1213 // Pass All Unhandled Messages To DefWindowProc | |
1214 return DefWindowProc(hWnd,uMsg,wParam,lParam); | |
1215 } | |
1216 | |
1217 VOID CALLBACK DoFrame(HWND myhWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) | |
1218 { | |
1219 if(active) | |
1220 { | |
1221 DrawGLScene(); // Draw The Scene | |
1222 SwapBuffers(hDC); // Swap Buffers (Double Buffering) | |
1223 } | |
1224 } | |
1225 | |
1226 int WINAPI WinMain( HINSTANCE hInstance, // Instance | |
1227 HINSTANCE hPrevInstance, // Previous Instance | |
1228 LPSTR lpCmdLine, // Command Line Parameters | |
1229 int nCmdShow) // Window Show State | |
1230 { | |
1231 MSG msg; // Windows Message Structure | |
1232 BOOL done=FALSE; // Bool Variable To Exit Loop | |
1233 // Ask The User Which Screen Mode They Prefer | |
1234 if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO) | |
1235 { | |
1236 fullscreen=FALSE; // Windowed Mode | |
1237 } | |
1238 // Create Our OpenGL Window | |
1239 if (!CreateGLWindow("Visuality",640,480,16,fullscreen)) | |
1240 { | |
1241 return 0; // Quit If Window Was Not Created | |
1242 } | |
1243 | |
1244 initworkers(); | |
1245 view_workerlist = deflist[0].implement_func->workerlist; | |
1246 view_wirelist = deflist[0].implement_func->wirelist; | |
1247 | |
1248 | |
1249 outfile = fopen("output.txt", "w"); | |
1250 | |
1251 SetTimer(hWnd, 1, 17, DoFrame); | |
1252 | |
1253 | |
1254 while(!done) // Loop That Runs Until done=TRUE | |
1255 { | |
1256 if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? | |
1257 { | |
1258 if (msg.message==WM_QUIT) // Have We Received A Quit Message? | |
1259 { | |
1260 done=TRUE; // If So done=TRUE | |
1261 } | |
1262 else // If Not, Deal With Window Messages | |
1263 { | |
1264 TranslateMessage(&msg); // Translate The Message | |
1265 DispatchMessage(&msg); // Dispatch The Message | |
1266 } | |
1267 } | |
1268 // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() | |
1269 if (active) // Program Active? | |
1270 { | |
1271 if (keys[VK_ESCAPE]) // Was ESC Pressed? | |
1272 { | |
1273 done=TRUE; // ESC Signalled A Quit | |
1274 } | |
1275 else // Not Time To Quit, Update Screen | |
1276 { | |
1277 | |
1278 | |
1279 } | |
1280 | |
1281 if (keys[VK_F1]) // Is F1 Being Pressed? | |
1282 { | |
1283 keys[VK_F1]=FALSE; // If So Make Key FALSE | |
1284 KillTimer(hWnd, 1); | |
1285 KillGLWindow(); // Kill Our Current Window | |
1286 fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode | |
1287 // Recreate Our OpenGL Window | |
1288 if (!CreateGLWindow("Rini's Love Quest",640,480,16,fullscreen)) | |
1289 { | |
1290 return 0; // Quit If Window Was Not Created | |
1291 } | |
1292 SetTimer(hWnd, 1, 17, DoFrame); | |
1293 } | |
1294 } | |
1295 Sleep(0); | |
1296 } | |
1297 KillTimer(hWnd, 1); | |
1298 | |
1299 // Shutdown | |
1300 KillGLWindow(); // Kill The Window | |
1301 return (msg.wParam); // Exit The Program | |
1302 } |