anders_tale_room_editor

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | LICENSE

commit c50643665660d56938828c11d630514a4517b2df
parent 326bdaab263573a11cdbc64d83f13db932e09378
Author: Samdal <samdal@protonmail.com>
Date:   Thu,  2 Sep 2021 21:44:30 +0200

fixed some more stuff and cleaned up, added aroom

Diffstat:
Msrc/main.c | 214++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
1 file changed, 119 insertions(+), 95 deletions(-)

diff --git a/src/main.c b/src/main.c @@ -3,28 +3,25 @@ #define HS_SFD #include "hs/hs_graphics.h" -hs_game_data gd = { - .width = 960, - .height = 540 -}; - #define MAX_VERTEX_BUFFER 512 * 1024 #define MAX_ELEMENT_BUFFER 128 * 1024 -hs_key* escape = hs_key_init(GLFW_KEY_ESCAPE); - -uint32_t popup_menu_up = 1; -uint32_t popup_new_up = 0; - struct nk_context *ctx; struct nk_glfw glfw = {0}; -uint32_t tileset_tex; hs_tilemap tilemap; uint32_t selected_tile = 0; +hs_aroom current_room; + +uint32_t popup_new_room_on = 0; + +hs_game_data gd = { + .width = 960, + .height = 540 +}; sfd_Options anders_tale_room = { - .title = "Johan, you may now choose a Anders Tale Room file", + .title = "Johan, you may now choose an Anders Tale Room file", .filter_name = "Anders tale room", .filter = "*.aroom", }; @@ -36,22 +33,24 @@ framebuffer_size_callback(GLFWwindow* win, const int width, const int height) { gd.width = width; gd.height = height; - const float viewport_height = (float)gd.width * ((float)tilemap.height/tilemap.width); - if (viewport_height > gd.height) { - const float viewport_width = (float)gd.height * ((float)tilemap.width/tilemap.height); - tilemap_offset_x = ((gd.width - viewport_width) / 2); - tilemap_offset_y = 0; - tilemap_screen_width = viewport_width; - tilemap_screen_height = gd.height; - } else { - tilemap_offset_x = 0; - tilemap_offset_y = ((gd.height - viewport_height) / 2); - tilemap_screen_width = gd.width; - tilemap_screen_height = viewport_height; - } + if (tilemap.vertices) { + const float viewport_height = (float)gd.width * ((float)tilemap.height/tilemap.width); + if (viewport_height > gd.height) { + const float viewport_width = (float)gd.height * ((float)tilemap.width/tilemap.height); + tilemap_offset_x = (gd.width - viewport_width) / 2; + tilemap_offset_y = 0; + tilemap_screen_width = viewport_width; + tilemap_screen_height = gd.height; + } else { + tilemap_offset_x = 0; + tilemap_offset_y = (gd.height - viewport_height) / 2; + tilemap_screen_width = gd.width; + tilemap_screen_height = viewport_height; + } - tilemap_offset_x += 200; - tilemap_screen_width -= 200; + tilemap_offset_x += 200; + tilemap_screen_width -= 200; + } } static inline void init() @@ -68,39 +67,31 @@ static inline void init() } } -static inline void popup_start() +static inline void +init_new_room(const uint32_t room_width, const uint32_t room_height) { - - if (nk_begin(ctx, "Main Menu", nk_rect(gd.width/2 - 100, gd.height/2 - 70, 200, 140), - NK_WINDOW_BORDER | NK_WINDOW_TITLE | NK_WINDOW_NO_SCROLLBAR)) { - - nk_layout_row_dynamic(ctx, 30, 1); - if (nk_button_label(ctx, "New room")) { - popup_new_up = true; - popup_menu_up = false; - } - - nk_layout_row_dynamic(ctx, 30, 1); - if (nk_button_label(ctx, "load room")) { - const char *filename = sfd_open_dialog(&anders_tale_room); - if (filename) { - printf("Got file: '%s'\n", filename); - } else { - printf("load canceled\n"); - } - } - - if (nk_button_label(ctx, "Quit without saving")) { - exit(0); - } - } - nk_end(ctx); + hs_tilemap_init(&tilemap, tilemap.sp.tex.tex_unit, 0); + framebuffer_size_callback(gd.window, gd.width, gd.height); + + if (current_room.data) + free(current_room.data); + uint16_t* room_data = malloc(sizeof(uint16_t) * room_width * room_height); + assert(room_data); + + current_room = (hs_aroom){ + .width = room_width, + .height = room_height, + .data = room_data, + }; } -static inline void popup_new_room() +static inline uint32_t +popup_create_new_room() { - if (nk_begin(ctx, "Create new room", nk_rect(gd.width/2 - 100, gd.height/2 - 70, 200, 140), - NK_WINDOW_BORDER | NK_WINDOW_TITLE | NK_WINDOW_NO_SCROLLBAR)) { + uint32_t new_tilemap = false; + if (nk_popup_begin(ctx, NK_POPUP_STATIC, "Create new room", + NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_TITLE, + nk_rect(200, 10, 320, 140))) { static int room_width = 20; static int room_height = 20; @@ -115,28 +106,28 @@ static inline void popup_new_room() tilemap.tile_width = 1.0f/room_width; tilemap.tile_height = 1.0f/room_height; - if (tileset_tex) { - hs_tilemap_init(&tilemap, tileset_tex, 0); - } + init_new_room(room_width, room_height); - popup_new_up = false; - popup_menu_up = false; + popup_new_room_on = false; + new_tilemap = true; + nk_popup_close(ctx); } - if (nk_button_label(ctx, "Back")) { - popup_new_up = false; - popup_menu_up = true; + if (nk_button_label(ctx, "Quit")) { + popup_new_room_on = false; + nk_popup_close(ctx); } + nk_popup_end(ctx); } - nk_end(ctx); + return new_tilemap; } static struct nk_image* create_tiles(const char* filename, const uint32_t width, const uint32_t height) { int tileset_width, tileset_height; - tileset_tex = hs_tex2d_create_size_info_pixel(filename, GL_RGBA, &tileset_width, &tileset_height); - struct nk_image tileset = nk_image_id(tileset_tex); + tilemap.sp.tex.tex_unit = hs_tex2d_create_size_info_pixel(filename, GL_RGBA, &tileset_width, &tileset_height); + struct nk_image tileset = nk_image_id(tilemap.sp.tex.tex_unit); struct nk_image* tiles = (struct nk_image*)malloc(sizeof(struct nk_image) * width * height); assert(tiles); @@ -156,19 +147,48 @@ create_tiles(const char* filename, const uint32_t width, const uint32_t height) return tiles; } -static inline void side_bar() +static inline void +side_bar() { - static uint32_t tileset_chosen = 0; const char* const default_name = "No tileset selected"; static char* filename = NULL; static char* filename_short = (char*)default_name; + const char* const not_selected = "No tilemap selected"; + const char* const edited = "Tilemap edited"; + const char* const saved = "All changes saved"; + static char* room_editing_state = (char*)not_selected; + static struct nk_image* tileset_images = NULL; static uint32_t tileset_image_count = 0; - static uint32_t tileset_popup = 0; + static uint32_t tileset_popup_on = 0; if (nk_begin(ctx, "Side bar", nk_rect(0, 0, 200, gd.height), 0)) { nk_layout_row_dynamic(ctx, 10, 1); + nk_label(ctx, room_editing_state, NK_TEXT_CENTERED); + + nk_layout_row_dynamic(ctx, 30, 1); + if (nk_button_label(ctx, "New room")) { + popup_new_room_on = true; + } + + nk_layout_row_dynamic(ctx, 30, 1); + if (nk_button_label(ctx, "load room")) { + const char *filename = sfd_open_dialog(&anders_tale_room); + if (filename) { + printf("Got file: '%s'\n", filename); + } else { + printf("load canceled\n"); + } + } + if (popup_new_room_on) { + if (popup_create_new_room()) { + room_editing_state = (char*)edited; + } + } + + nk_layout_row_dynamic(ctx, 10, 1); + nk_layout_row_dynamic(ctx, 10, 1); nk_label(ctx, filename_short, NK_TEXT_CENTERED); nk_layout_row_dynamic(ctx, 30, 1); @@ -178,8 +198,9 @@ static inline void side_bar() .filter_name = "PNG image", .filter = "*.png", }); + if (filename && filename[0] != '\0') { - tileset_popup = true; + tileset_popup_on = true; char* last_slash_pos = filename; for (char* f = filename; f[1] != '\0'; f++) @@ -191,7 +212,9 @@ static inline void side_bar() } } - if (tileset_popup && + static uint32_t tileset_created = false; + + if (tileset_popup_on && nk_popup_begin(ctx, NK_POPUP_STATIC, "Select tileset size", NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_TITLE, nk_rect(200, 30, 320, 140))) { @@ -206,8 +229,8 @@ static inline void side_bar() if (nk_button_label(ctx, "Confirm")) { if (tileset_images) { free(tileset_images); - glDeleteTextures(1, &tileset_tex); - tileset_tex = 0; + glDeleteTextures(1, &tilemap.sp.tex.tex_unit); + tilemap.sp.tex.tex_unit = 0; } tileset_images = create_tiles(filename, tileset_width, tileset_height); @@ -215,23 +238,29 @@ static inline void side_bar() tilemap.tileset_width = tileset_width; tilemap.tileset_height = tileset_height; - tilemap.sp.tex.tex_unit = tileset_tex; + tilemap.sp.tex.tex_unit = tilemap.sp.tex.tex_unit; + + if (tilemap.width) { + //TODO change texture and offsets instead of recreating tilemap + hs_tilemap_init(&tilemap, tilemap.sp.tex.tex_unit, 0); + framebuffer_size_callback(gd.window, gd.width, gd.height); + } - tileset_chosen = true; - tileset_popup = false; + tileset_popup_on = false; + tileset_created = true; nk_popup_close(ctx); } if (nk_button_label(ctx, "Quit")) { filename = NULL; filename_short = (char*)default_name; - tileset_popup = false; + tileset_popup_on = false; nk_popup_close(ctx); } nk_popup_end(ctx); } - if (tileset_chosen) { + if (tileset_created) { nk_layout_row_dynamic(ctx, 20, 1); nk_label(ctx, "Selected tile:", NK_TEXT_CENTERED); @@ -250,16 +279,6 @@ static inline void side_bar() static inline void loop() { - if (!popup_new_up) - if (hs_get_key_toggle(gd, escape) == HS_KEY_PRESSED) popup_menu_up = !popup_menu_up; - - side_bar(); - if (popup_menu_up) { - popup_start(); - } else if (popup_new_up) { - popup_new_room(); - } - glViewport(0, 0, gd.height, gd.width); hs_clear(0.2, 0.2, 0.2, 1.0, 0); glEnable(GL_BLEND); @@ -269,23 +288,28 @@ static inline void loop() hs_tex2d_activate(tilemap.sp.tex.tex_unit, GL_TEXTURE0); hs_tilemap_draw(tilemap); - if (glfwGetMouseButton(gd.window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS) { + if (tilemap.sp.tex.tex_unit && glfwGetMouseButton(gd.window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS) { double xpos, ypos; glfwGetCursorPos(gd.window, &xpos, &ypos); + // reverse y axis ypos -= gd.height; ypos *= -1; - - if(xpos > tilemap_offset_x && xpos < tilemap_offset_x + tilemap_screen_width && - ypos > tilemap_offset_y && ypos < tilemap_offset_y + tilemap_screen_height) { - int32_t tilex = (xpos - tilemap_offset_x) / (tilemap.tile_width * tilemap_screen_width); - int32_t tiley = (ypos - tilemap_offset_y) / (tilemap.tile_height * tilemap_screen_height); + int32_t tilex = (xpos - tilemap_offset_x) / (tilemap.tile_width * tilemap_screen_width); + int32_t tiley = (ypos - tilemap_offset_y) / (tilemap.tile_height * tilemap_screen_height); + if (hs_aroom_get_xy(current_room, tilex, tiley) != selected_tile && + xpos > tilemap_offset_x && xpos < tilemap_offset_x + tilemap_screen_width && + ypos > tilemap_offset_y && ypos < tilemap_offset_y + tilemap_screen_height) { + puts("edited"); hs_tilemap_set_xy(&tilemap, tilex, tiley, selected_tile); + hs_aroom_set_xy(&current_room, tilex, tiley, selected_tile); hs_tilemap_update_vbo(tilemap); } } } + side_bar(); + /* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state * with blending, scissor, face culling, depth test and viewport and * defaults everything back into a default state.