diff options
author | 2025-05-14 23:07:48 +0200 | |
---|---|---|
committer | 2025-05-14 23:07:48 +0200 | |
commit | 5c726aea53703620aa4a44cfe3c16bcb2d4cc06a (patch) | |
tree | 37c209bdf2047586784888e4f57abbc5e88e52f6 | |
parent | c41f692089ec680ca9ef2436d90ec3c599aeb86d (diff) | |
download | tris-5c726aea53703620aa4a44cfe3c16bcb2d4cc06a.tar.gz tris-5c726aea53703620aa4a44cfe3c16bcb2d4cc06a.zip |
Add UI management
-rw-r--r-- | src/main/ui/ui.h | 24 | ||||
-rw-r--r-- | src/main/ui/x11/ui.c | 88 |
2 files changed, 112 insertions, 0 deletions
diff --git a/src/main/ui/ui.h b/src/main/ui/ui.h new file mode 100644 index 0000000..a98729e --- /dev/null +++ b/src/main/ui/ui.h @@ -0,0 +1,24 @@ +/*- + * This file is part of Tris Game. + * + * Tris Game is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tris Game is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tris Game. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef __UI_H__ +#define __UI_H__ + +void ui_init(void); +void ui_loop(void (*draw_frame)()); + +#endif /* __UI_H__ */ diff --git a/src/main/ui/x11/ui.c b/src/main/ui/x11/ui.c new file mode 100644 index 0000000..349f49c --- /dev/null +++ b/src/main/ui/x11/ui.c @@ -0,0 +1,88 @@ +/*- + * This file is part of Tris Game. + * + * Tris Game is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tris Game is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tris Game. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <GL/gl.h> +#include <GL/glx.h> +#include <X11/Xlib.h> + +static Display *display; +static Window window; +static GLXContext gl_context; + +void cleanup(void); + +void +ui_init() +{ + display = XOpenDisplay(NULL); + if (!display) { + fprintf(stderr, "Error: Can't open X11 display\n"); + exit(1); + } + + int screen = DefaultScreen(display); + Window root = RootWindow(display, screen); + + GLint attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; + XVisualInfo *vi = glXChooseVisual(display, screen, attribs); + + if (!vi) { + fprintf(stderr, "Error: No compatible Visual found\n"); + exit(1); + } + + Colormap colormap = XCreateColormap(display, root, vi->visual, AllocNone); + XSetWindowAttributes swa; + swa.colormap = colormap; + swa.event_mask = ExposureMask | KeyPressMask; + + window = XCreateWindow(display, root, 0, 0, 640, 480, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa); + XMapWindow(display, window); + + XStoreName(display, window, "Tris game"); + + gl_context = glXCreateContext(display, vi, NULL, GL_TRUE); + glXMakeCurrent(display, window, gl_context); +} + +void +ui_loop(void (*draw_frame)()) +{ + XEvent event; + while (1) { + XNextEvent(display, &event); + if (event.type == Expose) { + draw_frame(); + glXSwapBuffers(display, window); + } + if (event.type == KeyPress) { + break; + } + } + + cleanup(); +} + +void +cleanup(void) { + glXMakeCurrent(display, None, NULL); + glXDestroyContext(display, gl_context); + XDestroyWindow(display, window); + XCloseDisplay(display); +} |