]> icculus.org git repositories - divverent/netradiant.git/blob - contrib/gtkgensurf/gensurf.cpp
add missing EndBSPFile() call
[divverent/netradiant.git] / contrib / gtkgensurf / gensurf.cpp
1 /*
2 GenSurf plugin for GtkRadiant
3 Copyright (C) 2001 David Hyde, Loki software and qeradiant.com
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <gtk/gtk.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 /*
24 #include <string.h>
25 #include <tchar.h>
26 #include <math.h>
27 */
28 #include "gensurf.h"
29
30 char      gszAppDir[NAME_MAX];
31 char      gszCaption[64];
32 char      gszIni[NAME_MAX];
33 char      gszHelpFile[NAME_MAX];
34 char      gszMapFile[NAME_MAX];
35 char      gszVersion[64];
36 double    Amplitude;
37 double    Roughness;
38 double    TexOffset[2];
39 double    TexScale[2];
40 double    WaveLength;
41 double    Hll, Hur, Vll, Vur;
42 double    Z00, Z01, Z10, Z11;
43 ELEMENT   Vertex[(MAX_ROWS+1)*(MAX_ROWS+1)];
44 int       AddHints;
45 int       ArghRad2;
46 int       AutoOverwrite;
47 int       Decimate=0;
48 int                             SnapToGrid=0; // 0, or the grid size to snap to. // Hydra : snap to grid
49 int       FileAppend=0;
50 int       FixBorders;
51 int       HideBackFaces=0;
52 int       NH, NV;
53 int       NumVerticesSelected;
54 int       Plane;
55 int       Preview;
56 int       RandomSeed=1;
57 int       Skybox;
58 int       UseDetail;
59 int       UseLadder;
60 int       VertexMode=0;
61 int       WaveType;
62 int       gNumNodes=0;
63 int       gNumTris=0;
64 int       vid_x, vid_y;
65 int       view_x, view_y;
66 int       view_cx, view_cy;
67 int       UsePatches;
68 int       SlantAngle;
69 int       GimpHints;
70 int                             Antialiasing; // ^Fishman - Antializing for the preview window.
71 int                             AddTerrainKey; // ^Fishman - Add terrain key to func_group.
72 int                             SP; // ^Fishman - Snap to grid.
73
74 GtkWidget *g_pWnd;        // ghwnd;
75 GtkWidget *g_pRadiantWnd; // ghwnd_main;
76 /*HWND      ghwndAngles;
77 */GtkWidget *g_pWndPreview;
78 GtkWidget *g_pPreviewWidget;
79 MYBITMAP  gbmp;
80 NODE      *gNode=(NODE *)NULL;
81 TRI       *gTri=(TRI *)NULL;
82
83 int       Game;
84 bounding_box PlayerBox[NUMGAMES] = { {{-16., 16.}, {-16., 16.}, {-24., 32.}},    // Quake2
85                                      {{-16., 16.}, {-16., 16.}, {-36., 36.}},    // Half-Life
86                                      {{-16., 16.}, {-16., 16.}, {-32., 32.}},    // SiN
87                                      {{-16., 16.}, {-16., 16.}, {-24., 32.}},    // Heretic2 (guess)
88                                      {{-16., 16.}, {-16., 16.}, {-24., 32.}},    // KingPin (guess)
89                                      {{-30., 30.}, {-30., 30.}, {-10.,160.}},    // Genesis3D (no idea)
90                                      {{-16., 16.}, {-16., 16.}, {-24., 32.}}};   // Quake3 (not sure)
91 //char      gszOutputDir[NUMGAMES][NAME_MAX];
92 //char      gszTextureDir[NUMGAMES][NAME_MAX];
93 char      Texture[NUMGAMES][3][64];
94 //char      pakfile[NUMGAMES][NAME_MAX];
95 //char      lastpakfile[NUMGAMES][NAME_MAX];
96 //int       UsePak[NUMGAMES];
97 //char      GameDir[NUMGAMES][NAME_MAX];
98
99 char GameName[NUMGAMES][16] = {"Quake2", "Half-Life", "SiN", "Heretic2", "Kingpin", "Genesis3D", "Quake3" };
100
101
102 bool GenSurfInit ()
103 {
104   strcpy (gszVersion, "1.05");
105   strcpy (gszCaption, "GtkGenSurf");
106   if (strlen (gszVersion))
107   {
108     strcat (gszCaption, " v");
109     strcat (gszCaption, gszVersion);
110   }
111
112   strcpy (gszIni, g_FuncTable.m_pfnProfileGetDirectory ());
113   strcat (gszIni, "gensurf.ini");
114
115 /*if (g_FuncTable.m_pfnReadProjectKey != NULL)
116   {
117     char *basepath;
118
119     basepath = g_FuncTable.m_pfnReadProjectKey("basepath");
120     if (basepath)
121     {
122       g_strdown (basepath);
123       if (strstr(basepath,"baseq3"))
124         Game = QUAKE3;
125       else if (strstr (basepath,"baseq2"))
126         Game = QUAKE2;
127       else // Gotta have a game, might as well be Quake3
128         Game = QUAKE3;
129     }
130     else
131       Game = QUAKE3;
132   }
133   else */
134     Game = QUAKE3;
135
136
137   if (g_pWnd == NULL)
138     g_pWnd = create_main_dialog ();
139
140   ReadIniFile (gszIni);
141
142   return true;
143 }
144
145 // Reads default values
146
147 #define OPTS_SECTION "Options"
148
149 void ReadIniFile (const char *file)
150 {
151   char *Text;
152   float x1,x2,x3,x4;
153   int   i;
154
155   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "Amplitude", "");
156   if (strlen (Text))
157     Amplitude = atof (Text);
158   else
159     Amplitude = 128;
160
161   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "Roughness", "");
162   if (strlen (Text))
163     Roughness = atof (Text);
164   else
165     Roughness = 16;
166
167   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "WaveLength", "");
168   if (strlen (Text))
169     WaveLength = atof (Text);
170   else
171     WaveLength = 1024;
172
173   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "Extents", "");
174   if (strlen (Text))
175   {
176     sscanf(Text,"%f,%f,%f,%f",&x1,&x2,&x3,&x4);
177     Hll = x1;
178     Vll = x2;
179     Hur = x3;
180     Vur = x4;
181   }
182   else
183   {
184     Hll = -512;
185     Vll = -512;
186     Hur =  512;
187     Vur =  512;
188   }
189
190   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "CornerValues", "");
191   if (strlen (Text))
192   {
193     sscanf(Text,"%f,%f,%f,%f",&x1,&x2,&x3,&x4);
194     Z00 = x1;
195     Z01 = x2;
196     Z10 = x3;
197     Z11 = x4;
198   }
199   else
200   {
201     Z00 = 0.;
202     Z01 = 0.;
203     Z10 = 0.;
204     Z11 = 0.;
205   }
206
207   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "TextureOffset", "");
208   if (strlen (Text))
209   {
210     sscanf(Text,"%f,%f",&x1,&x2);
211     TexOffset[0] = x1;
212     TexOffset[1] = x2;
213   }
214   else
215   {
216     TexOffset[0] = 0.;
217     TexOffset[1] = 0.;
218   }
219
220   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION,"TextureScale","");
221   if (strlen (Text))
222   {
223     sscanf(Text,"%f,%f",&x1,&x2);
224     TexScale[0] = x1;
225     TexScale[1] = x2;
226     if(TexScale[0] == 0.) TexScale[0] = 1.0;
227     if(TexScale[1] == 0.) TexScale[1] = 1.0;
228   }
229   else
230   {
231     TexScale[0] = 1.;
232     TexScale[1] = 1.;
233   }
234
235   NH = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"NH",8);
236   NH = max(1,min(NH,MAX_ROWS));
237   NV = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"NV",8);
238   NV = max(1,min(NV,MAX_ROWS));
239
240 //      Decimate   = GetPrivateProfileInt(OPTS_SECTION,"Decimate",0,file);
241 //      Decimate = max(0,min(Decimate,100));
242
243   AddHints                      = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"AddHints",0);
244   ArghRad2                      = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"ArghRad2",0);
245   AutoOverwrite = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"AutoOverwrite",0);
246   FixBorders            = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"FixBorders",1);
247   HideBackFaces = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"HideBackFaces",0);
248   Plane                                 = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"Plane",0);
249   Preview                               = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"Preview", 0);
250         Antialiasing    = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"Antialiasing",0); // ^Fishman - Antializing for the preview window.
251   RandomSeed            = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"RandomSeed",1);
252   Skybox                                = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"Skybox",0);
253   UseDetail                     = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"UseDetail",0);
254         AddTerrainKey   =       g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"AddTerrainKey",0); // ^Fishman - Add terrain key to func_group.
255   UseLadder                     = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"UseLadder",0);
256   WaveType                      = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"WaveType",0);
257   vid_x                                 = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"vid_x", 0);
258   vid_y                                 = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"vid_y", 0);
259   view_x                                = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"view_x",0);
260   view_y                                = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"view_y",0);
261   view_cx                               = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"view_cx",0);
262   view_cy                               = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"view_cy",0);
263
264   UsePatches            = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"UsePatches",0);
265
266   SlantAngle = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"SlantAngle",60);
267   GimpHints  = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"GimpHints",0);
268
269   for(i=0; i<NUMGAMES; i++)
270   {
271     //    strcpy (gszOutputDir[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"OutputDir",""));
272     strcpy (Texture[i][0], g_FuncTable.m_pfnProfileLoadString (file, GameName[i], "Texture", ""));
273     strcpy (Texture[i][1], g_FuncTable.m_pfnProfileLoadString (file, GameName[i], "Texture2", ""));
274     strcpy (Texture[i][2], g_FuncTable.m_pfnProfileLoadString (file, GameName[i], "Texture3", ""));
275     //    strcpy (gszTextureDir[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"TextureDir",""));
276     //    UsePak[i] = GetPrivateProfileInt(GameName[i],"UsePak",0);
277     //    strcpy (pakfile[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"PakFile",""));
278     //    strcpy (lastpakfile[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"LastPakFile",""));
279     //    strcpy (GameDir[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"GameDir","\0"));
280   }
281   /*
282         if(!strlen(gszTextureDir[QUAKE2]))
283                 strcpy(gszTextureDir[QUAKE2],"c:\\quake2\\baseq2\\textures\\");
284         if(!strlen(gszTextureDir[KINGPIN]))
285                 strcpy(gszTextureDir[KINGPIN],"c:\\kingpin\\main\\textures\\");
286   */
287   if(!strlen(Texture[QUAKE2][0]))    strcpy(Texture[QUAKE2][0],   "textures/e1u1/grass1_4");
288   if(!strlen(Texture[HALFLIFE][0]))  strcpy(Texture[HALFLIFE][0], "textures/OUT_GRND1");
289   if(!strlen(Texture[SIN][0]))       strcpy(Texture[SIN][0],      "textures/generic/floor_organic/fl_grass");
290   if(!strlen(Texture[HERETIC2][0]))  strcpy(Texture[HERETIC2][0], "textures/canyon/canyon05");
291   if(!strlen(Texture[KINGPIN][0]))   strcpy(Texture[KINGPIN][0],  "textures/bricks/s_sr_m3");
292   if(!strlen(Texture[GENESIS3D][0])) strcpy(Texture[GENESIS3D][0],"textures/rock13");
293   if(!strlen(Texture[QUAKE3][0]))    strcpy(Texture[QUAKE3][0],   "textures/organics/grass3");
294   if(!strlen(Texture[QUAKE3][1]))    strcpy(Texture[QUAKE3][1],   "textures/common/caulk");
295   strcpy (gbmp.name, g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","Filename",""));
296
297
298   gbmp.colors = NULL;
299   if (strlen(gbmp.name))
300     OpenBitmap ();
301
302   strcpy (gbmp.defpath, g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","DefaultPath",""));
303
304   Text = g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","BlackValue","");
305   if (strlen (Text))
306     gbmp.black_value = atof (Text);
307   else
308     gbmp.black_value = 0;
309
310   Text = g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","WhiteValue","");
311   if (strlen (Text))
312     gbmp.white_value = atof (Text);
313   else
314     gbmp.white_value = 256.;
315 }
316
317 /*
318 ============
319 va
320
321 does a varargs printf into a temp buffer, so I don't need to have
322 varargs versions of all text functions.
323 FIXME: make this buffer size safe someday
324 ============
325 */
326 char *va (const char *format, ...)
327 {
328   va_list argptr;
329   static char string[1024];
330
331   va_start (argptr, format);
332   vsprintf (string, format,argptr);
333   va_end (argptr);
334
335   return string;        
336 }
337
338
339 // Writes current values to INI file
340 void WriteIniFile(const char *file)
341 {
342   int i;
343
344   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "Amplitude",    va("%g",Amplitude));
345   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "Roughness",    va("%g",Roughness));
346   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "WaveLength",   va("%g",WaveLength));
347   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "Extents",      va("%g,%g,%g,%g",Hll,Vll,Hur,Vur));
348   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "CornerValues", va("%g,%g,%g,%g",Z00,Z01,Z10,Z11));
349   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "TextureOffset",va("%g,%g",TexOffset[0],TexOffset[1]));
350   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "TextureScale", va("%g,%g",TexScale[0],TexScale[1]));
351   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "NH", NH);
352   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "NV", NV);
353   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "AddHints", AddHints);
354   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "ArghRad2", ArghRad2);
355   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "AutoOverwrite", AutoOverwrite);
356   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "FixBorders", FixBorders);
357   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "Plane", Plane);
358   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "Preview", Preview);
359         g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "Antialiasing", Antialiasing); // ^Fishman - Antializing for the preview window.
360   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "RandomSeed", RandomSeed);
361   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "Skybox", Skybox);
362   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "UseDetail", UseDetail);
363   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "AddTerrainKey", AddTerrainKey); // ^Fishman - Add terrain key to func_group.
364   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "UseLadder", UseLadder);
365   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "WaveType", WaveType);
366   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "vid_x", vid_x);
367   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "vid_y", vid_y);
368   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "view_x", view_x);
369   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "view_y", view_y);
370   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "view_cx", view_cx);
371   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "view_cy", view_cy);
372   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "UsePatches", UsePatches);
373   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "SlantAngle", SlantAngle);
374   for(i=0; i<NUMGAMES; i++)
375   {
376     g_FuncTable.m_pfnProfileSaveString (file, GameName[i], "Texture",   Texture[i][0] );
377     g_FuncTable.m_pfnProfileSaveString (file, GameName[i], "Texture2",  Texture[i][1] );
378     g_FuncTable.m_pfnProfileSaveString (file, GameName[i], "Texture3",  Texture[i][2] );
379   }
380
381   g_FuncTable.m_pfnProfileSaveString (file, "Bitmap", "Filename", gbmp.name );
382   g_FuncTable.m_pfnProfileSaveString (file, "Bitmap", "DefaultPath", gbmp.defpath );
383   g_FuncTable.m_pfnProfileSaveString (file, "Bitmap", "BlackValue", va("%g",gbmp.black_value));
384   g_FuncTable.m_pfnProfileSaveString (file, "Bitmap", "WhiteValue", va("%g",gbmp.white_value));
385 //g_FuncTable.m_pfnProfileSaveString (file, "Formula", "Formula", ExcelFunc );
386 }
387
388 void UpdatePreview (bool DataChange)
389 {
390   if (g_pWndPreview && GTK_WIDGET_VISIBLE (g_pWndPreview))
391   {
392     if (DataChange)
393       GenerateXYZ ();
394
395     gtk_widget_draw (g_pPreviewWidget, NULL);
396   }
397 }
398
399 void SaveSetup (GtkWidget *parent)
400 {
401   const char *name = g_FuncTable.m_pfnFileDialog (parent, false, "Save GenSurf Settings",
402                                              g_FuncTable.m_pfnProfileGetDirectory ());
403
404   if (name != NULL)
405   {
406     char key[32], text[32]; 
407     int i, j;
408
409     WriteIniFile (name);
410     g_FuncTable.m_pfnProfileSaveString (name, OPTS_SECTION,"MapFile",gszMapFile);
411     sprintf(text,"0x%04x",FileAppend);
412     g_FuncTable.m_pfnProfileSaveString (name, OPTS_SECTION,"Append",text);
413     sprintf(text,"0x%04x",Decimate);
414     g_FuncTable.m_pfnProfileSaveString (name, OPTS_SECTION,"Decimate",text);
415     for(i=0; i<=NH; i++)
416     {
417       for(j=0; j<=NV; j++)
418       {
419         if(xyz[i][j].fixed)
420         {
421           sprintf(key,"I%dJ%d",i,j);
422           sprintf(text,"%g %g %g", xyz[i][j].fixed_value, xyz[i][j].range, xyz[i][j].rate);
423           g_FuncTable.m_pfnProfileSaveString (name, "FixedPoints",key,text);
424         }
425       }
426     }
427   }
428 }
429
430 void OpenSetup (GtkWidget *parent, int UseDefaults)
431 {
432   const char *name;
433   char key[32], *text;
434   float value,range,rate;
435   int i, j;
436
437   if (UseDefaults)
438     name = g_strdup ("plugins/defaults.srf"); // dummy string
439   else
440     name = g_FuncTable.m_pfnFileDialog (parent, true, "Open GenSurf Settings",
441                                         g_FuncTable.m_pfnProfileGetDirectory ());
442
443   if(name != NULL)
444   {
445     ReadIniFile (name);
446     Decimate   = g_FuncTable.m_pfnProfileLoadInt (name, OPTS_SECTION,"Decimate",0);
447     Decimate   = max(0,min(Decimate,100));
448
449     for (i=0; i<=NH; i++)
450     {
451       for (j=0; j<=NV; j++)
452       {
453         sprintf(key,"I%dJ%d",i,j);
454         text = g_FuncTable.m_pfnProfileLoadString (name, "FixedPoints", key, "");
455         if (strlen (text))
456         {
457           xyz[i][j].fixed = 1;
458           xyz[i][j].rate        = 0.;
459           sscanf(text,"%g %g %g",&value,&range,&rate);
460           xyz[i][j].fixed_value = value;
461           xyz[i][j].range       = range;
462           xyz[i][j].rate        = rate;
463         }
464         else
465           xyz[i][j].fixed = 0;
466       }
467     }
468   }
469 }