Skip to content

Commit

Permalink
trying to add gui support
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGAzed committed Jun 27, 2024
1 parent 15076a2 commit 767ffb4
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 22 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/cmake-single-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,4 @@ jobs:
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: |
pwd
xxd ../test/puzzles/easy/1.kkr
ls -l ../test/puzzles/easy/
ctest -C ${{env.BUILD_TYPE}} --rerun-failed --output-on-failure
run: ctest -C ${{env.BUILD_TYPE}} --rerun-failed --output-on-failure
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "external/glfw"]
path = external/glfw
url = [email protected]:glfw/glfw.git
[submodule "external/glew"]
path = external/glew
url = [email protected]:Perlmint/glew-cmake.git
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ project(SLNOSLAV
if (WIN32)
add_compile_options(/W4)
else()
add_compile_options(-std=c17 -pg -Wall -Wextra -Wpedantic)
set(CMAKE_CXX_FLAGS "-std=c17 -Wall -Wextra -Wpedantic -Wnodiscarded-qualifiers")
set(CMAKE_CXX_FLAGS_DEBUG "-g -pg")
set(CMAKE_CXX_FLAGS_RELEASE "-03")
endif()

add_subdirectory(external)
Expand Down
8 changes: 5 additions & 3 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ add_library(GREATEST_LIB INTERFACE "")

target_include_directories(GREATEST_LIB INTERFACE greatest)

add_library(NUKLEAR_LIB INTERFACE "")
add_library(nuklear INTERFACE "")

set_target_properties(NUKLEAR_LIB PROPERTIES LINKER_LANGUAGE C)
target_include_directories(nuklear INTERFACE nuklear nuklear/demo/glfw_opengl3)

target_include_directories(NUKLEAR_LIB INTERFACE nuklear/src)
find_package(OpenGL REQUIRED)

if (!WIN32)
find_package(PkgConfig REQUIRED)
Expand All @@ -15,6 +15,8 @@ if (!WIN32)
find_package(Doxygen)
endif()

add_subdirectory(glew)

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
Expand Down
1 change: 1 addition & 0 deletions external/glew
Submodule glew added at a5494d
5 changes: 4 additions & 1 deletion program/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ add_library(PROGRAM_LIBRARY "")

target_sources(PROGRAM_LIBRARY
PRIVATE
source/gui/graphics.c

source/algorithms/depth_first_search.c
source/algorithms/backtrack.c
source/algorithms/arc_consistency.c
Expand All @@ -21,5 +23,6 @@ target_sources(PROGRAM_LIBRARY
)

target_include_directories(PROGRAM_LIBRARY PUBLIC include)
target_link_libraries(PROGRAM_LIBRARY PRIVATE glfw libglew_shared nuklear)

target_link_libraries(${PROJECT_NAME} PROGRAM_LIBRARY)
target_link_libraries(${PROJECT_NAME} PRIVATE PROGRAM_LIBRARY)
9 changes: 9 additions & 0 deletions program/include/gui/graphics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef GUI_GRAPHICS_H
#define GUI_GRAPHICS_H

#define WINDOW_WIDTH 200
#define WINDOW_HEIGHT 250

void gui(void);

#endif /* GUI_GRAPHICS_H */
11 changes: 0 additions & 11 deletions program/include/gui/screen.h

This file was deleted.

3 changes: 2 additions & 1 deletion program/include/instance/expect.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <assert.h>

typedef enum error_mode_type {
DEFAULT_E, ABORT_E, ASSERT_E,
DEFAULT_E, ABORT_E, ASSERT_E, EXIT_E,
} error_mode_e;

static error_mode_e error_mode = DEFAULT_E;
Expand Down Expand Up @@ -41,6 +41,7 @@ static FILE * error_log = NULL;
switch (error_mode) { \
case ABORT_E : { error_action; abort(); } \
case ASSERT_E : { error_action; assert(0 && assertion); } \
case EXIT_E : { error_action; exit(EXIT_FAILURE); } \
default : { error_action; } \
} \
} \
Expand Down
104 changes: 104 additions & 0 deletions program/source/gui/graphics.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <gui/graphics.h>

#include <GL/glew.h>

#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_GLFW_GL3_IMPLEMENTATION
#define NK_KEYSTATE_BASED_INPUT
#include <nuklear.h>
#include <nuklear_glfw_gl3.h>

#include <instance/expect.h>

static void error_callback(int number, const char * description) {
error_mode = DEFAULT_E;
expect(NULL, NO_ACTION, "Error %d: %s\n", number, description);
}

void _glfw_initialize_window(GLFWwindow * window);
void _glew_initialize(void);
void _font_stash(struct nk_glfw * glfw);

void _input(struct nk_glfw * glfw);
void _interface(struct nk_context * context);
void _draw(struct nk_glfw * glfw, GLFWwindow * window);

void gui(void) {
static GLFWwindow * window;
_glfw_initialize_window(window);
struct nk_glfw glfw = { 0 };
struct nk_context * context = nk_glfw3_init(&glfw, window, NK_GLFW3_INSTALL_CALLBACKS);

_glew_initialize();
_font_stash(&glfw);

while (!glfwWindowShouldClose(window)) {
_input(&glfw);
_interface(context);
_draw(&glfw, window);
}

nk_glfw3_shutdown(&glfw);
glfwTerminate();
}

void _glfw_initialize_window(GLFWwindow * window) {
error_mode = EXIT_E;
expect(window, NO_ACTION, "GLFWwindow pointer is NULL (%p)", (void*)window);
expect(glfwInit(), NO_ACTION, "GLFW failed to initialize");

glfwSetErrorCallback(error_callback);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
glfwMakeContextCurrent(window);
}

void _glew_initialize(void) {
glewExperimental = GL_TRUE;
error_mode = EXIT_E;
expect(glewInit() == GLEW_OK, NO_ACTION, "GLEW setup failed");
}

void _font_stash(struct nk_glfw * glfw) {
struct nk_font_atlas *atlas;
nk_glfw3_font_stash_begin(glfw, &atlas);
nk_glfw3_font_stash_end(glfw);
}

void _input(struct nk_glfw * glfw) {
glfwPollEvents();
nk_glfw3_new_frame(glfw);
}

void _interface(struct nk_context * context) {
if (nk_begin(context, "Anything window", nk_rect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT), NK_WINDOW_BORDER)) {
nk_layout_row_dynamic(context, 120, 1);
nk_label(context, "Hello world!", NK_TEXT_CENTERED);

nk_layout_row_dynamic(context, 50, 1);
nk_label(context, "Hello world!", NK_TEXT_LEFT);

nk_layout_row_static(context, 30, 80, 1);
if (nk_button_label(context, "AnyButton")) fprintf(stdout, "button pressed\n");
}
nk_end(context);
}

void _draw(struct nk_glfw * glfw, GLFWwindow * window) {
glViewport(0, 0, WINDOW_HEIGHT, WINDOW_WIDTH);
glClear(GL_COLOR_BUFFER_BIT);
nk_glfw3_render(glfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
glfwSwapBuffers(window);
}
Empty file removed program/source/gui/screen.c
Empty file.

0 comments on commit 767ffb4

Please sign in to comment.