From ff89fc7ca7c2768dd371078dc812f60762360689 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 27 Sep 2017 20:33:18 -0400 Subject: [PATCH] Begin removing SDL Window and OpenGL context creation working. Keyboard, mouse, window events not yet working. --- src/gui/Gui-X.cc | 94 +++++++++++++++++++++++++++++++++++++++++++++++ src/gui/Gui.h | 19 ++++++++++ src/gui/Window.cc | 64 +++++++++++++------------------- src/gui/Window.h | 9 ++++- wscript | 15 +++++--- 5 files changed, 155 insertions(+), 46 deletions(-) create mode 100644 src/gui/Gui-X.cc create mode 100644 src/gui/Gui.h diff --git a/src/gui/Gui-X.cc b/src/gui/Gui-X.cc new file mode 100644 index 0000000..58a0df4 --- /dev/null +++ b/src/gui/Gui-X.cc @@ -0,0 +1,94 @@ +#ifdef GUI_X + +#include "Gui.h" +#include +#include + +static Display * g_display; +static XVisualInfo * g_vi; +static XSetWindowAttributes g_swa; +static GLXContext g_context; + +static Bool WaitForNotify(Display * display, XEvent * event, XPointer arg) +{ + return (event->type == MapNotify) && (event->xmap.window == (Window)arg); +} + +/** + * Initialize the Gui subsystem. + * + * @return true on success, false on failure + */ +bool Gui_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; + + return true; +} + +void * Gui_CreateWindow() +{ + XEvent event; + Window window = XCreateWindow(g_display, + RootWindow(g_display, g_vi->screen), + 0, 0, 800, 800, 0, g_vi->depth, InputOutput, g_vi->visual, + CWBorderPixel | CWColormap | CWEventMask, &g_swa); + XMapWindow(g_display, window); + XIfEvent(g_display, &event, WaitForNotify, (XPointer)window); + if (glXMakeCurrent(g_display, window, g_context) == False) + { + fprintf(stderr, "glXMakeCurrent() failure\n"); + XDestroyWindow(g_display, window); + return nullptr; + } + + return (void *)window; +} + +void Gui_SwapBuffers(void * window) +{ + glXSwapBuffers(g_display, (Window)window); +} + +void Gui_CloseWindow(void * window) +{ + XDestroyWindow(g_display, (Window)window); +} + +void Gui_WaitForEvent(Gui_Event * event) +{ +} + +#endif diff --git a/src/gui/Gui.h b/src/gui/Gui.h new file mode 100644 index 0000000..64835ac --- /dev/null +++ b/src/gui/Gui.h @@ -0,0 +1,19 @@ +#ifndef GUI_H +#define GUI_H + +#include + +#define GUI_EVENT_CLOSE_WINDOW 1u + +typedef struct +{ + uint8_t type; +} Gui_Event; + +bool Gui_Init(); +void * Gui_CreateWindow(); +void Gui_SwapBuffers(void * window); +void Gui_CloseWindow(void * window); +void Gui_WaitForEvent(Gui_Event * event); + +#endif diff --git a/src/gui/Window.cc b/src/gui/Window.cc index 95c6628..219ead0 100644 --- a/src/gui/Window.cc +++ b/src/gui/Window.cc @@ -2,13 +2,16 @@ #include "Window.h" #include "Runtime.h" #include "BufferPane.h" +#include "Gui.h" #include #include "jes_icon-32x32.h" +#include #define INITIAL_WIDTH 800 #define INITIAL_HEIGHT 800 #define FONT_SIZE 16 +#if 0 struct { SDL_TimerID timer_id; @@ -16,32 +19,7 @@ struct bool pressed; bool event_pending; } Key_Statuses[SDL_NUM_SCANCODES]; - -/** - * Initialize SDL. - * - * @retval true SDL was loaded successfully. - * @retval false Loading SDL failed. - */ -static bool Initialize_SDL() -{ - static bool initialized = false; - - if (initialized) - { - return true; - } - - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) - { - return false; - } - - atexit(SDL_Quit); - - initialized = true; - return true; -} +#endif /** * Initialize OpenGL. @@ -80,6 +58,7 @@ static bool Initialize_OpenGL() return true; } +#if 0 void Window::set_window_icon() { SDL_Surface * surface = SDL_CreateRGBSurfaceFrom((void *)jes_icon_32x32, 32, 32, 24, 32 * 3, 0xFF0000u, 0xFF00u, 0xFFu, 0u); @@ -88,6 +67,7 @@ void Window::set_window_icon() SDL_FreeSurface(surface); } +#endif /** * Create a Window. @@ -97,28 +77,22 @@ void Window::set_window_icon() */ bool Window::create(std::shared_ptr buffer) { - if (!Initialize_SDL()) + if (!Gui_Init()) { - std::cerr << "Error initializing SDL" << std::endl; + std::cerr << "Error initializing GUI" << std::endl; return false; } - m_window = SDL_CreateWindow( - APPNAME, - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - INITIAL_WIDTH, - INITIAL_HEIGHT, - SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); + m_window = Gui_CreateWindow(); if (m_window == NULL) { - std::cerr << "Error creating SDL window" << std::endl; + std::cerr << "Error creating window" << std::endl; return false; } +#if 0 set_window_icon(); - - (void)SDL_GL_CreateContext(m_window); +#endif if (!Initialize_OpenGL()) { @@ -169,6 +143,7 @@ bool Window::create(std::shared_ptr buffer) */ void Window::run_event_loop() { +#if 0 SDL_Event event; while ((!m_exit_requested) && SDL_WaitEvent(&event)) @@ -180,8 +155,12 @@ void Window::run_event_loop() redraw(); } } +#endif + redraw(); + sleep(5); } +#if 0 Uint32 Key_Repeat(Uint32 interval, void * param) { if (Key_Statuses[(uintptr_t)param].pressed) @@ -423,10 +402,15 @@ void Window::handle_keyval(uint32_t keyval) break; } } +#endif void Window::resize() { + m_width = 800; + m_height = 800; +#if 0 SDL_GetWindowSize(m_window, &m_width, &m_height); +#endif glViewport(0, 0, m_width, m_height); m_gl->resize(m_width, m_height); int command_buffer_height = m_command_buffer_screen_rows * m_font->get_line_height(); @@ -453,9 +437,10 @@ void Window::redraw() m_gl->draw_rect(0, m_command_buffer_screen_rows * m_font->get_line_height(), m_width, 1, 0.5, 0.5, 0.5, 1.0); m_command_buffer_pane->draw(); - SDL_GL_SwapWindow(m_window); + Gui_SwapBuffers(m_window); } +#if 0 uint32_t Window::get_keyval(SDL_Keycode keysym) { uint32_t keyval = keysym; @@ -560,6 +545,7 @@ uint32_t Window::get_shifted(uint32_t keysym) return 0u; } +#endif void Window::change_focus(std::shared_ptr buffer_pane) { diff --git a/src/gui/Window.h b/src/gui/Window.h index 759d022..8c1aa1c 100644 --- a/src/gui/Window.h +++ b/src/gui/Window.h @@ -3,7 +3,6 @@ #include #include -#include #include "Font.h" #include "Buffer.h" #include "GL.h" @@ -55,19 +54,23 @@ protected: void resize(); void redraw(); +#if 0 void handle_event(SDL_Event & event); void handle_keysym(uint32_t keysym); void handle_keyval(uint32_t keyval); uint32_t get_keyval(SDL_Keycode keysym); uint32_t get_shifted(uint32_t keysym); +#endif void change_focus(std::shared_ptr buffer_pane); +#if 0 void set_window_icon(); +#endif void handle_command(const EncodedString & command); void command_write_file(const CommandParser & cp); void command_quit(const CommandParser & cp); - SDL_Window * m_window; + void * m_window; bool m_exit_requested; bool m_redraw_requested; int m_width; @@ -84,7 +87,9 @@ protected: std::shared_ptr m_command_buffer; std::shared_ptr m_command_buffer_pane; +#if 0 Uint16 m_keymod; +#endif }; #endif diff --git a/wscript b/wscript index 4ac4744..14a418c 100644 --- a/wscript +++ b/wscript @@ -11,8 +11,11 @@ def options(opt): def configure(conf): conf.load("compiler_c compiler_cxx") conf.check(header_name = "getopt.h", global_define = False) - conf.check_cfg(package = "sdl2", args = "--cflags --libs") conf.check_cfg(package = "freetype2", uselib_store = "FreeType2", args = "--cflags --libs") + if platform.system() == "Linux": + conf.check(header_name = "X11/Xlib.h", global_define = False) + elif platform.system() == "Windows": + conf.check(header_name = "windows.h", global_define = False) def build(bld): defines = ['APPNAME="%s"' % APPNAME] @@ -22,9 +25,11 @@ def build(bld): libs = [] if platform.system() == "Linux": defines += ["PLATFORM_LINUX"] - libs += ["dl", "GL"] - elif re.search(r'MINGW', platform.system()): + defines += ["GUI_X"] + libs += ["dl", "GL", "X11"] + elif platform.system() == "Windows": defines += ["PLATFORM_WINDOWS"] + defines += ["GUI_WINDOWS"] libs += ["opengl32"] defines += ['GLCXX_GL_INCLUDE="gl3w.h"'] sources = bld.path.ant_glob(["src/**/*.cc", "src/**/*.c", "libs/glcxx/src/glcxx/*"]) @@ -35,7 +40,7 @@ def build(bld): defines = defines, cxxflags = ["-Wall", "-std=gnu++14", "-O2", "-Wno-switch"], lib = libs, - uselib = ["SDL2", "FreeType2"]) + uselib = ["FreeType2"]) test_libs = libs + [] if platform.system() == "Linux": @@ -56,4 +61,4 @@ def build(bld): lib = test_libs, cxxflags = ["-Wall", "-std=gnu++14", "--coverage", "-Wno-switch", "-include", "iostream"], linkflags = ["--coverage"], - uselib = ["SDL2", "FreeType2"]) + uselib = ["FreeType2"])