From ad037e718cbb06035121f2636a2ac61a4349d286 Mon Sep 17 00:00:00 2001 From: Toni Spets Date: Sun, 17 Oct 2010 10:53:01 +0300 Subject: [PATCH] Initialize OpenGL and clear the screen to blue on update --- Makefile | 2 +- main.c | 39 +++++++++++++++++++++++++++++++++++++++ main.h | 5 +++++ surface.c | 32 +++++++++++++++++++++++++++++++- surface.h | 3 +++ 5 files changed, 79 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index cde0558..5ea6a88 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: - i586-mingw32msvc-gcc -Wall -Wl,--enable-stdcall-fixup -shared -s -o ddraw.dll main.c palette.c surface.c ddraw.def + i586-mingw32msvc-gcc -Wall -Wl,--enable-stdcall-fixup -shared -s -o ddraw.dll main.c palette.c surface.c ddraw.def -lgdi32 -lopengl32 clean: rm -f ddraw.dll diff --git a/main.c b/main.c index 89da013..8349bb7 100644 --- a/main.c +++ b/main.c @@ -67,6 +67,9 @@ HRESULT ddraw_SetDisplayMode(void *_This, DWORD width, DWORD height, DWORD bpp) This->height = height; This->bpp = bpp; + MoveWindow(This->hWnd, 0, 0, This->width, This->height, TRUE); + SetWindowLong(This->hWnd, GWL_STYLE, WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE); + return DD_OK; } @@ -132,8 +135,12 @@ fakeDirectDraw iface = null //WaitForVerticalBlank }; +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); + HRESULT WINAPI DirectDrawCreate(GUID FAR* lpGUID, LPDIRECTDRAW FAR* lplpDD, IUnknown FAR* pUnkOuter) { + WNDCLASS wc; + printf("DirectDrawCreate(lpGUID=%p, lplpDD=%p, pUnkOuter=%p)\n", lpGUID, lplpDD, pUnkOuter); fakeDirectDrawObject *This = (fakeDirectDrawObject *)malloc(sizeof(fakeDirectDrawObject)); @@ -142,5 +149,37 @@ HRESULT WINAPI DirectDrawCreate(GUID FAR* lpGUID, LPDIRECTDRAW FAR* lplpDD, IUnk printf(" This = %p\n", This); *lplpDD = (LPDIRECTDRAW)This; + This->hInstance = GetModuleHandle( 0 ); + + /* create dummy window */ + wc.style = CS_OWNDC; + wc.lpfnWndProc = WndProc; + wc.cbClsExtra = 0; + wc.cbWndExtra = 0; + wc.hInstance = This->hInstance; + wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); + wc.hCursor = LoadCursor( NULL, IDC_ARROW ); + wc.hbrBackground = 0; + wc.lpszMenuName = NULL; + wc.lpszClassName = "cnc-ddraw"; + RegisterClass( &wc ); + + This->hWnd = CreateWindow("cnc-ddraw", "cnc-ddraw", 0, 0, 0, 0, 0, NULL, NULL, This->hInstance, NULL ); + return DD_OK; } + +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + switch (message) + { + case WM_CLOSE: + PostQuitMessage( 0 ); + case WM_CREATE: + case WM_DESTROY: + case WM_KEYDOWN: + return 0; + default: + return DefWindowProc( hWnd, message, wParam, lParam ); + } +} diff --git a/main.h b/main.h index 00a634b..99cf29f 100644 --- a/main.h +++ b/main.h @@ -19,6 +19,8 @@ #include #include "ddraw.h" +#include +#include typedef struct { @@ -60,6 +62,9 @@ typedef struct DWORD height; DWORD bpp; + HINSTANCE hInstance; + HWND hWnd; + } fakeDirectDrawObject; #endif diff --git a/surface.c b/surface.c index 621d95f..8020875 100644 --- a/surface.c +++ b/surface.c @@ -70,6 +70,7 @@ HRESULT ddraw_CreateSurface(void *_This, LPDDSURFACEDESC lpDDSurfaceDesc, LPDIRE Surface->Functions = &siface; /* private stuff */ + Surface->hDC = NULL; Surface->bpp = This->bpp; Surface->surface = NULL; Surface->caps = 0; @@ -78,8 +79,27 @@ HRESULT ddraw_CreateSurface(void *_This, LPDDSURFACEDESC lpDDSurfaceDesc, LPDIRE { if(lpDDSurfaceDesc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) { + PIXELFORMATDESCRIPTOR pfd; + + printf("Creating primary surface and initializing OpenGL\n"); + Surface->width = This->width; Surface->height = This->height; + + Surface->hDC = GetDC(This->hWnd); + + 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( Surface->hDC, ChoosePixelFormat( Surface->hDC, &pfd ), &pfd ); + + Surface->hRC = wglCreateContext( Surface->hDC ); + wglMakeCurrent( Surface->hDC, Surface->hRC ); } dump_ddscaps(lpDDSurfaceDesc->ddsCaps.dwCaps); @@ -183,11 +203,21 @@ HRESULT ddraw_surface_Lock(void *_This, LPRECT lpDestRect, LPDDSURFACEDESC lpDDS return DD_OK; } -HRESULT ddraw_surface_Unlock(void *This, LPVOID lpRect) +HRESULT ddraw_surface_Unlock(void *_This, LPVOID lpRect) { + fakeDirectDrawSurfaceObject *This = (fakeDirectDrawSurfaceObject *)_This; #if _DEBUG printf("DirectDrawSurface::Unlock(This=%p, lpRect=%p)\n", This, lpRect); #endif + + if(This->caps & DDSCAPS_PRIMARYSURFACE) + { + /* clearing the screen for now */ + glClearColor( 0.0f, 0.0f, 255.0f, 0.0f ); + glClear( GL_COLOR_BUFFER_BIT ); + SwapBuffers( This->hDC ); + } + return DD_OK; } diff --git a/surface.h b/surface.h index 5fca809..590d5cb 100644 --- a/surface.h +++ b/surface.h @@ -75,6 +75,9 @@ typedef struct DWORD caps; void *surface; + HDC hDC; + HGLRC hRC; + ULONG Ref; } fakeDirectDrawSurfaceObject;