aboutsummaryrefslogtreecommitdiff
path: root/sword2/driver/rdwin.cpp
blob: 101c4cf7e2affa715d25a3a68ca15e348316b449 (plain)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* Copyright (C) 1994-2003 Revolution Software Ltd
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#define WIN32_LEAN_AND_MEAN

//#include <windows.h>
//#include <windowsx.h>
#include <stdio.h>

#include "common/stdafx.h"
#include "common/engine.h"

#include "driver96.h"

#include "_mouse.h"
#include "keyboard.h"
#include "rdwin.h"
#include "d_draw.h"
#include "palette.h"
#include "render.h"
#include "menu.h"
#include "d_sound.h"
#include "../sword2.h"


#define MENUDEEP 40		// Temporary, until menu.h is written!



//static BOOL		bMouseVisible = FALSE;
//static BOOL		controlKey = FALSE;
//static BOOL		altKey = FALSE;
//static BOOL		wasScreenSaverActive = FALSE;
//static BOOL		myAppClosed = FALSE;
static BOOL		controlQDisabled = FALSE;
//static uint8	gameName[80];


//BOOL			gotTheFocus = FALSE;
//assume we always have focus for the time being - khalek
BOOL			gotTheFocus = TRUE; 
//HWND			hwnd;
//RECT			rcWindow;



//-----------------------------------------------------------------------------

void Zdebug(const char *format,...) {
#ifdef __PALM_OS__
	char buf[256]; // 1024 is too big overflow the stack
#else
	char buf[1024];
#endif
	va_list va;

	va_start(va, format);
	vsprintf(buf, format, va);
	va_end(va);

#ifdef __GP32__ //ph0x FIXME: implement fprint?
	printf("ZDEBUG: %s\n", buf);
#else
	fprintf(stderr, "ZDEBUG: %s!\n", buf);
#endif
#if defined( USE_WINDBG )
	strcat(buf, "\n");
#if defined( _WIN32_WCE )
	TCHAR buf_unicode[1024];
	MultiByteToWideChar(CP_ACP, 0, buf, strlen(buf) + 1, buf_unicode, sizeof(buf_unicode));
	OutputDebugString(buf_unicode);
#else
	OutputDebugString(buf);
#endif
#endif
}

/*
void Zdebug(char *format,...)	//Tony's special debug logging file March96
{
	warning("stub Zdebug");
//	Write a printf type string to a debug file

	va_list		arg_ptr;			// Variable argument pointer
	FILE *		debug_filep=0;			// Debug file pointer
	static int	first_debug = 1;		// Flag for first time this is used

	va_start(arg_ptr,format);

	if (first_debug)					//First time round delete any previous debug file
	{
		unlink("debug.txt");
		first_debug = 0;
	}

	debug_filep = fopen("debug.txt","a+t");

	if (debug_filep != NULL)	// if it could be opened
	{
		vfprintf(debug_filep, format, arg_ptr);
		fprintf(debug_filep,"\n");

		fclose(debug_filep);
	}
}
*/
//-----------------------------------------------------------------------------

/*
void Message(LPSTR fmt, ...)
{
    char	buff[256];
    va_list	va;

    
	va_start(va, fmt);

    //
    // format message with header
    //
    lstrcpy( buff, "DRIVER96:" );
    wvsprintf( &buff[lstrlen(buff)], fmt, va );
    lstrcat( buff, "\r\n" );

    //
    // To the debugger
    //
    OutputDebugString( buff );

}
*/

//-----------------------------------------------------------------------------
//	OSystem Event Handler. Full of cross platform goodness and 99% fat free!
//-----------------------------------------------------------------------------
void Sword2State::parseEvents() {
	OSystem::Event event;
	
	while (_system->poll_event(&event)) {
		switch(event.event_code) {
		
		case OSystem::EVENT_KEYDOWN:
			Zdebug("key %d", event.kbd.keycode);

			if (event.kbd.flags==OSystem::KBD_CTRL) {
				if (event.kbd.keycode == 'w')
					GrabScreenShot();
			}
			WriteKey(event.kbd.keycode);

			break;
		case OSystem::EVENT_MOUSEMOVE:
			mousex = event.mouse.x;
			mousey = event.mouse.y;
			_system->set_mouse_pos(event.mouse.x, event.mouse.y);
			break;
		case OSystem::EVENT_LBUTTONDOWN:
			LogMouseEvent(RD_LEFTBUTTONDOWN);
			break;
		case OSystem::EVENT_RBUTTONDOWN:
			LogMouseEvent(RD_RIGHTBUTTONDOWN);
			break;
		case OSystem::EVENT_LBUTTONUP:
			LogMouseEvent(RD_LEFTBUTTONUP);
			break;
		case OSystem::EVENT_RBUTTONUP:
			LogMouseEvent(RD_RIGHTBUTTONUP);
			break;
		case OSystem::EVENT_QUIT:
			Close_game();
			RestoreDisplay();
			CloseAppWindow();
			break;
		default:
			break;
		}
	}
		
}


