forked from vcc6809/VCC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectDrawInterface.cpp
481 lines (442 loc) · 15.7 KB
/
DirectDrawInterface.cpp
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
/*
Copyright 2015 by Joseph Forgione
This file is part of VCC (Virtual Color Computer).
VCC (Virtual Color Computer) 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 3 of the License, or
(at your option) any later version.
VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
*/
#include <afxwin.h>
#include <commctrl.h> // Windows common controls
#include "defines.h"
#include "ddraw.h"
#include "stdio.h"
#include "DirectDrawInterface.h"
#include "resource.h"
#include "tcc1014graphics.h"
#include "throttle.h"
//Global Variables for Direct Draw funcions
LPDIRECTDRAW g_pDD = NULL; // The DirectDraw object
LPDIRECTDRAWCLIPPER g_pClipper = NULL; // Clipper for primary surface
LPDIRECTDRAWSURFACE g_pDDS = NULL; // Primary surface
LPDIRECTDRAWSURFACE g_pDDSBack = NULL; // Back surface
static IDirectDrawPalette* ddpal; //Needed for 8bit Palette mode
static HWND hwndStatusBar=NULL;
static RECT WindowDefaultSize;
static unsigned int StatusBarHeight=0;
static TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
static TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
static HINSTANCE g_hInstance;
static int g_nCmdShow;
static WNDCLASSEX wcex;
static unsigned char InfoBand=1;
static unsigned char Resizeable=1;
static unsigned char ForceAspect=1;
static char StatusText[255]="";
static unsigned int Color=0;
//Function Prototypes for this module
extern LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //Callback for the main window
void DisplayFlip(SystemState *);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
g_hInstance=hInstance;
g_nCmdShow=nCmdShow;
AfxInitRichEdit();
LoadString(g_hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(g_hInstance, IDS_APP_TITLE, szWindowClass, MAX_LOADSTRING);
return TRUE;
}
bool CreateDDWindow(SystemState *CWState)
{
HRESULT hr;
DDSURFACEDESC ddsd; // A structure to describe the surfaces we want
RECT rStatBar;
RECT rc;
unsigned char ColorValues[4]={0,85,170,255};
PALETTEENTRY pal[256];
memset(&ddsd, 0, sizeof(ddsd)); // Clear all members of the structure to 0
ddsd.dwSize = sizeof(ddsd); // The first parameter of the structure must contain the size of the structure
rc.top=0;
rc.left=0;
rc.right=CWState->WindowSize.x;
rc.bottom=CWState->WindowSize.y;
if (CWState->WindowHandle != NULL) //If its go a value it must be a mode switch
{
if (g_pDD != NULL)
g_pDD->Release(); //Destroy the current Window
DestroyWindow(CWState->WindowHandle);
UnregisterClass(wcex.lpszClassName,wcex.hInstance);
}
wcex.cbSize = sizeof(WNDCLASSEX); //And Rebuilt it from scratch
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = g_hInstance;
wcex.hIcon = LoadIcon(g_hInstance, (LPCTSTR)IDI_COCO3);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = (LPCSTR)IDR_MENU;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_COCO3);
if (CWState->FullScreen)
{
wcex.lpszMenuName =NULL; //Fullscreen has no Menu Bar and no Mouse pointer
wcex.hCursor = LoadCursor(g_hInstance, MAKEINTRESOURCE(IDC_NONE));
}
if (!RegisterClassEx(&wcex))
return FALSE;
switch (CWState->FullScreen)
{
case 0: //Windowed Mode
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, TRUE ); //Add the height of the menu bar
CWState->WindowHandle = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, g_hInstance, NULL);
if (!CWState->WindowHandle) //Can't creat window
return FALSE;
hwndStatusBar =CreateStatusWindow (WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | CCS_BOTTOM , "Ready", CWState->WindowHandle, 2) ;
if (!hwndStatusBar) //Can't create Status bar
return FALSE;
GetWindowRect(hwndStatusBar,&rStatBar); //Get the size of the Status bar
StatusBarHeight= rStatBar.bottom - rStatBar.top; //Calculate it height
GetWindowRect(CWState->WindowHandle,&rStatBar); //Get the size of main window and add height of status bar then resize Main
MoveWindow(CWState->WindowHandle,rStatBar.left,rStatBar.top,rStatBar.right - rStatBar.left,(rStatBar.bottom+StatusBarHeight)-rStatBar.top,1);
SendMessage(hwndStatusBar,WM_SIZE,0,0); //Redraw Status bar in new position
GetWindowRect(CWState->WindowHandle,&WindowDefaultSize); //And save the Final size of the Window
ShowWindow(CWState->WindowHandle, g_nCmdShow);
UpdateWindow(CWState->WindowHandle);
hr = DirectDrawCreate( NULL, &g_pDD, NULL ); // Initialize DirectDraw
if (hr) return FALSE;
hr = g_pDD->SetCooperativeLevel(CWState->WindowHandle, DDSCL_NORMAL); // Set DDSCL_NORMAL to use windowed mode
if (hr) return FALSE;
ddsd.dwFlags = DDSD_CAPS ;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hr = g_pDD->CreateSurface(&ddsd, &g_pDDS, NULL);
if (hr) return FALSE;
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS ;
ddsd.dwWidth = CWState->WindowSize.x; // Make our off-screen surface
ddsd.dwHeight = CWState->WindowSize.y;
ddsd.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY; // Try to create back buffer in video RAM
hr = g_pDD->CreateSurface(&ddsd, &g_pDDSBack, NULL);
if (hr) // If not enough Video Ram
{
ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY; // Try to create back buffer in System RAM
hr = g_pDD->CreateSurface(&ddsd, &g_pDDSBack, NULL);
if (hr) return FALSE; //Giving Up
MessageBox(0,"Creating Back Buffer in System Ram!\nThis will be slower","Performance Warning",0);
}
hr= g_pDD->GetDisplayMode(&ddsd);
if (hr) return FALSE;
hr = g_pDD->CreateClipper(0, &g_pClipper, NULL); // Create the clipper using the DirectDraw object
if (hr) return FALSE;
hr = g_pClipper->SetHWnd(0, CWState->WindowHandle); // Assign your window's HWND to the clipper
if (hr) return FALSE;
hr = g_pDDS->SetClipper(g_pClipper); // Attach the clipper to the primary surface
if (hr) return FALSE;
hr= g_pDDSBack->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
if (hr) return FALSE;
hr = g_pDDSBack->Unlock( NULL ); // Unlock surface
if (hr) return FALSE;
break;
case 1: //Full Screen Mode
ddsd.lPitch=0;
ddsd.ddpfPixelFormat.dwRGBBitCount=0;
CWState->WindowHandle = CreateWindow(szWindowClass, NULL, WS_POPUP | WS_VISIBLE,0, 0, CWState->WindowSize.x, CWState->WindowSize.y, NULL, NULL, g_hInstance, NULL);
if (!CWState->WindowHandle )
return FALSE;
GetWindowRect(CWState->WindowHandle,&WindowDefaultSize);
ShowWindow(CWState->WindowHandle, g_nCmdShow);
UpdateWindow(CWState->WindowHandle);
hr = DirectDrawCreate( NULL, &g_pDD, NULL ); // Initialize DirectDraw
if (hr) return FALSE;
hr = g_pDD->SetCooperativeLevel(CWState->WindowHandle, DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_NOWINDOWCHANGES);
if (hr) return FALSE;
hr = g_pDD->SetDisplayMode(CWState->WindowSize.x, CWState->WindowSize.y, 8); // Set 640x480x8 Bit full-screen mode
if (hr) return FALSE;
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
ddsd.dwBackBufferCount = 1;
hr = g_pDD->CreateSurface(&ddsd, &g_pDDS, NULL);
if (hr) return FALSE;
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
g_pDDS->GetAttachedSurface(&ddsd.ddsCaps, &g_pDDSBack);
hr= g_pDD->GetDisplayMode(&ddsd);
if (hr) return FALSE;
//***********************************TEST*****************************************
for ( unsigned short i=0;i<=63;i++)
{
pal[i+128].peBlue= ColorValues[(i & 8 ) >>2 | (i & 1) ];
pal[i+128].peGreen=ColorValues[(i & 16) >>3 | (i & 2) >>1];
pal[i+128].peRed= ColorValues[(i & 32) >>4 | (i & 4) >>2];
pal[i+128].peFlags = PC_RESERVED | PC_NOCOLLAPSE;
}
g_pDD->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256,pal,&ddpal,NULL);
g_pDDS->SetPalette(ddpal); // Set pallete for Primary surface
//**********************************END TEST***************************************
break;
}
return TRUE;
}
/*--------------------------------------------------------------------------*/
// Checks if the memory associated with surfaces is lost and restores if necessary.
void CheckSurfaces()
{
if (g_pDDS) // Check the primary surface
if (g_pDDS->IsLost() == DDERR_SURFACELOST)
g_pDDS->Restore();
if (g_pDDSBack) // Check the back buffer
if (g_pDDSBack->IsLost() == DDERR_SURFACELOST)
g_pDDSBack->Restore();
// g_pDDS->SetPalette(ddpal);
}
/*--------------------------------------------------------------------------*/
void DisplayFlip(SystemState *DFState) // Double buffering flip
{
static HRESULT hr;
static RECT rcSrc; // source blit rectangle
static RECT rcDest; // destination blit rectangle
static RECT Temp;
static POINT p;
if (DFState->FullScreen) // if we're windowed do the blit, else just Flip
hr = g_pDDS->Flip(NULL,DDFLIP_NOVSYNC |DDFLIP_DONOTWAIT ); //DDFLIP_WAIT
else
{
p.x = 0; p.y = 0;
::ClientToScreen(DFState->WindowHandle, &p); // find out where on the primary surface our window lives
::GetClientRect(DFState->WindowHandle, &rcDest);
OffsetRect(&rcDest, p.x, p.y);
SetRect(&rcSrc, 0, 0, DFState->WindowSize.x, DFState->WindowSize.y);
if (Resizeable)
rcDest.bottom-=StatusBarHeight;
else
{
rcDest.right=rcDest.left+DFState->WindowSize.x;
rcDest.bottom=rcDest.top+DFState->WindowSize.y;
GetWindowRect(DFState->WindowHandle,&Temp);
MoveWindow(DFState->WindowHandle,Temp.left,Temp.top,WindowDefaultSize.right-WindowDefaultSize.left,WindowDefaultSize.bottom-WindowDefaultSize.top,1);
}
if (g_pDDSBack == NULL)
MessageBox(0,"Odd","Error",0);
hr = g_pDDS->Blt(&rcDest, g_pDDSBack, &rcSrc,DDBLT_WAIT , NULL); //DDBLT_WAIT
}
return;
}
unsigned char LockScreen(SystemState *LSState)
{
HRESULT hr;
DDSURFACEDESC ddsd; // A structure to describe the surfaces we want
memset(&ddsd, 0, sizeof(ddsd)); // Clear all members of the structure to 0
ddsd.dwSize = sizeof(ddsd); // The first parameter of the structure must contain the size of the structure
CheckSurfaces();
// Lock entire surface, wait if it is busy, return surface memory pointer
hr = g_pDDSBack->Lock( NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL );
if (hr)
{
// MessageBox(0,"Can't Lock surface","Error",0);
return(1);
}
switch (ddsd.ddpfPixelFormat.dwRGBBitCount)
{
case 8:
LSState->SurfacePitch=ddsd.lPitch;
LSState->BitDepth=0;
break;
case 15:
case 16:
LSState->SurfacePitch=ddsd.lPitch/2;
LSState->BitDepth=1;
break;
case 24:
MessageBox(0,"24 Bit color is currnetly unsupported","Ok",0);
exit(0);
LSState->SurfacePitch=ddsd.lPitch;
LSState->BitDepth=2;
break;
case 32:
LSState->SurfacePitch=ddsd.lPitch/4;
LSState->BitDepth=3;
break;
default:
MessageBox(0,"Unsupported Color Depth!","Error",0);
return 1;
break;
}
if (ddsd.lpSurface==NULL)
MessageBox(0,"Returning NULL!!","ok",0);
LSState->PTRsurface8=(unsigned char *)ddsd.lpSurface;
LSState->PTRsurface16=(unsigned short *)ddsd.lpSurface;
LSState->PTRsurface32=(unsigned int *)ddsd.lpSurface;
return(0);
}
void UnlockScreen(SystemState *USState)
{
static HRESULT hr;
static unsigned char Index=0;
static HDC hdc;
if (USState->FullScreen & InfoBand) //Put StatusText for full screen here
{
g_pDDSBack->GetDC(&hdc);
SetBkColor( hdc, RGB( 0, 0, 0 ) );
SetTextColor(hdc, RGB(255,255,255));
Index=strlen(StatusText);
for (;Index<132;Index++)
StatusText[Index]=32;
StatusText[Index]=0;
TextOut(hdc, 0, 0, StatusText, 132);
g_pDDSBack->ReleaseDC(hdc);
}
hr = g_pDDSBack->Unlock( NULL );
DisplayFlip(USState);
return;
}
void SetStatusBarText( char *TextBuffer,SystemState *STState)
{
if (!STState->FullScreen)
{
SendMessage(hwndStatusBar,WM_SETTEXT ,strlen(TextBuffer),(LPARAM)(LPCSTR)TextBuffer);
SendMessage(hwndStatusBar,WM_SIZE,0,0);
}
else
strcpy(StatusText,TextBuffer);
return;
}
void Cls(unsigned int ClsColor,SystemState *CLState)
{
CLState->ResetPending=3; //Tell Main loop to hold Emu
Color=ClsColor;
return;
}
void DoCls(SystemState *CLStatus)
{
unsigned short x=0,y=0;
if(LockScreen(CLStatus))
return;
switch (CLStatus->BitDepth)
{
case 0:
for (y=0;y<480;y++)
for (x=0;x<640;x++)
CLStatus->PTRsurface8[x + (y*CLStatus->SurfacePitch) ]=Color|128;
break;
case 1:
for (y=0;y<480;y++)
for (x=0;x<640;x++)
CLStatus->PTRsurface16[x + (y*CLStatus->SurfacePitch) ]=Color;
break;
case 2:
for (y=0;y<480;y++)
for (x=0;x<640;x++)
{
CLStatus->PTRsurface8[(x*3) + (y*CLStatus->SurfacePitch) ]=(Color & 0xFF0000)>>16;
CLStatus->PTRsurface8[(x*3)+1 + (y*CLStatus->SurfacePitch) ]=(Color & 0x00FF00)>>8;
CLStatus->PTRsurface8[(x*3)+2 + (y*CLStatus->SurfacePitch) ]=(Color & 0xFF);
}
break;
case 3:
for (y=0;y<480;y++)
for (x=0;x<640;x++)
CLStatus->PTRsurface32[x + (y*CLStatus->SurfacePitch) ]=Color;
break;
default:
return;
}
UnlockScreen(CLStatus);
return;
}
unsigned char SetInfoBand( unsigned char Tmp)
{
if (Tmp!=QUERY)
InfoBand=Tmp;
return(InfoBand);
}
unsigned char SetResize(unsigned char Tmp)
{
if (Tmp!=QUERY)
Resizeable=Tmp;
return(Resizeable);
}
unsigned char SetAspect (unsigned char Tmp)
{
if (Tmp!=QUERY)
ForceAspect=Tmp;
return(ForceAspect);
}
float Static(SystemState *STState)
{
unsigned short x=0;
static unsigned short y=0;
// unsigned char Depth=0;
unsigned char Temp=0;
static unsigned short TextX=0,TextY=0;
static unsigned char Counter,Counter1=32;
static char Phase=1;
static char Message[]=" Signal Missing! Press F9";
static unsigned char GreyScales[4]={128,135,184,191};
HDC hdc;
LockScreen(STState);
if (STState->PTRsurface32==NULL)
return(0);
// y=(y+1) % 480;
switch (STState->BitDepth)
{
case 0:
for (y=0;y<480;y+=2)
for (x=0;x<160;x++)
{
Temp=rand() & 3;
STState->PTRsurface32[x + (y*STState->SurfacePitch>>2) ]= GreyScales[Temp]|(GreyScales[Temp]<<8)|(GreyScales[Temp]<<16) | (GreyScales[Temp]<<24);
STState->PTRsurface32[x + ((y+1)*STState->SurfacePitch>>2) ]= GreyScales[Temp]|(GreyScales[Temp]<<8)|(GreyScales[Temp]<<16) | (GreyScales[Temp]<<24);
}
break;
case 1:
for (y=0;y<480;y+=2)
for (x=0;x<320;x++)
{
Temp=rand() & 31;
STState->PTRsurface32[x + (y*STState->SurfacePitch>>1) ]=Temp | (Temp <<6) | (Temp<<11) | (Temp<<16) |(Temp<<22)| (Temp<<27);
STState->PTRsurface32[x + ((y+1)*STState->SurfacePitch>>1) ]=Temp | (Temp <<6) | (Temp<<11) | (Temp<<16) |(Temp<<22)| (Temp<<27);
}
break;
case 2:
for (y=0;y<480;y++)
for (x=0;x<640;x++)
{
STState->PTRsurface8[(x*3) + (y*STState->SurfacePitch) ]=Temp;
STState->PTRsurface8[(x*3)+1 + (y*STState->SurfacePitch) ]=Temp<<8;
STState->PTRsurface8[(x*3)+2 + (y*STState->SurfacePitch) ]=Temp <<16;
}
break;
case 3:
for (y=0;y<480;y++)
for (x=0;x<640;x++)
{
Temp=rand() & 255;
STState->PTRsurface32[x + (y*STState->SurfacePitch) ]=Temp | (Temp<<8) | (Temp <<16);
}
break;
default:
return(0);
}
g_pDDSBack->GetDC(&hdc);
SetBkColor( hdc, 0);
SetTextColor(hdc, RGB(Counter1<<2,Counter1<<2,Counter1<<2));
TextOut(hdc, TextX, TextY, Message, strlen(Message));
Counter++;
Counter1+=Phase;
if ((Counter1==60) | (Counter1==20))
Phase=-Phase;
Counter%=60; //about 1 seconds
if (!Counter)
{
TextX=rand()%580;
TextY=rand()%470;
}
g_pDDSBack->ReleaseDC(hdc);
UnlockScreen(STState);
return(CalculateFPS());
}