- Added libraries from formats and libvitaboy
This commit is contained in:
Tony Bark 2024-04-28 05:33:13 -04:00
parent 66ce473514
commit 5efdb29315
101 changed files with 11711 additions and 10889 deletions

View file

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 2.6)
project(libgldemo)
if(WIN32)
set(LIBGLDEMO_SOURCES wgl.c)
else()
set(LIBGLDEMO_SOURCES glx.c)
add_definitions(-D_POSIX_C_SOURCE=200112)
endif()
add_library(libgldemo_static STATIC ${LIBGLDEMO_SOURCES})
set_target_properties(libgldemo_static PROPERTIES
OUTPUT_NAME "gldemo"
CLEAN_DIRECT_OUTPUT 1)

234
library/libgldemo/glx.c Normal file
View file

@ -0,0 +1,234 @@
/*
libgldemo - General-purpose OpenGL demo backend
glx.c - Copyright (c) 2012 Niotso Project <http://niotso.org/>
Author(s): Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or 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 "libgldemo.h"
static Display *display = NULL;
static int screen = 0;
static Window window = 0;
static GLXContext hrc = 0;
static uint8_t keys[256] = {0};
static uint8_t f11_pressed = 0;
static uint16_t WndWidth, WndHeight;
static uint16_t ResWidth, ResHeight;
static int fullscreen = 0;
void KillGLWindow()
{
if(hrc){
glXMakeCurrent(display, None, NULL);
glXDestroyContext(display, hrc);
hrc = 0;
}
if(window){
XDestroyWindow(display, window);
window = 0;
}
}
static int CreateGLWindow(const char *__restrict title, uint16_t width, uint16_t height)
{
int attlist[] = {
GLX_RGBA,
GLX_RED_SIZE, 4,
GLX_GREEN_SIZE, 4,
GLX_BLUE_SIZE, 4,
GLX_DEPTH_SIZE, 16,
GLX_DOUBLEBUFFER,
None
};
XVisualInfo *visualinfo;
Colormap cmap;
Atom wmDelete;
XSetWindowAttributes attr;
int (APIENTRY *glXSwapIntervalSGIptr)(int);
visualinfo = glXChooseVisual(display, screen, attlist);
if(visualinfo == NULL){
KillGLWindow();
DemoErrorBox("Can't find a suitable pixel format.");
return 0;
}
/* create a color map */
cmap = XCreateColormap(display, RootWindow(display, visualinfo->screen), visualinfo->visual, AllocNone);
attr.colormap = cmap;
attr.border_pixel = 0;
hrc = glXCreateContext(display, visualinfo, NULL, GL_TRUE);
if(hrc == NULL){
KillGLWindow();
DemoErrorBox("Failed to create an OpenGL rendering context.");
return 0;
}
attr.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask;
if(fullscreen){
attr.override_redirect = True;
width = ResWidth;
height = ResHeight;
XWarpPointer(display, None, window, 0, 0, 0, 0, 0, 0);
}else{
attr.override_redirect = False;
}
if(!(window = XCreateWindow(display, XRootWindow(display, visualinfo->screen),
(ResWidth - width)>>1,
(ResHeight - height)>>1,
width,
height,
0, visualinfo->depth, InputOutput, visualinfo->visual,
CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect, &attr)
)){
KillGLWindow();
DemoErrorBox("Window creation error.");
return 0;
}
WndWidth = width;
WndHeight = height;
XSetStandardProperties(display, window, title, title, None, NULL, 0, NULL);
XMapRaised(display, window);
XGrabKeyboard(display, window, True, GrabModeAsync, GrabModeAsync, CurrentTime);
wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True);
XSetWMProtocols(display, window, &wmDelete, 1);
/* connect the glx-context to the window */
glXMakeCurrent(display, window, hrc);
XFlush(display);
if(!Demo.InitGL()){
KillGLWindow();
DemoErrorBox("Initialization failed.");
return 0;
}
if(!Demo.ResizeScene(width&&height ? width : 1, width&&height ? height : 1)){
KillGLWindow();
DemoErrorBox("Scene resize failed.");
return 0;
}
glXSwapIntervalSGIptr = (int (APIENTRY *)(int)) glXGetProcAddressARB((const GLubyte *) "glXSwapIntervalSGI");
if(glXSwapIntervalSGIptr) glXSwapIntervalSGIptr(1);
return 1;
}
int main()
{
int dotclock;
XF86VidModeModeLine modeline;
struct timespec time1;
display = XOpenDisplay(0);
if(display == NULL){
KillGLWindow();
DemoErrorBox("Failed to obtain the X11 display context.");
return 0;
}
XkbSetDetectableAutoRepeat(display, True, 0);
screen = XDefaultScreen(display);
XF86VidModeGetModeLine(display, screen, &dotclock, &modeline);
ResWidth = modeline.hdisplay;
ResHeight = modeline.vdisplay;
if(Demo.Startup && !Demo.Startup())
return -1;
if(!CreateGLWindow(Demo.Title, Demo.Width, Demo.Height))
return -1;
clock_gettime(CLOCK_REALTIME, &time1);
while(1){
struct timespec time2;
float TimeDelta;
while(XPending(display)){
XEvent event;
XNextEvent(display, &event);
switch(event.type){
case ConfigureNotify:
if(((unsigned)event.xconfigure.width != WndWidth) ||
((unsigned)event.xconfigure.height != WndHeight)){
WndWidth = event.xconfigure.width;
WndHeight = event.xconfigure.height;
Demo.ResizeScene(event.xconfigure.width, event.xconfigure.height);
}
break;
case KeyPress: {
KeySym key = XLookupKeysym(&event.xkey, 0);
if(key <= 255)
keys[(key + 'A' - 'a') & 255] = 1;
else if(key == XK_Left) keys[KEY_LEFT] = 1;
else if(key == XK_Up) keys[KEY_UP] = 1;
else if(key == XK_Right) keys[KEY_RIGHT] = 1;
else if(key == XK_Down) keys[KEY_DOWN] = 1;
else{
if(key == XK_Escape){
KillGLWindow();
XCloseDisplay(display);
return (!Demo.Shutdown || Demo.Shutdown()) ? 0 : -1;
}
if(key == XK_F11 && !f11_pressed){
KillGLWindow();
fullscreen = !fullscreen;
CreateGLWindow(Demo.Title, Demo.Width, Demo.Height);
f11_pressed = 1;
}
}
break;
}
case KeyRelease: {
KeySym key = XLookupKeysym(&event.xkey, 0);
if(key <= 255)
keys[(key + 'A' - 'a') & 255] = 0;
else if(key == XK_Left) keys[KEY_LEFT] = 0;
else if(key == XK_Up) keys[KEY_UP] = 0;
else if(key == XK_Right) keys[KEY_RIGHT] = 0;
else if(key == XK_Down) keys[KEY_DOWN] = 0;
else if(key == XK_F11) f11_pressed = 0;
break;
}
case ClientMessage:
if (XGetAtomName(display, event.xclient.message_type)[0] == 'W'){
KillGLWindow();
XCloseDisplay(display);
return (!Demo.Shutdown || Demo.Shutdown()) ? 0 : -1;
}
}
}
/* Find the timedelta */
clock_gettime(CLOCK_REALTIME, &time2);
TimeDelta = (float)(time2.tv_sec - time1.tv_sec) + ((float)(time2.tv_nsec - time1.tv_nsec)) * 1e-9;
if(TimeDelta < 0) TimeDelta = 0; /* Safe-guard in case of system delay */
time1 = time2;
/* Draw */
Demo.DrawScene(TimeDelta, keys);
glXSwapBuffers(display, window);
}
}

