]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/spritemodel/plugin.cpp
0b3426fad6e5e0c58ab0df6da9274cef0c34a355
[xonotic/netradiant.git] / plugins / spritemodel / plugin.cpp
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 //\r
23 // Sprite Model Plugin\r
24 //\r
25 // Code by Hydra aka Dominic Clifton\r
26 //\r
27 // Based on MD3Model source code by SPoG\r
28 //\r
29 \r
30 /*\r
31     Overview\r
32     ========\r
33 \r
34 \r
35     Why ?\r
36     -----\r
37 \r
38     It allows the user to see a graphical representation of the entity in the 3D view (maybe 2D views later) where the entity would just otherwise be a non-descriptive coloured box.\r
39 \r
40     It is designed to be used with the entity view set to WireFrame (as the sprite images are rendered in the middle of the entity's bbox).\r
41 \r
42     How ?\r
43     -----\r
44 \r
45     Implemented as a model module, without any ISelect stuff.\r
46 \r
47     For an entity to use an image (instead of a model) you just update the entity defintion file so that the eclass_t's modelpath is filled in with a relative path and filename of an image file.\r
48 \r
49     e.g:\r
50 \r
51       baseq3/scripts/entities.def\r
52       ===========================\r
53 \r
54       \/\*QUAKED ammo_bfg (.3 .3 1) (-16 -16 -16) (16 16 16) SUSPENDED\r
55       ...\r
56       -------- MODEL FOR RADIANT ONLY - DO NOT SET THIS AS A KEY --------\r
57       model="sprites/powerups/ammo/bfgam.bmp"\*\/\r
58 \r
59 \r
60       valve/scripts/halflife.fgd\r
61       ==========================\r
62 \r
63       @PointClass iconsprite("sprites/lightbulb.spr") base(Target, Targetname, Light) = light : "Invisible   lightsource"\r
64       [\r
65               ...\r
66       ]\r
67 \r
68     What image formats are supported ?\r
69     ----------------------------------\r
70 \r
71     This module can load any image format that there is an active image module for.  For q3 this would be bmp, tga and jpg.  For Half-Life this would be hlw and spr.\r
72 \r
73     Version History\r
74     ===============\r
75 \r
76     v0.1 - 27/May/2002\r
77       - Created an inital implementation of a sprite model plugin.\r
78         According to the powers that be, it seems creating a model\r
79         plugin is hackish.\r
80         It works ok, but there is no way to attach models (sprites if you will)\r
81         to non-fixedsize entities (like func_bombtarget)\r
82         Also, I can't get the alpha map stuff right so I had to invert the alpha\r
83         mask in the spr loader so that 0xff = not drawn pixel.\r
84 \r
85     v0.2 - 10/March/2003\r
86       - Updated to coincide with Radiant 1.3.5 test builds.  Also, I made sure it worked\r
87         under quake3 and it does.\r
88 \r
89     v0.3 - 10/March/2003\r
90       - Added about box.\r
91 \r
92     ToDo\r
93     ====\r
94 \r
95     * make sprites always face the camera (is this done in camwindow.cpp ?)\r
96       but only if the entity model doesn't have "angle" keys.  At the moment\r
97       it's better to rotate the model with the angles.\r
98 \r
99     * maybe add an option to scale the sprites in the prefs ?\r
100 \r
101     * maybe convert to a new kind of class not based on model.\r
102 \r
103     * allow sprites on non-fixedsize ents\r
104 \r
105     * fix reversed alpha map in spr loader\r
106       -> is this actually broken?\r
107 \r
108     * allow an entity to have multiple models (e.g .md3 and a sprite model)\r
109       and allow the user to toggle either models on or off.\r
110 \r
111     * dynamically add the api's depending on what image loading modules are\r
112       supported by radiant.\r
113       Currently, we hard code to the list in "supportedmodelformats" (see below)\r
114       but, all these extensions are stripped when the actual image is loaded.\r
115       current the bit of code that decided what model api to use needs reworking\r
116       as it decides by looking at the extension of the model name, when in fact\r
117       we don't even need an extension.\r
118 \r
119       Previously the code fell though to use this model as the default model\r
120       plugin, but that also has issues.\r
121 \r
122       what it means is, in the .def files you must specify an image filename\r
123       that has one of the extensions listed below, but in actual fact radiant\r
124       will use any available image module to load the image.\r
125 \r
126 \r
127       e.g. you could use a model name of "sprites/target_speaker.tga" and have\r
128       a file called sprites/target_speaker.png and it would be correctly loaded\r
129       even if it not listed below in "supportedmodelformats".\r
130 \r
131       So, currently in the .def files you can just use the name\r
132       "sprites/target_speaker.spr" and it will load the file\r
133       from "sprites/target_speaker.*" which is what I propose anyone creating image sets for Q3/Wolf/etc does.\r
134 */\r
135 \r
136 #include "plugin.h"\r
137 \r
138 // =============================================================================\r
139 // Globals\r
140 \r
141 // function tables\r
142 _QERFuncTable_1 g_FuncTable;\r
143 _QERQglTable g_QglTable;\r
144 _QERShadersTable g_ShadersTable;\r
145 \r
146 // =============================================================================\r
147 // plugin implementation\r
148 \r
149 static const char *PLUGIN_NAME = "Sprite Model loading module";\r
150 \r
151 static const char *PLUGIN_COMMANDS = "About...";\r
152 \r
153 static const char *PLUGIN_ABOUT = "Sprite Model loading module v0.2 for GTKRadiant\n\n"\r
154                            "By Hydra!";\r
155 \r
156 char *supportedmodelformats[] = {"spr","bmp","tga","jpg","hlw",NULL}; // NULL is list delimiter\r
157 \r
158 static void add_model_apis(CSynapseClient& client)\r
159 {\r
160   char **ext;\r
161   for (ext = supportedmodelformats; *ext != NULL; ext++)\r
162   {\r
163     client.AddAPI(MODEL_MAJOR, *ext, sizeof(_QERPlugModelTable));\r
164   }\r
165 }\r
166 \r
167 static bool model_is_supported(const char* extension)\r
168 {\r
169   char **ext;\r
170   for (ext = supportedmodelformats; *ext != NULL; ext++)\r
171   {\r
172     if (stricmp(extension,*ext)==0)\r
173       return true;\r
174   }\r
175   return false;\r
176 }\r
177 \r
178 void init_filetypes()\r
179 {\r
180   char **ext;\r
181   for (ext = supportedmodelformats; *ext != NULL; ext++)\r
182   {\r
183     GetFileTypeRegistry()->addType(MODEL_MAJOR, filetype_t("sprite", *ext));\r
184   }\r
185 }\r
186 \r
187 extern "C" const char* QERPlug_Init (void *hApp, void* pMainWidget)\r
188 {\r
189   init_filetypes(); // see todo list above.\r
190   return (char *) PLUGIN_NAME;\r
191 }\r
192 \r
193 extern "C" const char* QERPlug_GetName ()\r
194 {\r
195   return (char *) PLUGIN_NAME;\r
196 }\r
197 \r
198 extern "C" const char* QERPlug_GetCommandList ()\r
199 {\r
200   return (char *) PLUGIN_COMMANDS;\r
201 }\r
202 \r
203 extern "C" void QERPlug_Dispatch (const char *p, vec3_t vMin, vec3_t vMax, bool bSingleBrush)\r
204 {\r
205         // NOTE: this never happens in a module\r
206   if(!strcmp(p, "About..."))\r
207                 g_FuncTable.m_pfnMessageBox(NULL, PLUGIN_ABOUT, "About", MB_OK, NULL);\r
208 }\r
209 \r
210 // =============================================================================\r
211 // SYNAPSE\r
212 \r
213 CSynapseServer* g_pSynapseServer = NULL;\r
214 CSynapseClientModel g_SynapseClient;\r
215     \r
216 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces (const char *version, CSynapseServer *pServer)\r
217 {\r
218   if (strcmp(version, SYNAPSE_VERSION))\r
219   {\r
220     Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);\r
221     return NULL;\r
222   }\r
223   g_pSynapseServer = pServer;\r
224   g_pSynapseServer->IncRef();\r
225   Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());\r
226 \r
227   add_model_apis(g_SynapseClient); // see todo list above.\r
228 \r
229   g_SynapseClient.AddAPI( PLUGIN_MAJOR, "sprite", sizeof( _QERPluginTable ) );\r
230   g_SynapseClient.AddAPI( RADIANT_MAJOR, NULL, sizeof( g_FuncTable ), SYN_REQUIRE, &g_FuncTable );\r
231   g_SynapseClient.AddAPI( QGL_MAJOR, NULL, sizeof( g_QglTable ), SYN_REQUIRE, &g_QglTable );\r
232   g_SynapseClient.AddAPI( SHADERS_MAJOR, "*", sizeof( g_ShadersTable ), SYN_REQUIRE, &g_ShadersTable );\r
233 \r
234   return &g_SynapseClient;\r
235 }\r
236 \r
237 bool CSynapseClientModel::RequestAPI(APIDescriptor_t *pAPI)\r
238 {\r
239   if (!strcmp(pAPI->major_name, MODEL_MAJOR))\r
240   {\r
241     _QERPlugModelTable* pTable= static_cast<_QERPlugModelTable*>(pAPI->mpTable);\r
242 \r
243     if (model_is_supported(pAPI->minor_name))  // see todo list above.\r
244     {\r
245       pTable->m_pfnLoadModel = &LoadSpriteModel;\r
246       return true;\r
247     }\r
248   }\r
249   else if (!strcmp(pAPI->major_name, PLUGIN_MAJOR))\r
250   {\r
251     _QERPluginTable* pTable= static_cast<_QERPluginTable*>(pAPI->mpTable);\r
252 \r
253     pTable->m_pfnQERPlug_Init = QERPlug_Init;\r
254     pTable->m_pfnQERPlug_GetName = QERPlug_GetName;\r
255     pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;\r
256     pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;\r
257     return true;\r
258   }\r
259 \r
260   Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());\r
261   return false;\r
262 }\r
263 \r
264 #include "version.h"\r
265 \r
266 const char* CSynapseClientModel::GetInfo()\r
267 {\r
268   return "Sprite Model module built " __DATE__ " " RADIANT_VERSION;\r
269 }\r
270 \r
271 const char* CSynapseClientModel::GetName()\r
272 {\r
273   return "sprite";\r
274 }\r