//-----------------------------------------------------------------------------
//	Windows Message Handler.  All keyboard and mouse input is handled here.
//-----------------------------------------------------------------------------
/*
long FAR PASCAL WindowsMessageHandler(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{

	switch( message )
	{


	case WM_TIMER:
		switch (wParam)
		{
		case 25:
			FadeServer();
			return(0);
		case 1:
			FxServer();
			return(0);
		}
		break;
		

    case WM_CLOSE:
		Zdebug("WM_CLOSE");
		break;

    case WM_SIZE:
    case WM_MOVE:
        if (IsIconic(hwnd))
        {
            Message("minimising");
//            PauseGame();
        }

        if (bFullScreen)
        {
            SetRect(&rcWindow, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
        }
        else
        {
            GetClientRect(hwnd, &rcWindow);
            ClientToScreen(hwnd, (LPPOINT)&rcWindow);
            ClientToScreen(hwnd, (LPPOINT)&rcWindow+1);
        }
        Message("WINDOW RECT: [%d,%d,%d,%d]", rcWindow.left, rcWindow.top, rcWindow.right, rcWindow.bottom);
		if (bFullScreen)
		{
			SetCapture(hwnd);
		}
		else
		{
			ReleaseCapture();
		}
//		SetCursor(NULL);
//		ShowCursor(FALSE);
        break;



	case WM_ACTIVATEAPP:
		gotTheFocus = wParam;
		if (gotTheFocus)
		{
			Message("Got the focus");
			bMouseVisible = FALSE;
			Message("Mouse invisible");
			ShowCursor(FALSE);
		}
		else
		{
			if (bMouseVisible == FALSE)
				ShowCursor(TRUE);
			Message("Lost the focus");
			bMouseVisible = TRUE;
			Message("Mouse visible");
		}
		break;


    case WM_SYSKEYUP:
        switch( wParam )
        {

//			int32 rv;

        // handle ALT+ENTER (fullscreen)
        case VK_RETURN:
			break;
        }
        break;
 

	case WM_DISPLAYCHANGE:
		break;

	case WM_CREATE:
		SystemParametersInfo(SPI_GETSCREENSAVEACTIVE , 0 , &wasScreenSaverActive , 0);
		if (wasScreenSaverActive)
		{
			SystemParametersInfo(SPI_SETSCREENSAVEACTIVE , FALSE , 0 , 0 );
		}
    	break;


    case WM_QUERYNEWPALETTE:
        //
        //  we are getting the palette focus, select our palette
        //
        if (!bFullScreen && lpPalette && lpPrimarySurface)
        {
			int32 hr;

            hr = IDirectDrawSurface_SetPalette(lpPrimarySurface, lpPalette);
            if (hr == DDERR_SURFACELOST)
            {
                IDirectDrawSurface_Restore(lpPrimarySurface);

                hr= IDirectDrawSurface_SetPalette(lpPrimarySurface, lpPalette);
                if(hr == DDERR_SURFACELOST)
                {
                   Message("Failed to restore palette after second try");
                }
            }

            //
            // Restore normal title if palette is ours
            //
            if(hr == DD_OK)
            {
                SetWindowText(hwnd, gameName);
            }
        }
        break;

    case WM_PALETTECHANGED:
        //
        //  if another app changed the palette we dont have full control
        //  of the palette. NOTE this only applies for FoxBear in a window
        //  when we are fullscreen we get all the palette all of the time.
        //
        if ((HWND)wParam != hwnd)
        {
            if( !bFullScreen )
            {
				Message("Lost palette but continuing");
            }
        }
        break;


//    case WM_SETCURSOR:
//		if (bMouseVisible)
//			SetCursor(LoadCursor(NULL, IDC_ARROW));
//		else
//			SetCursor(NULL);
//	    return TRUE;
//		break;

	case WM_CHAR:
		if (lParam & (1 << 30))
			return(0);
		WriteKey((char) (wParam & 0xff));
		return(0);

    case WM_KEYDOWN:

		Zdebug("key %d", wParam);

		switch( wParam )
		{
			case VK_CONTROL:
				controlKey = TRUE;
				break;

//			case VK_ALT:
//				altKey = TRUE;
//				break;

			case 'W':
				if (controlKey)
					GrabScreenShot();
				return 0;

			case 'Q':
				if (controlKey && !controlQDisabled)
					DestroyWindow( hWnd );
  				return 0;

			case 'F4':
				DestroyWindow( hWnd );
  				return 0;

        }
      	break;


	case WM_KEYUP:
		switch(wParam)
		{
			case VK_CONTROL:
				controlKey = FALSE;
				break;

//			case VK_ALT:
//				altKey = FALSE;
//				break;

		}
	    break;


    case WM_DESTROY:
		Zdebug("*destroy*");

		if (wasScreenSaverActive)
			SystemParametersInfo(SPI_SETSCREENSAVEACTIVE , TRUE , 0 , 0 );
        PostQuitMessage( 0 );
        break;


	case WM_MOUSEMOVE:
		mousex = lParam & 0xffff;

		if (bFullScreen)
		{
			mousey = (uint16) (lParam >> 16) - MENUDEEP;
		}
		else
		{
			mousey = (uint16) (lParam >> 16) - MENUDEEP;

			if (mousex < 0)
				mousex = 0;
			if (mousex >= RENDERWIDE)
				mousex = RENDERWIDE-1;
		}

		if (mousey < -MENUDEEP)
			mousey = -MENUDEEP;
		if (mousey >= RENDERDEEP + MENUDEEP)
			mousey = RENDERDEEP + MENUDEEP - 1;

		return(0);

	case WM_LBUTTONDOWN:
		LogMouseEvent(RD_LEFTBUTTONDOWN);
		return(0);

	case WM_LBUTTONUP:
		LogMouseEvent(RD_LEFTBUTTONUP);
		return(0);

	case WM_RBUTTONDOWN:
		LogMouseEvent(RD_RIGHTBUTTONDOWN);
		return(0);

	case WM_RBUTTONUP:
		LogMouseEvent(RD_RIGHTBUTTONUP);
		return(0);

	case WM_LBUTTONDBLCLK:
		LogMouseEvent(RD_LEFTBUTTONDOWN);
		return(0);
		
	case WM_RBUTTONDBLCLK:
		LogMouseEvent(RD_RIGHTBUTTONDOWN);


	case WM_SYSCOMMAND:
		if (gotTheFocus)
			return(0);




	}

	return DefWindowProc(hWnd, message, wParam, lParam);

}
*/


