Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ project(eglut LANGUAGES CXX)

include(BuildSettings.cmake)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")

set(GAMEWINDOW_SOURCES include/game_window.h include/game_window_manager.h src/game_window_manager.cpp src/game_window_error_handler.cpp src/joystick_manager.cpp)
set(GAMEWINDOW_SOURCES_LINUX_GAMEPAD src/joystick_manager_linux_gamepad.cpp src/joystick_manager_linux_gamepad.h src/window_with_linux_gamepad.cpp src/window_with_linux_gamepad.h)
set(GAMEWINDOW_SOURCES_EGLUT src/window_eglut.h src/window_eglut.cpp src/window_manager_eglut.cpp src/window_manager_eglut.h)
set(GAMEWINDOW_SOURCES_GLFW src/window_glfw.h src/window_glfw.cpp src/window_manager_glfw.cpp src/window_manager_glfw.h src/joystick_manager_glfw.cpp src/joystick_manager_glfw.h)
set(GAMEWINDOW_SOURCES_SDL2 src/window_sdl2.h src/window_sdl2.cpp src/window_manager_sdl2.cpp src/window_manager_sdl2.h)

add_library(gamewindow ${GAMEWINDOW_SOURCES})
target_include_directories(gamewindow PUBLIC include/)
Expand All @@ -18,4 +21,11 @@ if (GAMEWINDOW_SYSTEM STREQUAL "EGLUT")
elseif (GAMEWINDOW_SYSTEM STREQUAL "GLFW")
target_sources(gamewindow PRIVATE ${GAMEWINDOW_SOURCES_GLFW})
target_link_libraries(gamewindow PUBLIC glfw3)
endif()
elseif (GAMEWINDOW_SYSTEM STREQUAL "SDL2")
find_package(EGL REQUIRED)
message(STATUS "Found EGL")
find_package(SDL2 REQUIRED)
message(STATUS "Found SDL2: -I" ${SDL2_INCLUDE_DIRS} " " ${SDL2_LIBRARIES})
target_sources(gamewindow PRIVATE ${GAMEWINDOW_SOURCES_SDL2})
target_link_libraries(gamewindow ${EGL_LIBRARIES} ${SDL2_LIBRARIES})
endif()
25 changes: 25 additions & 0 deletions cmake/FindEGL.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Finds the `egl` library.
# This file is released under the Public Domain.
# Once done this will define
# EGL_FOUND - Set to true if egl has been found
# EGL_INCLUDE_DIRS - The egl include directories
# EGL_LIBRARIES - The libraries needed to use egl

find_package(PkgConfig)
pkg_check_modules(PC_EGL QUIET egl)

find_path(EGL_INCLUDE_DIR
NAMES EGL/egl.h
HINTS ${PC_EGL_INCLUDEDIR} ${PC_EGL_INCLUDE_DIRS})
find_library(EGL_LIBRARY
NAMES EGL libEGL
HINTS ${PC_EGL_LIBDIR} ${PC_EGL_LIBRARY_DIRS})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(EGL DEFAULT_MSG
EGL_LIBRARY EGL_INCLUDE_DIR)

mark_as_advanced(EGL_INCLUDE_DIR EGL_LIBRARY)

set(EGL_LIBRARIES ${EGL_LIBRARY})
set(EGL_INCLUDE_DIRS ${EGL_INCLUDE_DIR})
59 changes: 59 additions & 0 deletions src/window_manager_sdl2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "window_manager_sdl2.h"
#include "window_sdl2.h"
#include <EGL/egl.h>
#include <dlfcn.h>

GameWindowManager::ProcAddrFunc dlsymGetProcAddress(const char* sym) {
if (!sym)
return NULL;
void *eglFunc;

// try official EGL method first
eglFunc = (void*)eglGetProcAddress(sym);
if (eglFunc)
return (GameWindowManager::ProcAddrFunc)eglFunc;

// manual fallback "If the EGL version is not 1.5 or greater, only queries of EGL and client API extension functions will succeed."
void *libEGL;
libEGL = dlopen("libEGL.so", RTLD_LAZY | RTLD_GLOBAL);
if (!libEGL) {
printf("Error: Unable to open libEGL.so for symbol processing");
return NULL;
}

eglFunc = dlsym(libEGL, sym);
dlclose(libEGL);

return (GameWindowManager::ProcAddrFunc)eglFunc;
}

SDL2WindowManager::SDL2WindowManager() {
/*
nothing instantiated yet
just a handle to:
1. access the GL libraries via dlsymGetProcAddress()
2. the future window
*/
}

GameWindowManager::ProcAddrFunc SDL2WindowManager::getProcAddrFunc() {
return (GameWindowManager::ProcAddrFunc) dlsymGetProcAddress;
}

std::shared_ptr<GameWindow> SDL2WindowManager::createWindow(const std::string& title, int width, int height,
GraphicsApi api) {
return std::shared_ptr<GameWindow>(new SDL2GameWindow(title, width, height, api));
}

void SDL2WindowManager::addGamepadMappingFile(const std::string &path) {
printf("Loaded %d more controller mappings from %s\n", SDL_GameControllerAddMappingsFromFile(path.c_str()), path.c_str());
}

void SDL2WindowManager::addGamePadMapping(const std::string &content) {
// NOOP
}

// Define this window manager as the used one
std::shared_ptr<GameWindowManager> GameWindowManager::createManager() {
return std::shared_ptr<GameWindowManager>(new SDL2WindowManager());
}
21 changes: 21 additions & 0 deletions src/window_manager_sdl2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "game_window_manager.h"


GameWindowManager::ProcAddrFunc dlsymGetProcAddress(const char*);


class SDL2WindowManager : public GameWindowManager {

public:
SDL2WindowManager();

ProcAddrFunc getProcAddrFunc() override;

std::shared_ptr<GameWindow> createWindow(const std::string& title, int width, int height, GraphicsApi api) override;

void addGamepadMappingFile(const std::string& path) override;

void addGamePadMapping(const std::string &content) override;
};
Loading