2017-12-14 06:48:08 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
|
|
|
Copyright (C) 2017 Geoffrey McRae <geoff@hostfission.com>
|
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2017-12-05 09:33:05 +00:00
|
|
|
#include "lg-renderer.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2017-12-10 14:31:52 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-12-14 05:09:39 +00:00
|
|
|
#include <SDL2/SDL_ttf.h>
|
2017-12-05 09:33:05 +00:00
|
|
|
|
|
|
|
#define GL_GLEXT_PROTOTYPES
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/glu.h>
|
2017-12-10 16:02:45 +00:00
|
|
|
#include <GL/glx.h>
|
2017-12-05 09:33:05 +00:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "memcpySSE.h"
|
2017-12-10 14:31:52 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2017-12-13 23:30:55 +00:00
|
|
|
#define BUFFER_COUNT 1
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
#define FRAME_TEXTURE 0
|
|
|
|
#define FPS_TEXTURE 1
|
|
|
|
#define MOUSE_TEXTURE 2
|
|
|
|
#define TEXTURE_COUNT 3
|
2017-12-12 15:22:47 +00:00
|
|
|
|
2017-12-10 16:02:45 +00:00
|
|
|
static PFNGLXGETVIDEOSYNCSGIPROC glXGetVideoSyncSGI = NULL;
|
|
|
|
static PFNGLXWAITVIDEOSYNCSGIPROC glXWaitVideoSyncSGI = NULL;
|
|
|
|
|
2017-12-17 11:21:59 +00:00
|
|
|
struct Options
|
|
|
|
{
|
|
|
|
bool mipmap;
|
|
|
|
bool vsync;
|
2017-12-19 00:02:35 +00:00
|
|
|
bool preventBuffer;
|
2017-12-18 23:19:12 +00:00
|
|
|
bool splitMouse;
|
2017-12-17 11:21:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct Options defaultOptions =
|
|
|
|
{
|
2017-12-19 00:02:35 +00:00
|
|
|
.mipmap = true,
|
|
|
|
.vsync = true,
|
|
|
|
.preventBuffer = true,
|
|
|
|
.splitMouse = false
|
2017-12-17 11:21:59 +00:00
|
|
|
};
|
|
|
|
|
2017-12-05 09:33:05 +00:00
|
|
|
struct LGR_OpenGL
|
|
|
|
{
|
2017-12-10 14:31:52 +00:00
|
|
|
LG_RendererParams params;
|
2017-12-17 11:21:59 +00:00
|
|
|
struct Options opt;
|
|
|
|
|
2017-12-14 06:42:16 +00:00
|
|
|
bool configured;
|
2017-12-15 05:21:38 +00:00
|
|
|
SDL_Window * sdlWindow;
|
2017-12-10 14:31:52 +00:00
|
|
|
SDL_GLContext glContext;
|
2017-12-14 06:42:16 +00:00
|
|
|
bool doneInfo;
|
|
|
|
|
|
|
|
SDL_Point window;
|
2017-12-10 14:31:52 +00:00
|
|
|
bool resizeWindow;
|
2017-12-12 15:22:47 +00:00
|
|
|
bool frameUpdate;
|
2017-12-10 14:31:52 +00:00
|
|
|
|
2017-12-05 09:33:05 +00:00
|
|
|
LG_RendererFormat format;
|
|
|
|
GLuint intFormat;
|
|
|
|
GLuint vboFormat;
|
|
|
|
size_t texSize;
|
|
|
|
|
2017-12-10 14:31:52 +00:00
|
|
|
uint64_t drawStart;
|
2017-12-05 09:33:05 +00:00
|
|
|
bool hasBuffers;
|
2017-12-13 19:54:53 +00:00
|
|
|
GLuint vboID[1];
|
|
|
|
uint8_t * texPixels[BUFFER_COUNT];
|
2017-12-05 09:33:05 +00:00
|
|
|
int texIndex;
|
2017-12-10 16:47:07 +00:00
|
|
|
int texList;
|
|
|
|
int fpsList;
|
2017-12-13 02:10:32 +00:00
|
|
|
int mouseList;
|
2017-12-10 16:47:07 +00:00
|
|
|
LG_RendererRect destRect;
|
2017-12-05 09:33:05 +00:00
|
|
|
|
|
|
|
bool hasTextures;
|
2017-12-12 15:22:47 +00:00
|
|
|
GLuint textures[TEXTURE_COUNT];
|
2017-12-10 14:31:52 +00:00
|
|
|
|
2017-12-10 16:02:45 +00:00
|
|
|
uint gpuFrameCount;
|
2017-12-10 14:31:52 +00:00
|
|
|
bool fpsTexture;
|
|
|
|
uint64_t lastFrameTime;
|
|
|
|
uint64_t renderTime;
|
|
|
|
uint64_t frameCount;
|
2017-12-19 01:17:50 +00:00
|
|
|
uint64_t renderCount;
|
2017-12-10 14:31:52 +00:00
|
|
|
SDL_Rect fpsRect;
|
2017-12-12 15:22:47 +00:00
|
|
|
|
2017-12-13 17:18:30 +00:00
|
|
|
bool mouseUpdate;
|
2017-12-15 05:53:26 +00:00
|
|
|
bool newShape;
|
2017-12-13 17:18:30 +00:00
|
|
|
uint64_t lastMouseDraw;
|
2017-12-12 17:49:43 +00:00
|
|
|
LG_RendererCursor mouseType;
|
2017-12-12 15:22:47 +00:00
|
|
|
bool mouseVisible;
|
2017-12-12 16:51:25 +00:00
|
|
|
SDL_Rect mousePos;
|
2017-12-05 09:33:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool lgr_opengl_check_error(const char * name)
|
|
|
|
{
|
|
|
|
GLenum error = glGetError();
|
|
|
|
if (error == GL_NO_ERROR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const GLubyte * errStr = gluErrorString(error);
|
|
|
|
DEBUG_ERROR("%s = %d (%s)", name, error, errStr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * lgr_opengl_get_name()
|
|
|
|
{
|
|
|
|
return "OpenGL";
|
|
|
|
}
|
|
|
|
|
2017-12-17 11:21:59 +00:00
|
|
|
bool lgr_opengl_create(void ** opaque, const LG_RendererParams params)
|
2017-12-05 09:33:05 +00:00
|
|
|
{
|
|
|
|
// create our local storage
|
|
|
|
*opaque = malloc(sizeof(struct LGR_OpenGL));
|
|
|
|
if (!*opaque)
|
|
|
|
{
|
|
|
|
DEBUG_INFO("Failed to allocate %lu bytes", sizeof(struct LGR_OpenGL));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memset(*opaque, 0, sizeof(struct LGR_OpenGL));
|
2017-12-14 06:42:16 +00:00
|
|
|
|
2017-12-05 09:33:05 +00:00
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)*opaque;
|
2017-12-17 11:21:59 +00:00
|
|
|
memcpy(&this->params, ¶ms , sizeof(LG_RendererParams));
|
|
|
|
memcpy(&this->opt , &defaultOptions, sizeof(struct Options ));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lgr_opengl_initialize(void * opaque, Uint32 * sdlFlags)
|
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
|
|
|
if (!this)
|
|
|
|
return false;
|
2017-12-10 14:31:52 +00:00
|
|
|
|
2017-12-10 16:02:45 +00:00
|
|
|
if (!glXGetVideoSyncSGI)
|
2017-12-10 14:31:52 +00:00
|
|
|
{
|
2017-12-10 16:02:45 +00:00
|
|
|
glXGetVideoSyncSGI = (PFNGLXGETVIDEOSYNCSGIPROC )glXGetProcAddress((const GLubyte *)"glXGetVideoSyncSGI" );
|
|
|
|
glXWaitVideoSyncSGI = (PFNGLXWAITVIDEOSYNCSGIPROC)glXGetProcAddress((const GLubyte *)"glXWaitVideoSyncSGI");
|
|
|
|
|
2017-12-10 20:14:43 +00:00
|
|
|
if (!glXGetVideoSyncSGI || !glXWaitVideoSyncSGI)
|
2017-12-10 16:02:45 +00:00
|
|
|
{
|
|
|
|
glXGetVideoSyncSGI = NULL;
|
|
|
|
DEBUG_ERROR("Failed to get proc addresses");
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-10 14:31:52 +00:00
|
|
|
}
|
2017-12-10 16:02:45 +00:00
|
|
|
|
2017-12-14 06:42:16 +00:00
|
|
|
*sdlFlags = SDL_WINDOW_OPENGL;
|
2017-12-15 05:21:38 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
2017-12-14 06:42:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lgr_opengl_configure(void * opaque, SDL_Window *window, const LG_RendererFormat format)
|
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
|
|
|
if (!this)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (this->configured)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Renderer already configured, call deconfigure first");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-15 05:21:38 +00:00
|
|
|
this->sdlWindow = window;
|
2017-12-14 06:42:16 +00:00
|
|
|
this->glContext = SDL_GL_CreateContext(window);
|
|
|
|
if (!this->glContext)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create the OpenGL context");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->doneInfo)
|
|
|
|
{
|
|
|
|
DEBUG_INFO("Vendor : %s", glGetString(GL_VENDOR ));
|
|
|
|
DEBUG_INFO("Renderer: %s", glGetString(GL_RENDERER));
|
|
|
|
DEBUG_INFO("Version : %s", glGetString(GL_VERSION ));
|
|
|
|
this->doneInfo = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_GL_MakeCurrent(window, this->glContext) != 0)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to make the GL context current");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-17 12:24:58 +00:00
|
|
|
SDL_GL_SetSwapInterval(this->opt.vsync ? 1 : 0);
|
2017-12-10 14:31:52 +00:00
|
|
|
|
|
|
|
// check if the GPU supports GL_ARB_buffer_storage first
|
|
|
|
// there is no advantage to this renderer if it is not present.
|
|
|
|
const GLubyte * extensions = glGetString(GL_EXTENSIONS);
|
|
|
|
if (!gluCheckExtension((const GLubyte *)"GL_ARB_buffer_storage", extensions))
|
|
|
|
{
|
|
|
|
DEBUG_INFO("The GPU doesn't support GL_ARB_buffer_storage");
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-05 09:33:05 +00:00
|
|
|
|
|
|
|
// assume 24 and 32 bit formats are RGB and RGBA
|
|
|
|
switch(format.bpp)
|
|
|
|
{
|
|
|
|
case 24:
|
|
|
|
this->intFormat = GL_RGB8;
|
|
|
|
this->vboFormat = GL_BGR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
this->intFormat = GL_RGBA8;
|
|
|
|
this->vboFormat = GL_BGRA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_INFO("%d bpp not supported", format.bpp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate the texture size in bytes
|
|
|
|
this->texSize = format.height * format.pitch;
|
|
|
|
|
2017-12-10 16:47:07 +00:00
|
|
|
// generate lists for drawing
|
2017-12-13 19:54:53 +00:00
|
|
|
this->texList = glGenLists(BUFFER_COUNT);
|
2017-12-13 02:10:32 +00:00
|
|
|
this->fpsList = glGenLists(1);
|
|
|
|
this->mouseList = glGenLists(1);
|
2017-12-10 16:47:07 +00:00
|
|
|
|
2017-12-05 09:33:05 +00:00
|
|
|
// generate the pixel unpack buffers
|
2017-12-13 19:54:53 +00:00
|
|
|
glGenBuffers(1, this->vboID);
|
2017-12-05 09:33:05 +00:00
|
|
|
if (lgr_opengl_check_error("glGenBuffers"))
|
|
|
|
return false;
|
|
|
|
this->hasBuffers = true;
|
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vboID[0]);
|
|
|
|
if (lgr_opengl_check_error("glBindBuffer"))
|
|
|
|
return false;
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
glBufferStorage(
|
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
this->texSize * BUFFER_COUNT,
|
|
|
|
NULL,
|
|
|
|
GL_MAP_WRITE_BIT |
|
2017-12-13 23:06:22 +00:00
|
|
|
GL_MAP_PERSISTENT_BIT
|
2017-12-13 19:54:53 +00:00
|
|
|
);
|
|
|
|
if (lgr_opengl_check_error("glBufferStorage"))
|
|
|
|
return false;
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
this->texPixels[0] = glMapBufferRange(
|
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
0,
|
|
|
|
this->texSize * BUFFER_COUNT,
|
|
|
|
GL_MAP_WRITE_BIT |
|
|
|
|
GL_MAP_PERSISTENT_BIT |
|
2017-12-13 23:06:22 +00:00
|
|
|
GL_MAP_FLUSH_EXPLICIT_BIT
|
2017-12-13 19:54:53 +00:00
|
|
|
);
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
if (lgr_opengl_check_error("glMapBufferRange"))
|
|
|
|
return false;
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
for(int i = 1; i < BUFFER_COUNT; ++i)
|
|
|
|
this->texPixels[i] = this->texPixels[i-1] + this->texSize;
|
2017-12-05 09:33:05 +00:00
|
|
|
|
|
|
|
// create the textures
|
2017-12-12 15:22:47 +00:00
|
|
|
glGenTextures(TEXTURE_COUNT, this->textures);
|
2017-12-05 09:33:05 +00:00
|
|
|
if (lgr_opengl_check_error("glGenTextures"))
|
|
|
|
return false;
|
|
|
|
this->hasTextures = true;
|
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
// create the frame texture
|
|
|
|
glBindTexture(GL_TEXTURE_2D, this->textures[FRAME_TEXTURE]);
|
|
|
|
if (lgr_opengl_check_error("glBindTexture"))
|
|
|
|
return false;
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
glTexImage2D(
|
|
|
|
GL_TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
this->intFormat,
|
|
|
|
format.width,
|
|
|
|
format.height * BUFFER_COUNT,
|
|
|
|
0,
|
|
|
|
this->vboFormat,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
(void*)0
|
|
|
|
);
|
|
|
|
if (lgr_opengl_check_error("glTexImage2D"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// configure the texture
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
for (int i = 0; i < BUFFER_COUNT; ++i)
|
|
|
|
{
|
|
|
|
const float ts = (1.0f / BUFFER_COUNT) * i;
|
|
|
|
const float te = (1.0f / BUFFER_COUNT) + ts;
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-13 02:10:32 +00:00
|
|
|
// create the display lists
|
|
|
|
glNewList(this->texList + i, GL_COMPILE);
|
2017-12-13 19:54:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, this->textures[FRAME_TEXTURE]);
|
2017-12-13 02:10:32 +00:00
|
|
|
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
glBegin(GL_TRIANGLE_STRIP);
|
2017-12-13 19:54:53 +00:00
|
|
|
glTexCoord2f(0.0f, ts); glVertex2i(0 , 0 );
|
|
|
|
glTexCoord2f(1.0f, ts); glVertex2i(format.width, 0 );
|
|
|
|
glTexCoord2f(0.0f, te); glVertex2i(0 , format.height);
|
|
|
|
glTexCoord2f(1.0f, te); glVertex2i(format.width, format.height);
|
2017-12-13 02:10:32 +00:00
|
|
|
glEnd();
|
|
|
|
glEndList();
|
2017-12-05 09:33:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 15:22:47 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2017-12-13 19:54:53 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2017-12-12 15:22:47 +00:00
|
|
|
|
2017-12-05 09:33:05 +00:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
2017-12-10 14:31:52 +00:00
|
|
|
glEnable(GL_COLOR_MATERIAL);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glBlendEquation(GL_FUNC_ADD);
|
2017-12-12 15:22:47 +00:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2017-12-10 14:31:52 +00:00
|
|
|
|
|
|
|
this->resizeWindow = true;
|
|
|
|
this->drawStart = nanotime();
|
2017-12-10 20:14:43 +00:00
|
|
|
glXGetVideoSyncSGI(&this->gpuFrameCount);
|
2017-12-05 09:33:05 +00:00
|
|
|
|
|
|
|
// copy the format into the local storage
|
|
|
|
memcpy(&this->format, &format, sizeof(LG_RendererFormat));
|
2017-12-14 06:42:16 +00:00
|
|
|
this->configured = true;
|
2017-12-05 09:33:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-14 06:42:16 +00:00
|
|
|
void lgr_opengl_deconfigure(void * opaque)
|
2017-12-05 09:33:05 +00:00
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
2017-12-14 06:42:16 +00:00
|
|
|
if (!this || !this->configured)
|
2017-12-05 09:33:05 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (this->hasTextures)
|
2017-12-14 06:42:16 +00:00
|
|
|
{
|
2017-12-13 19:54:53 +00:00
|
|
|
glDeleteTextures(TEXTURE_COUNT, this->textures);
|
2017-12-14 06:42:16 +00:00
|
|
|
this->hasTextures = false;
|
|
|
|
}
|
2017-12-05 09:33:05 +00:00
|
|
|
|
|
|
|
if (this->hasBuffers)
|
2017-12-14 06:42:16 +00:00
|
|
|
{
|
2017-12-13 19:54:53 +00:00
|
|
|
glDeleteBuffers(1, this->vboID);
|
2017-12-14 06:42:16 +00:00
|
|
|
this->hasBuffers = false;
|
|
|
|
}
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-10 14:31:52 +00:00
|
|
|
if (this->glContext)
|
2017-12-14 06:42:16 +00:00
|
|
|
{
|
2017-12-10 14:31:52 +00:00
|
|
|
SDL_GL_DeleteContext(this->glContext);
|
2017-12-14 06:42:16 +00:00
|
|
|
this->glContext = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->configured = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lgr_opengl_deinitialize(void * opaque)
|
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
|
|
|
if (!this)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (this->configured)
|
|
|
|
lgr_opengl_deconfigure(opaque);
|
2017-12-10 14:31:52 +00:00
|
|
|
|
2017-12-05 09:33:05 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lgr_opengl_is_compatible(void * opaque, const LG_RendererFormat format)
|
|
|
|
{
|
|
|
|
const struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
2017-12-14 06:42:16 +00:00
|
|
|
if (!this || !this->configured)
|
2017-12-05 09:33:05 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return (memcmp(&this->format, &format, sizeof(LG_RendererFormat)) == 0);
|
|
|
|
}
|
|
|
|
|
2017-12-10 16:47:07 +00:00
|
|
|
void lgr_opengl_on_resize(void * opaque, const int width, const int height, const LG_RendererRect destRect)
|
2017-12-10 14:31:52 +00:00
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
2017-12-14 06:42:16 +00:00
|
|
|
if (!this || !this->configured)
|
2017-12-10 14:31:52 +00:00
|
|
|
return;
|
|
|
|
|
2017-12-16 00:25:01 +00:00
|
|
|
this->window.x = width;
|
2017-12-14 06:42:16 +00:00
|
|
|
this->window.y = height;
|
2017-12-10 16:47:07 +00:00
|
|
|
memcpy(&this->destRect, &destRect, sizeof(LG_RendererRect));
|
|
|
|
|
2017-12-13 02:10:32 +00:00
|
|
|
this->resizeWindow = true;
|
2017-12-10 14:31:52 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:42:09 +00:00
|
|
|
bool lgr_opengl_on_mouse_shape(void * opaque, const LG_RendererCursor cursor, const int width, const int height, const int pitch, const uint8_t * data)
|
2017-12-12 16:08:13 +00:00
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
2017-12-14 06:42:16 +00:00
|
|
|
if (!this || !this->configured)
|
2017-12-12 16:08:13 +00:00
|
|
|
return false;
|
|
|
|
|
2017-12-12 17:49:43 +00:00
|
|
|
this->mouseType = cursor;
|
2017-12-12 16:51:25 +00:00
|
|
|
switch(cursor)
|
|
|
|
{
|
|
|
|
case LG_CURSOR_COLOR:
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, this->textures[MOUSE_TEXTURE]);
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT , 4 );
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
|
|
|
|
glTexImage2D
|
|
|
|
(
|
|
|
|
GL_TEXTURE_2D,
|
|
|
|
0 ,
|
|
|
|
GL_RGBA,
|
|
|
|
width ,
|
|
|
|
height ,
|
|
|
|
0 ,
|
2017-12-14 23:20:20 +00:00
|
|
|
GL_BGRA, // windows cursors are in BGRA format
|
2017-12-12 16:51:25 +00:00
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
data
|
|
|
|
);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
this->mousePos.w = width;
|
|
|
|
this->mousePos.h = height;
|
2017-12-13 02:10:32 +00:00
|
|
|
|
|
|
|
glNewList(this->mouseList, GL_COMPILE);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, this->textures[MOUSE_TEXTURE]);
|
|
|
|
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
glBegin(GL_TRIANGLE_STRIP);
|
|
|
|
glTexCoord2f(0.0f, 0.0f); glVertex2i(0 , 0 );
|
|
|
|
glTexCoord2f(1.0f, 0.0f); glVertex2i(width, 0 );
|
|
|
|
glTexCoord2f(0.0f, 1.0f); glVertex2i(0 , height);
|
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex2i(width, height);
|
|
|
|
glEnd();
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glEndList();
|
2017-12-12 16:51:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LG_CURSOR_MONOCHROME:
|
|
|
|
{
|
2017-12-13 02:10:32 +00:00
|
|
|
const int hheight = height / 2;
|
2017-12-12 17:49:43 +00:00
|
|
|
uint32_t d[width * height];
|
2017-12-13 02:10:32 +00:00
|
|
|
for(int y = 0; y < hheight; ++y)
|
2017-12-12 17:49:43 +00:00
|
|
|
for(int x = 0; x < width; ++x)
|
|
|
|
{
|
|
|
|
const uint8_t * srcAnd = data + (pitch * y) + (x / 8);
|
2017-12-13 02:10:32 +00:00
|
|
|
const uint8_t * srcXor = srcAnd + pitch * hheight;
|
2017-12-12 17:49:43 +00:00
|
|
|
const uint8_t mask = 0x80 >> (x % 8);
|
|
|
|
const uint32_t andMask = (*srcAnd & mask) ? 0xFFFFFFFF : 0xFF000000;
|
|
|
|
const uint32_t xorMask = (*srcXor & mask) ? 0x00FFFFFF : 0x00000000;
|
|
|
|
|
2017-12-13 02:10:32 +00:00
|
|
|
d[y * width + x ] = andMask;
|
|
|
|
d[y * width + x + width * hheight] = xorMask;
|
2017-12-12 17:49:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, this->textures[MOUSE_TEXTURE]);
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT , 4 );
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
|
|
|
|
glTexImage2D
|
|
|
|
(
|
|
|
|
GL_TEXTURE_2D,
|
|
|
|
0 ,
|
|
|
|
GL_RGBA,
|
|
|
|
width ,
|
|
|
|
height ,
|
|
|
|
0 ,
|
|
|
|
GL_RGBA,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
d
|
|
|
|
);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
this->mousePos.w = width;
|
2017-12-13 02:10:32 +00:00
|
|
|
this->mousePos.h = hheight;
|
|
|
|
|
|
|
|
glNewList(this->mouseList, GL_COMPILE);
|
|
|
|
glEnable(GL_COLOR_LOGIC_OP);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, this->textures[MOUSE_TEXTURE]);
|
|
|
|
glLogicOp(GL_AND);
|
|
|
|
glBegin(GL_TRIANGLE_STRIP);
|
2017-12-13 09:44:58 +00:00
|
|
|
glTexCoord2f(0.0f, 0.0f); glVertex2i(0 , 0 );
|
|
|
|
glTexCoord2f(1.0f, 0.0f); glVertex2i(width, 0 );
|
2017-12-13 02:10:32 +00:00
|
|
|
glTexCoord2f(0.0f, 0.5f); glVertex2i(0 , hheight);
|
|
|
|
glTexCoord2f(1.0f, 0.5f); glVertex2i(width, hheight);
|
|
|
|
glEnd();
|
|
|
|
glLogicOp(GL_XOR);
|
|
|
|
glBegin(GL_TRIANGLE_STRIP);
|
2017-12-13 09:44:58 +00:00
|
|
|
glTexCoord2f(0.0f, 0.5f); glVertex2i(0 , 0 );
|
|
|
|
glTexCoord2f(1.0f, 0.5f); glVertex2i(width, 0 );
|
2017-12-13 02:10:32 +00:00
|
|
|
glTexCoord2f(0.0f, 1.0f); glVertex2i(0 , hheight);
|
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex2i(width, hheight);
|
|
|
|
glEnd();
|
|
|
|
glDisable(GL_COLOR_LOGIC_OP);
|
|
|
|
glEndList();
|
2017-12-12 16:51:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LG_CURSOR_MASKED_COLOR:
|
|
|
|
{
|
2017-12-13 02:10:32 +00:00
|
|
|
//TODO
|
2017-12-12 16:51:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 17:18:30 +00:00
|
|
|
this->mouseUpdate = true;
|
2017-12-15 05:53:26 +00:00
|
|
|
this->newShape = true;
|
2017-12-12 16:08:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-12 15:22:47 +00:00
|
|
|
bool lgr_opengl_on_mouse_event(void * opaque, const bool visible, const int x, const int y)
|
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
2017-12-14 06:42:16 +00:00
|
|
|
if (!this || !this->configured)
|
2017-12-12 15:22:47 +00:00
|
|
|
return false;
|
|
|
|
|
2017-12-13 03:10:24 +00:00
|
|
|
if (this->mousePos.x == x && this->mousePos.y == y && this->mouseVisible == visible)
|
2017-12-12 15:22:47 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
this->mouseVisible = visible;
|
|
|
|
this->mousePos.x = x;
|
|
|
|
this->mousePos.y = y;
|
2017-12-13 17:18:30 +00:00
|
|
|
this->mouseUpdate = true;
|
2017-12-12 15:22:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:06:22 +00:00
|
|
|
bool lgr_opengl_on_frame_event(void * opaque, const uint8_t * data)
|
2017-12-05 09:33:05 +00:00
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
2017-12-14 06:42:16 +00:00
|
|
|
if (!this)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Invalid opaque pointer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->configured)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Not configured");
|
2017-12-05 09:33:05 +00:00
|
|
|
return false;
|
2017-12-14 06:42:16 +00:00
|
|
|
}
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-10 16:02:45 +00:00
|
|
|
if (this->params.showFPS && this->renderTime > 1e9)
|
|
|
|
{
|
|
|
|
char str[128];
|
2017-12-19 01:17:50 +00:00
|
|
|
const float avgFPS = 1000.0f / (((float)this->renderTime / this->frameCount ) / 1e6f);
|
|
|
|
const float renderFPS = 1000.0f / (((float)this->renderTime / this->renderCount) / 1e6f);
|
|
|
|
snprintf(str, sizeof(str), "UPS: %8.4f, FPS: %8.4f", avgFPS, renderFPS);
|
2017-12-10 16:02:45 +00:00
|
|
|
SDL_Color color = {0xff, 0xff, 0xff};
|
|
|
|
SDL_Surface *textSurface = NULL;
|
|
|
|
if (!(textSurface = TTF_RenderText_Blended(this->params.font, str, color)))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to render text");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D , this->textures[FPS_TEXTURE]);
|
2017-12-12 15:22:47 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT , 4 );
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, textSurface->w );
|
2017-12-10 16:02:45 +00:00
|
|
|
glTexImage2D(
|
|
|
|
GL_TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
textSurface->format->BytesPerPixel,
|
|
|
|
textSurface->w,
|
|
|
|
textSurface->h,
|
|
|
|
0,
|
|
|
|
GL_BGRA,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
textSurface->pixels
|
|
|
|
);
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
this->fpsRect.x = 5;
|
|
|
|
this->fpsRect.y = 5;
|
|
|
|
this->fpsRect.w = textSurface->w;
|
|
|
|
this->fpsRect.h = textSurface->h;
|
|
|
|
|
|
|
|
SDL_FreeSurface(textSurface);
|
|
|
|
|
2017-12-19 01:17:50 +00:00
|
|
|
this->renderTime = 0;
|
|
|
|
this->frameCount = 0;
|
|
|
|
this->renderCount = 0;
|
|
|
|
this->fpsTexture = true;
|
2017-12-10 16:47:07 +00:00
|
|
|
|
|
|
|
glNewList(this->fpsList, GL_COMPILE);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
|
|
|
|
glBegin(GL_TRIANGLE_STRIP);
|
|
|
|
glVertex2i(this->fpsRect.x , this->fpsRect.y );
|
|
|
|
glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y );
|
|
|
|
glVertex2i(this->fpsRect.x , this->fpsRect.y + this->fpsRect.h);
|
|
|
|
glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y + this->fpsRect.h);
|
|
|
|
glEnd();
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, this->textures[FPS_TEXTURE]);
|
2017-12-10 16:47:07 +00:00
|
|
|
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
glBegin(GL_TRIANGLE_STRIP);
|
|
|
|
glTexCoord2f(0.0f , 0.0f); glVertex2i(this->fpsRect.x , this->fpsRect.y );
|
|
|
|
glTexCoord2f(1.0f , 0.0f); glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y );
|
|
|
|
glTexCoord2f(0.0f , 1.0f); glVertex2i(this->fpsRect.x , this->fpsRect.y + this->fpsRect.h);
|
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y + this->fpsRect.h);
|
|
|
|
glEnd();
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glEndList();
|
2017-12-10 16:02:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 09:33:05 +00:00
|
|
|
// bind the texture and update it
|
2017-12-13 19:54:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D , this->textures[FRAME_TEXTURE]);
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vboID[0] );
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT , 4 );
|
2017-12-14 09:35:36 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH , this->format.stride );
|
2017-12-10 16:02:45 +00:00
|
|
|
|
2017-12-13 23:06:22 +00:00
|
|
|
// copy the buffer to the texture
|
|
|
|
memcpySSE(this->texPixels[this->texIndex], data, this->texSize);
|
|
|
|
glFlushMappedBufferRange(
|
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
this->texSize * this->texIndex,
|
|
|
|
this->texSize
|
|
|
|
);
|
|
|
|
|
2017-12-10 16:02:45 +00:00
|
|
|
// update the texture
|
2017-12-05 09:33:05 +00:00
|
|
|
glTexSubImage2D(
|
|
|
|
GL_TEXTURE_2D,
|
|
|
|
0,
|
2017-12-13 19:54:53 +00:00
|
|
|
0,
|
2017-12-13 23:06:22 +00:00
|
|
|
this->texIndex * this->format.height,
|
2017-12-10 16:02:45 +00:00
|
|
|
this->format.width ,
|
2017-12-05 09:33:05 +00:00
|
|
|
this->format.height,
|
|
|
|
this->vboFormat,
|
|
|
|
GL_UNSIGNED_BYTE,
|
2017-12-13 23:06:22 +00:00
|
|
|
(void*)(this->texIndex * this->texSize)
|
2017-12-05 09:33:05 +00:00
|
|
|
);
|
2017-12-13 19:54:53 +00:00
|
|
|
lgr_opengl_check_error("glTexSubImage2D");
|
|
|
|
|
|
|
|
// unbind the buffer
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-17 11:21:59 +00:00
|
|
|
const bool mipmap = this->opt.mipmap && (
|
2017-12-10 16:47:07 +00:00
|
|
|
(this->format.width > this->destRect.w) ||
|
|
|
|
(this->format.height > this->destRect.h));
|
2017-12-05 09:33:05 +00:00
|
|
|
|
|
|
|
if (mipmap)
|
2017-12-16 00:25:01 +00:00
|
|
|
{
|
2017-12-05 09:33:05 +00:00
|
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
2017-12-16 00:25:01 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
}
|
2017-12-13 19:54:53 +00:00
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-13 23:06:22 +00:00
|
|
|
if (++this->texIndex == BUFFER_COUNT)
|
|
|
|
this->texIndex = 0;
|
|
|
|
|
2017-12-19 01:17:50 +00:00
|
|
|
++this->frameCount;
|
2017-12-12 15:22:47 +00:00
|
|
|
this->frameUpdate = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-13 21:23:58 +00:00
|
|
|
static inline void lgr_opengl_draw_mouse(struct LGR_OpenGL * this)
|
2017-12-12 15:22:47 +00:00
|
|
|
{
|
|
|
|
if (!this->mouseVisible)
|
|
|
|
return;
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-12 15:22:47 +00:00
|
|
|
glPushMatrix();
|
2017-12-13 19:54:53 +00:00
|
|
|
glTranslatef(this->mousePos.x, this->mousePos.y, 0.0f);
|
2017-12-13 02:10:32 +00:00
|
|
|
glCallList(this->mouseList);
|
2017-12-12 15:22:47 +00:00
|
|
|
glPopMatrix();
|
|
|
|
}
|
2017-12-10 17:31:35 +00:00
|
|
|
|
2017-12-12 15:22:47 +00:00
|
|
|
bool lgr_opengl_render(void * opaque)
|
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
2017-12-14 06:42:16 +00:00
|
|
|
if (!this || !this->configured)
|
2017-12-12 15:22:47 +00:00
|
|
|
return false;
|
2017-12-10 16:47:07 +00:00
|
|
|
|
2017-12-13 02:10:32 +00:00
|
|
|
if (this->resizeWindow)
|
2017-12-12 15:22:47 +00:00
|
|
|
{
|
2017-12-13 02:10:32 +00:00
|
|
|
// setup the projection matrix
|
2017-12-14 06:42:16 +00:00
|
|
|
glViewport(0, 0, this->window.x, this->window.y);
|
2017-12-13 02:10:32 +00:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2017-12-14 06:42:16 +00:00
|
|
|
gluOrtho2D(0, this->window.x, this->window.y, 0);
|
2017-12-12 15:28:41 +00:00
|
|
|
|
2017-12-13 02:10:32 +00:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
glTranslatef(this->destRect.x, this->destRect.y, 0.0f);
|
|
|
|
glScalef(
|
|
|
|
(float)this->destRect.w / (float)this->format.width,
|
|
|
|
(float)this->destRect.h / (float)this->format.height,
|
|
|
|
1.0f
|
|
|
|
);
|
2017-12-12 15:28:41 +00:00
|
|
|
|
2017-12-13 02:10:32 +00:00
|
|
|
// update the scissor rect to prevent drawing outside of the frame
|
|
|
|
glScissor(
|
|
|
|
this->destRect.x,
|
|
|
|
this->destRect.y,
|
|
|
|
this->destRect.w,
|
|
|
|
this->destRect.h
|
|
|
|
);
|
|
|
|
|
|
|
|
this->resizeWindow = false;
|
|
|
|
}
|
2017-12-12 15:22:47 +00:00
|
|
|
|
2017-12-18 23:19:12 +00:00
|
|
|
if (this->opt.splitMouse)
|
2017-12-13 02:10:32 +00:00
|
|
|
{
|
2017-12-18 23:19:12 +00:00
|
|
|
if (!this->frameUpdate)
|
2017-12-15 05:53:26 +00:00
|
|
|
{
|
2017-12-18 23:19:12 +00:00
|
|
|
if (!this->mouseUpdate)
|
2017-12-15 05:53:26 +00:00
|
|
|
return true;
|
2017-12-18 23:19:12 +00:00
|
|
|
|
|
|
|
if (!this->newShape)
|
|
|
|
{
|
|
|
|
// don't update the mouse too fast
|
|
|
|
const uint64_t delta = nanotime() - this->lastMouseDraw;
|
|
|
|
if (delta < 5e6)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
this->newShape = false;
|
|
|
|
|
|
|
|
glDrawBuffer(GL_FRONT);
|
|
|
|
glCallList(this->texList + this->texIndex);
|
|
|
|
lgr_opengl_draw_mouse(this);
|
|
|
|
if (this->fpsTexture)
|
|
|
|
glCallList(this->fpsList);
|
|
|
|
glDrawBuffer(GL_BACK);
|
|
|
|
glFlush();
|
|
|
|
|
|
|
|
this->mouseUpdate = false;
|
|
|
|
this->lastMouseDraw = nanotime();
|
|
|
|
return true;
|
2017-12-15 05:53:26 +00:00
|
|
|
}
|
2017-12-13 23:06:22 +00:00
|
|
|
}
|
2017-12-12 15:22:47 +00:00
|
|
|
|
2017-12-13 23:06:22 +00:00
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2017-12-13 17:18:30 +00:00
|
|
|
|
2017-12-13 19:54:53 +00:00
|
|
|
glCallList(this->texList + this->texIndex);
|
|
|
|
lgr_opengl_draw_mouse(this);
|
|
|
|
if (this->fpsTexture)
|
|
|
|
glCallList(this->fpsList);
|
2017-12-15 05:21:38 +00:00
|
|
|
|
2017-12-19 00:02:35 +00:00
|
|
|
if (this->opt.preventBuffer)
|
|
|
|
{
|
|
|
|
unsigned int before, after;
|
|
|
|
glXGetVideoSyncSGI(&before);
|
|
|
|
SDL_GL_SwapWindow(this->sdlWindow);
|
|
|
|
|
|
|
|
// wait for the swap to happen to ensure we dont buffer frames //
|
|
|
|
glXGetVideoSyncSGI(&after);
|
|
|
|
if (before == after)
|
|
|
|
glXWaitVideoSyncSGI(1, 0, &before);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SDL_GL_SwapWindow(this->sdlWindow);
|
2017-12-10 14:31:52 +00:00
|
|
|
|
2017-12-13 03:10:24 +00:00
|
|
|
const uint64_t t = nanotime();
|
|
|
|
this->renderTime += t - this->lastFrameTime;
|
|
|
|
this->lastFrameTime = t;
|
2017-12-19 01:17:50 +00:00
|
|
|
++this->renderCount;
|
2017-12-05 09:33:05 +00:00
|
|
|
|
2017-12-13 17:18:30 +00:00
|
|
|
this->frameUpdate = false;
|
|
|
|
this->mouseUpdate = false;
|
|
|
|
this->lastMouseDraw = t;
|
2017-12-05 09:33:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-17 11:21:59 +00:00
|
|
|
static void handle_opt_mipmap(void * opaque, const char *value)
|
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
|
|
|
if (!this)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->opt.mipmap = LG_RendererValueToBool(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_opt_vsync(void * opaque, const char *value)
|
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
|
|
|
if (!this)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->opt.vsync = LG_RendererValueToBool(value);
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:02:35 +00:00
|
|
|
static void handle_opt_prevent_buffer(void * opaque, const char *value)
|
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
|
|
|
if (!this)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->opt.preventBuffer = LG_RendererValueToBool(value);
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:19:12 +00:00
|
|
|
static void handle_opt_split_mouse(void * opaque, const char *value)
|
|
|
|
{
|
|
|
|
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
|
|
|
if (!this)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->opt.splitMouse = LG_RendererValueToBool(value);
|
|
|
|
}
|
|
|
|
|
2017-12-17 11:21:59 +00:00
|
|
|
static LG_RendererOpt lgr_opengl_options[] =
|
|
|
|
{
|
2017-12-17 11:33:02 +00:00
|
|
|
{
|
|
|
|
.name = "mipmap",
|
|
|
|
.desc = "Enable or disable mipmapping [default: enabled]",
|
|
|
|
.validator = LG_RendererValidatorBool,
|
|
|
|
.handler = handle_opt_mipmap
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "vsync",
|
|
|
|
.desc ="Enable or disable vsync [default: enabled]",
|
|
|
|
.validator = LG_RendererValidatorBool,
|
|
|
|
.handler = handle_opt_vsync
|
2017-12-18 23:19:12 +00:00
|
|
|
},
|
2017-12-19 00:02:35 +00:00
|
|
|
{
|
|
|
|
.name = "preventBuffer",
|
|
|
|
.desc = "Prevent the driver from buffering frames [default: enabled]",
|
|
|
|
.validator = LG_RendererValidatorBool,
|
|
|
|
.handler = handle_opt_prevent_buffer
|
|
|
|
},
|
2017-12-18 23:19:12 +00:00
|
|
|
{
|
|
|
|
.name = "splitMouse",
|
|
|
|
.desc = "Draw mouse updates directly to the front buffer [default: disabled]",
|
|
|
|
.validator = LG_RendererValidatorBool,
|
|
|
|
.handler = handle_opt_split_mouse
|
2017-12-17 11:33:02 +00:00
|
|
|
}
|
2017-12-17 11:21:59 +00:00
|
|
|
};
|
|
|
|
|
2017-12-05 09:33:05 +00:00
|
|
|
const LG_Renderer LGR_OpenGL =
|
|
|
|
{
|
2017-12-12 15:22:47 +00:00
|
|
|
.get_name = lgr_opengl_get_name,
|
2017-12-17 11:21:59 +00:00
|
|
|
.options = lgr_opengl_options,
|
|
|
|
.option_count = LGR_OPTION_COUNT(lgr_opengl_options),
|
|
|
|
.create = lgr_opengl_create,
|
2017-12-12 15:22:47 +00:00
|
|
|
.initialize = lgr_opengl_initialize,
|
2017-12-14 06:42:16 +00:00
|
|
|
.configure = lgr_opengl_configure,
|
|
|
|
.deconfigure = lgr_opengl_deconfigure,
|
2017-12-12 15:22:47 +00:00
|
|
|
.deinitialize = lgr_opengl_deinitialize,
|
|
|
|
.is_compatible = lgr_opengl_is_compatible,
|
|
|
|
.on_resize = lgr_opengl_on_resize,
|
2017-12-12 16:08:13 +00:00
|
|
|
.on_mouse_shape = lgr_opengl_on_mouse_shape,
|
2017-12-12 15:22:47 +00:00
|
|
|
.on_mouse_event = lgr_opengl_on_mouse_event,
|
|
|
|
.on_frame_event = lgr_opengl_on_frame_event,
|
|
|
|
.render = lgr_opengl_render
|
2017-12-05 09:33:05 +00:00
|
|
|
};
|