Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RSGL renderer, RGFW backend and GLFW backend #140

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ if(NOT MSVC)
add_subdirectory("examples/clay-official-website")
add_subdirectory("examples/introducing-clay-video-demo")
add_subdirectory("examples/SDL2-video-demo")
add_subdirectory("examples/SDL2-video-demo")
add_subdirectory("examples/RSGL_rendering/GLFW_windowing")
add_subdirectory("examples/RSGL_rendering/RGFW_windowing")
endif()
25 changes: 25 additions & 0 deletions examples/RSGL_rendering/GLFW_windowing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.10)

project(GLFWExample)

set(SRC main.c)

# Set libraries and extensions based on OS
if(WIN32)
set(LIBS -lglfw3 -lgdi32 -lopengl32)
elseif(APPLE)
set(LIBS -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo)
elseif(UNIX)
set(LIBS -lglfw -lGL -lm -ldl -lpthread)
endif()

# Include directories
include_directories(${CMAKE_SOURCE_DIR})

# Add executable
add_executable(main${EXT} ${SRC})

# Link libraries
target_include_directories(main PRIVATE ../../../renderers/RSGL)
target_include_directories(main PRIVATE ../../../)
target_link_libraries(main${EXT} ${LIBS})
86 changes: 86 additions & 0 deletions examples/RSGL_rendering/GLFW_windowing/clay_backend_glfw.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <GLFW/glfw3.h>

#define GLFW_UNUSED(x) (void)(x);

Clay_Vector2 GLFW_mousePosition;

u32 GLFW_mouseLeft = 0;

GLFWkeyfun GLFW_old_key = NULL;
GLFWcharfun GLFW_old_char = NULL;
GLFWwindowsizefun GLFW_old_resize = NULL;
GLFWmousebuttonfun GLFW_old_button = NULL;
GLFWcursorposfun GLFW_old_cursor = NULL;
GLFWscrollfun GLFW_old_scroll = NULL;


void windowSizeCallback(GLFWwindow *window, int width, int height) {
GLFW_UNUSED(window)
Clay_SetLayoutDimensions((Clay_Dimensions) { (float)width, (float)height });

if (GLFW_old_resize != NULL)
GLFW_old_resize(window, width, height);
}

static void charCallback(GLFWwindow *window, unsigned int codepoint) {
GLFW_UNUSED(window)

if (GLFW_old_char != NULL)
GLFW_old_char(window, codepoint);
}


static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
GLFW_UNUSED(window)
GLFW_UNUSED(mods)
GLFW_UNUSED(scancode)


if (GLFW_old_key != NULL)
GLFW_old_key(window, key, scancode, action, mods);

}

static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods) {
GLFW_UNUSED(window)
GLFW_UNUSED(mods)

if (button == GLFW_MOUSE_BUTTON_LEFT)
GLFW_mouseLeft = action == GLFW_PRESS ? 1 : 0;

Clay_SetPointerState(GLFW_mousePosition, GLFW_mouseLeft);
if (GLFW_old_cursor != NULL)
GLFW_old_button(window, button, action, mods);
}

static void mouseCursorPosCallback(GLFWwindow *window, double x, double y) {
GLFW_UNUSED(window);

GLFW_mousePosition = (Clay_Vector2){ (float)x, (float)y };
Clay_SetPointerState(GLFW_mousePosition, GLFW_mouseLeft);
if (GLFW_old_cursor != NULL)
GLFW_old_cursor(window, x, y);
}

static void mouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset) {
GLFW_UNUSED(window)

Clay_UpdateScrollContainers(
false,
(Clay_Vector2) { xoffset, yoffset },
0
);

if (GLFW_old_scroll != NULL)
GLFW_old_scroll(window, xoffset, yoffset);
}

void clay_GLFW_callbackInit(GLFWwindow* window) {
GLFW_old_key = glfwSetKeyCallback(window, keyCallback);
GLFW_old_char = glfwSetCharCallback(window, charCallback);
GLFW_old_resize = glfwSetWindowSizeCallback(window, windowSizeCallback);
GLFW_old_button = glfwSetMouseButtonCallback(window, mouseButtonCallback);
GLFW_old_cursor = glfwSetCursorPosCallback(window, mouseCursorPosCallback);
GLFW_old_scroll = glfwSetScrollCallback(window, mouseScrollCallback);
}
324 changes: 324 additions & 0 deletions examples/RSGL_rendering/GLFW_windowing/main.c

Large diffs are not rendered by default.

Binary file not shown.
38 changes: 38 additions & 0 deletions examples/RSGL_rendering/RGFW_windowing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.10)

project(MyProject)

set(SRC main.c)

# Detect OS
if(WIN32)
set(LIBS -ggdb -lshell32 -lwinmm -lgdi32 -lopengl32)
set(EXT ".exe")
elseif(APPLE)
set(LIBS -lm -framework Foundation -framework AppKit -framework IOKit -framework OpenGL)
set(EXT "")
elseif(UNIX)
set(LIBS -lXrandr -lX11 -lm -lGL -ldl -lpthread)
set(EXT "")
else()
set(detected_OS "Unknown")
endif()

# Settings for emcc
if(CMAKE_C_COMPILER MATCHES "emcc")
set(EXPORTED_JS "-s EXPORTED_RUNTIME_METHODS=['stringToNewUTF8']")
set(LIBS "-s FULL_ES3 -s WASM=1 -s ASYNCIFY -s USE_WEBGL2=1 -s GL_SUPPORT_EXPLICIT_SWAP_CONTROL=1 ${EXPORTED_JS}")
set(LIBS "${LIBS} -s EXPORTED_FUNCTIONS=['_malloc', '_main']")
set(EXT ".js")
set(CMAKE_C_COMPILER "emcc")
set(LIBS "${LIBS} --preload-file ./")
endif()

# Include directories
include_directories(../../../ ../../../renderers/RSGL)

# Add executable
add_executable(main${EXT} ${SRC})

# Link libraries
target_link_libraries(main${EXT} ${LIBS})
Loading