This commit is contained in:
John Alanbrook 2022-08-14 19:19:36 +00:00
parent b0d2757f70
commit 28c69ff62f
27 changed files with 900 additions and 1117 deletions

View file

@ -89,18 +89,18 @@ yuginec = ./source/engine/yugine.c
ENGINE = $(BIN)libengine.a ENGINE = $(BIN)libengine.a
INCLUDE = $(BIN)include INCLUDE = $(BIN)include
LINK = $(LIBPATH) $(LINKER_FLAGS) $(LELIBS) -o $@ LINK = $(LIBPATH) $(LINKER_FLAGS) $(LELIBS)
engine: $(yuginec:.%.c=$(objprefix)%.o) $(ENGINE) engine: $(yuginec:.%.c=$(objprefix)%.o) $(ENGINE)
@echo Linking engine @echo Linking engine
$(CLINK) $< $(LINK) $(CLINK) $< $(LINK) -o $@
@echo Finished build @echo Finished build
bs: engine bs: engine
cp engine brainstorm cp engine brainstorm
ed: engine pin: engine
cp engine editor cp engine pinball
$(ENGINE): $(eobjects) bin/libglfw3.a $(ENGINE): $(eobjects) bin/libglfw3.a
@echo Making library engine.a @echo Making library engine.a

View file

View file

@ -1,10 +0,0 @@
window = win_make("editor")
nuke_cb(window, :editor)
def editor
editor_render()
end
def update(dt)
end

477
source/engine/editor.c Executable file → Normal file
View file

