diff --git a/Makefile b/Makefile index d402e7d..c99756f 100755 --- a/Makefile +++ b/Makefile @@ -89,18 +89,18 @@ yuginec = ./source/engine/yugine.c ENGINE = $(BIN)libengine.a INCLUDE = $(BIN)include -LINK = $(LIBPATH) $(LINKER_FLAGS) $(LELIBS) -o $@ +LINK = $(LIBPATH) $(LINKER_FLAGS) $(LELIBS) engine: $(yuginec:.%.c=$(objprefix)%.o) $(ENGINE) @echo Linking engine - $(CLINK) $< $(LINK) + $(CLINK) $< $(LINK) -o $@ @echo Finished build bs: engine cp engine brainstorm -ed: engine - cp engine editor +pin: engine + cp engine pinball $(ENGINE): $(eobjects) bin/libglfw3.a @echo Making library engine.a diff --git a/editor/camera.rb b/editor/camera.rb deleted file mode 100644 index e69de29..0000000 diff --git a/editor/game.rb b/editor/game.rb deleted file mode 100644 index 27f3838..0000000 --- a/editor/game.rb +++ /dev/null @@ -1,10 +0,0 @@ -window = win_make("editor") -nuke_cb(window, :editor) - -def editor - editor_render() -end - -def update(dt) - -end \ No newline at end of file diff --git a/source/engine/editor.c b/source/engine/editor.c old mode 100755 new mode 100644 index c3d1445..d6603e2 --- a/source/engine/editor.c +++ b/source/engine/editor.c @@ -1,49 +1,46 @@ #include "nuke.h" -#include "openglrender.h" -#include "editor.h" -#include "window.h" -#include "resources.h" -#include "registry.h" -#include "datastream.h" -#include "gameobject.h" +#include "2dphysics.h" #include "camera.h" +#include "config.h" +#include "datastream.h" +#include "debug.h" +#include "debugdraw.h" +#include "editor.h" +#include "editorstate.h" +#include "gameobject.h" +#include "input.h" +#include "level.h" +#include "math.h" +#include "openglrender.h" +#include "registry.h" +#include "resources.h" +#include "script.h" #include "shader.h" +#include "sound.h" +#include "sprite.h" +#include "texture.h" +#include "vec.h" +#include "window.h" +#include +#include #include #include -#include "editorstate.h" #include -#include "input.h" -#include "2dphysics.h" -#include "debugdraw.h" -#include "level.h" -#include "texture.h" -#include "sprite.h" -#include -#include "math.h" -#include -#include "config.h" -#include "vec.h" -#include "debug.h" -#include -#include "script.h" -#include "sound.h" - #define __USE_XOPEN_EXTENDED 1 #include "ftw.h" - #include -#define ASSET_TEXT_BUF 1024*1024 /* 1 MB buffer for editing text files */ +#define ASSET_TEXT_BUF 1024 * 1024 /* 1 MB buffer for editing text files */ -struct gameproject *cur_project; -struct vec *projects; +struct gameproject *cur_project = NULL; +struct vec *projects = NULL; static char setpath[MAXPATH]; // Menus // TODO: Pack this into a bitfield -static struct editorVars editor = { 0 }; +static struct editorVars editor = {0}; // Lighting effect flags static bool renderAO = true; @@ -54,18 +51,18 @@ static bool renderGizmos = false; static bool showGrid = true; static bool debugDrawPhysics = false; -const char *allowed_extensions[] = { "jpg", "png", "gltf", "glsl" }; +const char *allowed_extensions[] = {"jpg", "png", "gltf", "glsl"}; static const char *editor_filename = "editor.ini"; struct asset { - char *key; - struct fileasset *value; + char *key; + struct fileasset *value; }; static struct asset *assets = NULL; -static char asset_search_buffer[100] = { 0 }; +static char asset_search_buffer[100] = {0}; struct fileasset *selected_asset; @@ -79,1144 +76,950 @@ static bool grid1_draw = true; static bool grid2_draw = true; static float tex_scale = 1.f; -static struct TexAnimation tex_gui_anim = { 0 }; +static struct TexAnimation tex_gui_anim = {0}; -char current_level[MAXNAME] = { '\0' }; -char levelname[MAXNAME] = { '\0' }; +char current_level[MAXNAME] = {'\0'}; +char levelname[MAXNAME] = {'\0'}; static struct vec *levels = NULL; static const int ASSET_WIN_SIZE = 512; -static const char *get_extension(const char *filepath) -{ - return strrchr(filepath, '.'); +static const char *get_extension(const char *filepath) { + return strrchr(filepath, '.'); } -static int -check_if_resource(const char *fpath, const struct stat *sb, int typeflag, - struct FTW *ftwbuf) -{ - if (typeflag == FTW_F) { - const char *ext = get_extension(fpath); - if (ext && is_allowed_extension(ext)) { - struct fileasset *newasset = - (struct fileasset *) calloc(1, sizeof(struct fileasset)); - newasset->filename = - (char *) malloc(sizeof(char) * strlen(fpath) + 1); - strcpy(newasset->filename, fpath); - newasset->extension_len = strlen(ext); - newasset->searched = true; - shput(assets, newasset->filename, newasset); - } +static int check_if_resource(const char *fpath, const struct stat *sb, + int typeflag, struct FTW *ftwbuf) { + if (typeflag == FTW_F) { + const char *ext = get_extension(fpath); + if (ext && is_allowed_extension(ext)) { + struct fileasset *newasset = + (struct fileasset *)calloc(1, sizeof(struct fileasset)); + newasset->filename = (char *)malloc(sizeof(char) * strlen(fpath) + 1); + strcpy(newasset->filename, fpath); + newasset->extension_len = strlen(ext); + newasset->searched = true; + shput(assets, newasset->filename, newasset); } + } - return 0; + return 0; } -static void print_files_in_directory(const char *dirpath) -{ - int nflags = 0; - shfree(assets); - nftw(dirpath, &check_if_resource, 10, nflags); +static void print_files_in_directory(const char *dirpath) { + int nflags = 0; + shfree(assets); + nftw(dirpath, &check_if_resource, 10, nflags); } -static void get_all_files() -{ - print_files_in_directory(DATA_PATH); +static void get_all_files() { print_files_in_directory(DATA_PATH); } + +static int *compute_prefix_function(const char *str) { + int str_len = strlen(str); + int *pi = (int *)malloc(sizeof(int) * str_len); + pi[0] = 0; + int k = 0; + + for (int q = 2; q < str_len; q++) { + while (k > 0 && str[k + 1] != str[q]) + k = pi[k]; + + if (str[k + 1] == str[q]) + k += 1; + + pi[q] = k; + } + + return pi; } -static int *compute_prefix_function(const char *str) -{ - int str_len = strlen(str); - int *pi = (int *) malloc(sizeof(int) * str_len); - pi[0] = 0; - int k = 0; +static bool kmp_match(const char *search, const char *text, int *pi) { + int s_len = strlen(search); + int t_len = strlen(text); + // int *pi = compute_prefix_function(search); + int q = 0; + bool found = false; - for (int q = 2; q < str_len; q++) { - while (k > 0 && str[k + 1] != str[q]) - k = pi[k]; + for (int i = 0; i < t_len; i++) { + while (q > 0 && search[q + 1] != text[i]) + q = pi[q]; - if (str[k + 1] == str[q]) - k += 1; + if (search[q + 1] == text[i]) + q += 1; - pi[q] = k; + if (q == s_len) { + q = pi[q]; + found = true; + goto end; } + } - return pi; +end: + return found; } -static bool kmp_match(const char *search, const char *text, int *pi) -{ - int s_len = strlen(search); - int t_len = strlen(text); - // int *pi = compute_prefix_function(search); - int q = 0; - bool found = false; +static int MyCallback() { + /* + if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) { + data->InsertChars(data->CursorPos, ".."); + } else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory) { + if (data->EventKey == ImGuiKey_UpArrow) { + data->DeleteChars(0, data->BufTextLen); + data->InsertChars(0, "Pressed Up!"); + data->SelectAll(); + } else if (data->EventKey == ImGuiKey_DownArrow) { + data->DeleteChars(0, data->BufTextLen); + data->InsertChars(0, "Pressed Down!"); + data->SelectAll(); + } + } else if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit) { + int i = 0; + if (data->Buf[0] == '\0') + while (i < shlen(assets)) + assets[i].value->searched = true; + else + while (i < shlen(assets)) + assets[i].value->searched = + (strstr(assets[i].value->filename, data->Buf) == + NULL) ? false : true; - for (int i = 0; i < t_len; i++) { - while (q > 0 && search[q + 1] != text[i]) - q = pi[q]; - - if (search[q + 1] == text[i]) - q += 1; - - if (q == s_len) { - q = pi[q]; - found = true; - goto end; - } - } - - end: - return found; + } + */ + return 0; } - - -static int MyCallback()//ImGuiInputTextCallbackData * data) +static int TextEditCallback() // ImGuiInputTextCallbackData * data) { -/* - if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) { - data->InsertChars(data->CursorPos, ".."); - } else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory) { - if (data->EventKey == ImGuiKey_UpArrow) { - data->DeleteChars(0, data->BufTextLen); - data->InsertChars(0, "Pressed Up!"); - data->SelectAll(); - } else if (data->EventKey == ImGuiKey_DownArrow) { - data->DeleteChars(0, data->BufTextLen); - data->InsertChars(0, "Pressed Down!"); - data->SelectAll(); - } - } else if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit) { - int i = 0; - if (data->Buf[0] == '\0') - while (i < shlen(assets)) - assets[i].value->searched = true; - else - while (i < shlen(assets)) - assets[i].value->searched = - (strstr(assets[i].value->filename, data->Buf) == - NULL) ? false : true; + /* + static int dirty = 0; - } -*/ - return 0; + if (data->EventChar == '\n') { + dirty = 1; + } else if (data->EventChar == '(') { + //data->EventChar = 245; + dirty = 2; + } else if (data->EventChar == ')') { + dirty = 3; + } + + if (data->EventFlag == ImGuiInputTextFlags_CallbackAlways) { + if (dirty == 1) { + dirty = 0; + char *c = &data->Buf[data->CursorPos - 2]; + + + while (*c != '\n') + c--; + + c++; + + if (isblank(*c)) { + char *ce = c; + + while (isblank(*ce)) + ce++; + + data->InsertChars(data->CursorPos, c, ce); + } + + } + } + + */ + return 0; } -static int TextEditCallback()//ImGuiInputTextCallbackData * data) -{ -/* - static int dirty = 0; - - if (data->EventChar == '\n') { - dirty = 1; - } else if (data->EventChar == '(') { - //data->EventChar = 245; - dirty = 2; - } else if (data->EventChar == ')') { - dirty = 3; - } - - if (data->EventFlag == ImGuiInputTextFlags_CallbackAlways) { - if (dirty == 1) { - dirty = 0; - char *c = &data->Buf[data->CursorPos - 2]; - - - while (*c != '\n') - c--; - c++; - if (isblank(*c)) { - char *ce = c; - - while (isblank(*ce)) - ce++; - - data->InsertChars(data->CursorPos, c, ce); - } - - } - } - -*/ - return 0; +void editor_save() { + FILE *feditor = fopen(editor_filename, "w+"); + fwrite(&editor, sizeof(editor), 1, feditor); + fclose(feditor); } -void editor_save() -{ - FILE *feditor = fopen(editor_filename, "w+"); - fwrite(&editor, sizeof(editor), 1, feditor); +static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int mods) { + if (editor_wantkeyboard()) + return; + + if (action != GLFW_PRESS) + return; + + switch (key) { + case GLFW_KEY_ESCAPE: + quit = true; + //editor_save_projects(); + editor_save(); + break; + + case GLFW_KEY_1: + renderMode = LIT; + break; + + case GLFW_KEY_2: + renderMode = UNLIT; + break; + + case GLFW_KEY_3: + renderMode = WIREFRAME; + break; + + case GLFW_KEY_4: + renderMode = DIRSHADOWMAP; + break; + + case GLFW_KEY_5: + renderGizmos = !renderGizmos; + break; + + case GLFW_KEY_6: + debugDrawPhysics = !debugDrawPhysics; + break; + + case GLFW_KEY_7: + break; + + case GLFW_KEY_8: + break; + + case GLFW_KEY_9: + break; + + case GLFW_KEY_0: + break; + + case GLFW_KEY_T: + break; + + case GLFW_KEY_F2: + editor.showAssetMenu = !editor.showAssetMenu; + break; + + case GLFW_KEY_F3: + editor.showStats = !editor.showStats; + break; + + case GLFW_KEY_F4: + editor.showHierarchy = !editor.showHierarchy; + break; + + case GLFW_KEY_F5: + editor.showLighting = !editor.showLighting; + break; + + case GLFW_KEY_F6: + editor.showGameSettings = !editor.showGameSettings; + break; + + case GLFW_KEY_F7: + editor.showViewmode = !editor.showViewmode; + break; + + case GLFW_KEY_F8: + editor.showDebugMenu = !editor.showDebugMenu; + break; + + case GLFW_KEY_F9: + editor.showExport = !editor.showExport; + break; + + case GLFW_KEY_F10: + editor.showLevel = !editor.showLevel; + break; + + case GLFW_KEY_F11: + window_togglefullscreen(mainwin); + break; + + case GLFW_KEY_GRAVE_ACCENT: + editor.showREPL = !editor.showREPL; + break; + + case GLFW_KEY_K: + showGrid = !showGrid; + break; + + case GLFW_KEY_DELETE: + break; + + case GLFW_KEY_F: + /* + if (selectedobject != NULL) { + cam_goto_object(&camera, &selectedobject->transform); + } + */ + break; + } +} + +static void edit_mouse_cb(GLFWwindow *w, int button, int action, int mods) { + if (editor_wantkeyboard()) + return; + + if (action == GLFW_PRESS) { + switch (button) { + case GLFW_MOUSE_BUTTON_RIGHT: + cursor_hide(); + break; + + case GLFW_MOUSE_BUTTON_MIDDLE: + /* + glBindFramebuffer(GL_FRAMEBUFFER, debugColorPickBO); + int mx = 0; + int my = 0; + SDL_GetMouseState(&mx, &my); + unsigned char data[4]; + glReadPixels(mx, SCREEN_HEIGHT - my, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data); + int pickID = data[0] + data[1]*256 + data[2]*256*256; + snprintf(objectName, 200, "Object %d", pickID); + pickGameObject(pickID); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + */ + + pickGameObject(-1); + + break; + } + } else if (action == GLFW_RELEASE) { + switch (button) { + case GLFW_MOUSE_BUTTON_RIGHT: + cursor_show(); + break; + } + } +} + +void editor_init(struct mSDLWindow *window) { + levels = vec_make(MAXNAME, 10); + editor_load_projects(); + + FILE *feditor = fopen(editor_filename, "r"); + if (feditor == NULL) { + editor_save(); + } else { + fread(&editor, sizeof(editor), 1, feditor); fclose(feditor); + } + + nuke_init(window); + window->nuke_gui = editor_render; + glfwSetKeyCallback(window->window, edit_input_cb); + glfwSetMouseButtonCallback(window->window, edit_mouse_cb); } -static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int mods) -{ - if (editor_wantkeyboard()) return; - - switch (key) { - case GLFW_KEY_ESCAPE: - quit = true; - editor_save_projects(); - editor_save(); - break; - - case GLFW_KEY_1: - renderMode = LIT; - break; - - case GLFW_KEY_2: - renderMode = UNLIT; - break; - - case GLFW_KEY_3: - renderMode = WIREFRAME; - break; - - case GLFW_KEY_4: - renderMode = DIRSHADOWMAP; - break; - - case GLFW_KEY_5: - renderGizmos = !renderGizmos; - break; - - case GLFW_KEY_6: - debugDrawPhysics = !debugDrawPhysics; - break; - - case GLFW_KEY_7: - break; - - case GLFW_KEY_8: - break; - - case GLFW_KEY_9: - break; - - case GLFW_KEY_0: - break; - - case GLFW_KEY_T: - break; - - case GLFW_KEY_F2: - editor.showAssetMenu = !editor.showAssetMenu; - break; - - case GLFW_KEY_F3: - editor.showStats = !editor.showStats; - break; - - case GLFW_KEY_F4: - editor.showHierarchy = !editor.showHierarchy; - break; - - case GLFW_KEY_F5: - editor.showLighting = !editor.showLighting; - break; - - case GLFW_KEY_F6: - editor.showGameSettings = !editor.showGameSettings; - break; - - case GLFW_KEY_F7: - editor.showViewmode = !editor.showViewmode; - break; - - case GLFW_KEY_F8: - editor.showDebugMenu = !editor.showDebugMenu; - break; - - case GLFW_KEY_F9: - editor.showExport = !editor.showExport; - break; - - case GLFW_KEY_F10: - editor.showLevel = !editor.showLevel; - break; - - case GLFW_KEY_F11: - window_togglefullscreen(mainwin); - break; - - case GLFW_KEY_GRAVE_ACCENT: - editor.showREPL = !editor.showREPL; - break; - - case GLFW_KEY_K: - showGrid = !showGrid; - break; - - case GLFW_KEY_DELETE: - break; - - case GLFW_KEY_F: - /* - if (selectedobject != NULL) { - cam_goto_object(&camera, &selectedobject->transform); - } - */ - break; - - } -} - -static void edit_mouse_cb(GLFWwindow *w, int button, int action, int mods) -{ - if (editor_wantkeyboard()) return; - - if (action == GLFW_PRESS) { - switch (button) { - case GLFW_MOUSE_BUTTON_RIGHT: - cursor_hide(); - break; - - case GLFW_MOUSE_BUTTON_MIDDLE: - /* - glBindFramebuffer(GL_FRAMEBUFFER, debugColorPickBO); - int mx = 0; - int my = 0; - SDL_GetMouseState(&mx, &my); - unsigned char data[4]; - glReadPixels(mx, SCREEN_HEIGHT - my, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data); - int pickID = data[0] + data[1]*256 + data[2]*256*256; - snprintf(objectName, 200, "Object %d", pickID); - pickGameObject(pickID); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - */ - - pickGameObject(-1); - - break; - } - } else if (action == GLFW_RELEASE) { - switch (button) { - case GLFW_MOUSE_BUTTON_RIGHT: - cursor_show(); - break; - } - } -} - - - -void editor_init(struct mSDLWindow *window) -{ - projects = vec_make(sizeof(struct gameproject), 5); - levels = vec_make(MAXNAME, 10); - editor_load_projects(); - - FILE *feditor = fopen(editor_filename, "r"); - if (feditor == NULL) { - editor_save(); - } else { - fread(&editor, sizeof(editor), 1, feditor); - fclose(feditor); - } - - nuke_init(window); - - glfwSetKeyCallback(window->window, edit_input_cb); - glfwSetMouseButtonCallback(window->window, edit_mouse_cb); -} - - // TODO: Implement -int editor_wantkeyboard() -{ - return 0; -} +int editor_wantkeyboard() { return 0; } -const int nuk_std = NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| - NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE; +const int nuk_std = NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | + NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE; const struct nk_rect nk_rect_std = {250, 250, 250, 250}; -void editor_project_gui() -{ -/* Grid, etc */ - if (grid1_draw) - draw_grid(grid1_width, grid1_span); - if (grid2_draw) - draw_grid(grid2_width, grid2_span); +void editor_project_gui() { + /* Grid, etc */ + if (grid1_draw) + draw_grid(grid1_width, grid1_span); + if (grid2_draw) + draw_grid(grid2_width, grid2_span); + if (debugDrawPhysics) { + // for (int i = 0; i < number_of_gameobjects(); i++) + // phys2d_dbgdrawcircle(objects[i]->circle); + } + static char text[3][64]; + static int text_len[3]; + static const char *items[] = {"Item 0", "item 1", "item 2"}; + static int selected_item = 0; + static int check = 1; - if (debugDrawPhysics) { - /* - for (int i = 0; i < number_of_gameobjects(); i++) - phys2d_dbgdrawcircle(objects[i]->circle); - */ + int i; + + if (nk_begin(ctx, "Menu Demo", nk_rect(600, 350, 275, 250), nuk_std)) { + nk_menubar_begin(ctx); + + nk_layout_row_dynamic(ctx, 30, 2); + + char bbbuf[256]; + snprintf(bbbuf, 256, "Current level: %s", current_level[0] == '\0' ? "Level not saved!" : current_level); + + nk_label(ctx, bbbuf, NK_TEXT_LEFT); + + if (nk_menu_begin_label(ctx, "Windows", NK_TEXT_LEFT, nk_vec2(100, 200))) { + nk_layout_row_dynamic(ctx, 30, 1); + + nk_checkbox_label(ctx, "Resources", &editor.showAssetMenu); + nk_checkbox_label(ctx, "Hierarchy", &editor.showHierarchy); + nk_checkbox_label(ctx, "Lighting F5", &editor.showLighting); + nk_checkbox_label(ctx, "Game Settings F6", &editor.showGameSettings); + nk_checkbox_label(ctx, "View F7", &editor.showViewmode); + nk_checkbox_label(ctx, "Debug F8", &editor.showDebugMenu); + nk_checkbox_label(ctx, "Export F9", &editor.showExport); + nk_checkbox_label(ctx, "Level F10", &editor.showLevel); + nk_checkbox_label(ctx, "REPL", &editor.showREPL); + + nk_menu_end(ctx); } - static char text[3][64]; - static int text_len[3]; - static const char *items[] = {"Item 0","item 1","item 2"}; - static int selected_item = 0; - static int check = 1; + if (nk_menu_begin_text(ctx, "Levels", 100, 0, nk_vec2(100, 50))) { - int i; + if (nk_button_label(ctx, "New")) { + new_level(); + current_level[0] = '\0'; + } - if (nk_begin(ctx, "Grid Demo", nk_rect(600, 350, 275, 250), - NK_WINDOW_TITLE|NK_WINDOW_BORDER|NK_WINDOW_MOVABLE| - NK_WINDOW_NO_SCROLLBAR)) - { - nk_layout_row_dynamic(ctx, 25, 2); - static struct wav ss; + if (nk_button_label(ctx, "Save")) { + save_level(current_level); + get_levels(); + } - if (nk_button_label(ctx, "Load sound")) { - ss = make_sound("alert.wav"); - } + if (nk_button_label(ctx, "Save as")) { + save_level(levelname); + strcpy(current_level, levelname); + levelname[0] = '\0'; + get_levels(); + } - if (nk_button_label(ctx, "Play sound")) { - play_sound(&ss); - } + nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, levelname, + MAXNAME - 1, nk_filter_default); - nk_layout_row_dynamic(ctx, 30, 2); - nk_label(ctx, "Floating point:", NK_TEXT_RIGHT); - nk_edit_string(ctx, NK_EDIT_FIELD, text[0], &text_len[0], 64, nk_filter_float); - nk_label(ctx, "Hexadecimal:", NK_TEXT_RIGHT); - nk_edit_string(ctx, NK_EDIT_FIELD, text[1], &text_len[1], 64, nk_filter_hex); - nk_label(ctx, "Binary:", NK_TEXT_RIGHT); - nk_edit_string(ctx, NK_EDIT_FIELD, text[2], &text_len[2], 64, nk_filter_binary); - nk_label(ctx, "Checkbox:", NK_TEXT_RIGHT); - nk_checkbox_label(ctx, "Check me", &check); - nk_label(ctx, "Combobox:", NK_TEXT_RIGHT); - if (nk_combo_begin_label(ctx, items[selected_item], nk_vec2(nk_widget_width(ctx), 200))) { - nk_layout_row_dynamic(ctx, 25, 1); - for (i = 0; i < 3; ++i) - if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT)) - selected_item = i; - nk_combo_end(ctx); - } + vec_walk(levels, (void (*)(void *)) & editor_level_btn); } -nk_end(ctx); - -if (nk_begin(ctx, "Menu Demo", nk_rect(600, 350, 275, 250), nuk_std)) { - nk_menubar_begin(ctx); - - nk_layout_row_dynamic(ctx, 30, 4); -/* - char bbbuf[256]; - snprintf(bbbuf, 256, "Current level: %s", current_level[0] == '\0' ? "Level not saved!" : current_level); - nk_label(ctx, bbbuf, NK_TEXT_LEFT); -*/ - if (nk_menu_begin_label(ctx, "Windows", NK_TEXT_LEFT, nk_vec2(100, 200))) { - nk_layout_row_dynamic(ctx, 30, 1); - - nk_checkbox_label(ctx, "Resources", &editor.showAssetMenu); - nk_checkbox_label(ctx, "Hierarchy", &editor.showHierarchy); - nk_checkbox_label(ctx, "Lighting F5", &editor.showLighting); - nk_checkbox_label(ctx, "Game Settings F6", &editor.showGameSettings); - nk_checkbox_label(ctx, "View F7", &editor.showViewmode); - nk_checkbox_label(ctx, "Debug F8", &editor.showDebugMenu); - nk_checkbox_label(ctx, "Export F9", &editor.showExport); - nk_checkbox_label(ctx, "Level F10", &editor.showLevel); - nk_checkbox_label(ctx, "REPL", &editor.showREPL); - - nk_menu_end(ctx); - } - - if (nk_menu_begin_text(ctx, "Levels", 100, 0, nk_vec2(100, 50))) { - - if (nk_button_label(ctx, "New")) { - new_level(); - current_level[0] = '\0'; - } - - if (nk_button_label(ctx, "Save")) { - save_level(current_level); - get_levels(); - } - - if (nk_button_label(ctx, "Save as")) { - save_level(levelname); - strcpy(current_level, levelname); - levelname[0] = '\0'; - get_levels(); - } - - nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, levelname, MAXNAME-1, nk_filter_default); - - vec_walk(levels, (void (*)(void *)) &editor_level_btn); - - } nk_menubar_end(ctx); + } + nk_end(ctx); + + if (editor.showExport && + nk_begin(ctx, "Export and Bake", nk_rect_std, nuk_std)) { + + if (nk_button_label(ctx, "Bake")) { + } + if (nk_button_label(ctx, "Build")) { + } + + nk_end(ctx); + } + + // Shadow map vars + if (nk_begin(ctx, "Lighting options", nk_rect_std, nuk_std)) { + nk_layout_row_dynamic(ctx, 25, 1); + nk_label(ctx, "Directional shadow map", NK_TEXT_LEFT); + + nk_property_float(ctx, "Near plane", -200.f, &near_plane, 200.f, 1.f, + 0.01f); + nk_property_float(ctx, "Far plane", -200.f, &far_plane, 200.f, 1.f, 0.01f); + nk_property_float(ctx, "Shadow lookahead", 0.f, &shadowLookahead, 100.f, + 1.f, 0.01f); + nk_property_float(ctx, "Plane size", 0.f, &plane_size, 100.f, 1.f, 0.01f); + } + nk_end(ctx); + + if (editor.showGameSettings) { + nk_begin(ctx, "Game settings", nk_rect_std, nuk_std); + + // nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, cur_project->name, + // 126, nk_filter_default); + + if (nk_tree_push(ctx, NK_TREE_NODE, "Physics", NK_MINIMIZED)) { + nk_property_float(ctx, "2d Gravity", -5000.f, &phys2d_gravity, 0.f, 1.f, + 0.1f); + phys2d_apply(); + nk_tree_pop(ctx); + } + + if (nk_tree_push(ctx, NK_TREE_NODE, "Quality", NK_MINIMIZED)) { + nk_tree_pop(ctx); + } + nk_end(ctx); + } + + if (editor.showStats) { + nk_begin(ctx, "Stats", nk_rect_std, nuk_std); + nk_labelf(ctx, NK_TEXT_LEFT, "FPS: %2.4f", 1.f / deltaT); + nk_labelf(ctx, NK_TEXT_LEFT, "Triangles rendered: %llu", triCount); + nk_end(ctx); + } + + if (editor.showREPL) { + nk_begin(ctx, "REPL", nk_rect_std, nuk_std); + + nk_flags active; + + static char buffer[512] = {'\0'}; + + active = nk_edit_string_zero_terminated(ctx, NK_EDIT_BOX | NK_EDIT_SIG_ENTER, buffer, 512 - 1, nk_filter_ascii); + if (active && NK_EDIT_COMMITED) { + script_run(buffer); + buffer[0] = '\0'; + } + + nk_end(ctx); + } + + if (editor.showViewmode) { + nk_begin(ctx, "View options", nk_rect_std, nuk_std); + + nk_property_float(ctx, "Camera FOV", 0.1f, &editorFOV, 90.f, 1.f, 0.1f); + nk_property_float(ctx, "Camera Near Plane", 0.1f, &editorClose, 5.f, 0.1f, + 0.01f); + nk_property_float(ctx, "Camera Far Plane", 50.f, &editorFar, 10000.f, 1.f, + 1.f); + + if (nk_tree_push(ctx, NK_TREE_NODE, "Shading mode", NK_MINIMIZED)) { + renderMode = + nk_option_label(ctx, "Lit", renderMode == LIT) ? LIT : renderMode; + renderMode = nk_option_label(ctx, "Unlit", renderMode == UNLIT) + ? UNLIT + : renderMode; + renderMode = nk_option_label(ctx, "Wireframe", renderMode == WIREFRAME) + ? WIREFRAME + : renderMode; + renderMode = nk_option_label(ctx, "Directional shadow map", + renderMode == DIRSHADOWMAP) + ? DIRSHADOWMAP + : renderMode; + nk_tree_pop(ctx); + } + + if (nk_tree_push(ctx, NK_TREE_NODE, "Lighting", NK_MINIMIZED)) { + nk_checkbox_label(ctx, "Shadows", &renderDynamicShadows); + nk_checkbox_label(ctx, "Ambient Occlusion", &renderAO); + nk_tree_pop(ctx); + } + + if (nk_tree_push(ctx, NK_TREE_NODE, "Debug Draws", NK_MINIMIZED)) { + nk_checkbox_label(ctx, "Gizmos", &renderGizmos); + nk_checkbox_label(ctx, "Grid", &showGrid); + nk_checkbox_label(ctx, "Physics", &debugDrawPhysics); + nk_tree_pop(ctx); + } + + nk_end(ctx); + } + + if (editor.showHierarchy) { + editor.showHierarchy = nk_begin(ctx, "Objects", nk_rect_std, nuk_std); + + if (nk_button_label(ctx, "New Object")) { + MakeGameobject(); + } + + obj_gui_hierarchy(selectedobject); + + nk_end(ctx); + } + + if (nk_begin(ctx, "Simulate", nk_rect_std, nuk_std)) { + + if (physOn) { + if (nk_button_label(ctx, "Pause")) + game_pause(); + + if (nk_button_label(ctx, "Stop")) + game_stop(); + } else { + if (nk_button_label(ctx, "Play")) + game_start(); + } + + nk_end(ctx); + } + + if (nk_begin(ctx, "Prefab Creator", nk_rect_std, nuk_std)) { + vec_walk(prefabs, (void (*)(void *)) & editor_prefab_btn); + nk_end(ctx); + } + + if (editor.showAssetMenu) { + nk_begin(ctx, "Asset Menu", nk_rect_std, nuk_std); + + nk_edit_string_zero_terminated(ctx, + NK_EDIT_BOX | NK_EDIT_NO_HORIZONTAL_SCROLL, + asset_search_buffer, 100, nk_filter_ascii); + + /* +if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) { +data->InsertChars(data->CursorPos, ".."); +} else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory) { +if (data->EventKey == ImGuiKey_UpArrow) { +data->DeleteChars(0, data->BufTextLen); +data->InsertChars(0, "Pressed Up!"); +data->SelectAll(); +} else if (data->EventKey == ImGuiKey_DownArrow) { +data->DeleteChars(0, data->BufTextLen); +data->InsertChars(0, "Pressed Down!"); +data->SelectAll(); } -nk_end(ctx); +} else if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit) { +int i = 0; +if (data->Buf[0] == '\0') +while (i < shlen(assets)) +assets[i].value->searched = true; +else +while (i < shlen(assets)) +assets[i].value->searched = +(strstr(assets[i].value->filename, data->Buf) == +NULL) ? false : true; - - if (editor.showExport && nk_begin(ctx, "Export and Bake", nk_rect_std, nuk_std)) { - - if (nk_button_label(ctx, "Bake")) { - } - if (nk_button_label(ctx, "Build")) { - } - - nk_end(ctx); - } - - - // Shadow map vars - if (nk_begin(ctx, "Lighting options", nk_rect_std, nuk_std)) { - nk_layout_row_dynamic(ctx, 25, 1); - nk_label(ctx, "Directional shadow map", NK_TEXT_LEFT); - - nk_property_float(ctx, "Near plane", -200.f, &near_plane, 200.f, 1.f, 0.01f); - nk_property_float(ctx, "Far plane", -200.f, &far_plane, 200.f, 1.f, 0.01f); - nk_property_float(ctx, "Shadow lookahead", 0.f, &shadowLookahead, 100.f, 1.f, 0.01f); - nk_property_float(ctx, "Plane size", 0.f, &plane_size, 100.f, 1.f, 0.01f); - - - - }nk_end(ctx); - - - if (editor.showGameSettings) { - nk_begin(ctx, "Game settings", nk_rect_std, nuk_std); - - //nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, cur_project->name, 126, nk_filter_default); - - if (nk_tree_push(ctx, NK_TREE_NODE, "Physics", NK_MINIMIZED)) { - nk_property_float(ctx, "2d Gravity", -5000.f, &phys2d_gravity, 0.f, 1.f, 0.1f); - phys2d_apply(); - nk_tree_pop(ctx); - } - - if (nk_tree_push(ctx, NK_TREE_NODE, "Quality", NK_MINIMIZED)) { - nk_tree_pop(ctx); - } - nk_end(ctx); - } - - - if (editor.showStats) { - nk_begin(ctx, "Stats", nk_rect_std, nuk_std); - nk_labelf(ctx, NK_TEXT_LEFT, "FPS: %2.4f", 1.f/deltaT); - nk_labelf(ctx, NK_TEXT_LEFT, "Triangles rendered: %llu", triCount); - nk_end(ctx); - } - - - if (editor.showREPL) { - nk_begin(ctx, "REPL", nk_rect_std, nuk_std); - - nk_flags active; - - static char buffer[512] = { '\0' }; - - active = nk_edit_string_zero_terminated(ctx, NK_EDIT_BOX|NK_EDIT_SIG_ENTER, buffer, 512-1, nk_filter_ascii); - if (active && NK_EDIT_COMMITED) { - script_run(buffer); - buffer[0] = '\0'; - } - - nk_end(ctx); - - } - - if (editor.showViewmode) { - nk_begin(ctx, "View options", nk_rect_std, nuk_std); - - nk_property_float(ctx, "Camera FOV", 0.1f, &editorFOV, 90.f, 1.f, 0.1f); - nk_property_float(ctx, "Camera Near Plane", 0.1f, &editorClose, 5.f, 0.1f, 0.01f); - nk_property_float(ctx, "Camera Far Plane", 50.f, &editorFar, 10000.f, 1.f, 1.f); - - if (nk_tree_push(ctx, NK_TREE_NODE, "Shading mode", NK_MINIMIZED)) { - renderMode = nk_option_label(ctx, "Lit", renderMode == LIT) ? LIT : renderMode; - renderMode = nk_option_label(ctx, "Unlit", renderMode == UNLIT) ? UNLIT : renderMode; - renderMode = nk_option_label(ctx, "Wireframe", renderMode == WIREFRAME) ? WIREFRAME : renderMode; - renderMode = nk_option_label(ctx, "Directional shadow map", renderMode == DIRSHADOWMAP) ? DIRSHADOWMAP : renderMode; - nk_tree_pop(ctx); - } - - if (nk_tree_push(ctx, NK_TREE_NODE, "Lighting", NK_MINIMIZED)) { - nk_checkbox_label(ctx, "Shadows", &renderDynamicShadows); - nk_checkbox_label(ctx, "Ambient Occlusion", &renderAO); - nk_tree_pop(ctx); - } - - if (nk_tree_push(ctx, NK_TREE_NODE, "Debug Draws", NK_MINIMIZED)) { - nk_checkbox_label(ctx, "Gizmos", &renderGizmos); - nk_checkbox_label(ctx, "Grid", &showGrid); - nk_checkbox_label(ctx, "Physics", &debugDrawPhysics); - nk_tree_pop(ctx); - } - - nk_end(ctx); - } - - if (editor.showHierarchy) { - nk_begin(ctx, "Objects", nk_rect_std, nuk_std); - - if (nk_button_label(ctx, "New Object")) { - MakeGameobject(); - } - - obj_gui_hierarchy(selectedobject); - - nk_end(ctx); - } - - - nk_begin(ctx, "Simulate", nk_rect_std, nuk_std); - - if (physOn) { - if (nk_button_label(ctx, "Pause")) - game_pause(); - - if (nk_button_label(ctx, "Stop")) - game_stop(); - } else { - if (nk_button_label(ctx, "Play")) - game_start(); - } - - nk_end(ctx); - - - nk_begin(ctx, "Prefab Creator", nk_rect_std, nuk_std); - - vec_walk(prefabs, (void (*)(void *)) &editor_prefab_btn); - - nk_end(ctx); - - - if (editor.showAssetMenu) { - nk_begin(ctx, "Asset Menu", nk_rect_std, nuk_std); - //active = nk_edit_string_zero_terminated(ctx, NK_EDIT_BOX|NK_EDIT_SIG_ENTER, buffer, 512-1, nk_filter_ascii); - nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, asset_search_buffer, 100, nk_filter_ascii); - - - /* - if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) { - data->InsertChars(data->CursorPos, ".."); - } else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory) { - if (data->EventKey == ImGuiKey_UpArrow) { - data->DeleteChars(0, data->BufTextLen); - data->InsertChars(0, "Pressed Up!"); - data->SelectAll(); - } else if (data->EventKey == ImGuiKey_DownArrow) { - data->DeleteChars(0, data->BufTextLen); - data->InsertChars(0, "Pressed Down!"); - data->SelectAll(); - } - } else if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit) { - int i = 0; - if (data->Buf[0] == '\0') - while (i < shlen(assets)) - assets[i].value->searched = true; - else - while (i < shlen(assets)) - assets[i].value->searched = - (strstr(assets[i].value->filename, data->Buf) == - NULL) ? false : true; - - } +} */ - if (nk_button_label(ctx,"Reload all files")) - get_all_files(); + if (nk_button_label(ctx, "Reload all files")) + get_all_files(); - nk_group_begin(ctx, "##scrolling", NK_WINDOW_NO_SCROLLBAR); - for (int i = 0; i < shlen(assets); i++) { - if (!assets[i].value->searched) - continue; + nk_group_begin(ctx, "##scrolling", NK_WINDOW_NO_SCROLLBAR); + for (int i = 0; i < shlen(assets); i++) { + if (!assets[i].value->searched) + continue; - if (nk_button_label(ctx, assets[i].value->filename + stemlen)) { - editor_selectasset(assets[i].value); - } - } - nk_group_end(ctx); + if (nk_button_label(ctx, assets[i].value->filename + stemlen)) { + editor_selectasset(assets[i].value); + } + } + nk_group_end(ctx); - nk_end(ctx); + nk_end(ctx); + } + + if (selected_asset) + editor_asset_gui(selected_asset); + + if (editor.showDebugMenu) { + nk_begin(ctx, "Debug Menu", nk_rect_std, nuk_std); + + if (nk_button_label(ctx, "Reload Shaders")) { + shader_compile_all(); } - if (selected_asset) - editor_asset_gui(selected_asset); + nk_property_int(ctx, "Grid 1 Span", 1, &grid1_span, 500, 1, 1); + nk_checkbox_label(ctx, "Draw", &grid1_draw); + nk_property_int(ctx, "Grid 2 Span", 10, &grid2_span, 1000, 1, 1); + nk_checkbox_label(ctx, "Draw", &grid2_draw); + nk_property_float(ctx, "Grid Opacity", 0.f, &gridOpacity, 1.f, 0.01f, + 0.01f); + nk_property_float(ctx, "Small unit", 0.5f, &smallGridUnit, 5.f, 0.1f, 0.1f); + nk_property_float(ctx, "Big unit", 10.f, &bigGridUnit, 50.f, 1.f, 0.1f); + nk_property_float(ctx, "Small thickness", 1.f, &gridSmallThickness, 10.f, + 0.1f, 0.1f); + nk_property_float(ctx, "Big thickness", 1.f, &gridBigThickness, 10.f, 0.1f, + 0.1f); + static struct nk_colorf smgrd; + static struct nk_colorf lgrd; - if (editor.showDebugMenu) { - nk_begin(ctx, "Debug Menu", nk_rect_std, nuk_std); - if (nk_button_label(ctx, "Reload Shaders")) { - shader_compile_all(); - } - //ImGui::SliderFloat("Grid scale", &gridScale, 100.f, 500.f, "%1.f"); + nk_color_pick(ctx, &smgrd, NK_RGBA); + nk_color_pick(ctx, &lgrd, NK_RGBA); - nk_property_int(ctx, "Grid 1 Span", 1, &grid1_span, 500, 1, 1); - nk_checkbox_label(ctx, "Draw", &grid1_draw); + nk_end(ctx); + } - nk_property_int(ctx, "Grid 2 Span", 10, &grid2_span, 1000, 1, 1); - nk_checkbox_label(ctx, "Draw", &grid2_draw); +startobjectgui: + if (selectedobject) { + draw_point(selectedobject->transform.position[0], + selectedobject->transform.position[1], 5); - nk_property_float(ctx, "Grid Opacity",0.f, &gridOpacity, 1.f, 0.01f, 0.01f); - nk_property_float(ctx, "Small unit", 0.5f,&smallGridUnit, 5.f, 0.1f, 0.1f); - nk_property_float(ctx, "Big unit", 10.f,&bigGridUnit, 50.f, 1.f, 0.1f); - nk_property_float(ctx, "Small thickness",1.f, &gridSmallThickness, 10.f, 0.1f, 0.1f); - nk_property_float(ctx, "Big thickness", 1.f, &gridBigThickness, 10.f, 0.1f, 0.1f); + nk_begin(ctx, "Object Parameters", nk_rect_std, nuk_std); - static struct nk_colorf smgrd; - static struct nk_colorf lgrd; + nk_layout_row_dynamic(ctx, 30, 3); - nk_color_pick(ctx, &smgrd, NK_RGBA); - nk_color_pick(ctx, &lgrd, NK_RGBA); + if (nk_button_label(ctx, "Save")) + gameobject_saveprefab(selectedobject); - //ImGui::SliderInt("MSAA", &msaaSamples, 0, 4); - nk_end(ctx); + if (nk_button_label(ctx, "Del")) { + gameobject_delete(selected_index); + pickGameObject(-1); + nk_end(ctx); + goto startobjectgui; } - startobjectgui: - - if (selectedobject) { - draw_point(selectedobject->transform.position[0], - selectedobject->transform.position[1], 5); - - - nk_begin(ctx, "Object Parameters", nk_rect_std, nuk_std); - - if (nk_button_label(ctx, "Save")) - gameobject_saveprefab(selectedobject); - - // ImGui::SameLine(); - if (nk_button_label(ctx, "Del")) { - gameobject_delete(selected_index); - pickGameObject(-1); - nk_end(ctx); - goto startobjectgui; - } - - // ImGui::SameLine(); - if (selectedobject->editor.prefabSync) { - if (nk_button_label(ctx, "Revert")) - gameobject_revertprefab(selectedobject); - - } - - - nk_label(ctx, "Name", NK_TEXT_LEFT); - nk_edit_string_zero_terminated(ctx, 0, selectedobject->editor.mname, 50, nk_filter_ascii); - - nk_label(ctx, "Prefab", NK_TEXT_LEFT); - nk_edit_string_zero_terminated(ctx, 0, selectedobject->editor.prefabName, 50, nk_filter_ascii); - // Disabled if::::: selectedobject->editor.prefabSync ? ImGuiInputTextFlags_ReadOnly : 0); - - object_gui(selectedobject); - - - nk_end(ctx); - - - nk_begin(ctx, "Components", nk_rect_std, nuk_std); - - for (int i = 0; i < ncomponent; i++) { - if (nk_button_label(ctx, components[i].name)) { - gameobject_addcomponent(selectedobject, &components[i]); - } - } - - - nk_end(ctx); - + if (selectedobject->editor.prefabSync) { + if (nk_button_label(ctx, "Revert")) + gameobject_revertprefab(selectedobject); } -} + nk_label(ctx, "Name", NK_TEXT_LEFT); + nk_edit_string_zero_terminated(ctx, 0, selectedobject->editor.mname, 50, + nk_filter_ascii); -void editor_render() -{ - if (cur_project) - editor_project_gui(); - else - editor_proj_select_gui(); -} + nk_label(ctx, "Prefab", NK_TEXT_LEFT); + nk_edit_string_zero_terminated(ctx, 0, selectedobject->editor.prefabName, + 50, nk_filter_ascii); + // Disabled if::::: selectedobject->editor.prefabSync ? + // ImGuiInputTextFlags_ReadOnly : 0); - -void pickGameObject(int pickID) -{ - if (pickID >= 0 && pickID < gameobjects->len) { - selected_index = pickID; - selectedobject = - (struct mGameObject *) vec_get(gameobjects, pickID); - } else { - selected_index = -1; - selectedobject = NULL; - } -} - -int is_allowed_extension(const char *ext) -{ - for (size_t i = 0; - i < sizeof(allowed_extensions) / sizeof(allowed_extensions[0]); - i++) { - if (!strcmp(ext + 1, allowed_extensions[i])) - return true; - } - - return false; -} - -void editor_level_btn(char *level) -{ - - if (nk_button_label(ctx, level)) { - load_level(level); - strcpy(current_level, level); - } - -} - -void editor_selectasset(struct fileasset *asset) -{ - const char *ext = get_extension(asset->filename); - - if (!strcmp(ext + 1, "png") || !strcmp(ext + 1, "jpg")) { - asset->data = texture_loadfromfile(asset->filename); - tex_gui_anim.tex = (struct Texture *) asset->data; - asset->type = ASSET_TYPE_IMAGE; - tex_anim_set(&tex_gui_anim); - //float tex_scale = float((float) ASSET_WIN_SIZE / tex_gui_anim.tex->width); - if (tex_scale >= 10.f) - tex_scale = 10.f; - } else if (!strcmp(ext + 1, "glsl")) { - asset->type = ASSET_TYPE_TEXT; - - FILE *fasset = fopen(asset->filename, "rb"); - - fseek(fasset, 0, SEEK_END); - long length = ftell(fasset); - fseek(fasset, 0, SEEK_SET); - asset->data = malloc(ASSET_TEXT_BUF); - fread(asset->data, 1, length, fasset); - fclose(fasset); - - - } - selected_asset = asset; -} - -void editor_selectasset_str(char *path) -{ - struct fileasset *asset = (struct fileasset*)shget(assets, path); - - if (asset) - editor_selectasset(asset); -} - -void editor_asset_tex_gui(struct Texture *tex) -{ -/* - ImGui::Text("%dx%d", tex->width, tex->height); - - ImGui::SliderFloat("Zoom", &tex_scale, 0.01f, 10.f); - - int old_sprite = tex->opts.sprite; - ImGui::Checkbox("Sprite", (bool *) &tex->opts.sprite); - - if (old_sprite != tex->opts.sprite) - tex_gpu_load(tex); - - - ImGui::RadioButton("Raw", &tex_view, 0); - ImGui::SameLine(); ImGui::RadioButton("View 1", &tex_view, 1); - ImGui::SameLine(); ImGui::RadioButton("View 2", &tex_view, 2); - - - ImGui::Checkbox("Animation", (bool *) &tex->opts.animation); - - if (tex->opts.animation) { - int old_frames = tex->anim.frames; - int old_ms = tex->anim.ms; - ImGui::SliderInt("Frames", &tex->anim.frames, 1, 20); - ImGui::SliderInt("FPS", &tex->anim.ms, 1, 24); - - - - - if (tex_gui_anim.playing) { - if (ImGui::Button("Pause")) - anim_pause(&tex_gui_anim); - ImGui::SameLine(); - if (tex_gui_anim.playing && ImGui::Button("Stop")) - anim_stop(&tex_gui_anim); - } else { - if (ImGui::Button("Play")) - anim_play(&tex_gui_anim); - - ImGui::SameLine(); - if (ImGui::Button("Bkwd")) - anim_bkwd(&tex_gui_anim); - - - ImGui::SameLine(); - if (ImGui::Button("Fwd")) - anim_fwd(&tex_gui_anim); - } - - - - - - - - - ImGui::SameLine(); - ImGui::Text("Frame %d/%d", tex_gui_anim.frame + 1, - tex_gui_anim.tex->anim.frames); - - - - - - if (old_frames != tex->anim.frames || old_ms != tex->anim.ms) - tex_anim_set(&tex_gui_anim); - - ImVec2 uv0 = ImVec2(tex_gui_anim.uv.x, tex_gui_anim.uv.y); - ImVec2 uv1 = ImVec2(tex_gui_anim.uv.x + tex_gui_anim.uv.w, - tex_gui_anim.uv.y + tex_gui_anim.uv.h); - ImGui::Image((void *) (intptr_t) tex->id, - ImVec2(tex->width * tex_gui_anim.uv.w * tex_scale, - tex->height * tex_gui_anim.uv.h * tex_scale), - uv0, uv1); - } else { - ImGui::Image((void *) (intptr_t) tex->id, - ImVec2(tex->width * tex_scale, - tex->height * tex_scale)); - } - */ - -} - -void editor_asset_text_gui(char *text) -{ -/* - ImGui::InputTextMultiline("File edit", text, ASSET_TEXT_BUF, - ImVec2(600, 500), - ImGuiInputTextFlags_CallbackAlways | - ImGuiInputTextFlags_CallbackCharFilter, - TextEditCallback); - if (ImGui::Button("Save")) { - FILE *f = fopen(selected_asset->filename, "wd"); - size_t len = strlen(text); - fwrite(text, len, 1, f); - fclose(f); - } - */ -} - -void editor_asset_gui(struct fileasset *asset) -{ - - nk_begin(ctx, "Asset Viewer", nk_rect_std, nuk_std); - - nk_labelf(ctx, NK_TEXT_LEFT, "%s", selected_asset->filename); - - //ImGui::SameLine(); - if (nk_button_label(ctx, "Close")) - selected_asset = NULL; - - switch (asset->type) { - case ASSET_TYPE_NULL: - break; - - case ASSET_TYPE_IMAGE: - editor_asset_tex_gui((struct Texture *) asset->data); - break; - - case ASSET_TYPE_TEXT: - editor_asset_text_gui((char *) asset->data); - break; - } + object_gui(selectedobject); nk_end(ctx); -} + nk_begin(ctx, "Components", nk_rect_std, nuk_std); -void editor_load_projects() -{ - FILE *f = fopen("projects.yugh", "r"); - if (!f) - return; - - vec_load(projects, f); - fclose(f); -} - -void editor_save_projects() -{ - FILE *f = fopen("projects.yugh", "w"); - vec_store(projects, f); - fclose(f); -} - -void editor_project_btn_gui(struct gameproject *gp) -{ -/* - if (ImGui::Button(gp->name)) - editor_init_project(gp); - - - ImGui::SameLine(); - ImGui::Text("%s", gp->path); - */ -} - -void editor_proj_select_gui() -{ -/* - ImGui::Begin("Project Select"); - - vec_walk(projects, (void (*)(void *)) &editor_project_btn_gui); - - ImGui::InputText("Project import path", setpath, MAXPATH); - ImGui::SameLine(); - if (ImGui::Button("Create")) { - editor_make_project(setpath); - } - ImGui::SameLine(); - if (ImGui::Button("Import")) { - editor_import_project(setpath); + for (int i = 0; i < ncomponent; i++) { + if (nk_button_label(ctx, components[i].name)) { + gameobject_addcomponent(selectedobject, &components[i]); + } } - ImGui::End(); - */ + nk_end(ctx); + } } -void editor_init_project(struct gameproject *gp) -{ - cur_project = gp; - DATA_PATH = strdup(gp->path); - stemlen = strlen(DATA_PATH); - findPrefabs(); - get_levels(); - get_all_files(); +void editor_render() { editor_project_gui(); } + +void pickGameObject(int pickID) { + if (pickID >= 0 && pickID < gameobjects->len) { + selected_index = pickID; + selectedobject = (struct mGameObject *)vec_get(gameobjects, pickID); + } else { + selected_index = -1; + selectedobject = NULL; + } } -void editor_make_project(char *path) -{ - FILE *f = path_open("w", "%s%s", path, "/project.yugh"); - cur_project = - (struct gameproject *) malloc(sizeof(struct gameproject)); - strncpy(cur_project->name, "New Game", 127); - strncpy(cur_project->path, path, 2048); - vec_add(projects, cur_project); - fwrite(cur_project, sizeof(*cur_project), 1, f); - fclose(f); +int is_allowed_extension(const char *ext) { + for (size_t i = 0; + i < sizeof(allowed_extensions) / sizeof(allowed_extensions[0]); i++) { + if (!strcmp(ext + 1, allowed_extensions[i])) + return true; + } - editor_init_project(cur_project); - - editor_save_projects(); + return false; } -void editor_import_project(char *path) -{ - FILE *f = path_open("r", "%s%s", path, "/project.yugh"); - if (!f) - return; +void editor_level_btn(char *level) { - struct gameproject *gp = (struct gameproject *) malloc(sizeof(*gp)); - fread(gp, sizeof(*gp), 1, f); - fclose(f); - - vec_add(projects, gp); + if (nk_button_label(ctx, level)) { + load_level(level); + strcpy(current_level, level); + } } -void editor_makenewobject() -{ +void editor_selectasset(struct fileasset *asset) { + const char *ext = get_extension(asset->filename); + if (!strcmp(ext + 1, "png") || !strcmp(ext + 1, "jpg")) { + asset->data = texture_loadfromfile(asset->filename); + tex_gui_anim.tex = (struct Texture *)asset->data; + asset->type = ASSET_TYPE_IMAGE; + tex_anim_set(&tex_gui_anim); + // float tex_scale = float((float) ASSET_WIN_SIZE / + // tex_gui_anim.tex->width); + if (tex_scale >= 10.f) + tex_scale = 10.f; + } else if (!strcmp(ext + 1, "glsl")) { + asset->type = ASSET_TYPE_TEXT; + + FILE *fasset = fopen(asset->filename, "rb"); + + fseek(fasset, 0, SEEK_END); + long length = ftell(fasset); + fseek(fasset, 0, SEEK_SET); + asset->data = malloc(ASSET_TEXT_BUF); + fread(asset->data, 1, length, fasset); + fclose(fasset); + } + selected_asset = asset; } -int obj_gui_hierarchy(struct mGameObject *selected) -{ +void editor_selectasset_str(char *path) { + struct fileasset *asset = (struct fileasset *)shget(assets, path); - for (int i = 0; i < gameobjects->len; i++) { - struct mGameObject *go = (struct mGameObject *) vec_get(gameobjects, i); + if (asset) + editor_selectasset(asset); +} - if (nk_select_label(ctx, go->editor.mname, NK_TEXT_LEFT, go == selected)) { - if (go != selected) - pickGameObject(i); - } +void editor_asset_tex_gui(struct Texture *tex) { + /* + ImGui::Text("%dx%d", tex->width, tex->height); + + ImGui::SliderFloat("Zoom", &tex_scale, 0.01f, 10.f); + + int old_sprite = tex->opts.sprite; + ImGui::Checkbox("Sprite", (bool *) &tex->opts.sprite); + + if (old_sprite != tex->opts.sprite) + tex_gpu_load(tex); + + + ImGui::RadioButton("Raw", &tex_view, 0); + ImGui::SameLine(); ImGui::RadioButton("View 1", &tex_view, 1); + ImGui::SameLine(); ImGui::RadioButton("View 2", &tex_view, 2); + + + ImGui::Checkbox("Animation", (bool *) &tex->opts.animation); + + if (tex->opts.animation) { + int old_frames = tex->anim.frames; + int old_ms = tex->anim.ms; + ImGui::SliderInt("Frames", &tex->anim.frames, 1, 20); + ImGui::SliderInt("FPS", &tex->anim.ms, 1, 24); + + + + + if (tex_gui_anim.playing) { + if (ImGui::Button("Pause")) + anim_pause(&tex_gui_anim); + ImGui::SameLine(); + if (tex_gui_anim.playing && ImGui::Button("Stop")) + anim_stop(&tex_gui_anim); + } else { + if (ImGui::Button("Play")) + anim_play(&tex_gui_anim); + + ImGui::SameLine(); + if (ImGui::Button("Bkwd")) + anim_bkwd(&tex_gui_anim); + + + ImGui::SameLine(); + if (ImGui::Button("Fwd")) + anim_fwd(&tex_gui_anim); + } + + + + + + + + + ImGui::SameLine(); + ImGui::Text("Frame %d/%d", tex_gui_anim.frame + 1, + tex_gui_anim.tex->anim.frames); + + + + + + if (old_frames != tex->anim.frames || old_ms != tex->anim.ms) + tex_anim_set(&tex_gui_anim); + + ImVec2 uv0 = ImVec2(tex_gui_anim.uv.x, tex_gui_anim.uv.y); + ImVec2 uv1 = ImVec2(tex_gui_anim.uv.x + tex_gui_anim.uv.w, + tex_gui_anim.uv.y + tex_gui_anim.uv.h); + ImGui::Image((void *) (intptr_t) tex->id, + ImVec2(tex->width * tex_gui_anim.uv.w * tex_scale, + tex->height * tex_gui_anim.uv.h * tex_scale), + uv0, uv1); + } else { + ImGui::Image((void *) (intptr_t) tex->id, + ImVec2(tex->width * tex_scale, + tex->height * tex_scale)); + } + */ +} + +void editor_asset_text_gui(char *text) { + /* + ImGui::InputTextMultiline("File edit", text, ASSET_TEXT_BUF, + ImVec2(600, 500), + ImGuiInputTextFlags_CallbackAlways | + ImGuiInputTextFlags_CallbackCharFilter, + TextEditCallback); + if (ImGui::Button("Save")) { + FILE *f = fopen(selected_asset->filename, "wd"); + size_t len = strlen(text); + fwrite(text, len, 1, f); + fclose(f); + } + */ +} + +void editor_asset_gui(struct fileasset *asset) { + + nk_begin(ctx, "Asset Viewer", nk_rect_std, nuk_std); + + nk_labelf(ctx, NK_TEXT_LEFT, "%s", selected_asset->filename); + + // ImGui::SameLine(); + if (nk_button_label(ctx, "Close")) + selected_asset = NULL; + + switch (asset->type) { + case ASSET_TYPE_NULL: + break; + + case ASSET_TYPE_IMAGE: + editor_asset_tex_gui((struct Texture *)asset->data); + break; + + case ASSET_TYPE_TEXT: + editor_asset_text_gui((char *)asset->data); + break; + } + + nk_end(ctx); +} + +void editor_makenewobject() {} + +int obj_gui_hierarchy(struct mGameObject *selected) { + + for (int i = 0; i < gameobjects->len; i++) { + struct mGameObject *go = (struct mGameObject *)vec_get(gameobjects, i); + + if (nk_select_label(ctx, go->editor.mname, NK_TEXT_LEFT, go == selected)) { + if (go != selected) + pickGameObject(i); } + } - return 0; + return 0; } -void get_levels() -{ - fill_extensions(levels, DATA_PATH, EXT_LEVEL); +void get_levels() { fill_extensions(levels, DATA_PATH, EXT_LEVEL); } + +void editor_prefab_btn(char *prefab) { + if (nk_button_label(ctx, prefab)) { + gameobject_makefromprefab(prefab); + /*GameObject* newprefab = (GameObject*)createPrefab(*prefab); */ + /*cam_inverse_goto(&camera, &newprefab->transform); */ + } } -void editor_prefab_btn(char *prefab) -{ - if (nk_button_label(ctx, prefab)) { - gameobject_makefromprefab(prefab); - /*GameObject* newprefab = (GameObject*)createPrefab(*prefab); */ - /*cam_inverse_goto(&camera, &newprefab->transform); */ - } +void game_start() { physOn = 1; } -} +void game_resume() { physOn = 1; } -void game_start() -{ - physOn = 1; -} +void game_stop() { physOn = 0; } -void game_resume() -{ - physOn = 1; -} +void game_pause() { physOn = 0; } -void game_stop() -{ - physOn = 0; -} +void sprite_gui(struct mSprite *sprite) { + nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", sprite->tex->path); + // ImGui::SameLine(); -void game_pause() -{ - physOn = 0; -} + if (nk_button_label(ctx, "Load texture") && selected_asset != NULL) { + sprite_loadtex(sprite, selected_asset->filename); + } + if (sprite->tex != NULL) { + nk_labelf(ctx, NK_TEXT_LEFT, "%s", sprite->tex->path); + nk_labelf(ctx, NK_TEXT_LEFT, "%dx%d", sprite->tex->width, + sprite->tex->height); + if (nk_button_label(ctx, "Imgbutton")) + editor_selectasset_str(sprite->tex->path); + // if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50, + // 50))) { + } + nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, + 0.01f); + nk_layout_row_dynamic(ctx, 25, 3); + if (nk_button_label(ctx, "C")) { + sprite->pos[0] = -0.5f; + sprite->pos[1] = -0.5f; + } + if (nk_button_label(ctx, "U")) { + sprite->pos[0] = -0.5f; + sprite->pos[1] = -1.f; + } - -void sprite_gui(struct mSprite *sprite) -{ - nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", sprite->tex->path); - //ImGui::SameLine(); - - if (nk_button_label(ctx, "Load texture") && selected_asset != NULL) { - sprite_loadtex(sprite, selected_asset->filename); - } - - if (sprite->tex != NULL) { - nk_labelf(ctx, NK_TEXT_LEFT, "%s", sprite->tex->path); - nk_labelf(ctx, NK_TEXT_LEFT, "%dx%d", sprite->tex->width, sprite->tex->height); - if (nk_button_label(ctx, "Imgbutton")) editor_selectasset_str(sprite->tex->path); - // if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50, 50))) { - } - nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f); - - nk_layout_row_dynamic(ctx, 25, 3); - if (nk_button_label(ctx, "C")) { - sprite->pos[0] = -0.5f; - sprite->pos[1] = -0.5f; - } - - - if (nk_button_label(ctx, "U")) { - sprite->pos[0] = -0.5f; - sprite->pos[1] = -1.f; - } - - - if (nk_button_label(ctx, "D")) { - sprite->pos[0] = -0.5f; - sprite->pos[1] = 0.f; - } - + if (nk_button_label(ctx, "D")) { + sprite->pos[0] = -0.5f; + sprite->pos[1] = 0.f; + } } \ No newline at end of file diff --git a/source/engine/editor.h b/source/engine/editor.h index 9aaedc0..a2aecef 100755 --- a/source/engine/editor.h +++ b/source/engine/editor.h @@ -31,10 +31,13 @@ struct editorVars { bool showLevel; }; -struct gameproject { - char name[127]; - char path[MAXPATH]; -}; +struct vec; +struct gameproject; +struct mSprite; + +extern struct gameproject *cur_project; +extern struct vec *projects; + struct Texture; struct mSDLWindow; @@ -51,13 +54,6 @@ void editor_makenewobject(); void editor_project_gui(); -void editor_init_project(struct gameproject *gp); -void editor_save_projects(); -void editor_load_projects(); -void editor_proj_select_gui(); -void editor_import_project(char *path); -void editor_make_project(char *path); - void editor_selectasset(struct fileasset *asset); void editor_selectasset_str(char *path); void editor_asset_gui(struct fileasset *asset); diff --git a/source/engine/font.c b/source/engine/font.c index b67fada..33a6479 100755 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -40,7 +40,7 @@ void font_init(struct mShader *textshader) { // Default font - font = MakeFont("notosans.ttf", 300); + font = MakeFont("teenytinypixels.ttf", 300); } void font_frame(struct mSDLWindow *w) { diff --git a/source/engine/nuke.c b/source/engine/nuke.c index 11e1b6b..dd9027e 100644 --- a/source/engine/nuke.c +++ b/source/engine/nuke.c @@ -39,7 +39,6 @@ void nuke_start() void nuke_end() { - nk_end(ctx); nk_glfw3_render(&nkglfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER); } diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index cb6422b..a70727e 100755 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -93,31 +93,6 @@ struct mSprite *tsprite = NULL; static unsigned int projUBO; -const char *textvert = -"#version 330 core\n" -"layout (location = 0) in vec4 vertex; \n" -"out vec2 TexCoords;\n" - -"uniform mat4 projection;\n" - -"void main() {\n" - " gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n" -" TexCoords = vec2(vertex.z, 1.0 - vertex.w);\n" -"}\n"; - -const char *textfrag = -"#version 330 core\n" -"in vec2 TexCoords;\n" -"out vec4 color;\n" - -"uniform sampler2D text;\n" -"uniform vec3 textColor;\n" - -"void main() { \n" -" vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);\n" -" color = vec4(textColor, 1.0) * sampled;\n" -"}\n"; - void openglInit() { if (!mainwin) { @@ -126,11 +101,9 @@ void openglInit() } ////// MAKE SHADERS - outlineShader = MakeShader("outlinevert.glsl", "outline.glsl"); spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl"); animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl"); textShader = MakeShader("textvert.glsl", "textfrag.glsl"); - //textShader = CreateShader(textverg, textfrag); shader_use(textShader); shader_setint(textShader, "text", 0); @@ -139,7 +112,7 @@ void openglInit() font_init(textShader); sprite_initialize(); - //debugdraw_init(); + debugdraw_init(); //glEnable(GL_STENCIL_TEST); @@ -168,8 +141,6 @@ void openglInit() shader_setUBO(spriteShader, "Projection", 0); shader_setUBO(textShader, "Projection", 0); shader_setUBO(animSpriteShader, "Projection", 0); - - } diff --git a/source/engine/window.c b/source/engine/window.c index 07ab32a..4f61b32 100755 --- a/source/engine/window.c +++ b/source/engine/window.c @@ -272,7 +272,13 @@ void window_render(struct mSDLWindow *w) { nuke_start(); script_call_sym(w->nuke_cb); nuke_end(); + } else if (w->nuke_gui != NULL) { + nuke_start(); + w->nuke_gui(); + nuke_end(); } + + window_swap(w); } diff --git a/source/engine/window.h b/source/engine/window.h index 372f1ba..a8222a1 100755 --- a/source/engine/window.h +++ b/source/engine/window.h @@ -23,6 +23,7 @@ struct mSDLWindow { float projection[16]; mrb_sym nuke_cb; mrb_sym gui_cb; + void (*nuke_gui)(); }; struct Texture; diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 752e475..fb9fa0c 100755 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -5,6 +5,8 @@ #include "input.h" #include "openglrender.h" +#include "string.h" + int physOn = 0; double renderlag = 0; @@ -15,13 +17,28 @@ double renderMS = 1/60.f; double physMS = 1/120.f; double updateMS = 1/60.f; +static int ed = 1; + int main(int argc, char **args) { + for (int i = 1; i < argc; i++) { + if (args[i][0] == '-') { + if (strncmp(args[i][1], "play", 4) == 0) { + ed = 0; + } + } + } + + engine_init(); window_set_icon("icon.png"); - script_dofile("game.rb"); + if (ed) { + editor_init(MakeSDLWindow("Editor", 600, 600, 0)); + } else { + script_dofile("game.rb"); + } openglInit(); @@ -45,7 +62,7 @@ int main(int argc, char **args) { input_poll(updateMS - elapsed < 0 ? 0 : updateMS - elapsed); window_all_handle_events(); - script_update(updateMS); + } } diff --git a/source/shaders/albedofrag.glsl b/source/shaders/3d/albedofrag.glsl similarity index 100% rename from source/shaders/albedofrag.glsl rename to source/shaders/3d/albedofrag.glsl diff --git a/source/shaders/fragment.glsl b/source/shaders/3d/fragment.glsl similarity index 100% rename from source/shaders/fragment.glsl rename to source/shaders/3d/fragment.glsl diff --git a/source/shaders/gizmofrag.glsl b/source/shaders/3d/gizmofrag.glsl similarity index 100% rename from source/shaders/gizmofrag.glsl rename to source/shaders/3d/gizmofrag.glsl diff --git a/source/shaders/gizmovert.glsl b/source/shaders/3d/gizmovert.glsl similarity index 100% rename from source/shaders/gizmovert.glsl rename to source/shaders/3d/gizmovert.glsl diff --git a/source/shaders/lightfrag.glsl b/source/shaders/3d/lightfrag.glsl similarity index 100% rename from source/shaders/lightfrag.glsl rename to source/shaders/3d/lightfrag.glsl diff --git a/source/shaders/lightvert.glsl b/source/shaders/3d/lightvert.glsl similarity index 100% rename from source/shaders/lightvert.glsl rename to source/shaders/3d/lightvert.glsl diff --git a/source/shaders/modelfrag.glsl b/source/shaders/3d/modelfrag.glsl similarity index 100% rename from source/shaders/modelfrag.glsl rename to source/shaders/3d/modelfrag.glsl diff --git a/source/shaders/modelvert.glsl b/source/shaders/3d/modelvert.glsl similarity index 100% rename from source/shaders/modelvert.glsl rename to source/shaders/3d/modelvert.glsl diff --git a/source/shaders/outline.glsl b/source/shaders/3d/outline.glsl similarity index 100% rename from source/shaders/outline.glsl rename to source/shaders/3d/outline.glsl diff --git a/source/shaders/outlinevert.glsl b/source/shaders/3d/outlinevert.glsl similarity index 100% rename from source/shaders/outlinevert.glsl rename to source/shaders/3d/outlinevert.glsl diff --git a/source/shaders/shadowfrag.glsl b/source/shaders/3d/shadowfrag.glsl similarity index 100% rename from source/shaders/shadowfrag.glsl rename to source/shaders/3d/shadowfrag.glsl diff --git a/source/shaders/shadowvert.glsl b/source/shaders/3d/shadowvert.glsl similarity index 100% rename from source/shaders/shadowvert.glsl rename to source/shaders/3d/shadowvert.glsl diff --git a/source/shaders/simplevert.glsl b/source/shaders/3d/simplevert.glsl similarity index 100% rename from source/shaders/simplevert.glsl rename to source/shaders/3d/simplevert.glsl diff --git a/source/shaders/skyfrag.glsl b/source/shaders/3d/skyfrag.glsl similarity index 100% rename from source/shaders/skyfrag.glsl rename to source/shaders/3d/skyfrag.glsl diff --git a/source/shaders/skyvert.glsl b/source/shaders/3d/skyvert.glsl similarity index 100% rename from source/shaders/skyvert.glsl rename to source/shaders/3d/skyvert.glsl diff --git a/source/shaders/vertex.glsl b/source/shaders/3d/vertex.glsl similarity index 100% rename from source/shaders/vertex.glsl rename to source/shaders/3d/vertex.glsl