2010-11-06 08:28:11 +02:00
|
|
|
/*
|
2018-05-09 05:09:57 +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.
|
|
|
|
*/
|
2010-11-06 08:28:11 +02:00
|
|
|
|
2013-06-26 11:19:12 +02:00
|
|
|
|
2010-11-06 08:28:11 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
2018-05-09 05:09:57 +02:00
|
|
|
#include "opengl.h"
|
2010-11-06 08:28:11 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "surface.h"
|
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
const GLchar *PaletteVertShaderSrc =
|
|
|
|
"# version 110\n"
|
|
|
|
"varying vec2 TexCoord0; \n"
|
|
|
|
"\n"
|
|
|
|
"void main(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_Position = ftransform(); \n"
|
|
|
|
" TexCoord0 = gl_MultiTexCoord0.xy; \n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
const GLchar *PaletteFragShaderSrc =
|
|
|
|
"//Fragment shader\n"
|
|
|
|
"#version 110\n"
|
|
|
|
"uniform sampler2D PaletteTex; \n"
|
|
|
|
"uniform sampler2D SurfaceTex; \n"
|
|
|
|
"varying vec2 TexCoord0; \n"
|
|
|
|
"\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" vec4 myindex = texture2D(SurfaceTex, TexCoord0); \n"
|
|
|
|
" vec4 texel = texture2D(PaletteTex, myindex.xy); \n"
|
|
|
|
" gl_FragColor = texel;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
|
|
|
|
GLuint SurfaceTexId, PaletteTexId;
|
|
|
|
GLint SurfaceUniLoc, PaletteUniLoc;
|
|
|
|
GLuint PaletteConvProgram;
|
2017-11-16 22:21:13 +01:00
|
|
|
|
2011-03-24 19:41:36 +02:00
|
|
|
BOOL detect_cutscene();
|
|
|
|
|
2010-11-13 10:49:46 +02:00
|
|
|
DWORD WINAPI render_main(void)
|
2010-11-06 08:28:11 +02:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
HGLRC hRC = wglCreateContext(ddraw->render.hDC);
|
|
|
|
wglMakeCurrent(ddraw->render.hDC, hRC);
|
2010-11-06 08:28:11 +02:00
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
OpenGL_Init();
|
2010-11-17 22:12:19 +02:00
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
if (OpenGL_ExtExists("WGL_EXT_swap_control"))
|
2010-11-17 22:12:19 +02:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
BOOL(APIENTRY *wglSwapIntervalEXT)(int) = (BOOL(APIENTRY *)(int))wglGetProcAddress("wglSwapIntervalEXT");
|
|
|
|
if (wglSwapIntervalEXT)
|
2010-11-17 22:12:19 +02:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
if (ddraw->vsync)
|
|
|
|
wglSwapIntervalEXT(1);
|
|
|
|
else
|
|
|
|
wglSwapIntervalEXT(0);
|
2010-11-17 22:12:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-11 18:58:58 +02:00
|
|
|
DWORD tick_start = 0;
|
|
|
|
DWORD tick_end = 0;
|
|
|
|
DWORD frame_len = 0;
|
2010-11-06 21:30:48 +02:00
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
if (ddraw->render.maxfps < 0)
|
2010-11-13 10:49:46 +02:00
|
|
|
ddraw->render.maxfps = ddraw->mode.dmDisplayFrequency;
|
2010-11-06 21:30:48 +02:00
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
if (ddraw->render.maxfps > 0)
|
2010-11-12 19:50:00 +02:00
|
|
|
frame_len = 1000.0f / ddraw->render.maxfps;
|
2010-11-06 08:28:11 +02:00
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
int tex_width = ddraw->width > 1024 ? ddraw->width : 1024;
|
|
|
|
int tex_height = ddraw->height > 1024 ? ddraw->height : 1024;
|
|
|
|
int tex_size = tex_width * tex_height * sizeof(int);
|
|
|
|
int *tex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tex_size);
|
|
|
|
|
|
|
|
if (!PaletteConvProgram)
|
|
|
|
PaletteConvProgram = OpenGL_BuildProgram(&PaletteVertShaderSrc, &PaletteFragShaderSrc);
|
|
|
|
|
|
|
|
// primary surface texture
|
|
|
|
glGenTextures(1, &SurfaceTexId);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, SurfaceTexId);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
|
|
|
|
if (PaletteConvProgram)
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, tex_width, tex_height, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
|
|
|
|
else
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex);
|
|
|
|
|
|
|
|
// palette texture
|
|
|
|
glGenTextures(1, &PaletteTexId);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, PaletteTexId);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
2010-11-07 09:59:44 +02:00
|
|
|
|
2018-03-15 20:49:27 +01:00
|
|
|
glViewport(
|
2018-05-09 05:09:57 +02:00
|
|
|
ddraw->render.viewport.x, ddraw->render.viewport.y,
|
2018-03-15 20:49:27 +01:00
|
|
|
ddraw->render.viewport.width, ddraw->render.viewport.height);
|
2018-05-09 05:09:57 +02:00
|
|
|
|
2017-11-16 22:21:13 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2018-05-09 05:09:57 +02:00
|
|
|
|
|
|
|
if (PaletteConvProgram)
|
2017-11-16 22:21:13 +01:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
glUseProgram(PaletteConvProgram);
|
|
|
|
SurfaceUniLoc = glGetUniformLocation(PaletteConvProgram, "SurfaceTex");
|
|
|
|
PaletteUniLoc = glGetUniformLocation(PaletteConvProgram, "PaletteTex");
|
2017-11-16 22:21:13 +01:00
|
|
|
}
|
2018-05-09 05:09:57 +02:00
|
|
|
|
2010-11-07 09:59:44 +02:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
2018-05-09 05:09:57 +02:00
|
|
|
|
|
|
|
while (ddraw->render.run && WaitForSingleObject(ddraw->render.sem, INFINITE) != WAIT_FAILED)
|
2010-11-06 08:28:11 +02:00
|
|
|
{
|
2017-11-26 08:49:30 +01:00
|
|
|
#if _DEBUG
|
|
|
|
static DWORD tick_fps = 0;
|
|
|
|
static DWORD frame_count = 0;
|
|
|
|
tick_start = timeGetTime();
|
|
|
|
if (tick_start >= tick_fps)
|
|
|
|
{
|
|
|
|
printf("Frames: %lu - Elapsed: %lu ms\n", frame_count, (tick_start - tick_fps) + 1000);
|
|
|
|
frame_count = 0;
|
|
|
|
tick_fps = tick_start + 1000;
|
|
|
|
}
|
|
|
|
frame_count++;
|
|
|
|
#endif
|
2018-05-09 05:09:57 +02:00
|
|
|
|
|
|
|
float scale_w = (float)ddraw->width / tex_width;
|
|
|
|
float scale_h = (float)ddraw->height / tex_height;
|
|
|
|
|
|
|
|
if (ddraw->render.maxfps > 0)
|
2017-11-12 01:27:14 +01:00
|
|
|
tick_start = timeGetTime();
|
2010-11-06 08:28:11 +02:00
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
if (PaletteConvProgram && glActiveTexture && glUniform1i)
|
2017-11-16 22:21:13 +01:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, PaletteTexId);
|
|
|
|
glUniform1i(PaletteUniLoc, 0);
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, SurfaceTexId);
|
|
|
|
glUniform1i(SurfaceUniLoc, 1);
|
2017-11-16 22:21:13 +01:00
|
|
|
}
|
2018-05-09 05:09:57 +02:00
|
|
|
|
2010-11-15 22:25:12 +02:00
|
|
|
EnterCriticalSection(&ddraw->cs);
|
2011-07-09 17:39:01 +03:00
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
if (ddraw->primary && ddraw->primary->palette)
|
2010-11-06 08:28:11 +02:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
if (ddraw->vhack && detect_cutscene())
|
2011-03-24 19:41:36 +02:00
|
|
|
{
|
2011-04-02 21:37:04 +03:00
|
|
|
scale_w *= (float)CUTSCENE_WIDTH / ddraw->width;
|
|
|
|
scale_h *= (float)CUTSCENE_HEIGHT / ddraw->height;
|
2011-03-24 19:42:36 +02:00
|
|
|
|
2018-03-23 03:21:16 +01:00
|
|
|
if (!ddraw->incutscene)
|
|
|
|
ddraw->incutscene = TRUE;
|
2011-03-24 19:42:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-23 03:21:16 +01:00
|
|
|
if (ddraw->incutscene)
|
|
|
|
ddraw->incutscene = FALSE;
|
2011-03-24 19:41:36 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
if (PaletteConvProgram)
|
2017-11-16 22:21:13 +01:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, PaletteTexId);
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 1, GL_RGBA, GL_UNSIGNED_BYTE, ddraw->primary->palette->data_bgr);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, SurfaceTexId);
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, ddraw->width, ddraw->height, GL_RED, GL_UNSIGNED_BYTE, ddraw->primary->surface);
|
2017-11-16 22:21:13 +01:00
|
|
|
}
|
|
|
|
else
|
2010-11-06 08:28:11 +02:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
int i, j;
|
|
|
|
for (i = 0; i<ddraw->height; i++)
|
2010-11-08 17:34:33 +02:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
int i_dst = i*ddraw->width;
|
|
|
|
int i_src = i*ddraw->primary->lPitch;
|
|
|
|
|
|
|
|
for (j = 0; j<ddraw->width; j++)
|
2017-11-16 22:21:13 +01:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
tex[i_dst + j] =
|
|
|
|
ddraw->primary->palette->data_bgr[
|
|
|
|
((unsigned char *)ddraw->primary->surface)[i_src + j*ddraw->primary->lXPitch]];
|
2017-11-16 22:21:13 +01:00
|
|
|
}
|
2010-11-08 17:34:33 +02:00
|
|
|
}
|
2018-05-09 05:09:57 +02:00
|
|
|
|
2010-11-06 08:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-09 05:09:57 +02:00
|
|
|
|
2010-11-15 22:25:12 +02:00
|
|
|
LeaveCriticalSection(&ddraw->cs);
|
2010-11-06 08:28:11 +02:00
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
if (!PaletteConvProgram)
|
2017-11-16 22:21:13 +01:00
|
|
|
{
|
2018-05-09 05:09:57 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, SurfaceTexId);
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, ddraw->width, ddraw->height, GL_RGBA, GL_UNSIGNED_BYTE, tex);
|
2017-11-16 22:21:13 +01:00
|
|
|
}
|
2010-11-06 08:28:11 +02:00
|
|
|
|
|
|
|
glBegin(GL_TRIANGLE_FAN);
|
2018-05-09 05:09:57 +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();
|
2018-05-09 05:09:57 +02:00
|
|
|
|
2017-11-16 22:21:13 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2018-05-09 05:09:57 +02:00
|
|
|
|
2017-11-16 22:21:13 +01:00
|
|
|
SwapBuffers(ddraw->render.hDC);
|
2013-06-26 11:19:12 +02:00
|
|
|
|
2018-05-09 05:09:57 +02:00
|
|
|
if (ddraw->render.maxfps > 0)
|
|
|
|
{
|
2017-11-14 04:46:52 +01:00
|
|
|
tick_end = timeGetTime();
|
2018-05-09 05:09:57 +02:00
|
|
|
|
|
|
|
if (tick_end - tick_start < frame_len)
|
|
|
|
Sleep(frame_len - (tick_end - tick_start));
|
2010-11-06 08:28:11 +02:00
|
|
|
}
|
2018-05-09 05:09:57 +02:00
|
|
|
|
2017-11-17 23:08:13 +01:00
|
|
|
SetEvent(ddraw->render.ev);
|
2010-11-06 08:28:11 +02:00
|
|
|
}
|
2017-11-13 13:50:54 +01:00
|
|
|
|
2010-11-16 17:32:31 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, tex);
|
2018-05-09 05:09:57 +02:00
|
|
|
glDeleteTextures(1, &SurfaceTexId);
|
|
|
|
glDeleteTextures(1, &PaletteTexId);
|
2010-11-06 08:28:11 +02:00
|
|
|
|
|
|
|
wglMakeCurrent(NULL, NULL);
|
|
|
|
wglDeleteContext(hRC);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|