]> icculus.org git repositories - taylor/freespace2.git/blob - src/nebedit/nebedit.cpp
merge in updated platform code
[taylor/freespace2.git] / src / nebedit / nebedit.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/nebedit/NebEdit.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Program to edit nebulas in 2d
16  *
17  * $Log$
18  * Revision 1.3  2002/06/09 04:41:23  relnev
19  * added copyright header
20  *
21  * Revision 1.2  2002/05/07 03:16:47  theoddone33
22  * The Great Newline Fix
23  *
24  * Revision 1.1.1.1  2002/05/03 03:28:10  root
25  * Initial import.
26  *
27  * 
28  * 7     7/15/99 3:07p Dave
29  * 32 bit detection support. Mouse coord commandline.
30  * 
31  * 6     5/19/99 4:07p Dave
32  * Moved versioning code into a nice isolated common place. Fixed up
33  * updating code on the pxo screen. Fixed several stub problems.
34  * 
35  * 5     1/08/99 2:06p Dave
36  * Fixed pfoview for software mode.
37  * 
38  * 4     12/18/98 1:14a Dave
39  * Rough 1024x768 support for Direct3D. Proper detection and usage through
40  * the launcher.
41  * 
42  * 3     11/13/98 2:32p Dave
43  * Improved commandline parsing for exe pathname.
44  * 
45  * 2     10/24/98 9:51p Dave
46  * 
47  * 1     10/24/98 9:39p Dave
48  * 
49  * 13    4/13/98 10:11a John
50  * Made timer functions thread safe.  Made timer_init be called in all
51  * projects.
52  * 
53  * 12    3/23/98 1:35p Sandeep
54  * 
55  * 11    3/05/98 11:15p Hoffoss
56  * Changed non-game key checking to use game_check_key() instead of
57  * game_poll().
58  * 
59  * 10    12/29/97 5:10p Allender
60  * fixed problems with speed not being reported properly in multiplayer
61  * games.  Made read_flying_controls take frametime as a parameter.  More
62  * ship/weapon select stuff
63  * 
64  * 9     12/04/97 3:47p John
65  * Made joystick move mouse cursor
66  * 
67  * 8     11/21/97 11:32a John
68  * Added nebulas.   Fixed some warpout bugs.
69  * 
70  * 7     11/19/97 10:15p Adam
71  * upped maxtris to 200
72  * 
73  * 6     11/19/97 4:28p Sandeep
74  * added poly/vert counter
75  * 
76  * 5     11/19/97 1:59p Sandeep
77  * Added multiple vertex editing mode
78  * 
79  * 4     11/16/97 2:29p John
80  * added versioning to nebulas; put nebula code into freespace.
81  * 
82  * 3     11/13/97 12:04p Sandeep
83  * Added face editing support, deletion support, and a saving and loading
84  * stuff.
85  * 
86  * 2     11/10/97 9:59p John
87  * some tweaking
88  * 
89  * 1     11/10/97 9:42p John
90  *
91  * $NoKeywords: $
92  */
93
94
95 #include "wx/wxprec.h"
96
97 #ifndef WX_PRECOMP
98 #include "wx/wx.h"
99 #endif
100
101 #include "wx/filedlg.h"
102
103 #include "pstypes.h"
104 #include "2d.h"
105 #include "3d.h"
106 #include "key.h"
107 #include "palman.h"
108 #include "bmpman.h"
109 #include "timer.h"
110 #include "floating.h"
111 #include "osapi.h"
112 #include "cfile.h"
113 #include "linklist.h"
114 #include "lighting.h"
115 #include "mouse.h"
116 #include "vecmat.h"
117 #include "physics.h"
118 #include "model.h"
119 #include "font.h"
120 #include "cmdline.h"
121 #include "cfilesystem.h"
122
123
124 #define SCREEN_W        640     
125 #define SCREEN_H        480
126
127 vector ViewerPos;
128 matrix ViewerOrient;
129 matrix ModelOrient;
130 vector ModelPos;
131 physics_info ViewerPhysics;
132 float ViewerZoom = 1.0f;
133
134 int test_model = -1;
135 int Fred_running = 0;
136 int Pofview_running = 0;
137 float flFrametime = 0.0f;
138
139 int Font1 = -1;
140
141 color color_green;
142
143 vector Global_light_world = { { { 0.208758f, -0.688253f, -0.694782f } } };
144
145 // nebula stuff
146
147 #define NEB_W 6
148 #define NEB_H 6
149 #define MAX_TRIS 200
150 #define MAX_POINTS 300
151
152 int neb_w = 0, neb_h = 0;
153
154 int nebula_inited = 0;
155 int num_pts = 0;
156
157 int x[MAX_POINTS], y[MAX_POINTS], l[MAX_POINTS];
158 float scale_factor = 1.0f;
159
160 int num_tris = 0;
161 int tri[MAX_TRIS][3];
162
163 color nebula_color;
164
165 int Mouse_x, Mouse_y;
166 int Current_point;
167 bool Selected[MAX_POINTS];
168 bool Sel_mode = false;   // false = 1 point at a time, true = select multiple points
169 int Current_face;
170
171 int View_mode = 0;      // 0 = 2d editor, 1 = 3d viewer
172
173 int Vert_mode = 0;   // 0 = Move vertices/Add vertices, 2 = Move face/Add face
174
175 int Which_vert = 0;  // Current vertex of the faceadd
176
177 int Orig_pos_x;
178 int Orig_pos_y;
179 int End_pos_x;
180 int End_pos_y;
181 bool Draw_sel_box = false;
182
183 int Neb_created = 0;
184
185 int Nebedit_running = 1;
186
187 extern void project_2d_onto_sphere(vector *, float, float);
188
189 class NebeditApp: public wxApp
190 {
191 public:
192         virtual bool OnInit();
193 };
194
195 bool NebeditApp::OnInit()
196 {
197         return false;
198 }
199
200
201 void create_default_neb()
202 {
203         int i,j;
204         neb_w = NEB_W;
205         neb_h = NEB_H;
206         num_pts = neb_w*neb_h;
207
208         for (j=0; j<neb_h; j++ )        {
209                 for (i=0; i<neb_w; i++ )        {
210                         x[i+j*neb_w] = ((i+1)*SCREEN_W)/(neb_w+2);
211                         y[i+j*neb_w] = ((j+3)*SCREEN_H)/(neb_h+6);
212                         if ( (j==0) || (j==neb_h-1))    {
213                                 l[i+j*neb_w] = 0;
214                         } else {
215                                 l[i+j*neb_w] = 31;
216                         }
217                 }
218         }
219
220         num_tris = 0;
221
222         for (j=0; j<neb_h-1; j++ )      {
223                 for (i=0; i<neb_w-1; i++ )      {
224                         tri[num_tris][0] = i+neb_w*j;
225                         tri[num_tris][1] = i+neb_w*j+1;
226                         tri[num_tris][2] = i+neb_w*(j+1)+1;
227                         num_tris++;
228
229                         tri[num_tris][0] = i+neb_w*j;
230                         tri[num_tris][1] = i+neb_w*(j+1)+1;
231                         tri[num_tris][2] = i+neb_w*(j+1);
232                         num_tris++;
233                 }
234         }
235
236         Neb_created = 1;
237 }
238
239 #define NEBULA_FILE_ID "NEBU"                   
240 #define NEBULA_MAJOR_VERSION 1          // Can be 1-?
241 #define NEBULA_MINOR_VERSION 0          // Can be 0-99
242
243 void save_nebula_sub(const char *filename)
244 {
245         CFILE *fp;
246         float xf, yf;
247         int version;
248         int i;
249
250         fp = cfopen(filename, "wb");
251
252         if ( !fp )      {
253                 return;
254         }
255
256         // ID of NEBU
257         cfwrite( "NEBU", 4, 1, fp );
258         version = NEBULA_MAJOR_VERSION*100+NEBULA_MINOR_VERSION;
259         cfwrite_int(version, fp);
260         cfwrite_int(num_pts, fp);
261         cfwrite_int(num_tris, fp);
262
263         for (i=0; i<num_pts; i++ )      {
264                 xf = INTEL_FLOAT(float(x[i])/640.0f);
265                 yf = INTEL_FLOAT(float(y[i])/480.0f);
266                 cfwrite_float(xf, fp);
267                 cfwrite_float(yf, fp);
268                 cfwrite_int(l[i], fp);
269         }
270
271         for (i=0; i<num_tris; i++ )     {
272                 cfwrite_int(tri[i][0], fp);
273                 cfwrite_int(tri[i][1], fp);
274                 cfwrite_int(tri[i][2], fp);
275         }
276
277         cfclose(fp);
278 }
279
280 // returns 0 if failed
281 int load_nebula_sub(const char *filename)
282 {
283         CFILE *fp;
284         char id[16];
285         int version, major, minor;
286
287         fp = cfopen(filename, "rb");
288
289         if ( !fp )      {
290                 return 0;
291         }
292
293         // ID of NEBU
294         cfread( id, 4, 1, fp );
295         if ( strncmp( id, NEBULA_FILE_ID, 4))   {
296                 mprintf(( "Not a valid nebula file.\n" ));
297                 return 0;
298         }
299
300         version = cfread_int(fp);
301         major = version / 100;
302         minor = version % 100;
303
304         if ( (major != NEBULA_MAJOR_VERSION) && (minor != NEBULA_MINOR_VERSION) ) {
305                 mprintf(( "An out of date nebula file.\n" ));
306                 return 0;
307         }
308
309         num_pts = cfread_int(fp);
310         SDL_assert( num_pts < MAX_POINTS );
311         num_tris = cfread_int(fp);
312         SDL_assert( num_tris < MAX_TRIS );
313
314         for (int i=0; i<num_pts; i++ )  {
315                 x[i] = fl2i(cfread_float(fp) * 640.0f);
316                 y[i] = fl2i(cfread_float(fp) * 480.0f);
317                 l[i] = cfread_int(fp);
318         }
319
320         for (int i=0; i<num_tris; i++ ) {
321                 tri[i][0] = cfread_int(fp);
322                 tri[i][1] = cfread_int(fp);
323                 tri[i][2] = cfread_int(fp);
324         }
325
326         cfclose(fp);
327
328         return 1;
329 }
330
331 void nebedit_close()
332 {
333         char a_path[MAX_PATH_LEN];
334
335         cf_create_default_path_string(a_path, CF_TYPE_CACHE, "autosaved.neb");
336
337         save_nebula_sub( a_path );
338 }
339
340 void save_nebula()
341 {
342         wxFileDialog saveFileDialog(NULL, _("Save Nebula File"), wxEmptyString,
343                                                                 wxEmptyString, _("Nebula Files (*.neb)|*.neb"),
344                                                                 wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
345
346         if (saveFileDialog.ShowModal() == wxID_OK) {
347                 save_nebula_sub(saveFileDialog.GetPath().ToAscii());
348         }
349 }
350
351 void load_nebula()
352 {
353         int create_default = 1;
354
355         wxFileDialog openFileDialog(NULL, _("Open Nebula File"), wxEmptyString,
356                                                                 wxEmptyString, _("Nebula Files (*.neb)|*.neb"),
357                                                                 wxFD_OPEN|wxFD_FILE_MUST_EXIST);
358
359         if (openFileDialog.ShowModal() == wxID_OK) {
360                 create_default = !load_nebula_sub(openFileDialog.GetPath().ToAscii());
361         }
362
363         if ( create_default )   {
364                 create_default_neb();
365         }
366
367         Neb_created = 1;
368 }
369
370 void nebula_init()
371 {
372         if ( nebula_inited ) return;
373         memset(Selected, 0, sizeof(bool)*MAX_POINTS);
374         nebula_inited++;
375
376         create_default_neb();   
377         gr_init_alphacolor( &nebula_color, 0, 255, 0, 255, AC_TYPE_HUD );
378
379         return;
380 }
381
382 void draw_tri_2d( int i, int j, int k )
383 {
384         int index[3];
385
386         index[0] = i;
387         index[1] = j;
388         index[2] = k;
389
390         vertex va, vb, vc;
391         vertex * verts[3] = { &va, &vb, &vc };
392 //      gr_zbuffering = 0;
393         for (int v=0; v<3; v++ )        {
394                 verts[v]->sx = i2fl(x[index[v]]);   
395                 verts[v]->sy = i2fl(y[index[v]]);
396                 verts[v]->u = 0.0f;
397                 verts[v]->v = 0.0f;
398                 verts[v]->sw = 1.0f; 
399                 verts[v]->b = (ubyte)(i2fl(l[index[v]]*255)/31.0f);
400         }
401
402 //      gr_set_color( 0, 0, 0 );
403         gr_tmapper(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
404
405         if ( !key_pressed(SDLK_LSHIFT) )        {
406                 gr_set_color(100,100,100);
407                 gr_line( x[i], y[i], x[j], y[j] );
408                 gr_line( x[j], y[j], x[k], y[k] );
409                 gr_line( x[k], y[k], x[i], y[i] );
410         }
411 }
412
413 void nebula_draw_2d()
414 {
415         int i;
416         
417         for (i=0; i<num_tris; i++ )     {
418                 draw_tri_2d( tri[i][0], tri[i][1], tri[i][2] );
419         }               
420
421                 gr_set_color(100,0,0);
422                 for (i=0; i<num_pts; i++ )      {
423                         gr_circle( x[i], y[i], 4 );
424                 }
425                 if (Sel_mode == 1) { // multiple selection
426                         if (Draw_sel_box) {
427                                 gr_set_color(200,0,200);
428                                 gr_line(Orig_pos_x, Orig_pos_y, Orig_pos_x, End_pos_y);
429                                 gr_line(Orig_pos_x, End_pos_y, End_pos_x, End_pos_y);
430                                 gr_line(End_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
431                                 gr_line(End_pos_x, Orig_pos_y, Orig_pos_x, Orig_pos_y);
432                         } else {
433                                 gr_set_color(0,100,0);
434                                 for (int i=0;i<num_pts;i++)
435                                         if (Selected[i]) gr_circle( x[i], y[i], 5);
436                         }
437                 } else if ((Vert_mode==0)&&(Current_point>-1)) {
438                         gr_set_color(0,100,0);
439                         gr_circle( x[Current_point], y[Current_point], 5);
440                 } else if (Vert_mode == 1) {
441                         for (i=0;i<Which_vert;i++) {
442                                 gr_set_color(0,0,100);
443                                 gr_circle( x[tri[num_tris][i]], y[tri[num_tris][i]], 6);
444                         }
445                         gr_set_color(200,200,0);
446                         if (Current_face>-1) {
447                                 gr_line(x[tri[Current_face][0]], y[tri[Current_face][0]], 
448                                                 x[tri[Current_face][1]], y[tri[Current_face][1]]);
449                                 gr_line(x[tri[Current_face][1]], y[tri[Current_face][1]], 
450                                                 x[tri[Current_face][2]], y[tri[Current_face][2]]);
451                                 gr_line(x[tri[Current_face][2]], y[tri[Current_face][2]], 
452                                                 x[tri[Current_face][0]], y[tri[Current_face][0]]);
453                         }
454                 }
455 }
456
457
458 void draw_tri_3d( int i, int j, int k )
459 {
460         int index[3];
461
462         index[2] = k;
463         index[1] = j;
464         index[0] = i;
465
466         vertex va, vb, vc;
467         vertex * verts[3] = { &va, &vb, &vc };
468         //gr_zbuffering = 0;
469         for (int v=0; v<3; v++ )        {
470                 vector tmp;
471                 
472                 project_2d_onto_sphere( &tmp, 1.0f - i2fl(x[index[v]])/640.0f, i2fl(y[index[v]])/480.0f );
473         
474                 vm_vec_scale( &tmp, 10.0f );
475
476                 g3_rotate_faraway_vertex( verts[v], &tmp );
477                 //g3_rotate_vertex( verts[v], &tmp );
478                 g3_project_vertex( verts[v] );
479
480                 verts[v]->b = (ubyte)(i2fl(l[index[v]]*255)/31.0f);
481         }
482
483         //gr_zbuffering = 0;
484         //gr_set_color_fast( &nebula_color );
485         //gr_set_color( 0, 0, 0 );
486         g3_draw_poly(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
487 }
488
489 void nebula_draw_3d()
490 {
491         int i;
492         for (i=0; i<num_tris; i++ )     {
493                 draw_tri_3d( tri[i][0], tri[i][1], tri[i][2] );
494         }               
495 }
496
497 void render_frame()
498 {
499         gr_reset_clip();
500
501         gr_set_color(0,0,0);            // set color to black
502         gr_rect(0,0,SCREEN_W,SCREEN_H); // clear screen
503
504         light_reset();
505         light_add_directional( &Global_light_world, 0.1f, 1.0f, 1.0f, 1.0f );
506
507         g3_start_frame(1);
508
509         g3_set_view_matrix(&ViewerPos, &ViewerOrient,ViewerZoom);
510
511         if ( View_mode == 0 )   {
512                 nebula_draw_2d();
513
514                 gr_set_font(Font1);
515                 gr_set_color_fast( &color_green );
516                 gr_printf(10,10,"Nebula Editor. Mode :");
517                 if (Sel_mode ==1) {
518                                 gr_printf(180, 10, "Multiple Vertex Selection Editing");
519                 } else if (Vert_mode ==0 ) {
520                         gr_printf(180,10,"Vertex Editing");
521                         if(Current_point >= 0){
522                                 gr_printf(180, 20, "Current vertex intensity : %d\n", l[Current_point]);
523                         }
524                 } else if (Vert_mode ==1) {
525                         gr_printf(180,10,"Face Editing");
526                 }
527                 char blah[255];
528                 gr_printf(20,30,"# Points:");
529                 gr_printf(100,30, SDL_itoa(num_pts, blah, 10));
530                 gr_printf(220,30,"# Polys:");
531                 gr_printf(300,30, SDL_itoa(num_tris, blah, 10));
532         } else {
533                 nebula_draw_3d();
534                 model_render( test_model, &ModelOrient, &ModelPos );
535         }
536
537         g3_end_frame();
538
539         gr_flip();
540 }
541
542 int get_closest(int mx, int my)
543 {
544         int i, closest = -1, cval = 100000;
545
546         for (i=0; i<num_pts; i++ )      {
547                 int dist;
548                 int dx, dy;
549                 
550                 dx = x[i] - mx;
551                 dy = y[i] - my;
552                 
553                 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
554
555                 if ( dist < cval )      {
556                         cval = dist;
557                         closest = i;
558                 }
559         }
560
561         return closest;
562 }
563
564 int get_closest_face(int mx, int my)
565 {
566         int i, closest = -1, cval = 100000;
567
568         for (i=0; i<num_tris; i++ )     {
569                 int dist;
570                 int dx, dy;
571                 
572                 dx = x[tri[i][0]] - mx;
573                 dy = y[tri[i][0]] - my;
574
575                 dx += x[tri[i][1]] - mx;
576                 dy += y[tri[i][1]] - my;
577                 
578                 dx += x[tri[i][2]] - mx;
579                 dy += y[tri[i][2]] - my;
580
581                 dist = fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
582 /*
583                 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
584
585                 dx = x[tri[i][2]] - mx;
586                 dy = y[tri[i][2]] - my;
587                 dist += fl2i(fl_sqrt( i2fl((dx*dx)+(dy*dy)) ));
588 */
589
590                 if ( dist < cval )      {
591                         cval = dist;
592                         closest = i;
593                 }
594         }
595
596         return closest;
597 }
598
599 void delete_face(int i)
600 {
601         for (int j=i;j<num_tris;j++) {
602                 memcpy(tri[j], tri[j+1], sizeof(int)*3);
603         }
604         num_tris--;
605 }
606
607 void delete_vert(int i)
608 {
609         int j;
610
611         for (j=0;j<num_tris;j++) {
612                 if ((tri[j][0]==i)||(tri[j][1]==i)||(tri[j][2]==i)) {
613                         delete_face(j);
614                         j=0;
615                 }
616         }
617         for (j=0;j<num_tris;j++) {
618                 if (tri[j][0]>i) tri[j][0]--;
619                 if (tri[j][1]>i) tri[j][1]--;
620                 if (tri[j][2]>i) tri[j][2]--;
621         }
622         for (j=i;j<num_pts;j++) {
623                 x[j] = x[j+1];
624                 y[j] = y[j+1];
625                 l[j] = l[j+1];
626         }
627         num_pts--;
628 }
629
630 int add_vert(int mx, int my)
631 {
632         SDL_assert(num_pts<300);
633         x[num_pts] = mx;
634         y[num_pts] = my;
635         l[num_pts] = 0;
636         num_pts++;
637         return num_pts-1;
638 }
639
640 void select_by_box(int x1, int y1, int x2, int y2)
641 {
642         if (x1>x2) {
643                 int temp = x1;
644                 x1 = x2;
645                 x2 = temp;
646         }
647         if (y1>y2) {
648                 int temp = y1;
649                 y1 = y2;
650                 y2 = temp;
651         }
652         for (int i=0;i<num_pts;i++) {
653                 if ((x[i]<=x2) && (x[i]>=x1) &&
654                          (y[i]<=y2) && (y[i]>=y1)) {
655                         Selected[i] = true;
656                 }
657         }
658 }
659
660 void sphericalize_nebula()
661 {
662         int idx1, idx2;
663         int px = SCREEN_W / (neb_w - 1);
664         int py = SCREEN_H / (neb_h - 1);
665
666         // flatten out the nebula so that it covers the entire sphere evenly
667         for(idx1=0; idx1<neb_w; idx1++){
668                 for(idx2=0; idx2<neb_h; idx2++){
669                         x[idx1+idx2*neb_w] = px * idx1;
670                         y[idx1+idx2*neb_w] = py * idx2;
671                 }
672         }
673 }
674
675 void controls_read_all(control_info * ci, float sim_time )
676 {
677         float kh;
678
679         {
680                 float temp = ci->heading;
681                 float temp1 = ci->pitch;
682                 memset( ci, 0, sizeof(control_info) );
683                 ci->heading = temp;
684                 ci->pitch = temp1;
685         }
686
687         // From keyboard...
688         kh = (key_down_timef(SDLK_KP_6) - key_down_timef(SDLK_KP_4))/8.0f;
689         if (kh == 0.0f)
690                 ci->heading = 0.0f;
691         else if (kh > 0.0f) {
692                 if (ci->heading < 0.0f)
693                         ci->heading = 0.0f;
694         } else // kh < 0
695                 if (ci->heading > 0.0f)
696                         ci->heading = 0.0f;
697         ci->heading += kh;
698
699         kh = (key_down_timef(SDLK_KP_8) - key_down_timef(SDLK_KP_2))/8.0f;
700         if (kh == 0.0f)
701                 ci->pitch = 0.0f;
702         else if (kh > 0.0f) {
703                 if (ci->pitch < 0.0f)
704                         ci->pitch = 0.0f;
705         } else // kh < 0
706                 if (ci->pitch > 0.0f)
707                         ci->pitch = 0.0f;
708         ci->pitch += kh;
709
710         ci->bank = (key_down_timef(SDLK_KP_7) - key_down_timef(SDLK_KP_9))*.75f;
711         ci->forward = key_down_timef(SDLK_a) - key_down_timef(SDLK_z);
712         ci->sideways = key_down_timef(SDLK_KP_3) - key_down_timef(SDLK_KP_1);
713         ci->vertical = key_down_timef(SDLK_KP_PLUS) - key_down_timef(SDLK_KP_ENTER);
714 }
715
716 int check_keys()
717 {
718         int k;
719
720         while( (k = key_inkey()) != 0 ) {
721 //mprintf(( "Key = %x\n", k ));
722                 if ( k == SDLK_ESCAPE ) {
723                         return 1;
724                 }
725
726                 switch( k )     {
727                 case SDLK_RETURN:
728                         Sel_mode = false;
729                         Vert_mode = !Vert_mode;
730                         Which_vert = 0;
731                         break;
732
733                 case SDLK_DELETE:
734                         if (Sel_mode) break;
735                         if (Vert_mode==1) delete_face(Current_face);
736                         else if (Vert_mode==0) {
737                                 delete_vert(Current_point);
738                         }
739                         break;
740
741                 case SDLK_MINUS:
742                         scale_factor -= 0.05f;
743                         mprintf(( "Scale = %.1f\n", scale_factor ));
744                         break;
745
746
747                 case SDLK_EQUALS:
748                         scale_factor += 0.05f;
749                         mprintf(( "Scale = %.1f\n", scale_factor ));
750                         break;
751
752                 case SDLK_INSERT:
753                         Sel_mode = !Sel_mode;
754                         break;
755
756                 case SDLK_SPACE:
757                         View_mode = !View_mode;
758                         break;
759
760                 case SDLK_COMMA:
761                         if (Sel_mode) {
762                                 int i;
763                                 for (i=0;i<num_pts;i++) if (Selected[i]) {
764                                         if ( l[i] > 0 ) {
765                                                 l[i]--;
766                                         }
767                                 }
768                         } else if (Vert_mode==0) {
769                                 if ( Current_point > -1 )       {
770                                         if ( l[Current_point] > 0 )     {
771                                                 l[Current_point]--;
772                                         }
773                                 }
774                         } else if (Vert_mode ==1) {
775                                 if ( l[tri[Current_face][0]] > 0 )      {
776                                                 l[tri[Current_face][0]]--;
777                                         }
778                                 if ( l[tri[Current_face][1]] > 0 )      {
779                                                 l[tri[Current_face][1]]--;
780                                         }
781                                 if ( l[tri[Current_face][2]] > 0 )      {
782                                                 l[tri[Current_face][2]]--;
783                                         }
784                         }
785                         break;
786
787                 case SDLK_PERIOD:
788                         if (Sel_mode) {
789                                 int i;
790                                 for (i=0;i<num_pts;i++) if (Selected[i]) {
791                                         if ( l[i] < 31 )        {
792                                                 l[i]++;
793                                         }
794                                 }
795                         } else if (Vert_mode==0) {
796                                 if ( Current_point > -1 )       {
797                                         if ( l[Current_point] < 31 )    {
798                                                 l[Current_point]++;
799                                         }
800                                 }
801                         } else if (Vert_mode ==1) {
802                                 if ( l[tri[Current_face][0]] <31 )      {
803                                                 l[tri[Current_face][0]]++;
804                                         }
805                                 if ( l[tri[Current_face][1]] <31)       {
806                                                 l[tri[Current_face][1]]++;
807                                         }
808                                 if ( l[tri[Current_face][2]] <31 )      {
809                                                 l[tri[Current_face][2]]++;
810                                         }
811                         }
812                         
813                         break;          
814
815                 case SDLK_F5:
816                         save_nebula();
817                         break;
818                 case SDLK_F7:
819                         load_nebula();
820                         break;
821
822                 case SDLK_BACKSPACE:
823                         sphericalize_nebula();
824                         break;
825                 }
826         }
827         return 0;
828 }
829
830 void os_close()
831 {
832         exit(1);
833 }
834
835 int newtri[3];
836
837 bool mdflag = false;
838
839 extern "C"
840 int main(int argc, char *argv[])
841 {
842         int i;
843         fix t1, t2;
844         control_info ci;
845         float speed = 20.0f;            // how fast viewer moves        
846
847         // setup the fred exe directory so CFILE can init properly
848         /*
849         char *c = GetCommandLine();
850         SDL_assert(c != NULL);
851         char *tok = strtok(c, " ");
852         SDL_assert(tok != NULL);        
853         */
854
855         Cmdline_window = 1; // always windowed
856
857         timer_init();
858         // cfile_init(tok);
859         cfile_init();
860         os_init( "NebEdit", "NebEdit" );        //SCREEN_W, SCREEN_H );
861         os_set_title("NebEdit");
862         gr_init(GR_640, GR_OPENGL, 16);
863         palette_load_table( "gamepalette1-01.pcx" );
864         key_init();
865         mouse_init();
866         SDL_ShowCursor(1);
867         Font1 = gr_init_font( "font01.vf" );
868         gr_init_alphacolor( &color_green, 0,255,0,255,AC_TYPE_HUD );
869
870         test_model = model_load( "fighter01.pof", 0, NULL );
871
872         physics_init( &ViewerPhysics );
873         ViewerPhysics.flags |= PF_ACCELERATES | PF_SLIDE_ENABLED;
874
875         ViewerPhysics.max_vel.xyz.x = 2.0f*speed;               //sideways
876         ViewerPhysics.max_vel.xyz.y = 2.0f*speed;               //up/down
877         ViewerPhysics.max_vel.xyz.z = 2.0f*speed;               //forward
878         ViewerPhysics.max_rear_vel = 2.0f*speed;        //backward -- controlled seperately
879         
880         memset( &ci, 0, sizeof(control_info) );
881
882         ModelOrient = vmd_identity_matrix;
883         ModelPos.xyz.x=0.0f; ModelPos.xyz.y = 0.0f; ModelPos.xyz.z = 0.0f;
884
885         ViewerOrient = vmd_identity_matrix;
886         ViewerPos.xyz.x=0.0f; ViewerPos.xyz.y = 0.0f; ViewerPos.xyz.z = -50.0f;
887
888         flFrametime = 0.033f;
889
890         t1 = timer_get_fixed_seconds();
891
892         nebula_init();
893
894         wxApp::SetInstance( new NebeditApp() );
895
896         wxEntryStart(argc, argv);
897
898         //bool some_selected = false;
899
900         while(1)        {
901                 os_poll();
902
903                 /*some_selected = false;
904                 if (Sel_mode) {
905                         for (i=0;i<num_pts;i++) {
906                                 if (Selected[i]) {
907                                         some_selected = true;
908                                         break;
909                                 }
910                         }
911                 }*/
912
913                 mouse_get_pos( &Mouse_x, &Mouse_y );
914
915                 if (Current_face==-1) {
916                         Current_face = get_closest_face(Mouse_x, Mouse_y);
917                 }
918                 
919                 if ( check_keys() ){
920                         break;
921                 }
922
923                 if ( Sel_mode==1 ) {
924                         // Special mouse handlers for multiple selmode
925                         if (mouse_down_count(LOWEST_MOUSE_BUTTON)) {
926                                 Orig_pos_x = Mouse_x;
927                                 Orig_pos_y = Mouse_y;
928                         }
929                         if (mouse_down(LOWEST_MOUSE_BUTTON)) {
930                                 for (i=0;i<num_pts;i++) {
931                                         if (Selected[i]) {
932                                                 x[i]+=Mouse_x - Orig_pos_x;
933                                                 y[i]+=Mouse_y - Orig_pos_y;
934                                         }
935                                 }
936                                 Orig_pos_x = Mouse_x;
937                                 Orig_pos_y = Mouse_y;
938                         }
939                         if (mouse_down_count(MOUSE_RIGHT_BUTTON)) {
940                                 Orig_pos_x = Mouse_x;
941                                 Orig_pos_y = Mouse_y;
942                                 for (i=0;i<num_pts;i++) {
943                                         Selected[i] = FALSE;
944                                 }
945                         }
946                         if (mouse_down(MOUSE_RIGHT_BUTTON)) {
947                                 Draw_sel_box = true;
948                                 End_pos_x = Mouse_x;
949                                 End_pos_y = Mouse_y;
950                         }
951                         if (mouse_up_count(MOUSE_RIGHT_BUTTON)) {
952                                 Draw_sel_box = false;
953                                 End_pos_x = Mouse_x;
954                                 End_pos_y = Mouse_y;
955                                 select_by_box(Orig_pos_x, Orig_pos_y, End_pos_x, End_pos_y);
956                         }
957
958                 } else {
959
960                         if ( mouse_down(LOWEST_MOUSE_BUTTON) )  {
961                                 if ( mdflag )   {
962                                         if (Vert_mode==0) {
963                                                 x[Current_point] = Mouse_x;
964                                                 y[Current_point] = Mouse_y;
965                                         } else if (Vert_mode==1) {
966                                                 x[tri[Current_face][0]] += Mouse_x - Orig_pos_x;
967                                                 y[tri[Current_face][0]] += Mouse_y - Orig_pos_y;
968                                                 x[tri[Current_face][1]] += Mouse_x - Orig_pos_x;
969                                                 y[tri[Current_face][1]] += Mouse_y - Orig_pos_y;
970                                                 x[tri[Current_face][2]] += Mouse_x - Orig_pos_x;
971                                                 y[tri[Current_face][2]] += Mouse_y - Orig_pos_y;
972                                                 Orig_pos_x = Mouse_x;
973                                                 Orig_pos_y = Mouse_y;
974                                         }
975                                 } else {
976                                         if (Vert_mode == 1) {
977                                                 Current_face = get_closest_face(Mouse_x, Mouse_y);
978                                                 Orig_pos_x = Mouse_x;
979                                                 Orig_pos_y = Mouse_y;
980                                         }
981                                         if (Vert_mode==0) {
982                                                 Current_point = get_closest(Mouse_x, Mouse_y);
983                                                 mouse_set_pos(x[Current_point], y[Current_point]);
984                                         }
985                                         mdflag = true;
986                                 }
987                         }
988                         if ( mouse_up_count(LOWEST_MOUSE_BUTTON)) {
989                                 //Current_point = -1;
990                                 //Current_face = -1;
991                                 mdflag = false;
992                         }
993
994                         if ( mouse_up_count(MOUSE_RIGHT_BUTTON) ) {
995                                 if (Vert_mode==0) {
996                                         Current_point = add_vert(Mouse_x, Mouse_y);
997                                 } else if (Vert_mode==1) {
998                                         if ((num_tris<MAX_TRIS-1)) {
999                                                 tri[num_tris][Which_vert] = get_closest(Mouse_x, Mouse_y);
1000                                                 Which_vert++;
1001                                                 if (Which_vert>2) {
1002                                                         Which_vert = 0;
1003                                                         num_tris++;
1004                                                 }
1005                                         }
1006                                 }
1007                         }
1008                 }
1009                 controls_read_all(&ci, flFrametime );
1010                 physics_read_flying_controls( &ViewerOrient, &ViewerPhysics, &ci, flFrametime );
1011                 physics_sim(&ViewerPos, &ViewerOrient, &ViewerPhysics, flFrametime );           
1012
1013                 render_frame();
1014
1015                 t2 = timer_get_fixed_seconds();
1016                 if ( t2 > t1 )  {
1017                         flFrametime = f2fl(t2 - t1);
1018                 }
1019
1020                 t1 = t2;
1021
1022                 SDL_Delay(10);
1023         }
1024
1025         nebedit_close();
1026
1027         wxEntryCleanup();
1028
1029         return 0;
1030 }
1031
1032 // Stub functions and variables.
1033 // These do nothing but are needed to prevent link errors.
1034 void demo_set_playback_filter() {}
1035
1036 void freespace_menu_background()
1037 {
1038         gr_reset_clip();
1039         gr_clear();
1040 }
1041
1042 int game_check_key()
1043 {
1044         return key_inkey();
1045 }
1046
1047 int game_poll()
1048 {
1049         return key_inkey();
1050 }
1051
1052 vector Camera_pos;
1053 vector Dead_player_last_vel;
1054 // end stubs
1055