cOSGameLib_12.c 28.1 KB
Newer Older
Peter Achten's avatar
Peter Achten committed
1
2
3
4
5

/* Clean Game Library by Mike Wiering, Nijmegen */

/* DirectX implementation of the OS specific functions */

6
#include "cOSGameLib.h"
Peter Achten's avatar
Peter Achten committed
7
8
9
10
11
12

extern HWND ghMainWindow;

int ScreenWidth = 320;
int ScreenHeight = 240;
int BitsPerPixel = 8;
13
14
15
16
17
18
19

int ActualWidth = 320;
int ActualHeight = 240;

int XShiftScreen = 0;
int YShiftScreen = 0;

Peter Achten's avatar
Peter Achten committed
20
21
22
23
24
25
26
27
BOOL FullScreen = TRUE;
BOOL bGameActive = FALSE;
HWND ghGameWindow = NULL;

static LPDIRECTDRAW lpDD = NULL;
static LPDIRECTDRAWSURFACE lpDDSFront = NULL;
static LPDIRECTDRAWSURFACE lpDDSBack = NULL;
static LPDIRECTDRAWPALETTE lpDDPal = NULL;
28
static IDirectDrawClipper *clipper = NULL;
Peter Achten's avatar
Peter Achten committed
29
30
31

typedef struct GAMEBLOCKSEQUENCE
{
32
    int iSequenceID;
Peter Achten's avatar
Peter Achten committed
33
34
35
36
37
38
39
40
41
    int iSequenceLength;
    char *sSequence;
    int iPosition;
    int iCounter;
    struct GAMEBLOCKSEQUENCE *gbsNext;
} GAMEBLOCKSEQUENCE;

typedef struct GAMEBITMAPINFO
{
42
    int iBitmapID;
Peter Achten's avatar
Peter Achten committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    LPDIRECTDRAWSURFACE lpDDSBitmap;
    int iBitmapWidth;
    int iBitmapHeight;
    int iBlockWidth;
    int iBlockHeight;
    int iBlockCountX;
    int iBlockCountY;
    BOOL bTransparent;
    struct GAMEBLOCKSEQUENCE *gbsGameBlockSequence;
    struct GAMEBITMAPINFO *gbipNext;
    char *sName;
} GAMEBITMAPINFO;

static GAMEBITMAPINFO *gbipGameBitmapInfo = NULL;

static int iPrevGBIP = 0;
static GAMEBITMAPINFO *gbipPrev = NULL;


/* release DirectDraw object */
void ReleaseDD (void)
{
Mike Wiering's avatar
Mike Wiering committed
65
66
67
68
69
70
  if (lpDDPal)
  {
    IDirectDrawSurface_Release (lpDDPal);
    lpDDPal = NULL;
  }

Peter Achten's avatar
Peter Achten committed
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
  if (clipper)
  {
     IDirectDrawClipper_Release (clipper);
     clipper = NULL;
  }

  if (lpDD != NULL)
  {
    if (lpDDSFront != NULL)
    {
      IDirectDraw_Release (lpDDSFront);
      lpDDSFront = NULL;
    }
    IDirectDraw_Release (lpDD);
    lpDD = NULL;
  }
}

/* restore directdraw object */
void DDRestoreAll ()
{
    HRESULT ddrval;
    DDSURFACEDESC ddsd;
    GAMEBITMAPINFO *gbip = gbipGameBitmapInfo;

96
    if (lpDD != NULL)
Peter Achten's avatar
Peter Achten committed
97
    {
98
        ddrval = IDirectDrawSurface_Restore (lpDDSFront);
Peter Achten's avatar
Peter Achten committed
99
100
        if (ddrval == DD_OK)
        {
101
            ddrval = IDirectDrawSurface_Restore (lpDDSBack);
Peter Achten's avatar
Peter Achten committed
102
            if (ddrval == DD_OK)
103
104
105
106
107
108
109
110
111
112
113
114
            {
                while (gbip)
                {
                    ddrval = IDirectDrawSurface_Restore (gbip->lpDDSBitmap);
                    if (ddrval == DD_OK)
                    {
                        DDReLoadBitmap (gbip->lpDDSBitmap, gbip->sName);
                    }
                    gbip = gbip->gbipNext;
                }
            }
        }
Peter Achten's avatar
Peter Achten committed
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
    }
}




/* --------------------- window / screen functions --------------------- */

typedef struct {
  GUID *guid;
  GUID DriverGUID;
  CHAR DriverDesc[128];
  CHAR DriverName[128];
} DEVICES;

DEVICES aDDDevs[15];
int MaxDevIndex = 0;
int DevIndex = 0;

HRESULT CALLBACK DDEnumCallback (GUID *lpGUID,
                                 LPSTR DriverDesc,
                                 LPSTR DriverName,
                                 LPVOID lpContext)
{
    if (MaxDevIndex >= sizeof (aDDDevs) / sizeof (aDDDevs[0]))
        return E_FAIL;

    // Msg ("Device: %s (%s)", DriverDesc, DriverName);

    if (lpGUID == NULL || lpGUID == (GUID*) DDCREATE_EMULATIONONLY)
    {
        aDDDevs[MaxDevIndex].guid = lpGUID;
    }
    else
    {
        aDDDevs[MaxDevIndex].DriverGUID = *lpGUID;
        aDDDevs[MaxDevIndex].guid = &aDDDevs[MaxDevIndex].DriverGUID;
    }

    lstrcpyn(aDDDevs[MaxDevIndex].DriverDesc, DriverDesc, 128);
    lstrcpyn(aDDDevs[MaxDevIndex].DriverName, DriverName, 128);

    MaxDevIndex++;
    return DDENUMRET_OK;
}


