rename and separate type def

This commit is contained in:
NishiOwO 2025-04-03 18:14:35 +09:00
parent 0d71f02db3
commit 48f98f3f18
No known key found for this signature in database
GPG Key ID: 27EF69B208EB9343
37 changed files with 750 additions and 428 deletions

View File

@ -1,72 +0,0 @@
#define NB_EXPOSE_DRAW_PLATFORM
#include "nb_pre.h"
/* External library */
#include <GL/gl.h>
#include <GL/glu.h>
#include <GLFW/glfw3.h>
/* Interface */
#include "nb_draw_platform.h"
/* NishBox */
#include "nb_log.h"
#include "nb_draw.h"
/* Standard */
#include <string.h>
void _nb_draw_init(void) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
}
int _nb_draw_has_extension(nb_draw_t* draw, const char* query) {
const char* ext = NULL;
const char* ptr;
const int len = strlen(query);
glfwMakeContextCurrent(draw->window);
return glfwExtensionSupported(query);
}
int _nb_draw_step(nb_draw_t* draw) {
int ret = 0;
glfwMakeContextCurrent(draw->window);
draw->close = glfwWindowShouldClose(draw->window);
if(draw->close) glfwSetWindowShouldClose(draw->window, GLFW_FALSE);
glfwPollEvents();
if(ret == 0) {
nb_draw_frame(draw);
glFlush();
glfwSwapBuffers(draw->window);
}
return ret;
}
void _nb_draw_create(nb_draw_t** pdraw) {
nb_draw_t* draw = *pdraw;
draw->window = glfwCreateWindow(draw->width, draw->height, "NishBox (GLFW)", NULL, NULL);
if(draw->window == NULL) {
nb_function_log("Failed to create window", "");
nb_draw_destroy(draw);
*pdraw = NULL;
return;
}
glfwMakeContextCurrent(draw->window);
glfwSwapInterval(1);
}
void _nb_draw_destroy(nb_draw_t* draw) {
if(draw->window != NULL) {
glfwDestroyWindow(draw->window);
}
}

View File

@ -1,191 +0,0 @@
#define NB_EXPOSE_DRAW_PLATFORM
#include "nb_pre.h"
/* External library */
#include <GL/gl.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <GL/glx.h>
#include <GL/glxext.h>
/* Interface */
#include "nb_draw_platform.h"
/* NishBox */
#include "nb_log.h"
#include "nb_draw.h"
/* Standard */
#include <string.h>
#ifndef GLX_MESA_swap_control
#define GLX_MESA_swap_control 1
typedef int (*PFNGLXGETSWAPINTERVALMESAPROC)(void);
typedef void (*PFNGLXSWAPINTERVALMESAPROC)(int);
#endif
#ifndef GLX_EXT_swap_control
#define GLX_EXT_swap_control 1
typedef void (*PFNGLXSWAPINTERVALEXTPROC)(Display*, GLXDrawable, int);
#endif
#ifndef GLX_SGI_swap_control
#define GLX_SGI_swap_control 1
typedef void (*PFNGLXSWAPINTERVALSGIPROC)(int);
#endif
void _nb_draw_init(void) {}
int _nb_draw_has_extension(nb_draw_t* draw, const char* query) {
const char* ext = NULL;
const char* ptr;
const int len = strlen(query);
glXMakeCurrent(draw->display, draw->window, draw->context);
ext = glXQueryExtensionsString(draw->display, DefaultScreen(draw->display));
ptr = strstr(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 = 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);
*pdraw = NULL;
return;
}
attribs[i++] = GLX_RGBA;
attribs[i++] = GLX_DOUBLEBUFFER;
attribs[i++] = GLX_RED_SIZE;
attribs[i++] = 1;
attribs[i++] = GLX_GREEN_SIZE;
attribs[i++] = 1;
attribs[i++] = GLX_BLUE_SIZE;
attribs[i++] = 1;
attribs[i++] = GLX_DEPTH_SIZE;
attribs[i++] = 1;
attribs[i++] = None;
screen = DefaultScreen(draw->display);
root = RootWindow(draw->display, screen);
visual = glXChooseVisual(draw->display, screen, attribs);
if(visual == NULL) {
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 (GLX)", "NishBox", None, (char**)NULL, 0, &hints);
draw->wm_delete_window = XInternAtom(draw->display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(draw->display, draw->window, &draw->wm_delete_window, 1);
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(_nb_draw_has_extension(draw, "GLX_EXT_swap_control")) {
unsigned int tmp = -1;
PFNGLXSWAPINTERVALEXTPROC proc = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB("glXSwapIntervalEXT");
if(proc != NULL) {
proc(draw->display, draw->window, 1);
}
glXQueryDrawable(draw->display, draw->window, GLX_SWAP_INTERVAL_EXT, &tmp);
interval = tmp;
} else
#endif
if(_nb_draw_has_extension(draw, "GLX_MESA_swap_control")) {
PFNGLXGETSWAPINTERVALMESAPROC proc = (PFNGLXGETSWAPINTERVALMESAPROC)glXGetProcAddressARB("glXGetSwapIntervalMESA");
PFNGLXSWAPINTERVALMESAPROC proc2 = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddressARB("glXSwapIntervalMESA");
if(proc2 != NULL) {
proc2(1);
}
interval = proc();
} else if(_nb_draw_has_extension(draw, "GLX_SGI_swap_control")) {
PFNGLXSWAPINTERVALSGIPROC proc = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB("glXSwapIntervalSGI");
proc(1);
interval = 1;
}
if(interval > 0) {
nb_function_log("Enabled VSync", "");
}
}
int _nb_draw_step(nb_draw_t* draw) {
int ret = 0;
glXMakeCurrent(draw->display, draw->window, draw->context);
while(XPending(draw->display) > 0) {
XEvent event;
XNextEvent(draw->display, &event);
if(event.type == Expose) {
break;
} else if(event.type == ConfigureNotify) {
draw->x = event.xconfigure.x;
draw->y = event.xconfigure.y;
draw->width = event.xconfigure.width;
draw->height = event.xconfigure.height;
glXMakeCurrent(draw->display, draw->window, draw->context);
nb_draw_reshape(draw);
} else if(event.type == ClientMessage) {
if(event.xclient.data.l[0] == draw->wm_delete_window) {
draw->close = 1;
break;
}
}
}
if(ret == 0) {
nb_draw_frame(draw);
glFlush();
glXSwapBuffers(draw->display, draw->window);
}
return ret;
}
void _nb_draw_destroy(nb_draw_t* draw) {
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);
}
}

