mirror of
https://github.com/LostArtefacts/TRX.git
synced 2025-04-29 21:27:59 +03:00
context: migrate from wgl_ext to SDL_GL (#680)
Migrate to SDL_GL functions made available in SDL2. Remove wgl_ext.c and wgl_ext.h. Add additional information to the log file to help debug issues like #582.
This commit is contained in:
parent
7afb2b9db0
commit
ffb9e8b51e
5 changed files with 74 additions and 342 deletions
|
@ -231,7 +231,6 @@ sources = [
|
|||
'src/gfx/gl/texture.c',
|
||||
'src/gfx/gl/utils.c',
|
||||
'src/gfx/gl/vertex_array.c',
|
||||
'src/gfx/gl/wgl_ext.c',
|
||||
'src/gfx/screenshot.c',
|
||||
'src/global/vars.c',
|
||||
'src/init.c',
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include "config.h"
|
||||
#include "game/shell.h"
|
||||
#include "gfx/gl/gl_core_3_3.h"
|
||||
#include "gfx/gl/wgl_ext.h"
|
||||
#include "gfx/gl/utils.h"
|
||||
#include "gfx/screenshot.h"
|
||||
#include "log.h"
|
||||
#include "memory.h"
|
||||
|
@ -13,9 +13,9 @@
|
|||
#include <string.h>
|
||||
|
||||
typedef struct GFX_Context {
|
||||
void *window_handle;
|
||||
HDC hdc; // GDI device context
|
||||
HGLRC hglrc; // OpenGL context handle
|
||||
SDL_GLContext context;
|
||||
SDL_Window *window_handle;
|
||||
|
||||
bool is_fullscreen; // fullscreen flag
|
||||
bool is_rendered; // rendering flag
|
||||
int32_t display_width;
|
||||
|
@ -31,8 +31,25 @@ typedef struct GFX_Context {
|
|||
|
||||
static GFX_Context m_Context = { 0 };
|
||||
|
||||
const char *get_extension_supported(const char *name)
|
||||
{
|
||||
int i, number_of_extensions;
|
||||
|
||||
glGetIntegerv(GL_NUM_EXTENSIONS, &number_of_extensions);
|
||||
|
||||
for (i = 0; i < number_of_extensions; i++) {
|
||||
const char *gl_ext = (const char *)glGetStringi(GL_EXTENSIONS, i);
|
||||
if (gl_ext && !strcmp(gl_ext, name)) {
|
||||
return "yes";
|
||||
}
|
||||
}
|
||||
return "no";
|
||||
}
|
||||
|
||||
void GFX_Context_Attach(void *window_handle)
|
||||
{
|
||||
const char *shading_ver;
|
||||
|
||||
if (m_Context.window_handle) {
|
||||
return;
|
||||
}
|
||||
|
@ -40,55 +57,47 @@ void GFX_Context_Attach(void *window_handle)
|
|||
LOG_INFO("Attaching to window %p", window_handle);
|
||||
|
||||
m_Context.window_handle = window_handle;
|
||||
m_Context.context = SDL_GL_CreateContext(m_Context.window_handle);
|
||||
|
||||
SDL_SysWMinfo wm_info;
|
||||
SDL_VERSION(&wm_info.version);
|
||||
SDL_GetWindowWMInfo(window_handle, &wm_info);
|
||||
|
||||
HWND hwnd = wm_info.info.win.window;
|
||||
if (!hwnd) {
|
||||
Shell_ExitSystem("System Error: cannot create window");
|
||||
return;
|
||||
}
|
||||
|
||||
m_Context.hdc = GetDC(hwnd);
|
||||
if (!m_Context.hdc) {
|
||||
Shell_ExitSystem("Can't get device context");
|
||||
}
|
||||
|
||||
// get screen dimensions
|
||||
m_Context.screen_width = GetSystemMetrics(SM_CXSCREEN);
|
||||
m_Context.screen_height = GetSystemMetrics(SM_CYSCREEN);
|
||||
|
||||
// set pixel format
|
||||
PIXELFORMATDESCRIPTOR pfd;
|
||||
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
|
||||
pfd.nVersion = 1;
|
||||
pfd.iPixelType = PFD_TYPE_RGBA;
|
||||
pfd.cColorBits = 32;
|
||||
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
||||
pfd.cDepthBits = 32;
|
||||
pfd.iLayerType = PFD_MAIN_PLANE;
|
||||
|
||||
int pf = ChoosePixelFormat(m_Context.hdc, &pfd);
|
||||
if (!pf) {
|
||||
Shell_ExitSystem("Can't choose pixel format");
|
||||
}
|
||||
|
||||
if (!SetPixelFormat(m_Context.hdc, pf, &pfd)) {
|
||||
Shell_ExitSystem("Can't set pixel format");
|
||||
}
|
||||
|
||||
m_Context.hglrc = wglCreateContext(m_Context.hdc);
|
||||
if (!m_Context.hglrc || !wglMakeCurrent(m_Context.hdc, m_Context.hglrc)) {
|
||||
if (!m_Context.context) {
|
||||
Shell_ExitSystem("Can't create OpenGL context");
|
||||
}
|
||||
|
||||
if (SDL_GL_MakeCurrent(m_Context.window_handle, m_Context.context)) {
|
||||
Shell_ExitSystem("Can't activate OpenGL context");
|
||||
}
|
||||
|
||||
LOG_INFO("OpenGL vendor string: %s", glGetString(GL_VENDOR));
|
||||
LOG_INFO("OpenGL renderer string: %s", glGetString(GL_RENDERER));
|
||||
LOG_INFO("OpenGL version string: %s", glGetString(GL_VERSION));
|
||||
|
||||
shading_ver = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
|
||||
if (shading_ver != NULL) {
|
||||
LOG_INFO("Shading version string: %s", shading_ver);
|
||||
} else {
|
||||
GFX_GL_CheckError();
|
||||
}
|
||||
|
||||
LOG_INFO(
|
||||
"GL_ARB_explicit_attrib_location supported: %s",
|
||||
get_extension_supported("GL_ARB_explicit_attrib_location"));
|
||||
LOG_INFO(
|
||||
"GL_EXT_gpu_shader4 supported: %s",
|
||||
get_extension_supported("GL_EXT_gpu_shader4"));
|
||||
|
||||
// get screen dimensions
|
||||
SDL_DisplayMode DM;
|
||||
|
||||
SDL_GetDesktopDisplayMode(0, &DM);
|
||||
m_Context.screen_width = DM.w;
|
||||
m_Context.screen_height = DM.h;
|
||||
LOG_INFO("GetDesktopDisplayMode=%dx%d", DM.w, DM.h);
|
||||
|
||||
glClearColor(0, 0, 0, 0);
|
||||
glClearDepth(1);
|
||||
|
||||
// VSync defaults to on unless user disabled it in runtime json
|
||||
wglSwapIntervalEXT(1);
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
|
||||
GFX_2D_Renderer_Init(&m_Context.renderer_2d);
|
||||
GFX_3D_Renderer_Init(&m_Context.renderer_3d);
|
||||
|
@ -103,15 +112,18 @@ void GFX_Context_Detach(void)
|
|||
GFX_2D_Renderer_Close(&m_Context.renderer_2d);
|
||||
GFX_3D_Renderer_Close(&m_Context.renderer_3d);
|
||||
|
||||
wglDeleteContext(m_Context.hglrc);
|
||||
m_Context.hglrc = NULL;
|
||||
SDL_GL_MakeCurrent(NULL, NULL);
|
||||
|
||||
if (m_Context.context != NULL) {
|
||||
SDL_GL_DeleteContext(m_Context.context);
|
||||
m_Context.context = NULL;
|
||||
}
|
||||
m_Context.window_handle = NULL;
|
||||
}
|
||||
|
||||
void GFX_Context_SetVSync(bool vsync)
|
||||
{
|
||||
wglSwapIntervalEXT(vsync);
|
||||
SDL_GL_SetSwapInterval(vsync);
|
||||
}
|
||||
|
||||
bool GFX_Context_IsFullscreen(void)
|
||||
|
@ -206,7 +218,8 @@ void GFX_Context_SwapBuffers(void)
|
|||
Memory_FreePointer(&m_Context.scheduled_screenshot_path);
|
||||
}
|
||||
|
||||
SwapBuffers(m_Context.hdc);
|
||||
SDL_GL_SwapWindow(m_Context.window_handle);
|
||||
|
||||
glDrawBuffer(GL_BACK);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
|
|
|
@ -1,205 +0,0 @@
|
|||
#include "gfx/gl/wgl_ext.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if __GNUC__ || __MINGW32__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wcast-function-type"
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include <dlfcn.h>
|
||||
|
||||
static void *AppleGLGetProcAddress(const char *name)
|
||||
{
|
||||
static void *image = NULL;
|
||||
|
||||
if (NULL == image)
|
||||
image = dlopen(
|
||||
"/System/Library/Frameworks/OpenGL.framework/Versions/"
|
||||
"Current/OpenGL",
|
||||
RTLD_LAZY);
|
||||
|
||||
return (image ? dlsym(image, name) : NULL);
|
||||
}
|
||||
#endif /* __APPLE__ */
|
||||
|
||||
#if defined(__sgi) || defined(__sun)
|
||||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static void *SunGetProcAddress(const GLubyte *name)
|
||||
{
|
||||
static void *h = NULL;
|
||||
static void *gpa;
|
||||
|
||||
if (h == NULL) {
|
||||
if ((h = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL)) == NULL)
|
||||
return NULL;
|
||||
gpa = dlsym(h, "glXGetProcAddress");
|
||||
}
|
||||
|
||||
if (gpa != NULL)
|
||||
return ((void *(*)(const GLubyte *))gpa)(name);
|
||||
else
|
||||
return dlsym(h, (const char *)name);
|
||||
}
|
||||
#endif /* __sgi || __sun */
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable : 4055)
|
||||
#pragma warning(disable : 4054)
|
||||
#pragma warning(disable : 4996)
|
||||
#endif
|
||||
|
||||
static int TestPointer(const PROC pTest)
|
||||
{
|
||||
ptrdiff_t iTest;
|
||||
if (!pTest)
|
||||
return 0;
|
||||
iTest = (ptrdiff_t)pTest;
|
||||
|
||||
if (iTest == 1 || iTest == 2 || iTest == 3 || iTest == -1)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static PROC WinGetProcAddress(const char *name)
|
||||
{
|
||||
HMODULE glMod = NULL;
|
||||
PROC pFunc = wglGetProcAddress((LPCSTR)name);
|
||||
if (TestPointer(pFunc)) {
|
||||
return pFunc;
|
||||
}
|
||||
glMod = GetModuleHandleA("OpenGL32.dll");
|
||||
return (PROC)GetProcAddress(glMod, (LPCSTR)name);
|
||||
}
|
||||
|
||||
#define IntGetProcAddress(name) WinGetProcAddress(name)
|
||||
#else
|
||||
#if defined(__APPLE__)
|
||||
#define IntGetProcAddress(name) AppleGLGetProcAddress(name)
|
||||
#else
|
||||
#if defined(__sgi) || defined(__sun)
|
||||
#define IntGetProcAddress(name) SunGetProcAddress(name)
|
||||
#else /* GLX */
|
||||
#include <GL/glx.h>
|
||||
|
||||
#define IntGetProcAddress(name) \
|
||||
(*glXGetProcAddressARB)((const GLubyte *)name)
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
int wgl_ext_EXT_swap_control = 0;
|
||||
|
||||
/* Extension: EXT_swap_control*/
|
||||
typedef int(CODEGEN_FUNCPTR *PFN_PTRC_WGLGETSWAPINTERVALEXTPROC)(void);
|
||||
static int CODEGEN_FUNCPTR Switch_GetSwapIntervalEXT(void);
|
||||
typedef BOOL(CODEGEN_FUNCPTR *PFN_PTRC_WGLSWAPINTERVALEXTPROC)(int);
|
||||
static BOOL CODEGEN_FUNCPTR Switch_SwapIntervalEXT(int interval);
|
||||
|
||||
/* Extension: EXT_swap_control*/
|
||||
PFN_PTRC_WGLGETSWAPINTERVALEXTPROC _ptrc_wglGetSwapIntervalEXT =
|
||||
Switch_GetSwapIntervalEXT;
|
||||
PFN_PTRC_WGLSWAPINTERVALEXTPROC _ptrc_wglSwapIntervalEXT =
|
||||
Switch_SwapIntervalEXT;
|
||||
|
||||
/* Extension: EXT_swap_control*/
|
||||
static int CODEGEN_FUNCPTR Switch_GetSwapIntervalEXT(void)
|
||||
{
|
||||
_ptrc_wglGetSwapIntervalEXT =
|
||||
(PFN_PTRC_WGLGETSWAPINTERVALEXTPROC)IntGetProcAddress(
|
||||
"wglGetSwapIntervalEXT");
|
||||
return _ptrc_wglGetSwapIntervalEXT();
|
||||
}
|
||||
|
||||
static BOOL CODEGEN_FUNCPTR Switch_SwapIntervalEXT(int interval)
|
||||
{
|
||||
_ptrc_wglSwapIntervalEXT =
|
||||
(PFN_PTRC_WGLSWAPINTERVALEXTPROC)IntGetProcAddress(
|
||||
"wglSwapIntervalEXT");
|
||||
return _ptrc_wglSwapIntervalEXT(interval);
|
||||
}
|
||||
|
||||
static void ClearExtensionVariables(void)
|
||||
{
|
||||
wgl_ext_EXT_swap_control = 0;
|
||||
}
|
||||
|
||||
typedef struct wgl_MapTable_s {
|
||||
char *extName;
|
||||
int *extVariable;
|
||||
} wgl_MapTable;
|
||||
|
||||
static wgl_MapTable
|
||||
g_mappingTable[1]; /*This is intensionally left uninitialized.*/
|
||||
|
||||
static void LoadExtByName(const char *extensionName)
|
||||
{
|
||||
wgl_MapTable *tableEnd = &g_mappingTable[1];
|
||||
wgl_MapTable *entry = &g_mappingTable[0];
|
||||
for (; entry != tableEnd; ++entry) {
|
||||
if (strcmp(entry->extName, extensionName) == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (entry != tableEnd)
|
||||
*(entry->extVariable) = 1;
|
||||
}
|
||||
|
||||
static void ProcExtsFromExtString(const char *strExtList)
|
||||
{
|
||||
size_t iExtListLen = strlen(strExtList);
|
||||
const char *strExtListEnd = strExtList + iExtListLen;
|
||||
const char *strCurrPos = strExtList;
|
||||
char strWorkBuff[256];
|
||||
|
||||
while (*strCurrPos) {
|
||||
/*Get the extension at our position.*/
|
||||
int iStrLen = 0;
|
||||
const char *strEndStr = strchr(strCurrPos, ' ');
|
||||
int iStop = 0;
|
||||
if (strEndStr == NULL) {
|
||||
strEndStr = strExtListEnd;
|
||||
iStop = 1;
|
||||
}
|
||||
|
||||
iStrLen = (int)((ptrdiff_t)strEndStr - (ptrdiff_t)strCurrPos);
|
||||
|
||||
if (iStrLen > 255)
|
||||
return;
|
||||
|
||||
strncpy(strWorkBuff, strCurrPos, iStrLen);
|
||||
strWorkBuff[iStrLen] = '\0';
|
||||
|
||||
LoadExtByName(strWorkBuff);
|
||||
|
||||
strCurrPos = strEndStr + 1;
|
||||
if (iStop)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void wgl_CheckExtensions(HDC hdc)
|
||||
{
|
||||
ClearExtensionVariables();
|
||||
|
||||
{
|
||||
typedef const char *(CODEGEN_FUNCPTR * MYGETEXTSTRINGPROC)(HDC);
|
||||
MYGETEXTSTRINGPROC InternalGetExtensionString =
|
||||
(MYGETEXTSTRINGPROC)IntGetProcAddress("wglGetExtensionsStringARB");
|
||||
if (!InternalGetExtensionString)
|
||||
return;
|
||||
ProcExtsFromExtString((const char *)InternalGetExtensionString(hdc));
|
||||
}
|
||||
}
|
||||
|
||||
#if __GNUC__ || __MINGW32__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
|
@ -1,84 +0,0 @@
|
|||
#ifndef WINDOWSGL_NOLOAD_STYLE_H
|
||||
#define WINDOWSGL_NOLOAD_STYLE_H
|
||||
|
||||
#ifdef __wglext_h_
|
||||
#error Attempt to include auto-generated WGL header after wglext.h
|
||||
#endif
|
||||
|
||||
#define __wglext_h_
|
||||
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#endif
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
|
||||
#ifdef CODEGEN_FUNCPTR
|
||||
#undef CODEGEN_FUNCPTR
|
||||
#endif /*CODEGEN_FUNCPTR*/
|
||||
#define CODEGEN_FUNCPTR WINAPI
|
||||
|
||||
#ifndef GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS
|
||||
#define GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS
|
||||
|
||||
typedef unsigned int GLenum;
|
||||
typedef unsigned char GLboolean;
|
||||
typedef unsigned int GLbitfield;
|
||||
typedef signed char GLbyte;
|
||||
typedef short GLshort;
|
||||
typedef int GLint;
|
||||
typedef int GLsizei;
|
||||
typedef unsigned char GLubyte;
|
||||
typedef unsigned short GLushort;
|
||||
typedef unsigned int GLuint;
|
||||
typedef float GLfloat;
|
||||
typedef float GLclampf;
|
||||
typedef double GLdouble;
|
||||
typedef double GLclampd;
|
||||
#define GLvoid void
|
||||
|
||||
#endif /*GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS*/
|
||||
|
||||
#ifndef GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS
|
||||
#define GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS
|
||||
|
||||
#endif /*GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS*/
|
||||
|
||||
struct _GPU_DEVICE {
|
||||
DWORD cb;
|
||||
CHAR DeviceName[32];
|
||||
CHAR DeviceString[128];
|
||||
DWORD Flags;
|
||||
RECT rcVirtualScreen;
|
||||
};
|
||||
DECLARE_HANDLE(HPBUFFERARB);
|
||||
DECLARE_HANDLE(HPBUFFEREXT);
|
||||
DECLARE_HANDLE(HVIDEOOUTPUTDEVICENV);
|
||||
DECLARE_HANDLE(HPVIDEODEV);
|
||||
DECLARE_HANDLE(HGPUNV);
|
||||
DECLARE_HANDLE(HVIDEOINPUTDEVICENV);
|
||||
typedef struct _GPU_DEVICE *PGPU_DEVICE;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /*__cplusplus*/
|
||||
|
||||
/***********************/
|
||||
/* Extension Variables*/
|
||||
|
||||
extern int wgl_ext_EXT_swap_control;
|
||||
|
||||
/* Extension: EXT_swap_control*/
|
||||
extern int(CODEGEN_FUNCPTR *_ptrc_wglGetSwapIntervalEXT)(void);
|
||||
#define wglGetSwapIntervalEXT _ptrc_wglGetSwapIntervalEXT
|
||||
extern BOOL(CODEGEN_FUNCPTR *_ptrc_wglSwapIntervalEXT)(int interval);
|
||||
#define wglSwapIntervalEXT _ptrc_wglSwapIntervalEXT
|
||||
|
||||
void wgl_CheckExtensions(HDC hdc);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /*__cplusplus*/
|
||||
|
||||
#endif /*WINDOWSGL_NOLOAD_STYLE_H*/
|
|
@ -151,18 +151,27 @@ int main(int argc, char **argv)
|
|||
m_ArgCount = argc;
|
||||
m_ArgStrings = argv;
|
||||
|
||||
if (SDL_Init(SDL_INIT_EVENTS) < 0) {
|
||||
if (SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO) < 0) {
|
||||
char buf[256];
|
||||
sprintf(buf, "Cannot initialize SDL: %s", SDL_GetError());
|
||||
S_Shell_ShowFatalError(buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Setup minimum properties of GL context
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
m_Window = SDL_CreateWindow(
|
||||
"Tomb1Main", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280,
|
||||
720,
|
||||
SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP
|
||||
| SDL_WINDOW_RESIZABLE);
|
||||
SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_RESIZABLE
|
||||
| SDL_WINDOW_OPENGL);
|
||||
|
||||
if (!m_Window) {
|
||||
S_Shell_ShowFatalError("System Error: cannot create window");
|
||||
return 1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue