File: ddraw.cpp

package info (click to toggle)
acm 5.0-23.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,364 kB
  • ctags: 4,793
  • sloc: ansic: 42,444; makefile: 706; cpp: 293; perl: 280; sh: 198
file content (139 lines) | stat: -rwxr-xr-x 3,117 bytes parent folder | download | duplicates (9)
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
#include <ddraw.h>
#include <ddutil.h>
#include <assert.h>

extern "C" {
	
	extern void printDDError (char *, HRESULT code);
    
    extern LPDIRECTDRAW GetDirectDrawInterface (int iWndIndex);
    extern LPDIRECTDRAWSURFACE GetBackBuffer (int iWndIndex);
    extern printf (const char *, ...);
    
    LPDIRECTDRAW lpDD = 0;
    LPDIRECTDRAWSURFACE lpBack = 0;
    LPDIRECTDRAWSURFACE lpFront = 0;
    static BOOL bFirstTime = TRUE;
    
    LPDIRECTDRAWSURFACE
		LoadACMBitmap (char *szBitmap, int * width, int * height)
    {
		HBITMAP	hbm;
		DDSURFACEDESC ddsd;
		LPDIRECTDRAWSURFACE lpDDS;
		LPDIRECTDRAWPALETTE lpDDPal;
		HRESULT	ddrval;
		BITMAP              bm;
		
		// Lazy initialization
		
		if (!lpDD) {
			lpDD = GetDirectDrawInterface ( 0 );
		}
		
		// Load bitmap and get dimensions
		
		hbm = (HBITMAP) LoadImage (GetModuleHandle(NULL), szBitmap,
			IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
		
		if (hbm == NULL) {
			return NULL;
		}
		
		GetObject(hbm, sizeof(bm), &bm);    // get size of bitmap
		*width = bm.bmWidth;
		*height = bm.bmHeight;
		
		// Create an off-screen bitmap.
		ZeroMemory(&ddsd, sizeof(ddsd));
		ddsd.dwSize = sizeof(ddsd);
		ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH;
		ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
		ddsd.dwWidth = *width;
		ddsd.dwHeight = *height;
		ddrval = lpDD->CreateSurface(&ddsd, &lpDDS, NULL);
		if (ddrval != DD_OK) {
			printDDError("Creating bitmap surface", ddrval);
		}
		
		if(ddrval != DD_OK) {
			*width = *height = 0;
			return NULL;
		}
		
		/*
		*  Get the standard 16 colors used for the bitmaps into the palette
		*  TODO:  If multiple windows are ever used, this will need to be modified
		*  to update the palette in each window.
		*/
		
		if (bFirstTime) {
			lpDDPal = DDLoadPalette(lpDD, szBitmap);
			if (lpDDPal) {
				ddrval = lpDDS->SetPalette (lpDDPal);
				if (ddrval != DD_OK) {
					printf ("Error setting palette %d\n", ddrval);
				}
			}
			//bFirstTime = FALSE;
		}
		
		DDCopyBitmap(lpDDS, hbm, 0, 0, *width, *height);
		
		DeleteObject(hbm);
		
		return lpDDS;
    }
    
    void
		PutACMBitmap (LPDIRECTDRAWSURFACE bm, char *szBitmap,
			int w, int h, int x, int y, int len)
    {
		RECT        rcRect;
		HRESULT	ddrval;
		
		if (!lpBack) {
			lpBack = GetBackBuffer (0);
		}
		
		rcRect.left = 0;
		rcRect.top = 0;
		rcRect.right = w;
		rcRect.bottom = h;
		while(1) {
			ddrval = lpBack->BltFast(x, y, bm, &rcRect, FALSE);
			if(ddrval == DD_OK) {
				break;
			}
			else if (ddrval == DDERR_SURFACELOST) {
				HBITMAP hbm;

				ddrval = bm->Restore();
				if (ddrval != DD_OK) {
					printDDError ("Attempting to Restore() bitmap surface", ddrval);
					exit (1);
				}

				hbm = (HBITMAP) LoadImage (GetModuleHandle(NULL), szBitmap,
					IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
		
				if (hbm == NULL) {
					printf ("Unable reload bitmap: returned null\n");
					exit (1);
				}

				DDCopyBitmap(bm, hbm, 0, 0, 0, 0);
		
				DeleteObject(hbm);
				printf ("reloaded bitmap %x\n", szBitmap);
			}
			else {
				printDDError ("In BltFast code", ddrval);
				exit (1);
			}
		}
    }
    
    
}