View File

@ -1,14 +1,16 @@
TARGET = libnishbox.a
OBJS = version.o core.o draw.o log.o $(BACKEND)_draw.o font.o stb_image.o math.o
OBJS = nb_version.o nb_core.o nb_draw.o nb_log.o nb_font.o nb_math.o nb_physics.o nb_mesh.o nb_model.o
OBJS += nb_$(BACKEND)_draw.o
OBJS += nb_stb_image.o
include ../common.mk
include ext_lua.mk
-include ext_lua.mk
$(TARGET): $(OBJS)
$(AR) rcs $@ $(OBJS)
stb_image.c draw.c: ext_stb_image.h
nb_stb_image.c nb_draw.c: ext_stb_image.h
ext_stb_image.h:
wget https://github.com/nothings/stb/raw/refs/heads/master/stb_image.h -O $@

78
engine/nb_GLFW_draw.c Normal file
View File

@ -0,0 +1,78 @@
#define NB_EXPOSE_DRAW_PLATFORM
#define NB_EXPOSE_DRAW
#include "nb_pre.h"
/* External library */
#include <GL/gl.h>
#include <GL/glu.h>
#include <GLFW/glfw3.h>
/* Interface */
#include "nb_draw_platform.h"
/* NishBox */
#include "nb_log.h"
#include "nb_draw.h"
/* Standard */
#include <string.h>
#include <stdlib.h>
void nb_draw_platform_begin(void) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
}
void nb_draw_platform_end(void) {}
int nb_draw_platform_has_extension(nb_draw_t* draw, const char* query) {
const char* ext = NULL;
const char* ptr;
const int len = strlen(query);
glfwMakeContextCurrent(draw->platform->window);
return glfwExtensionSupported(query);
}
int nb_draw_platform_step(nb_draw_t* draw) {
int ret = 0;
glfwMakeContextCurrent(draw->platform->window);
draw->close = glfwWindowShouldClose(draw->platform->window);
if(draw->close) glfwSetWindowShouldClose(draw->platform->window, GLFW_FALSE);
glfwPollEvents();
if(ret == 0) {
nb_draw_frame(draw);
glFlush();
glfwSwapBuffers(draw->platform->window);
}
return ret;
}
void nb_draw_platform_create(nb_draw_t* draw) {
draw->platform = malloc(sizeof(*draw->platform));
memset(draw->platform, 0, sizeof(*draw->platform));
draw->platform->window = glfwCreateWindow(draw->width, draw->height, "NishBox (GLFW)", NULL, NULL);
if(draw->platform->window == NULL) {
nb_function_log("Failed to create window", "");
nb_draw_destroy(draw);
*pdraw = NULL;
return;
}
glfwMakeContextCurrent(draw->platform->window);
glfwSwapInterval(1);
}
void nb_draw_platform_destroy(nb_draw_t* draw) {
if(draw->platform->window != NULL) {
glfwDestroyWindow(draw->platform->window);
}
free(draw->platform);
}

194
engine/nb_GLX_draw.c Normal file
View File

