Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

init_win32.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: init_win32.cpp,v 1.30 2001/03/21 07:28:40 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 
00014         File purpose:
00015                 This file is the WinMain entry point. It will setup the clanCore
00016                 win32 environment and then afterwards call the ClanApplication
00017                 instance.
00018 
00019                 This file also contain the win32 specific implementations
00020                 of the CL_System class.
00021 
00022                 The win32 versions of CL_SetupCore functions are also defined here.
00023 */
00024 
00025 #include "Core/precomp.h" // visual c++: precompiled header file MUST be mentioned FIRST.
00026 
00027 #ifdef BORLAND
00028 #include <stdio.h>
00029 #pragma hdrstop
00030 #endif
00031 
00032 #include "init_win32.h"
00033 #include "API/Core/System/keep_alive.h"
00034 #include "API/Core/System/setupcore.h"
00035 #include "API/Core/System/error.h"
00036 #include "API/Core/System/cl_assert.h"
00037 
00038 #include <Hermes/hermes.h>
00039 
00040 class CL_Win32Event_Dispatcher : public CL_KeepAlive
00041 {
00042 public:
00043         virtual void keep_alive();
00044 };
00045 
00046 static void calc_commandline(int *argc, char ***argv);
00047 int datafile_main(int argc, char **argv);
00048 
00049 static HANDLE scrbuf = NULL;
00050 
00051 void redirect_to_console(const char *title)
00052 {
00053         AllocConsole();
00054         SetConsoleTitle(title);
00055         COORD coord;
00056         coord.X = 120;
00057         coord.Y = 80;
00058 //      coord.Y = 400;
00059         scrbuf =
00060                 CreateConsoleScreenBuffer(
00061                         GENERIC_READ | GENERIC_WRITE,
00062                         FILE_SHARE_READ | FILE_SHARE_WRITE,
00063                         NULL,
00064                         CONSOLE_TEXTMODE_BUFFER,
00065                         NULL);
00066 
00067         cl_assert(scrbuf != INVALID_HANDLE_VALUE);
00068 
00069         SetConsoleActiveScreenBuffer(scrbuf);
00070         SetConsoleScreenBufferSize(scrbuf, coord);
00071 
00072         freopen("CONIN$", "wt", stdin);
00073         freopen("CONOUT$", "wt", stderr);
00074         freopen("CONOUT$", "wt", stdout);
00075 }
00076 
00077 // Setup a CL_System::keep_alive() listener that will read win32 events
00078 // and dispatch them.
00079 CL_Win32Event_Dispatcher *event_dispatcher = NULL;
00080 
00081 void CL_SetupCore::set_instance(HINSTANCE hInstance)
00082 {
00083         CL_System_Win32::hInstance = hInstance;
00084 }
00085 
00086 static int init_ref_count = 0;
00087 void init_system()
00088 {
00089         init_ref_count++;
00090         if (init_ref_count > 1) return;
00091 
00092         event_dispatcher = new CL_Win32Event_Dispatcher;
00093 
00094         // if you get this assertion, you forgot to call CL_SetupCore::set_instance()
00095         // prior to CL_SetupCore::init().
00096         cl_assert(CL_System_Win32::hInstance != NULL);
00097 
00098         // If clanlib is compiled in debug mode, allocate a console window.
00099 #ifdef _DEBUG
00100 //      redirect_to_console("ClanLib debug console window");
00101 #endif
00102 
00103         std::cout << "$Id: init_win32.cpp,v 1.30 2001/03/21 07:28:40 mbn Exp $" << std::endl;
00104 
00105         // Redirect C++ output streams to the output window in developer studio:
00106 //      std::cout = iostream(&debug_buf);
00107 //      cerr = iostream(&debug_buf);
00108 }
00109 
00110 void deinit_system()
00111 {
00112         init_ref_count--;
00113         if (init_ref_count > 0) return;
00114 
00115         delete event_dispatcher;
00116         event_dispatcher = NULL;
00117 
00118         // And then shutdown core anyways - we assume the developer is an idiot!
00119 //      CL_SetupCore::deinit_display();
00120 //      CL_SetupCore::deinit_sound();
00121 
00122 #ifdef _DEBUG
00123         std::cout << std::endl << "(press any key to close debug console window)";
00124         std::cout.flush();
00125 
00126         // If your application crashes here, you are linking with a single threaded
00127         // libc in your application! -- mbn 13. Jan 2001.
00128         while (!kbhit()) Sleep(250);
00129 #endif
00130 }
00131 
00132 void CL_Win32Event_Dispatcher::keep_alive()
00133 {
00134         // Check for win32 events and dispatch them to MainMessageHandler().
00135 
00136         MSG msg;
00137 
00138         while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
00139         {
00140                 if (GetMessage(&msg, NULL, 0, 0))
00141                 {
00142                         TranslateMessage(&msg);
00143                         DispatchMessage(&msg);
00144                 }
00145                 else
00146                 {
00147                         throw CL_Error("Win32 event dispatcher received WM_QUIT");
00148                 }
00149         }
00150 }
00151 
00152 LONG WINAPI MainMessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
00153 {
00154         // Route the win32 events through our Win32 event listener list.
00155         // If no listener handles the event, pass it on to the default window procedure.
00156 
00157         bool handled = false;
00158 
00159         for (
00160                 std::list<CL_Win32EventListener *>::iterator it = CL_System_Win32::listeners.begin();
00161                 it != CL_System_Win32::listeners.end();
00162                 it++)
00163         {
00164                 if ((*it)->received_event(uMsg, wParam, lParam)) handled = true;
00165         }
00166 
00167         if (handled) return TRUE;
00168 
00169         switch (uMsg)
00170         {
00171         case WM_CLOSE:
00172                 PostQuitMessage(0);
00173                 break;
00174 
00175         case WM_PAINT:
00176                 {
00177                         PAINTSTRUCT ps;
00178 
00179                         BeginPaint(hWnd, &ps);
00180                         EndPaint(hWnd, &ps);
00181                 }
00182                 break;
00183 
00184         default:
00185                 return DefWindowProc(hWnd, uMsg, wParam, lParam);
00186         }
00187 
00188         return TRUE;
00189 }
00190 
00191 void CL_System_Win32::add_listener(CL_Win32EventListener *listener)
00192 {
00193         listeners.push_back(listener);
00194 }
00195 
00196 void CL_System_Win32::remove_listener(CL_Win32EventListener *listener)
00197 {
00198         listeners.remove(listener);
00199 }
00200 
00201 // Win32 implementation of CL_System functions:
00202 
00203 unsigned int CL_System::get_time()
00204 {
00205         return timeGetTime();
00206 }
00207 
00208 void CL_System::sleep(int millis)
00209 {
00210         Sleep(millis);
00211 }
00212 
00213 // Global vars:
00214 
00215 HINSTANCE CL_System_Win32::hInstance = NULL;
00216 std::list<CL_Win32EventListener*> CL_System_Win32::listeners;

Generated at Wed Apr 4 19:54:00 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001