static void ShowError (char *Msg)
{
    ReleaseDD ();
    if (ghGameWindow)
    {
        DestroyWindow (ghGameWindow);
        ghGameWindow = NULL;
    }
    if (ghMainWindow)
    {
        DestroyWindow (ghMainWindow);
        ghMainWindow = NULL;
    }
    MessageBox (NULL, Msg, NULL, MB_OK);
}


/* set up the game window */
BOOL OSInitGameWindow ()
{
    HRESULT ddrval;
    DDSURFACEDESC ddsd;
    DDSCAPS ddscaps;
    int result;

    result = FALSE;

//    DirectDrawEnumerate ((LPDDENUMCALLBACK) &DDEnumCallback, NULL);
//    DDEnumCallback ((GUID *) DDCREATE_EMULATIONONLY,
//                    "Hardware Emulation Layer", "", NULL);
//    DevIndex = MaxDevIndex - 1;

//    ddrval = DirectDrawCreate (aDDDevs[DevIndex].guid, &lpDD, NULL);
    ddrval = DirectDrawCreate (NULL, &lpDD, NULL);
196
    if (!(ddrval == DD_OK))
Peter Achten's avatar
Peter Achten committed
197
198
        ShowError ("DirectDrawCreate failed");
    else
199
    {
Peter Achten's avatar
Peter Achten committed
200
        if (FullScreen)
201
            ddrval = IDirectDraw_SetCooperativeLevel (lpDD, ghGameWindow,
Peter Achten's avatar
Peter Achten committed
202
203
204
205
206
                         DDSCL_EXCLUSIVE |
                         DDSCL_FULLSCREEN |
                         DDSCL_ALLOWMODEX |
                         DDSCL_ALLOWREBOOT);
        else
207
            ddrval = IDirectDraw_SetCooperativeLevel (lpDD, ghGameWindow,
Peter Achten's avatar
Peter Achten committed
208
                DDSCL_NORMAL);
209
        if (!(ddrval == DD_OK))
Peter Achten's avatar
Peter Achten committed
210
211
            ShowError ("IDirectDraw_SetCooperativeLevel failed");
        else
212
213
214
215
        {
            ActualWidth = ScreenWidth;
            ActualHeight = ScreenHeight;

Peter Achten's avatar
Peter Achten committed
216
217
            if (FullScreen)
            {
218
219
220
221
222
223
224
225
226
227
                int oldbpp = BitsPerPixel;

                ShowCursor (FALSE);
                ddrval = IDirectDraw_SetDisplayMode (lpDD, ScreenWidth, ScreenHeight, BitsPerPixel);

                /* added 11-24-99, try 8 bit color if 16/24/32 bit fails */
                if (BitsPerPixel > 8)
                {
                    if (!(ddrval == DD_OK))
                    {
Mike Wiering's avatar
Mike Wiering committed
228
                        BitsPerPixel = 8;
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
                        ddrval = IDirectDraw_SetDisplayMode (lpDD, ScreenWidth, ScreenHeight, BitsPerPixel);
                    }
                }
                /* added  3-29-00, try 640x480 if lower resolution fails */
                if ((!(ddrval == DD_OK)) && ((ScreenWidth < 640) || (ScreenHeight < 480)))
                {
                  //  MessageBox (NULL, "Screen mode not supported. Trying higher resolution", NULL, MB_OK);

                    ActualWidth = 640;
                    ActualHeight = 480;
                    BitsPerPixel = oldbpp;
                    ddrval = IDirectDraw_SetDisplayMode (lpDD, ActualWidth, ActualHeight, BitsPerPixel);

                    if (BitsPerPixel > 8)
                    {
                        if (!(ddrval == DD_OK))
                        {
Mike Wiering's avatar
Mike Wiering committed
246
                            BitsPerPixel = 8;
247
248
249
250
                            ddrval = IDirectDraw_SetDisplayMode (lpDD, ScreenWidth, ScreenHeight, BitsPerPixel);
                        }
                    }
                }
Peter Achten's avatar
Peter Achten committed
251
252
            }

253
254
255
            XShiftScreen = (ActualWidth - ScreenWidth) / 2;
            YShiftScreen = (ActualHeight - ScreenHeight) / 2;

Peter Achten's avatar
Peter Achten committed
256
257
258
259
            if (!(ddrval == DD_OK))
                ShowError ("IDirectDraw_SetDisplayMode failed");
            else
            {
260
261
                memset (&ddsd, 0, sizeof (DDSURFACEDESC));  // set non-used values to zero!
                ddsd.dwSize = sizeof (ddsd);
Peter Achten's avatar
Peter Achten committed
262
263
                if (FullScreen)
                {
264
265
266
267
                    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
                    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
                                          DDSCAPS_FLIP |
                                          DDSCAPS_COMPLEX |
Peter Achten's avatar
Peter Achten committed
268
269
270
271
                                          DDSCAPS_MODEX;
                }
                else
                {
272
273
                    ddsd.dwFlags = DDSD_CAPS;
                    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
Peter Achten's avatar
Peter Achten committed
274
275
276
                }

                ddsd.dwBackBufferCount = 1;
277
                ddrval = IDirectDraw_CreateSurface (lpDD, &ddsd, &lpDDSFront, NULL);
Peter Achten's avatar
Peter Achten committed
278
279
280
281

                if (!(ddrval == DD_OK))
                    ShowError ("IDirectDraw_CreateSurface failed (lpDDSFront)");
                else
282
283
                {
                    ddrval = IDirectDraw_CreateClipper (lpDD, 0, &clipper, NULL);
Peter Achten's avatar
Peter Achten committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
                    if (!(ddrval == DD_OK))
                        ShowError ("IDirectDraw_CreateClipper failed");
                    else
                    {
                        ddrval = IDirectDrawClipper_SetHWnd (clipper, 0, ghGameWindow);
                        if (!(ddrval == DD_OK))
                            ShowError ("IDirectDrawClipper_SetHWnd failed");
                        else
                        {
                            ddrval = IDirectDrawSurface_SetClipper (lpDDSFront, clipper);
                            if (!(ddrval == DD_OK))
                                ShowError ("IDirectDrawClipper_SetClipper failed");
                            else
                            {
298
299
300
301
302
303
304
305
306
307
308
309
310
                                if (!FullScreen)
                                {

                                    memset (&ddsd, 0, sizeof (DDSURFACEDESC));
                                    ddsd.dwSize = sizeof (ddsd);
                                    ddsd.dwFlags = DDSD_CAPS |
                                                   DDSD_WIDTH |
                                                   DDSD_HEIGHT;

                                    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
                                    ddsd.dwWidth = ScreenWidth;
                                    ddsd.dwHeight = ScreenHeight;
                                    ddrval = IDirectDraw_CreateSurface (lpDD, &ddsd, &lpDDSBack, NULL);
Peter Achten's avatar
Peter Achten committed
311
312
313
314
                                    if (!(ddrval == DD_OK))
                                        ShowError ("IDirectDrawClipper_CreateSurface failed (lpDDSBack)");
                                    else
                                        result = TRUE;
315
316
317
318
319
                                }
                                else
                                {
                                    ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
                                    ddrval = IDirectDrawSurface_GetAttachedSurface (lpDDSFront, &ddscaps, &lpDDSBack);
Peter Achten's avatar
Peter Achten committed
320
321
322
                                    if (!(ddrval == DD_OK))
                                        ShowError ("IDirectDrawSurface_GetAttachedSurface failed");
                                    else
Mike Wiering's avatar
Mike Wiering committed
323
324
325
326
327
328
329
330
331
332
                                    {
                                        if (BitsPerPixel == 8)
                                        {
                                           lpDDPal = DDLoadPalette (lpDD, NULL);
                                           if (lpDDPal == NULL)
                                               ShowError ("DDMakeDefaultPalette failed");
                                           else
                                               IDirectDrawSurface_SetPalette (lpDDSFront, lpDDPal);
                                        }

Peter Achten's avatar
Peter Achten committed
333
                                        result = TRUE;
Mike Wiering's avatar
Mike Wiering committed
334
                                    }
335
                                }
Peter Achten's avatar
Peter Achten committed
336
337
338
                            }
                        }
                    }
339
340
                }
            }
Peter Achten's avatar
Peter Achten committed
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
        }
    }
    return result;
}