@ -0,0 +1,194 @@
#define NB_EXPOSE_DRAW_PLATFORM
#define NB_EXPOSE_DRAW
#include "nb_pre.h"
/* External library */
#include <GL/gl.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <GL/glx.h>
#include <GL/glxext.h>
/* Interface */
#include "nb_draw_platform.h"
/* NishBox */
#include "nb_log.h"
#include "nb_draw.h"
/* Standard */
#include <string.h>
#include <stdlib.h>
#ifndef GLX_MESA_swap_control
#define GLX_MESA_swap_control 1
typedef int (*PFNGLXGETSWAPINTERVALMESAPROC)(void);
typedef void (*PFNGLXSWAPINTERVALMESAPROC)(int);
#endif
#ifndef GLX_EXT_swap_control
#define GLX_EXT_swap_control 1
typedef void (*PFNGLXSWAPINTERVALEXTPROC)(Display*, GLXDrawable, int);
#endif
#ifndef GLX_SGI_swap_control
#define GLX_SGI_swap_control 1
typedef void (*PFNGLXSWAPINTERVALSGIPROC)(int);
#endif
void nb_draw_platform_begin(void) {}
void nb_draw_platform_end(void) {}
int nb_draw_platform_has_extension(nb_draw_t* draw, const char* query) {
const char* ext = NULL;
const char* ptr;
const int len = strlen(query);
glXMakeCurrent(draw->platform->display, draw->platform->window, draw->platform->context);
ext = glXQueryExtensionsString(draw->platform->display, DefaultScreen(draw->platform->display));
ptr = strstr(ext, query);
return ((ptr != NULL) && ((ptr[len] == ' ') || (ptr[len] == '\0')));
}
void nb_draw_platform_create(nb_draw_t* draw) {
int i = 0;
int attribs[64];
int screen;
Window root;
XVisualInfo* visual;
XSetWindowAttributes attr;
XSizeHints hints;
int interval = 0;
draw->platform = malloc(sizeof(*draw->platform));
memset(draw->platform, 0, sizeof(*draw->platform));
draw->platform->display = XOpenDisplay(NULL);
if(draw->platform->display == NULL) {
nb_function_log("Failed to open display", "");
nb_draw_destroy(draw);
return;
}
attribs[i++] = GLX_RGBA;
attribs[i++] = GLX_DOUBLEBUFFER;
attribs[i++] = GLX_RED_SIZE;
attribs[i++] = 1;
attribs[i++] = GLX_GREEN_SIZE;
attribs[i++] = 1;
attribs[i++] = GLX_BLUE_SIZE;
attribs[i++] = 1;
attribs[i++] = GLX_DEPTH_SIZE;
attribs[i++] = 1;
attribs[i++] = None;
screen = DefaultScreen(draw->platform->display);
root = RootWindow(draw->platform->display, screen);
visual = glXChooseVisual(draw->platform->display, screen, attribs);
if(visual == NULL) {
nb_function_log("Failed to get visual", "");
nb_draw_destroy(draw);
return;
}
attr.colormap = XCreateColormap(draw->platform->display, root, visual->visual, AllocNone);
attr.event_mask = StructureNotifyMask | ExposureMask;
draw->platform->window = XCreateWindow(draw->platform->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->platform->display, draw->platform->window, &hints);
XSetStandardProperties(draw->platform->display, draw->platform->window, "NishBox (GLX)", "NishBox", None, (char**)NULL, 0, &hints);
draw->platform->wm_delete_window = XInternAtom(draw->platform->display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(draw->platform->display, draw->platform->window, &draw->platform->wm_delete_window, 1);
draw->platform->context = glXCreateContext(draw->platform->display, visual, NULL, True);
if(draw->platform->context == NULL) {
XFree(visual);
nb_function_log("Failed to get OpenGL context", "");
nb_draw_destroy(draw);
return;
}
XFree(visual);
XMapWindow(draw->platform->display, draw->platform->window);
glXMakeCurrent(draw->platform->display, draw->platform->window, draw->platform->context);
#if defined(GLX_EXT_swap_control)
if(nb_draw_platform_has_extension(draw, "GLX_EXT_swap_control")) {
unsigned int tmp = -1;
PFNGLXSWAPINTERVALEXTPROC proc = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB("glXSwapIntervalEXT");
if(proc != NULL) {
proc(draw->platform->display, draw->platform->window, 1);
}
glXQueryDrawable(draw->platform->display, draw->platform->window, GLX_SWAP_INTERVAL_EXT, &tmp);
interval = tmp;
} else
#endif
if(nb_draw_platform_has_extension(draw, "GLX_MESA_swap_control")) {
PFNGLXGETSWAPINTERVALMESAPROC proc = (PFNGLXGETSWAPINTERVALMESAPROC)glXGetProcAddressARB("glXGetSwapIntervalMESA");
PFNGLXSWAPINTERVALMESAPROC proc2 = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddressARB("glXSwapIntervalMESA");
if(proc2 != NULL) {
proc2(1);
}
interval = proc();
} else if(nb_draw_platform_has_extension(draw, "GLX_SGI_swap_control")) {
PFNGLXSWAPINTERVALSGIPROC proc = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB("glXSwapIntervalSGI");
proc(1);
interval = 1;
}
if(interval > 0) {
nb_function_log("Enabled VSync", "");
}
}
int nb_draw_platform_step(nb_draw_t* draw) {
int ret = 0;
glXMakeCurrent(draw->platform->display, draw->platform->window, draw->platform->context);
while(XPending(draw->platform->display) > 0) {
XEvent event;
XNextEvent(draw->platform->display, &event);
if(event.type == Expose) {
break;
} else if(event.type == ConfigureNotify) {
draw->x = event.xconfigure.x;
draw->y = event.xconfigure.y;
draw->width = event.xconfigure.width;
draw->height = event.xconfigure.height;
glXMakeCurrent(draw->platform->display, draw->platform->window, draw->platform->context);
nb_draw_reshape(draw);
} else if(event.type == ClientMessage) {
if(event.xclient.data.l[0] == draw->platform->wm_delete_window) {
draw->close = 1;
break;
}
}
}
if(ret == 0) {
nb_draw_frame(draw);
glFlush();
glXSwapBuffers(draw->platform->display, draw->platform->window);
}
return ret;
}
void nb_draw_platform_destroy(nb_draw_t* draw) {
if(draw->platform->context != NULL) {
glXMakeCurrent(draw->platform->display, None, NULL);
glXDestroyContext(draw->platform->display, draw->platform->context);
}
if(draw->platform->display != NULL) {
XDestroyWindow(draw->platform->display, draw->platform->window);
XCloseDisplay(draw->platform->display);
}
}

View File

@ -4,7 +4,7 @@
/* External library */
#include <GL/gl.h>
#include <windows.h>
#include <GL/wgl.h>
/* Interface */
#include "nb_draw_platform.h"
@ -15,13 +15,15 @@
/* Standard */
#include <string.h>
#include <stdlib.h>
typedef const char*(APIENTRY* PFNWGLGETEXTENSIONSSTRINGARB)(HDC);
typedef BOOL(APIENTRY* PFNWGLSWAPINTERVALPROC)(int);
void _nb_draw_init(void) {}
void nb_draw_platform_begin(void) {}
void nb_draw_platform_end(void) {}
LRESULT CALLBACK _nb_draw_proc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
LRESULT CALLBACK nb_draw_platform_proc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
PAINTSTRUCT ps;
RECT rect;
nb_draw_t* draw = (nb_draw_t*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
@ -36,7 +38,7 @@ LRESULT CALLBACK _nb_draw_proc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
draw->y = rect.top;
draw->width = rect.right - rect.left;
draw->height = rect.bottom - rect.top;
wglMakeCurrent(draw->dc, draw->glrc);
wglMakeCurrent(draw->platform->dc, draw->platform->glrc);
nb_draw_reshape(draw);
break;
case WM_CLOSE:
@ -51,30 +53,30 @@ LRESULT CALLBACK _nb_draw_proc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
return 0;
}
int _nb_draw_has_extension(nb_draw_t* draw, const char* query) {
int nb_draw_platform_has_extension(nb_draw_t* draw, const char* query) {
const char* ext = NULL;
const char* ptr;
const int len = strlen(query);
PFNWGLGETEXTENSIONSSTRINGARB proc;
wglMakeCurrent(draw->dc, draw->glrc);
wglMakeCurrent(draw->platform->dc, draw->platform->glrc);
proc = (PFNWGLGETEXTENSIONSSTRINGARB)wglGetProcAddress("wglGetExtensionsStringARB");
if(proc != NULL) {
ext = proc(draw->dc);
ext = proc(draw->platform->dc);
ptr = strstr(ext, query);
return ((ptr != NULL) && ((ptr[len] == ' ') || (ptr[len] == '\0')));
}
return 0;
}
int _nb_draw_step(nb_draw_t* draw) {
int nb_draw_platform_step(nb_draw_t* draw) {
MSG msg;
int ret = 0;
wglMakeCurrent(draw->dc, draw->glrc);
while(PeekMessage(&msg, draw->window, 0, 0, PM_NOREMOVE)) {
if(GetMessage(&msg, draw->window, 0, 0)) {
wglMakeCurrent(draw->platform->dc, draw->platform->glrc);
while(PeekMessage(&msg, draw->platform->window, 0, 0, PM_NOREMOVE)) {
if(GetMessage(&msg, draw->platform->window, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
@ -86,21 +88,24 @@ int _nb_draw_step(nb_draw_t* draw) {
nb_draw_frame(draw);
glFlush();
SwapBuffers(draw->dc);
SwapBuffers(draw->platform->dc);
}
return ret;
}
void _nb_draw_create(nb_draw_t** pdraw) {
nb_draw_t* draw = *pdraw;
void nb_draw_platform_create(nb_draw_t* draw) {
WNDCLASSEX wc;
PIXELFORMATDESCRIPTOR desc;
PFNWGLSWAPINTERVALPROC wglSwapIntervalEXT;
RECT rect;
int fmt;
DWORD style;
draw->instance = (HINSTANCE)GetModuleHandle(NULL);
if(draw->instance == NULL) {
draw->platform = malloc(sizeof(*draw->platform));
memset(draw->platform, 0, sizeof(*draw->platform));
draw->platform->instance = (HINSTANCE)GetModuleHandle(NULL);
if(draw->platform->instance == NULL) {
nb_function_log("Failed to get instance", "");
nb_draw_destroy(draw);
*pdraw = NULL;
@ -111,7 +116,7 @@ void _nb_draw_create(nb_draw_t** pdraw) {
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = _nb_draw_proc;
wc.lpfnWndProc = nb_draw_platform_proc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = draw->instance;
@ -124,23 +129,21 @@ void _nb_draw_create(nb_draw_t** pdraw) {
if(!RegisterClassEx(&wc)) {
nb_function_log("Failed to register class", "");
nb_draw_destroy(draw);
*pdraw = NULL;
return;
} else {
nb_function_log("Registered class", "");
}
draw->window = CreateWindow("nishbox", "NishBox (WGL)", (WS_OVERLAPPEDWINDOW), draw->x, draw->y, draw->width, draw->height, NULL, 0, draw->instance, NULL);
if(draw->window == NULL) {
draw->platform->window = CreateWindow("nishbox", "NishBox (WGL)", (WS_OVERLAPPEDWINDOW), draw->x, draw->y, draw->width, draw->height, NULL, 0, draw->platform->instance, NULL);
if(draw->platform->window == NULL) {
nb_function_log("Failed to create window", "");
nb_draw_destroy(draw);
*pdraw = NULL;
return;
} else {
nb_function_log("Created window", "");
}
SetWindowLongPtr(draw->window, GWLP_USERDATA, (LONG_PTR)draw);
SetWindowLongPtr(draw->platform->window, GWLP_USERDATA, (LONG_PTR)draw);
memset(&desc, 0, sizeof(desc));
desc.nSize = sizeof(desc);
@ -151,21 +154,20 @@ void _nb_draw_create(nb_draw_t** pdraw) {
desc.cAlphaBits = 8;
desc.cDepthBits = 32;
draw->dc = GetDC(draw->window);
draw->dc = GetDC(draw->platform->window);
fmt = ChoosePixelFormat(draw->dc, &desc);
SetPixelFormat(draw->dc, fmt, &desc);
fmt = ChoosePixelFormat(draw->platform->dc, &desc);
SetPixelFormat(draw->platform->dc, fmt, &desc);
draw->glrc = wglCreateContext(draw->dc);
draw->platform->glrc = wglCreateContext(draw->platform->dc);
if(draw->glrc == NULL) {
nb_function_log("Failed to create OpenGL context", "");
nb_draw_destroy(draw);
*pdraw = NULL;
return;
} else {
nb_function_log("Created OpenGL context", "");
}
wglMakeCurrent(draw->dc, draw->glrc);
wglMakeCurrent(draw->platform->dc, draw->platform->glrc);
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALPROC)wglGetProcAddress("wglSwapIntervalEXT");
if(wglSwapIntervalEXT != NULL) {
@ -174,21 +176,21 @@ void _nb_draw_create(nb_draw_t** pdraw) {
}
SetRect(&rect, 0, 0, draw->width, draw->height);
style = (DWORD)GetWindowLongPtr(draw->window, GWL_STYLE);
style = (DWORD)GetWindowLongPtr(draw->platform->window, GWL_STYLE);
AdjustWindowRect(&rect, style, FALSE);
SetWindowPos(draw->window, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE);
SetWindowPos(draw->platform->window, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE);
ShowWindow(draw->window, SW_NORMAL);
UpdateWindow(draw->window);
ShowWindow(draw->platform->window, SW_NORMAL);
UpdateWindow(draw->platform->window);
}
void _nb_draw_destroy(nb_draw_t* draw) {
if(draw->glrc != NULL) {
void nb_draw_platform_destroy(nb_draw_t* draw) {
if(draw->platform->glrc != NULL) {
wglMakeCurrent(NULL, NULL);
ReleaseDC(draw->window, draw->dc);
wglDeleteContext(draw->glrc);
ReleaseDC(draw->platform->window, draw->platform->dc);
wglDeleteContext(draw->platform->glrc);
}
if(draw->window != NULL) {
DestroyWindow(draw->window);
if(draw->platform->window != NULL) {
DestroyWindow(draw->platform->window);
}
}

View File

@ -15,7 +15,7 @@
/* NishBox */
#include "nb_draw.h"
#include "nb_draw_platform.h"
#include "nb_physics.h"
#include "nb_log.h"
#include "nb_version.h"
@ -36,11 +36,14 @@ void nb_engine_begin(void) {
WSAStartup(MAKEWORD(1, 1), &wsa);
nb_function_log("Winsock ready", "");
#endif
_nb_draw_init();
dInitODE();
nb_draw_begin();
nb_physics_begin();
}
void nb_engine_end(void) { dCloseODE(); }
void nb_engine_end(void) {
nb_physics_end();
nb_draw_end();
}
nb_engine_t* nb_engine_create(int nogui) {
nb_engine_t* engine = malloc(sizeof(*engine));
@ -57,8 +60,7 @@ nb_engine_t* nb_engine_create(int nogui) {
return NULL;
}
}
engine->world = dWorldCreate();
dWorldSetGravity(engine->world, 0, 0, -9.81);
engine->physics = nb_physics_create();
return engine;
}
@ -82,7 +84,7 @@ void nb_engine_loop(nb_engine_t* engine) {
}
void nb_engine_destroy(nb_engine_t* engine) {
dWorldDestroy(engine->world);
if(engine->physics != NULL) nb_physics_destroy(engine->physics);
if(engine->draw != NULL) nb_draw_destroy(engine->draw);
free(engine);
nb_function_log("Destroyed engine", "");

View File

@ -4,25 +4,13 @@
#include <nb_pre.h>
#include <nb_macro.h>
/* External library */
#ifdef NB_EXPOSE_CORE
#include <ode/ode.h>
#endif
/* Type */
#include <nb_type/core.h>
/* NishBox */
#include <nb_draw.h>
/* Standard */
#ifdef NB_EXPOSE_CORE
typedef struct nb_engine {
dWorldID world;
nb_draw_t* draw;
} nb_engine_t;
#else
typedef void nb_engine_t;
#endif
void nb_engine_begin(void);
void nb_engine_end(void);
nb_engine_t* nb_engine_create(int nogui);

View File

@ -26,6 +26,10 @@ GLfloat lightwht[] = {1.0, 1.0, 1.0, 1.0};
GLfloat lightdim[] = {0.2, 0.2, 0.2, 1.0};
GLfloat lightblk[] = {0.0, 0.0, 0.0, 1.0};
void nb_draw_begin(void) { nb_draw_platform_begin(); }
void nb_draw_end(void) { nb_draw_platform_end(); }
nb_draw_t* nb_draw_create(void) {
nb_draw_t* draw = malloc(sizeof(*draw));
memset(draw, 0, sizeof(*draw));
@ -34,8 +38,8 @@ nb_draw_t* nb_draw_create(void) {
draw->width = 640;
draw->height = 480;
draw->running = 0;
_nb_draw_create(&draw);
if(draw != NULL) {
nb_draw_platform_create(draw);
if(draw->platform != NULL) {
nb_function_log("Created drawing interface successfully", "");
nb_draw_init_opengl(draw);
nb_draw_reshape(draw);
@ -46,8 +50,6 @@ nb_draw_t* nb_draw_create(void) {
GLuint nb_test_texture;
void nb_draw_body(nb_draw_t* draw);
void nb_draw_init_opengl(nb_draw_t* draw) {
int i;
int w, h, ch;
@ -118,7 +120,7 @@ void nb_draw_init_opengl(nb_draw_t* draw) {
}
int nb_draw_has_extension(nb_draw_t* draw, const char* query) {
int ret = _nb_draw_has_extension(draw, query);
int ret = nb_draw_platform_has_extension(draw, query);
const char* ext = NULL;
const char* ptr;
const int len = strlen(query);
@ -185,7 +187,7 @@ void nb_draw_frame(nb_draw_t* draw) {
}
int nb_draw_step(nb_draw_t* draw) {
int ret = _nb_draw_step(draw);
int ret = nb_draw_platform_step(draw);
if(ret != 0) return ret;
draw->close = 0;
@ -209,7 +211,6 @@ void nb_draw_destroy(nb_draw_t* draw) {
glDeleteTextures(1, &draw->font[i]);
}
}
_nb_draw_destroy(draw);
free(draw);
nb_draw_platform_destroy(draw);
nb_function_log("Destroyed drawing interface", "");
}

View File

@ -4,31 +4,13 @@
#include <nb_pre.h>
#include <nb_macro.h>
/* External library */
#ifdef NB_EXPORT_DRAW
#include <GL/gl.h>
#endif
/* Type */
#include <nb_type/draw.h>
/* NishBox */
#include <nb_draw_platform.h>
/* Standard */
#ifdef NB_EXPORT_DRAW
typedef struct nb_shape {
GLfloat points[3][3];
GLfloat color[3];
GLuint texture;
} nb_shape_t;
typedef struct nb_mesh {
nb_shape_t* shapes;
int shape_count;
} nb_mesh_t;
#else
typedef void nb_mesh_t;
typedef void nb_shape_t;
#endif
nb_draw_t* nb_draw_create(void);
void nb_draw_destroy(nb_draw_t* draw);
void nb_draw_frame(nb_draw_t* draw);
@ -36,5 +18,7 @@ int nb_draw_step(nb_draw_t* draw);
void nb_draw_reshape(nb_draw_t* draw);
void nb_draw_init_opengl(nb_draw_t* draw);
int nb_draw_has_extension(nb_draw_t* draw, const char* query);
void nb_draw_begin(void);
void nb_draw_end(void);
#endif

View File

@ -4,60 +4,19 @@
#include <nb_pre.h>
#include <nb_macro.h>
/* External library */
#ifdef NB_EXPOSE_DRAW_PLATFORM
#include <GL/gl.h>
#include <GL/glu.h>
#if defined(USE_GLX)
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <GL/glx.h>
#elif defined(USE_WGL)
#include <windows.h>
#elif defined(USE_GLFW)
#include <GLFW/glfw3.h>
#endif
#endif
/* Type */
#include <nb_type/draw_platform.h>
/* NishBox */
#include <nb_type/draw.h>
/* Standard */
#ifdef NB_EXPOSE_DRAW_PLATFORM
typedef struct nb_draw {
#if defined(USE_GLX)
Display* display;
Window window;
GLXContext context;
Atom wm_delete_window;
#elif defined(USE_WGL)
HINSTANCE instance;
HWND window;
HDC dc;
HGLRC glrc;
#elif defined(USE_GLFW)
GLFWwindow* window;
#endif
int close;
int x;
int y;
int width;
int height;
int running;
GLuint font[128];
GLfloat light[4];
GLfloat lookat[3];
GLfloat camera[3];
GLUquadric* quadric;
} nb_draw_t;
#else
typedef void nb_draw_t;
#endif
void _nb_draw_create(nb_draw_t** pdraw);
void _nb_draw_destroy(nb_draw_t* draw);
int _nb_draw_step(nb_draw_t* draw);
int _nb_draw_has_extension(nb_draw_t* draw, const char* query);
void _nb_draw_init(void);
void nb_draw_platform_create(nb_draw_t* draw);
void nb_draw_platform_destroy(nb_draw_t* draw);
int nb_draw_platform_step(nb_draw_t* draw);
int nb_draw_platform_has_extension(nb_draw_t* draw, const char* query);
void nb_draw_platform_begin(void);
void nb_draw_platform_end(void);
#endif

View File

@ -4,7 +4,7 @@
#include <nb_pre.h>
#include <nb_macro.h>
/* External library */
/* Type */
/* NishBox */

View File

@ -4,14 +4,14 @@
#include <nb_pre.h>
#include <nb_macro.h>
/* External library */
/* Type */
/* NishBox */
/* Standard */
void nb_log(const char* fmt, ...);
#define nb_function_log(fmt, arg...) nb_log("%6d %24s: " fmt "\n", __LINE__, __FUNCTION_NAME__, arg)
void nb_log(const char* fmt, ...);
#endif

View File

@ -15,6 +15,30 @@
#define NB_EXPOSE_DRAW_PLATFORM
#endif
#ifndef NB_EXPOSE_MESH
#define NB_EXPOSE_MESH
#endif
#ifndef NB_EXPOSE_MODEL
#define NB_EXPOSE_MODEL
#endif
#ifndef NB_EXPOSE_TEXTURE
#define NB_EXPOSE_TEXTURE
#endif
#ifndef NB_EXPOSE_PHYSICS
#define NB_EXPOSE_PHYSICS
#endif
#ifndef NB_EXPOSE_MATH
#define NB_EXPOSE_MATH
#endif
#ifndef NB_EXPOSE_VERSION
#define NB_EXPOSE_VERSION
#endif
#endif
#ifndef __FUNCTION_NAME__
@ -25,4 +49,15 @@
#endif
#endif
#define NB_DECLARE_TYPE(n, b) typedef struct nb_##n b nb_##n##_t;
/* Expose them by default */
#ifndef NB_EXPOSE_MATH
#define NB_EXPOSE_MATH
#endif
#ifndef NB_EXPOSE_VERSION
#define NB_EXPOSE_VERSION
#endif
#endif

View File

@ -1,5 +1,3 @@
#define NB_EXPOSE_MATH
#include "nb_pre.h"
/* External library */

View File

@ -4,25 +4,17 @@
#include <nb_pre.h>
#include <nb_macro.h>
/* External library */
#ifdef NB_EXPOSE_MATH
#include <GL/gl.h>
#endif
/* Type */
#include <nb_type/math.h>
/* NishBox */
/* Standard */
#ifdef NB_EXPOSE_MATH
typedef GLfloat* nb_vector_t;
#define NB_VECTOR_COPY(from, to) \
to[0] = from[0]; \
to[1] = from[1]; \
to[2] = from[2]
#else
typedef void* nb_vector_t;
#endif
float nb_log2(float x);
nb_vector_t nb_calculate_normal(nb_vector_t v0, nb_vector_t v1, nb_vector_t v2);

13
engine/nb_mesh.c Normal file
View File

@ -0,0 +1,13 @@
#define NB_EXPOSE_MESH
#include "nb_pre.h"
/* External library */
/* Interface */
#include "nb_mesh.h"
/* NishBox */
#include "nb_math.h"
/* Standard */

14
engine/nb_mesh.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __NB_MESH_H__
#define __NB_MESH_H__
#include <nb_pre.h>
#include <nb_macro.h>
/* Type */
#include <nb_type/mesh.h>
/* NishBox */
/* Standard */
#endif

13
engine/nb_model.c Normal file
View File

@ -0,0 +1,13 @@
#define NB_EXPOSE_MODEL
#include "nb_pre.h"
/* External library */
/* Interface */
#include "nb_model.h"
/* NishBox */
#include "nb_math.h"
/* Standard */

14
engine/nb_model.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __NB_MODEL_H__
#define __NB_MODEL_H__
#include <nb_pre.h>
#include <nb_macro.h>
/* Type */
#include <nb_type/model.h>
/* NishBox */
/* Standard */
#endif

32
engine/nb_physics.c Normal file
View File

@ -0,0 +1,32 @@
#define NB_EXPOSE_PHYSICS
#include "nb_pre.h"
/* External library */
#include <ode/ode.h>
/* Interface */
#include "nb_physics.h"
/* NishBox */
#include "nb_log.h"
/* Standard */
#include <stdlib.h>
void nb_physics_begin(void) { dInitODE(); }
void nb_physics_end(void) { dCloseODE(); }
nb_physics_t* nb_physics_create(void) {
nb_physics_t* physics = malloc(sizeof(*physics));
physics->id = dWorldCreate();
dWorldSetGravity(physics->id, 0, 0, -9.81);
return physics;
}
void nb_physics_destroy(nb_physics_t* physics) {
dWorldDestroy(physics->id);
free(physics);
nb_function_log("Destroyed physics", "");
}

19
engine/nb_physics.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __NB_PHYSICS_H__
#define __NB_PHYSICS_H__
#include <nb_pre.h>
#include <nb_macro.h>
/* Type */
#include <nb_type/physics.h>
/* NishBox */
/* Standard */
void nb_physics_begin(void);
void nb_physics_end(void);
nb_physics_t* nb_physics_create(void);
void nb_physics_destroy(nb_physics_t* physics);
#endif

14
engine/nb_texture.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __NB_TEXTURE_H__
#define __NB_TEXTURE_H__
#include <nb_pre.h>
#include <nb_macro.h>
/* Type */
#include <nb_type/texture.h>
/* NishBox */
/* Standard */
#endif

24
engine/nb_type/core.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef __NB_TYPE_CORE_H__
#define __NB_TYPE_CORE_H__
#include <nb_pre.h>
#include <nb_macro.h>
#ifdef NB_EXPOSE_CORE
/* External library */
/* NishBox */
#include <nb_type/physics.h>
#include <nb_type/draw.h>
/* Standard */
NB_DECLARE_TYPE(engine, {
nb_physics_t* physics;
nb_draw_t* draw;
});
#else
typedef void nb_engine_t;
#endif
#endif

33
engine/nb_type/draw.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef __NB_TYPE_DRAW_H__
#define __NB_TYPE_DRAW_H__
#include <nb_pre.h>
#include <nb_macro.h>
#ifdef NB_EXPOSE_DRAW
/* External library */
/* NishBox */
#include <nb_type/draw_platform.h>
/* Standard */
NB_DECLARE_TYPE(draw, {
nb_draw_platform_t* platform;
int close;
int x;
int y;
int width;
int height;
int running;
GLuint font[128];
GLfloat light[4];
GLfloat lookat[3];
GLfloat camera[3];
GLUquadric* quadric;
});
#else
typedef void nb_draw_t;
#endif
#endif

View File

@ -0,0 +1,44 @@
#ifndef __NB_TYPE_DRAW_PLATFORM_H__
#define __NB_TYPE_DRAW_PLATFORM_H__
#include <nb_pre.h>
#include <nb_macro.h>
#ifdef NB_EXPOSE_DRAW_PLATFORM
/* External library */
#include <GL/gl.h>
#include <GL/glu.h>
#if defined(USE_GLX)
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <GL/glx.h>
#elif defined(USE_WGL)
#include <GL/wgl.h>
#elif defined(USE_GLFW)
#include <GLFW/glfw3.h>
#endif
/* NishBox */
/* Standard */
NB_DECLARE_TYPE(draw_platform, {
#if defined(USE_GLX)
Display* display;
Window window;
GLXContext context;
Atom wm_delete_window;
#elif defined(USE_WGL)
HINSTANCE instance;
HWND window;
HDC dc;
HGLRC glrc;
#elif defined(USE_GLFW)
GLFWwindow* window;
#endif
});
#else
typedef void nb_draw_platform_t;
#endif
#endif

19
engine/nb_type/math.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __NB_TYPE_MATH_H__
#define __NB_TYPE_MATH_H__
#include <nb_pre.h>
#include <nb_macro.h>
#ifdef NB_EXPOSE_MATH
/* External library */
/* NishBox */
/* Standard */
typedef double* nb_vector_t;
#else
typedef void* nb_vector_t;
#endif
#endif

29
engine/nb_type/mesh.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef __NB_TYPE_MESH_H__
#define __NB_TYPE_MESH_H__
#include <nb_pre.h>
#include <nb_macro.h>
#ifdef NB_EXPOSE_MESH
/* External library */
/* NishBox */
#include <nb_type/math.h>
/* Standard */
NB_DECLARE_TYPE(shape, {
nb_vector_t points[3];
nb_vector_t color;
});
NB_DECLARE_TYPE(mesh, {
nb_shape_t* shapes;
int shape_count;
});
#else
typedef void nb_mesh_t;
typedef void nb_shape_t;
#endif
#endif

24
engine/nb_type/model.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef __NB_TYPE_MODEL_H__
#define __NB_TYPE_MODEL_H__
#include <nb_pre.h>
#include <nb_macro.h>
#ifdef NB_EXPOSE_MODEL
/* External library */
/* NishBox */
#include <nb_type/mesh.h>
#include <nb_type/texture.h>
/* Standard */
NB_DECLARE_TYPE(model, {
nb_mesh_t* mesh;
nb_texture_t* texture;
});
#else
typedef void nb_model_t;
#endif
#endif

20
engine/nb_type/physics.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef __NB_TYPE_PHYSICS_H__
#define __NB_TYPE_PHYSICS_H__
#include <nb_pre.h>
#include <nb_macro.h>
#ifdef NB_EXPOSE_PHYSICS
/* External library */
#include <ode/ode.h>
/* NishBox */
/* Standard */
NB_DECLARE_TYPE(physics, { dWorldID id; });
#else
typedef void nb_physics_t;
#endif
#endif

20
engine/nb_type/texture.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef __NB_TYPE_TEXTURE_H__
#define __NB_TYPE_TEXTURE_H__
#include <nb_pre.h>
#include <nb_macro.h>
#ifdef NB_EXPOSE_TEXTURE
/* External library */
#include <GL/gl.h>
/* NishBox */
/* Standard */
NB_DECLARE_TYPE(texture, { GLuint id; });
#else
typedef void nb_texture_t;
#endif
#endif

25
engine/nb_type/version.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef __NB_TYPE_VERSION_H__
#define __NB_TYPE_VERSION_H__
#include <nb_pre.h>
#include <nb_macro.h>
#ifdef NB_EXPOSE_VERSION
/* External library */
/* NishBox */
/* Standard */
typedef struct nb_version {
int major;
int minor;
int patch;
char full[64];
char opengl[32];
} nb_version_t;
#else
typedef void nb_version_t;
#endif
#endif

View File

@ -4,20 +4,13 @@
#include <nb_pre.h>
#include <nb_macro.h>
/* External library */
/* Type */
#include <nb_type/version.h>
/* NishBox */
/* Standard */
typedef struct nb_version {
int major;
int minor;
int patch;
char full[64];
char opengl[32];
} nb_version_t;
void nb_get_version(nb_version_t* version);
#endif