File: init_win32.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (181 lines) | stat: -rw-r--r-- 4,099 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
	$Id: init_win32.cpp,v 1.38 2002/01/16 02:00:57 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------

	File purpose:
		This file is the WinMain entry point. It will setup the clanCore
		win32 environment.

		This file also contain the win32 specific implementations
		of the CL_System class.

		The win32 versions of CL_SetupCore functions are also defined here.
*/

#include "Core/precomp.h" // visual c++: precompiled header file MUST be mentioned FIRST.

#ifdef BORLAND
#include <stdio.h>
#pragma hdrstop
#endif

#include "init_win32.h"
#include "API/Core/System/keep_alive.h"
#include "API/Core/System/setupcore.h"
#include "API/Core/System/error.h"
#include "API/Core/System/cl_assert.h"

class CL_Win32Event_Dispatcher : public CL_KeepAlive
{
public:
	virtual void keep_alive();
};

static void calc_commandline(int *argc, char ***argv);
int datafile_main(int argc, char **argv);

// Setup a CL_System::keep_alive() listener that will read win32 events
// and dispatch them.
CL_Win32Event_Dispatcher *event_dispatcher = NULL;

void CL_SetupCore::set_instance(HINSTANCE hInstance)
{
	CL_System_Win32::hInstance = hInstance;
}

static int init_ref_count = 0;
void init_system()
{
	init_ref_count++;
	if (init_ref_count > 1) return;

	event_dispatcher = new CL_Win32Event_Dispatcher;

	// if you get this assertion, you forgot to call CL_SetupCore::set_instance()
	// prior to CL_SetupCore::init().
	cl_assert(CL_System_Win32::hInstance != NULL);

	// Redirect C++ output streams to the output window in developer studio:
//	std::cout = iostream(&debug_buf);
//	cerr = iostream(&debug_buf);
}

void deinit_system()
{
	init_ref_count--;
	if (init_ref_count > 0) return;

	delete event_dispatcher;
	event_dispatcher = NULL;

	// And then shutdown core anyways - we assume the developer is an idiot!
//	CL_SetupCore::deinit_display();
//	CL_SetupCore::deinit_sound();
}

void CL_Win32Event_Dispatcher::keep_alive()
{
	// Check for win32 events and dispatch them to MainMessageHandler().

	MSG msg;

	while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
	{
		if (GetMessage(&msg, NULL, 0, 0))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			CL_System::sig_quit().call();
			break;
		}
	}
}

LONG WINAPI MainMessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	// Route the win32 events through our Win32 event listener list.
	// If no listener handles the event, pass it on to the default window procedure.

	bool handled = false;

	for (
		std::list<CL_Win32EventListener *>::iterator it = CL_System_Win32::listeners.begin();
		it != CL_System_Win32::listeners.end();
		it++)
	{
		if ((*it)->received_event(uMsg, wParam, lParam)) handled = true;
	}

	if (handled) return TRUE;

	switch (uMsg)
	{
	case WM_CLOSE:
		PostQuitMessage(0);
		break;

	case WM_PAINT:
		{
			PAINTSTRUCT ps;

			BeginPaint(hWnd, &ps);
			EndPaint(hWnd, &ps);
		}
		break;

	default:
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}

	return TRUE;
}

void CL_System_Win32::add_listener(CL_Win32EventListener *listener)
{
	listeners.push_back(listener);
}

void CL_System_Win32::remove_listener(CL_Win32EventListener *listener)
{
	listeners.remove(listener);
}

// Win32 implementation of CL_System functions:

unsigned int CL_System::get_time()
{
	static LARGE_INTEGER perf_frequency, perf_counter;
	static bool first_time = true;

	if (first_time)
	{
		QueryPerformanceFrequency(&perf_frequency);
		perf_frequency.QuadPart /= 1000;
		first_time = false;
	}

	QueryPerformanceCounter(&perf_counter);
	return (unsigned int) perf_counter.QuadPart / perf_frequency.QuadPart;
}

void CL_System::sleep(int millis)
{
	Sleep(millis);
}

// Global vars:

HINSTANCE CL_System_Win32::hInstance = NULL;
std::list<CL_Win32EventListener*> CL_System_Win32::listeners;