1 #include "render_gdi.h"
2 #include "screen_win32.h"
3 #include "colours.h"
4 #include "screen.h"
5 
6 extern HWND hWndMain;
7 extern char *myname;
8 
9 // GDI specific rendering code
refreshv_gdi(UBYTE * scr_ptr,FRAMEPARAMS * fp)10 void refreshv_gdi(UBYTE *scr_ptr, FRAMEPARAMS *fp)
11 {
12 	HDC hCdc;
13 	BITMAPINFO bi;
14 	HBITMAP hBitmap;
15 	DWORD *pixels = NULL;
16 	int pixel = 0;
17 	int i, x, y;
18 	int v_adj = 0;
19 
20 	// calculate bitmap height & width
21 	int bmWidth = fp->view.right - fp->view.left;
22 	int bmHeight = fp->view.bottom - fp->view.top;
23 
24 	bmHeight *= fp->scanlinemode + 1;
25 
26     // GDI specific window fill adjustment
27 	if (!fp->scanlinemode) {
28 		v_adj = (int) fp->height / bmHeight;
29 	}
30 
31 	bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
32 	bi.bmiHeader.biWidth = bmWidth;
33 	bi.bmiHeader.biHeight = bmHeight;
34 	bi.bmiHeader.biPlanes = 1;
35 	bi.bmiHeader.biBitCount = 32;
36 	bi.bmiHeader.biCompression = BI_RGB;
37 	bi.bmiHeader.biSizeImage = 0;
38 	bi.bmiHeader.biXPelsPerMeter = 0;
39 	bi.bmiHeader.biYPelsPerMeter = 0;
40 	bi.bmiHeader.biClrUsed = 0;
41 	bi.bmiHeader.biClrImportant = 0;
42 
43 	hCdc = CreateCompatibleDC(fp->hdc);
44 	hBitmap = CreateDIBSection(hCdc, &bi, DIB_RGB_COLORS, (void *)&pixels, NULL, 0);
45 	if (!hBitmap) {
46 		MessageBox(hWndMain, "Could not create bitmap", myname, MB_OK);
47 		DestroyWindow(hWndMain);
48 		return;
49 	}
50 
51 	// copy screen buffer to the bitmap
52 	scr_ptr += fp->view.top * Screen_WIDTH + fp->view.left;
53 	for (y = fp->view.top; y < fp->view.bottom; y++) {
54 		for (x = fp->view.left; x < fp->view.right; x++) {
55 			if (y < 0 || y >= Screen_HEIGHT || x < 0 || x >= Screen_WIDTH)
56 				pixels[pixel] = Colours_table[0];
57 			else
58 				pixels[pixel] = Colours_table[*scr_ptr];
59 
60 				for (i = 0; i < fp->scanlinemode; i++) {
61 					pixels[pixel + i * bmWidth] = pixels[pixel];
62 				}
63 
64 			scr_ptr++;
65 			pixel++;
66 		}
67 		scr_ptr += Screen_WIDTH - bmWidth;
68 	    pixel += bmWidth * fp->scanlinemode;
69 	}
70 
71 	SelectObject(hCdc, hBitmap);
72 
73 	// Draw the bitmap on the screen. The image must be flipped vertically
74 	if (!StretchBlt(fp->hdc, fp->x_origin, fp->y_origin, fp->width, fp->height + v_adj,
75 		hCdc, 0, bi.bmiHeader.biHeight, bi.bmiHeader.biWidth, -bi.bmiHeader.biHeight, SRCCOPY))
76 	{
77 		MessageBox(hWndMain, "Could not StretchBlt", myname, MB_OK);
78 		DestroyWindow(hWndMain);
79 	}
80 
81 	DeleteDC(hCdc);
82 	DeleteObject(hBitmap);
83 }
84