2010-11-06 08:28:11 +02:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2010-11-06 21:30:48 +02:00
|
|
|
int maxfps;
|
|
|
|
int width;
|
|
|
|
int height;
|
2010-11-07 09:59:44 +02:00
|
|
|
int bpp;
|
2010-11-06 21:30:48 +02:00
|
|
|
int filter;
|
|
|
|
|
2010-11-06 08:28:11 +02:00
|
|
|
HANDLE thread;
|
|
|
|
BOOL run;
|
|
|
|
HANDLE ev;
|
2010-11-08 21:42:47 +02:00
|
|
|
|
|
|
|
DEVMODE restore;
|
2010-11-06 08:28:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
2010-11-06 21:30:48 +02:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2010-11-07 09:59:44 +02:00
|
|
|
0,
|
2010-11-06 21:30:48 +02:00
|
|
|
|
2010-11-06 08:28:11 +02:00
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
HRESULT WINAPI render_opengl_Initialize()
|
|
|
|
{
|
|
|
|
return DD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WINAPI render_opengl_SetDisplayMode(DWORD width, DWORD height)
|
|
|
|
{
|
2010-11-08 21:42:47 +02:00
|
|
|
DEVMODE mode;
|
|
|
|
|
2010-11-09 17:47:41 +02:00
|
|
|
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &render_opengl.restore);
|
|
|
|
|
2010-11-08 22:00:07 +02:00
|
|
|
if(ddraw->windowed)
|
|
|
|
{
|
|
|
|
return DD_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-08 21:42:47 +02:00
|
|
|
memset(&mode, 0, sizeof(DEVMODE));
|
|
|
|
mode.dmSize = sizeof(DEVMODE);
|
2010-11-09 17:47:41 +02:00
|
|
|
mode.dmFields = DM_PELSWIDTH|DM_PELSHEIGHT;
|
2010-11-08 21:42:47 +02:00
|
|
|
mode.dmPelsWidth = render_opengl.width;
|
|
|
|
mode.dmPelsHeight = render_opengl.height;
|
2010-11-09 17:47:41 +02:00
|
|
|
if(render_opengl.bpp)
|
|
|
|
{
|
|
|
|
mode.dmFields |= DM_BITSPERPEL;
|
|
|
|
mode.dmBitsPerPel = render_opengl.bpp;
|
|
|
|
}
|
2010-11-08 21:42:47 +02:00
|
|
|
|
|
|
|
return ChangeDisplaySettings(&mode, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL ? DD_OK : DDERR_INVALIDMODE;
|
2010-11-06 08:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WINAPI render_opengl_RestoreDisplayMode(void)
|
|
|
|
{
|
2010-11-08 22:00:07 +02:00
|
|
|
if(!ddraw->windowed)
|
|
|
|
{
|
|
|
|
render_opengl.restore.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT|DM_DISPLAYFLAGS|DM_DISPLAYFREQUENCY|DM_POSITION;
|
|
|
|
ChangeDisplaySettings(&render_opengl.restore, 0);
|
|
|
|
}
|
2010-11-06 08:28:11 +02:00
|
|
|
return DD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI render_opengl_main(IDirectDrawSurfaceImpl *surface)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
PIXELFORMATDESCRIPTOR pfd;
|
|
|
|
HDC hDC;
|
|
|
|
HGLRC hRC;
|
|
|
|
|
|
|
|
hDC = GetDC(ddraw->hWnd);
|
|
|
|
|
2010-11-07 09:59:44 +02:00
|
|
|
int tex_width = 1024;
|
|
|
|
int tex_height = 1024;
|
|
|
|
float scale_w = (float)surface->width/tex_width;
|
|
|
|
float scale_h = (float)surface->height/tex_height;
|
|
|
|
int *tex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tex_width * tex_height * sizeof(int));
|
2010-11-06 08:28:11 +02:00
|
|
|
|
|
|
|
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;
|
2010-11-09 17:47:41 +02:00
|
|
|
pfd.cColorBits = render_opengl.bpp ? render_opengl.bpp : render_opengl.restore.dmBitsPerPel;
|
2010-11-06 08:28:11 +02:00
|
|
|
pfd.iLayerType = PFD_MAIN_PLANE;
|
|
|
|
SetPixelFormat( hDC, ChoosePixelFormat( hDC, &pfd ), &pfd );
|
|
|
|
|
|
|
|
hRC = wglCreateContext( hDC );
|
|
|
|
wglMakeCurrent( hDC, hRC );
|
|
|
|
|
|
|
|
DWORD tick_start;
|
|
|
|
DWORD tick_end;
|
2010-11-06 21:30:48 +02:00
|
|
|
DWORD frame_len;
|
|
|
|
|
|
|
|
if(render_opengl.maxfps < 0)
|
|
|
|
{
|
|
|
|
render_opengl.maxfps = ddraw->freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(render_opengl.maxfps > 0)
|
|
|
|
{
|
|
|
|
frame_len = 1000.0f / render_opengl.maxfps;
|
|
|
|
}
|
2010-11-06 08:28:11 +02:00
|
|
|
|
|
|
|
render_opengl.ev = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
|
2010-11-07 09:59:44 +02:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex);
|
|
|
|
glViewport(0, 0, render_opengl.width, render_opengl.height);
|
|
|
|
|
|
|
|
if(render_opengl.filter)
|
|
|
|
{
|
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
|
|
|
|
}
|
|
|
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
|
2010-11-06 08:28:11 +02:00
|
|
|
while(render_opengl.run)
|
|
|
|
{
|
2010-11-07 14:00:02 +02:00
|
|
|
ResetEvent(render_opengl.ev);
|
|
|
|
|
2010-11-06 21:30:48 +02:00
|
|
|
if(render_opengl.maxfps > 0)
|
|
|
|
{
|
|
|
|
tick_start = GetTickCount();
|
|
|
|
}
|
2010-11-06 08:28:11 +02:00
|
|
|
|
|
|
|
/* convert ddraw surface to opengl texture */
|
2010-11-08 17:34:33 +02:00
|
|
|
if(surface->palette)
|
2010-11-06 08:28:11 +02:00
|
|
|
{
|
2010-11-08 17:34:33 +02:00
|
|
|
for(i=0; i<surface->height; i++)
|
2010-11-06 08:28:11 +02:00
|
|
|
{
|
2010-11-08 17:34:33 +02:00
|
|
|
for(j=0; j<surface->width; j++)
|
|
|
|
{
|
|
|
|
tex[i*surface->width+j] = surface->palette->data_bgr[((unsigned char *)surface->surface)[i*surface->lPitch + j*surface->lXPitch]];
|
|
|
|
}
|
2010-11-06 08:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-07 09:59:44 +02:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, surface->width, surface->height, GL_RGBA, GL_UNSIGNED_BYTE, tex);
|
2010-11-06 08:28:11 +02:00
|
|
|
|
|
|
|
glBegin(GL_TRIANGLE_FAN);
|
2010-11-07 09:59:44 +02:00
|
|
|
glTexCoord2f(0,0); glVertex2f(-1, 1);
|
|
|
|
glTexCoord2f(scale_w,0); glVertex2f( 1, 1);
|
|
|
|
glTexCoord2f(scale_w,scale_h); glVertex2f( 1, -1);
|
|
|
|
glTexCoord2f(0,scale_h); glVertex2f(-1, -1);
|
2010-11-06 08:28:11 +02:00
|
|
|
glEnd();
|
|
|
|
|
|
|
|
SwapBuffers(hDC);
|
|
|
|
|
2010-11-06 21:30:48 +02:00
|
|
|
if(render_opengl.maxfps > 0)
|
2010-11-06 08:28:11 +02:00
|
|
|
{
|
2010-11-06 21:30:48 +02:00
|
|
|
tick_end = GetTickCount();
|
|
|
|
|
|
|
|
if(tick_end - tick_start < frame_len)
|
|
|
|
{
|
|
|
|
Sleep( frame_len - (tick_end - tick_start) );
|
|
|
|
}
|
2010-11-06 08:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SetEvent(render_opengl.ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(render_opengl.ev);
|
|
|
|
|
2010-11-07 09:59:44 +02:00
|
|
|
free(tex);
|
2010-11-06 08:28:11 +02:00
|
|
|
|
|
|
|
wglMakeCurrent(NULL, NULL);
|
|
|
|
wglDeleteContext(hRC);
|
|
|
|
ReleaseDC(ddraw->hWnd, hDC);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|