Skip to content

Commit

Permalink
major changes to gui
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGAzed committed Jul 17, 2024
1 parent 6d83028 commit 8dff3cf
Show file tree
Hide file tree
Showing 14 changed files with 806 additions and 82 deletions.
13 changes: 7 additions & 6 deletions program/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ find_package(GLEW REQUIRED)

add_executable(${PROJECT_NAME} main.c)

add_library(program "")
add_library(program

target_sources(program
PRIVATE
source/gui/graphics.c
source/gui/input.c
source/gui/interface.c
source/gui/draw.c

source/gui/interface/grid.c
source/gui/interface/solver.c
source/gui/interface/biggest_rectangle.c
source/gui/interface/interface.c

source/algorithms/depth_first_search.c
source/algorithms/backtrack.c
source/algorithms/arc_consistency.c
Expand All @@ -23,10 +25,9 @@ target_sources(program
source/instance/settings.c
source/instance/statistics.c
source/instance/expect.c

source/structures/concrete/board.c
source/structures/concrete/state.c

source/slnoslav.c
)

Expand Down
16 changes: 16 additions & 0 deletions program/include/gui/interface/biggest_rectangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef GUI_INTERFACE_BIGGEST_RECTANGLE_H
#define GUI_INTERFACE_BIGGEST_RECTANGLE_H

#include <stdlib.h>

#include <structures/concrete/board.h>

struct nk_rect_array {
struct nk_rect * elements;
size_t size;
};

struct nk_rect_array create_max_overlap_rectangles (board_s board);
void destroy_max_overlap_rectangles(struct nk_rect_array * array);

#endif //GUI_INTERFACE_BIGGEST_RECTANGLE_H
18 changes: 18 additions & 0 deletions program/include/gui/interface/grid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef GUI_INTERFACE_GRID_H
#define GUI_INTERFACE_GRID_H

#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_KEYSTATE_BASED_INPUT
#include <nuklear.h>

#include <structures/concrete/board.h>

void grid(struct nk_context * context, board_s board);

#endif //GUI_INTERFACE_GRID_H
8 changes: 8 additions & 0 deletions program/include/gui/interface/interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef GUI_INTERFACE_H
#define GUI_INTERFACE_H

#include <gui/default.h>

void interface(struct nk_super * super);

#endif /* GUI_INTERFACE_H */
39 changes: 39 additions & 0 deletions program/include/gui/interface/solver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef GUI_INTERFACE_SOLVER_H
#define GUI_INTERFACE_SOLVER_H

#include <gui/default.h>

#include <structures/concrete/state.h>

typedef enum ds_action {
CREATE_DS, DESTROY_DS, NEXT_VALUE_DS, NEXT_VALUE_TIMED_DS,
PREV_VALUE_DS, RESET_DS, CURRENT_VALUE_DS, PREV_VALUE_TIMED_DS,
START_VALUE_DS, END_VALUE_DS,
} ds_action_e;

#define IS_PLAY 1

typedef enum player_state {
PLAY_FORWARDS_E = 0b00 | IS_PLAY,
PLAY_BACKWARDS_E = 0b10 | IS_PLAY,

STOP_UNSET_E = 0b00 << 1,
STOP_START_E = 0b01 << 1,
STOP_MIDDLE_E = 0b10 << 1,
STOP_END_E = 0b11 << 1,
} player_state_e;

typedef enum solver_state {
SOLVE_UNSET_E, SOLVE_RUNNING_E, SOLVE_FINISHED_E,
} solver_state_e;

typedef struct player {
player_state_e play_state;
solver_state_e solve_state;
} player_s;

player_s * get_player_singleton(void);
void solve(struct nk_solver * data);
state_array_s state_provider(const ds_action_e action);

#endif /* GUI_INTERFACE_SOLVER_H */
9 changes: 2 additions & 7 deletions program/include/instance/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
#define INSTANCE_SETTINGS_H

#include <stdbool.h>
#include <stdint.h>

typedef enum playstate {
STOP_E, PLAY_E, PAUSE_E,
} playstate_e;
#include <time.h>

typedef struct settings {
char * filepath;
Expand All @@ -16,8 +12,7 @@ typedef struct settings {
bool is_arc_consistency;
bool is_reduce;

playstate_e state;
uint16_t time;
clock_t time_ms;
} Settings;

Settings * get_settings_singleton(void);
Expand Down
51 changes: 32 additions & 19 deletions program/source/gui/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
#undef NK_GLFW_GL3_IMPLEMENTATION
#include <gui/graphics.h>
#include <gui/input.h>
#include <gui/interface.h>
#include <gui/draw.h>
#include <gui/interface/interface.h>
#include <gui/interface/solver.h>

#include <instance/expect.h>
#include <instance/settings.h>
Expand All @@ -32,31 +33,19 @@ static void error_callback(int number, const char * description) {

void _glew_initialize(void);
void _glfw_initialize(GLFWwindow ** window, int * width, int * height);
struct nk_super _create_super(struct nk_glfw * glfw, GLFWwindow * window);
void _destroy_super(struct nk_super * super);

void gui(void) {
struct nk_glfw glfw = { 0 };
int width = 0, height = 0;
struct nk_super super = { 0 };
static GLFWwindow * window;

_glfw_initialize(&window, &width, &height);
_glew_initialize();
struct nk_super super = _create_super(&glfw, window);

struct nk_context * context = nk_glfw3_init(&glfw, window, NK_GLFW3_INSTALL_CALLBACKS);
struct nk_font_atlas atlas;
struct nk_font_atlas * atlas_ptr = &atlas;
nk_glfw3_font_stash_begin(&glfw, &atlas_ptr);
nk_glfw3_font_stash_end(&glfw);

get_settings_singleton()->filepath = "test/puzzles/medium/5.kkr";
FILE * fp = fopen(get_settings_singleton()->filepath, "rb");
expect(fp, NO_ACTION, "file pointer variable (fp) is NULL (%p): %s", (void*)fp, strerror(errno));
board_s board = create_board(fp);

super.board = &board;
super.context = context;
super.atlas = &glfw.atlas;

solve(&super.solver);
while (!glfwWindowShouldClose(window)) {
input(&glfw);
interface(&super);
Expand All @@ -71,7 +60,7 @@ void _glfw_initialize(GLFWwindow ** window, int * width, int * height) {
glfwSetErrorCallback(error_callback);

error_mode = ASSERT_E;
expect(glfwInit(), NO_ACTION, "GLFW failed to initialize");
expect(glfwInit(), NO_ACTION, "[ERROR] GLFW failed to initialize");

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
Expand All @@ -85,5 +74,29 @@ void _glew_initialize(void) {
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glewExperimental = GL_TRUE;
error_mode = ASSERT_E;
expect(glewInit() == GLEW_OK, NO_ACTION, "Failed to setup GLEW");
expect(glewInit() == GLEW_OK, NO_ACTION, "[ERROR] Failed to setup GLEW");
}

struct nk_super _create_super(struct nk_glfw * glfw, GLFWwindow * window) {
struct nk_context * context = nk_glfw3_init(glfw, window, NK_GLFW3_INSTALL_CALLBACKS);
struct nk_font_atlas atlas;
struct nk_font_atlas * atlas_ptr = &atlas;
nk_glfw3_font_stash_begin(glfw, &atlas_ptr);
nk_glfw3_font_stash_end(glfw);

FILE * fp = fopen(get_settings_singleton()->filepath, "rb");
expect(fp, NO_ACTION, "[ERROR] File pointer variable (fp) is NULL (%p): %s", (void*)fp, strerror(errno));
board_s board = create_board(fp);
fclose(fp);

return (struct nk_super) {
.context = context,
.solver = {
.board = board,
},
};
}

void _destroy_super(struct nk_super * super) {
destroy_board(&super->solver.board);
}
89 changes: 89 additions & 0 deletions program/source/gui/interface/biggest_rectangle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <gui/interface/biggest_rectangle.h>

#include <stdbool.h>

#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_KEYSTATE_BASED_INPUT
#include <nuklear.h>
#include <nuklear_glfw_gl3.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

bool _is_inner_rectangle(struct nk_rect inner, struct nk_rect outer);
bool _is_outer_rectangle(struct nk_rect inner, struct nk_rect outer);
bool _replace_max_rectangle(struct nk_rect_array * array, struct nk_rect guess);

struct nk_rect_array create_max_overlap_rectangles(board_s board) {
ulookup_t row_size = board.game.size[ROW_E], col_size = board.game.size[COLUMN_E];
size_t * matrix_row = calloc(col_size, sizeof(size_t));

struct nk_rect_array array = {
.elements = malloc((((row_size * col_size) / 2) + 1) * sizeof(struct nk_rect)),
.size = 0,
};

for (size_t r = 0; r < row_size; r++) {
for (size_t c = 0; c < col_size; c++) {
matrix_row[c] = board.grid[r][c] == -1 ? matrix_row[c] + 1 : 0;
}

for (size_t c = 0; c < col_size; c++) {
if (matrix_row[c] == 0) continue;
float height = matrix_row[c], width = 0, x = c;

while (matrix_row[c] != 0 && c < col_size) {
height = MIN(height, matrix_row[c]);
width++;
c++;
}

struct nk_rect temp = { .x = x, .y = r - (height - 1), .w = width, .h = height, };
if (!_replace_max_rectangle(&array, temp)) array.elements[array.size++] = temp;
}

for (size_t c = 0; c < col_size; c++) {
if (matrix_row[c] == 0) continue;
struct nk_rect temp = { .x = c, .y = r - (matrix_row[c] - 1), .w = 1, .h = matrix_row[c], };
if (!_replace_max_rectangle(&array, temp)) array.elements[array.size++] = temp;
}
}

return array;
}

void destroy_max_overlap_rectangles(struct nk_rect_array * array) {
free(array->elements);
array->elements = NULL;
array->size = 0;
}

bool _is_inner_rectangle(struct nk_rect inner, struct nk_rect outer) {
return
(inner.x >= outer.x) && (inner.y >= outer.y) &&
((inner.w + inner.x) <= (outer.w + outer.x)) &&
((inner.h + inner.y) <= (outer.h + outer.y));
}

bool _is_outer_rectangle(struct nk_rect inner, struct nk_rect outer) {
return _is_inner_rectangle(outer, inner);
}

bool _replace_max_rectangle(struct nk_rect_array * array, struct nk_rect guess) {
bool replaced = false;
for (size_t s = 0; s < array->size; s++) {
if (_is_inner_rectangle(array->elements[s], guess)) {
array->elements[s] = guess;
replaced = true;
}
if (_is_outer_rectangle(array->elements[s], guess)) replaced = true;
}

return replaced;
}
Loading

0 comments on commit 8dff3cf

Please sign in to comment.