@ -1,44 +1,41 @@
#include "nuke.h" #include "nuke.h"
#include "openglrender.h" #include "2dphysics.h"
#include "editor.h"
#include "window.h"
#include "resources.h"
#include "registry.h"
#include "datastream.h"
#include "gameobject.h"
#include "camera.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 "shader.h"
#include "sound.h"
#include "sprite.h"
#include "texture.h"
#include "vec.h"
#include "window.h"
#include <chipmunk/chipmunk.h>
#include <ctype.h>
#include <dirent.h> #include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include "editorstate.h"
#include <stdlib.h> #include <stdlib.h>
#include "input.h"
#include "2dphysics.h"
#include "debugdraw.h"
#include "level.h"
#include "texture.h"
#include "sprite.h"
#include <chipmunk/chipmunk.h>
#include "math.h"
#include <ctype.h>
#include "config.h"
#include "vec.h"
#include "debug.h"
#include <stdlib.h>
#include "script.h"
#include "sound.h"
#define __USE_XOPEN_EXTENDED 1 #define __USE_XOPEN_EXTENDED 1
#include "ftw.h" #include "ftw.h"
#include <stb_ds.h> #include <stb_ds.h>
#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 gameproject *cur_project = NULL;
struct vec *projects; struct vec *projects = NULL;
static char setpath[MAXPATH]; static char setpath[MAXPATH];
// Menus // Menus
@ -88,22 +85,18 @@ static struct vec *levels = NULL;
static const int ASSET_WIN_SIZE = 512; static const int ASSET_WIN_SIZE = 512;
static const char *get_extension(const char *filepath) static const char *get_extension(const char *filepath) {
{
return strrchr(filepath, '.'); return strrchr(filepath, '.');
} }
static int static int check_if_resource(const char *fpath, const struct stat *sb,
check_if_resource(const char *fpath, const struct stat *sb, int typeflag, int typeflag, struct FTW *ftwbuf) {
struct FTW *ftwbuf)
{
if (typeflag == FTW_F) { if (typeflag == FTW_F) {
const char *ext = get_extension(fpath); const char *ext = get_extension(fpath);
if (ext && is_allowed_extension(ext)) { if (ext && is_allowed_extension(ext)) {
struct fileasset *newasset = struct fileasset *newasset =
(struct fileasset *)calloc(1, sizeof(struct fileasset)); (struct fileasset *)calloc(1, sizeof(struct fileasset));
newasset->filename = newasset->filename = (char *)malloc(sizeof(char) * strlen(fpath) + 1);
(char *) malloc(sizeof(char) * strlen(fpath) + 1);
strcpy(newasset->filename, fpath); strcpy(newasset->filename, fpath);
newasset->extension_len = strlen(ext); newasset->extension_len = strlen(ext);
newasset->searched = true; newasset->searched = true;
@ -114,20 +107,15 @@ check_if_resource(const char *fpath, const struct stat *sb, int typeflag,
return 0; return 0;
} }
static void print_files_in_directory(const char *dirpath) static void print_files_in_directory(const char *dirpath) {
{
int nflags = 0; int nflags = 0;
shfree(assets); shfree(assets);
nftw(dirpath, &check_if_resource, 10, nflags); nftw(dirpath, &check_if_resource, 10, nflags);
} }
static void get_all_files() static void get_all_files() { print_files_in_directory(DATA_PATH); }
{
print_files_in_directory(DATA_PATH);
}
static int *compute_prefix_function(const char *str) static int *compute_prefix_function(const char *str) {
{
int str_len = strlen(str); int str_len = strlen(str);
int *pi = (int *)malloc(sizeof(int) * str_len); int *pi = (int *)malloc(sizeof(int) * str_len);
pi[0] = 0; pi[0] = 0;
@ -146,8 +134,7 @@ static int *compute_prefix_function(const char *str)
return pi; return pi;
} }
static bool kmp_match(const char *search, const char *text, int *pi) static bool kmp_match(const char *search, const char *text, int *pi) {
{
int s_len = strlen(search); int s_len = strlen(search);
int t_len = strlen(text); int t_len = strlen(text);
// int *pi = compute_prefix_function(search); // int *pi = compute_prefix_function(search);
@ -172,10 +159,7 @@ static bool kmp_match(const char *search, const char *text, int *pi)
return found; return found;
} }
static int MyCallback() {
static int MyCallback()//ImGuiInputTextCallbackData * data)
{
/* /*
if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) { if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) {
data->InsertChars(data->CursorPos, ".."); data->InsertChars(data->CursorPos, "..");
@ -227,7 +211,9 @@ static int TextEditCallback()//ImGuiInputTextCallbackData * data)
while (*c != '\n') while (*c != '\n')
c--; c--;
c++; c++;
if (isblank(*c)) { if (isblank(*c)) {
char *ce = c; char *ce = c;
@ -244,21 +230,23 @@ static int TextEditCallback()//ImGuiInputTextCallbackData * data)
return 0; return 0;
} }
void editor_save() void editor_save() {
{
FILE *feditor = fopen(editor_filename, "w+"); FILE *feditor = fopen(editor_filename, "w+");
fwrite(&editor, sizeof(editor), 1, feditor); fwrite(&editor, sizeof(editor), 1, feditor);
fclose(feditor); fclose(feditor);
} }
static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int mods) static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int mods) {
{ if (editor_wantkeyboard())
if (editor_wantkeyboard()) return; return;
if (action != GLFW_PRESS)
return;
switch (key) { switch (key) {
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
quit = true; quit = true;
editor_save_projects(); //editor_save_projects();
editor_save(); editor_save();
break; break;
@ -359,13 +347,12 @@ static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int
} }
*/ */
break; break;
} }
} }
static void edit_mouse_cb(GLFWwindow *w, int button, int action, int mods) static void edit_mouse_cb(GLFWwindow *w, int button, int action, int mods) {
{ if (editor_wantkeyboard())
if (editor_wantkeyboard()) return; return;
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
switch (button) { switch (button) {
@ -400,11 +387,7 @@ static void edit_mouse_cb(GLFWwindow *w, int button, int action, int mods)
} }
} }
void editor_init(struct mSDLWindow *window) {
void editor_init(struct mSDLWindow *window)
{
projects = vec_make(sizeof(struct gameproject), 5);
levels = vec_make(MAXNAME, 10); levels = vec_make(MAXNAME, 10);
editor_load_projects(); editor_load_projects();
@ -417,38 +400,29 @@ void editor_init(struct mSDLWindow *window)
} }
nuke_init(window); nuke_init(window);
window->nuke_gui = editor_render;
glfwSetKeyCallback(window->window, edit_input_cb); glfwSetKeyCallback(window->window, edit_input_cb);
glfwSetMouseButtonCallback(window->window, edit_mouse_cb); glfwSetMouseButtonCallback(window->window, edit_mouse_cb);
} }
// TODO: Implement // TODO: Implement
int editor_wantkeyboard() int editor_wantkeyboard() { return 0; }
{
return 0;
}
const int nuk_std = NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | const int nuk_std = NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE; NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE;
const struct nk_rect nk_rect_std = {250, 250, 250, 250}; const struct nk_rect nk_rect_std = {250, 250, 250, 250};
void editor_project_gui() void editor_project_gui() {
{
/* Grid, etc */ /* Grid, etc */
if (grid1_draw) if (grid1_draw)
draw_grid(grid1_width, grid1_span); draw_grid(grid1_width, grid1_span);
if (grid2_draw) if (grid2_draw)
draw_grid(grid2_width, grid2_span); draw_grid(grid2_width, grid2_span);
if (debugDrawPhysics) { if (debugDrawPhysics) {
/* // for (int i = 0; i < number_of_gameobjects(); i++)
for (int i = 0; i < number_of_gameobjects(); i++) // phys2d_dbgdrawcircle(objects[i]->circle);
phys2d_dbgdrawcircle(objects[i]->circle);
*/
} }
static char text[3][64]; static char text[3][64];
@ -459,50 +433,16 @@ void editor_project_gui()
int i; int i;
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, "Load sound")) {
ss = make_sound("alert.wav");
}
if (nk_button_label(ctx, "Play sound")) {
play_sound(&ss);
}
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);
}
}
nk_end(ctx);
if (nk_begin(ctx, "Menu Demo", nk_rect(600, 350, 275, 250), nuk_std)) { if (nk_begin(ctx, "Menu Demo", nk_rect(600, 350, 275, 250), nuk_std)) {
nk_menubar_begin(ctx); nk_menubar_begin(ctx);
nk_layout_row_dynamic(ctx, 30, 4); nk_layout_row_dynamic(ctx, 30, 2);
/*
char bbbuf[256]; char bbbuf[256];
snprintf(bbbuf, 256, "Current level: %s", current_level[0] == '\0' ? "Level not saved!" : current_level); snprintf(bbbuf, 256, "Current level: %s", current_level[0] == '\0' ? "Level not saved!" : current_level);
nk_label(ctx, bbbuf, NK_TEXT_LEFT); nk_label(ctx, bbbuf, NK_TEXT_LEFT);
*/
if (nk_menu_begin_label(ctx, "Windows", NK_TEXT_LEFT, nk_vec2(100, 200))) { if (nk_menu_begin_label(ctx, "Windows", NK_TEXT_LEFT, nk_vec2(100, 200))) {
nk_layout_row_dynamic(ctx, 30, 1); nk_layout_row_dynamic(ctx, 30, 1);
@ -538,18 +478,18 @@ if (nk_begin(ctx, "Menu Demo", nk_rect(600, 350, 275, 250), nuk_std)) {
get_levels(); get_levels();
} }
nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, levelname, MAXNAME-1, nk_filter_default); nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, levelname,
MAXNAME - 1, nk_filter_default);
vec_walk(levels, (void (*)(void *)) & editor_level_btn); vec_walk(levels, (void (*)(void *)) & editor_level_btn);
} }
nk_menubar_end(ctx); nk_menubar_end(ctx);
} }
nk_end(ctx); nk_end(ctx);
if (editor.showExport &&
if (editor.showExport && nk_begin(ctx, "Export and Bake", nk_rect_std, nuk_std)) { nk_begin(ctx, "Export and Bake", nk_rect_std, nuk_std)) {
if (nk_button_label(ctx, "Bake")) { if (nk_button_label(ctx, "Bake")) {
} }
@ -559,29 +499,29 @@ nk_end(ctx);
nk_end(ctx); nk_end(ctx);
} }
// Shadow map vars // Shadow map vars
if (nk_begin(ctx, "Lighting options", nk_rect_std, nuk_std)) { if (nk_begin(ctx, "Lighting options", nk_rect_std, nuk_std)) {
nk_layout_row_dynamic(ctx, 25, 1); nk_layout_row_dynamic(ctx, 25, 1);
nk_label(ctx, "Directional shadow map", NK_TEXT_LEFT); 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, "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, "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, "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_property_float(ctx, "Plane size", 0.f, &plane_size, 100.f, 1.f, 0.01f);
}
nk_end(ctx);
}nk_end(ctx);
if (editor.showGameSettings) { if (editor.showGameSettings) {
nk_begin(ctx, "Game settings", nk_rect_std, nuk_std); 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); // 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)) { 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); nk_property_float(ctx, "2d Gravity", -5000.f, &phys2d_gravity, 0.f, 1.f,
0.1f);
phys2d_apply(); phys2d_apply();
nk_tree_pop(ctx); nk_tree_pop(ctx);
} }
@ -592,7 +532,6 @@ nk_end(ctx);
nk_end(ctx); nk_end(ctx);
} }
if (editor.showStats) { if (editor.showStats) {
nk_begin(ctx, "Stats", nk_rect_std, nuk_std); 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, "FPS: %2.4f", 1.f / deltaT);
@ -600,7 +539,6 @@ nk_end(ctx);
nk_end(ctx); nk_end(ctx);
} }
if (editor.showREPL) { if (editor.showREPL) {
nk_begin(ctx, "REPL", nk_rect_std, nuk_std); nk_begin(ctx, "REPL", nk_rect_std, nuk_std);
@ -615,21 +553,30 @@ nk_end(ctx);
} }
nk_end(ctx); nk_end(ctx);
} }
if (editor.showViewmode) { if (editor.showViewmode) {
nk_begin(ctx, "View options", nk_rect_std, nuk_std); 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 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 Near Plane", 0.1f, &editorClose, 5.f, 0.1f,
nk_property_float(ctx, "Camera Far Plane", 50.f, &editorFar, 10000.f, 1.f, 1.f); 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)) { if (nk_tree_push(ctx, NK_TREE_NODE, "Shading mode", NK_MINIMIZED)) {
renderMode = nk_option_label(ctx, "Lit", renderMode == LIT) ? LIT : renderMode; renderMode =
renderMode = nk_option_label(ctx, "Unlit", renderMode == UNLIT) ? UNLIT : renderMode; nk_option_label(ctx, "Lit", renderMode == LIT) ? LIT : renderMode;
renderMode = nk_option_label(ctx, "Wireframe", renderMode == WIREFRAME) ? WIREFRAME : renderMode; renderMode = nk_option_label(ctx, "Unlit", renderMode == UNLIT)
renderMode = nk_option_label(ctx, "Directional shadow map", renderMode == DIRSHADOWMAP) ? DIRSHADOWMAP : renderMode; ? 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); nk_tree_pop(ctx);
} }
@ -650,7 +597,7 @@ nk_end(ctx);
} }
if (editor.showHierarchy) { if (editor.showHierarchy) {
nk_begin(ctx, "Objects", nk_rect_std, nuk_std); editor.showHierarchy = nk_begin(ctx, "Objects", nk_rect_std, nuk_std);
if (nk_button_label(ctx, "New Object")) { if (nk_button_label(ctx, "New Object")) {
MakeGameobject(); MakeGameobject();
@ -661,8 +608,7 @@ nk_end(ctx);
nk_end(ctx); nk_end(ctx);
} }
if (nk_begin(ctx, "Simulate", nk_rect_std, nuk_std)) {
nk_begin(ctx, "Simulate", nk_rect_std, nuk_std);
if (physOn) { if (physOn) {
if (nk_button_label(ctx, "Pause")) if (nk_button_label(ctx, "Pause"))
@ -676,20 +622,19 @@ nk_end(ctx);
} }
nk_end(ctx); nk_end(ctx);
}
if (nk_begin(ctx, "Prefab Creator", nk_rect_std, nuk_std)) {
nk_begin(ctx, "Prefab Creator", nk_rect_std, nuk_std);
vec_walk(prefabs, (void (*)(void *)) & editor_prefab_btn); vec_walk(prefabs, (void (*)(void *)) & editor_prefab_btn);
nk_end(ctx); nk_end(ctx);
}
if (editor.showAssetMenu) { if (editor.showAssetMenu) {
nk_begin(ctx, "Asset Menu", nk_rect_std, nuk_std); 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);
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) { if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) {
@ -738,15 +683,12 @@ nk_end(ctx);
if (selected_asset) if (selected_asset)
editor_asset_gui(selected_asset); editor_asset_gui(selected_asset);
if (editor.showDebugMenu) { if (editor.showDebugMenu) {
nk_begin(ctx, "Debug Menu", nk_rect_std, nuk_std); nk_begin(ctx, "Debug Menu", nk_rect_std, nuk_std);
if (nk_button_label(ctx, "Reload Shaders")) { if (nk_button_label(ctx, "Reload Shaders")) {
shader_compile_all(); shader_compile_all();
} }
//ImGui::SliderFloat("Grid scale", &gridScale, 100.f, 500.f, "%1.f");
nk_property_int(ctx, "Grid 1 Span", 1, &grid1_span, 500, 1, 1); nk_property_int(ctx, "Grid 1 Span", 1, &grid1_span, 500, 1, 1);
nk_checkbox_label(ctx, "Draw", &grid1_draw); nk_checkbox_label(ctx, "Draw", &grid1_draw);
@ -754,12 +696,14 @@ nk_end(ctx);
nk_property_int(ctx, "Grid 2 Span", 10, &grid2_span, 1000, 1, 1); nk_property_int(ctx, "Grid 2 Span", 10, &grid2_span, 1000, 1, 1);
nk_checkbox_label(ctx, "Draw", &grid2_draw); nk_checkbox_label(ctx, "Draw", &grid2_draw);
nk_property_float(ctx, "Grid Opacity", 0.f, &gridOpacity, 1.f, 0.01f,
nk_property_float(ctx, "Grid Opacity",0.f, &gridOpacity, 1.f, 0.01f, 0.01f); 0.01f);
nk_property_float(ctx, "Small unit", 0.5f, &smallGridUnit, 5.f, 0.1f, 0.1f); 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, "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, "Small thickness", 1.f, &gridSmallThickness, 10.f,
nk_property_float(ctx, "Big thickness", 1.f, &gridBigThickness, 10.f, 0.1f, 0.1f); 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 smgrd;
static struct nk_colorf lgrd; static struct nk_colorf lgrd;
@ -767,7 +711,6 @@ nk_end(ctx);
nk_color_pick(ctx, &smgrd, NK_RGBA); nk_color_pick(ctx, &smgrd, NK_RGBA);
nk_color_pick(ctx, &lgrd, NK_RGBA); nk_color_pick(ctx, &lgrd, NK_RGBA);
//ImGui::SliderInt("MSAA", &msaaSamples, 0, 4);
nk_end(ctx); nk_end(ctx);
} }
@ -777,13 +720,13 @@ nk_end(ctx);
draw_point(selectedobject->transform.position[0], draw_point(selectedobject->transform.position[0],
selectedobject->transform.position[1], 5); selectedobject->transform.position[1], 5);
nk_begin(ctx, "Object Parameters", nk_rect_std, nuk_std); nk_begin(ctx, "Object Parameters", nk_rect_std, nuk_std);
nk_layout_row_dynamic(ctx, 30, 3);
if (nk_button_label(ctx, "Save")) if (nk_button_label(ctx, "Save"))
gameobject_saveprefab(selectedobject); gameobject_saveprefab(selectedobject);
// ImGui::SameLine();
if (nk_button_label(ctx, "Del")) { if (nk_button_label(ctx, "Del")) {
gameobject_delete(selected_index); gameobject_delete(selected_index);
pickGameObject(-1); pickGameObject(-1);
@ -791,27 +734,25 @@ nk_end(ctx);
goto startobjectgui; goto startobjectgui;
} }
// ImGui::SameLine();
if (selectedobject->editor.prefabSync) { if (selectedobject->editor.prefabSync) {
if (nk_button_label(ctx, "Revert")) if (nk_button_label(ctx, "Revert"))
gameobject_revertprefab(selectedobject); gameobject_revertprefab(selectedobject);
} }
nk_label(ctx, "Name", NK_TEXT_LEFT); nk_label(ctx, "Name", NK_TEXT_LEFT);
nk_edit_string_zero_terminated(ctx, 0, selectedobject->editor.mname, 50, nk_filter_ascii); nk_edit_string_zero_terminated(ctx, 0, selectedobject->editor.mname, 50,
nk_filter_ascii);
nk_label(ctx, "Prefab", NK_TEXT_LEFT); nk_label(ctx, "Prefab", NK_TEXT_LEFT);
nk_edit_string_zero_terminated(ctx, 0, selectedobject->editor.prefabName, 50, nk_filter_ascii); nk_edit_string_zero_terminated(ctx, 0, selectedobject->editor.prefabName,
// Disabled if::::: selectedobject->editor.prefabSync ? ImGuiInputTextFlags_ReadOnly : 0); 50, nk_filter_ascii);
// Disabled if::::: selectedobject->editor.prefabSync ?
// ImGuiInputTextFlags_ReadOnly : 0);
object_gui(selectedobject); object_gui(selectedobject);
nk_end(ctx); nk_end(ctx);
nk_begin(ctx, "Components", nk_rect_std, nuk_std); nk_begin(ctx, "Components", nk_rect_std, nuk_std);
for (int i = 0; i < ncomponent; i++) { for (int i = 0; i < ncomponent; i++) {
@ -820,39 +761,25 @@ nk_end(ctx);
} }
} }
nk_end(ctx); nk_end(ctx);
}
} }
} void editor_render() { editor_project_gui(); }
void editor_render() void pickGameObject(int pickID) {
{
if (cur_project)
editor_project_gui();
else
editor_proj_select_gui();
}
void pickGameObject(int pickID)
{
if (pickID >= 0 && pickID < gameobjects->len) { if (pickID >= 0 && pickID < gameobjects->len) {
selected_index = pickID; selected_index = pickID;
selectedobject = selectedobject = (struct mGameObject *)vec_get(gameobjects, pickID);
(struct mGameObject *) vec_get(gameobjects, pickID);
} else { } else {
selected_index = -1; selected_index = -1;
selectedobject = NULL; selectedobject = NULL;
} }
} }
int is_allowed_extension(const char *ext) int is_allowed_extension(const char *ext) {
{
for (size_t i = 0; for (size_t i = 0;
i < sizeof(allowed_extensions) / sizeof(allowed_extensions[0]); i < sizeof(allowed_extensions) / sizeof(allowed_extensions[0]); i++) {
i++) {
if (!strcmp(ext + 1, allowed_extensions[i])) if (!strcmp(ext + 1, allowed_extensions[i]))
return true; return true;
} }
@ -860,18 +787,15 @@ int is_allowed_extension(const char *ext)
return false; return false;
} }
void editor_level_btn(char *level) void editor_level_btn(char *level) {
{
if (nk_button_label(ctx, level)) { if (nk_button_label(ctx, level)) {
load_level(level); load_level(level);
strcpy(current_level, level); strcpy(current_level, level);
} }
} }
void editor_selectasset(struct fileasset *asset) void editor_selectasset(struct fileasset *asset) {
{
const char *ext = get_extension(asset->filename); const char *ext = get_extension(asset->filename);
if (!strcmp(ext + 1, "png") || !strcmp(ext + 1, "jpg")) { if (!strcmp(ext + 1, "png") || !strcmp(ext + 1, "jpg")) {
@ -879,7 +803,8 @@ void editor_selectasset(struct fileasset *asset)
tex_gui_anim.tex = (struct Texture *)asset->data; tex_gui_anim.tex = (struct Texture *)asset->data;
asset->type = ASSET_TYPE_IMAGE; asset->type = ASSET_TYPE_IMAGE;
tex_anim_set(&tex_gui_anim); tex_anim_set(&tex_gui_anim);
//float tex_scale = float((float) ASSET_WIN_SIZE / tex_gui_anim.tex->width); // float tex_scale = float((float) ASSET_WIN_SIZE /
// tex_gui_anim.tex->width);
if (tex_scale >= 10.f) if (tex_scale >= 10.f)
tex_scale = 10.f; tex_scale = 10.f;
} else if (!strcmp(ext + 1, "glsl")) { } else if (!strcmp(ext + 1, "glsl")) {
@ -893,22 +818,18 @@ void editor_selectasset(struct fileasset *asset)
asset->data = malloc(ASSET_TEXT_BUF); asset->data = malloc(ASSET_TEXT_BUF);
fread(asset->data, 1, length, fasset); fread(asset->data, 1, length, fasset);
fclose(fasset); fclose(fasset);
} }
selected_asset = asset; selected_asset = asset;
} }
void editor_selectasset_str(char *path) void editor_selectasset_str(char *path) {
{
struct fileasset *asset = (struct fileasset *)shget(assets, path); struct fileasset *asset = (struct fileasset *)shget(assets, path);
if (asset) if (asset)
editor_selectasset(asset); editor_selectasset(asset);
} }
void editor_asset_tex_gui(struct Texture *tex) void editor_asset_tex_gui(struct Texture *tex) {
{
/* /*
ImGui::Text("%dx%d", tex->width, tex->height); ImGui::Text("%dx%d", tex->width, tex->height);
@ -988,11 +909,9 @@ void editor_asset_tex_gui(struct Texture *tex)
tex->height * tex_scale)); tex->height * tex_scale));
} }
*/ */
} }
void editor_asset_text_gui(char *text) void editor_asset_text_gui(char *text) {
{
/* /*
ImGui::InputTextMultiline("File edit", text, ASSET_TEXT_BUF, ImGui::InputTextMultiline("File edit", text, ASSET_TEXT_BUF,
ImVec2(600, 500), ImVec2(600, 500),
@ -1008,8 +927,7 @@ void editor_asset_text_gui(char *text)
*/ */
} }
void editor_asset_gui(struct fileasset *asset) void editor_asset_gui(struct fileasset *asset) {
{
nk_begin(ctx, "Asset Viewer", nk_rect_std, nuk_std); nk_begin(ctx, "Asset Viewer", nk_rect_std, nuk_std);
@ -1033,105 +951,11 @@ void editor_asset_gui(struct fileasset *asset)
} }
nk_end(ctx); nk_end(ctx);
} }
void editor_load_projects() void editor_makenewobject() {}
{
FILE *f = fopen("projects.yugh", "r");
if (!f)
return;
vec_load(projects, f); int obj_gui_hierarchy(struct mGameObject *selected) {
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);
}
ImGui::End();
*/
}
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 =
(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);
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 = (struct gameproject *) malloc(sizeof(*gp));
fread(gp, sizeof(*gp), 1, f);
fclose(f);
vec_add(projects, gp);
}
void editor_makenewobject()
{
}
int obj_gui_hierarchy(struct mGameObject *selected)
{
for (int i = 0; i < gameobjects->len; i++) { for (int i = 0; i < gameobjects->len; i++) {
struct mGameObject *go = (struct mGameObject *)vec_get(gameobjects, i); struct mGameObject *go = (struct mGameObject *)vec_get(gameobjects, i);
@ -1145,47 +969,25 @@ int obj_gui_hierarchy(struct mGameObject *selected)
return 0; return 0;
} }
void get_levels() void get_levels() { fill_extensions(levels, DATA_PATH, EXT_LEVEL); }
{
fill_extensions(levels, DATA_PATH, EXT_LEVEL);
}
void editor_prefab_btn(char *prefab) void editor_prefab_btn(char *prefab) {
{
if (nk_button_label(ctx, prefab)) { if (nk_button_label(ctx, prefab)) {
gameobject_makefromprefab(prefab); gameobject_makefromprefab(prefab);
/*GameObject* newprefab = (GameObject*)createPrefab(*prefab); */ /*GameObject* newprefab = (GameObject*)createPrefab(*prefab); */
/*cam_inverse_goto(&camera, &newprefab->transform); */ /*cam_inverse_goto(&camera, &newprefab->transform); */
} }
} }
void game_start() void game_start() { physOn = 1; }
{
physOn = 1;
}
void game_resume() void game_resume() { physOn = 1; }
{
physOn = 1;
}
void game_stop() void game_stop() { physOn = 0; }
{
physOn = 0;
}
void game_pause() void game_pause() { physOn = 0; }
{
physOn = 0;
}
void sprite_gui(struct mSprite *sprite) {
void sprite_gui(struct mSprite *sprite)
{
nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", sprite->tex->path); nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", sprite->tex->path);
// ImGui::SameLine(); // ImGui::SameLine();
@ -1195,11 +997,15 @@ void sprite_gui(struct mSprite *sprite)
if (sprite->tex != NULL) { if (sprite->tex != NULL) {
nk_labelf(ctx, NK_TEXT_LEFT, "%s", sprite->tex->path); nk_labelf(ctx, NK_TEXT_LEFT, "%s", sprite->tex->path);
nk_labelf(ctx, NK_TEXT_LEFT, "%dx%d", sprite->tex->width, sprite->tex->height); nk_labelf(ctx, NK_TEXT_LEFT, "%dx%d", sprite->tex->width,
if (nk_button_label(ctx, "Imgbutton")) editor_selectasset_str(sprite->tex->path); sprite->tex->height);
// if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50, 50))) { 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_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f,
0.01f);
nk_layout_row_dynamic(ctx, 25, 3); nk_layout_row_dynamic(ctx, 25, 3);
if (nk_button_label(ctx, "C")) { if (nk_button_label(ctx, "C")) {
@ -1207,16 +1013,13 @@ void sprite_gui(struct mSprite *sprite)
sprite->pos[1] = -0.5f; sprite->pos[1] = -0.5f;
} }
if (nk_button_label(ctx, "U")) { if (nk_button_label(ctx, "U")) {
sprite->pos[0] = -0.5f; sprite->pos[0] = -0.5f;
sprite->pos[1] = -1.f; sprite->pos[1] = -1.f;
} }
if (nk_button_label(ctx, "D")) { if (nk_button_label(ctx, "D")) {
sprite->pos[0] = -0.5f; sprite->pos[0] = -0.5f;
sprite->pos[1] = 0.f; sprite->pos[1] = 0.f;
} }
} }

View file

@ -31,10 +31,13 @@ struct editorVars {
bool showLevel; bool showLevel;
}; };
struct gameproject { struct vec;
char name[127]; struct gameproject;
char path[MAXPATH]; struct mSprite;
};
extern struct gameproject *cur_project;
extern struct vec *projects;
struct Texture; struct Texture;
struct mSDLWindow; struct mSDLWindow;
@ -51,13 +54,6 @@ void editor_makenewobject();
void editor_project_gui(); 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(struct fileasset *asset);
void editor_selectasset_str(char *path); void editor_selectasset_str(char *path);
void editor_asset_gui(struct fileasset *asset); void editor_asset_gui(struct fileasset *asset);