View file

@ -0,0 +1,68 @@
/*
libgldemo - General-purpose OpenGL demo backend
libgldemo.h - Copyright (c) 2012 Niotso Project <http://niotso.org/>
Author(s): Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or 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 <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
typedef struct {
const char *__restrict Title;
uint16_t Width, Height;
int (* Startup)(void);
int (* Shutdown)(void);
int (* InitGL)(void);
int (* ResizeScene)(uint16_t width, uint16_t height);
int (* DrawScene)(float TimeDelta, uint8_t keys[256]);
} DemoConfig;
#ifdef __cplusplus
extern "C" {
#endif
extern const DemoConfig Demo;
#ifdef __cplusplus
}
#endif
#ifdef _WIN32
#include <windows.h>
#define DemoMessageBox(x) MessageBox(NULL, x, NULL, MB_OK)
#define DemoErrorBox(x) MessageBox(NULL, x, NULL, MB_OK | MB_ICONERROR)
#define KEY_LEFT VK_LEFT
#define KEY_UP VK_UP
#define KEY_RIGHT VK_RIGHT
#define KEY_DOWN VK_DOWN
#else
#define POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <time.h>
#include <GL/glx.h>
#include <X11/extensions/xf86vmode.h>
#include <X11/XKBlib.h>
#include <X11/keysym.h>
#define DemoMessageBox(x) fprintf(stdout, "%s\n", x)
#define DemoErrorBox(x) fprintf(stderr, "%s\n", x)
#define KEY_LEFT 0x25
#define KEY_UP 0x26
#define KEY_RIGHT 0x27
#define KEY_DOWN 0x28
#endif

281
library/libgldemo/wgl.c Normal file
View file

@ -0,0 +1,281 @@
/*
libgldemo - General-purpose OpenGL demo backend
wgl.c - Copyright (c) 2012 Niotso Project <http://niotso.org/>
Author(s): Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or 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 "libgldemo.h"
static HWND hWnd = NULL;
static HDC hDC = NULL;
static HGLRC hRC = NULL;
static HINSTANCE hInst;
static uint8_t keys[256] = {0};
static uint16_t ResWidth, ResHeight, ResDepth;
static int fullscreen = 0;
static float FramePeriod;
static void KillGLWindow()
{
if(fullscreen){
ShowCursor(1);
}
if(hRC){
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
hRC = NULL;
}
if(hDC){
ReleaseDC(hWnd, hDC);
hDC = NULL;
}
if(hWnd){
DestroyWindow(hWnd);
hWnd = NULL;
}
}
static int CreateGLWindow(const char *__restrict title, uint16_t width, uint16_t height)
{
const PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), 1, /* Size and version */
PFD_DRAW_TO_WINDOW | /* dwFlags */
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA, /* iPixelType */
0, /* cColorBits */
0, 0, 0, 0, 0, 0, 0, 0, /* R,G,B,A bits */
0, 0, 0, 0, 0, /* Accumulation buffer bits */
16, /* cDepthBits */
0, /* cStencilBits */
0, /* cAuxBuffers */
PFD_MAIN_PLANE, /* iLayerType */
0, /* Reserved */
0, 0, 0 /* Masks */
};
DWORD dwStyle, dwExStyle;
RECT WindowRect;
int PixelFormat;
BOOL (WINAPI *wglSwapIntervalEXT)(int);
int (WINAPI *wglGetSwapIntervalEXT)(void);
if(fullscreen){
dwExStyle = WS_EX_APPWINDOW | WS_EX_TOPMOST;
dwStyle = WS_POPUP;
width = ResWidth;
height = ResHeight;
ShowCursor(0);
}else{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle = WS_OVERLAPPEDWINDOW;
WindowRect.left = 0;
WindowRect.right = width;
WindowRect.top = 0;
WindowRect.bottom = height;
AdjustWindowRectEx(&WindowRect, dwStyle, 0, dwExStyle);
width = WindowRect.right - WindowRect.left;
height = WindowRect.bottom - WindowRect.top;
}
if(!(hWnd = CreateWindowEx(dwExStyle, "OpenGL",
title,
dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
(ResWidth - width)>>1,
(ResHeight - height)>>1,
width,
height,
NULL, NULL, hInst, NULL))
){
KillGLWindow();
DemoErrorBox("Window creation error.");
return 0;
}
hDC = GetDC(hWnd);
if(!hDC){
KillGLWindow();
DemoErrorBox("Failed to create an OpenGL device context.");
return 0;
}
PixelFormat = ChoosePixelFormat(hDC, &pfd);
if(!PixelFormat){
KillGLWindow();
DemoErrorBox("Can't find a suitable PixelFormat.");
return 0;
}
if(!SetPixelFormat(hDC, PixelFormat, &pfd)){
KillGLWindow();
DemoErrorBox("Can't set the PixelFormat.");
return 0;
}
hRC = wglCreateContext(hDC);
if(!hRC){
KillGLWindow();
DemoErrorBox("Failed to create an OpenGL rendering context.");
return 0;
}
if(!wglMakeCurrent(hDC, hRC)){
KillGLWindow();
DemoErrorBox("Failed to activate the OpenGL device context.");
return 0;
}
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
if(!Demo.InitGL()){
KillGLWindow();
DemoErrorBox("Initialization failed.");
return 0;
}
if(!Demo.ResizeScene(width&&height ? width : 1, width&&height ? height : 1)){
KillGLWindow();
DemoErrorBox("Scene resize failed.");
return 0;
}
wglSwapIntervalEXT = (BOOL (WINAPI *)(int)) wglGetProcAddress("wglSwapIntervalEXT");
if(wglSwapIntervalEXT) wglSwapIntervalEXT(1);
wglGetSwapIntervalEXT = (int (WINAPI *)(void)) wglGetProcAddress("wglGetSwapIntervalEXT");
if(wglGetSwapIntervalEXT) wglGetSwapIntervalEXT(); /* Seems necessary on some cards */
return 1;
}
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_KEYDOWN:
if(wParam == VK_ESCAPE){
PostQuitMessage(0);
}else if(wParam == VK_F11 && !keys[VK_F11]){
KillGLWindow();
fullscreen = !fullscreen;
if(!CreateGLWindow(Demo.Title, Demo.Width, Demo.Height))
PostQuitMessage(0);
}
case WM_KEYUP:
keys[wParam] = (uMsg == WM_KEYDOWN);
return 0;
case WM_DEVMODECHANGE: {
DEVMODE dm;
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
ResWidth = dm.dmPelsWidth;
ResHeight = dm.dmPelsHeight;
ResDepth = dm.dmBitsPerPel;
FramePeriod = 1.0f/dm.dmDisplayFrequency;
}
case WM_SIZE:
Demo.ResizeScene(LOWORD(lParam), HIWORD(lParam));
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc = {
CS_HREDRAW | CS_VREDRAW | CS_OWNDC, /* style */
(WNDPROC) WndProc, /* lpfnWndProc */
0, /* cbClsExtra */
0, /* cbWndExtra */
NULL, /* hInstance */
NULL, /* hIcon */
NULL, /* hCursor */
NULL, /* hbrBackground */
NULL, /* lpszMenuName */
"OpenGL" /* lpszClassName */
};
DEVMODE dm;
LARGE_INTEGER ClockFreq, PreviousTime;
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
wc.hInstance = hInst = hInstance;
wc.hIcon = (HICON) LoadImage(NULL, IDI_WINLOGO, IMAGE_ICON, 0, 0, LR_SHARED);
wc.hCursor = (HCURSOR) LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
ResWidth = dm.dmPelsWidth;
ResHeight = dm.dmPelsHeight;
ResDepth = dm.dmBitsPerPel;
FramePeriod = 1.0f/dm.dmDisplayFrequency;
QueryPerformanceFrequency(&ClockFreq);
if(!RegisterClass(&wc)){
MessageBox(NULL, "Failed to register the window class.", NULL, MB_OK | MB_ICONERROR);
return 0;
}
if(Demo.Startup && !Demo.Startup())
return -1;
if(!CreateGLWindow(Demo.Title, Demo.Width, Demo.Height))
return -1;
QueryPerformanceCounter(&PreviousTime);
while(1){
MSG msg;
LARGE_INTEGER CurrentTime;
float TimeDelta;
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT){
KillGLWindow();
UnregisterClass("OpenGL", hInstance);
return (!Demo.Shutdown || Demo.Shutdown()) ? 0 : -1;
}
}
/* Find the timedelta */
QueryPerformanceCounter(&CurrentTime);
TimeDelta = (float)(CurrentTime.QuadPart-PreviousTime.QuadPart)/ClockFreq.QuadPart;
PreviousTime = CurrentTime;
if(TimeDelta < 0 || TimeDelta > 5) /* Safe-guard in case of system delay */
continue;
/* Draw */
Demo.DrawScene(TimeDelta, keys);
SwapBuffers(hDC);
/* Sleep for the remainder of the frame */
QueryPerformanceCounter(&CurrentTime);
TimeDelta = (float)(CurrentTime.QuadPart-PreviousTime.QuadPart)/ClockFreq.QuadPart;
TimeDelta = (FramePeriod - TimeDelta) * 1000;
if(TimeDelta > 1 && TimeDelta < 100) Sleep((unsigned) TimeDelta);
}
}