Skip to content

Commit

Permalink
Web: Implement SDL2 window backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
geneotech committed Feb 26, 2024
1 parent 775e111 commit 28c9475
Show file tree
Hide file tree
Showing 15 changed files with 438 additions and 39 deletions.
27 changes: 23 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -853,17 +853,21 @@ if(BUILD_WINDOW_FRAMEWORK)
"src/augs/window_framework/window_explorer_utils.cpp"
)

if(USE_GLFW)
if(USE_SDL2)
message("Using SDL as the window framework.")

list(APPEND HYPERSOMNIA_CODEBASE_CPPS
"src/augs/window_framework/window_sdl2.cpp"
)
elseif(USE_GLFW)
message("Using GLFW as the window framework.")

list(APPEND HYPERSOMNIA_CODEBASE_CPPS
"src/augs/window_framework/window_glfw.cpp"
"src/augs/window_framework/translate_glfw_enums.cpp"
)
elseif(UNIX)
list(APPEND HYPERSOMNIA_CODEBASE_CPPS
"src/augs/window_framework/window_x.cpp"
"src/augs/window_framework/translate_x_enums.cpp"
"src/augs/window_framework/print_x_devices.cpp"
)
endif()
Expand Down Expand Up @@ -1625,6 +1629,10 @@ if(BUILD_WINDOW_FRAMEWORK)
add_definitions(-DBUILD_WINDOW_FRAMEWORK=1)
endif()

if(USE_SDL2)
add_definitions(-DUSE_SDL2=1)
endif()

if(USE_GLFW)
add_definitions(-DUSE_GLFW=1)
endif()
Expand Down Expand Up @@ -1796,7 +1804,18 @@ if(BUILD_OPENGL)
endif()

if(BUILD_WINDOW_FRAMEWORK)
if(USE_GLFW)
if(USE_SDL2)
# Find the SDL2 package
find_package(SDL2 REQUIRED)

# Include the SDL2 directories
include_directories(${SDL2_INCLUDE_DIRS})

# Link against the SDL2 libraries
list(APPEND HYPERSOMNIA_LIBS ${SDL2_LIBRARIES})

message("Found SDL2 library: ${SDL2_LIBRARIES}")
elseif(USE_GLFW)
message("Linking GLFW statically.")

prefer_static_libraries()
Expand Down
4 changes: 3 additions & 1 deletion src/augs/graphics/OpenGL_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#if BUILD_OPENGL
#include <glad/glad.h>

