/*- * Copyright (C) 2025 Alessandro Iezzi * * 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 . */ #include #include #include #include #include #include "../../util.h" static Display *display; static Window window; static GLXContext gl_context; void cleanup(void); static Display * ui_open_display(void) { Display *display = XOpenDisplay(NULL); if (!display) { log_error("Can't open X11 display"); exit(1); } return display; } static XVisualInfo * gl_choose_visual(int screen) { GLint attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; XVisualInfo *vi = glXChooseVisual(display, screen, attribs); if (!vi) { log_error("No compatible Visual found"); exit(1); } return vi; } static XSetWindowAttributes ui_get_attributes(Window root, XVisualInfo *vi) { Colormap colormap = XCreateColormap(display, root, vi->visual, AllocNone); XSetWindowAttributes swa; swa.colormap = colormap; swa.event_mask = ExposureMask | KeyPressMask; return swa; } static Window ui_create_window(Window root, XVisualInfo *vi, int width, int height) { XSetWindowAttributes swa = ui_get_attributes(root, vi); Window window = XCreateWindow(display, root, 0, 0, width, width, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa); XMapWindow(display, window); return window; } static void ui_set_title(Display *display, Window window, const char *title) { XStoreName(display, window, title); } void ui_init() { display = ui_open_display(); int screen = DefaultScreen(display); Window root = RootWindow(display, screen); XVisualInfo *vi = gl_choose_visual(screen); window = ui_create_window(root, vi, 640, 480); ui_set_title(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); }