00001 // /////////////////////////////////////////////////////////////////////////// 00002 // lesson00_main.cpp by Victor Dods, created 2006/07/25 00003 // /////////////////////////////////////////////////////////////////////////// 00004 // Unless a different license was explicitly granted in writing by the 00005 // copyright holder (Victor Dods), this software is freely distributable under 00006 // the terms of the GNU General Public License, version 2. Any works deriving 00007 // from this work must also be released under the GNU GPL. See the included 00008 // file LICENSE for details. 00009 // /////////////////////////////////////////////////////////////////////////// 00010 00011 00012 // /////////////////////////////////////////////////////////////////////////// 00013 // Lesson 00 - Initializing And Shutting Down The Engine 00014 // /////////////////////////////////////////////////////////////////////////// 00015 00016 /* @endcode 00019 This lesson will show you how to initialize the basic systems needed by 00020 XuqRijBuh -- initializing video mode and Singleton and shutting down. If 00021 you're reading the source file directly, instead of the doxygen-generated 00022 document, then pay no attention to the "code" and "endcode" tags in each 00023 comment. 00024 00025 <ul> 00026 <li>@ref lesson00_main.cpp "This lesson's source code"</li> 00027 <li>@ref lessons "Main lesson index"</li> 00028 </ul> 00029 00030 <strong>Procedural Overview</strong> 00031 00032 <ul> 00033 <li>Main function</li> 00034 <ul> 00035 <li>Initialization (video, sound, and whatever else is needed).</li> 00036 <li>Initialize game engine singletons (necessary for correct operation of the game engine).</li> 00037 <li>Create the Screen object. This is what sets the video mode. </li> 00038 <li>Execute game-specific code.</li> 00039 <li>Delete the Screen object. This does NOT reset the video mode.</li> 00040 <li>Shutdown game engine singletons (necessary for correct operation of the game engine).</li> 00041 <li>Shutdown (this is what resets the video mode).</li> 00042 </ul> 00043 </ul> 00044 00045 <strong>Code Diving!</strong> 00046 00047 To start off, the <tt>"xrb.hpp"</tt> header MUST be included in every source and 00048 header file, as it contains definitions necessary for the correct usage and 00049 operation of the game engine. 00050 @code */ 00051 #include "xrb.hpp" 00052 00053 #include "xrb_screen.hpp" // For use of the necessary Screen widget class. 00054 #include "xrb_sdlpal.hpp" // For use of the SDLPal platform abstraction layer. 00055 00056 /* @endcode 00057 Every declaration in the game engine library is within the <tt>Xrb</tt> 00058 namespace. Although this is pedantic, it is used to avoid any possible naming 00059 collision. It turns out not to be inconvenient, because of C++'s 00060 <tt>using</tt> keyword. This <tt>using</tt> statement is used so we don't 00061 need to qualify every library type/class/etc with <tt>Xrb::</tt> 00062 @code */ 00063 using namespace Xrb; 00064 00065 // This is just a helper function to group all the shutdown code together. 00066 void CleanUp () 00067 { 00068 fprintf(stderr, "CleanUp();\n"); 00069 00070 // Shutdown the platform abstraction layer. 00071 Singleton::Pal().Shutdown(); 00072 // Shutdown the game engine singletons. This is necessary for the 00073 // game engine to shutdown cleanly. 00074 Singleton::Shutdown(); 00075 } 00076 00077 int main (int argc, char **argv) 00078 { 00079 fprintf(stderr, "main();\n"); 00080 00081 /* @endcode 00082 This call initializes the game engine singleton facilities. This must 00083 be done, or the engine will just not work. The singletons include: 00084 <ul> 00085 <li>ResourceLibrary ensures that we only load one copy of certain 00086 resources (textures, fonts, sounds, etc) into memory.</li> 00087 <li>InputState provides accessors for the immediate state of the keyboard 00088 and mouse (and eventually joysticks, etc). This is not the 00089 primary or only means for user input, but we'll get to that 00090 later.</li> 00091 <li>KeyMap performs keyboard layout mapping (e.g. Dvorak), which is 00092 necessary only on Windows builds because the Windows version of 00093 SDL lacks proper key mapping.</li> 00094 <li>FTLibrary is used transparently by the font system to use the 00095 FreeType font rendering facilities (assuming SDLPal is in use). 00096 There's nothing to worry about. Trust me.</li> 00097 </ul> 00098 @code */ 00099 Singleton::Initialize(SDLPal::Create, "none"); 00100 00101 /* @endcode 00102 Initialize the platform abstraction layer (Pal). Then set the window caption. 00103 @code */ 00104 if (Singleton::Pal().Initialize() != Pal::SUCCESS) 00105 return 1; 00106 00107 Singleton::Pal().SetWindowCaption("XuqRijBuh Lesson 00"); 00108 00109 /* @endcode 00110 This call creates the Screen object and initializes the given video mode 00111 (800x600, 32 bit color). There is no constraint on the size or aspect 00112 ratio of the screen, apart from the ability of your video hardware to 00113 handle it. The Screen object is the root widget of the GUI widget 00114 hierarchy, and does a bunch of special handling to draw its child widgets 00115 properly. 00116 @code */ 00117 Screen *screen = Screen::Create( 00118 800, // video mode/screen width 00119 600, // video mode/screen height 00120 32, // video mode pixel bitdepth 00121 false); // not fullscreen -- for now 00122 /* @endcode 00123 If the Screen failed to initialize for whatever reason (probably because 00124 the system was unable to set the video mode), screen will be NULL. If 00125 this happens, print an error message and quit with an error code. 00126 @code */ 00127 if (screen == NULL) 00128 { 00129 // this shuts down the Pal and singletons. 00130 CleanUp(); 00131 // return with an error value. 00132 return 2; 00133 } 00134 00135 /* @endcode 00136 Here is where the game code goes. To avoid overloading your brain, for 00137 now we'll just pause for 5 seconds. 00138 @code */ 00139 { 00140 fprintf(stderr, "pausing for 5000 milliseconds...\n"); 00141 Singleton::Pal().Sleep(5000); 00142 } 00143 00144 /* @endcode 00145 Delete the Screen object, and with it the entire GUI widget hierarchy. 00146 @code */ 00147 Delete(screen); 00148 // this shuts down the Pal and the singletons. 00149 CleanUp(); 00150 // return with success value. 00151 return 0; 00152 } 00153 /* @endcode 00154 00155 <strong>Exercises</strong> 00156 00157 <ul> 00158 <li>Change the last parameter passed to Screen::Create -- currently 00159 <tt>false</tt> -- to <tt>true</tt>. This will cause the 00160 application to be run in fullscreen mode. Make sure not to use 00161 fullscreen mode while developing/debugging, because hitting a 00162 breakpoint while in fullscreen video mode will make your computer 00163 a touch ornery.</li> 00164 <li>With the last parameter to Screen::Create set to <tt>true</tt> 00165 (indicating fullscreen is enabled), change the video mode width, 00166 height and bitdepth to various values, seeing what video modes are 00167 supported by your hardware.</li> 00168 <li>Write code that parses the commandline arguments so you can specify 00169 what video mode width, height and bitdepth to use from the 00170 commandline.</li> 00171 </ul> 00172 00173 Thus concludes lesson00. Did it hurt? The fuck it didn't. 00174 */