View file

@ -40,7 +40,7 @@ void font_init(struct mShader *textshader) {
// Default font // Default font
font = MakeFont("notosans.ttf", 300); font = MakeFont("teenytinypixels.ttf", 300);
} }
void font_frame(struct mSDLWindow *w) { void font_frame(struct mSDLWindow *w) {

View file

@ -39,7 +39,6 @@ void nuke_start()
void nuke_end() void nuke_end()
{ {
nk_end(ctx);
nk_glfw3_render(&nkglfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER); nk_glfw3_render(&nkglfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
} }

View file

@ -93,31 +93,6 @@ struct mSprite *tsprite = NULL;
static unsigned int projUBO; 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() void openglInit()
{ {
if (!mainwin) { if (!mainwin) {
@ -126,11 +101,9 @@ void openglInit()
} }
////// MAKE SHADERS ////// MAKE SHADERS
outlineShader = MakeShader("outlinevert.glsl", "outline.glsl");
spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl"); spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl");
animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl"); animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl");
textShader = MakeShader("textvert.glsl", "textfrag.glsl"); textShader = MakeShader("textvert.glsl", "textfrag.glsl");
//textShader = CreateShader(textverg, textfrag);
shader_use(textShader); shader_use(textShader);
shader_setint(textShader, "text", 0); shader_setint(textShader, "text", 0);
@ -139,7 +112,7 @@ void openglInit()
font_init(textShader); font_init(textShader);
sprite_initialize(); sprite_initialize();
//debugdraw_init(); debugdraw_init();
//glEnable(GL_STENCIL_TEST); //glEnable(GL_STENCIL_TEST);
@ -168,8 +141,6 @@ void openglInit()
shader_setUBO(spriteShader, "Projection", 0); shader_setUBO(spriteShader, "Projection", 0);
shader_setUBO(textShader, "Projection", 0); shader_setUBO(textShader, "Projection", 0);
shader_setUBO(animSpriteShader, "Projection", 0); shader_setUBO(animSpriteShader, "Projection", 0);
} }

View file

@ -272,7 +272,13 @@ void window_render(struct mSDLWindow *w) {
nuke_start(); nuke_start();
script_call_sym(w->nuke_cb); script_call_sym(w->nuke_cb);
nuke_end(); nuke_end();
} else if (w->nuke_gui != NULL) {
nuke_start();
w->nuke_gui();
nuke_end();
} }
window_swap(w); window_swap(w);
} }

