2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 // writes quake3 map format from internal objects
26 static int g_count_entities;
27 static int g_count_brushes;
30 extern int g_MapVersion;
32 void Float_Write(float data, IDataStream *out)
34 if (data == (int)data)
35 out->printf("%i ", (int)data);
37 out->printf("%f ", data);
40 void Patch_Write(patchMesh_t *pPatch, IDataStream *out)
45 // write shader name and matrix dimensions
46 str = pPatch->pShader->getName();
49 Sys_FPrintf(SYS_WRN, "WARNING: Patch_Write: shader names with spaces are not allowed, ignoring '%s'\n", str);
50 str = SHADER_NOT_FOUND;
52 if(!strncmp(str, "textures/", 9)) str+=9;
53 out->printf("patchDef2\n{\n%s\n( %i %i 0 0 0 )\n",
54 str, pPatch->width, pPatch->height);
58 for(i=0; i<pPatch->width; i++)
61 for(j=0; j<pPatch->height; j++)
65 Float_Write(pPatch->ctrl[i][j].xyz[0], out);
66 Float_Write(pPatch->ctrl[i][j].xyz[1], out);
67 Float_Write(pPatch->ctrl[i][j].xyz[2], out);
68 Float_Write(pPatch->ctrl[i][j].st[0], out);
69 Float_Write(pPatch->ctrl[i][j].st[1], out);
75 out->printf(")\n}\n");
78 void Face_Write (face_t *face, IDataStream *out, bool bAlternateTexdef = false)
89 Float_Write(face->planepts[i][j], out);
96 // write alternate texdef
99 Float_Write(face->brushprimit_texdef.coords[0][i], out);
102 Float_Write(face->brushprimit_texdef.coords[1][i], out);
107 str = face->texdef.GetName();
108 if (strchr(str, ' '))
110 Sys_FPrintf(SYS_WRN, "WARNING: Face_Write: shader names with spaces are not allowed, ignoring '%s'\n", str);
111 str = SHADER_NOT_FOUND;
113 if(!strncmp(str, "textures/", 9)) str+=9;
115 // Strip all remaining paths.
116 // FIXME: Hydra - this is actually a HalfLife specific bit, not Q2 map format specific.
117 if (g_MapVersion == MAPVERSION_HL)
120 while ( (pos = (char*)strchr( str, '/' )) != NULL ) {
121 str = pos+1; // to speed optimize, change the "while" to an "if"
124 out->printf("%s ", str);
126 if(!bAlternateTexdef)
129 out->printf("%i %i %i %f %f ",
130 (int)face->texdef.shift[0],
131 (int)face->texdef.shift[1],
132 (int)face->texdef.rotate,
133 face->texdef.scale[0],
134 face->texdef.scale[1]);
137 if (g_MapVersion == MAPVERSION_Q3)
139 // write surface flags
140 out->printf("%i %i %i\n",
141 face->texdef.contents,
146 if ( (g_MapVersion == MAPVERSION_HL) || (g_MapVersion == MAPVERSION_Q2) )
148 // write surface flags if non-zero values.
149 if (face->texdef.contents || face->texdef.flags || face->texdef.value)
151 out->printf("%i %i %i\n",
152 face->texdef.contents,
164 void Primitive_Write (brush_t *pBrush, IDataStream *out)
166 if ( (g_MapVersion == MAPVERSION_Q2) && (pBrush->patchBrush) )
168 Sys_FPrintf(SYS_WRN, "WARNING: Primitive_Write: Patches are not supported in Quake2, ignoring Brush %d\n", g_count_brushes++);
172 out->printf("// brush %i\n", g_count_brushes++);
174 if(pBrush->patchBrush)
175 Patch_Write(pBrush->pPatch, out);
176 else if(pBrush->bBrushDef)
178 out->printf("brushDef\n{\n");
179 for(face_t *face = pBrush->brush_faces; face != NULL; face = face->next)
180 Face_Write (face, out, true);
184 for(face_t *face = pBrush->brush_faces; face != NULL; face = face->next)
185 Face_Write (face, out);
190 void Entity_Write(entity_t *pEntity, IDataStream *out)
193 CPtrArray *brushes = (CPtrArray*)pEntity->pData;
194 out->printf("// entity %i\n", g_count_entities++);
196 for(pEpair = pEntity->epairs; pEpair != NULL; pEpair = pEpair->next)
197 out->printf("\"%s\" \"%s\"\n", pEpair->key, pEpair->value);
199 for(int i=0; i<brushes->GetSize(); i++)
200 Primitive_Write((brush_t*)brushes->GetAt(i), out);
204 void Map_Write (CPtrArray *map, IDataStream *out)
206 g_count_entities = 0;
207 for(int i=0; i<map->GetSize(); i++)
208 Entity_Write((entity_t*)map->GetAt(i), out);
211 void Map_WriteQ3 (CPtrArray *map, IDataStream *out)
213 g_MapVersion = MAPVERSION_Q3;
217 void Map_WriteHL (CPtrArray *map, IDataStream *out)
219 g_MapVersion = MAPVERSION_HL;
223 void Map_WriteQ2 (CPtrArray *map, IDataStream *out)
225 g_MapVersion = MAPVERSION_Q2;