diff --git a/Makefile b/Makefile index f00618b..918129a 100755 --- a/Makefile +++ b/Makefile @@ -71,13 +71,13 @@ includeflag != find source -type d -name include includeflag += $(engincs) source/engine/thirdparty/Nuklear includeflag := $(addprefix -I, $(includeflag)) -WARNING_FLAGS = -Wall -Wno-unused-function# -pedantic -Wextra -Wwrite-strings -Wno-incompatible-function-pointer-types -Wno-incompatible-pointer-types -Wno-unused-function +WARNING_FLAGS = -Wall -Wno-incompatible-function-pointer-types -Wno-unused-function# -pedantic -Wextra -Wwrite-strings -Wno-incompatible-function-pointer-types -Wno-incompatible-pointer-types -Wno-unused-function SEM = 0.0.1 COM != git rev-parse --short HEAD VER = $(SEM)-$(COM) -COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -I. -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" -march=native -std=c99 -c $< -o $@ +COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -I. -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" -c $< -o $@ LIBPATH = -L$(BIN) @@ -87,8 +87,8 @@ ifeq ($(OS), WIN32) CLIBS = EXT = .exe else - LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -rdynamic - ELIBS = engine pthread yughc glfw3 quickjs c m dl GL + LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -pthread -rdynamic + ELIBS = engine pthread yughc quickjs glfw3 GL c m dl CLIBS = endif diff --git a/source/engine/3d/3pfollow.h b/source/engine/3d/3pfollow.h index 5709e03..8bfd359 100644 --- a/source/engine/3d/3pfollow.h +++ b/source/engine/3d/3pfollow.h @@ -4,20 +4,21 @@ #define THIRDPERSONFOLLOW_H #include "transform.h" +#include "HandmadeMath.h" struct follow { float distance; HMM_Quat target_rot; }; -HMM_Vec3follow_calccenter(); -HMM_Vec3follow_postoffset(); -HMM_Vec3extentsoffset(); -HMM_Vec3framebasedveclerp(); +HMM_Vec3 follow_calccenter(); +HMM_Vec3 follow_postoffset(); +HMM_Vec3 extentsoffset(); +HMM_Vec3 framebasedveclerp(); int lerpparam(float offset, float anchorwidth, float floatwidth); -HMM_Vec3vec3lerp(HMM_Vec3 from, HMM_Vec3 to, HMM_Vec3 a); +HMM_Vec3 vec3lerp(HMM_Vec3 from, HMM_Vec3 to, HMM_Vec3 a); void follow_calctargets(); -HMM_Vec3follow_removelockedrot(); +HMM_Vec3 follow_removelockedrot(); void follow_targetoffset(struct follow *follow); int float_epsilon(float a, float b, float e); diff --git a/source/engine/datastream.c b/source/engine/datastream.c index b35c6c1..1e34673 100644 --- a/source/engine/datastream.c +++ b/source/engine/datastream.c @@ -44,7 +44,7 @@ static void render_audio(plm_t *mpeg, plm_samples_t *samples, void *user) { for (int i = 0; i < samples->count * CHANNELS; i++) { t = (short)(samples->interleaved[i] * SHRT_MAX); - cbuf_push(ds->astream->buf, t * 5); +// cbuf_push(ds->astream->buf, t * 5); } } diff --git a/source/engine/editor/ed_project.c b/source/engine/editor/ed_project.c deleted file mode 100644 index 45f7fd9..0000000 --- a/source/engine/editor/ed_project.c +++ /dev/null @@ -1,96 +0,0 @@ -#include "ed_project.h" - -#include -#include -#include "editor.h" - -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_make_project(char *path) -{ - FILE *f = path_open("w", "%s%s", path, "/project.yugh"); - cur_project = 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); - - editor_init_project(cur_project); - - editor_save_projects(); -} - -void editor_import_project(char *path) -{ - FILE *f = path_open("r", "%s%s", path, "/project.yugh"); - if (!f) - return; - - struct gameproject *gp = malloc(sizeof(*gp)); - fread(gp, sizeof(*gp), 1, f); - fclose(f); - - vec_add(projects, gp); -} - -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); - } - - ImGui::End(); - */ -} - - -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); -} diff --git a/source/engine/editor/ed_project.h b/source/engine/editor/ed_project.h deleted file mode 100644 index b916452..0000000 --- a/source/engine/editor/ed_project.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef ED_PROJECT_H -#define ED_PROJECT_H - -#include "config.h" - -struct gameproject { - char name[127]; - char path[MAXPATH]; -}; - -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); - -#endif diff --git a/source/engine/editor/editor.c b/source/engine/editor/editor.c deleted file mode 100644 index db5e991..0000000 --- a/source/engine/editor/editor.c +++ /dev/null @@ -1,1030 +0,0 @@ -void editor_init(){} - - -#ifdef GOOGLE - -#include "editor.h" -#include "ed_project.h" - -#include "2dphysics.h" -#include "camera.h" -#include "config.h" -#include "datastream.h" -#include "debug.h" -#include "debugdraw.h" -#include "editorstate.h" -#include "gameobject.h" -#include "input.h" -#include "level.h" -#include "math.h" -#include "openglrender.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 -#include "nuke.h" -#include "texture.h" - -#include "log.h" - -#include "ftw.h" - -extern struct nk_context *ctx; - -#include -#define ASSET_TEXT_BUF 1024 * 1024 /* 1 MB buffer for editing text files */ - -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}; - -// Lighting effect flags -static bool renderAO = true; -static bool renderDynamicShadows = true; - -// Debug render modes -static int renderGizmos = false; -static int showGrid = true; -static int debugDrawPhysics = false; - -const char *allowed_extensions[] = {"jpg", "png", "rb", "wav", "mp3", }; - -void text_ed_cb(GLFWwindow *win, unsigned int codepoint); -void asset_srch_cb(GLFWwindow *win, unsigned int codepoint) -{ - YughInfo("Pushed %d.", codepoint); -} - -static const char *editor_filename = "editor.ini"; - -static struct { - char *key; - struct fileasset *value; -} *assets = NULL; - -static char asset_search_buffer[100] = {'\0'}; - -struct fileasset *selected_asset; - -static int selected_index = -1; - -int show_desktop = 0; - -int tex_view = 0; - -static int grid1_width = 1; -static int grid1_span = 100; -static int grid2_width = 3; -static int grid2_span = 1000; -static int grid1_draw = true; -static int grid2_draw = true; - -static float tex_scale = 1.f; -static struct anim2d tex_gui_anim = {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, '.'); -} - -size_t asset_side_size(struct fileasset *asset) { - if (asset->type == ASSET_TYPE_IMAGE) - return sizeof(struct Texture); - - return 0; -} - -void save_asset(struct fileasset *asset) { - if (asset == NULL) { - YughWarn("No asset to save.", 0); - return; - } - - if (asset->type != ASSET_TYPE_IMAGE) return; - - FILE *f = res_open(str_replace_ext(asset->filename, EXT_ASSET), "w"); - fwrite(asset->data, asset_side_size(asset), 1, f); - fclose(f); -} - -void load_asset(struct fileasset *asset) { - if (asset == NULL) { - YughWarn("No asset to load.", 0); - return; - } - - if (asset->type != ASSET_TYPE_IMAGE) - return; - - if (asset->data == NULL) - asset->data = malloc(asset_side_size(asset)); - - FILE *f = res_open(str_replace_ext(asset->filename, EXT_ASSET), "r"); - if (f == NULL) - return; - - struct Texture tex; - struct Texture *asset_tex = asset->data; - - fread(&tex, asset_side_size(asset), 1, f); - - asset_tex->opts = tex.opts; - asset_tex->anim = tex.anim; - fclose(f); -} - - - -static int check_if_resource(const char *fpath, const struct stat *sb, int typeflag) { - if (typeflag != FTW_F) - return 0; - - const char *ext = get_extension(fpath); - if (ext && is_allowed_extension(ext)) { - struct fileasset *newasset = calloc(1, sizeof(struct fileasset)); - newasset->searched = true; - - if (!strcmp(ext+1, "png") || !strcmp(ext+1, "jpg")) - newasset->type = ASSET_TYPE_IMAGE; - else if (!strcmp(ext+1, "rb")) - newasset->type = ASSET_TYPE_TEXT; - else if (!strcmp(ext+1, "wav") || !strcmp(ext+1, "mp3")) - newasset->type = ASSET_TYPE_SOUND; - else - newasset->type = ASSET_TYPE_NULL; - - newasset->filename = strdup(fpath); - - shput(assets, newasset->filename, newasset); - } - - - return 0; -} - - -static void print_files_in_directory(const char *dirpath) { - struct fileasset *n = NULL; - for (int i = 0; i < shlen(assets); i++) { - free(assets[i].key); - free(assets[i].value); - } - - shfree(assets); - ftw(dirpath, check_if_resource, 10); -} - -static void get_all_files() { print_files_in_directory("."); } - - -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 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 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; -} - -void filter_asset_srch() -{ - if (asset_search_buffer[0] == '\0') { - for (int i = 0; i < shlen(assets); i++) - assets[i].value->searched = true; - } else { - for (int i = 0; i < shlen(assets); i++) - assets[i].value->searched = (strstr(assets[i].value->filename, asset_search_buffer) == NULL) ? false : true; - } -} - -void filter_autoindent() -{ - -} - - -void editor_save() { - FILE *feditor = fopen(editor_filename, "w+"); - fwrite(&editor, sizeof(editor), 1, feditor); - fclose(feditor); -} - -static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int mods) { - if (editor_wantkeyboard()) { - if (editor.asset_srch & NK_EDIT_ACTIVE) { - filter_asset_srch(); - } - - if ((editor.text_ed & NK_EDIT_ACTIVE) && key == GLFW_KEY_ENTER) { - filter_autoindent(); - } - - return; - } - - if (action == GLFW_RELEASE) { - switch(key) { - case GLFW_KEY_TAB: - show_desktop = 0; - break; - } - return; - } - - switch (key) { - case GLFW_KEY_ESCAPE: - quit(); - //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: - NEGATE(editor.assets.show); - break; - - case GLFW_KEY_F3: - NEGATE(editor.stats.show); - break; - - case GLFW_KEY_F4: - NEGATE(editor.hierarchy.show); - break; - - case GLFW_KEY_F5: - - break; - - case GLFW_KEY_F6: - NEGATE(editor.gamesettings.show); - break; - - case GLFW_KEY_F7: - NEGATE(editor.viewmode.show); - break; - - case GLFW_KEY_F8: - NEGATE(editor.debug.show); - break; - - case GLFW_KEY_F9: - NEGATE(editor.export.show); - break; - - case GLFW_KEY_F10: - NEGATE(editor.level.show); - break; - - case GLFW_KEY_F11: - window_togglefullscreen(mainwin); - break; - - case GLFW_KEY_GRAVE_ACCENT: - NEGATE(editor.repl.show); - 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; - - case GLFW_KEY_TAB: - show_desktop = 1; - 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 window *window) { - levels = vec_make(MAXPATH, 10); - get_levels(); - editor_load_projects(); - findPrefabs(); - - 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; - window_makefullscreen(window); - glfwSetKeyCallback(window->window, edit_input_cb); - glfwSetMouseButtonCallback(window->window, edit_mouse_cb); - - //glfwSetCharCallback(window->window, text_ed_cb); - //glfwSetCharCallback(window->window, asset_srch_cb); - - get_all_files(); -} - -int editor_wantkeyboard() { - if (editor.text_ed & NK_EDIT_ACTIVE) - return 1; - - if (editor.asset_srch & NK_EDIT_ACTIVE) - return 1; - - return 0; -} - -const int nuk_std = NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | 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); - - if (debugDrawPhysics) { - // for (int i = 0; i < number_of_gameobjects(); i++) - // phys2d_dbgdrawcircle(objects[i]->circle); - } - -/* - if (nk_menu_begin_label(ctx, "Windows", NK_TEXT_LEFT, nk_vec2(100, 200))) { - nk_layout_row_dynamic(ctx, 25, 1); - - nk_checkbox_label(ctx, "Resources", &editor.assets.show); - nk_checkbox_label(ctx, "Hierarchy", &editor.hierarchy.show); - nk_checkbox_label(ctx, "Lighting F5", &editor.lighting.show); - nk_checkbox_label(ctx, "Game Settings F6", &editor.gamesettings.show); - nk_checkbox_label(ctx, "View F7", &editor.viewmode.show); - nk_checkbox_label(ctx, "Debug F8", &editor.debug.show); - nk_checkbox_label(ctx, "Export F9", &editor.export.show); - nk_checkbox_label(ctx, "Level F10", &editor.level.show); - nk_checkbox_label(ctx, "REPL", &editor.repl.show); - - nk_menu_end(ctx); - } -*/ - NK_MENU_START(level) - nuke_nel(1); - nuke_labelf("Current level: %s", current_level[0] == '\0' ? "No level loaded." : current_level); - - nuke_nel(3); - if (nuke_btn("New")) { - new_level(); - current_level[0] = '\0'; - } - - if (nuke_btn("Save")) { - if (strlen(current_level) == 0) { - YughWarn("Can't save level that has no name."); - } else { - save_level(current_level); - get_levels(); - } - } - - if (nuke_btn("Save as")) { - if (strlen(current_level) == 0) { - YughWarn("Can't save level that has no name."); - } else { - strcpy(current_level, levelname); - strncat(current_level, EXT_LEVEL, 10); - save_level(current_level); - levelname[0] = '\0'; - get_levels(); - - - - } - } - nuke_nel(1); - nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, levelname, MAXNAME - 1, nk_filter_default); - - - vec_walk(levels, editor_level_btn); - NK_MENU_END() - - NK_MENU_START(export) - nuke_nel(2); - if (nuke_btn("Bake")) { - } - if (nuke_btn("Build")) { - } - - NK_MENU_END() - - NK_MENU_START(gamesettings) - nk_layout_row_dynamic(ctx,25,1); - - // nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, cur_project->name, - // 126, nk_filter_default); - - if (nuke_push_tree_id("Physics", 0)) { - nuke_prop_float("2d Gravity", -5000.f, &phys2d_gravity, 0.f, 1.f, 0.1f); - //phys2d_apply(); - nk_tree_pop(ctx); - } - - if (nuke_push_tree_id("Quality", 0)) { - nk_tree_pop(ctx); - } - - NK_MENU_END() - - NK_MENU_START(stats) - nuke_labelf("FPS: %2.4f", 1.f / deltaT); - nuke_labelf("Triangles rendered: %llu", triCount); - NK_MENU_END() - - NK_MENU_START(repl) - - nk_layout_row_dynamic(ctx, 300, 1); - nk_edit_string_zero_terminated(ctx, NK_EDIT_MULTILINE|NK_EDIT_READ_ONLY|NK_EDIT_GOTO_END_ON_ACTIVATE, lastlog, ERROR_BUFFER, NULL); - static char buffer[512] = {'\0'}; - nk_layout_row_dynamic(ctx, 25, 2); - nk_flags active = nk_edit_string_zero_terminated(ctx, NK_EDIT_BOX | NK_EDIT_SIG_ENTER|NK_EDIT_AUTO_SELECT, buffer, 512-1, nk_filter_ascii); - if (active & NK_EDIT_COMMITED || nuke_btn("Submit")) { - char bigbuf[1024]; - snprintf(bigbuf, 1024, "(loginfo %s)", buffer); - script_run(bigbuf); - buffer[0] = '\0'; - } - - NK_MENU_END() - - NK_MENU_START(hierarchy) - nuke_nel(1); - - if (nuke_btn("New Object")) { - MakeGameobject(); - } - - obj_gui_hierarchy(selectedobject); - - NK_MENU_END() - - NK_FORCE(simulate) - nuke_nel(2); - if (physOn) { - if (nuke_btn("Pause")) - game_pause(); - - if (nuke_btn("Stop")) - game_stop(); - } else { - if (nuke_btn("Play")) - game_start(); - } - - NK_FORCE_END() - - NK_FORCE(prefab) - nuke_nel(1); - - vec_walk(prefabs, editor_prefab_btn); - NK_FORCE_END() - - NK_MENU_START(assets) - nuke_nel(1); - editor.asset_srch = nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, asset_search_buffer, 100, nk_filter_ascii); - - if (nuke_btn("Reload all files")) - get_all_files(); - - - for (int i = 0; i < shlen(assets); i++) { - if (!assets[i].value->searched) - continue; - - if (nuke_btn(assets[i].key)) - editor_selectasset_str(assets[i].key); - - } - - NK_MENU_END() - - if (selected_asset) - editor_asset_gui(selected_asset); - - - NK_MENU_START(debug) - nuke_nel(1); - if (nk_tree_push(ctx, NK_TREE_NODE, "Debug Draws", NK_MINIMIZED)) { - nuke_checkbox("Gizmos", &renderGizmos); - nuke_checkbox("Grid", &showGrid); - nuke_checkbox("Physics", &debugDrawPhysics); - nk_tree_pop(ctx); - } - - if (nuke_btn("Reload Shaders")) { - shader_compile_all(); - } - - nuke_property_int("Grid 1 Span", 1, &grid1_span, 500, 1); - nuke_checkbox("Draw", &grid1_draw); - - nuke_property_int("Grid 2 Span", 10, &grid2_span, 1000, 1); - nuke_checkbox("Draw", &grid2_draw); - - nuke_property_float("Grid Opacity", 0.f, &gridOpacity, 1.f, 0.01f, 0.01f); - nuke_property_float("Small unit", 0.5f, &smallGridUnit, 5.f, 0.1f, 0.1f); - nuke_property_float("Big unit", 10.f, &bigGridUnit, 50.f, 1.f, 0.1f); - nuke_property_float("Small thickness", 1.f, &gridSmallThickness, 10.f, 0.1f, 0.1f); - nuke_property_float("Big thickness", 1.f, &gridBigThickness, 10.f, 0.1f, 0.1f); - - static struct nk_colorf smgrd; - static struct nk_colorf lgrd; - - nk_color_pick(ctx, &smgrd, NK_RGBA); - nk_color_pick(ctx, &lgrd, NK_RGBA); - - NK_MENU_END() - -startobjectgui: - - if (selectedobject) { - cpVect pos = cpBodyGetPosition(selectedobject->body); - draw_point(pos.x, pos.y, 5); - - NK_FORCE(gameobject) - - nuke_nel(3); - - - if (nuke_btn("Save")) - gameobject_saveprefab(selectedobject); - - if (nuke_btn("Del")) { - gameobject_delete(selected_index); - pickGameObject(-1); - nk_end(ctx); - goto startobjectgui; - } - - if (selectedobject->editor.prefabSync && nuke_btn("Revert")) - gameobject_revertprefab(selectedobject); - - nuke_label("Name"); - nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, selectedobject->editor.mname, 50, nk_filter_ascii); - - nuke_label("Prefab"); - nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, selectedobject->editor.prefabName, 50, nk_filter_ascii); - - object_gui(selectedobject); - - - // nuke_label("Components"); - nuke_nel(3); - - for (int i = 0; i < gameobject_ncomponents(selectedobject); i++) { - if (nuke_btn(selectedobject->components[i].ref->name)) { - gameobject_addcomponent(selectedobject, &selectedobject->components[i]); - } - } - - NK_FORCE_END() - -/* - NK_FORCE(components) - nuke_nel(1); - - for (int i = 0; i < ncomponent; i++) { - if (nuke_btn(components[i].name)) { - gameobject_addcomponent(selectedobject, &components[i]); - } - } - - NK_FORCE_END() - */ - } -} - -void editor_render() { editor_project_gui(); } - -void pickGameObject(int pickID) { - if (pickID >= 0 && pickID < arrlen(gameobjects)) { - selected_index = pickID; - selectedobject = &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 (nuke_btn(level)) { - load_level(level); - strcpy(current_level, level); - } -} - -struct fileasset *asset_from_path(const char *p) -{ - return shget(assets, p); -} - -void editor_selectasset_str(const char *path) { - struct fileasset *asset = asset_from_path(path); - - - - FILE *fasset; - - switch (asset->type) { - case ASSET_TYPE_IMAGE: - if (asset->data == NULL) { - asset->data = texture_loadfromfile(path); - load_asset(asset); - } - //else - //tex_pull(asset->data); - - struct Texture *tex = asset->data; - tex_gui_anim.anim = &tex->anim; - tex_anim_set(&tex_gui_anim); - anim_setframe(&tex_gui_anim, 0); - float tex_scale = (float) ASSET_WIN_SIZE / (float)tex_gui_anim.anim->tex->width; - if (tex_scale >= 10.f) { - tex_scale = 10.f; - } - - break; - - case ASSET_TYPE_TEXT: - 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); - break; - - case ASSET_TYPE_SOUND: - break; - - default: - break; - } - - load_asset(asset); - - if (selected_asset != NULL) - save_asset(selected_asset); - - selected_asset = asset; - -} - -void editor_asset_tex_gui(struct Texture *tex) { - nuke_labelf("%dx%d", tex->width, tex->height); - nuke_prop_float("Zoom", 0.01f, &tex_scale, 10.f, 0.1f, 0.01f); - int old_sprite = tex->opts.sprite; - - nuke_checkbox("Sprite", &tex->opts.sprite); - - //if (old_sprite != tex->opts.sprite) - //tex_gpu_load(tex); - - nuke_nel(4); - nuke_radio_btn("Raw", &tex_view, 0); - nuke_radio_btn("View 1", &tex_view, 1); - nuke_radio_btn("View 2", &tex_view, 2); - - nuke_checkbox("Animation", &tex->opts.animation); - - - if (tex->opts.animation) { - /* - int old_frames = tex->anim.frames; - int old_ms = tex->anim.ms; - - nuke_nel(2); - nuke_property_int("Frames", 1, &tex->anim.frames, 20, 1); - nuke_property_int("FPS", 1, &tex->anim.ms, 24, 1); - - if (tex_gui_anim.playing) { - if (nuke_btn("Pause")) - anim_pause(&tex_gui_anim); - if (tex_gui_anim.playing && nuke_btn("Stop")) - anim_stop(&tex_gui_anim); - } else { - nuke_nel(3); - if (nuke_btn("Play")) - anim_play(&tex_gui_anim); - - if (nuke_btn("Bkwd")) - anim_bkwd(&tex_gui_anim); - - if (nuke_btn("Fwd")) - anim_fwd(&tex_gui_anim); - } - - nuke_nel(1); - nuke_labelf("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); - - nk_layout_row_static(ctx, tex->height*tex_scale*(tex_gui_anim.st.t1-tex_gui_anim.st.t0), tex->width*tex_scale*(tex_gui_anim.st.s1 - tex_gui_anim.st.s0), 1); - struct nk_rect r; - r.x = tex_gui_anim.st.s0*tex->width; - r.y = tex_gui_anim.st.t0*tex->height; - r.w = st_s_w(tex_gui_anim.st)*tex->width; - r.h = st_s_h(tex_gui_anim.st)*tex->height; - - nk_image(ctx, nk_subimage_id(tex->id, tex->width, tex->height, r)); - */ - } else { - nk_layout_row_static(ctx, tex->height*tex_scale, tex->width*tex_scale, 1); - nk_image(ctx, nk_image_id(tex->id)); - } -} - -void text_ed_cb(GLFWwindow *win, unsigned int codepoint) -{ - YughInfo("Pressed button %d", codepoint); - if (editor.text_ed & NK_EDIT_ACTIVE) { - if (codepoint == '\n') { - YughInfo("Hit newline."); - } - } -} - -void editor_asset_text_gui(char *text) { - nk_layout_row_dynamic(ctx, 600, 1); - editor.text_ed = nk_edit_string_zero_terminated(ctx, NK_EDIT_BOX, text, ASSET_TEXT_BUF, nk_filter_ascii); - - nuke_nel(4); - if (nuke_btn("Save")) { - FILE *f = fopen(selected_asset->filename, "wd"); - size_t len = strlen(text); - fwrite(text, len, 1, f); - fclose(f); - } - - /* TODO: Nicer formatting for text input. Auto indent. */ -} - -void editor_asset_sound_gui(struct wav *wav) -{ - -} - -void editor_asset_gui(struct fileasset *asset) { - - NK_FORCE(asset) - - nuke_nel(2); - nk_labelf(ctx, NK_TEXT_LEFT, "%s", selected_asset->filename); - - if (nuke_btn("Close")) - selected_asset = NULL; - - nuke_nel(1); - switch (asset->type) { - case ASSET_TYPE_NULL: - break; - - case ASSET_TYPE_IMAGE: - editor_asset_tex_gui(asset->data); - break; - - case ASSET_TYPE_TEXT: - editor_asset_text_gui(asset->data); - break; - - case ASSET_TYPE_SOUND: - editor_asset_sound_gui(asset->data); - break; - } - - NK_FORCE_END() -} - -void editor_makenewobject() {} - -int obj_gui_hierarchy(struct gameobject *selected) { - - for (int i = 0; i < arrlen(gameobjects); i++) { - struct gameobject *go = &gameobjects[i]; - - if (nk_select_label(ctx, go->editor.mname, NK_TEXT_LEFT, go == selected)) { - if (go != selected) - pickGameObject(i); - } - } - - return 0; -} - -void get_levels() { fill_extensions(levels, DATA_PATH, EXT_LEVEL); } - -void editor_prefab_btn(char *prefab) { - if (nuke_btn(prefab)) { - YughInfo("Making prefab '%s'.", prefab); - gameobject_makefromprefab(prefab); - } -} - -void game_start() { physOn = 1; } - -void game_resume() { physOn = 1; } - -void game_stop() { physOn = 0; } - -void game_pause() { physOn = 0; } - -void sprite_gui(struct sprite *sprite) { - - nuke_nel(2); - //nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", tex_get_path(sprite->tex)); - - - - if (nuke_btn("Load texture") && selected_asset != NULL) { - sprite_loadtex(sprite, selected_asset->filename); - } - - - if (sprite->tex != NULL) { - //nk_labelf(ctx, NK_TEXT_LEFT, "%s", tex_get_path(sprite->tex)); - nk_labelf(ctx, NK_TEXT_LEFT, "%dx%d", sprite->tex->width, sprite->tex->height); - - nk_layout_row_static(ctx, sprite->tex->height, sprite->tex->width, 1); - if (nk_button_image(ctx, nk_image_id(sprite->tex->id))) - editor_selectasset_str(tex_get_path(sprite->tex)); - } - - - nuke_property_float2("Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f); - - nuke_nel(3); - if (nuke_btn("C")) { - sprite->pos[0] = -0.5f; - sprite->pos[1] = -0.5f; - } - - if (nuke_btn("U")) { - sprite->pos[0] = -0.5f; - sprite->pos[1] = -1.f; - } - - if (nuke_btn("D")) { - sprite->pos[0] = -0.5f; - sprite->pos[1] = 0.f; - } - -} - -#endif diff --git a/source/engine/editor/editor.h b/source/engine/editor/editor.h deleted file mode 100644 index 6149995..0000000 --- a/source/engine/editor/editor.h +++ /dev/null @@ -1,108 +0,0 @@ -#ifndef EDITOR_H -#define EDITOR_H - -#include "config.h" -#include -#include "resources.h" - -#include "nuke.h" - -#define ASSET_TYPE_NULL 0 -#define ASSET_TYPE_IMAGE 1 -#define ASSET_TYPE_TEXT 2 -#define ASSET_TYPE_SOUND 3 - -struct fileasset { - char *filename; - bool searched; - short type; - void *data; // Struct of the underlying asset - Texture struct, etc -}; - -typedef struct { - bool show; - struct nk_rect rect; -} editor_win; - -struct editorVars { - editor_win stats; - editor_win hierarchy; - editor_win gamesettings; - editor_win viewmode; - editor_win debug; - editor_win assets; - editor_win asset; - editor_win repl; - editor_win export; - editor_win level; - editor_win gameobject; - editor_win components; - editor_win simulate; - editor_win prefab; - nk_flags text_ed; - nk_flags asset_srch; -}; - -struct gameobject; - -extern int show_desktop; - -#define NK_MENU_START(VAR) if (editor.VAR.show && !show_desktop) { \ - if (editor.VAR.rect.w == 0) editor.VAR.rect = nk_rect_std; \ - if (nuke_begin(#VAR, editor.VAR.rect, nuk_std)) { \ - editor.VAR.rect = nuke_win_get_bounds(); - -#define NK_MENU_END() } nuke_stop(); } - -#define NK_FORCE(VAR) if (editor.VAR.rect.w == 0) editor.VAR.rect = nk_rect_std; \ - if (!show_desktop && nuke_begin(#VAR, editor.VAR.rect, nuk_std)) { \ - editor.VAR.rect = nuke_win_get_bounds(); - -#define NK_FORCE_END() nuke_stop(); } - -#define NEGATE(VAR) VAR = ! VAR - -struct vec; -struct gameproject; -struct sprite; - -extern struct gameproject *cur_project; -extern struct vec *projects; - - -struct Texture; -struct window; - -void pickGameObject(int pickID); -int is_allowed_extension(const char *ext); - -void editor_init(struct window *window); -void editor_input(); -void editor_render(); -int editor_wantkeyboard(); -void editor_save(); -void editor_makenewobject(); - -void editor_project_gui(); - -void editor_selectasset(struct fileasset *asset); -void editor_selectasset_str(const char *path); -void editor_asset_gui(struct fileasset *asset); -void editor_asset_tex_gui(struct Texture *tex); -void editor_asset_text_gui(char *text); - -void editor_level_btn(char *level); -void editor_prefab_btn(char *prefab); - -void game_start(); -void game_resume(); -void game_stop(); -void game_pause(); - -void get_levels(); - -int obj_gui_hierarchy(struct gameobject *selected); - -void sprite_gui(struct sprite *sprite); - -#endif diff --git a/source/engine/editor/editorstate.c b/source/engine/editor/editorstate.c deleted file mode 100644 index 7e2ec62..0000000 --- a/source/engine/editor/editorstate.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "editorstate.h" -#include - -/* -void (*asset_command)(char *asset) = print_file; - -void print_file(char *file) -{ - YughInfo("File path: %s", file); -} - -void set_new_model(char *modelPath) -{ - printf("Loading new model: %s\n", modelPath); - curActor->model = GetExistingModel(modelPath); - strcpy(curActor->currentModelPath, modelPath); -} - -*/ diff --git a/source/engine/editor/editorstate.h b/source/engine/editor/editorstate.h deleted file mode 100644 index bb23f7c..0000000 --- a/source/engine/editor/editorstate.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef EDITORSTATE_H -#define EDITORSTATE_H - -void set_new_model(char *modelPath); - -extern void (*asset_command)(char *asset); - -void print_file(char *file); - - - -#endif diff --git a/source/engine/engine.c b/source/engine/engine.c index 2053e97..3ab4d5f 100644 --- a/source/engine/engine.c +++ b/source/engine/engine.c @@ -16,10 +16,6 @@ #define PL_MPEG_IMPLEMENTATION #include -#ifdef EDITOR -#include "editor.h" -#endif - #include "render.h" #include "2dphysics.h" @@ -33,9 +29,7 @@ #include "sprite.h" #include "timer.h" #include "window.h" - #include "sound.h" - #include "engine.h" void error_callback(int error, const char *description) { @@ -44,6 +38,7 @@ void error_callback(int error, const char *description) { } void engine_init() { + glfwSetErrorCallback(error_callback); /* Initialize GLFW */ if (!glfwInit()) { diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 8cc2570..2dbbe73 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -5,7 +5,6 @@ #include "anim.h" #include "debug.h" #include "debugdraw.h" -#include "editor.h" #include "engine.h" #include "font.h" #include "gameobject.h" diff --git a/source/engine/font.c b/source/engine/font.c index a5d71cc..88205d4 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -235,7 +235,7 @@ void draw_char_box(struct Character c, cpVect cursor, float scale, struct rgba c cursor.x += wh.x / 2.f; cursor.y += wh.y / 2.f; - draw_box(cursor, wh, color); +// draw_box(cursor, wh, color); } void text_flush() { diff --git a/source/engine/script.c b/source/engine/script.c index 40fb1fe..c98527c 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -42,6 +42,34 @@ void script_startup() { JSValue num_cache[100] = {0}; +int js_print_exception(JSValue v) { +#ifdef DBG + if (JS_IsException(v)) { + JSValue exception = JS_GetException(js); + + /* TODO: Does it need freed if null? */ + if (JS_IsNull(exception)) + return 0; + + JSValue val = JS_GetPropertyStr(js, exception, "stack"); + const char *name = JS_ToCString(js, JS_GetPropertyStr(js, exception, "name")); + const char *msg = JS_ToCString(js, JS_GetPropertyStr(js, exception, "message")); + const char *stack = JS_ToCString(js, val); + YughLog(LOG_SCRIPT, LOG_ERROR, "%s :: %s\n%s", name, msg,stack); + + JS_FreeCString(js, name); + JS_FreeCString(js, msg); + JS_FreeCString(js, stack); + JS_FreeValue(js,val); + JS_FreeValue(js,exception); + + return 1; + } +#endif + return 0; +} + + void script_init() { /* Load all prefabs into memory */ // if (DBG) @@ -89,32 +117,6 @@ void js_dump_stack() { js_stacktrace(); } -int js_print_exception(JSValue v) { -#ifdef DBG - if (JS_IsException(v)) { - JSValue exception = JS_GetException(js); - - /* TODO: Does it need freed if null? */ - if (JS_IsNull(exception)) - return 0; - - JSValue val = JS_GetPropertyStr(js, exception, "stack"); - const char *name = JS_ToCString(js, JS_GetPropertyStr(js, exception, "name")); - const char *msg = JS_ToCString(js, JS_GetPropertyStr(js, exception, "message")); - const char *stack = JS_ToCString(js, val); - YughLog(LOG_SCRIPT, LOG_ERROR, "%s :: %s\n%s", name, msg,stack); - - JS_FreeCString(js, name); - JS_FreeCString(js, msg); - JS_FreeCString(js, stack); - JS_FreeValue(js,val); - JS_FreeValue(js,exception); - - return 1; - } -#endif - return 0; -} diff --git a/source/engine/sound.c b/source/engine/sound.c index fa4331b..6a7f16d 100644 --- a/source/engine/sound.c +++ b/source/engine/sound.c @@ -151,7 +151,7 @@ void free_sound(const char *wav) { struct soundstream *soundstream_make() { struct soundstream *new = malloc(sizeof(*new)); - new->buf = circbuf_make(sizeof(short), BUF_FRAMES * CHANNELS * 2); +// new->buf = circbuf_make(sizeof(short), BUF_FRAMES * CHANNELS * 2); return new; } @@ -278,10 +278,10 @@ void mp3_fillbuf(struct sound *s, short *buf, int n) { } void soundstream_fillbuf(struct soundstream *s, short *buf, int n) { - int max = s->buf->write - s->buf->read; + int max = 1;//s->buf->write - s->buf->read; int lim = (max < n * CHANNELS) ? max : n * CHANNELS; for (int i = 0; i < lim; i++) { - buf[i] = cbuf_shift(s->buf); +// buf[i] = cbuf_shift(s->buf); } } diff --git a/source/engine/sound/dsp.c b/source/engine/sound/dsp.c index 9074d42..bcc7d1c 100644 --- a/source/engine/sound/dsp.c +++ b/source/engine/sound/dsp.c @@ -426,10 +426,10 @@ struct dsp_delay dsp_delay_make(unsigned int ms_delay) /* Circular buffer size is enough to have the delay */ unsigned int datasize = ms_delay * CHANNELS * (SAMPLERATE / 1000); - new.buf = circbuf_init(sizeof(short), datasize); - new.buf.write = datasize; +// new.buf = circbuf_init(sizeof(short), datasize); +// new.buf.write = datasize; - YughInfo("Buffer size is %u.", new.buf.len); +// YughInfo("Buffer size is %u.", new.buf.len); return new; } @@ -440,8 +440,8 @@ void dsp_delay_filbuf(struct dsp_delay *delay, short *buf, int n) dsp_run(delay->in, cache, n); for (int i = 0; i < n*CHANNELS; i++) { - cbuf_push(&delay->buf, cache[i] / 2); - buf[i] = cache[i] + cbuf_shift(&delay->buf); +// cbuf_push(&delay->buf, cache[i] / 2); +// buf[i] = cache[i] + cbuf_shift(&delay->buf); } } diff --git a/source/engine/sound/dsp.h b/source/engine/sound/dsp.h index d2f741c..6eef841 100644 --- a/source/engine/sound/dsp.h +++ b/source/engine/sound/dsp.h @@ -5,8 +5,7 @@ #define BUF_FRAMES 128 /* At 48k, 128 needed for 240fps consistency */ #define CHANNELS 2 - -#include "circbuf.h" +//#include "circbuf.h" struct dsp_iir; @@ -66,7 +65,7 @@ struct dsp_filter make_adsr(unsigned int atk, unsigned int dec, unsigned int sus struct dsp_delay { unsigned int ms_delay; - struct circbuf buf; +// struct circbuf buf; struct dsp_filter in; }; diff --git a/source/engine/window.c b/source/engine/window.c index 0ad07e6..8ad4505 100644 --- a/source/engine/window.c +++ b/source/engine/window.c @@ -233,6 +233,7 @@ void window_makecurrent(struct window *w) { if (w->window != glfwGetCurrentContext()) glfwMakeContextCurrent(w->window); + glViewport(0, 0, w->width, w->height); } diff --git a/source/engine/yugine.c b/source/engine/yugine.c index ec7f005..71b8269 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -24,7 +24,10 @@ #include "2dphysics.h" +#ifdef __linux__ #include +#endif + #include #include @@ -32,7 +35,7 @@ #define SOKOL_TRACE_HOOKS #define SOKOL_GFX_IMPL -#define SOKOL_GLCORE33 +#define SOKOL_GLES3 #include "sokol/sokol_gfx.h" int physOn = 0; @@ -81,7 +84,9 @@ int backtrace(void **buffer, int size) { } #endif + void print_stacktrace() { +#ifdef __linux__ void *ents[512]; size_t size = backtrace(ents, 512); @@ -94,6 +99,7 @@ void print_stacktrace() { YughCritical(stackstr[i]); js_stacktrace(); +#endif } void seghandle(int sig) {