2018-06-13 19:15:16 +00:00
|
|
|
|
/*
|
|
|
|
|
* glsurface.cpp
|
|
|
|
|
* A 32-bit rendering surface that can quickly blit 8-bit paletted buffers implemented in OpenGL.
|
|
|
|
|
*
|
|
|
|
|
* Copyright <EFBFBD> 2018, Alex Dawson. All rights reserved.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "glsurface.h"
|
|
|
|
|
|
|
|
|
|
#include "baselayer.h"
|
|
|
|
|
#include "build.h"
|
2019-10-05 11:09:15 +00:00
|
|
|
|
#include "tarray.h"
|
2019-09-16 19:08:42 +00:00
|
|
|
|
#include "../../glbackend/glbackend.h"
|
2018-06-13 19:15:16 +00:00
|
|
|
|
|
2019-10-05 11:09:15 +00:00
|
|
|
|
static TArray<uint8_t> buffer;
|
2019-09-18 18:44:21 +00:00
|
|
|
|
static FHardwareTexture* bufferTexture;
|
2018-06-13 19:15:16 +00:00
|
|
|
|
static vec2_t bufferRes;
|
|
|
|
|
|
2019-09-18 18:44:21 +00:00
|
|
|
|
static FHardwareTexture* paletteTexture;
|
2018-06-13 19:15:16 +00:00
|
|
|
|
|
2018-07-14 21:36:44 +00:00
|
|
|
|
bool glsurface_initialize(vec2_t bufferResolution)
|
2018-06-13 19:15:16 +00:00
|
|
|
|
{
|
2019-10-05 11:09:15 +00:00
|
|
|
|
if (buffer.Size())
|
2018-06-13 19:15:16 +00:00
|
|
|
|
glsurface_destroy();
|
|
|
|
|
|
2018-07-14 21:36:44 +00:00
|
|
|
|
bufferRes = bufferResolution;
|
2019-10-05 11:09:15 +00:00
|
|
|
|
buffer.Resize(bufferRes.x * bufferRes.y);
|
2018-06-13 19:15:16 +00:00
|
|
|
|
|
2019-09-18 18:44:21 +00:00
|
|
|
|
bufferTexture = GLInterface.NewTexture();
|
|
|
|
|
bufferTexture->CreateTexture(bufferRes.x, bufferRes.y, true, false);
|
2018-06-13 19:15:16 +00:00
|
|
|
|
|
2018-06-25 14:53:46 +00:00
|
|
|
|
glsurface_setPalette(curpalettefaded);
|
2019-10-05 11:09:15 +00:00
|
|
|
|
GLInterface.SetSurfaceShader();
|
2018-06-13 19:15:16 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void glsurface_destroy()
|
|
|
|
|
{
|
2019-10-05 11:09:15 +00:00
|
|
|
|
if (bufferTexture) delete bufferTexture;
|
2019-09-18 18:44:21 +00:00
|
|
|
|
bufferTexture = nullptr;
|
2019-10-05 11:09:15 +00:00
|
|
|
|
if (paletteTexture) delete paletteTexture;
|
2019-09-18 18:44:21 +00:00
|
|
|
|
paletteTexture = nullptr;
|
2018-06-13 19:15:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 14:53:46 +00:00
|
|
|
|
void glsurface_setPalette(void* pPalette)
|
2018-06-13 19:15:16 +00:00
|
|
|
|
{
|
2019-10-05 11:09:15 +00:00
|
|
|
|
if (!buffer.Size())
|
2018-06-13 19:15:16 +00:00
|
|
|
|
return;
|
|
|
|
|
if (!pPalette)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-09-18 18:44:21 +00:00
|
|
|
|
if (!paletteTexture)
|
|
|
|
|
{
|
|
|
|
|
paletteTexture = GLInterface.NewTexture();
|
|
|
|
|
paletteTexture->CreateTexture(256, 1, false, false);
|
|
|
|
|
}
|
|
|
|
|
paletteTexture->LoadTexture(palette);
|
|
|
|
|
GLInterface.BindTexture(1, paletteTexture, Sampler2DNoFilter);
|
2018-06-13 19:15:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* glsurface_getBuffer()
|
|
|
|
|
{
|
2019-10-05 11:09:15 +00:00
|
|
|
|
return buffer.Data();
|
2018-06-13 19:15:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec2_t glsurface_getBufferResolution()
|
|
|
|
|
{
|
|
|
|
|
return bufferRes;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 14:53:46 +00:00
|
|
|
|
void glsurface_blitBuffer()
|
2018-06-13 19:15:16 +00:00
|
|
|
|
{
|
2019-10-05 11:09:15 +00:00
|
|
|
|
if (!buffer.Size())
|
|
|
|
|
return;
|
2018-06-13 19:15:16 +00:00
|
|
|
|
|
2019-10-05 11:09:15 +00:00
|
|
|
|
bufferTexture->LoadTexture(buffer.Data());
|
2019-09-18 18:44:21 +00:00
|
|
|
|
GLInterface.BindTexture(0, bufferTexture, Sampler2DNoFilter);
|
2018-06-13 19:15:16 +00:00
|
|
|
|
|
2019-09-16 19:08:42 +00:00
|
|
|
|
auto data = GLInterface.AllocVertices(4);
|
|
|
|
|
auto vt = data.second;
|
|
|
|
|
|
|
|
|
|
vt[0].Set(-1.0f, 1.0f, 0.0f, 0.0f, 0.0f); //top-left
|
|
|
|
|
vt[1].Set(-1.0f, -1.0f, 0.0f, 0.0f, 1.0f); //bottom-left
|
|
|
|
|
vt[2].Set(1.0f, 1.0f, 0.0f, 1.0f, 0.0f); //top-right
|
|
|
|
|
vt[3].Set(1.0f, -1.0f, 0.0f, 1.0f, 1.0f); //bottom-right
|
|
|
|
|
GLInterface.Draw(DT_TRIANGLE_STRIP, data.first, 4);
|
2018-06-13 19:15:16 +00:00
|
|
|
|
}
|