-
Notifications
You must be signed in to change notification settings - Fork 16
/
dd.cpp
337 lines (267 loc) · 8.13 KB
/
dd.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
#if USE_DDRAW
#include <stdio.h>
#include "types.h"
#include "dd.h"
#include "main.h"
#include "mednafen.h"
#include "movie.h"
#include "video.h"
#include "aggdraw.h"
#include "GPU_osd.h"
#include "pcejin.h"
#include "lua-engine.h"
DDSURFACEDESC2 ddsd;
LPDIRECTDRAW lpdd = NULL;
LPDIRECTDRAW7 lpdd7 = NULL;
LPDIRECTDRAWSURFACE7 lpPrimary = NULL;
LPDIRECTDRAWSURFACE7 lpBack = NULL;
LPDIRECTDRAWCLIPPER lpDDClipPrimary=NULL;
int lpitch = 0;
ushort *lpscreen = NULL;
bool quit = false;
int CreateDDrawBuffers()
{
if (lpDDClipPrimary!=NULL) { IDirectDraw7_Release(lpDDClipPrimary); lpDDClipPrimary = NULL; }
if (lpPrimary != NULL) { IDirectDraw7_Release(lpPrimary); lpPrimary = NULL; }
if (lpBack != NULL) { IDirectDraw7_Release(lpBack); lpBack = NULL; }
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
ddsd.dwFlags = DDSD_CAPS;
if (IDirectDraw7_CreateSurface(lpdd7, &ddsd, &lpPrimary, NULL) != DD_OK) return -1;
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = pcejin.width;//MDFNGameInfo->DisplayRect.w;//256;//video.rotatedwidth();
ddsd.dwHeight = pcejin.height;//MDFNGameInfo->DisplayRect.h;//232;//video.rotatedheight();
if (IDirectDraw7_CreateSurface(lpdd7, &ddsd, &lpBack, NULL) != DD_OK) return -2;
if (IDirectDraw7_CreateClipper(lpdd7, 0, &lpDDClipPrimary, NULL) != DD_OK) return -3;
if (IDirectDrawClipper_SetHWnd(lpDDClipPrimary, 0, g_hWnd) != DD_OK) return -4;
if (IDirectDrawSurface7_SetClipper(lpPrimary, lpDDClipPrimary) != DD_OK) return -5;
return 1;
}
void DirectDrawInit (void)
{
if (DirectDrawCreateEx(NULL, (LPVOID*)&lpdd7, IID_IDirectDraw7, NULL) != DD_OK)
{
printf("Unable to initialize DirectDraw");
// MessageBox(hwnd,"Unable to initialize DirectDraw","Error",MB_OK);
return;
}
if (IDirectDraw7_SetCooperativeLevel(lpdd7,g_hWnd, DDSCL_NORMAL) != DD_OK)
{
printf("Unable to set DirectDraw Cooperative Level");
// MessageBox(hwnd,"Unable to set DirectDraw Cooperative Level","Error",MB_OK);
return;
}
if (CreateDDrawBuffers()<1)
{
printf("Unable to set DirectDraw buffers");
// MessageBox(hwnd,"Unable to set DirectDraw buffers","Error",MB_OK);
return;
}
CreateDDrawBuffers();
}
RECT MainScreenRect;
RECT MainScreenSrcRect;
void UpdateWndRects(HWND hwnd)
{
POINT ptClient;
RECT rc;
int wndWidth, wndHeight;
int defHeight = (pcejin.height);//; + video.screengap);
float ratio;
int oneScreenHeight;
GetClientRect(g_hWnd, &rc);
wndWidth = (rc.right - rc.left);
wndHeight = (rc.bottom - rc.top);
ratio = ((float)wndHeight / (float)defHeight);
oneScreenHeight = (int)((pcejin.height) * ratio);
// Main screen
ptClient.x = rc.left;
ptClient.y = rc.top;
ClientToScreen(hwnd, &ptClient);
MainScreenRect.left = ptClient.x;
MainScreenRect.top = ptClient.y;
ptClient.x = (rc.left + wndWidth);
ptClient.y = (rc.top + oneScreenHeight);
ClientToScreen(hwnd, &ptClient);
MainScreenRect.right = ptClient.x;
MainScreenRect.bottom = ptClient.y;
}
void UpdateSrcRect()
{
// Main screen
MainScreenSrcRect.left = 0;
MainScreenSrcRect.top = 0;
MainScreenSrcRect.right = pcejin.width;
MainScreenSrcRect.bottom = pcejin.height;
}
template<typename T, int bpp> static void doRotate(void* dst) {
u8* buffer = (u8*)dst;
u8* src = (u8*)convert_buffer;
if(ddsd.lPitch == 1024)
{
for(int i = 0; i < pcejin.width*pcejin.height; i++)
((T*)buffer)[i] = ((T*)src)[i];
}
else
{
for(int y = 0; y < pcejin.height; y++)
{
for(int x = 0; x < pcejin.width; x++)
((T*)buffer)[x] = ((T*)src)[(y * pcejin.width) + x];
buffer += ddsd.lPitch;
}
}
}
//32bit to 32bit
static void __forceinline convert32(const uint8* buffer, EmulateSpecStruct *espec){
//NEWTODO
//#if 0
uint8 *pb_ptr = (uint8*)convert_buffer;
for(int y = espec->DisplayRect.y; y < espec->DisplayRect.y + espec->DisplayRect.h; y++)
{
uint16 meow_width = (espec->LineWidths[0].w == ~0) ? espec->DisplayRect.w : espec->LineWidths[y].w;
int meow_x = (espec->LineWidths[0].w == ~0) ? espec->DisplayRect.x : espec->LineWidths[y].x;
uint32 *fb_line = espec->surface->pixels + y * (MDFNGameInfo->pitch >> 2) + meow_x;
for(int x = 0; x < meow_width; x++)
{
uint32 pixel = fb_line[x];
int r, g, b;
espec->surface->DecodeColor(pixel, r, g, b);
// DECOMP_COLOR(pixel, r, g, b);
*pb_ptr++ = b;
*pb_ptr++ = g;
*pb_ptr++ = r;
*pb_ptr++ = 255;
}
}
//#endif
}
static void convert24(const uint8* buffer, EmulateSpecStruct *espec){
//NEWTODO
#if 0
uint8 *pb_ptr = (uint8*)convert_buffer;
for(int y = MDFNGameInfo->DisplayRect.y; y < MDFNGameInfo->DisplayRect.y + MDFNGameInfo->DisplayRect.h; y++)
{
uint16 meow_width = (espec->LineWidths[0].w == ~0) ? MDFNGameInfo->DisplayRect.w : espec->LineWidths[y].w;
int meow_x = (espec->LineWidths[0].w == ~0) ? MDFNGameInfo->DisplayRect.x : espec->LineWidths[y].x;
uint32 *fb_line = espec->pixels + y * (MDFNGameInfo->pitch >> 2) + meow_x;
for(int x = 0; x < meow_width; x++)
{
uint32 pixel = fb_line[x];
int r, g, b;
DECOMP_COLOR(pixel, r, g, b);
*pb_ptr++ = b;
*pb_ptr++ = g;
*pb_ptr++ = r;
}
}
#endif
}
static void convert16(const uint8* buffer, EmulateSpecStruct *espec){
//NEWTODO
#if 0
uint16 *pb_ptr = (uint16*)convert_buffer;
for(int y = MDFNGameInfo->DisplayRect.y; y < MDFNGameInfo->DisplayRect.y + MDFNGameInfo->DisplayRect.h; y++)
{
uint16 meow_width = (espec->LineWidths[0].w == ~0) ? MDFNGameInfo->DisplayRect.w : espec->LineWidths[y].w;
int meow_x = (espec->LineWidths[0].w == ~0) ? MDFNGameInfo->DisplayRect.x : espec->LineWidths[y].x;
uint32 *fb_line = espec->pixels + y * (MDFNGameInfo->pitch >> 2) + meow_x;
for(int x = 0; x < meow_width; x++)
{
uint32 pixel = fb_line[x];
int r, g, b;
DECOMP_COLOR(pixel, r, g, b);
*pb_ptr++ = _16BIT(r,g,b);
// *pb_ptr++ = b;
// *pb_ptr++ = g;
// *pb_ptr++ = r;
}
}
#endif
}
void ClearDirectDrawOutput() {
//Gets rid of the garbage in the middle of the screen
//when using Side By Side mode.
espec.surface->Fill(0,0,0,255);
}
/*void ClearDDrawBuffers() {
// Clear the ddsd memory
memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
ddsd.dwSize = sizeof(DDSURFACEDESC2);
}*/
void render() {
if(!pcejin.romLoaded || espec.skip)
return;
//NEWTODO
//#if 0
if(pcejin.width != espec.DisplayRect.w) {
pcejin.width = espec.DisplayRect.w;
pcejin.height = espec.DisplayRect.h;
CreateDDrawBuffers();
if(!pcejin.maximized)
ScaleScreen((float)pcejin.windowSize);
}
pcejin.width = espec.DisplayRect.w;
pcejin.height = espec.DisplayRect.h;
//#endif
//uint32 *src = (uint32*)convert_buffer;
SetInputDisplayCharacters(pcejin.pads);
UpdateSrcRect();
UpdateWndRects(g_hWnd);
int res = 0;
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags=DDSD_ALL;
res = lpBack->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
if (res==DDERR_SURFACELOST)
{
printf("lost");
if (IDirectDrawSurface7_Restore(lpPrimary)==DD_OK)
IDirectDrawSurface7_Restore(lpBack);
}
else if(res != DD_OK)
return;
switch(ddsd.ddpfPixelFormat.dwRGBBitCount)
{
//NEWTODO
//#if 0
case 32: convert32((uint8*)espec.surface->pixels, &espec); break;
case 24: convert24((uint8*)espec.surface->pixels, &espec); break;
case 16: convert16((uint8*)espec.surface->pixels, &espec); break;
//#endif
default:
{
printf("wrong color");
}
break;
}
CallRegisteredLuaFunctions(LUACALL_AFTEREMULATIONGUI);
osd->update();
DrawHUD();
osd->clear();
aggDraw.hud->attach(convert_buffer, pcejin.width, pcejin.height, 4*pcejin.width);
//uint32 *dst = (uint32*)ddsd.lpSurface;
switch(ddsd.ddpfPixelFormat.dwRGBBitCount)
{
case 32: doRotate<u32,32>(ddsd.lpSurface); break;
case 24: doRotate<u32,24>(ddsd.lpSurface); break;
case 16: doRotate<u16,16>(ddsd.lpSurface); break;
default:
{
printf("wrong color");
}
break;
}
lpBack->Unlock((LPRECT)ddsd.lpSurface);
if(lpPrimary->Blt(&MainScreenRect, lpBack, &MainScreenSrcRect, DDBLT_WAIT, 0) == DDERR_SURFACELOST)
{
printf("lost");
if(IDirectDrawSurface7_Restore(lpPrimary) == DD_OK)
IDirectDrawSurface7_Restore(lpBack);
}
}
#endif // #if USE_DDRAW