View file

@ -23,6 +23,7 @@ struct mSDLWindow {
float projection[16]; float projection[16];
mrb_sym nuke_cb; mrb_sym nuke_cb;
mrb_sym gui_cb; mrb_sym gui_cb;
void (*nuke_gui)();
}; };
struct Texture; struct Texture;

View file

@ -5,6 +5,8 @@
#include "input.h" #include "input.h"
#include "openglrender.h" #include "openglrender.h"
#include "string.h"
int physOn = 0; int physOn = 0;
double renderlag = 0; double renderlag = 0;
@ -15,13 +17,28 @@ double renderMS = 1/60.f;
double physMS = 1/120.f; double physMS = 1/120.f;
double updateMS = 1/60.f; double updateMS = 1/60.f;
static int ed = 1;
int main(int argc, char **args) { 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(); engine_init();
window_set_icon("icon.png"); window_set_icon("icon.png");
if (ed) {
editor_init(MakeSDLWindow("Editor", 600, 600, 0));
} else {
script_dofile("game.rb"); script_dofile("game.rb");
}
openglInit(); openglInit();
@ -45,7 +62,7 @@ int main(int argc, char **args) {
input_poll(updateMS - elapsed < 0 ? 0 : updateMS - elapsed); input_poll(updateMS - elapsed < 0 ? 0 : updateMS - elapsed);
window_all_handle_events(); window_all_handle_events();
script_update(updateMS);
} }
} }