/*
int32 InitialiseWindow(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow, char *gameName)

{
	
	WNDCLASS    wc;
//	uint32		err;


//	hPrevInstance = hPrevInstance;
	wc.style = CS_DBLCLKS;
	wc.lpfnWndProc = WindowsMessageHandler;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon( hInstance, "resourc1");  //IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = GetStockObject(BLACK_BRUSH);

	wc.lpszMenuName = gameName;
	wc.lpszClassName = gameName;

	RegisterClass( &wc );

	// Create a window
	hwnd = CreateWindowEx(WS_EX_APPWINDOW, gameName, gameName, WS_VISIBLE | WS_SYSMENU | WS_POPUP, 0, 0,
			GetSystemMetrics( SM_CXSCREEN ), GetSystemMetrics( SM_CYSCREEN ),
			NULL, NULL, hInstance, NULL );

	if(!hwnd)
	{
	    MessageBox(hwnd, "Failed to create window", gameName, MB_OK );
	    DestroyWindow(hwnd);
		return(RDERR_CREATEWINDOW);
	}

//	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);
	SetFocus(hwnd);
	
	SetTimer(hwnd, 25, 1000 / 25, NULL);
	SetTimer(hwnd, 1, 100, NULL);

	return(RD_OK);

}
*/


int32 CloseAppWindow(void)

{
	warning("stub CloseAppWindow");
/*
	DestroyWindow(hwnd);
*/
	// just quit for now
	g_system->quit();
	return(RD_OK);

}



int32 ServiceWindows(void)

{
	g_sword2->parseEvents();
//	warning("stub ServiceWindows");  // too noisy
/*
	MSG msg;

	if (myAppClosed)
		return(RDERR_APPCLOSED);

	while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
	{
		if (!GetMessage(&msg, NULL, 0, 0))
		{
			myAppClosed = TRUE;
			return(RDERR_APPCLOSED);
		}

		TranslateMessage(&msg);
		DispatchMessage(&msg);
  	}
*/
	return(RD_OK);

}




int32 _ReportDriverError(int32 error, const uint8 *filename, uint32 line)

{
	warning("stub _ReportDriverError 0x%.8x file: %s, line: %d ", error, (const char *) filename, line);
/*

	char errorText[128];
	char name[80];


	GetGameName(name);
	sprintf(errorText, "Fatal error in %s, line %u!  Code 0x%.8x", filename, line, error);
    MessageBox(hwnd, errorText, name, MB_SYSTEMMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE);
*/	
	return(RD_OK);

}



int32 _ReportFatalError(const uint8 *error, const uint8 *filename, uint32 line)

{

	warning("stub _ReportFatalError");
	char errorText[500];
	char name[80];


	GetGameName((uint8 *)name);
	sprintf(errorText, "FATAL ERROR - GAME TERMINATED\n%s", error);
	//MessageBox(hwnd, errorText, name, MB_SYSTEMMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE);
	warning("%s", errorText);

	return(RD_OK);

}


int32 DisableQuitKey(void)
{
	controlQDisabled = TRUE;
	return(RD_OK);
}

void SetWindowName(const char *windowName)
{
	warning("stub SetWindowName( %s )", windowName);
//	SetWindowText(hwnd,windowName);
//	strcpy(gameName,windowName);
}