mirror of
https://github.com/FunkyFr3sh/cnc-ddraw.git
synced 2025-03-24 17:49:52 +01:00
Move renderers out of surface
This commit is contained in:
parent
5edb561b2b
commit
1521648a6e
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
|||||||
all:
|
all:
|
||||||
i586-mingw32msvc-gcc -D_DEBUG -Wall -Wl,--enable-stdcall-fixup -shared -s -o ddraw.dll main.c mouse.c palette.c surface.c clipper.c ddraw.def -lgdi32 -lopengl32
|
i586-mingw32msvc-gcc -Wall -Wl,--enable-stdcall-fixup -shared -s -o ddraw.dll main.c mouse.c palette.c surface.c clipper.c render_ddraw.c render_opengl.c ddraw.def -lgdi32 -lopengl32
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f ddraw.dll
|
rm -f ddraw.dll
|
||||||
|
55
main.c
55
main.c
@ -30,6 +30,9 @@ void mouse_unlock();
|
|||||||
|
|
||||||
IDirectDrawImpl *ddraw = NULL;
|
IDirectDrawImpl *ddraw = NULL;
|
||||||
|
|
||||||
|
extern struct render render_ddraw;
|
||||||
|
extern struct render render_opengl;
|
||||||
|
|
||||||
HRESULT __stdcall ddraw_Compact(IDirectDrawImpl *This)
|
HRESULT __stdcall ddraw_Compact(IDirectDrawImpl *This)
|
||||||
{
|
{
|
||||||
printf("DirectDraw::Compact(This=%p)\n", This);
|
printf("DirectDraw::Compact(This=%p)\n", This);
|
||||||
@ -136,7 +139,7 @@ HRESULT __stdcall ddraw_Initialize(IDirectDrawImpl *This, GUID *a)
|
|||||||
HRESULT __stdcall ddraw_RestoreDisplayMode(IDirectDrawImpl *This)
|
HRESULT __stdcall ddraw_RestoreDisplayMode(IDirectDrawImpl *This)
|
||||||
{
|
{
|
||||||
printf("DirectDraw::RestoreDisplayMode(This=%p)\n", This);
|
printf("DirectDraw::RestoreDisplayMode(This=%p)\n", This);
|
||||||
return DD_OK;
|
return ddraw->render->RestoreDisplayMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||||
@ -232,29 +235,13 @@ HRESULT __stdcall ddraw_SetCooperativeLevel(IDirectDrawImpl *This, HWND hWnd, DW
|
|||||||
|
|
||||||
This->hWnd = hWnd;
|
This->hWnd = hWnd;
|
||||||
|
|
||||||
#ifndef USE_OPENGL
|
|
||||||
if(This->windowed)
|
if(This->windowed)
|
||||||
{
|
{
|
||||||
This->WndProc = (LRESULT CALLBACK (*)(HWND, UINT, WPARAM, LPARAM))GetWindowLong(This->hWnd, GWL_WNDPROC);
|
This->WndProc = (LRESULT CALLBACK (*)(HWND, UINT, WPARAM, LPARAM))GetWindowLong(This->hWnd, GWL_WNDPROC);
|
||||||
SetWindowLong(This->hWnd, GWL_WNDPROC, (LONG)WndProc);
|
SetWindowLong(This->hWnd, GWL_WNDPROC, (LONG)WndProc);
|
||||||
|
|
||||||
mouse_init(hWnd);
|
mouse_init(hWnd);
|
||||||
|
|
||||||
if(IDirectDraw_SetCooperativeLevel(This->real_ddraw, hWnd, DDSCL_NORMAL) != DD_OK)
|
|
||||||
{
|
|
||||||
printf(" internal SetCooperativeLevel failed\n");
|
|
||||||
return DDERR_GENERIC;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if(IDirectDraw_SetCooperativeLevel(This->real_ddraw, hWnd, dwFlags) != DD_OK)
|
|
||||||
{
|
|
||||||
printf(" internal SetCooperativeLevel failed\n");
|
|
||||||
return DDERR_GENERIC;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return DD_OK;
|
return DD_OK;
|
||||||
}
|
}
|
||||||
@ -298,17 +285,8 @@ HRESULT __stdcall ddraw_SetDisplayMode(IDirectDrawImpl *This, DWORD width, DWORD
|
|||||||
AdjustWindowRect(&dst, GetWindowLong(This->hWnd, GWL_STYLE), FALSE);
|
AdjustWindowRect(&dst, GetWindowLong(This->hWnd, GWL_STYLE), FALSE);
|
||||||
SetWindowPos(This->hWnd, HWND_TOPMOST, dst.left, dst.top, (dst.right - dst.left), (dst.bottom - dst.top), SWP_SHOWWINDOW);
|
SetWindowPos(This->hWnd, HWND_TOPMOST, dst.left, dst.top, (dst.right - dst.left), (dst.bottom - dst.top), SWP_SHOWWINDOW);
|
||||||
}
|
}
|
||||||
#ifndef USE_OPENGL
|
|
||||||
else
|
return ddraw->render->SetDisplayMode(width, height);
|
||||||
{
|
|
||||||
if(IDirectDraw_SetDisplayMode(This->real_ddraw, width, height, mode.dmBitsPerPel) != DD_OK)
|
|
||||||
{
|
|
||||||
printf(" error setting real display mode\n");
|
|
||||||
return DDERR_INVALIDMODE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return DD_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HRESULT __stdcall ddraw_WaitForVerticalBlank(IDirectDrawImpl *This, DWORD a, HANDLE b)
|
HRESULT __stdcall ddraw_WaitForVerticalBlank(IDirectDrawImpl *This, DWORD a, HANDLE b)
|
||||||
@ -316,7 +294,7 @@ HRESULT __stdcall ddraw_WaitForVerticalBlank(IDirectDrawImpl *This, DWORD a, HAN
|
|||||||
#if _DEBUG
|
#if _DEBUG
|
||||||
printf("DirectDraw::WaitForVerticalBlank(This=%p, ...)\n", This);
|
printf("DirectDraw::WaitForVerticalBlank(This=%p, ...)\n", This);
|
||||||
#endif
|
#endif
|
||||||
WaitForSingleObject(ddraw->ev, INFINITE);
|
WaitForSingleObject(ddraw->render->ev, INFINITE);
|
||||||
return DD_OK;
|
return DD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -343,9 +321,6 @@ ULONG __stdcall ddraw_Release(IDirectDrawImpl *This)
|
|||||||
|
|
||||||
if(This->Ref == 0)
|
if(This->Ref == 0)
|
||||||
{
|
{
|
||||||
#ifndef USE_OPENGL
|
|
||||||
IDirectDraw_Release(This->real_ddraw);
|
|
||||||
#endif
|
|
||||||
free(This);
|
free(This);
|
||||||
ddraw = NULL;
|
ddraw = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
@ -410,20 +385,8 @@ HRESULT WINAPI DirectDrawCreate(GUID FAR* lpGUID, LPDIRECTDRAW FAR* lplpDD, IUnk
|
|||||||
ddraw = This;
|
ddraw = This;
|
||||||
|
|
||||||
This->windowed = TRUE;
|
This->windowed = TRUE;
|
||||||
|
This->render = &render_ddraw;
|
||||||
#ifndef USE_OPENGL
|
This->render->Initialize();
|
||||||
This->real_dll = LoadLibrary("system32\\ddraw.dll");
|
|
||||||
if(!This->real_dll)
|
|
||||||
{
|
|
||||||
return DDERR_UNSUPPORTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
This->real_DirectDrawCreate = (HRESULT WINAPI (*)(GUID FAR*, LPDIRECTDRAW FAR*, IUnknown FAR*))GetProcAddress(This->real_dll, "DirectDrawCreate");
|
|
||||||
if(This->real_DirectDrawCreate(NULL, &This->real_ddraw, NULL) != DD_OK)
|
|
||||||
{
|
|
||||||
return DDERR_UNSUPPORTED;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
This->Ref = 0;
|
This->Ref = 0;
|
||||||
ddraw_AddRef(This);
|
ddraw_AddRef(This);
|
||||||
|
20
main.h
20
main.h
@ -38,8 +38,20 @@ typedef struct IDirectDrawImpl
|
|||||||
DWORD bpp;
|
DWORD bpp;
|
||||||
DWORD freq;
|
DWORD freq;
|
||||||
BOOL windowed;
|
BOOL windowed;
|
||||||
CRITICAL_SECTION cs;
|
|
||||||
HANDLE ev;
|
struct render
|
||||||
|
{
|
||||||
|
LPTHREAD_START_ROUTINE (*main)(void *);
|
||||||
|
HRESULT WINAPI (*Initialize)(void);
|
||||||
|
HRESULT WINAPI (*SetDisplayMode)(DWORD width, DWORD height);
|
||||||
|
HRESULT WINAPI (*RestoreDisplayMode)(void);
|
||||||
|
|
||||||
|
HANDLE thread;
|
||||||
|
BOOL run;
|
||||||
|
HANDLE ev;
|
||||||
|
|
||||||
|
/* rest is private */
|
||||||
|
} *render;
|
||||||
|
|
||||||
HWND hWnd;
|
HWND hWnd;
|
||||||
LRESULT CALLBACK (*WndProc)(HWND, UINT, WPARAM, LPARAM);
|
LRESULT CALLBACK (*WndProc)(HWND, UINT, WPARAM, LPARAM);
|
||||||
@ -50,10 +62,6 @@ typedef struct IDirectDrawImpl
|
|||||||
|
|
||||||
BOOL key_ctrl;
|
BOOL key_ctrl;
|
||||||
BOOL key_tab;
|
BOOL key_tab;
|
||||||
|
|
||||||
HMODULE real_dll;
|
|
||||||
LPDIRECTDRAW real_ddraw;
|
|
||||||
HRESULT WINAPI (*real_DirectDrawCreate)(GUID FAR*, LPDIRECTDRAW FAR*, IUnknown FAR*);
|
|
||||||
} IDirectDrawImpl;
|
} IDirectDrawImpl;
|
||||||
|
|
||||||
typedef struct IDirectDrawImplVtbl IDirectDrawImplVtbl;
|
typedef struct IDirectDrawImplVtbl IDirectDrawImplVtbl;
|
||||||
|
@ -34,11 +34,8 @@ HRESULT __stdcall ddraw_palette_SetEntries(IDirectDrawPaletteImpl *This, DWORD d
|
|||||||
|
|
||||||
for(i=0;i<256;i++)
|
for(i=0;i<256;i++)
|
||||||
{
|
{
|
||||||
#if USE_OPENGL
|
This->data_bgr[i] = (lpEntries[i].peBlue<<16)|(lpEntries[i].peGreen<<8)|lpEntries[i].peRed;
|
||||||
This->data[i] = (lpEntries[i].peBlue<<16)|(lpEntries[i].peGreen<<8)|lpEntries[i].peRed;
|
This->data_rgb[i] = (lpEntries[i].peRed<<16)|(lpEntries[i].peGreen<<8)|(lpEntries[i].peBlue);
|
||||||
#else
|
|
||||||
This->data[i] = (lpEntries[i].peRed<<16)|(lpEntries[i].peGreen<<8)|(lpEntries[i].peBlue);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return DD_OK;
|
return DD_OK;
|
||||||
|
@ -30,7 +30,8 @@ typedef struct IDirectDrawPaletteImpl
|
|||||||
|
|
||||||
ULONG Ref;
|
ULONG Ref;
|
||||||
|
|
||||||
int data[256];
|
int data_bgr[256];
|
||||||
|
int data_rgb[256];
|
||||||
|
|
||||||
} IDirectDrawPaletteImpl;
|
} IDirectDrawPaletteImpl;
|
||||||
|
|
||||||
|
185
render_ddraw.c
Normal file
185
render_ddraw.c
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2010 Toni Spets <toni.spets@iki.fi>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include "surface.h"
|
||||||
|
|
||||||
|
struct render_ddraw_impl
|
||||||
|
{
|
||||||
|
DWORD WINAPI (*main)(IDirectDrawSurfaceImpl *surface);
|
||||||
|
HRESULT WINAPI (*Initialize)(void);
|
||||||
|
HRESULT WINAPI (*SetDisplayMode)(DWORD width, DWORD height);
|
||||||
|
HRESULT WINAPI (*RestoreDisplayMode)(void);
|
||||||
|
|
||||||
|
HANDLE thread;
|
||||||
|
BOOL run;
|
||||||
|
HANDLE ev;
|
||||||
|
|
||||||
|
LPDIRECTDRAW ddraw;
|
||||||
|
};
|
||||||
|
|
||||||
|
DWORD WINAPI render_ddraw_main(IDirectDrawSurfaceImpl *surface);
|
||||||
|
HRESULT WINAPI render_ddraw_Initialize();
|
||||||
|
HRESULT WINAPI render_ddraw_SetDisplayMode(DWORD width, DWORD height);
|
||||||
|
HRESULT WINAPI render_ddraw_RestoreDisplayMode(void);
|
||||||
|
|
||||||
|
struct render_ddraw_impl render_ddraw =
|
||||||
|
{
|
||||||
|
render_ddraw_main,
|
||||||
|
render_ddraw_Initialize,
|
||||||
|
render_ddraw_SetDisplayMode,
|
||||||
|
render_ddraw_RestoreDisplayMode,
|
||||||
|
|
||||||
|
NULL,
|
||||||
|
TRUE,
|
||||||
|
NULL,
|
||||||
|
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
HRESULT WINAPI render_ddraw_Initialize()
|
||||||
|
{
|
||||||
|
HMODULE real_dll;
|
||||||
|
HRESULT WINAPI (*real_DirectDrawCreate)(GUID FAR*, LPDIRECTDRAW FAR*, IUnknown FAR*);
|
||||||
|
|
||||||
|
real_dll = LoadLibrary("system32\\ddraw.dll");
|
||||||
|
if(!real_dll)
|
||||||
|
{
|
||||||
|
return DDERR_GENERIC;
|
||||||
|
}
|
||||||
|
|
||||||
|
render_ddraw.ddraw = NULL;
|
||||||
|
|
||||||
|
real_DirectDrawCreate = (HRESULT WINAPI (*)(GUID FAR*, LPDIRECTDRAW FAR*, IUnknown FAR*))GetProcAddress(real_dll, "DirectDrawCreate");
|
||||||
|
if(real_DirectDrawCreate(NULL, &render_ddraw.ddraw, NULL) != DD_OK)
|
||||||
|
{
|
||||||
|
return DDERR_GENERIC;
|
||||||
|
}
|
||||||
|
|
||||||
|
return DD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT WINAPI render_ddraw_SetDisplayMode(DWORD width, DWORD height)
|
||||||
|
{
|
||||||
|
if(ddraw->windowed == FALSE)
|
||||||
|
{
|
||||||
|
return IDirectDraw_SetDisplayMode(render_ddraw.ddraw, width, height, 24);
|
||||||
|
}
|
||||||
|
return DD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT WINAPI render_ddraw_RestoreDisplayMode(void)
|
||||||
|
{
|
||||||
|
if(ddraw->windowed == FALSE)
|
||||||
|
{
|
||||||
|
return IDirectDraw_RestoreDisplayMode(render_ddraw.ddraw);
|
||||||
|
}
|
||||||
|
return DD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD WINAPI render_ddraw_main(IDirectDrawSurfaceImpl *surface)
|
||||||
|
{
|
||||||
|
int i,j;
|
||||||
|
DDSURFACEDESC ddsd;
|
||||||
|
LPDIRECTDRAWSURFACE primary;
|
||||||
|
LPDIRECTDRAWCLIPPER clipper;
|
||||||
|
DWORD width;
|
||||||
|
|
||||||
|
if(render_ddraw.ddraw == NULL)
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
render_ddraw.run = TRUE;
|
||||||
|
|
||||||
|
if(ddraw->windowed)
|
||||||
|
{
|
||||||
|
IDirectDraw_SetCooperativeLevel(render_ddraw.ddraw, ddraw->hWnd, DDSCL_NORMAL);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
IDirectDraw_SetCooperativeLevel(render_ddraw.ddraw, ddraw->hWnd, DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&ddsd, 0, sizeof(DDSURFACEDESC));
|
||||||
|
ddsd.dwSize = sizeof(DDSURFACEDESC);
|
||||||
|
ddsd.dwFlags = DDSD_CAPS;
|
||||||
|
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
|
||||||
|
|
||||||
|
IDirectDraw_CreateSurface(render_ddraw.ddraw, &ddsd, &primary, NULL);
|
||||||
|
|
||||||
|
memset(&ddsd, 0, sizeof(DDSURFACEDESC));
|
||||||
|
ddsd.dwSize = sizeof(DDSURFACEDESC);
|
||||||
|
IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
|
||||||
|
|
||||||
|
width = ddsd.dwWidth * ddsd.lPitch / (ddsd.dwWidth * ddsd.ddpfPixelFormat.dwRGBBitCount / 8);
|
||||||
|
|
||||||
|
IDirectDraw_CreateClipper(render_ddraw.ddraw, 0, &clipper, NULL);
|
||||||
|
IDirectDrawClipper_SetHWnd(clipper, 0, ddraw->hWnd);
|
||||||
|
IDirectDrawSurface_SetClipper(primary, clipper);
|
||||||
|
|
||||||
|
#ifdef FRAME_LIMIT
|
||||||
|
DWORD tick_start;
|
||||||
|
DWORD tick_end;
|
||||||
|
DWORD frame_len = 1000.0f / ddraw->freq;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
render_ddraw.ev = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||||
|
|
||||||
|
while(render_ddraw.run)
|
||||||
|
{
|
||||||
|
ResetEvent(render_ddraw.ev);
|
||||||
|
|
||||||
|
#ifdef FRAME_LIMIT
|
||||||
|
tick_start = GetTickCount();
|
||||||
|
#endif
|
||||||
|
IDirectDrawSurface_Lock(primary, NULL, &ddsd, DDLOCK_WRITEONLY|DDLOCK_WAIT, NULL);
|
||||||
|
|
||||||
|
if(surface->palette)
|
||||||
|
{
|
||||||
|
for(i=0; i<surface->height; i++)
|
||||||
|
{
|
||||||
|
for(j=0; j<surface->width; j++)
|
||||||
|
{
|
||||||
|
((int *)ddsd.lpSurface)[(i+ddraw->winpos.y)*width+(j+ddraw->winpos.x)] = surface->palette->data_rgb[((unsigned char *)surface->surface)[i*surface->lPitch + j*surface->lXPitch]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IDirectDrawSurface_Unlock(primary, NULL);
|
||||||
|
|
||||||
|
#ifdef FRAME_LIMIT
|
||||||
|
tick_end = GetTickCount();
|
||||||
|
|
||||||
|
if(tick_end - tick_start < frame_len)
|
||||||
|
{
|
||||||
|
Sleep( frame_len - (tick_end - tick_start) );
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
SetEvent(render_ddraw.ev);
|
||||||
|
}
|
||||||
|
|
||||||
|
CloseHandle(render_ddraw.ev);
|
||||||
|
|
||||||
|
IDirectDrawClipper_Release(clipper);
|
||||||
|
IDirectDrawSurface_Release(primary);
|
||||||
|
IDirectDraw_Release(render_ddraw.ddraw);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
151
render_opengl.c
Normal file
151
render_opengl.c
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2010 Toni Spets <toni.spets@iki.fi>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include "surface.h"
|
||||||
|
|
||||||
|
struct render_opengl_impl
|
||||||
|
{
|
||||||
|
DWORD WINAPI (*main)(IDirectDrawSurfaceImpl *surface);
|
||||||
|
HRESULT WINAPI (*Initialize)(void);
|
||||||
|
HRESULT WINAPI (*SetDisplayMode)(DWORD width, DWORD height);
|
||||||
|
HRESULT WINAPI (*RestoreDisplayMode)(void);
|
||||||
|
|
||||||
|
HANDLE thread;
|
||||||
|
BOOL run;
|
||||||
|
HANDLE ev;
|
||||||
|
};
|
||||||
|
|
||||||
|
DWORD WINAPI render_opengl_main(IDirectDrawSurfaceImpl *surface);
|
||||||
|
HRESULT WINAPI render_opengl_Initialize();
|
||||||
|
HRESULT WINAPI render_opengl_SetDisplayMode(DWORD width, DWORD height);
|
||||||
|
HRESULT WINAPI render_opengl_RestoreDisplayMode(void);
|
||||||
|
|
||||||
|
struct render_opengl_impl render_opengl =
|
||||||
|
{
|
||||||
|
render_opengl_main,
|
||||||
|
render_opengl_Initialize,
|
||||||
|
render_opengl_SetDisplayMode,
|
||||||
|
render_opengl_RestoreDisplayMode,
|
||||||
|
|
||||||
|
NULL,
|
||||||
|
TRUE,
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
HRESULT WINAPI render_opengl_Initialize()
|
||||||
|
{
|
||||||
|
return DD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT WINAPI render_opengl_SetDisplayMode(DWORD width, DWORD height)
|
||||||
|
{
|
||||||
|
return DD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT WINAPI render_opengl_RestoreDisplayMode(void)
|
||||||
|
{
|
||||||
|
return DD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD WINAPI render_opengl_main(IDirectDrawSurfaceImpl *surface)
|
||||||
|
{
|
||||||
|
int i,j;
|
||||||
|
PIXELFORMATDESCRIPTOR pfd;
|
||||||
|
HDC hDC;
|
||||||
|
HGLRC hRC;
|
||||||
|
|
||||||
|
hDC = GetDC(ddraw->hWnd);
|
||||||
|
|
||||||
|
int *glTex = malloc(surface->width * surface->height * sizeof(int));
|
||||||
|
|
||||||
|
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
|
||||||
|
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
|
||||||
|
pfd.nVersion = 1;
|
||||||
|
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
||||||
|
pfd.iPixelType = PFD_TYPE_RGBA;
|
||||||
|
pfd.cColorBits = 24;
|
||||||
|
pfd.cDepthBits = 16;
|
||||||
|
pfd.iLayerType = PFD_MAIN_PLANE;
|
||||||
|
SetPixelFormat( hDC, ChoosePixelFormat( hDC, &pfd ), &pfd );
|
||||||
|
|
||||||
|
hRC = wglCreateContext( hDC );
|
||||||
|
wglMakeCurrent( hDC, hRC );
|
||||||
|
|
||||||
|
#ifdef FRAME_LIMIT
|
||||||
|
DWORD tick_start;
|
||||||
|
DWORD tick_end;
|
||||||
|
DWORD frame_len = 1000.0f / ddraw->freq;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
render_opengl.ev = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||||
|
|
||||||
|
while(render_opengl.run)
|
||||||
|
{
|
||||||
|
#ifdef FRAME_LIMIT
|
||||||
|
tick_start = GetTickCount();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* convert ddraw surface to opengl texture */
|
||||||
|
for(i=0; i<surface->height; i++)
|
||||||
|
{
|
||||||
|
for(j=0; j<surface->width; j++)
|
||||||
|
{
|
||||||
|
glTex[i*surface->width+j] = surface->palette->data_bgr[((unsigned char *)surface->surface)[i*surface->lPitch + j*surface->lXPitch]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, surface->width, surface->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, glTex);
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
|
||||||
|
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
glBegin(GL_TRIANGLE_FAN);
|
||||||
|
glTexCoord2f(0,0); glVertex2f(-1, 1);
|
||||||
|
glTexCoord2f(1,0); glVertex2f( 1, 1);
|
||||||
|
glTexCoord2f(1,1); glVertex2f( 1, -1);
|
||||||
|
glTexCoord2f(0,1); glVertex2f(-1, -1);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
SwapBuffers(hDC);
|
||||||
|
|
||||||
|
#ifdef FRAME_LIMIT
|
||||||
|
tick_end = GetTickCount();
|
||||||
|
|
||||||
|
if(tick_end - tick_start < frame_len)
|
||||||
|
{
|
||||||
|
Sleep( frame_len - (tick_end - tick_start) );
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
SetEvent(render_opengl.ev);
|
||||||
|
}
|
||||||
|
|
||||||
|
CloseHandle(render_opengl.ev);
|
||||||
|
|
||||||
|
free(glTex);
|
||||||
|
|
||||||
|
wglMakeCurrent(NULL, NULL);
|
||||||
|
wglDeleteContext(hRC);
|
||||||
|
ReleaseDC(ddraw->hWnd, hDC);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
180
surface.c
180
surface.c
@ -20,13 +20,11 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "surface.h"
|
#include "surface.h"
|
||||||
|
|
||||||
DWORD WINAPI ogl_Thread(IDirectDrawSurfaceImpl *This);
|
|
||||||
DWORD WINAPI dd_Thread(IDirectDrawSurfaceImpl *This);
|
|
||||||
void dump_ddsd(DWORD);
|
|
||||||
void dump_ddscaps(DWORD);
|
|
||||||
|
|
||||||
IDirectDrawSurfaceImpl *ddraw_primary = NULL;
|
IDirectDrawSurfaceImpl *ddraw_primary = NULL;
|
||||||
|
|
||||||
|
void dump_ddscaps(DWORD dwCaps);
|
||||||
|
void dump_ddsd(DWORD dwFlags);
|
||||||
|
|
||||||
HRESULT __stdcall ddraw_surface_QueryInterface(IDirectDrawSurfaceImpl *This, REFIID riid, void **obj)
|
HRESULT __stdcall ddraw_surface_QueryInterface(IDirectDrawSurfaceImpl *This, REFIID riid, void **obj)
|
||||||
{
|
{
|
||||||
printf("DirectDrawSurface::QueryInterface(This=%p, riid=%08X, obj=%p)\n", This, (unsigned int)riid, obj);
|
printf("DirectDrawSurface::QueryInterface(This=%p, riid=%08X, obj=%p)\n", This, (unsigned int)riid, obj);
|
||||||
@ -48,11 +46,10 @@ ULONG __stdcall ddraw_surface_Release(IDirectDrawSurfaceImpl *This)
|
|||||||
|
|
||||||
if(This->Ref == 0)
|
if(This->Ref == 0)
|
||||||
{
|
{
|
||||||
if(This->dThread)
|
if(This->caps == DDSCAPS_PRIMARYSURFACE)
|
||||||
{
|
{
|
||||||
This->dRun = FALSE;
|
ddraw->render->run = FALSE;
|
||||||
WaitForSingleObject(This->dThread, INFINITE);
|
WaitForSingleObject(ddraw->render->thread, INFINITE);
|
||||||
This->dThread = NULL;
|
|
||||||
}
|
}
|
||||||
if(This->surface)
|
if(This->surface)
|
||||||
{
|
{
|
||||||
@ -99,7 +96,7 @@ HRESULT __stdcall ddraw_surface_Blt(IDirectDrawSurfaceImpl *This, LPRECT lpDestR
|
|||||||
|
|
||||||
if(This->caps & DDSCAPS_PRIMARYSURFACE)
|
if(This->caps & DDSCAPS_PRIMARYSURFACE)
|
||||||
{
|
{
|
||||||
WaitForSingleObject(ddraw->ev, INFINITE);
|
WaitForSingleObject(ddraw->render->ev, INFINITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Source)
|
if(Source)
|
||||||
@ -401,9 +398,7 @@ HRESULT __stdcall ddraw_CreateSurface(IDirectDrawImpl *This, LPDDSURFACEDESC lpD
|
|||||||
Surface->lpVtbl = &siface;
|
Surface->lpVtbl = &siface;
|
||||||
|
|
||||||
/* private stuff */
|
/* private stuff */
|
||||||
Surface->parent = This;
|
|
||||||
Surface->bpp = This->bpp;
|
Surface->bpp = This->bpp;
|
||||||
Surface->dRun = TRUE;
|
|
||||||
|
|
||||||
if(lpDDSurfaceDesc->dwFlags & DDSD_CAPS)
|
if(lpDDSurfaceDesc->dwFlags & DDSD_CAPS)
|
||||||
{
|
{
|
||||||
@ -413,7 +408,6 @@ HRESULT __stdcall ddraw_CreateSurface(IDirectDrawImpl *This, LPDDSURFACEDESC lpD
|
|||||||
|
|
||||||
Surface->width = This->width;
|
Surface->width = This->width;
|
||||||
Surface->height = This->height;
|
Surface->height = This->height;
|
||||||
Surface->hWnd = This->hWnd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dump_ddscaps(lpDDSurfaceDesc->ddsCaps.dwCaps);
|
dump_ddscaps(lpDDSurfaceDesc->ddsCaps.dwCaps);
|
||||||
@ -442,170 +436,12 @@ HRESULT __stdcall ddraw_CreateSurface(IDirectDrawImpl *This, LPDDSURFACEDESC lpD
|
|||||||
|
|
||||||
if(Surface->caps & DDSCAPS_PRIMARYSURFACE)
|
if(Surface->caps & DDSCAPS_PRIMARYSURFACE)
|
||||||
{
|
{
|
||||||
#if USE_OPENGL
|
This->render->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)This->render->main, (void *)Surface, 0, NULL);
|
||||||
Surface->dThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ogl_Thread, (void *)Surface, 0, NULL);
|
|
||||||
#else
|
|
||||||
Surface->dThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)dd_Thread, (void *)Surface, 0, NULL);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return DD_OK;
|
return DD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if USE_OPENGL
|
|
||||||
DWORD WINAPI ogl_Thread(IDirectDrawSurfaceImpl *This)
|
|
||||||
{
|
|
||||||
int i,j;
|
|
||||||
PIXELFORMATDESCRIPTOR pfd;
|
|
||||||
|
|
||||||
This->hDC = GetDC(This->hWnd);
|
|
||||||
|
|
||||||
int *glTex = malloc(This->width * This->height * sizeof(int));
|
|
||||||
|
|
||||||
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
|
|
||||||
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
|
|
||||||
pfd.nVersion = 1;
|
|
||||||
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
|
||||||
pfd.iPixelType = PFD_TYPE_RGBA;
|
|
||||||
pfd.cColorBits = 24;
|
|
||||||
pfd.cDepthBits = 16;
|
|
||||||
pfd.iLayerType = PFD_MAIN_PLANE;
|
|
||||||
SetPixelFormat( This->hDC, ChoosePixelFormat( This->hDC, &pfd ), &pfd );
|
|
||||||
|
|
||||||
This->hRC = wglCreateContext( This->hDC );
|
|
||||||
wglMakeCurrent( This->hDC, This->hRC );
|
|
||||||
|
|
||||||
DWORD tick_start;
|
|
||||||
DWORD tick_end;
|
|
||||||
DWORD frame_len = 1000.0f / 60.0f /*This->parent->freq*/;
|
|
||||||
|
|
||||||
while(This->dRun)
|
|
||||||
{
|
|
||||||
tick_start = GetTickCount();
|
|
||||||
|
|
||||||
/* convert ddraw surface to opengl texture */
|
|
||||||
for(i=0; i<This->height; i++)
|
|
||||||
{
|
|
||||||
for(j=0; j<This->width; j++)
|
|
||||||
{
|
|
||||||
glTex[i*This->width+j] = This->palette->data[((unsigned char *)This->surface)[i*This->lPitch + j*This->lXPitch]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, This->width, This->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, glTex);
|
|
||||||
|
|
||||||
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
|
|
||||||
|
|
||||||
glEnable(GL_TEXTURE_2D);
|
|
||||||
|
|
||||||
glBegin(GL_TRIANGLE_FAN);
|
|
||||||
glTexCoord2f(0,0); glVertex2f(-1, 1);
|
|
||||||
glTexCoord2f(1,0); glVertex2f( 1, 1);
|
|
||||||
glTexCoord2f(1,1); glVertex2f( 1, -1);
|
|
||||||
glTexCoord2f(0,1); glVertex2f(-1, -1);
|
|
||||||
glEnd();
|
|
||||||
|
|
||||||
SwapBuffers(This->hDC);
|
|
||||||
|
|
||||||
tick_end = GetTickCount();
|
|
||||||
|
|
||||||
if(tick_end - tick_start < frame_len)
|
|
||||||
{
|
|
||||||
EnterCriticalSection(&ddraw->cs);
|
|
||||||
Sleep( frame_len - (tick_end - tick_start) );
|
|
||||||
LeaveCriticalSection(&ddraw->cs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(glTex);
|
|
||||||
|
|
||||||
wglMakeCurrent(NULL, NULL);
|
|
||||||
wglDeleteContext(This->hRC);
|
|
||||||
ReleaseDC(This->hWnd, This->hDC);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else // if USE_OPENGL
|
|
||||||
|
|
||||||
DWORD WINAPI dd_Thread(IDirectDrawSurfaceImpl *This)
|
|
||||||
{
|
|
||||||
int i,j;
|
|
||||||
DDSURFACEDESC ddsd;
|
|
||||||
LPDIRECTDRAWSURFACE primary;
|
|
||||||
LPDIRECTDRAWCLIPPER clipper;
|
|
||||||
DWORD width;
|
|
||||||
|
|
||||||
memset(&ddsd, 0, sizeof(DDSURFACEDESC));
|
|
||||||
ddsd.dwSize = sizeof(DDSURFACEDESC);
|
|
||||||
ddsd.dwFlags = DDSD_CAPS;
|
|
||||||
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
|
|
||||||
|
|
||||||
IDirectDraw_CreateSurface(This->parent->real_ddraw, &ddsd, &primary, NULL);
|
|
||||||
|
|
||||||
memset(&ddsd, 0, sizeof(DDSURFACEDESC));
|
|
||||||
ddsd.dwSize = sizeof(DDSURFACEDESC);
|
|
||||||
IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
|
|
||||||
|
|
||||||
width = ddsd.dwWidth * ddsd.lPitch / (ddsd.dwWidth * ddsd.ddpfPixelFormat.dwRGBBitCount / 8);
|
|
||||||
|
|
||||||
IDirectDraw_CreateClipper(This->parent->real_ddraw, 0, &clipper, NULL);
|
|
||||||
IDirectDrawClipper_SetHWnd(clipper, 0, This->hWnd);
|
|
||||||
IDirectDrawSurface_SetClipper(primary, clipper);
|
|
||||||
|
|
||||||
#ifdef FRAME_LIMIT
|
|
||||||
DWORD tick_start;
|
|
||||||
DWORD tick_end;
|
|
||||||
DWORD frame_len = 1000.0f / This->parent->freq;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
InitializeCriticalSection(&ddraw->cs);
|
|
||||||
ddraw->ev = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
||||||
|
|
||||||
while(This->dRun)
|
|
||||||
{
|
|
||||||
ResetEvent(ddraw->ev);
|
|
||||||
|
|
||||||
#ifdef FRAME_LIMIT
|
|
||||||
tick_start = GetTickCount();
|
|
||||||
#endif
|
|
||||||
IDirectDrawSurface_Lock(primary, NULL, &ddsd, DDLOCK_WRITEONLY|DDLOCK_WAIT, NULL);
|
|
||||||
|
|
||||||
if(This->palette)
|
|
||||||
{
|
|
||||||
for(i=0; i<This->height; i++)
|
|
||||||
{
|
|
||||||
for(j=0; j<This->width; j++)
|
|
||||||
{
|
|
||||||
((int *)ddsd.lpSurface)[(i+This->parent->winpos.y)*width+(j+This->parent->winpos.x)] = This->palette->data[((unsigned char *)This->surface)[i*This->lPitch + j*This->lXPitch]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
IDirectDrawSurface_Unlock(primary, NULL);
|
|
||||||
|
|
||||||
#ifdef FRAME_LIMIT
|
|
||||||
tick_end = GetTickCount();
|
|
||||||
|
|
||||||
if(tick_end - tick_start < frame_len)
|
|
||||||
{
|
|
||||||
Sleep( frame_len - (tick_end - tick_start) );
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
SetEvent(ddraw->ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
CloseHandle(ddraw->ev);
|
|
||||||
|
|
||||||
IDirectDrawClipper_Release(clipper);
|
|
||||||
IDirectDrawSurface_Release(primary);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void dump_ddscaps(DWORD dwCaps)
|
void dump_ddscaps(DWORD dwCaps)
|
||||||
{
|
{
|
||||||
if(dwCaps & DDSCAPS_PRIMARYSURFACE)
|
if(dwCaps & DDSCAPS_PRIMARYSURFACE)
|
||||||
|
12
surface.h
12
surface.h
@ -39,24 +39,12 @@ typedef struct IDirectDrawSurfaceImpl
|
|||||||
DWORD bpp;
|
DWORD bpp;
|
||||||
DWORD caps;
|
DWORD caps;
|
||||||
|
|
||||||
IDirectDrawImpl *parent;
|
|
||||||
IDirectDrawPaletteImpl *palette;
|
IDirectDrawPaletteImpl *palette;
|
||||||
|
|
||||||
void *surface;
|
void *surface;
|
||||||
DWORD lPitch;
|
DWORD lPitch;
|
||||||
DWORD lXPitch;
|
DWORD lXPitch;
|
||||||
|
|
||||||
HWND hWnd;
|
|
||||||
HANDLE dThread;
|
|
||||||
BOOL dRun;
|
|
||||||
HANDLE flipEvent;
|
|
||||||
|
|
||||||
#if USE_OPENGL
|
|
||||||
HDC hDC;
|
|
||||||
HGLRC hRC;
|
|
||||||
int *glTex;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} IDirectDrawSurfaceImpl;
|
} IDirectDrawSurfaceImpl;
|
||||||
|
|
||||||
struct IDirectDrawSurfaceImplVtbl
|
struct IDirectDrawSurfaceImplVtbl
|
||||||
|
Loading…
x
Reference in New Issue
Block a user