mirror of
https://github.com/nishiowo/nishbox
synced 2025-04-21 20:24:39 +00:00
init glx
This commit is contained in:
parent
16163edd5f
commit
945fc4dbd6
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
*.o
|
||||
*.exe
|
||||
*.a
|
||||
*.core
|
||||
/src/nishbox
|
||||
|
2
Makefile
2
Makefile
@ -3,7 +3,7 @@ include mk/p_*.mk
|
||||
|
||||
AR = $(TARGET_PREFIX)ar
|
||||
CC = $(TARGET_PREFIX)gcc
|
||||
CFLAGS = -D_DEFAULT_SOURCE -DUSE_$(BACKEND) -I../engine $(ODE_CFLAGS) $(AMX_CFLAGS) $(PAWNC_CFLAGS) $(GL_CFLAGS)
|
||||
CFLAGS = -g -D_DEFAULT_SOURCE -DUSE_$(BACKEND) -I../engine $(ODE_CFLAGS) $(AMX_CFLAGS) $(PAWNC_CFLAGS) $(GL_CFLAGS)
|
||||
LDFLAGS =
|
||||
LIBS = $(ODE_LIBS) $(AMX_LIBS) $(PAWNC_LIBS) $(GL_LIBS)
|
||||
|
||||
|
@ -3,26 +3,48 @@
|
||||
/* External library */
|
||||
#include <X11/Xlib.h>
|
||||
#include <GL/glx.h>
|
||||
#include <GL/glxext.h>
|
||||
|
||||
/* Interface */
|
||||
#include "nb_draw_platform.h"
|
||||
#include "nb_draw.h"
|
||||
|
||||
/* NishBox */
|
||||
#include "nb_log.h"
|
||||
|
||||
/* Standard */
|
||||
#include <string.h>
|
||||
|
||||
#ifndef GLX_MESA_swap_control
|
||||
#define GLX_MESA_swap_control 1
|
||||
typedef int (*PFNGLXGETSWAPINTERVALMESAPROC)(void);
|
||||
#endif
|
||||
|
||||
static int has_glx_extension(nb_draw_t* draw, const char* query) {
|
||||
const char* glx_ext = NULL;
|
||||
const char* ptr;
|
||||
const int len = strlen(query);
|
||||
if(glx_ext == NULL) {
|
||||
glx_ext = glXQueryExtensionsString(draw->display, DefaultScreen(draw->display));
|
||||
}
|
||||
ptr = strstr(glx_ext, query);
|
||||
return ((ptr != NULL) && ((ptr[len] == ' ') || (ptr[len] == '\0')));
|
||||
}
|
||||
|
||||
void _nb_draw_create(nb_draw_t** pdraw) {
|
||||
nb_draw_t* draw = *pdraw;
|
||||
int i;
|
||||
int attribs[64];
|
||||
int screen;
|
||||
Window root;
|
||||
XVisualInfo* visual;
|
||||
draw->display = XOpenDisplay(NULL);
|
||||
nb_draw_t* draw = *pdraw;
|
||||
int i = 0;
|
||||
int attribs[64];
|
||||
int screen;
|
||||
Window root;
|
||||
XVisualInfo* visual;
|
||||
XSetWindowAttributes attr;
|
||||
XSizeHints hints;
|
||||
int interval = 0;
|
||||
draw->display = XOpenDisplay(NULL);
|
||||
if(draw->display == NULL) {
|
||||
nb_function_log("Failed to open display", "");
|
||||
_nb_draw_destroy(draw);
|
||||
nb_draw_destroy(draw);
|
||||
*pdraw = NULL;
|
||||
return;
|
||||
}
|
||||
@ -45,12 +67,63 @@ void _nb_draw_create(nb_draw_t** pdraw) {
|
||||
|
||||
visual = glXChooseVisual(draw->display, screen, attribs);
|
||||
if(visual == NULL) {
|
||||
nb_function_log("Failed to get a visual", "");
|
||||
_nb_draw_destroy(draw);
|
||||
nb_function_log("Failed to get visual", "");
|
||||
nb_draw_destroy(draw);
|
||||
*pdraw = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
attr.colormap = XCreateColormap(draw->display, root, visual->visual, AllocNone);
|
||||
attr.event_mask = StructureNotifyMask | ExposureMask;
|
||||
draw->window = XCreateWindow(draw->display, root, draw->width, draw->height, draw->width, draw->height, 0, visual->depth, InputOutput, visual->visual, CWColormap | CWEventMask, &attr);
|
||||
|
||||
hints.x = draw->x;
|
||||
hints.y = draw->y;
|
||||
hints.width = draw->width;
|
||||
hints.height = draw->height;
|
||||
hints.flags = USSize | USPosition;
|
||||
XSetNormalHints(draw->display, draw->window, &hints);
|
||||
XSetStandardProperties(draw->display, draw->window, "NishBox", "NishBox", None, (char**)NULL, 0, &hints);
|
||||
|
||||
draw->context = glXCreateContext(draw->display, visual, NULL, True);
|
||||
if(draw->context == NULL) {
|
||||
XFree(visual);
|
||||
nb_function_log("Failed to get OpenGL context", "");
|
||||
nb_draw_destroy(draw);
|
||||
*pdraw = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
XFree(visual);
|
||||
|
||||
XMapWindow(draw->display, draw->window);
|
||||
glXMakeCurrent(draw->display, draw->window, draw->context);
|
||||
|
||||
#if defined(GLX_EXT_swap_control)
|
||||
if(has_glx_extension(draw, "GLX_EXT_swap_control")) {
|
||||
unsigned int tmp = -1;
|
||||
glXQueryDrawable(draw->display, draw->window, GLX_SWAP_INTERVAL_EXT, &tmp);
|
||||
interval = tmp;
|
||||
} else
|
||||
#endif
|
||||
if(has_glx_extension(draw, "GLX_MESA_swap_control")) {
|
||||
PFNGLXGETSWAPINTERVALMESAPROC proc = (PFNGLXGETSWAPINTERVALMESAPROC)glXGetProcAddressARB("glXGetSwapIntervalMESA");
|
||||
interval = proc();
|
||||
} else if(has_glx_extension(draw, "GLX_SGI_swap_control")) {
|
||||
interval = 1;
|
||||
}
|
||||
if(interval > 0) {
|
||||
nb_function_log("VSync should be enabled", "");
|
||||
}
|
||||
}
|
||||
|
||||
void _nb_draw_destroy(nb_draw_t* draw) {
|
||||
if(draw->display != NULL) XCloseDisplay(draw->display);
|
||||
if(draw->context != NULL) {
|
||||
glXMakeCurrent(draw->display, None, NULL);
|
||||
glXDestroyContext(draw->display, draw->context);
|
||||
}
|
||||
if(draw->display != NULL) {
|
||||
XDestroyWindow(draw->display, draw->window);
|
||||
XCloseDisplay(draw->display);
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
/* Standard */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void nb_engine_begin(void) {
|
||||
nb_version_t ver;
|
||||
@ -26,7 +27,8 @@ void nb_engine_end(void) { dCloseODE(); }
|
||||
|
||||
nb_engine_t* nb_engine_create(void) {
|
||||
nb_engine_t* engine = malloc(sizeof(*engine));
|
||||
engine->draw = nb_draw_create();
|
||||
memset(engine, 0, sizeof(*engine));
|
||||
engine->draw = nb_draw_create();
|
||||
if(engine->draw == NULL) {
|
||||
nb_function_log("Failed to create drawing interface", "");
|
||||
free(engine);
|
||||
@ -37,4 +39,9 @@ nb_engine_t* nb_engine_create(void) {
|
||||
return engine;
|
||||
}
|
||||
|
||||
void nb_engine_destroy(nb_engine_t* engine) { dWorldDestroy(engine->world); }
|
||||
void nb_engine_destroy(nb_engine_t* engine) {
|
||||
dWorldDestroy(engine->world);
|
||||
if(engine->draw != NULL) nb_draw_destroy(engine->draw);
|
||||
free(engine);
|
||||
nb_function_log("Destroyed engine", "");
|
||||
}
|
||||
|
@ -18,6 +18,10 @@
|
||||
nb_draw_t* nb_draw_create(void) {
|
||||
nb_draw_t* draw = malloc(sizeof(*draw));
|
||||
memset(draw, 0, sizeof(*draw));
|
||||
draw->x = 0;
|
||||
draw->y = 0;
|
||||
draw->width = 640;
|
||||
draw->height = 480;
|
||||
_nb_draw_create(&draw);
|
||||
if(draw != NULL) {
|
||||
nb_function_log("Created drawing interface successfully", "");
|
||||
@ -25,4 +29,8 @@ nb_draw_t* nb_draw_create(void) {
|
||||
return draw;
|
||||
}
|
||||
|
||||
void nb_draw_destroy(nb_draw_t* draw) { _nb_draw_destroy(draw); }
|
||||
void nb_draw_destroy(nb_draw_t* draw) {
|
||||
_nb_draw_destroy(draw);
|
||||
free(draw);
|
||||
nb_function_log("Destroyed drawing interface", "");
|
||||
}
|
||||
|
@ -29,6 +29,10 @@ typedef struct nb_draw {
|
||||
HDC dc;
|
||||
HGLRC glrc;
|
||||
#endif
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
int height;
|
||||
} nb_draw_t;
|
||||
#else
|
||||
typedef void nb_draw_t;
|
||||
|
Loading…
x
Reference in New Issue
Block a user