/* shut down the game window */
void OSDeInitGameWindow ()
{
    ReleaseDD ();
}

/* get game window handle */
BOOL OSGetGameWindowHDC (HDC *hdc)
{
   return (IDirectDrawSurface_GetDC (lpDDSBack, hdc) == DD_OK);
}

/* release game window handle */
void OSReleaseGameWindowHandle (HDC hdc)
{
   IDirectDrawSurface_ReleaseDC (lpDDSBack, hdc);
}

/* clear the (visual) screen */
void OSClearScreen ()
{
    DDBLTFX ddbltfx;

    memset (&ddbltfx, 0, sizeof (ddbltfx));
    ddbltfx.dwSize = sizeof (ddbltfx);
    ddbltfx.dwFillColor = DDColorMatch (lpDDSFront, 0);
    IDirectDrawSurface_Blt (lpDDSFront, NULL, NULL, NULL,
        DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
}

/* clear the virtual screen */
void OSClearVirtualScreen (COLORREF c)
{
    DDBLTFX ddbltfx;
380
381
382
383
384
385
    RECT dst;

    dst.left = XShiftScreen;
    dst.top = YShiftScreen;
    dst.right = ScreenWidth + XShiftScreen;
    dst.bottom = ScreenHeight + YShiftScreen;
Peter Achten's avatar
Peter Achten committed
386
387

    memset (&ddbltfx, 0, sizeof (ddbltfx));
388

Peter Achten's avatar
Peter Achten committed
389
390
    ddbltfx.dwSize = sizeof (ddbltfx);
    ddbltfx.dwFillColor = DDColorMatch (lpDDSBack, c);
391
    IDirectDrawSurface_Blt (lpDDSBack, &dst, NULL, NULL,
Peter Achten's avatar
Peter Achten committed
392
393
394
395
396
397
398
399
400
        DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
}

/* fill an area with black */
void OSFillBlack (BOOL vis, RECT dst)
{
    LPDIRECTDRAWSURFACE lpDDS = (vis)? lpDDSFront : lpDDSBack;
    DDBLTFX ddbltfx;

401
402
403
404
405
406
407
408
//    if (vis)
//    {
      dst.left += XShiftScreen;
      dst.top += YShiftScreen;
      dst.right += XShiftScreen;
      dst.bottom += YShiftScreen;
//    }

Peter Achten's avatar
Peter Achten committed
409
410
411
412
413
414
415
416
417
418
    memset (&ddbltfx, 0, sizeof (ddbltfx));
    ddbltfx.dwSize = sizeof (ddbltfx);
    ddbltfx.dwFillColor = DDColorMatch (lpDDS, 0);
    IDirectDrawSurface_Blt (lpDDS, &dst, NULL, NULL,
        DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
}

/* copy (part of) virtual screen to visual screen */
void OSBlit (RECT *r)
{
419
    HRESULT ddrval;
Peter Achten's avatar
Peter Achten committed
420
421
    DDBLTFX ddbltfx;
    int flags = DDBLT_WAIT + DDBLT_DDFX;
422
423
424
425
426
427
    RECT sr;

    sr.left = r->left + XShiftScreen;
    sr.top = r->top + YShiftScreen;
    sr.right = r->right + XShiftScreen;
    sr.bottom = r->bottom + YShiftScreen;
Peter Achten's avatar
Peter Achten committed
428
429
430
431
432

    memset (&ddbltfx, 0, sizeof (ddbltfx));
    ddbltfx.dwSize = sizeof (ddbltfx);
    ddbltfx.dwDDFX = DDBLTFX_NOTEARING;

433
434
    ddrval = IDirectDrawSurface_Blt
                (lpDDSFront, &sr, lpDDSBack, &sr, flags, &ddbltfx);
Peter Achten's avatar
Peter Achten committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
    if (ddrval == DDERR_SURFACELOST)
        DDRestoreAll ();
}

/* flip pages */
void OSFlip ()
{
    IDirectDrawSurface_Flip (lpDDSFront, NULL, DDFLIP_WAIT);
}


/* ------------------------- bitmap functions  ------------------------- */

/* get a pointer to the GAMEBITMAPINFO structure with id BID */
static GAMEBITMAPINFO *GetGameBitmapInfo (int BID)
{
    GAMEBITMAPINFO *gbip = gbipGameBitmapInfo;
    BOOL bFound = FALSE;

    if (BID == iPrevGBIP)
        gbip = gbipPrev;
    else
    {
458
        while (gbip && (!bFound))
Peter Achten's avatar
Peter Achten committed
459
460
461
        {
            if (gbip->iBitmapID == BID)
            {
462
                bFound = TRUE;
Peter Achten's avatar
Peter Achten committed
463
464
465
466
467
468
469
                iPrevGBIP = BID;
                gbipPrev = gbip;
            }
            else
                gbip = gbip->gbipNext;
        }
    }
470
    return gbip;
Peter Achten's avatar
Peter Achten committed
471
472
473
474
475
476
477
}

/* free members of a GAMEBITMAPINFO node */
static void FreeGameBitmapInfoNode (GAMEBITMAPINFO *gbip)
{
    GAMEBLOCKSEQUENCE *gbs;

478
    if (gbip->lpDDSBitmap)
Peter Achten's avatar
Peter Achten committed
479
480
481
482
483
    {
        IDirectDraw_Release (gbip->lpDDSBitmap);
        gbip->lpDDSBitmap = NULL;
    }

484
    if (gbip->sName)
Peter Achten's avatar
Peter Achten committed
485
486
487
488
489
490
    {
        rfree (gbip->sName);
        gbip->sName = NULL;
    }

    gbs = NULL;
491
    while (gbip->gbsGameBlockSequence)
Peter Achten's avatar
Peter Achten committed
492
    {
493
        gbs = gbip->gbsGameBlockSequence->gbsNext;
Peter Achten's avatar
Peter Achten committed
494
495
        if (gbip->gbsGameBlockSequence->sSequence)
        {
496
            rfree (gbip->gbsGameBlockSequence->sSequence);
Peter Achten's avatar
Peter Achten committed
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
        }
        rfree (gbip->gbsGameBlockSequence);
        gbip->gbsGameBlockSequence = gbs;
    }
}


/* initialize a game bitmap */
int OSInitGameBitmap (int id, char *name,
                      int bitmapwidth, int bitmapheight,
                      int blockwidth, int blockheight)
{
    HRESULT ddrval;
    DDSURFACEDESC ddsd;
    DDSCAPS ddscaps;
    int resultcode;
    GAMEBITMAPINFO *gbip1;
    GAMEBITMAPINFO *gbip2;
    LPDIRECTDRAWSURFACE lpDDS;

    iPrevGBIP = 0;
    gbipPrev = NULL;

    if (id==0)
    {
        /* find matching id or create new id */
523
        GAMEBITMAPINFO *gbip = gbipGameBitmapInfo;
Peter Achten's avatar
Peter Achten committed
524
525

        id = 1;
526
527
528
        while (gbip)
        {
            if (lstrcmp (gbip->sName, name) == 0)
Peter Achten's avatar
Peter Achten committed
529
            {
530
                resultcode = gbip->iBitmapID;
Peter Achten's avatar
Peter Achten committed
531
532
                return resultcode;
            }
533
            else
Peter Achten's avatar
Peter Achten committed
534
535
536
            {
                if (gbip->iBitmapID >= id)
                    id = gbip->iBitmapID + 1;
537
                gbip = gbip->gbipNext;
Peter Achten's avatar
Peter Achten committed
538
            }
539
        }
Peter Achten's avatar
Peter Achten committed
540
541
542
543
544
545
    }

    resultcode = GR_INVALID_BITMAP_ID;
    if (!GetGameBitmapInfo (id))  /* identifier not used yet */
    {
        /* set non-used values to zero! */
546
        memset (&ddsd, 0, sizeof (DDSURFACEDESC));
Peter Achten's avatar
Peter Achten committed
547
548
549
550
551
552
553
554
555
556
557
        ddsd.dwSize = sizeof (ddsd);
        ddsd.dwFlags = DDSD_CAPS |
                       DDSD_WIDTH |
                       DDSD_HEIGHT;

        /* must always be in systemmemory if size > screensize */
        ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; // | DDSCAPS_SYSTEMMEMORY;
        ddsd.dwWidth = bitmapwidth;
        ddsd.dwHeight = bitmapheight;
        ddrval = IDirectDraw_CreateSurface (lpDD, &ddsd, &lpDDS, NULL);

558
559
        resultcode = GR_NOT_FOUND;
        lpDDPal = DDLoadPalette (lpDD, name);
Peter Achten's avatar
Peter Achten committed
560
561
562
563
        if (lpDDPal)
        {
            ddrval = DDReLoadBitmap (lpDDS, name);
            if (ddrval == DD_OK)
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
            {
                resultcode = GR_OS_ERROR;
                if (ddrval == DD_OK)
                {
                    /* find last element of linked list */
                    gbip1 = gbipGameBitmapInfo;
                    gbip2 = NULL;
                    while (gbip1)
                    {
                        gbip2 = gbip1;
                        gbip1 = gbip1->gbipNext;
                    }

                    /* create new node */
                    gbip1 = rmalloc (sizeof (GAMEBITMAPINFO));
                    gbip1->iBitmapID = id;
                    gbip1->lpDDSBitmap = lpDDS;
                    gbip1->bTransparent = FALSE;    /* transparency can be set later */
                    gbip1->iBitmapWidth = bitmapwidth;
                    gbip1->iBitmapHeight = bitmapheight;
                    gbip1->iBlockWidth = blockwidth;
                    gbip1->iBlockHeight = blockheight;
                    gbip1->iBlockCountX = bitmapwidth / blockwidth;
                    gbip1->iBlockCountY = bitmapheight / blockheight;
Peter Achten's avatar
Peter Achten committed
588
                    gbip1->gbsGameBlockSequence = NULL;
589
                    gbip1->gbipNext = NULL;
Peter Achten's avatar
Peter Achten committed
590
                    gbip1->sName = rmalloc (strlen (name) + 1);
591
592
593
594
595
596
597
598
599
600
601
602
                    rsncopy (gbip1->sName, name, strlen (name));

                    /* gbip2 points to the last element or is NULL */
                    if (gbip2)
                        gbip2->gbipNext = gbip1;
                    else
                        gbipGameBitmapInfo = gbip1;  /* first element */

                    resultcode = GR_OK;
                }
            }
        }
Peter Achten's avatar
Peter Achten committed
603
    }
604
605
606
    if (resultcode == GR_OK)
        return id;
    else
Peter Achten's avatar
Peter Achten committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
        return resultcode;
}

/* get bitmap info */
BOOL OSGetGameBitmapInfo (int id, int *width, int *height,
                          int *blockwidth, int *blockheight,
                          int *blockcountx, int *blockcounty)
{
    GAMEBITMAPINFO *gbip;

    gbip = GetGameBitmapInfo (id);
    if (gbip)
    {
        *width = gbip->iBitmapWidth;
        *height = gbip->iBitmapHeight;
        *blockwidth = gbip->iBlockWidth;
        *blockheight = gbip->iBlockHeight;
        *blockcountx = gbip->iBlockCountX;
        *blockcounty = gbip->iBlockCountY;
Mike Wiering's avatar
Mike Wiering committed
626
        return TRUE;
Peter Achten's avatar
Peter Achten committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    }
    else
        return FALSE;
}


/* deinit a game bitmap */
int OSFreeGameBitmap (int id)
{
    GAMEBITMAPINFO *gbipCurrent = gbipGameBitmapInfo;
    GAMEBITMAPINFO *gbipNext = NULL;
    GAMEBITMAPINFO *gbipPrevious = NULL;
    int result;

    iPrevGBIP = 0;
    gbipPrev = NULL;

    result = GR_INVALID_BITMAP_ID;
645
    while (gbipCurrent)
Peter Achten's avatar
Peter Achten committed
646
647
648
649
    {
        if (gbipCurrent->iBitmapID != id)
        {
            gbipPrevious = gbipCurrent;
650
            gbipNext = gbipCurrent->gbipNext;
Peter Achten's avatar
Peter Achten committed
651
        }
652
        else
Peter Achten's avatar
Peter Achten committed
653
        {
654
            /* link previous node to next node */
Peter Achten's avatar
Peter Achten committed
655
            if (gbipPrevious)
656
                gbipPrevious->gbipNext = gbipCurrent->gbipNext;
Peter Achten's avatar
Peter Achten committed
657
658
659
            else
                gbipGameBitmapInfo = gbipCurrent->gbipNext;

660
            gbipNext = gbipCurrent->gbipNext;
Peter Achten's avatar
Peter Achten committed
661
662
663
            /* free the current node */

            FreeGameBitmapInfoNode (gbipCurrent);
664
            rfree (gbipCurrent);
Peter Achten's avatar
Peter Achten committed
665
666
667
668
669
670
671
672
673
674
675
676
677

            result = GR_OK;
        }
        gbipCurrent = gbipNext;
    }
    return result;
}

/* deinit all game bitmaps */
void OSFreeGameBitmaps ()
{
    GAMEBITMAPINFO *gbip;

678
    while (gbipGameBitmapInfo)
Peter Achten's avatar
Peter Achten committed
679
    {
680
        gbip = gbipGameBitmapInfo->gbipNext;
Peter Achten's avatar
Peter Achten committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
        FreeGameBitmapInfoNode (gbipGameBitmapInfo);
        rfree (gbipGameBitmapInfo);
        gbipGameBitmapInfo = gbip;
    }
    iPrevGBIP = 0;
    gbipPrev = NULL;
}


/* set transparent color */
int OSSetTransparentColor (int id, int x, int y)
{
    HDC hdc;
    COLORREF rgb;
    int result;
    GAMEBITMAPINFO *gbip = GetGameBitmapInfo (id);

    result = GR_INVALID_BITMAP_ID;
699
    if (gbip)
Peter Achten's avatar
Peter Achten committed
700
    {
701
702
        if (x < 0) x += gbip->iBitmapWidth;
        if (y < 0) y += gbip->iBitmapHeight;
Peter Achten's avatar
Peter Achten committed
703

704
705
706
707
708
        result = GR_OS_ERROR;
        if (IDirectDrawSurface_GetDC (gbip->lpDDSBitmap, &hdc) == DD_OK)
        {
            rgb = GetPixel (hdc, x, y);
            IDirectDrawSurface_ReleaseDC (gbip->lpDDSBitmap, hdc);
Peter Achten's avatar
Peter Achten committed
709
710

            DDSetColorKey (gbip->lpDDSBitmap, rgb);
711
            gbip->bTransparent = TRUE;
Peter Achten's avatar
Peter Achten committed
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729

            // DDSetColorKey (gbip->lpDDSFront, rgb);
            result = GR_OK;
        }
    }
    return result;
}

/* initialize a block sequence */
int OSInitBlockSequence (int bitmapid, int seqid, char *seq, int len)
{
    GAMEBITMAPINFO *gbip = GetGameBitmapInfo (bitmapid);
    GAMEBLOCKSEQUENCE *gbsNew;
    GAMEBLOCKSEQUENCE *gbs1;
    GAMEBLOCKSEQUENCE *gbs2;
    int resultcode;

    resultcode = GR_INVALID_BITMAP_ID;
730
    if (gbip)
Peter Achten's avatar
Peter Achten committed
731
732
733
734
735
736
    {
        gbsNew = rmalloc (sizeof (GAMEBLOCKSEQUENCE));
        gbsNew->iSequenceID = seqid;
        gbsNew->iSequenceLength = len / (2 * sizeof (int));
        gbsNew->sSequence = rmalloc (len + 1);
        rsncopy (gbsNew->sSequence, seq, len);
737
        gbsNew->iPosition = gbsNew->iSequenceLength;
Peter Achten's avatar
Peter Achten committed
738
        gbsNew->iCounter = 0;
739
        gbsNew->gbsNext = NULL;
Peter Achten's avatar
Peter Achten committed
740
741
742
743
744

        gbs1 = gbip->gbsGameBlockSequence;
        gbs2 = NULL;
        while (gbs1)
        {
745
            gbs2 = gbs1;
Peter Achten's avatar
Peter Achten committed
746
747
            gbs1 = gbs1->gbsNext;
        }
748
749
        if (gbs2)
            gbs2->gbsNext = gbsNew;
Peter Achten's avatar
Peter Achten committed
750
751
        else
            gbip->gbsGameBlockSequence = gbsNew;
752
        resultcode = GR_OK;
Peter Achten's avatar
Peter Achten committed
753
754
755
756
757
758
759
760
761
762
763
764
765
766
    }
    return resultcode;
}

/* run block sequences */
void OSRunBlockSequences ()
{
    GAMEBITMAPINFO *gbip;
    GAMEBLOCKSEQUENCE *gbs;
    int i;

    gbip = gbipGameBitmapInfo;
    while (gbip)
    {
767
        gbs = gbip->gbsGameBlockSequence;
Peter Achten's avatar
Peter Achten committed
768
769
        while (gbs)
        {
770
            if (--gbs->iCounter <= 0)
Peter Achten's avatar
Peter Achten committed
771
            {
772
                gbs->iPosition++;
Peter Achten's avatar
Peter Achten committed
773
                if (gbs->iPosition >= gbs->iSequenceLength)
774
775
776
                    gbs->iPosition = 0;
                i = (2 * gbs->iPosition * sizeof (int)) + sizeof (int);
                gbs->iCounter = (*(int *) &gbs->sSequence[i]);
Peter Achten's avatar
Peter Achten committed
777
778
779
            }
            gbs = gbs->gbsNext;
        }
780
        gbip = gbip->gbipNext;
Peter Achten's avatar
Peter Achten committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
    }
}

/* get current block */
int OSGetCurrentBlock (int bitmapid, int seqid)
{
    GAMEBITMAPINFO *gbip;
    GAMEBLOCKSEQUENCE *gbs;
    int result = 0;

    gbip = GetGameBitmapInfo (bitmapid);
    gbs = gbip->gbsGameBlockSequence;
    while ((gbs) && (seqid < 0))
    {
        if (gbs->iSequenceID == seqid)
        {
            int mappos;

            mappos = 2 * sizeof (int) * gbs->iPosition;
            result = (*(int *) &gbs->sSequence[mappos]);
        }
        gbs = gbs->gbsNext;
    }
    return result;
}

/* draw part of a bitmap to virtual screen */
void OSDraw (RECT *dst, int id, RECT *src, BOOL mirlr, BOOL mirud, int flags)
{
810
811
    HRESULT ddrval;
    DDBLTFX ddbltfx;
Peter Achten's avatar
Peter Achten committed
812
813
814
815
    int bltflags, bltfastflags;
    BOOL fast;
    static int i;
    GAMEBITMAPINFO *gbip = GetGameBitmapInfo (id);
816
817
818
819
820
821
    RECT r;

    r.left = dst->left + XShiftScreen;
    r.top = dst->top + YShiftScreen;
    r.right = dst->right + XShiftScreen;
    r.bottom = dst->bottom + YShiftScreen;
Peter Achten's avatar
Peter Achten committed
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868

    if (gbip)
    {
        fast = TRUE;

        bltfastflags = DDBLTFAST_WAIT;
        bltflags = DDBLT_WAIT | DDBLT_DDFX;

        if (gbip->bTransparent)
        {
            bltflags |= DDBLT_KEYSRC;
            bltfastflags |= DDBLTFAST_SRCCOLORKEY;
        }

      //  ddbltfx.dwDDFX = 0;
        memset (&ddbltfx, 0, sizeof (ddbltfx));
        ddbltfx.dwSize = sizeof (ddbltfx);

        if (mirlr)
        {
            ddbltfx.dwDDFX |= DDBLTFX_MIRRORLEFTRIGHT;
            fast = FALSE;
        }
        if (mirud)
        {
            ddbltfx.dwDDFX |= DDBLTFX_MIRRORUPDOWN;
            fast = FALSE;
        }

        if (flags & DO_ROTATE_90)
        {
            ddbltfx.dwDDFX |= DDBLTFX_ROTATE90;
            fast = FALSE;
        }
        if (flags & DO_ROTATE_180)
        {
            ddbltfx.dwDDFX |= DDBLTFX_ROTATE180;
            fast = FALSE;
        }
        if (flags & DO_ROTATE_270)
        {
            ddbltfx.dwDDFX |= DDBLTFX_ROTATE270;
            fast = FALSE;
        }

        if (fast)
            ddrval = IDirectDrawSurface_BltFast (lpDDSBack,
869
                         r.left, r.top, gbip->lpDDSBitmap, src,
Peter Achten's avatar
Peter Achten committed
870
871
                         bltfastflags);
        else
872
            ddrval = IDirectDrawSurface_Blt (lpDDSBack, &r,
Peter Achten's avatar
Peter Achten committed
873
874
875
876
877
878
879
880
881
882
                         gbip->lpDDSBitmap, src, bltflags, &ddbltfx);
        if (ddrval == DDERR_SURFACELOST)
            DDRestoreAll ();
    }
}

/* ------------------------- sound functions ------------------------- */

typedef struct SOUNDSAMPLEINFO
{
883
    int iSampleID;
Peter Achten's avatar
Peter Achten committed
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
    HSNDOBJ hsoSoundBuffer;
    struct SOUNDSAMPLEINFO *ssiNext;
} SOUNDSAMPLEINFO;

static BOOL bSoundEnabled = FALSE;
static LPDIRECTSOUND lpDS = NULL;
static SOUNDSAMPLEINFO *ssiSoundSampleInfo = NULL;


/* get a pointer to the SOUNDSAMPLEINFO structure with id ID */
SOUNDSAMPLEINFO *GetSoundSampleInfo (int ID)
{
    SOUNDSAMPLEINFO *ssi = ssiSoundSampleInfo;
    BOOL bFound = FALSE;

899
    while (ssi && (!bFound))
Peter Achten's avatar
Peter Achten committed
900
    {
901
902
        if (ssi->iSampleID == ID)
            bFound = TRUE;
Peter Achten's avatar
Peter Achten committed
903
        else
904
            ssi = ssi->ssiNext;
Peter Achten's avatar
Peter Achten committed
905
    }
906
    return ssi;
Peter Achten's avatar
Peter Achten committed
907
908
909
910
911
}

BOOL OSInitSound ()
{
    bSoundEnabled = FALSE;
912
    if (!(DirectSoundCreate (NULL, &lpDS, NULL) == DS_OK))
Peter Achten's avatar
Peter Achten committed
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
    {
       // MessageBox (NULL, "DirectSoundCreate failed", NULL, MB_OK);
    }
    else
    {
        if (!(IDirectSound_SetCooperativeLevel (lpDS, ghGameWindow,
                 DSSCL_NORMAL) == DS_OK))
        {
           // MessageBox (NULL, "IDirectSound_SetCooperativeLevel failed", NULL, MB_OK);
        }
        else
        {
            bSoundEnabled = TRUE;
        }
    }
    return bSoundEnabled;
}

/* deinitialize sound */
void OSDeInitSound ()
{
934
    if (lpDS != NULL)
Peter Achten's avatar
Peter Achten committed
935
936
    {
        IDirectSound_Release (lpDS);
937
        lpDS = NULL;
Peter Achten's avatar
Peter Achten committed
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
    }
    bSoundEnabled = FALSE;
}

/* initialize a sound sample */
BOOL OSInitSoundSample (int id, char *name, int buffers)
{
    SOUNDSAMPLEINFO *ssi;

    if (bSoundEnabled)
    {
        ssi = rmalloc (sizeof (SOUNDSAMPLEINFO));
        ssi->iSampleID = id;
        ssi->hsoSoundBuffer = SndObjCreate (lpDS, name, buffers);
        ssi->ssiNext = ssiSoundSampleInfo;
        ssiSoundSampleInfo = ssi;
        return TRUE;
    }
    else
        return FALSE;
}

/* free the SOUNDSAMPLEINFO list */
void OSFreeSoundSamples ()
{
    SOUNDSAMPLEINFO *ssi;

965
    while (ssiSoundSampleInfo)
Peter Achten's avatar
Peter Achten committed
966
    {
967
        ssi = ssiSoundSampleInfo->ssiNext;
Peter Achten's avatar
Peter Achten committed
968
969
        if (ssiSoundSampleInfo->hsoSoundBuffer)
        {
970
971
            SndObjDestroy (ssiSoundSampleInfo->hsoSoundBuffer);
            ssiSoundSampleInfo->hsoSoundBuffer = NULL;
Peter Achten's avatar
Peter Achten committed
972
973
974
975
976
977
978
979
980
981
982
983
984
        }

        rfree (ssiSoundSampleInfo);
        ssiSoundSampleInfo = ssi;
    }
}

/* play a sound sample */
BOOL OSPlaySoundSample (int id, int volume, int pan, int freq)
{
    SOUNDSAMPLEINFO *ssi = GetSoundSampleInfo (id);

    if (bSoundEnabled)
985
    {
Peter Achten's avatar
Peter Achten committed
986
987
        if (ssi)
        {
988
989
990
991
992
993
994
995
996
997
            IDirectSoundBuffer *pDSB = SndObjGetFreeBuffer (ssi->hsoSoundBuffer);

            if (pDSB)
            {
                IDirectSoundBuffer_SetVolume (pDSB, (LONG) volume);
                IDirectSoundBuffer_SetPan (pDSB, (LONG) pan);
                IDirectSoundBuffer_SetFrequency (pDSB, (LONG) freq);
                IDirectSoundBuffer_Play (pDSB, 0, 0, 0);
            }
        }
Peter Achten's avatar
Peter Achten committed
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
    }
    return bSoundEnabled;
}

/* start playing music in the background */
BOOL OSPlayMusic (char *midifile, BOOL restart)
{
    char buf[256];
    BOOL result = FALSE;

    if (midifile)
    {
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
        wsprintf (buf, "open %s type sequencer alias MUSIC", midifile);
        mciSendString ("close all", NULL, 0, NULL);

        if (mciSendString (buf, NULL, 0, NULL) == 0)
        {
            if (restart)
            {
                if (mciSendString ("play MUSIC from 0 notify", NULL, 0, ghGameWindow) == 0)
                    result = TRUE;
            }
            else
            {
                if (mciSendString ("play MUSIC from 0", NULL, 0, ghGameWindow) == 0)
                    result = TRUE;
            }
        }
Peter Achten's avatar
Peter Achten committed
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
    }
    else
        if (mciSendString ("play MUSIC from 0 notify", NULL, 0, ghGameWindow) == 0)
            result = TRUE;

    return result;
}

/* stop music */
BOOL OSStopMusic ()
{
    BOOL result = FALSE;
    if (mciSendString ("close all", NULL, 0, NULL) == 0)
1039
        result = TRUE;
Peter Achten's avatar
Peter Achten committed
1040
1041
1042
    return result;
}