00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "Core/precomp.h"
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
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
00078
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
00095
00096 cl_assert(CL_System_Win32::hInstance != NULL);
00097
00098
00099 #ifdef _DEBUG
00100
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
00106
00107
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
00119
00120
00121
00122 #ifdef _DEBUG
00123 std::cout << std::endl << "(press any key to close debug console window)";
00124 std::cout.flush();
00125
00126
00127
00128 while (!kbhit()) Sleep(250);
00129 #endif
00130 }
00131
00132 void CL_Win32Event_Dispatcher::keep_alive()
00133 {
00134
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
00155
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
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
00214
00215 HINSTANCE CL_System_Win32::hInstance = NULL;
00216 std::list<CL_Win32EventListener*> CL_System_Win32::listeners;