]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/gtkgensurf/gensurf.cpp
* applied patch by StefanV (from mailinglist) that fixes an error in config.py (broke...
[xonotic/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   ReadIniFile (gszIni);
137
138   if (g_pWnd == NULL)
139     g_pWnd = create_main_dialog ();
140
141   return true;
142 }
143
144 // Reads default values
145
146 #define OPTS_SECTION "Options"
147
148 void ReadIniFile (const char *file)
149 {
150   char *Text;
151   float x1,x2,x3,x4;
152   int   i;
153
154   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "Amplitude", "");
155   if (strlen (Text))
156     Amplitude = atof (Text);
157   else
158     Amplitude = 128;
159
160   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "Roughness", "");
161   if (strlen (Text))
162     Roughness = atof (Text);
163   else
164     Roughness = 16;
165
166   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "WaveLength", "");
167   if (strlen (Text))
168     WaveLength = atof (Text);
169   else
170     WaveLength = 1024;
171
172   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "Extents", "");
173   if (strlen (Text))
174   {
175     sscanf(Text,"%f,%f,%f,%f",&x1,&x2,&x3,&x4);
176     Hll = x1;
177     Vll = x2;
178     Hur = x3;
179     Vur = x4;
180   }
181   else
182   {
183     Hll = -512;
184     Vll = -512;
185     Hur =  512;
186     Vur =  512;
187   }
188
189   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "CornerValues", "");
190   if (strlen (Text))
191   {
192     sscanf(Text,"%f,%f,%f,%f",&x1,&x2,&x3,&x4);
193     Z00 = x1;
194     Z01 = x2;
195     Z10 = x3;
196     Z11 = x4;
197   }
198   else
199   {
200     Z00 = 0.;
201     Z01 = 0.;
202     Z10 = 0.;
203     Z11 = 0.;
204   }
205
206   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION, "TextureOffset", "");
207   if (strlen (Text))
208   {
209     sscanf(Text,"%f,%f",&x1,&x2);
210     TexOffset[0] = x1;
211     TexOffset[1] = x2;
212   }
213   else
214   {
215     TexOffset[0] = 0.;
216     TexOffset[1] = 0.;
217   }
218
219   Text = g_FuncTable.m_pfnProfileLoadString (file, OPTS_SECTION,"TextureScale","");
220   if (strlen (Text))
221   {
222     sscanf(Text,"%f,%f",&x1,&x2);
223     TexScale[0] = x1;
224     TexScale[1] = x2;
225     if(TexScale[0] == 0.) TexScale[0] = 1.0;
226     if(TexScale[1] == 0.) TexScale[1] = 1.0;
227   }
228   else
229   {
230     TexScale[0] = 1.;
231     TexScale[1] = 1.;
232   }
233
234   NH = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"NH",8);
235   NH = max(1,min(NH,MAX_ROWS));
236   NV = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"NV",8);
237   NV = max(1,min(NV,MAX_ROWS));
238
239 //      Decimate   = GetPrivateProfileInt(OPTS_SECTION,"Decimate",0,file);
240 //      Decimate = max(0,min(Decimate,100));
241
242   AddHints                      = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"AddHints",0);
243   ArghRad2                      = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"ArghRad2",0);
244   AutoOverwrite = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"AutoOverwrite",0);
245   FixBorders            = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"FixBorders",1);
246   HideBackFaces = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"HideBackFaces",0);
247   Plane                                 = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"Plane",0);
248   Preview                               = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"Preview", 0);
249         Antialiasing    = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"Antialiasing",0); // ^Fishman - Antializing for the preview window.
250   RandomSeed            = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"RandomSeed",1);
251   Skybox                                = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"Skybox",0);
252   UseDetail                     = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"UseDetail",0);
253         AddTerrainKey   =       g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"AddTerrainKey",0); // ^Fishman - Add terrain key to func_group.
254   UseLadder                     = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"UseLadder",0);
255   WaveType                      = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"WaveType",0);
256   vid_x                                 = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"vid_x", 0);
257   vid_y                                 = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"vid_y", 0);
258   view_x                                = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"view_x",0);
259   view_y                                = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"view_y",0);
260   view_cx                               = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"view_cx",0);
261   view_cy                               = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"view_cy",0);
262
263   UsePatches            = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"UsePatches",0);
264
265   SlantAngle = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"SlantAngle",60);
266   GimpHints  = g_FuncTable.m_pfnProfileLoadInt (file, OPTS_SECTION,"GimpHints",0);
267
268   for(i=0; i<NUMGAMES; i++)
269   {
270     //    strcpy (gszOutputDir[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"OutputDir",""));
271     strcpy (Texture[i][0], g_FuncTable.m_pfnProfileLoadString (file, GameName[i], "Texture", ""));
272     strcpy (Texture[i][1], g_FuncTable.m_pfnProfileLoadString (file, GameName[i], "Texture2", ""));
273     strcpy (Texture[i][2], g_FuncTable.m_pfnProfileLoadString (file, GameName[i], "Texture3", ""));
274     //    strcpy (gszTextureDir[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"TextureDir",""));
275     //    UsePak[i] = GetPrivateProfileInt(GameName[i],"UsePak",0);
276     //    strcpy (pakfile[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"PakFile",""));
277     //    strcpy (lastpakfile[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"LastPakFile",""));
278     //    strcpy (GameDir[i], g_FuncTable.m_pfnProfileLoadString (file, GameName[i],"GameDir","\0"));
279   }
280   /*
281         if(!strlen(gszTextureDir[QUAKE2]))
282                 strcpy(gszTextureDir[QUAKE2],"c:\\quake2\\baseq2\\textures\\");
283         if(!strlen(gszTextureDir[KINGPIN]))
284                 strcpy(gszTextureDir[KINGPIN],"c:\\kingpin\\main\\textures\\");
285   */
286   if(!strlen(Texture[QUAKE2][0]))    strcpy(Texture[QUAKE2][0],   "textures/e1u1/grass1_4");
287   if(!strlen(Texture[HALFLIFE][0]))  strcpy(Texture[HALFLIFE][0], "textures/OUT_GRND1");
288   if(!strlen(Texture[SIN][0]))       strcpy(Texture[SIN][0],      "textures/generic/floor_organic/fl_grass");
289   if(!strlen(Texture[HERETIC2][0]))  strcpy(Texture[HERETIC2][0], "textures/canyon/canyon05");
290   if(!strlen(Texture[KINGPIN][0]))   strcpy(Texture[KINGPIN][0],  "textures/bricks/s_sr_m3");
291   if(!strlen(Texture[GENESIS3D][0])) strcpy(Texture[GENESIS3D][0],"textures/rock13");
292   if(!strlen(Texture[QUAKE3][0]))    strcpy(Texture[QUAKE3][0],   "textures/organics/grass3");
293   if(!strlen(Texture[QUAKE3][1]))    strcpy(Texture[QUAKE3][1],   "textures/common/caulk");
294
295   strcpy (gbmp.name, g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","Filename",""));
296
297   if (strlen(gbmp.name))
298     OpenBitmap ();
299
300   strcpy (gbmp.defpath, g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","DefaultPath",""));
301
302   Text = g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","BlackValue","");
303   if (strlen (Text))
304     gbmp.black_value = atof (Text);
305   else
306     gbmp.black_value = 0;
307
308   Text = g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","WhiteValue","");
309   if (strlen (Text))
310     gbmp.white_value = atof (Text);
311   else
312     gbmp.white_value = 256.;
313 }
314
315 /*
316 ============
317 va
318
319 does a varargs printf into a temp buffer, so I don't need to have
320 varargs versions of all text functions.
321 FIXME: make this buffer size safe someday
322 ============
323 */
324 char *va (const char *format, ...)
325 {
326   va_list argptr;
327   static char string[1024];
328
329   va_start (argptr, format);
330   vsprintf (string, format,argptr);
331   va_end (argptr);
332
333   return string;
334 }
335
336
337 // Writes current values to INI file
338 void WriteIniFile(const char *file)
339 {
340   int i;
341
342   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "Amplitude",    va("%g",Amplitude));
343   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "Roughness",    va("%g",Roughness));
344   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "WaveLength",   va("%g",WaveLength));
345   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "Extents",      va("%g,%g,%g,%g",Hll,Vll,Hur,Vur));
346   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "CornerValues", va("%g,%g,%g,%g",Z00,Z01,Z10,Z11));
347   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "TextureOffset",va("%g,%g",TexOffset[0],TexOffset[1]));
348   g_FuncTable.m_pfnProfileSaveString (file, OPTS_SECTION, "TextureScale", va("%g,%g",TexScale[0],TexScale[1]));
349   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "NH", NH);
350   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "NV", NV);
351   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "AddHints", AddHints);
352   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "ArghRad2", ArghRad2);
353   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "AutoOverwrite", AutoOverwrite);
354   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "FixBorders", FixBorders);
355   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "Plane", Plane);
356   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "Preview", Preview);
357         g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "Antialiasing", Antialiasing); // ^Fishman - Antializing for the preview window.
358   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "RandomSeed", RandomSeed);
359   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "Skybox", Skybox);
360   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "UseDetail", UseDetail);
361   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "AddTerrainKey", AddTerrainKey); // ^Fishman - Add terrain key to func_group.
362   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "UseLadder", UseLadder);
363   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "WaveType", WaveType);
364   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "vid_x", vid_x);
365   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "vid_y", vid_y);
366   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "view_x", view_x);
367   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "view_y", view_y);
368   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "view_cx", view_cx);
369   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "view_cy", view_cy);
370   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "UsePatches", UsePatches);
371   g_FuncTable.m_pfnProfileSaveInt (file, OPTS_SECTION, "SlantAngle", SlantAngle);
372   for(i=0; i<NUMGAMES; i++)
373   {
374     g_FuncTable.m_pfnProfileSaveString (file, GameName[i], "Texture",   Texture[i][0] );
375     g_FuncTable.m_pfnProfileSaveString (file, GameName[i], "Texture2",  Texture[i][1] );
376     g_FuncTable.m_pfnProfileSaveString (file, GameName[i], "Texture3",  Texture[i][2] );
377   }
378
379   g_FuncTable.m_pfnProfileSaveString (file, "Bitmap", "Filename", gbmp.name );
380   g_FuncTable.m_pfnProfileSaveString (file, "Bitmap", "DefaultPath", gbmp.defpath );
381   g_FuncTable.m_pfnProfileSaveString (file, "Bitmap", "BlackValue", va("%g",gbmp.black_value));
382   g_FuncTable.m_pfnProfileSaveString (file, "Bitmap", "WhiteValue", va("%g",gbmp.white_value));
383 //g_FuncTable.m_pfnProfileSaveString (file, "Formula", "Formula", ExcelFunc );
384 }
385
386 void UpdatePreview (bool DataChange)
387 {
388   if (g_pWndPreview && GTK_WIDGET_VISIBLE (g_pWndPreview))
389   {
390     if (DataChange)
391       GenerateXYZ ();
392
393     gtk_widget_draw (g_pPreviewWidget, NULL);
394   }
395 }
396
397 void SaveSetup (GtkWidget *parent)
398 {
399   const char *name = g_FuncTable.m_pfnFileDialog (parent, false, "Save GenSurf Settings",
400                                              g_FuncTable.m_pfnProfileGetDirectory (), "gtkgensurf", NULL);
401
402   if (name != NULL)
403   {
404     char key[32], text[32];
405     int i, j;
406
407     WriteIniFile (name);
408     g_FuncTable.m_pfnProfileSaveString (name, OPTS_SECTION,"MapFile",gszMapFile);
409     sprintf(text,"0x%04x",FileAppend);
410     g_FuncTable.m_pfnProfileSaveString (name, OPTS_SECTION,"Append",text);
411     sprintf(text,"0x%04x",Decimate);
412     g_FuncTable.m_pfnProfileSaveString (name, OPTS_SECTION,"Decimate",text);
413     for(i=0; i<=NH; i++)
414     {
415       for(j=0; j<=NV; j++)
416       {
417         if(xyz[i][j].fixed)
418         {
419           sprintf(key,"I%dJ%d",i,j);
420           sprintf(text,"%g %g %g", xyz[i][j].fixed_value, xyz[i][j].range, xyz[i][j].rate);
421           g_FuncTable.m_pfnProfileSaveString (name, "FixedPoints",key,text);
422         }
423       }
424     }
425   }
426 }
427
428 void OpenSetup (GtkWidget *parent, int UseDefaults)
429 {
430   const char *name;
431   char key[32], *text;
432   float value,range,rate;
433   int i, j;
434
435   if (UseDefaults)
436     name = g_strdup ("plugins/defaults.srf"); // dummy string
437   else
438     name = g_FuncTable.m_pfnFileDialog (parent, true, "Open GenSurf Settings",
439                                         g_FuncTable.m_pfnProfileGetDirectory (), "gtkgensurf", NULL);
440
441   if(name != NULL)
442   {
443     ReadIniFile (name);
444     Decimate   = g_FuncTable.m_pfnProfileLoadInt (name, OPTS_SECTION,"Decimate",0);
445     Decimate   = max(0,min(Decimate,100));
446
447     for (i=0; i<=NH; i++)
448     {
449       for (j=0; j<=NV; j++)
450       {
451         sprintf(key,"I%dJ%d",i,j);
452         text = g_FuncTable.m_pfnProfileLoadString (name, "FixedPoints", key, "");
453         if (strlen (text))
454         {
455           xyz[i][j].fixed = 1;
456           xyz[i][j].rate        = 0.;
457           sscanf(text,"%g %g %g",&value,&range,&rate);
458           xyz[i][j].fixed_value = value;
459           xyz[i][j].range       = range;
460           xyz[i][j].rate        = rate;
461         }
462         else
463           xyz[i][j].fixed = 0;
464       }
465     }
466   }
467 }