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.
27 #define Q3R_CMD_SPLITTER "-"
28 #define Q3R_CMD_ABOUT "About Portal Viewer"
29 #define Q3R_CMD_LOAD "Load .prt file"
30 #define Q3R_CMD_RELEASE "Unload .prt file"
31 #define Q3R_CMD_SHOW_3D "Toggle portals (3D)"
32 #define Q3R_CMD_SHOW_2D "Toggle portals (2D)"
33 #define Q3R_CMD_OPTIONS "Configure Portal Viewer"
35 static char INIfn[NAME_MAX];
37 /////////////////////////////////////////////////////////////////////////////
38 // CPrtViewApp construction
40 #define RENDER_2D "Render2D"
41 #define WIDTH_2D "Width2D"
42 #define AA_2D "AntiAlias2D"
43 #define COLOR_2D "Color2D"
45 #define RENDER_3D "Render3D"
46 #define WIDTH_3D "Width3D"
47 #define AA_3D "AntiAlias3D"
48 #define COLOR_3D "Color3D"
49 #define COLOR_FOG "ColorFog"
51 #define ZBUFFER "ZBuffer"
52 #define POLYGON "Polygons"
54 #define TRANS_3D "Transparency"
55 #define CLIP_RANGE "ClipRange"
61 char fn_drive[_MAX_DRIVE];
62 char fn_dir[_MAX_DIR];
63 char fn_name[_MAX_FNAME];
64 char fn_ext[_MAX_EXT];
66 GetModuleFileName( GetModuleHandle( "PrtView.dll" ), fn, _MAX_PATH );
68 _splitpath( fn, fn_drive, fn_dir, fn_name, fn_ext );
70 strcpy( INIfn, fn_drive );
71 strcat( INIfn, fn_dir );
72 strcat( INIfn, fn_name );
73 strcat( INIfn, ".ini" );
74 #else // if def __linux__
75 strcpy( INIfn, g_get_home_dir() );
76 strcat( INIfn, "/.radiant/" );
77 strcat( INIfn, RADIANT_VERSION );
78 strcat( INIfn, "/prtview.ini" );
81 portals.show_2d = INIGetInt( RENDER_2D, FALSE ) ? true : false;
82 portals.aa_2d = INIGetInt( AA_2D, FALSE ) ? true : false;
83 portals.width_2d = (float)INIGetInt( WIDTH_2D, 10 );
84 portals.color_2d = (COLORREF)INIGetInt( COLOR_2D, RGB( 0, 0, 255 ) ) & 0xFFFFFF;
86 if ( portals.width_2d > 40.0f ) {
87 portals.width_2d = 40.0f;
89 else if ( portals.width_2d < 2.0f ) {
90 portals.width_2d = 2.0f;
93 portals.show_3d = INIGetInt( RENDER_3D, TRUE ) ? true : false;
95 portals.zbuffer = INIGetInt( ZBUFFER, 1 );
96 portals.fog = INIGetInt( FOG, FALSE ) ? true : false;
97 portals.polygons = INIGetInt( POLYGON, TRUE );
98 portals.lines = INIGetInt( LINE, TRUE );
99 portals.aa_3d = INIGetInt( AA_3D, FALSE ) ? true : false;
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 ) ? true : false;
105 portals.clip_range = (float)INIGetInt( CLIP_RANGE, 16 );
107 if ( portals.clip_range < 1 ) {
108 portals.clip_range = 1;
110 else if ( portals.clip_range > 128 ) {
111 portals.clip_range = 128;
114 if ( portals.zbuffer < 0 ) {
117 else if ( portals.zbuffer > 2 ) {
121 if ( portals.width_3d > 40.0f ) {
122 portals.width_3d = 40.0f;
124 else if ( portals.width_3d < 2.0f ) {
125 portals.width_3d = 2.0f;
128 if ( portals.trans_3d > 100.0f ) {
129 portals.trans_3d = 100.0f;
131 else if ( portals.trans_3d < 0.0f ) {
132 portals.trans_3d = 0.0f;
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" );
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)" );
160 // Radiant function table
161 // use to access what Radiant provides
162 _QERFuncTable_1 g_FuncTable;
163 _QERQglTable g_QglTable;
165 #define CONFIG_SECTION "Configuration"
167 #if defined( __linux__ ) || defined( __APPLE__ )
169 static bool read_var( const char *filename, const char *section, const char *key, char *value ){
170 char line[1024], *ptr;
173 rc = fopen( filename, "rt" );
179 while ( fgets( line, 1024, rc ) != 0 )
181 // First we find the section
182 if ( line[0] != '[' ) {
186 ptr = strchr( line, ']' );
189 if ( strcmp( &line[1], section ) == 0 ) {
190 while ( fgets( line, 1024, rc ) != 0 )
192 ptr = strchr( line, '=' );
195 // reached the end of the section
201 if ( strcmp( line, key ) == 0 ) {
202 strcpy( value, ptr + 1 );
205 while ( value[strlen( value ) - 1] == 10 ||
206 value[strlen( value ) - 1] == 13 ||
207 value[strlen( value ) - 1] == 32 )
208 value[strlen( value ) - 1] = 0;
219 static bool save_var( const char *filename, const char *section, const char *key, const char *value ){
220 char line[1024], *ptr;
221 FILE *old_rc = NULL, *rc;
224 rc = fopen( filename, "rb" );
230 char *tmpname = g_strdup_printf( "%s.tmp", filename );
231 old_rc = fopen( tmpname, "w+b" );
234 fseek( rc, 0, SEEK_END );
237 buf = g_malloc( len );
238 fread( buf, len, 1, rc );
239 fwrite( buf, len, 1, old_rc );
245 rc = fopen( filename, "wb" );
251 // First we need to find the section
253 if ( old_rc != NULL ) {
254 while ( fgets( line, 1024, old_rc ) != NULL )
258 if ( line[0] == '[' ) {
259 ptr = strchr( line, ']' );
262 if ( strcmp( &line[1], section ) == 0 ) {
272 fprintf( rc, "[%s]\n", section );
275 fprintf( rc, "%s=%s\n", key, value );
277 if ( old_rc != NULL ) {
278 while ( fgets( line, 1024, old_rc ) != NULL )
280 ptr = strchr( line, '=' );
285 if ( strcmp( line, key ) == 0 ) {
299 while ( fgets( line, 1024, old_rc ) != NULL )
304 char *tmpname = g_strdup_printf( "%s.tmp", filename );
316 int INIGetInt( const char *key, int def ){
317 #if defined( __linux__ ) || defined( __APPLE__ )
320 if ( read_var( INIfn, CONFIG_SECTION, key, value ) ) {
321 return atoi( value );
327 return GetPrivateProfileInt( CONFIG_SECTION, key, def, INIfn );
331 void INISetInt( const char *key, int val, const char *comment /* = NULL */ ){
335 sprintf( s, "%d ; %s", val, comment );
338 sprintf( s, "%d", val );
340 #if defined( __linux__ ) || defined( __APPLE__ )
341 save_var( INIfn, CONFIG_SECTION, key, s );
343 WritePrivateProfileString( CONFIG_SECTION, key, s, INIfn );
349 static const char *PLUGIN_NAME = "Portal Viewer";
350 // commands in the menu
351 static const char *PLUGIN_COMMANDS =
362 extern "C" LPVOID WINAPI QERPlug_GetFuncTable(){
367 //extern "C" LPCSTR WINAPI QERPlug_Init (HMODULE hApp, GtkWidget* hwndMain)
368 extern "C" const char* QERPlug_Init( void *hApp, void* pMainWidget ){
369 // Setup defaults & load config
372 return "Portal Viewer for Q3Radiant";
375 extern "C" const char* QERPlug_GetName(){
376 return (char*)PLUGIN_NAME;
379 extern "C" const char* QERPlug_GetCommandList(){
380 return (char*)PLUGIN_COMMANDS;
384 void Sys_Printf (char *text, ...)
389 va_start (argptr,text);
390 vsprintf (buf, text, argptr);
393 g_FuncTable.m_pfnSysMsg (buf);
397 bool interfaces_started = false;
399 static void CheckInterfaces(){
400 if ( interfaces_started ) {
406 interfaces_started = true;
409 extern "C" void QERPlug_Dispatch( const char* p, vec3_t vMin, vec3_t vMax, bool bSingleBrush ){
410 Sys_Printf( MSG_PREFIX "Command \"%s\"\n",p );
412 if ( !strcmp( p,Q3R_CMD_ABOUT ) ) {
415 else if ( !strcmp( p,Q3R_CMD_LOAD ) ) {
418 if ( interfaces_started ) {
419 if ( DoLoadPortalFileDialog() == IDOK ) {
421 g_FuncTable.m_pfnSysUpdateWindows( UPDATE_ALL );
425 Sys_Printf( MSG_PREFIX "Portal file load aborted.\n", portals.fn );
429 else if ( !strcmp( p,Q3R_CMD_RELEASE ) ) {
432 if ( interfaces_started ) {
433 g_FuncTable.m_pfnSysUpdateWindows( UPDATE_ALL );
436 Sys_Printf( MSG_PREFIX "Portals unloaded.\n" );
438 else if ( !strcmp( p,Q3R_CMD_SHOW_2D ) ) {
439 portals.show_2d = !portals.show_2d;
441 if ( interfaces_started ) {
442 g_FuncTable.m_pfnSysUpdateWindows( UPDATE_ALL );
446 if ( portals.show_2d ) {
447 Sys_Printf( MSG_PREFIX "Portals will be rendered in 2D view.\n" );
450 Sys_Printf( MSG_PREFIX "Portals will NOT be rendered in 2D view.\n" );
453 else if ( !strcmp( p,Q3R_CMD_SHOW_3D ) ) {
454 portals.show_3d = !portals.show_3d;
457 if ( interfaces_started ) {
458 g_FuncTable.m_pfnSysUpdateWindows( UPDATE_ALL );
461 if ( portals.show_3d ) {
462 Sys_Printf( MSG_PREFIX "Portals will be rendered in 3D view.\n" );
465 Sys_Printf( MSG_PREFIX "Portals will NOT be rendered in 3D view.\n" );
468 else if ( !strcmp( p,Q3R_CMD_OPTIONS ) ) {
472 if ( interfaces_started ) {
473 g_FuncTable.m_pfnSysUpdateWindows( UPDATE_ALL );
480 // =============================================================================
483 class CSynapseClientPrtView : public CSynapseClient
486 // CSynapseClient API
487 bool RequestAPI( APIDescriptor_t *pAPI );
488 const char* GetInfo();
490 CSynapseClientPrtView() { }
491 virtual ~CSynapseClientPrtView() { }
495 CSynapseServer* g_pSynapseServer = NULL;
496 CSynapseClientPrtView g_SynapseClient;
499 #pragma GCC visibility push(default)
501 extern "C" CSynapseClient * SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces( const char *version, CSynapseServer *pServer ) {
503 #pragma GCC visibility pop
505 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 ){
522 if ( !strcmp( pAPI->major_name, PLUGIN_MAJOR ) ) {
523 if ( !strcmp( pAPI->minor_name, PRTVIEW_MINOR ) ) {
524 _QERPluginTable* pTable = static_cast<_QERPluginTable*>( pAPI->mpTable );
526 pTable->m_pfnQERPlug_Init = QERPlug_Init;
527 pTable->m_pfnQERPlug_GetName = QERPlug_GetName;
528 pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
529 pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
534 Syn_Printf( "ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo() );
540 const char* CSynapseClientPrtView::GetInfo(){
541 return "PrtView module built " __DATE__ " " RADIANT_VERSION;