gcsx_main.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* GCSx
  2. ** MAIN.CPP
  3. **
  4. ** Startup and exception catching
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #include "all.h"
  24. void gcsxTerminate() {
  25. fatalCrash(0, "(unknown error- terminate() called)");
  26. }
  27. void gcsxUnexpected() {
  28. fatalCrash(0, "(unknown error- unexpected() called)");
  29. }
  30. void doAllCleanup() {
  31. try { beginExitStage(); } catch (...) { }
  32. try { clearEvents(); } catch (...) { }
  33. try { clipboardClear(); } catch (...) { }
  34. try { if (desktop) delete desktop; } catch (...) { }
  35. desktop = NULL;
  36. try { exitDebug(); } catch (...) { }
  37. try { closeFont(); } catch (...) { }
  38. try { exitVideo(); } catch (...) { }
  39. try { SDL_Quit(); } catch (...) { }
  40. try { if (config) delete config; } catch (...) { }
  41. try { if (mainDir) delete mainDir; } catch (...) { }
  42. try { if (resourceDir) delete resourceDir; } catch (...) { }
  43. try { ResolutionDialog::destroy(); } catch (...) { }
  44. try { TileSetPropertiesDialog::destroy(); } catch (...) { }
  45. try { RGBSelect::destroy(); } catch (...) { }
  46. try { ScenePropertiesDialog::destroy(); } catch (...) { }
  47. try { LayerPropertiesDialog::destroy(); } catch (...) { }
  48. try { AnimGroupPropertiesDialog::destroy(); } catch (...) { }
  49. try { ScriptPropertiesDialog::destroy(); } catch (...) { }
  50. try { SpawnPropertiesDialog::destroy(); } catch (...) { }
  51. try { WorldPropertiesDialog::destroy(); } catch (...) { }
  52. try { ImportImgDialog::destroy(); } catch (...) { }
  53. try { ImageSelect::destroyGlobals(); } catch (...) { }
  54. try { destroyFileDialogGlobals(); } catch (...) { }
  55. try { destroyConsoleGlobals(); } catch (...) { }
  56. try { Tokenizer::destroyGlobals(); } catch (...) { }
  57. config = NULL;
  58. mainDir = NULL;
  59. resourceDir = NULL;
  60. }
  61. int main(int argc, char *argv[]) {
  62. int retValue = 0;
  63. set_terminate(gcsxTerminate);
  64. set_unexpected(gcsxUnexpected);
  65. #ifdef MEMDEBUG
  66. enableMemListTrack();
  67. #endif
  68. // Catch all fatal exceptions
  69. try {
  70. // Catch exceptions from startup
  71. try {
  72. // For bytecode
  73. vPtrSize = sizeof(void*) / sizeof(Uint32);
  74. if (sizeof(void*) % sizeof(Uint32)) ++vPtrSize;
  75. bcFloatSize = sizeof(BCfloat) / sizeof(Uint32);
  76. if (sizeof(BCfloat) % sizeof(Uint32)) ++bcFloatSize;
  77. // Assert that long long is 48bits+
  78. // Other types are asserted in SDL_types.h
  79. if (sizeof(long long) < 6) {
  80. fatalCrash(1, "Integer type of 48 or more bits not available");
  81. }
  82. // Debugging/console
  83. initDebugBuffer();
  84. // Version identifier
  85. debugWrite(DEBUG_VERSION, PRODUCT_NAME " version %d.%d.%d.%d", VER_MAJOR, VER_MINOR, VER_RELEASE, VER_BUILD);
  86. // Global objects with minimal initialization
  87. config = new Config();
  88. // Determine main/resource directories
  89. mainDir = new string;
  90. resourceDir = new string;
  91. getPathname(argv[0], *mainDir);
  92. createDirname(mainDir->c_str(), "resource", *resourceDir);
  93. // Initialize SDL
  94. initVideo();
  95. // OpenGL tests
  96. selectVideoMode(640, 480, -1, 0, 0, 1);
  97. oglTestCapabilities();
  98. // Initialize default video mode
  99. initDefaultVideo();
  100. // Other misc. initializations
  101. prepareFont();
  102. initDebugWindow();
  103. screenFlip();
  104. fillInterpreterTables();
  105. // (default to this stuff off; this also seems to jumpstart a few things
  106. // for example Alt+F4 doesn't seem to work without these initializing lines)
  107. SDL_EnableKeyRepeat(0, 0);
  108. SDL_EnableUNICODE(0);
  109. // Create global desktop object
  110. desktop = new Desktop;
  111. }
  112. // Deal with exceptions from startup
  113. catch (const BaseException& e) {
  114. doAllCleanup();
  115. // Any exception from start is fatal and we definately can't use our
  116. // GUI, so just use platform-specific alert box
  117. systemErrorBox(e.details, "Startup Error");
  118. return -1;
  119. }
  120. // (any non-gcsx exception- not likely)
  121. catch (...) {
  122. doAllCleanup();
  123. // Any exception from start is fatal and we definately can't use our
  124. // GUI, so just use platform-specific alert box
  125. systemErrorBox("Unhandled exception on startup!", "Startup Error");
  126. return -1;
  127. }
  128. // Start front end
  129. gcsxFrontEnd();
  130. }
  131. // Temporary catch-all exceptions
  132. // Our exceptions
  133. catch (BaseException& e) {
  134. beginExitStage();
  135. int didntGui = 0;
  136. try {
  137. // Use our own GUI here to display error
  138. guiErrorBox(string(e.details), errorTitleException);
  139. }
  140. catch (...) {
  141. didntGui = 1;
  142. }
  143. doAllCleanup();
  144. if (didntGui) {
  145. // We didn't do a GUI error
  146. systemErrorBox(e.details, "Internal Exception");
  147. }
  148. retValue = -1;
  149. }
  150. // C++ exceptions
  151. catch (const exception& e) {
  152. doAllCleanup();
  153. // Don't use our GUI, we don't know what caused this exception
  154. systemErrorBox(e.what(), "GCC Exception");
  155. retValue = -1;
  156. }
  157. // Other exceptions?
  158. catch (...) {
  159. doAllCleanup();
  160. // Don't use our GUI, we don't know what caused this exception
  161. systemErrorBox("(unknown error)", "Unhandled Exception");
  162. retValue = -1;
  163. }
  164. // Cleanup (some duplicate cleanup may occur if an exception occurred-
  165. // this is safe)
  166. doAllCleanup();
  167. #ifdef MEMDEBUG
  168. memLeakCheck();
  169. if (memDebugAnyErrors()) {
  170. systemErrorBox("One or more memory errors or leaks occurred- please check stdout.txt", "Memory Exception");
  171. }
  172. #endif
  173. return retValue;
  174. }