QE3.CPP 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "qe3.h"
  23. #include <direct.h>
  24. #include <sys/stat.h>
  25. #include "WaitDlg.h"
  26. QEGlobals_t g_qeglobals;
  27. /*
  28. =======================================================================================================================
  29. =======================================================================================================================
  30. */
  31. void WINAPI QE_CheckOpenGLForErrors(void) {
  32. CString strMsg;
  33. int i = qglGetError();
  34. if (i != GL_NO_ERROR) {
  35. if (i == GL_OUT_OF_MEMORY) {
  36. //
  37. // strMsg.Format("OpenGL out of memory error %s\nDo you wish to save before
  38. // exiting?", gluErrorString((GLenum)i));
  39. //
  40. if (g_pParentWnd->MessageBox(strMsg, EDITOR_WINDOWTEXT " Error", MB_YESNO) == IDYES) {
  41. Map_SaveFile(NULL, false);
  42. }
  43. exit(1);
  44. }
  45. else {
  46. // strMsg.Format("Warning: OpenGL Error %s\n ", gluErrorString((GLenum)i));
  47. common->Printf(strMsg.GetBuffer(0));
  48. }
  49. }
  50. }
  51. /*
  52. =======================================================================================================================
  53. =======================================================================================================================
  54. */
  55. bool DoesFileExist(const char *pBuff, long &lSize) {
  56. CFile file;
  57. if (file.Open(pBuff, CFile::modeRead | CFile::shareDenyNone)) {
  58. lSize += file.GetLength();
  59. file.Close();
  60. return true;
  61. }
  62. return false;
  63. }
  64. /*
  65. =======================================================================================================================
  66. =======================================================================================================================
  67. */
  68. bool ExtractPath_and_Filename(const char *pPath, CString &strPath, CString &strFilename) {
  69. CString strPathName = pPath;
  70. int nSlash = strPathName.ReverseFind('\\');
  71. if (nSlash >= 0) {
  72. strPath = strPathName.Left(nSlash + 1);
  73. strFilename = strPathName.Right(strPathName.GetLength() - nSlash - 1);
  74. }
  75. else {
  76. strFilename = pPath;
  77. }
  78. return true;
  79. }
  80. /*
  81. =======================================================================================================================
  82. =======================================================================================================================
  83. */
  84. void Map_Snapshot() {
  85. CString strMsg;
  86. //
  87. // we need to do the following 1. make sure the snapshot directory exists (create
  88. // it if it doesn't) 2. find out what the lastest save is based on number 3. inc
  89. // that and save the map
  90. //
  91. CString strOrgPath, strOrgFile;
  92. ExtractPath_and_Filename(currentmap, strOrgPath, strOrgFile);
  93. AddSlash(strOrgPath);
  94. strOrgPath += "snapshots";
  95. bool bGo = true;
  96. struct _stat Stat;
  97. if (_stat(strOrgPath, &Stat) == -1) {
  98. bGo = (_mkdir(strOrgPath) != -1);
  99. }
  100. AddSlash(strOrgPath);
  101. if (bGo) {
  102. int nCount = 0;
  103. long lSize = 0;
  104. CString strNewPath = strOrgPath;
  105. strNewPath += strOrgFile;
  106. CString strFile;
  107. while (bGo) {
  108. strFile.Format("%s.%i", strNewPath, nCount);
  109. bGo = DoesFileExist(strFile, lSize);
  110. nCount++;
  111. }
  112. // strFile has the next available slot
  113. Map_SaveFile(strFile.GetBuffer(0), false);
  114. Sys_SetTitle(currentmap);
  115. if (lSize > 12 * 1024 * 1024) { // total size of saves > 4 mb
  116. common->Printf
  117. (
  118. "The snapshot files in the [%s] directory total more than 4 megabytes. You might consider cleaning the directory up.",
  119. strOrgPath
  120. );
  121. }
  122. }
  123. else {
  124. strMsg.Format("Snapshot save failed.. unabled to create directory\n%s", strOrgPath);
  125. g_pParentWnd->MessageBox(strMsg);
  126. }
  127. }
  128. /*
  129. =======================================================================================================================
  130. QE_CheckAutoSave If five minutes have passed since making a change and the map hasn't been saved, save it out.
  131. =======================================================================================================================
  132. */
  133. void QE_CheckAutoSave(void) {
  134. static bool inAutoSave = false;
  135. static bool autoToggle = false;
  136. if (inAutoSave) {
  137. Sys_Status("Did not autosave due recursive entry into autosave routine\n");
  138. return;
  139. }
  140. if ( !mapModified ) {
  141. return;
  142. }
  143. inAutoSave = true;
  144. if ( g_PrefsDlg.m_bAutoSave ) {
  145. CString strMsg = g_PrefsDlg.m_bSnapShots ? "Autosaving snapshot..." : "Autosaving...";
  146. Sys_Status(strMsg.GetBuffer(0), 0);
  147. if (g_PrefsDlg.m_bSnapShots && stricmp(currentmap, "unnamed.map") != 0) {
  148. Map_Snapshot();
  149. } else {
  150. Map_SaveFile(ValueForKey(g_qeglobals.d_project_entity, (autoToggle == 0) ? "autosave1" : "autosave2" ), false, true);
  151. autoToggle ^= 1;
  152. }
  153. Sys_Status("Autosaving...Saved.", 0);
  154. mapModified = 0; // DHM - _D3XP
  155. } else {
  156. common->Printf("Autosave skipped...\n");
  157. Sys_Status("Autosave skipped...", 0);
  158. }
  159. inAutoSave = false;
  160. }
  161. /*
  162. =======================================================================================================================
  163. =======================================================================================================================
  164. */
  165. const char *g_pPathFixups[] = {
  166. "basepath",
  167. // "remotebasepath",
  168. "entitypath",
  169. // "texturepath",
  170. "autosave",
  171. // "mapspath"
  172. };
  173. const int g_nPathFixupCount = sizeof(g_pPathFixups) / sizeof (const char *);
  174. /*
  175. =======================================================================================================================
  176. QE_LoadProject
  177. =======================================================================================================================
  178. */
  179. bool QE_LoadProject(char *projectfile) {
  180. char *data;
  181. ID_TIME_T time;
  182. common->Printf("QE_LoadProject (%s)\n", projectfile);
  183. if ( fileSystem->ReadFile( projectfile, reinterpret_cast < void ** > (&data), &time) <= 0 ) {
  184. return false;
  185. }
  186. g_strProject = projectfile;
  187. g_PrefsDlg.m_strLastProject = projectfile;
  188. g_PrefsDlg.SavePrefs();
  189. CString strData = data;
  190. fileSystem->FreeFile( data );
  191. StartTokenParsing(strData.GetBuffer(0));
  192. g_qeglobals.d_project_entity = Entity_Parse(true);
  193. if (!g_qeglobals.d_project_entity) {
  194. Error("Couldn't parse %s", projectfile);
  195. }
  196. // set here some default project settings you need
  197. if (strlen(ValueForKey(g_qeglobals.d_project_entity, "brush_primit")) == 0) {
  198. SetKeyValue(g_qeglobals.d_project_entity, "brush_primit", "0");
  199. }
  200. g_qeglobals.m_bBrushPrimitMode = IntForKey(g_qeglobals.d_project_entity, "brush_primit");
  201. Eclass_InitForSourceDirectory(ValueForKey(g_qeglobals.d_project_entity, "entitypath"));
  202. g_Inspectors->FillClassList(); // list in entity window
  203. Map_New();
  204. // FillTextureMenu();
  205. FillBSPMenu();
  206. return true;
  207. }
  208. /*
  209. =======================================================================================================================
  210. QE_SaveProject £
  211. =======================================================================================================================
  212. */
  213. bool QE_SaveProject(const char *pProjectFile) {
  214. idFile *file = fileSystem->OpenFileWrite(pProjectFile);
  215. if ( !file ) {
  216. return false;
  217. }
  218. file->Write("{\n", 2);
  219. int count = g_qeglobals.d_project_entity->epairs.GetNumKeyVals();
  220. for (int i = 0; i < count; i++) {
  221. file->WriteFloatString( "\"%s\" \"%s\"\n", g_qeglobals.d_project_entity->epairs.GetKeyVal(i)->GetKey().c_str(), g_qeglobals.d_project_entity->epairs.GetKeyVal(i)->GetValue().c_str());
  222. }
  223. file->Write("}\n", 2);
  224. fileSystem->CloseFile( file );
  225. return true;
  226. }
  227. /* QE_KeyDown */
  228. #define SPEED_MOVE 32
  229. #define SPEED_TURN 22.5
  230. /*
  231. =======================================================================================================================
  232. ConnectEntities Sets target / name on the two entities selected from the first selected to the secon
  233. =======================================================================================================================
  234. */
  235. void ConnectEntities(void) {
  236. entity_t *e1;
  237. const char *target;
  238. idStr strTarget;
  239. int i, t;
  240. if (g_qeglobals.d_select_count < 2) {
  241. Sys_Status("Must have at least two brushes selected.", 0);
  242. Sys_Beep();
  243. return;
  244. }
  245. e1 = g_qeglobals.d_select_order[0]->owner;
  246. for (i = 0; i < g_qeglobals.d_select_count; i++) {
  247. if (g_qeglobals.d_select_order[i]->owner == world_entity) {
  248. Sys_Status("Can't connect to the world.", 0);
  249. Sys_Beep();
  250. return;
  251. }
  252. }
  253. for (i = 1; i < g_qeglobals.d_select_count; i++) {
  254. if (e1 == g_qeglobals.d_select_order[i]->owner) {
  255. Sys_Status("Brushes are from same entity.", 0);
  256. Sys_Beep();
  257. return;
  258. }
  259. }
  260. target = ValueForKey(e1, "target");
  261. if ( target && *target) {
  262. for (t = 1; t < 2048; t++) {
  263. target = ValueForKey(e1, va("target%i", t));
  264. if (target && *target) {
  265. continue;
  266. } else {
  267. break;
  268. }
  269. }
  270. } else {
  271. t = 0;
  272. }
  273. for (i = 1; i < g_qeglobals.d_select_count; i++) {
  274. target = ValueForKey(g_qeglobals.d_select_order[i]->owner, "name");
  275. if (target && *target) {
  276. strTarget = target;
  277. } else {
  278. UniqueTargetName(strTarget);
  279. }
  280. if (t == 0) {
  281. SetKeyValue(e1, "target", strTarget);
  282. } else {
  283. SetKeyValue(e1, va("target%i", t), strTarget);
  284. }
  285. t++;
  286. }
  287. Sys_UpdateWindows(W_XY | W_CAMERA);
  288. Select_Deselect();
  289. Select_Brush(g_qeglobals.d_select_order[1]);
  290. }
  291. /*
  292. =======================================================================================================================
  293. =======================================================================================================================
  294. */
  295. bool QE_SingleBrush(bool bQuiet, bool entityOK) {
  296. if ((selected_brushes.next == &selected_brushes) || (selected_brushes.next->next != &selected_brushes)) {
  297. if (!bQuiet) {
  298. Sys_Status("Error: you must have a single brush selected\n");
  299. }
  300. return false;
  301. }
  302. if (!entityOK && selected_brushes.next->owner->eclass->fixedsize) {
  303. if (!bQuiet) {
  304. Sys_Status("Error: you cannot manipulate fixed size entities\n");
  305. }
  306. return false;
  307. }
  308. return true;
  309. }
  310. /*
  311. =======================================================================================================================
  312. =======================================================================================================================
  313. */
  314. void QE_Init(void) {
  315. /* initialize variables */
  316. g_qeglobals.d_gridsize = 8;
  317. g_qeglobals.d_showgrid = true;
  318. /*
  319. * other stuff £
  320. * FIXME: idMaterial Texture_Init (true); Cam_Init (); XY_Init ();
  321. */
  322. Z_Init();
  323. }
  324. int g_numbrushes, g_numentities;
  325. /*
  326. =======================================================================================================================
  327. =======================================================================================================================
  328. */
  329. void QE_CountBrushesAndUpdateStatusBar(void) {
  330. static int s_lastbrushcount, s_lastentitycount;
  331. static bool s_didonce;
  332. // entity_t *e;
  333. brush_t *b, *next;
  334. g_numbrushes = 0;
  335. g_numentities = 0;
  336. if (active_brushes.next != NULL) {
  337. for (b = active_brushes.next; b != NULL && b != &active_brushes; b = next) {
  338. next = b->next;
  339. if (b->brush_faces) {
  340. if (!b->owner->eclass->fixedsize) {
  341. g_numbrushes++;
  342. }
  343. else {
  344. g_numentities++;
  345. }
  346. }
  347. }
  348. }
  349. /*
  350. * if ( entities.next != NULL ) { for ( e = entities.next ; e != &entities &&
  351. * g_numentities != MAX_MAP_ENTITIES ; e = e->next) { g_numentities++; } }
  352. */
  353. if (((g_numbrushes != s_lastbrushcount) || (g_numentities != s_lastentitycount)) || (!s_didonce)) {
  354. Sys_UpdateStatusBar();
  355. s_lastbrushcount = g_numbrushes;
  356. s_lastentitycount = g_numentities;
  357. s_didonce = true;
  358. }
  359. }