]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/prtview/prtview.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / contrib / prtview / prtview.cpp
1 /*
2    PrtView plugin for GtkRadiant
3    Copyright (C) 2001 Geoffrey Dewan, 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
21 #include "prtview.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "profile/profile.h"
26
27 #include "qerplugin.h"
28 #include "iscenegraph.h"
29 #include "iglrender.h"
30 #include "iplugin.h"
31 #include "stream/stringstream.h"
32
33 #include "portals.h"
34 #include "AboutDialog.h"
35 #include "ConfigDialog.h"
36 #include "LoadPortalFileDialog.h"
37
38 #define Q3R_CMD_SPLITTER "-"
39 #define Q3R_CMD_ABOUT "About Portal Viewer"
40 #define Q3R_CMD_LOAD "Load .prt file"
41 #define Q3R_CMD_RELEASE "Unload .prt file"
42 #define Q3R_CMD_SHOW_3D "Toggle portals (3D)"
43 #define Q3R_CMD_SHOW_2D "Toggle portals (2D)"
44 #define Q3R_CMD_OPTIONS "Configure Portal Viewer"
45
46 CopiedString INIfn;
47
48 /////////////////////////////////////////////////////////////////////////////
49 // CPrtViewApp construction
50
51 const char *RENDER_2D = "Render2D";
52 const char *WIDTH_2D = "Width2D";
53 const char *AA_2D = "AntiAlias2D";
54 const char *COLOR_2D = "Color2D";
55
56 const char *RENDER_3D = "Render3D";
57 const char *WIDTH_3D = "Width3D";
58 const char *AA_3D = "AntiAlias3D";
59 const char *COLOR_3D = "Color3D";
60 const char *COLOR_FOG = "ColorFog";
61 const char *FOG = "Fog";
62 const char *ZBUFFER = "ZBuffer";
63 const char *POLYGON = "Polygons";
64 const char *LINE = "Lines";
65 const char *TRANS_3D = "Transparency";
66 const char *CLIP_RANGE = "ClipRange";
67 const char *CLIP = "Clip";
68
69
70 void PrtView_construct()
71 {
72     StringOutputStream tmp(64);
73     tmp << GlobalRadiant().getSettingsPath() << "prtview.ini";
74     INIfn = tmp.c_str();
75
76     portals.show_2d = INIGetInt(RENDER_2D, FALSE) ? true : false;
77     portals.aa_2d = INIGetInt(AA_2D, FALSE) ? true : false;
78     portals.width_2d = (float) INIGetInt(WIDTH_2D, 10);
79     portals.color_2d = (PackedColour) INIGetInt(COLOR_2D, RGB(0, 0, 255)) & 0xFFFFFF;
80
81     if (portals.width_2d > 40.0f) {
82         portals.width_2d = 40.0f;
83     } else if (portals.width_2d < 2.0f) {
84         portals.width_2d = 2.0f;
85     }
86
87     portals.show_3d = INIGetInt(RENDER_3D, TRUE) ? true : false;
88
89     portals.zbuffer = INIGetInt(ZBUFFER, 1);
90     portals.fog = INIGetInt(FOG, FALSE) ? true : false;
91     portals.polygons = INIGetInt(POLYGON, TRUE);
92     portals.lines = INIGetInt(LINE, TRUE);
93     portals.aa_3d = INIGetInt(AA_3D, FALSE) ? true : false;
94     portals.width_3d = (float) INIGetInt(WIDTH_3D, 4);
95     portals.color_3d = (PackedColour) INIGetInt(COLOR_3D, RGB(255, 255, 0)) & 0xFFFFFF;
96     portals.color_fog = (PackedColour) INIGetInt(COLOR_FOG, RGB(127, 127, 127)) & 0xFFFFFF;
97     portals.trans_3d = (float) INIGetInt(TRANS_3D, 50);
98     portals.clip = INIGetInt(CLIP, FALSE) ? true : false;
99     portals.clip_range = (float) INIGetInt(CLIP_RANGE, 16);
100
101     if (portals.clip_range < 1) {
102         portals.clip_range = 1;
103     } else if (portals.clip_range > 128) {
104         portals.clip_range = 128;
105     }
106
107     if (portals.zbuffer < 0) {
108         portals.zbuffer = 0;
109     } else if (portals.zbuffer > 2) {
110         portals.zbuffer = 0;
111     }
112
113     if (portals.width_3d > 40.0f) {
114         portals.width_3d = 40.0f;
115     } else if (portals.width_3d < 2.0f) {
116         portals.width_3d = 2.0f;
117     }
118
119     if (portals.trans_3d > 100.0f) {
120         portals.trans_3d = 100.0f;
121     } else if (portals.trans_3d < 0.0f) {
122         portals.trans_3d = 0.0f;
123     }
124
125     SaveConfig();
126
127     portals.FixColors();
128
129     Portals_constructShaders();
130     GlobalShaderCache().attachRenderable(render);
131 }
132
133 void PrtView_destroy()
134 {
135     GlobalShaderCache().detachRenderable(render);
136     Portals_destroyShaders();
137 }
138
139 void SaveConfig()
140 {
141     INISetInt(RENDER_2D, portals.show_2d, "Draw in 2D windows");
142     INISetInt(WIDTH_2D, (int) portals.width_2d, "Width of lines in 2D windows (in units of 1/2)");
143     INISetInt(COLOR_2D, (int) portals.color_2d, "Color of lines in 2D windows");
144     INISetInt(AA_2D, portals.aa_2d, "Draw lines in 2D window anti-aliased");
145
146     INISetInt(ZBUFFER, portals.zbuffer, "ZBuffer level in 3D window");
147     INISetInt(FOG, portals.fog, "Use depth cueing in 3D window");
148     INISetInt(POLYGON, portals.polygons, "Render using polygons polygons in 3D window");
149     INISetInt(LINE, portals.polygons, "Render using lines in 3D window");
150     INISetInt(RENDER_3D, portals.show_3d, "Draw in 3D windows");
151     INISetInt(WIDTH_3D, (int) portals.width_3d, "Width of lines in 3D window (in units of 1/2)");
152     INISetInt(COLOR_3D, (int) portals.color_3d, "Color of lines/polygons in 3D window");
153     INISetInt(COLOR_FOG, (int) portals.color_fog, "Color of distant lines/polygons in 3D window");
154     INISetInt(AA_3D, portals.aa_3d, "Draw lines in 3D window anti-aliased");
155     INISetInt(TRANS_3D, (int) portals.trans_3d, "Transparency in 3d view (0 = solid, 100 = invisible)");
156     INISetInt(CLIP, portals.clip, "Cubic clipper active for portal viewer");
157     INISetInt(CLIP_RANGE, (int) portals.clip_range, "Portal viewer cubic clip distance (in units of 64)");
158 }
159
160
161 const char *CONFIG_SECTION = "Configuration";
162
163 int INIGetInt(const char *key, int def)
164 {
165     char value[1024];
166
167     if (read_var(INIfn.c_str(), CONFIG_SECTION, key, value)) {
168         return atoi(value);
169     } else {
170         return def;
171     }
172 }
173
174 void INISetInt(const char *key, int val, const char *comment /* = NULL */ )
175 {
176     char s[1000];
177
178     if (comment) {
179         sprintf(s, "%d        ; %s", val, comment);
180     } else {
181         sprintf(s, "%d", val);
182     }
183     save_var(INIfn.c_str(), CONFIG_SECTION, key, s);
184 }
185
186
187 // plugin name
188 static const char *PLUGIN_NAME = "Portal Viewer";
189 // commands in the menu
190 static const char *PLUGIN_COMMANDS =
191         Q3R_CMD_ABOUT ";"
192                 Q3R_CMD_SPLITTER ";"
193                 Q3R_CMD_OPTIONS ";"
194                 Q3R_CMD_SPLITTER ";"
195                 Q3R_CMD_SHOW_2D ";"
196                 Q3R_CMD_SHOW_3D ";"
197                 Q3R_CMD_SPLITTER ";"
198                 Q3R_CMD_RELEASE ";"
199                 Q3R_CMD_LOAD;
200
201
202 const char *QERPlug_Init(void *hApp, void *pMainWidget)
203 {
204     return "Portal Viewer for Q3Radiant";
205 }
206
207 const char *QERPlug_GetName()
208 {
209     return PLUGIN_NAME;
210 }
211
212 const char *QERPlug_GetCommandList()
213 {
214     return PLUGIN_COMMANDS;
215 }
216
217
218 const char *QERPlug_GetCommandTitleList()
219 {
220     return "";
221 }
222
223
224 void QERPlug_Dispatch(const char *p, float *vMin, float *vMax, bool bSingleBrush)
225 {
226     globalOutputStream() << MSG_PREFIX "Command \"" << p << "\"\n";
227
228     if (!strcmp(p, Q3R_CMD_ABOUT)) {
229         DoAboutDlg();
230     } else if (!strcmp(p, Q3R_CMD_LOAD)) {
231         if (DoLoadPortalFileDialog() == IDOK) {
232             portals.Load();
233             SceneChangeNotify();
234         } else {
235             globalOutputStream() << MSG_PREFIX "Portal file load aborted.\n";
236         }
237     } else if (!strcmp(p, Q3R_CMD_RELEASE)) {
238         portals.Purge();
239
240         SceneChangeNotify();
241
242         globalOutputStream() << MSG_PREFIX "Portals unloaded.\n";
243     } else if (!strcmp(p, Q3R_CMD_SHOW_2D)) {
244         portals.show_2d = !portals.show_2d;
245
246         SceneChangeNotify();
247         SaveConfig();
248
249         if (portals.show_2d) {
250             globalOutputStream() << MSG_PREFIX "Portals will be rendered in 2D view.\n";
251         } else {
252             globalOutputStream() << MSG_PREFIX "Portals will NOT be rendered in 2D view.\n";
253         }
254     } else if (!strcmp(p, Q3R_CMD_SHOW_3D)) {
255         portals.show_3d = !portals.show_3d;
256         SaveConfig();
257
258         SceneChangeNotify();
259
260         if (portals.show_3d) {
261             globalOutputStream() << MSG_PREFIX "Portals will be rendered in 3D view.\n";
262         } else {
263             globalOutputStream() << MSG_PREFIX "Portals will NOT be rendered in 3D view.\n";
264         }
265     } else if (!strcmp(p, Q3R_CMD_OPTIONS)) {
266         DoConfigDialog();
267         SaveConfig();
268
269         SceneChangeNotify();
270     }
271 }
272
273
274 #include "modulesystem/singletonmodule.h"
275
276 class PrtViewPluginDependencies :
277         public GlobalSceneGraphModuleRef,
278         public GlobalRadiantModuleRef,
279         public GlobalShaderCacheModuleRef,
280         public GlobalOpenGLModuleRef,
281         public GlobalOpenGLStateLibraryModuleRef {
282 };
283
284 class PrtViewPluginModule {
285     _QERPluginTable m_plugin;
286 public:
287     typedef _QERPluginTable Type;
288
289     STRING_CONSTANT(Name, "prtview");
290
291     PrtViewPluginModule()
292     {
293         m_plugin.m_pfnQERPlug_Init = QERPlug_Init;
294         m_plugin.m_pfnQERPlug_GetName = QERPlug_GetName;
295         m_plugin.m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
296         m_plugin.m_pfnQERPlug_GetCommandTitleList = QERPlug_GetCommandTitleList;
297         m_plugin.m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
298
299         PrtView_construct();
300     }
301
302     ~PrtViewPluginModule()
303     {
304         PrtView_destroy();
305     }
306
307     _QERPluginTable *getTable()
308     {
309         return &m_plugin;
310     }
311 };
312
313 typedef SingletonModule<PrtViewPluginModule, PrtViewPluginDependencies> SingletonPrtViewPluginModule;
314
315 SingletonPrtViewPluginModule g_PrtViewPluginModule;
316
317
318 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer &server)
319 {
320     initialiseModule(server);
321
322     g_PrtViewPluginModule.selfRegister();
323 }