This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhugo-main.cpp
319 lines (271 loc) · 7.19 KB
/
hugo-main.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "hugo.h"
DIBSection offscreen;
BOOL fActive=TRUE;
HWND hGameWindow;
LPDIRECTSOUND lpds;
int drawx, drawy, drawscale;
HANDLE hEvent;
LPBITMAPFILEHEADER lpbfPal;
int cdNumTracks;
Context * CurrentContext=NULL;
static charset * theCharSet;
static level * theLevel;
static Game * theGame;
static Menu * theMenu;
static Hiscore * theHighScore;
static HiscoreIn * theHighScoreIn;
static EnterLevNo * theEnterLevNo;
static EnterTime * theEnterTime;
static Editor * theEditor;
static GameOver * theGameOver;
volatile int up=0,down=0,left=0,right=0,fire=0,space=0;
int gEscalFlag=0;
#ifdef _DEBUG
#define CX 1024
#define CY 768
#else
#define CX 320
#define CY 200
#endif
void PlayCDTrack(int levno)
{
if (cdNumTracks==0) return;
int track=levno%cdNumTracks+2;
}
/*****************************************************/
/*****************************************************/
void SetTheObject(Context * newO)
{
if (CurrentContext) CurrentContext->Deactivate();
CurrentContext=newO;
CurrentContext->Activate();
}
/*****************************************************/
/*****************************************************/
void CreateObjects()
{
theCharSet=new charset(MAKEINTRESOURCE(IDR_CHARSET));
theLevel=new level(theCharSet);
theMenu=new Menu;
theHighScore=new Hiscore;
theHighScoreIn=new HiscoreIn(theHighScore);
theEnterLevNo=new EnterLevNo(200); //theLevel->GetLevelCount());
theEnterTime=new EnterTime;
theGame=new Game(theLevel,theCharSet);
theEditor=new Editor(theLevel,theCharSet);
theGameOver=new GameOver;
}
/*****************************************************/
/*****************************************************/
void DeleteObjects()
{
delete theGameOver;
delete theEditor;
delete theGame;
delete theEnterTime;
delete theEnterLevNo;
delete theHighScoreIn;
delete theHighScore;
delete theMenu;
delete theLevel;
delete theCharSet;
}
int MX(int x)
{
return (x - drawx) / drawscale;
}
int MY(int y)
{
return (y - drawy) / drawscale;
}
/*****************************************************/
/*****************************************************/
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
switch(msg)
{
case WM_ACTIVATEAPP:
fActive=wparam;
if (wparam)
{
SetFocus(hGameWindow);
ShowWindow(hGameWindow,SW_SHOWMAXIMIZED);
ShowCursor(FALSE);
if (CurrentContext)
{
CurrentContext->Draw();
}
}
else
{
ShowWindow(hGameWindow,SW_SHOWMINIMIZED);
ShowCursor(TRUE);
}
break;
case WM_COMMAND:
switch(LOWORD(wparam))
{
case HHC_STARTGAME:
theGame->StartGame(lparam);
SetTheObject(theGame);
break;
case HHC_ENTERSCORE:
SetTheObject(theHighScoreIn);
break;
case HHC_MENU:
SetTheObject(theMenu);
break;
case HHC_HIGHSCORES:
SetTheObject(theHighScore);
break;
case HHC_STARTLEVEL:
theEnterLevNo->SetAction(lparam);
SetTheObject(theEnterLevNo);
break;
case HHC_STARTEDITOR:
theEditor->SetLevel(lparam);
SetTheObject(theEditor);
break;
case HHC_ENTERTIME:
SetTheObject(theEnterTime);
break;
}
break;
case HHM_GAMEOVER:
theHighScoreIn->SetScore(wparam,lparam);
SetTheObject(theGameOver);
break;
case WM_NCHITTEST:
if (fActive) return HTCLIENT;
else return DefWindowProc(hwnd,msg,wparam,lparam);
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
if (CurrentContext) HANDLE_WM_LBUTTONDOWN(hwnd,MX(wparam),MY(lparam),CurrentContext->OnLButtonDown);
break;
case WM_LBUTTONUP:
if (CurrentContext) HANDLE_WM_LBUTTONUP(hwnd, MX(wparam), MY(lparam),CurrentContext->OnLButtonUp);
break;
case WM_RBUTTONDOWN:
if (CurrentContext) HANDLE_WM_RBUTTONDOWN(hwnd, MX(wparam), MY(lparam),CurrentContext->OnRButtonDown);
break;
case WM_RBUTTONUP:
if (CurrentContext) HANDLE_WM_RBUTTONUP(hwnd, MX(wparam), MY(lparam),CurrentContext->OnRButtonUp);
break;
case WM_MOUSEMOVE:
if (CurrentContext) HANDLE_WM_MOUSEMOVE(hwnd, MX(wparam), MY(lparam),CurrentContext->OnMouseMove);
break;
case WM_KEYDOWN:
if (CurrentContext) HANDLE_WM_KEYDOWN(hwnd,wparam,lparam,CurrentContext->OnKey);
break;
case WM_CHAR:
if (CurrentContext) HANDLE_WM_CHAR(hwnd,wparam,lparam,CurrentContext->OnChar);
break;
case WM_PAINT:
{
RECT r;
GetClientRect(hwnd, &r);
drawscale = min(r.right / 320, r.bottom / 200);
drawx = (r.right - 320 * drawscale) / 2;
drawy = (r.bottom - 200 * drawscale) / 2;
HDC dc = GetDC(hwnd);
HDC dcb = offscreen.GetDC();
StretchBlt(dc, drawx, drawy, 320*drawscale, 200*drawscale, dcb, 0, 0, 320, 200, SRCCOPY);
offscreen.ReleaseDC(dcb);
ReleaseDC(hwnd, dc);
break;
}
default:
return DefWindowProc(hwnd,msg,wparam,lparam);
}
return 0;
}
/*****************************************************/
/*****************************************************/
void CreateOffScreenSurface()
{
offscreen.Create(320, 200);
}
/*****************************************************/
/*****************************************************/
int WINAPI WinMain(HINSTANCE,HINSTANCE,char * ,int)
{
WNDCLASS wClass;
HINSTANCE hInst=GetModuleHandle(NULL);
MSG msg;
__int64 pff,pfc,nextpfc;
int c;
//hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
lpbfPal=(LPBITMAPFILEHEADER)GetResourcePointer(NULL,MAKEINTRESOURCE(IDR_PALETTE),"BINARY");
wClass.style=CS_VREDRAW|CS_HREDRAW|CS_BYTEALIGNCLIENT;
wClass.lpfnWndProc=MainWndProc;
wClass.cbClsExtra=wClass.cbWndExtra=0;
wClass.hInstance=hInst;
wClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wClass.hCursor=NULL;
wClass.hbrBackground=GetStockBrush(BLACK_BRUSH);
wClass.lpszMenuName=NULL;
wClass.lpszClassName="HugoClass";
RegisterClass(&wClass);
hGameWindow=CreateWindow("HugoClass","The Hugo",WS_POPUP,0,0,500,500,NULL,NULL,hInst,NULL);
ShowWindow(hGameWindow,SW_SHOWMAXIMIZED);
SetFocus(hGameWindow);
UpdateWindow(hGameWindow);
CreateOffScreenSurface();
DirectSoundCreate(NULL,&lpds,NULL);
if (lpds) lpds->SetCooperativeLevel(hGameWindow,DSSCL_NORMAL);
InitSounds();
CreateObjects();
SetTheObject(theMenu);
QueryPerformanceFrequency((LARGE_INTEGER*)&pff);
pff=(pff+28)/56;
QueryPerformanceCounter((LARGE_INTEGER*)&nextpfc);
nextpfc+=pff;
while(1)
{
while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message==WM_QUIT) goto brk2;
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_PAINT) break;
}
if (fActive)
{
QueryPerformanceCounter((LARGE_INTEGER*)&pfc);
if (pfc>=nextpfc)
{
nextpfc+=pff;
if (pfc>nextpfc)
{
c=(int)((pfc-nextpfc)/pff);
nextpfc+=pff*c;
c++;
if (c>6) c=6;
}
else c=1;
theCharSet->Animate(1);
CurrentContext->Loop(1);
InvalidateRect(hGameWindow, NULL, false);
}
}
else WaitMessage();
}
brk2:
DeleteSounds();
DeleteObjects();
ClipCursor(NULL);
return 0;
}
const void * GetResourcePointer(HINSTANCE hInst, char *rname, char * rtype)
{
HRSRC hRsrc;
HGLOBAL hGlobal;
if (!(hRsrc = FindResource(hInst, rname, rtype))) return NULL;
hGlobal = LoadResource(hInst, hRsrc);
return LockResource(hGlobal);
}