add jtk.c

This commit is contained in:
Josh Holtrop 2019-12-16 22:23:02 -05:00
parent 18ed74615a
commit 4e6c392d44
5 changed files with 126 additions and 53 deletions

View File

@ -4,6 +4,7 @@ end
build do build do
main_env = Rscons::Environment.new do |env| main_env = Rscons::Environment.new do |env|
env["LIBS"] += %w[X11 GLX]
env["sources"] = glob("src/**/*.{c,d}") env["sources"] = glob("src/**/*.{c,d}")
env["D_IMPORT_PATH"] += glob("src/**") env["D_IMPORT_PATH"] += glob("src/**")
env.Program("jes", "${sources}") env.Program("jes", "${sources}")

View File

@ -1,53 +0,0 @@
struct Event
{
enum Type : ubyte
{
WINDOW_CLOSE,
WINDOW_EXPOSE,
KEY_PRESS,
KEY_RELEASE,
BUTTON_PRESS,
BUTTON_RELEASE,
TIMER,
WINDOW_RESIZE,
};
struct KeyEvent
{
bool repeat;
uint key;
uint x_keycode;
};
struct ButtonEvent
{
uint mods;
ubyte button;
};
struct TimerEvent
{
uint timer_id;
void * user1;
void * user2;
};
struct WindowResizeEvent
{
uint width;
uint height;
};
Type type;
union
{
KeyEvent key;
ButtonEvent button;
TimerEvent timer;
WindowResizeEvent resize;
};
}
class Window
{
}

54
src/jes/gui/jtk.c Normal file
View File

@ -0,0 +1,54 @@
#include <GL/glx.h>
#include <stdio.h>
#include <stdbool.h>
static Display * g_display;
static XVisualInfo * g_vi;
static XSetWindowAttributes g_swa;
static GLXContext g_context;
/**
* Initialize the Jtk subsystem.
*
* @return true on success, false on failure
*/
bool jtk_init()
{
static int glx_attribute_list[] = {
GLX_RGBA,
GLX_DOUBLEBUFFER,
None,
};
g_display = XOpenDisplay(NULL);
if (g_display == NULL)
{
fprintf(stderr, "XOpenDisplay() failure\n");
return false;
}
g_vi = glXChooseVisual(g_display, DefaultScreen(g_display),
glx_attribute_list);
if (g_vi == NULL)
{
fprintf(stderr, "glXChooseVisual() failure\n");
return false;
}
g_context = glXCreateContext(g_display, g_vi, NULL, True);
if (g_context == NULL)
{
fprintf(stderr, "glXCreateContext() failure\n");
return false;
}
Colormap colormap = XCreateColormap(g_display,
RootWindow(g_display, g_vi->screen), g_vi->visual, AllocNone);
g_swa.colormap = colormap;
g_swa.border_pixel = 0;
g_swa.event_mask = StructureNotifyMask |
KeyPressMask | KeyReleaseMask |
ButtonPressMask | ButtonReleaseMask;
return true;
}

68
src/jes/gui/jtk.d Normal file
View File

@ -0,0 +1,68 @@
private extern(C) bool jtk_init();
struct Jtk
{
struct Event
{
enum Type : ubyte
{
WINDOW_CLOSE,
WINDOW_EXPOSE,
KEY_PRESS,
KEY_RELEASE,
BUTTON_PRESS,
BUTTON_RELEASE,
TIMER,
WINDOW_RESIZE,
};
struct KeyEvent
{
bool repeat;
uint key;
uint x_keycode;
};
struct ButtonEvent
{
uint mods;
ubyte button;
};
struct TimerEvent
{
uint timer_id;
void * user1;
void * user2;
};
struct WindowResizeEvent
{
uint width;
uint height;
};
Type type;
union
{
KeyEvent key;
ButtonEvent button;
TimerEvent timer;
WindowResizeEvent resize;
};
}
class Window
{
}
static bool init()
{
if (!jtk_init())
{
return false;
}
return true;
}
}

View File

@ -1,4 +1,7 @@
import jes.gui.jtk;
int main(string[] args) int main(string[] args)
{ {
Jtk.init();
return 0; return 0;
} }