#if USE_GLFW
#if USE_SDL2
#include <SDL2/SDL.h>
#elif USE_GLFW
#include <GLFW/glfw3.h>
#elif PLATFORM_WINDOWS
#include <GL/GL.h>
Expand Down
14 changes: 10 additions & 4 deletions src/augs/graphics/renderer_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,32 @@ namespace augs {
renderer_backend::renderer_backend() : platform(std::make_unique<renderer_backend::platform_data>()) {
augs::window::get_current().check_current_context();

#if BUILD_OPENGL

const char* fname = "gladLoadGL";
#if USE_GLFW
#if USE_GLFW || USE_SDL2
fname = "gladLoadGLLoader";
#endif

#if BUILD_OPENGL
LOG("Calling %x.", fname);

#if USE_GLFW
#if USE_SDL2
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) {
#elif USE_GLFW
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
#else
if (!gladLoadGL()) {
#endif
LOG("Calling %x failed.", fname);
throw renderer_error("Failed to initialize GLAD!");
}
#endif

GL_CHECK(LOG("GL Version: %x", glGetString(GL_VERSION)));

LOG("Calling %x succeeded.", fname);
#else
LOG("OpenGL was not built. Nothing to init in the renderer backend.");
#endif

augs::window::get_current().check_current_context();

Expand Down
1 change: 1 addition & 0 deletions src/augs/window_framework/explorer_utils_winapi.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "augs/misc/scope_guard.h"
#include <Windows.h>
#include <shlobj.h>
#undef min
Expand Down
2 changes: 0 additions & 2 deletions src/augs/window_framework/platform_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace augs {
#if BUILD_WINDOW_FRAMEWORK

#if USE_GLFW
#include <GLFW/glfw3.h>

namespace augs {
bool set_display(const vec2i, const int) {
return true;
Expand Down
6 changes: 0 additions & 6 deletions src/augs/window_framework/translate_glfw_enums.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if BUILD_WINDOW_FRAMEWORK && USE_GLFW
#include <GLFW/glfw3.h>
#include "augs/window_framework/translate_glfw_enums.h"

using namespace augs::event;
using namespace augs::event::keys;
Expand Down Expand Up @@ -147,4 +145,3 @@ key translate_glfw_key(const int m) {
default: return key::INVALID;
}
}
#endif
111 changes: 111 additions & 0 deletions src/augs/window_framework/translate_sdl2_enums.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using namespace augs::event;
using namespace augs::event::keys;

augs::event::keys::key translate_sdl2_mouse_key(const int button) {
switch (button) {
case SDL_BUTTON_LEFT: return key::LMOUSE;
case SDL_BUTTON_RIGHT: return key::RMOUSE;
case SDL_BUTTON_MIDDLE: return key::MMOUSE;
case SDL_BUTTON_X1: return key::MOUSE4;
case SDL_BUTTON_X2: return key::MOUSE5;
default: return key::INVALID;
}
}

key translate_sdl2_key(const int sym) {
switch (sym) {
case SDLK_CAPSLOCK: return key::CAPSLOCK;

case SDLK_LCTRL: return key::LCTRL;
case SDLK_RCTRL: return key::RCTRL;

case SDLK_LALT: return key::LALT;
case SDLK_RALT: return key::RALT;

case SDLK_LSHIFT: return key::LSHIFT;
case SDLK_RSHIFT: return key::RSHIFT;

case SDLK_LGUI: return key::LWIN;
case SDLK_RGUI: return key::RWIN;

case SDLK_BACKSPACE: return key::BACKSPACE;
case SDLK_TAB: return key::TAB;
case SDLK_RETURN: return key::ENTER;
case SDLK_PAUSE: return key::PAUSE;
case SDLK_ESCAPE: return key::ESC;
case SDLK_SPACE: return key::SPACE;
case SDLK_PAGEUP: return key::PAGEUP;
case SDLK_PAGEDOWN: return key::PAGEDOWN;
case SDLK_END: return key::END;
case SDLK_HOME: return key::HOME;
case SDLK_LEFT: return key::LEFT;
case SDLK_UP: return key::UP;
case SDLK_RIGHT: return key::RIGHT;
case SDLK_DOWN: return key::DOWN;
case SDLK_PRINTSCREEN: return key::PRINTSCREEN;
case SDLK_INSERT: return key::INSERT;
case SDLK_DELETE: return key::DEL;

case SDLK_KP_MULTIPLY: return key::MULTIPLY;
case SDLK_KP_PLUS: return key::ADD;
case SDLK_KP_MINUS: return key::SUBTRACT;
case SDLK_KP_DECIMAL: return key::DECIMAL;
case SDLK_KP_DIVIDE: return key::DIVIDE;

case SDLK_F1: return key::F1;
case SDLK_F2: return key::F2;
case SDLK_F3: return key::F3;
case SDLK_F4: return key::F4;
case SDLK_F5: return key::F5;
case SDLK_F6: return key::F6;
case SDLK_F7: return key::F7;
case SDLK_F8: return key::F8;
case SDLK_F9: return key::F9;
case SDLK_F10: return key::F10;
case SDLK_F11: return key::F11;
case SDLK_F12: return key::F12;
// Add more F-keys if your application uses them

case SDLK_a: return key::A;
case SDLK_b: return key::B;
case SDLK_c: return key::C;
case SDLK_d: return key::D;
case SDLK_e: return key::E;
case SDLK_f: return key::F;
case SDLK_g: return key::G;
case SDLK_h: return key::H;
case SDLK_i: return key::I;
case SDLK_j: return key::J;
case SDLK_k: return key::K;
case SDLK_l: return key::L;
case SDLK_m: return key::M;
case SDLK_n: return key::N;
case SDLK_o: return key::O;
case SDLK_p: return key::P;
case SDLK_q: return key::Q;
case SDLK_r: return key::R;
case SDLK_s: return key::S;
case SDLK_t: return key::T;
case SDLK_u: return key::U;
case SDLK_v: return key::V;
case SDLK_w: return key::W;
case SDLK_x: return key::X;
case SDLK_y: return key::Y;
case SDLK_z: return key::Z;

case SDLK_0: return key::_0;
case SDLK_1: return key::_1;
case SDLK_2: return key::_2;
case SDLK_3: return key::_3;
case SDLK_4: return key::_4;
case SDLK_5: return key::_5;
case SDLK_6: return key::_6;
case SDLK_7: return key::_7;
case SDLK_8: return key::_8;
case SDLK_9: return key::_9;

// Add any other keys you need

default: return key::INVALID;
}
}
6 changes: 0 additions & 6 deletions src/augs/window_framework/translate_x_enums.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#if BUILD_WINDOW_FRAMEWORK
#define XK_MISCELLANY
#define XK_LATIN1
#define XK_3270
#include <X11/keysymdef.h>

#include "augs/window_framework/translate_x_enums.h"

using namespace augs::event;
using namespace augs::event::keys;

Expand Down Expand Up @@ -156,4 +153,3 @@ key translate_keysym(const xcb_keysym_t m) {
default: return key::INVALID;
}
}
#endif
2 changes: 1 addition & 1 deletion src/augs/window_framework/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace augs {
friend glfw_callbacks;
#endif

#if PLATFORM_WINDOWS || USE_GLFW
#if PLATFORM_WINDOWS || USE_GLFW || USE_SDL2
public:
struct platform_data;
private:
Expand Down
1 change: 1 addition & 0 deletions src/augs/window_framework/window_explorer_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "augs/filesystem/file.h"
#include "augs/log.h"
#include "augs/window_framework/shell.h"
#include "augs/window_framework/platform_utils.h"

#if PLATFORM_WINDOWS
#include "augs/window_framework/explorer_utils_winapi.hpp"
Expand Down
16 changes: 8 additions & 8 deletions src/augs/window_framework/window_glfw.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include <queue>
#include <GLFW/glfw3.h>
#include "augs/misc/scope_guard.h"

#include "augs/window_framework/event.h"
#include "augs/window_framework/window.h"
#include "augs/window_framework/translate_glfw_enums.h"
#include "augs/log.h"
#include "augs/window_framework/shell.h"
#include "augs/filesystem/file.h"
#include "augs/window_framework/platform_utils.h"
#include "all_paths.h"

augs::event::keys::key translate_glfw_key(int);
augs::event::keys::key translate_glfw_mouse_key(int);

struct unhandled_key {
int key, scancode, action, mods;
Expand Down Expand Up @@ -132,6 +131,7 @@ namespace augs {

glfwWindowHint(GLFW_SAMPLES, 0);
glfwWindowHint(GLFW_DEPTH_BITS, 0);
glfwWindowHint(GLFW_STENCIL_BITS, 8);
glfwWindowHint(GLFW_DECORATED, settings.border);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
Expand Down Expand Up @@ -343,7 +343,7 @@ namespace augs {

void window::set_window_rect(const xywhi r) {
auto& window = platform->window;
LOG("SETTING WINDOW RECT: %x", r);
LOG("SETTING WINDOW RECT: %x", r);

glfwSetWindowPos(window, r.x, r.y);
glfwSetWindowSize(window, r.w, r.h);
Expand Down Expand Up @@ -466,4 +466,4 @@ namespace augs {
}
}


#include "augs/window_framework/translate_glfw_enums.hpp"
Loading

0 comments on commit 28c9475

Please sign in to comment.