Improved display and layout code.
[wine.git] / graphics / vga.c
blob9e4801fd0b63325cb56260e5fbaa0b8bf2960693
1 /*
2 * VGA hardware emulation
3 *
4 * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
6 */
8 #include <string.h>
9 #include "windows.h"
10 #include "winbase.h"
11 #include "miscemu.h"
12 #include "vga.h"
13 #include "compobj.h"
14 #include "interfaces.h"
15 #include "ddraw.h"
16 #include "debug.h"
18 static IDirectDraw *lpddraw = NULL;
19 static IDirectDrawSurface *lpddsurf;
20 static IDirectDrawPalette *lpddpal;
21 static DDSURFACEDESC sdesc;
22 static WORD poll_timer;
23 static CRITICAL_SECTION vga_crit;
24 static int vga_polling;
26 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
28 if (lpddraw) VGA_Exit();
29 if (!lpddraw) {
30 DirectDrawCreate(NULL,&lpddraw,NULL);
31 if (!lpddraw) {
32 ERR(ddraw,"DirectDraw is not available\n");
33 return 1;
35 if (lpddraw->lpvtbl->fnSetDisplayMode(lpddraw,Xres,Yres,Depth)) {
36 ERR(ddraw,"DirectDraw does not support requested display mode\n");
37 lpddraw->lpvtbl->fnRelease(lpddraw);
38 lpddraw=NULL;
39 return 1;
41 lpddraw->lpvtbl->fnCreatePalette(lpddraw,0,NULL,&lpddpal,NULL);
42 memset(&sdesc,0,sizeof(sdesc));
43 sdesc.dwSize=sizeof(sdesc);
44 if (lpddraw->lpvtbl->fnCreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
45 ERR(ddraw,"DirectDraw surface is not available\n");
46 lpddraw->lpvtbl->fnRelease(lpddraw);
47 lpddraw=NULL;
48 return 1;
50 InitializeCriticalSection(&vga_crit);
51 /* poll every 20ms (50fps should provide adequate responsiveness) */
52 poll_timer = CreateSystemTimer( 20, (FARPROC16)VGA_Poll );
54 return 0;
57 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
59 if (!lpddraw) return 1;
60 if (!lpddsurf) return 1;
61 if (Height) *Height=sdesc.dwHeight;
62 if (Width) *Width=sdesc.dwWidth;
63 if (Depth) *Depth=sdesc.ddpfPixelFormat.x.dwRGBBitCount;
64 return 0;
67 void VGA_Exit(void)
69 if (lpddraw) {
70 SYSTEM_KillSystemTimer(poll_timer);
71 DeleteCriticalSection(&vga_crit);
72 lpddsurf->lpvtbl->fnRelease(lpddsurf);
73 lpddsurf=NULL;
74 lpddraw->lpvtbl->fnRelease(lpddraw);
75 lpddraw=NULL;
79 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
81 if (!lpddraw) return;
82 lpddpal->lpvtbl->fnSetEntries(lpddpal,0,start,len,pal);
83 lpddsurf->lpvtbl->fnSetPalette(lpddsurf,lpddpal);
86 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
88 PALETTEENTRY pal[256];
89 int c;
91 if (!lpddraw) return;
92 for (c=0; c<len; c++) {
93 pal[c].peRed =color[c].rgbRed;
94 pal[c].peGreen=color[c].rgbGreen;
95 pal[c].peBlue =color[c].rgbBlue;
96 pal[c].peFlags=0;
98 lpddpal->lpvtbl->fnSetEntries(lpddpal,0,start,len,pal);
99 lpddsurf->lpvtbl->fnSetPalette(lpddsurf,lpddpal);
102 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
104 if (!lpddraw) return NULL;
105 if (!lpddsurf) return NULL;
106 if (lpddsurf->lpvtbl->fnLock(lpddsurf,NULL,&sdesc,0,0)) {
107 ERR(ddraw,"could not lock surface!\n");
108 return NULL;
110 if (Pitch) *Pitch=sdesc.lPitch;
111 if (Height) *Height=sdesc.dwHeight;
112 if (Width) *Width=sdesc.dwWidth;
113 if (Depth) *Depth=sdesc.ddpfPixelFormat.x.dwRGBBitCount;
114 return sdesc.y.lpSurface;
117 void VGA_Unlock(void)
119 lpddsurf->lpvtbl->fnUnlock(lpddsurf,sdesc.y.lpSurface);
122 void VGA_Poll(void)
124 char *dat;
125 unsigned Pitch,Height,Width;
126 char *surf;
127 int Y,X;
129 EnterCriticalSection(&vga_crit);
130 if (!vga_polling) {
131 vga_polling++;
132 LeaveCriticalSection(&vga_crit);
133 /* FIXME: optimize by doing this only if the data has actually changed
134 * (in a way similar to DIBSection, perhaps) */
135 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
136 if (!surf) return;
137 dat = DOSMEM_MapDosToLinear(0xa0000);
138 /* copy from virtual VGA frame buffer to DirectDraw surface */
139 for (Y=0; Y<Height; Y++,surf+=Pitch,dat+=Width) {
140 memcpy(surf,dat,Width);
141 for (X=0; X<Width; X++) if (dat[X]) TRACE(ddraw,"data(%d) at (%d,%d)\n",dat[X],X,Y);
143 VGA_Unlock();
144 EnterCriticalSection(&vga_crit);
145 vga_polling--;
147 LeaveCriticalSection(&vga_crit);
150 static BYTE palreg,palcnt;
151 static PALETTEENTRY paldat;
153 void VGA_ioport_out( WORD port, BYTE val )
155 switch (port) {
156 case 0x3c8:
157 palreg=val; palcnt=0; break;
158 case 0x3c9:
159 ((BYTE*)&paldat)[palcnt++]=val << 2;
160 if (palcnt==3) {
161 VGA_SetPalette(&paldat,palreg,1);
162 palreg++;