From 029e15cfbf1f6d515d7401b0903cce9f0472176b Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 21 Feb 2022 20:54:56 -0600 Subject: [PATCH] added logging system; added texture to items --- run_tree/assets/images/beer-1.png | Bin 0 -> 990 bytes run_tree/scenes/default.items | 1 + run_tree/scenes/quick_save.scene | 116 +++++++++--------------------- src/build.onyx | 1 + src/entity/items.onyx | 29 +++++--- src/entity/manager.onyx | 10 +-- src/entity/player.onyx | 2 - src/entity/store.onyx | 22 +++--- src/game.onyx | 15 ++-- src/gfx/texture.onyx | 12 ++-- src/main.onyx | 4 +- src/utils/asset_loader.onyx | 8 +-- src/utils/logger.onyx | 35 +++++++++ 13 files changed, 130 insertions(+), 125 deletions(-) create mode 100644 run_tree/assets/images/beer-1.png create mode 100644 src/utils/logger.onyx diff --git a/run_tree/assets/images/beer-1.png b/run_tree/assets/images/beer-1.png new file mode 100644 index 0000000000000000000000000000000000000000..72a5ce13b90624f7e5df1d678fe711ad2e660421 GIT binary patch literal 990 zcmV<410np0P)EX>4Tx04R}tkv&MmP!xqvQ>7vm2aAX}M5s;{L`5963Pq?8YK2xEOkVm2O&XFE z7e~Rh;NZ_<)xpJCR|i)?5c~mgbaGO3krMAq3N2!MaCsl+y>qzlK0v6KnPzp21DbA| zsYG1NWLL$|D+KgY4I>~k%b1g-Bz)J`Jpz2ci}5V~dw;GTHD@s(AQI0q!?cMvh^IGg zgY!OdgcW6#_?&pmqy~u}xvqHp#<}3Kz%wIeIyFxmAr=d5th6yJni}yGaa7fG$`>*o ztDLtuYvn3y_Q_uu&gm=5T&EgB0*hFJ1Q80VD4`4+G1_%fETrf>?&BYH{Svtpa+Scy zv49FR$gUs!4}Q(v&^mat9cAGGtSBr684%&jasg^i5fy?-uA@^XAq($LRx*rdcI#fP+I| zv_RSGKJV^opWDB8n)CYs1y^!=^%%TX00006VoOIv00000008+zyMF)x010qNS#tmY zE+YT{E+YYWr9XB6000McNliru<^mQ1CK;zn_Lcwu02y>eSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{00Fj1L_t(I%cYagY7~u2>Npf-mRI66wTRAz+&So+y zWnCperz$l!<^m)@W_GFtN6Xlzv%IgIY{=7>R zRUACa$n$)q2j0?1eDUDPIir!Kjt%>_1MWV}=|>?^-!bU<07OxA0q`zsTNnUt+;rSO z%=k5K1JI}9;4tOHE1G1_^IDslnMF-F&710GX~Daf1CC7yKrsog9wR{O*fzQfIeH#L z7V`MTgrs-Q=Pw~~sOte=2yWaHx@|x+75`R%jXdB%L zI^4OJ6SvAWz{)6--Gqm)ER*S0 Item_Store { @@ -29,7 +31,7 @@ item_store_load_items_from_file :: (use this: ^Item_Store, path: str) { err, input_file := os.open(path, .Read); if err != .None { - printf("Failed to open file: {}\n", path); + debug_log(.Error, "Failed to open file: {}", path); return; } defer os.close(^input_file); @@ -51,8 +53,7 @@ item_store_load_items_from_file :: (use this: ^Item_Store, path: str) { |> string.strip_whitespace(); if items->has(item_id) { - printf("Duplicate definition for item with id '{}', on line: {}.\n", item_id); - return; + debug_log(.Warning, "Duplicate definition for item with id '{}', on line: {}.", item_id); } item_id = string.alloc_copy(item_id, allocator=item_allocator); @@ -71,44 +72,50 @@ item_store_load_items_from_file :: (use this: ^Item_Store, path: str) { member := get_any_for_member(ptr_to_any(current_item), var_name); if member.data == null { - printf("'{}' is not a valid member of Item on line {}.\n", var_name, line_number); + debug_log(.Warning, "'{}' is not a valid member of Item on line {}.", var_name, line_number); continue; } if !conv.parse_any(member.data, member.type, line) { - printf("Unable to parse '{}' for type '{}' on line {}.\n", line, member.type, line_number); + debug_log(.Warning, "Unable to parse '{}' for type '{}' on line {}.", line, member.type, line_number); continue; } } } +item_store_get_item :: (use this: ^Item_Store, id: str) -> ^Item { + return items[id]; +} Item_Entity :: struct { use entity: Entity; [Editor_Custom_Field.{render_item_picker}] item: str; - color: Color; init_data :: struct { pos := Vector2.{0, 0}; - color := Color.{1, 0, 1, 1}; } init :: (use this: ^Item_Entity, data: init_data) { this.pos = data.pos; this.size = .{16, 16}; - this.color = data.color; this.flags |= .Carryable; } get_rect :: Entity.get_rect draw :: (use this: ^Item_Entity) { - immediate_set_color(this.color); + item_data := item_store->get_item(item); + immediate_set_color(item_data.color); r := this->get_rect(); - immediate_rectangle(r.x, r.y, r.w, r.h); + texture, loaded := texture_lookup(item_data.texture_path); + if !loaded { + immediate_rectangle(r.x, r.y, r.w, r.h); + } else { + immediate_image(^texture, r.x, r.y, r.w, r.h); + } } } diff --git a/src/entity/manager.onyx b/src/entity/manager.onyx index 6a157d0..e11bad3 100644 --- a/src/entity/manager.onyx +++ b/src/entity/manager.onyx @@ -118,11 +118,11 @@ entity_manager_register :: (use this: ^Entity_Manager, $entity_type: type_expr) @CompilerFeatures // Maybe there should be data stored in the Type_Info_Struct about // the functions/methods that are defined in the structs scope. I don't know if that // would be worthwhile or if it would just be bulking up the type info data. - #if #defined(entity_type.destroy) { info.destroy = entity_type.destroy; if DEBUG do printf("{} has '{}'.\n", entity_type, "destroy"); } - #if #defined(entity_type.update) { info.update = entity_type.update; if DEBUG do printf("{} has '{}'.\n", entity_type, "update"); } - #if #defined(entity_type.draw) { info.draw = entity_type.draw; if DEBUG do printf("{} has '{}'.\n", entity_type, "draw"); } - #if #defined(entity_type.get_rect) { info.get_rect = entity_type.get_rect; if DEBUG do printf("{} has '{}'.\n", entity_type, "get_rect"); } - #if #defined(entity_type.interact) { info.interact = entity_type.interact; if DEBUG do printf("{} has '{}'.\n", entity_type, "interact"); } + #if #defined(entity_type.destroy) { info.destroy = entity_type.destroy; if DEBUG do debug_log(.Debug, "{} has '{}'.", entity_type, "destroy"); } + #if #defined(entity_type.update) { info.update = entity_type.update; if DEBUG do debug_log(.Debug, "{} has '{}'.", entity_type, "update"); } + #if #defined(entity_type.draw) { info.draw = entity_type.draw; if DEBUG do debug_log(.Debug, "{} has '{}'.", entity_type, "draw"); } + #if #defined(entity_type.get_rect) { info.get_rect = entity_type.get_rect; if DEBUG do debug_log(.Debug, "{} has '{}'.", entity_type, "get_rect"); } + #if #defined(entity_type.interact) { info.interact = entity_type.interact; if DEBUG do debug_log(.Debug, "{} has '{}'.", entity_type, "interact"); } entity_types[entity_type] = info; } diff --git a/src/entity/player.onyx b/src/entity/player.onyx index 850ef5b..560669f 100644 --- a/src/entity/player.onyx +++ b/src/entity/player.onyx @@ -244,8 +244,6 @@ Door :: struct { } interact :: (use this: ^Door, interactor: ^Entity) { - printf("The door at {} was interacted with by a {}!\n", pos, interactor.type); - // Doors only open if interacted with by a player if_entity_is(interactor, Player) { target_openness = 0.8f - target_openness; diff --git a/src/entity/store.onyx b/src/entity/store.onyx index 810604d..85c0ee7 100644 --- a/src/entity/store.onyx +++ b/src/entity/store.onyx @@ -56,10 +56,16 @@ entity_manager_save_to_file :: (use this: ^Entity_Manager, filename: str) { io.write(dest, output); } - case #default { - msg := conv.format(output_buffer, "Unhandled output case: {}\n", it.type); - // assert(false, msg); - printf("ERROR: {}\n", msg); + case #default do switch it.type { + case str { + output := conv.format_va(output_buffer, "{} = {\"}\n", .[.{^name, str}, member_any]); + io.write(dest, output); + } + + case #default { + msg := conv.format(output_buffer, "Unhandled output case: {}\n", it.type); + debug_log(.Error, msg); + } } } } @@ -69,7 +75,7 @@ entity_manager_save_to_file :: (use this: ^Entity_Manager, filename: str) { entity_manager_load_from_file :: (use this: ^Entity_Manager, filename: str) { err, input_file := os.open(filename, .Read); if err != .None { - printf("Failed to open file: {}\n", filename); + debug_log(.Error, "Failed to open file: {}", filename); return; } defer os.close(^input_file); @@ -106,7 +112,7 @@ entity_manager_load_from_file :: (use this: ^Entity_Manager, filename: str) { } if entity_info == null { - printf("Unknown entity kind '{}' on line {}.\n", entity_kind, line_number); + debug_log(.Error, "Unknown entity kind '{}' on line {}.", entity_kind, line_number); current_entity = null; continue; } @@ -125,12 +131,12 @@ entity_manager_load_from_file :: (use this: ^Entity_Manager, filename: str) { member := get_any_for_member(any.{current_entity, current_entity.type}, var_name); if member.data == null { entity_info := cast(^Type_Info_Struct) get_type_info(current_entity.type); - printf("'{}' is not a valid member of '{}' on line {}.\n", var_name, entity_info.name, line_number); + debug_log(.Warning, "'{}' is not a valid member of '{}' on line {}.", var_name, entity_info.name, line_number); continue; } if !conv.parse_any(member.data, member.type, line) { - printf("Unable to parse '{}' for type '{}' on line {}.\n", line, member.type, line_number); + debug_log(.Warning, "Unable to parse '{}' for type '{}' on line {}.", line, member.type, line_number); continue; } } diff --git a/src/game.onyx b/src/game.onyx index 36e28a5..97613eb 100644 --- a/src/game.onyx +++ b/src/game.onyx @@ -10,13 +10,12 @@ scene: Entity_Manager; item_store: Item_Store; game_init :: () { - // player_texture = texture_make(#cstr "./assets/images/player.png"); + // player_texture = texture_lookup(#cstr "./assets/images/player.png"); // This process of queueing the asset bucket should // be made automatic somehow... queue_assets(^player_assets); - load_assets(); scene = entity_manager_create(); @@ -30,21 +29,25 @@ game_init :: () { item_store->load_items_from_file("scenes/default.items"); #if DEBUG { - println("Registered Entity types:"); + debug_log(.Debug, "Registered Entity types:"); for scene.entity_types.entries { info := cast(^type_info.Type_Info_Struct) type_info.get_type_info(it.key); - printf(" {}\n", info.name); + debug_log(.Debug, " {}", info.name); } } } +#local quick_save_file := "scenes/quick_save.scene"; + game_update :: (dt: f32) { if is_key_just_up(GLFW_KEY_F8) { - scene->save_to_file("scenes/quick_save.scene"); + debug_log(.Info, "Saving to '{}'.", quick_save_file); + scene->save_to_file(quick_save_file); } if is_key_just_up(GLFW_KEY_F9) { - scene->load_from_file("scenes/quick_save.scene"); + debug_log(.Info, "Loading from '{}'.", quick_save_file); + scene->load_from_file(quick_save_file); } if editor_shown() { diff --git a/src/gfx/texture.onyx b/src/gfx/texture.onyx index b5c1427..8586b40 100644 --- a/src/gfx/texture.onyx +++ b/src/gfx/texture.onyx @@ -12,14 +12,18 @@ Texture :: struct { filename: str; } -texture_make :: #match {} -#match texture_make (filename: str) -> (Texture, bool) { +texture_lookup :: #match {} +#match texture_lookup (filename: str) -> (Texture, bool) { + if texture_cache->has(filename) { + return texture_cache[filename], true; + } + buffer: [512] u8; memory.copy(~~ buffer, filename.data, math.min(filename.count, 511)); - return texture_make(cast(cstr) buffer); + return texture_lookup(cast(cstr) buffer); } -#match texture_make (path: cstr) -> (Texture, bool) { +#match texture_lookup (path: cstr) -> (Texture, bool) { filename := string.from_cstr(path); if texture_cache->has(filename) { return texture_cache[filename], true; diff --git a/src/main.onyx b/src/main.onyx index 0b5088f..12cdd27 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -56,7 +56,7 @@ update :: (dt: f32) { } draw :: () { - immediate_clear(.{0.1, 0.1, 0.1}); + immediate_clear(.{0.15, 0.15, 0.2}); defer ui_end_frame(); defer input_post_update(); defer { @@ -144,7 +144,7 @@ create_window :: () => { main :: (args) => { if !glfwInit() { - println("Failed to initialize GLFW"); + debug_log(.Critical, "Failed to initialize GLFW!"); os.exit(1); } diff --git a/src/utils/asset_loader.onyx b/src/utils/asset_loader.onyx index 15a0b17..c4ecea6 100644 --- a/src/utils/asset_loader.onyx +++ b/src/utils/asset_loader.onyx @@ -43,18 +43,18 @@ load_assets :: () { if path_tag != null { path := *cast(^str) path_tag.data; - if texture, success := texture_make(path); success { + if texture, success := texture_lookup(path); success { *out_texture = texture; } else { - printf("[ERROR] Failed to load texture '{}' for asset queued here: {}:{},{}.\n", path, location.file, location.line, location.column); + debug_log(.Error, "Failed to load texture '{}' for asset queued here: {}:{},{}.\n", path, location.file, location.line, location.column); } } else { - printf("[ERROR] Texture path not found for texture asset load here: {}:{},{}.\n", location.file, location.line, location.column); + debug_log(.Error, "Texture path not found for texture asset load here: {}:{},{}.\n", location.file, location.line, location.column); } } case #default { - printf("[WARN] Asset loader does not know how to load a '{}'.\n", type); + debug_log(.Warning, "Asset loader does not know how to load a '{}'.\n", type); } } } diff --git a/src/utils/logger.onyx b/src/utils/logger.onyx new file mode 100644 index 0000000..529ddee --- /dev/null +++ b/src/utils/logger.onyx @@ -0,0 +1,35 @@ +// +// This may become an in-game or external file logger in the future, +// but for now this is just for logging to the command line. + +use package core + +Log_Level :: enum { + Debug; + Info; + Warning; + Error; + Critical; +} + +debug_log :: (level: Log_Level, format: str, args: ..any) { + debug_log_va(level, format, ~~args); +} + +debug_log_va :: (level: Log_Level, format: str, args: [] any) { + buf: [2048] u8; + output := conv.format_va(buf, format, args); + printf("[{}] {}\n", level_string(level), output); +} + +#local level_string :: (level: Log_Level) => { + switch level { + case .Debug do return "DEBUG"; + case .Info do return "INFO "; + case .Warning do return "WARN "; + case .Error do return "ERROR"; + case .Critical do return "CRIT "; + } + + return " "; +} -- 2.25.1