main.cpp

Go to the documentation of this file.
00001 /**
00002 *       Main program.
00003 *
00004 *       Author:
00005 *                       Tomas Mrkvicka
00006 *                       xmrkvi03@stud.fit.vutbr.cz
00007 *
00008 *       Version:
00009 *                       1.0
00010 *
00011 *       Updates:
00012 *       --------
00013 *
00014 *       NEW VERSION             DATE            AUTHOR                                          COMMENTS
00015 *       -----------             --------        ------                                          --------
00016 *       1.0                             23-07-06        Tomas Mrkvicka                          File created.
00017 *
00018 */
00019 
00020 #include <windows.h>
00021 #include <d3dx9.h>
00022 
00023 #define APPNAME         "Joy4D Framework"
00024 
00025 #include "sources/system/GlobalsSystem.h"
00026 
00027 #include "sources/math/includes.h"
00028 #include "sources/graphic/includes.h"
00029 
00030 #include "sources/math/MathTest.h"
00031 
00032 //FORWARD DECLARATIONS
00033 HWND InitWindow(void);
00034 LRESULT CALLBACK MsgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
00035 
00036 
00037 /** This is testing function which is called from WinMain before MainLoop.
00038 */
00039 void TestFunction()
00040 {
00041         // here you may test your functions
00042         BOOL res = TMathTest::AllTests();
00043 
00044         res = !res;
00045 }
00046 
00047 /** This is main loop function for our program.
00048 */
00049 void MainLoop(void)
00050 {       
00051         {
00052                 MSG msg;        
00053                 BOOL cont = TRUE;
00054 
00055                 while ( cont )
00056                 {
00057                         while ( PeekMessage( &msg, NULL , 0, 0, PM_REMOVE) )
00058                         {
00059                                 if ( msg.message == WM_QUIT )
00060                                 {
00061                                         cont = FALSE;
00062                                         break;
00063                                 }
00064 
00065                                 if ( msg.message == WM_KEYDOWN )
00066                                 {
00067                                         if ( msg.wParam == VK_ESCAPE )
00068                                         {
00069                                                 cont = FALSE;                                           
00070                                         }
00071                                 }
00072 
00073                                 DispatchMessage( &msg );                                
00074                         }
00075 
00076                         TRenderer * r = TGlobalsGraphic::GetRenderer();
00077                         r->Clear( TRUE, TRUE, TRUE, TColor( 0x0000ff00), 0, 0 );
00078                         r->Present();
00079                 }
00080         }
00081 }
00082 
00083 /** Entry-point function of program.
00084 *
00085 * \param h_instance                     handle on this process
00086 * \param h_prev_instance        handle on previous process of this program 
00087 * \param cmdline                        command-line arguments
00088 * \param how_to_show            defines display properties of this window
00089 *
00090 * \return 0 if program runs correctly
00091 *
00092 */
00093 int _stdcall WinMain(HINSTANCE h_instance,HINSTANCE h_prev_instance,LPSTR cmdline,int how_to_show)
00094 {
00095         //window initialization
00096         HWND wnd = InitWindow();
00097 
00098         //we initialize our framework
00099 
00100         //SYSTEM
00101         if ( ! TGlobalsSystem::Initialize() )
00102         {
00103                 MessageBox( NULL, "Cannot initialize main framework - SYSTEM!", "Critical error", MB_OK| MB_ICONERROR );
00104                 return -1;
00105         }
00106 
00107         //MATH
00108         if ( ! TGlobalsMath::Initialize() )
00109         {
00110                 TGlobalsSystem::Destroy();
00111 
00112                 MessageBox( NULL, "Cannot initialize main framework - MATH!", "Critical error", MB_OK| MB_ICONERROR );
00113                 return -1;
00114         }
00115 
00116         //GRAPHIC
00117         if ( ! TGlobalsGraphic::Initialize( (void*)wnd ) )
00118         {
00119                 TGlobalsMath::Destroy();
00120                 TGlobalsSystem::Destroy();
00121 
00122                 MessageBox( NULL, "Cannot initialize main framework - GRAPHIC!", "Critical error", MB_OK| MB_ICONERROR );
00123                 return -1;
00124         }
00125 
00126         //TODO - here must be add all initialization routines
00127 
00128         TGlobalsSystem::GetLogFile()->Write( "FRAMEWORK INITALIZED SUCCESSFULLY.\n");
00129         TGlobalsSystem::GetLogFile()->Write(
00130                 "#######################################################################\n\n");
00131 
00132         //testing function - may be removed
00133         TestFunction(); 
00134 
00135         //we show window
00136         ShowWindow(wnd,SW_NORMAL);
00137 
00138         //HERE IS MAIN LOOP OF OUR PROGRAM
00139         MainLoop();
00140 
00141         TGlobalsSystem::GetLogFile()->Write(
00142                 "\n#######################################################################\n");
00143         TGlobalsSystem::GetLogFile()->Write( "STARTING DESTROYING FRAMEWORK.\n");
00144 
00145         // TODO here we must unitialize entire framework - we do it in reverse order
00146 
00147         //GRAPHIC
00148         TGlobalsGraphic::Destroy();
00149 
00150         //MATH
00151         TGlobalsMath::Destroy();
00152 
00153         //SYSTEM
00154         TGlobalsSystem::Destroy();
00155 
00156         return 0;
00157 }
00158 
00159 /** This fucntion creates application window.
00160 */
00161 HWND InitWindow(void)
00162 {
00163 HWND hWnd;
00164 
00165         WNDCLASSEX twc = 
00166         {
00167                         sizeof(WNDCLASSEX),
00168                         CS_CLASSDC,
00169                         MsgProc,
00170                         0L,
00171                         0L,
00172                         GetModuleHandle(NULL),
00173                         NULL,
00174                         NULL,
00175                         (HBRUSH)COLOR_WINDOW,
00176                         NULL,
00177                         APPNAME,
00178                         NULL
00179         };
00180 
00181 
00182         if(!RegisterClassEx( &twc )) return NULL;
00183 
00184     hWnd = 
00185                 CreateWindow
00186                 (
00187                         APPNAME,
00188                         APPNAME,
00189                         WS_OVERLAPPEDWINDOW,
00190                         0,
00191                         0,
00192                         640,
00193                         480,
00194                         GetDesktopWindow(),
00195                         NULL,
00196                         twc.hInstance,
00197                         NULL
00198                 );
00199 
00200         return hWnd;
00201 }
00202 
00203 /** This is application message procedure.
00204 */
00205 LRESULT CALLBACK MsgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
00206 {
00207         switch ( uMsg )
00208         {
00209                 case WM_DESTROY:
00210                 {
00211                         PostQuitMessage(0);
00212                         return 0;
00213                 }
00214                 break;
00215         
00216                 case WM_PAINT:
00217                 {
00218                         RECT rc;
00219                         GetWindowRect( hWnd, &rc);
00220 
00221                         PAINTSTRUCT ps;
00222                         HDC dc = BeginPaint( hWnd, &ps);
00223 
00224                         EndPaint( hWnd, &ps );
00225 
00226                         return 0;
00227                 }
00228                 break;
00229 
00230                 default:
00231                         return DefWindowProc( hWnd, uMsg, wParam, lParam ); 
00232         }
00233 
00234         return DefWindowProc( hWnd, uMsg, wParam, lParam ); 
00235 }

Generated on Wed Nov 28 22:36:07 2007 for Joy4D by  doxygen 1.4.6-NO