2 PrtView plugin for GtkRadiant
3 Copyright (C) 2001 Geoffrey Dewan, Loki software and qeradiant.com
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.
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.
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
20 // PrtView.cpp : Defines the initialization routines for the DLL.
28 #define Q3R_CMD_SPLITTER "-"
29 #define Q3R_CMD_ABOUT "About Portal Viewer"
30 #define Q3R_CMD_LOAD "Load .prt file"
31 #define Q3R_CMD_RELEASE "Unload .prt file"
32 #define Q3R_CMD_SHOW_3D "Toggle portals (3D)"
33 #define Q3R_CMD_SHOW_2D "Toggle portals (2D)"
34 #define Q3R_CMD_OPTIONS "Configure Portal Viewer"
36 static char INIfn[PATH_MAX];
38 /////////////////////////////////////////////////////////////////////////////
39 // CPrtViewApp construction
41 #define RENDER_2D "Render2D"
42 #define WIDTH_2D "Width2D"
43 #define AA_2D "AntiAlias2D"
44 #define COLOR_2D "Color2D"
46 #define RENDER_3D "Render3D"
47 #define WIDTH_3D "Width3D"
48 #define AA_3D "AntiAlias3D"
49 #define COLOR_3D "Color3D"
50 #define COLOR_FOG "ColorFog"
52 #define ZBUFFER "ZBuffer"
53 #define POLYGON "Polygons"
55 #define TRANS_3D "Transparency"
56 #define CLIP_RANGE "ClipRange"
63 char fn_drive[_MAX_DRIVE];
64 char fn_dir[_MAX_DIR];
65 char fn_name[_MAX_FNAME];
66 char fn_ext[_MAX_EXT];
68 GetModuleFileName(GetModuleHandle("PrtView.dll"), fn, _MAX_PATH);
70 _splitpath(fn, fn_drive, fn_dir, fn_name, fn_ext);
72 strcpy(INIfn, fn_drive);
73 strcat(INIfn, fn_dir);
74 strcat(INIfn, fn_name);
75 strcat(INIfn, ".ini");
76 #else // if def __linux__
77 strcpy (INIfn, g_get_home_dir ());
78 strcat (INIfn, "/.radiant/");
79 strcat (INIfn, RADIANT_VERSION);
80 strcat (INIfn, "/prtview.ini");
83 portals.show_2d = INIGetInt(RENDER_2D, FALSE) ? qtrue : qfalse;
84 portals.aa_2d = INIGetInt(AA_2D, FALSE) ? qtrue : qfalse;
85 portals.width_2d = (float)INIGetInt(WIDTH_2D, 10);
86 portals.color_2d = (COLORREF)INIGetInt(COLOR_2D, RGB(0, 0, 255)) & 0xFFFFFF;
88 if (portals.width_2d > 40.0f)
89 portals.width_2d = 40.0f;
90 else if (portals.width_2d < 2.0f)
91 portals.width_2d = 2.0f;
93 portals.show_3d = INIGetInt(RENDER_3D, TRUE) ? qtrue : qfalse;
95 portals.zbuffer = INIGetInt(ZBUFFER, 1);
96 portals.fog = INIGetInt(FOG, FALSE) ? qtrue : qfalse;
97 portals.polygons = INIGetInt(POLYGON, TRUE);
98 portals.lines = INIGetInt(LINE, TRUE);
99 portals.aa_3d = INIGetInt(AA_3D, FALSE) ? qtrue : qfalse;
100 portals.width_3d = (float)INIGetInt(WIDTH_3D, 4);
101 portals.color_3d = (COLORREF)INIGetInt(COLOR_3D, RGB(255, 255, 0)) & 0xFFFFFF;
102 portals.color_fog = (COLORREF)INIGetInt(COLOR_FOG, RGB(127, 127, 127)) & 0xFFFFFF;
103 portals.trans_3d = (float)INIGetInt(TRANS_3D, 50);
104 portals.clip = INIGetInt(CLIP, FALSE) ? qtrue : qfalse;
105 portals.clip_range = (float)INIGetInt(CLIP_RANGE, 16);
107 if (portals.clip_range < 1)
108 portals.clip_range = 1;
109 else if (portals.clip_range > 128)
110 portals.clip_range = 128;
112 if (portals.zbuffer < 0)
114 else if (portals.zbuffer > 2)
117 if (portals.width_3d > 40.0f)
118 portals.width_3d = 40.0f;
119 else if (portals.width_3d < 2.0f)
120 portals.width_3d = 2.0f;
122 if (portals.trans_3d > 100.0f)
123 portals.trans_3d = 100.0f;
124 else if (portals.trans_3d < 0.0f)
125 portals.trans_3d = 0.0f;
134 INISetInt(RENDER_2D, portals.show_2d, "Draw in 2D windows");
135 INISetInt(WIDTH_2D, (int)portals.width_2d, "Width of lines in 2D windows (in units of 1/2)");
136 INISetInt(COLOR_2D, (int)portals.color_2d, "Color of lines in 2D windows");
137 INISetInt(AA_2D, portals.aa_2d, "Draw lines in 2D window anti-aliased");
139 INISetInt(ZBUFFER, portals.zbuffer, "ZBuffer level in 3D window");
140 INISetInt(FOG, portals.fog, "Use depth cueing in 3D window");
141 INISetInt(POLYGON, portals.polygons, "Render using polygons polygons in 3D window");
142 INISetInt(LINE, portals.polygons, "Render using lines in 3D window");
143 INISetInt(RENDER_3D, portals.show_3d, "Draw in 3D windows");
144 INISetInt(WIDTH_3D, (int)portals.width_3d, "Width of lines in 3D window (in units of 1/2)");
145 INISetInt(COLOR_3D, (int)portals.color_3d, "Color of lines/polygons in 3D window");
146 INISetInt(COLOR_FOG, (int)portals.color_fog, "Color of distant lines/polygons in 3D window");
147 INISetInt(AA_3D, portals.aa_3d, "Draw lines in 3D window anti-aliased");
148 INISetInt(TRANS_3D, (int)portals.trans_3d, "Transparency in 3d view (0 = solid, 100 = invisible)");
149 INISetInt(CLIP, portals.clip, "Cubic clipper active for portal viewer");
150 INISetInt(CLIP_RANGE, (int)portals.clip_range, "Portal viewer cubic clip distance (in units of 64)");
153 // Radiant function table
154 // use to access what Radiant provides
155 _QERFuncTable_1 g_FuncTable;
156 _QERQglTable g_QglTable;
158 #define CONFIG_SECTION "Configuration"
160 #if defined(__linux__) || defined(__APPLE__)
162 static bool read_var (const char *filename, const char *section, const char *key, char *value)
164 char line[1024], *ptr;
167 rc = fopen (filename, "rt");
172 while (fgets (line, 1024, rc) != 0)
174 // First we find the section
178 ptr = strchr (line, ']');
181 if (strcmp (&line[1], section) == 0)
183 while (fgets (line, 1024, rc) != 0)
185 ptr = strchr (line, '=');
189 // reached the end of the section
195 if (strcmp (line, key) == 0)
197 strcpy (value, ptr+1);
200 while (value[strlen (value)-1] == 10 ||
201 value[strlen (value)-1] == 13 ||
202 value[strlen (value)-1] == 32)
203 value[strlen (value)-1] = 0;
214 static bool save_var (const char *filename, const char *section, const char *key, const char *value)
216 char line[1024], *ptr;
217 FILE *old_rc = NULL, *rc;
220 rc = fopen (filename, "rb");
227 char *tmpname = g_strdup_printf ("%s.tmp", filename);
228 old_rc = fopen (tmpname, "w+b");
231 fseek (rc, 0, SEEK_END);
234 buf = g_malloc (len);
235 fread (buf, len, 1, rc);
236 fwrite (buf, len, 1, old_rc);
242 rc = fopen (filename, "wb");
247 // First we need to find the section
250 while (fgets (line, 1024, old_rc) != NULL)
256 ptr = strchr (line, ']');
259 if (strcmp (&line[1], section) == 0)
270 fprintf (rc, "[%s]\n", section);
273 fprintf (rc, "%s=%s\n", key, value);
277 while (fgets (line, 1024, old_rc) != NULL)
279 ptr = strchr (line, '=');
285 if (strcmp (line, key) == 0)
298 while (fgets (line, 1024, old_rc) != NULL)
303 char *tmpname = g_strdup_printf ("%s.tmp", filename);
315 int INIGetInt(char *key, int def)
317 #if defined(__linux__) || defined(__APPLE__)
320 if (read_var (INIfn, CONFIG_SECTION, key, value))
325 return GetPrivateProfileInt(CONFIG_SECTION, key, def, INIfn);
329 void INISetInt(char *key, int val, char *comment /* = NULL */)
334 sprintf(s, "%d ; %s", val, comment);
336 sprintf(s, "%d", val);
337 #if defined(__linux__) || defined(__APPLE__)
338 save_var (INIfn, CONFIG_SECTION, key, s);
340 WritePrivateProfileString(CONFIG_SECTION, key, s, INIfn);
346 static const char *PLUGIN_NAME = "Portal Viewer";
347 // commands in the menu
348 static const char *PLUGIN_COMMANDS =
359 extern "C" LPVOID WINAPI QERPlug_GetFuncTable()
365 //extern "C" LPCSTR WINAPI QERPlug_Init (HMODULE hApp, GtkWidget* hwndMain)
366 extern "C" const char* QERPlug_Init (void *hApp, void* pMainWidget)
368 // Setup defaults & load config
371 return "Portal Viewer for Q3Radiant";
374 extern "C" const char* QERPlug_GetName()
376 return (char*)PLUGIN_NAME;
379 extern "C" const char* QERPlug_GetCommandList()
381 return (char*)PLUGIN_COMMANDS;
385 void Sys_Printf (char *text, ...)
390 va_start (argptr,text);
391 vsprintf (buf, text, argptr);
394 g_FuncTable.m_pfnSysMsg (buf);
398 bool interfaces_started = false;
400 static void CheckInterfaces()
402 if (interfaces_started)
407 interfaces_started = true;
410 extern "C" void QERPlug_Dispatch(const char* p, vec3_t vMin, vec3_t vMax, bool bSingleBrush)
412 Sys_Printf (MSG_PREFIX "Command \"%s\"\n",p);
414 if (!strcmp(p,Q3R_CMD_ABOUT))
418 else if (!strcmp(p,Q3R_CMD_LOAD))
422 if (interfaces_started)
424 if (DoLoadPortalFileDialog () == IDOK)
427 g_FuncTable.m_pfnSysUpdateWindows(UPDATE_ALL);
431 Sys_Printf(MSG_PREFIX "Portal file load aborted.\n", portals.fn);
435 else if (!strcmp(p,Q3R_CMD_RELEASE))
439 if (interfaces_started)
440 g_FuncTable.m_pfnSysUpdateWindows(UPDATE_ALL);
442 Sys_Printf(MSG_PREFIX "Portals unloaded.\n");
444 else if (!strcmp(p,Q3R_CMD_SHOW_2D))
446 portals.show_2d = !portals.show_2d;
448 if(interfaces_started)
449 g_FuncTable.m_pfnSysUpdateWindows(UPDATE_ALL);
453 Sys_Printf(MSG_PREFIX "Portals will be rendered in 2D view.\n");
455 Sys_Printf(MSG_PREFIX "Portals will NOT be rendered in 2D view.\n");
457 else if (!strcmp(p,Q3R_CMD_SHOW_3D))
459 portals.show_3d = !portals.show_3d;
462 if (interfaces_started)
463 g_FuncTable.m_pfnSysUpdateWindows(UPDATE_ALL);
466 Sys_Printf(MSG_PREFIX "Portals will be rendered in 3D view.\n");
468 Sys_Printf(MSG_PREFIX "Portals will NOT be rendered in 3D view.\n");
470 else if (!strcmp(p,Q3R_CMD_OPTIONS))
475 if (interfaces_started)
476 g_FuncTable.m_pfnSysUpdateWindows(UPDATE_ALL);
482 // =============================================================================
487 class CSynapseClientPrtView : public CSynapseClient
490 // CSynapseClient API
491 bool RequestAPI(APIDescriptor_t *pAPI);
492 const char* GetInfo();
494 CSynapseClientPrtView() { }
495 virtual ~CSynapseClientPrtView() { }
499 CSynapseServer* g_pSynapseServer = NULL;
500 CSynapseClientPrtView g_SynapseClient;
502 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces (const char *version, CSynapseServer *pServer)
504 if (strcmp(version, SYNAPSE_VERSION))
506 Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
509 g_pSynapseServer = pServer;
510 g_pSynapseServer->IncRef();
511 Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
513 g_SynapseClient.AddAPI(PLUGIN_MAJOR, PRTVIEW_MINOR, sizeof(_QERPluginTable));
515 g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(g_FuncTable), SYN_REQUIRE, &g_FuncTable);
516 g_SynapseClient.AddAPI(QGL_MAJOR, NULL, sizeof(g_QglTable), SYN_REQUIRE, &g_QglTable);
518 return &g_SynapseClient;
521 bool CSynapseClientPrtView::RequestAPI(APIDescriptor_t *pAPI)
523 if( !strcmp(pAPI->major_name, PLUGIN_MAJOR) )
525 if( !strcmp(pAPI->minor_name, PRTVIEW_MINOR) )
527 _QERPluginTable* pTable= static_cast<_QERPluginTable*>(pAPI->mpTable);
529 pTable->m_pfnQERPlug_Init = QERPlug_Init;
530 pTable->m_pfnQERPlug_GetName = QERPlug_GetName;
531 pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
532 pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
537 Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
543 const char* CSynapseClientPrtView::GetInfo()
545 return "PrtView module built " __DATE__ " " RADIANT_VERSION;