diff --git a/src/command.cpp b/src/command.cpp index 4962a44..0be947c 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -109,6 +109,10 @@ void command_fini(void) TRACE_NO_INDENT(); if (command_inited) { command_inited = false; + for (auto iter : commands_map) { + auto command = iter.second; + delete command; + } } } diff --git a/src/game.cpp b/src/game.cpp index d15af72..edc2d69 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -422,7 +422,7 @@ void Game::state_change(uint8_t new_state, const std::string &why) // // Why oh why change state // - CON("INF: Game state change: %s -> %s, reason %s", gama_state_to_string(old_state).c_str(), + LOG("INF: Game state change: %s -> %s, reason %s", gama_state_to_string(old_state).c_str(), gama_state_to_string(new_state).c_str(), why.c_str()); TRACE_AND_INDENT(); diff --git a/src/game_load.hpp b/src/game_load.hpp index a1db169..e6704da 100644 --- a/src/game_load.hpp +++ b/src/game_load.hpp @@ -681,7 +681,7 @@ void game_load_last_config(const char *appdata) { TRACE_NO_INDENT(); - CON("INI: Load config"); + LOG("INI: Load config"); game = new Game(std::string(appdata)); auto config_error = game->load_config(); diff --git a/src/level.cpp b/src/level.cpp index ea56688..9879f20 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -309,9 +309,9 @@ void level_map_set(Levelp l, int z, const char *in) if (need_floor) { auto tp_add = tp_floor; - auto t = level_thing_init(l, tp_add, x, y, z); + auto t = thing_init(l, tp_add, x, y, z); if (t) { - level_thing_push(l, t); + thing_push(l, t); } } @@ -319,9 +319,9 @@ void level_map_set(Levelp l, int z, const char *in) continue; } - auto t = level_thing_init(l, tp, x, y, z); + auto t = thing_init(l, tp, x, y, z); if (t) { - level_thing_push(l, t); + thing_push(l, t); } } } @@ -341,7 +341,7 @@ bool level_is_same_type(Levelp l, int x, int y, int z, Tpp tp) for (auto slot = 0; slot < MAP_SLOTS; slot++) { Tpp it_tp; - Thingp it = level_thing_and_tp_get(l, x, y, z, slot, &it_tp); + Thingp it = thing_and_tp_get(l, x, y, z, slot, &it_tp); if (! it) { continue; } diff --git a/src/level_anim.cpp b/src/level_anim.cpp index 67078de..6b38792 100644 --- a/src/level_anim.cpp +++ b/src/level_anim.cpp @@ -16,7 +16,7 @@ void level_anim(Levelp l) // // What level is the player on? // - auto player = level_thing_player(l); + auto player = thing_player(l); if (! player) { return; } @@ -37,7 +37,7 @@ void level_anim(Levelp l) for (auto y = l->miny; y < l->maxy; y++) { for (auto x = l->minx; x < l->maxx; x++) { Tpp tp; - Thingp t = level_thing_and_tp_get(l, x, y, z, slot, &tp); + Thingp t = thing_and_tp_get(l, x, y, z, slot, &tp); if (! t) { continue; } diff --git a/src/level_cursor.cpp b/src/level_cursor.cpp index a977c5a..d6656ef 100644 --- a/src/level_cursor.cpp +++ b/src/level_cursor.cpp @@ -208,7 +208,7 @@ static std::vector< point > level_cursor_path_draw_line(Levelp l, point start, p { static std::vector< point > empty; - auto player = level_thing_player(l); + auto player = thing_player(l); if (! player) { return empty; } @@ -245,7 +245,7 @@ void level_cursor_update(Levelp l) // memset(l->cursor, 0, sizeof(l->cursor)); - auto player = level_thing_player(l); + auto player = thing_player(l); if (! player) { return; } diff --git a/src/level_display.cpp b/src/level_display.cpp index 139a9d8..c93f4be 100644 --- a/src/level_display.cpp +++ b/src/level_display.cpp @@ -168,7 +168,7 @@ static void level_display_cursor(Levelp l, int x, int y) static void level_display_slot(Levelp l, int x, int y, int z, int slot, int depth, bool deco) { Tpp tp; - auto t = level_thing_and_tp_get(l, x, y, z, slot, &tp); + auto t = thing_and_tp_get(l, x, y, z, slot, &tp); if (! tp) { return; } @@ -187,7 +187,7 @@ void level_display(Levelp l) // // What level is the player on? // - auto player = level_thing_player(l); + auto player = thing_player(l); if (! player) { return; } diff --git a/src/level_player.cpp b/src/level_player.cpp index ffbf96c..7116aee 100644 --- a/src/level_player.cpp +++ b/src/level_player.cpp @@ -7,7 +7,7 @@ #include "my_level.hpp" #include "my_tp.hpp" -Thingp level_thing_player(Levelp l) +Thingp thing_player(Levelp l) { TRACE_NO_INDENT(); @@ -19,10 +19,10 @@ Thingp level_thing_player(Levelp l) return nullptr; } - return level_thing_find(l, l->player); + return thing_find(l, l->player); } -void level_thing_player_move_delta(Levelp l, int dx, int dy, int dz) +void thing_player_move_delta(Levelp l, int dx, int dy, int dz) { TRACE_NO_INDENT(); @@ -33,24 +33,24 @@ void level_thing_player_move_delta(Levelp l, int dx, int dy, int dz) return; } - auto t = level_thing_player(l); + auto t = thing_player(l); if (! t) { return; } - if (level_thing_can_move_to(l, t, t->x + dx, t->y + dy, t->z + dz)) { - level_thing_move(l, t, t->x + dx, t->y + dy, t->z + dz); + if (thing_can_move_to(l, t, t->x + dx, t->y + dy, t->z + dz)) { + thing_move(l, t, t->x + dx, t->y + dy, t->z + dz); level_tick_begin_requested(l, "player moved"); } - level_thing_player_move_reset(l); + thing_player_move_reset(l); } // // All keys have been released, forget any accumulation of events // -void level_thing_player_move_reset(Levelp l) +void thing_player_move_reset(Levelp l) { l->requested_move_up = false; l->requested_move_left = false; @@ -61,7 +61,7 @@ void level_thing_player_move_reset(Levelp l) // // Allow moves to accumulate so we can do diagonal moves. // -void level_thing_player_move_accum(Levelp l, bool up, bool down, bool left, bool right) +void thing_player_move_accum(Levelp l, bool up, bool down, bool left, bool right) { if (up) { l->requested_move_up = up; @@ -83,9 +83,9 @@ void level_thing_player_move_accum(Levelp l, bool up, bool down, bool left, bool // // Attempt to move // -bool level_thing_player_move_request(Levelp l, bool up, bool down, bool left, bool right) +bool thing_player_move_request(Levelp l, bool up, bool down, bool left, bool right) { - level_thing_player_move_accum(l, up, down, left, right); + thing_player_move_accum(l, up, down, left, right); // // If a move is in progress, do nothing @@ -99,24 +99,24 @@ bool level_thing_player_move_request(Levelp l, bool up, bool down, bool left, bo if (l->requested_move_up) { if (l->requested_move_keft) { - level_thing_player_move_delta(l, -1, -1, 0); + thing_player_move_delta(l, -1, -1, 0); } else if (l->requested_move_right) { - level_thing_player_move_delta(l, 1, -1, 0); + thing_player_move_delta(l, 1, -1, 0); } else { - level_thing_player_move_delta(l, 0, -1, 0); + thing_player_move_delta(l, 0, -1, 0); } } else if (l->requested_move_left) { if (l->requested_move_keft) { - level_thing_player_move_delta(l, -1, 1, 0); + thing_player_move_delta(l, -1, 1, 0); } else if (l->requested_move_right) { - level_thing_player_move_delta(l, 1, 1, 0); + thing_player_move_delta(l, 1, 1, 0); } else { - level_thing_player_move_delta(l, 0, 1, 0); + thing_player_move_delta(l, 0, 1, 0); } } else if (l->requested_move_keft) { - level_thing_player_move_delta(l, -1, 0, 0); + thing_player_move_delta(l, -1, 0, 0); } else if (l->requested_move_right) { - level_thing_player_move_delta(l, 1, 0, 0); + thing_player_move_delta(l, 1, 0, 0); } return true; diff --git a/src/level_scroll.cpp b/src/level_scroll.cpp index 48eacdb..620501a 100644 --- a/src/level_scroll.cpp +++ b/src/level_scroll.cpp @@ -15,7 +15,7 @@ void level_scroll_to_player(Levelp l) { TRACE_NO_INDENT(); - auto player = level_thing_player(l); + auto player = thing_player(l); if (! player) { return; } @@ -82,7 +82,7 @@ void level_scroll_warp_to_player(Levelp l) { TRACE_NO_INDENT(); - auto t = level_thing_player(l); + auto t = thing_player(l); if (! t) { return; } diff --git a/src/level_thing.cpp b/src/level_thing.cpp index c354024..c59b613 100644 --- a/src/level_thing.cpp +++ b/src/level_thing.cpp @@ -14,16 +14,7 @@ #include #include -Tpp tp(Thingp t) -{ - if (t->tp_id) { - return tp_find(t->tp_id); - } - - return nullptr; -} - -Thingp level_thing_get(Levelp l, int x, int y, int z, int slot) +Thingp thing_get(Levelp l, int x, int y, int z, int slot) { TRACE_NO_INDENT(); @@ -33,7 +24,7 @@ Thingp level_thing_get(Levelp l, int x, int y, int z, int slot) return nullptr; } - auto t = level_thing_find(l, id); + auto t = thing_find(l, id); if (! t) { return nullptr; } @@ -41,7 +32,7 @@ Thingp level_thing_get(Levelp l, int x, int y, int z, int slot) return t; } -Thingp level_thing_and_tp_get(Levelp l, int x, int y, int z, int slot, Tpp *out) +Thingp thing_and_tp_get(Levelp l, int x, int y, int z, int slot, Tpp *out) { TRACE_NO_INDENT(); @@ -55,7 +46,7 @@ Thingp level_thing_and_tp_get(Levelp l, int x, int y, int z, int slot, Tpp *out) return nullptr; } - auto t = level_thing_find(l, id); + auto t = thing_find(l, id); if (! t) { return nullptr; } @@ -67,8 +58,10 @@ Thingp level_thing_and_tp_get(Levelp l, int x, int y, int z, int slot, Tpp *out) return t; } -Thingp level_thing_find_optional(Level *l, ThingId id) +Thingp thing_find_optional(Level *l, ThingId id) { + TRACE_NO_INDENT(); + if (! id) { return nullptr; } @@ -85,8 +78,10 @@ Thingp level_thing_find_optional(Level *l, ThingId id) return nullptr; } -Thingp level_thing_find(Levelp l, ThingId id) +Thingp thing_find(Levelp l, ThingId id) { + TRACE_NO_INDENT(); + auto thing_id = id; auto index = THING_COMMON_ID_GET(thing_id); @@ -104,7 +99,7 @@ Thingp level_thing_find(Levelp l, ThingId id) return t; } -Thingp level_thing_new(Levelp l, Tpp tp, int x, int y, int z) +Thingp thing_new(Levelp l, Tpp tp, int x, int y, int z) { TRACE_NO_INDENT(); @@ -128,27 +123,33 @@ Thingp level_thing_new(Levelp l, Tpp tp, int x, int y, int z) t->id = thing_id; t->tp_id = tp_id_get(tp); + if (thing_is_monst(t) || thing_is_player(t)) { + thing_ai_new(l, t); + } + return t; } DIE("out of things"); } -void level_thing_free(Levelp l, Thingp t) +void thing_free(Levelp l, Thingp t) { TRACE_NO_INDENT(); - auto o = level_thing_find(l, t->id); + auto o = thing_find(l, t->id); if (t != o) { DIE("Thing mismatch found for id, %" PRIX32 "", t->id); } + thing_ai_free(l, t); + memset(t, 0, sizeof(*t)); } -ThingAip level_thing_ai_new(Levelp l, Thingp t) +ThingAip thing_ai_new(Levelp l, Thingp t) { - TRACE_AND_INDENT(); + TRACE_NO_INDENT(); static ThingAiId last_index; @@ -173,9 +174,9 @@ ThingAip level_thing_ai_new(Levelp l, Thingp t) return 0; } -void level_thing_ai_free(Levelp l, Thingp t) +void thing_ai_free(Levelp l, Thingp t) { - TRACE_AND_INDENT(); + TRACE_NO_INDENT(); auto ai_id = t->ai_id; if (! ai_id) { diff --git a/src/level_tick.cpp b/src/level_tick.cpp index 0d5d70f..f97f16e 100644 --- a/src/level_tick.cpp +++ b/src/level_tick.cpp @@ -70,7 +70,7 @@ void level_tick_body(Levelp l, float dt) { TRACE_NO_INDENT(); - auto p = level_thing_player(l); + auto p = thing_player(l); int player_speed = p ? p->speed : 100; FOR_ALL_THINGS(l, t) @@ -89,7 +89,7 @@ void level_tick_body(Levelp l, float dt) t->thing_dt = 1.0; } - level_thing_interpolate(l, t, t->thing_dt); + thing_interpolate(l, t, t->thing_dt); if (t->thing_dt >= 1.0) { // thing tick diff --git a/src/level_tiles.cpp b/src/level_tiles.cpp index 6599eb1..1c93e47 100644 --- a/src/level_tiles.cpp +++ b/src/level_tiles.cpp @@ -80,7 +80,7 @@ void level_assign_tiles(Levelp l, int z) for (auto y = 0; y < MAP_HEIGHT; y++) { for (auto x = 0; x < MAP_WIDTH; x++) { Tpp tp; - auto t = level_thing_and_tp_get(l, x, y, z, slot, &tp); + auto t = thing_and_tp_get(l, x, y, z, slot, &tp); if (! t) { continue; } diff --git a/src/main.cpp b/src/main.cpp index 5b31373..9122225 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -703,7 +703,7 @@ int main(int argc, char *argv[]) { TRACE_NO_INDENT(); - CON("INI: Load early gfx tiles, text, UI etc..."); + LOG("INI: Load early gfx tiles, text, UI etc..."); gfx_init(); LOG("INI: Loaded"); } @@ -718,7 +718,7 @@ int main(int argc, char *argv[]) { TRACE_NO_INDENT(); - CON("INI: Load fonts"); + LOG("INI: Load fonts"); if (! font_init()) { ERR("Font init"); } @@ -727,7 +727,7 @@ int main(int argc, char *argv[]) { TRACE_NO_INDENT(); - CON("INI: Load widgets"); + LOG("INI: Load widgets"); if (! wid_init()) { ERR("Wid init"); } @@ -736,7 +736,7 @@ int main(int argc, char *argv[]) { TRACE_NO_INDENT(); - CON("INI: Load console"); + LOG("INI: Load console"); if (! wid_console_init()) { ERR("Wid_console init"); } @@ -757,7 +757,7 @@ int main(int argc, char *argv[]) { TRACE_NO_INDENT(); - CON("INI: Load tiles"); + LOG("INI: Load tiles"); if (! wid_tiles_init()) { ERR("Wid tiles init"); } @@ -774,7 +774,7 @@ int main(int argc, char *argv[]) { TRACE_NO_INDENT(); - CON("INI: Load textures"); + LOG("INI: Load textures"); if (! tex_init()) { ERR("Tex init"); } @@ -784,7 +784,7 @@ int main(int argc, char *argv[]) { TRACE_NO_INDENT(); - CON("INI: Load audio"); + LOG("INI: Load audio"); if (! audio_init()) { ERR("Audio init"); } @@ -794,7 +794,7 @@ int main(int argc, char *argv[]) { TRACE_NO_INDENT(); - CON("INI: Load music"); + LOG("INI: Load music"); if (! music_init()) { ERR("Music init"); } @@ -804,7 +804,7 @@ int main(int argc, char *argv[]) { TRACE_NO_INDENT(); - CON("INI: Load sound"); + LOG("INI: Load sound"); if (! sound_init()) { ERR("Sound init"); } diff --git a/src/music.cpp b/src/music.cpp index d9f2d8e..c3a695e 100644 --- a/src/music.cpp +++ b/src/music.cpp @@ -103,15 +103,20 @@ bool music_load(uint32_t rate, const char *file, const char *name_alias) if (! m->m) { ERR("Mix_LoadMUS_RW fail [%s]: %s %s", file, Mix_GetError(), SDL_GetError()); SDL_ClearError(); + SDL_RWclose(rw); + delete m; return false; } auto result = all_music.insert(std::make_pair(name_alias, m)); if (! result.second) { - ERR("Cannot insert music name [%s] failed", name_alias); + ERR("Cannot insert music name [%s]", name_alias); + SDL_RWclose(rw); + delete m; return false; } + SDL_RWclose(rw); // DBG("Load %s", file); return true; diff --git a/src/my_enums.hpp b/src/my_enums.hpp index 50aa5f1..d4e7984 100644 --- a/src/my_enums.hpp +++ b/src/my_enums.hpp @@ -42,6 +42,18 @@ enum { THING_RARITY_UNIQUE, }; +enum { + THING_DIR_NONE, + THING_DIR_DOWN, + THING_DIR_UP, + THING_DIR_LEFT, + THING_DIR_RIGHT, + THING_DIR_TL, + THING_DIR_BL, + THING_DIR_TR, + THING_DIR_BR, +}; + enum { MONST_CLASS_A, MONST_CLASS_MAX }; /* clang-format off */ @@ -103,32 +115,32 @@ enum { MONST_CLASS_A, MONST_CLASS_MAX }; #define tp_is_lava(tp) tp_flag(tp, is_lava) #define tp_is_chasm(tp) tp_flag(tp, is_chasm) -#define thing_is_able_to_walk_through_walls(thing) tp_flag(tp(thing), is_able_to_walk_through_walls) -#define thing_is_animated_can_hflip(thing) tp_flag(tp(thing), is_animated_can_hflip) -#define thing_is_animated_no_dir(thing) tp_flag(tp(thing), is_animated_no_dir) -#define thing_is_blit_centered(thing) tp_flag(tp(thing), is_blit_centered) -#define thing_is_blit_on_ground(thing) tp_flag(tp(thing), is_blit_on_ground) -#define thing_is_blit_outlined(thing) tp_flag(tp(thing), is_blit_outlined) -#define thing_is_blit_square_outlined(thing) tp_flag(tp(thing), is_blit_square_outlined) -#define thing_is_cursor(thing) tp_flag(tp(thing), is_cursor) -#define thing_is_cursor_hazard(thing) tp_flag(tp(thing), is_cursor_hazard) -#define thing_is_cursor_path_blocker(thing) tp_flag(tp(thing), is_cursor_path_blocker) -#define thing_is_cursor_path_hazard(thing) tp_flag(tp(thing), is_cursor_path_hazard) -#define thing_is_cursor_path(thing) tp_flag(tp(thing), is_cursor_path) -#define thing_is_door(thing) tp_flag(tp(thing), is_door) -#define thing_is_dungeon_entrance(thing) tp_flag(tp(thing), is_dungeon_entrance) -#define thing_is_exit(thing) tp_flag(tp(thing), is_exit) -#define thing_is_floor(thing) tp_flag(tp(thing), is_floor) -#define thing_is_key(thing) tp_flag(tp(thing), is_key) -#define thing_is_monst(thing) tp_flag(tp(thing), is_monst) -#define thing_is_obs_monst(thing) tp_flag(tp(thing), is_obs_monst) -#define thing_is_obs_player(thing) tp_flag(tp(thing), is_obs_player) -#define thing_is_obs_wall_or_door(thing) tp_flag(tp(thing), is_obs_wall_or_door) -#define thing_is_player(thing) tp_flag(tp(thing), is_player) -#define thing_is_tiled(thing) tp_flag(tp(thing), is_tiled) -#define thing_is_wall(thing) tp_flag(tp(thing), is_wall) -#define thing_is_lava(thing) tp_flag(tp(thing), is_lava) -#define thing_is_chasm(thing) tp_flag(tp(thing), is_chasm) +#define thing_is_able_to_walk_through_walls(thing) tp_flag(thing_tp(thing), is_able_to_walk_through_walls) +#define thing_is_animated_can_hflip(thing) tp_flag(thing_tp(thing), is_animated_can_hflip) +#define thing_is_animated_no_dir(thing) tp_flag(thing_tp(thing), is_animated_no_dir) +#define thing_is_blit_centered(thing) tp_flag(thing_tp(thing), is_blit_centered) +#define thing_is_blit_on_ground(thing) tp_flag(thing_tp(thing), is_blit_on_ground) +#define thing_is_blit_outlined(thing) tp_flag(thing_tp(thing), is_blit_outlined) +#define thing_is_blit_square_outlined(thing) tp_flag(thing_tp(thing), is_blit_square_outlined) +#define thing_is_cursor(thing) tp_flag(thing_tp(thing), is_cursor) +#define thing_is_cursor_hazard(thing) tp_flag(thing_tp(thing), is_cursor_hazard) +#define thing_is_cursor_path_blocker(thing) tp_flag(thing_tp(thing), is_cursor_path_blocker) +#define thing_is_cursor_path_hazard(thing) tp_flag(thing_tp(thing), is_cursor_path_hazard) +#define thing_is_cursor_path(thing) tp_flag(thing_tp(thing), is_cursor_path) +#define thing_is_door(thing) tp_flag(thing_tp(thing), is_door) +#define thing_is_dungeon_entrance(thing) tp_flag(thing_tp(thing), is_dungeon_entrance) +#define thing_is_exit(thing) tp_flag(thing_tp(thing), is_exit) +#define thing_is_floor(thing) tp_flag(thing_tp(thing), is_floor) +#define thing_is_key(thing) tp_flag(thing_tp(thing), is_key) +#define thing_is_monst(thing) tp_flag(thing_tp(thing), is_monst) +#define thing_is_obs_monst(thing) tp_flag(thing_tp(thing), is_obs_monst) +#define thing_is_obs_player(thing) tp_flag(thing_tp(thing), is_obs_player) +#define thing_is_obs_wall_or_door(thing) tp_flag(thing_tp(thing), is_obs_wall_or_door) +#define thing_is_player(thing) tp_flag(thing_tp(thing), is_player) +#define thing_is_tiled(thing) tp_flag(thing_tp(thing), is_tiled) +#define thing_is_wall(thing) tp_flag(thing_tp(thing), is_wall) +#define thing_is_lava(thing) tp_flag(thing_tp(thing), is_lava) +#define thing_is_chasm(thing) tp_flag(thing_tp(thing), is_chasm) #define level_is_able_to_walk_through_walls(level, x, y, z) level_flag(level, is_able_to_walk_through_walls, x, y, z) #define level_is_animated_can_hflip(level, x, y, z) level_flag(level, is_animated_can_hflip, x, y, z) diff --git a/src/my_level.hpp b/src/my_level.hpp index 2bab751..2522374 100644 --- a/src/my_level.hpp +++ b/src/my_level.hpp @@ -120,19 +120,19 @@ bool level_is_oob(Levelp, int x, int y); bool level_is_oob(Levelp, int x, int y, int z); bool level_is_same_type(Levelp, int x, int y, int z, Tpp); bool level_set_thing_id(Levelp, int x, int y, int z, int slot, ThingId); -bool level_thing_can_move_to(Levelp, Thingp, int new_loc_x, int new_loc_y, int new_loc_z); -bool level_thing_player_move_request(Levelp, bool up, bool down, bool left, bool right); +bool thing_can_move_to(Levelp, Thingp, int new_loc_x, int new_loc_y, int new_loc_z); +bool thing_player_move_request(Levelp, bool up, bool down, bool left, bool right); bool level_tick_is_in_progress(Levelp); Levelp level_constructor(void); ThingId level_get_thing_id(Levelp, int x, int y, int z, int slot); -Thingp level_thing_and_tp_get(Levelp, int x, int y, int z, int slot, Tpp * = nullptr); -Thingp level_thing_find(Levelp, ThingId id); -Thingp level_thing_find_optional(Levelp, ThingId id); -Thingp level_thing_get(Levelp, int x, int y, int z, int slot); -Thingp level_thing_init(Levelp, Tpp, int x, int y, int z); -Thingp level_thing_new(Levelp, Tpp, int x, int y, int z); -Thingp level_thing_player(Levelp); -Tpp level_thing_tp(Levelp, Thingp); +Thingp thing_and_tp_get(Levelp, int x, int y, int z, int slot, Tpp * = nullptr); +Thingp thing_find(Levelp, ThingId id); +Thingp thing_find_optional(Levelp, ThingId id); +Thingp thing_get(Levelp, int x, int y, int z, int slot); +Thingp thing_init(Levelp, Tpp, int x, int y, int z); +Thingp thing_new(Levelp, Tpp, int x, int y, int z); +Thingp thing_player(Levelp); +Tpp thing_tp(Levelp, Thingp); void level_anim(Levelp); void level_assign_tiles(Levelp, int z); void level_bounds_set(Levelp); @@ -145,16 +145,16 @@ void level_mouse_position_get(Levelp); void level_scroll_delta(Levelp, int, int); void level_scroll_to_player(Levelp); void level_scroll_warp_to_player(Levelp); -void level_thing_free(Levelp, Thingp); -void level_thing_interpolate(Levelp, Thingp, float dt); -void level_thing_move(Levelp, Thingp, int new_x, int new_y, int new_z); -void level_thing_player_map_center(Levelp); -void level_thing_player_move_accum(Levelp, bool up, bool down, bool left, bool right); -void level_thing_player_move_delta(Levelp, int dx, int dy, int dz); -void level_thing_player_move_reset(Levelp); -void level_thing_pop(Levelp, Thingp); -void level_thing_push(Levelp, Thingp); -void level_thing_update(Levelp, Thingp); +void thing_free(Levelp, Thingp); +void thing_interpolate(Levelp, Thingp, float dt); +void thing_move(Levelp, Thingp, int new_x, int new_y, int new_z); +void thing_player_map_center(Levelp); +void thing_player_move_accum(Levelp, bool up, bool down, bool left, bool right); +void thing_player_move_delta(Levelp, int dx, int dy, int dz); +void thing_player_move_reset(Levelp); +void thing_pop(Levelp, Thingp); +void thing_push(Levelp, Thingp); +void thing_update(Levelp, Thingp); void level_tick_begin(Levelp); void level_tick_begin_requested(Levelp, const char *); void level_tick_body(Levelp, float dt); @@ -177,17 +177,17 @@ void level_cursor_set(Levelp, int x, int y); Thingp t; \ for (auto _id_ = 0; _id_ < 1 << THING_COMMON_ID_BITS; _id_++) \ if (_l_copy_.thing_body[ _id_ ].id) \ - if ((_t_ = level_thing_find_optional(_l_, _l_copy_.thing_body[ _id_ ].id))) + if ((_t_ = thing_find_optional(_l_, _l_copy_.thing_body[ _id_ ].id))) #define FOR_ALL_THINGS_AT(_l_, _t_, _x_, _y_, _z_) \ Thingp _t_; \ - for (auto _slot_ = 0; _t_ = level_thing_get(_l_, _x_, _y_, _z_, _slot_), _slot_ < MAP_SLOTS; _slot_++) \ + for (auto _slot_ = 0; _t_ = thing_get(_l_, _x_, _y_, _z_, _slot_), _slot_ < MAP_SLOTS; _slot_++) \ if (_t_) #define FOR_ALL_THINGS_AND_TPS_AT(_l_, _t_, _tp_, _x_, _y_, _z_) \ Thingp _t_; \ Tpp _tp_; \ - for (auto _slot_ = 0; _t_ = level_thing_and_tp_get(_l_, _x_, _y_, _z_, _slot_, &_tp_), _slot_ < MAP_SLOTS; \ + for (auto _slot_ = 0; _t_ = thing_and_tp_get(_l_, _x_, _y_, _z_, _slot_, &_tp_), _slot_ < MAP_SLOTS; \ _slot_++) \ if (_t_) diff --git a/src/my_thing.hpp b/src/my_thing.hpp index 9946b1b..d537849 100644 --- a/src/my_thing.hpp +++ b/src/my_thing.hpp @@ -27,18 +27,6 @@ #define THING_AI_MAX 65535 /* sizeof ai_id */ #define THING_MOVE_PATH_MAX (MAP_WIDTH * MAP_HEIGHT) -enum { - THING_DIR_NONE, - THING_DIR_DOWN, - THING_DIR_UP, - THING_DIR_LEFT, - THING_DIR_RIGHT, - THING_DIR_TL, - THING_DIR_BL, - THING_DIR_TR, - THING_DIR_BR, -}; - typedef struct ThingAi_ { // // Unique ID @@ -115,7 +103,7 @@ typedef struct Thing_ { uint8_t count[ THING_FLAG_MAX ]; } Thing; -Tpp tp(Thingp t); +Tpp thing_tp(Thingp t); bool thing_is_dir_down(Thingp t); bool thing_is_dir_tr(Thingp t); @@ -136,7 +124,7 @@ void thing_dir_right_set(Thingp t, uint8_t); void thing_dir_up_set(Thingp t, uint8_t); void thing_set_dir_from_delta(Thingp, int dx, int dy); -ThingAip level_thing_ai_new(Levelp, Thingp); -void level_thing_ai_free(Levelp, Thingp); +ThingAip thing_ai_new(Levelp, Thingp); +void thing_ai_free(Levelp, Thingp); #endif diff --git a/src/sdl_events.cpp b/src/sdl_events.cpp index c536fc0..9c641be 100644 --- a/src/sdl_events.cpp +++ b/src/sdl_events.cpp @@ -567,7 +567,7 @@ static void sdl_key_repeat_events_(void) } if (time_have_x_hundredths_passed_since(SDL_KEY_REPEAT_PLAYER, last_movement_keypress)) { - if (level_thing_player_move_request(l, up, down, left, right)) { + if (thing_player_move_request(l, up, down, left, right)) { last_movement_keypress = time_ms(); if (up_pressed > 0) { diff --git a/src/sound.cpp b/src/sound.cpp index ec5978c..04d08ea 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -106,17 +106,20 @@ bool sound_load(float volume, const std::string &file, const std::string &alias) if (! s->chunk) { ERR("Mix_LoadWAV_RW fail [%s]: %s %s", file.c_str(), Mix_GetError(), SDL_GetError()); SDL_ClearError(); + SDL_RWclose(rw); delete s; return false; } auto result = all_sound.insert(std::make_pair(alias, s)); if (! result.second) { - ERR("Cannot insert sound name [%s] failed", alias.c_str()); + ERR("Cannot insert sound name [%s]", alias.c_str()); + SDL_RWclose(rw); delete s; return false; } + SDL_RWclose(rw); // DBG("Load %s", file.c_str()); return true; diff --git a/src/thing.cpp b/src/thing.cpp index a6dae54..445e91d 100644 --- a/src/thing.cpp +++ b/src/thing.cpp @@ -16,11 +16,20 @@ ENUM_DEF_C(THING_FLAG_ENUM, ThingFlag) -Thingp level_thing_init(Levelp l, Tpp tp, int x, int y, int z) +Tpp thing_tp(Thingp t) +{ + if (t->tp_id) { + return tp_find(t->tp_id); + } + + return nullptr; +} + +Thingp thing_init(Levelp l, Tpp tp, int x, int y, int z) { TRACE_NO_INDENT(); - auto t = level_thing_new(l, tp, x, y, z); + auto t = thing_new(l, tp, x, y, z); if (! t) { return nullptr; } @@ -56,21 +65,21 @@ Thingp level_thing_init(Levelp l, Tpp tp, int x, int y, int z) } } - level_thing_update(l, t); + thing_update(l, t); return t; } -void level_thing_update(Level *l, Thingp t) +void thing_update(Level *l, Thingp t) { TRACE_NO_INDENT(); - auto tp = level_thing_tp(l, t); + auto tp = thing_tp(l, t); t->speed = tp_speed_get(tp); } -Tpp level_thing_tp(Level *l, Thingp t) +Tpp thing_tp(Level *l, Thingp t) { TRACE_NO_INDENT(); return tp_find(t->tp_id); diff --git a/src/thing_move.cpp b/src/thing_move.cpp index 1e01f13..4cdeb93 100644 --- a/src/thing_move.cpp +++ b/src/thing_move.cpp @@ -13,7 +13,7 @@ void thing_dir_set_none(Thingp t) { - if (tp_is_animated_no_dir(tp(t))) { + if (tp_is_animated_no_dir(thing_tp(t))) { return; } @@ -27,7 +27,7 @@ bool thing_is_dir_none(Thingp t) { return (t->dir == THING_DIR_NONE); } void thing_dir_set_down(Thingp t) { - if (tp_is_animated_no_dir(tp(t))) { + if (tp_is_animated_no_dir(thing_tp(t))) { return; } @@ -41,7 +41,7 @@ bool thing_is_dir_down(Thingp t) { return (t->dir == THING_DIR_DOWN); } void thing_dir_set_up(Thingp t) { - if (tp_is_animated_no_dir(tp(t))) { + if (tp_is_animated_no_dir(thing_tp(t))) { return; } @@ -55,7 +55,7 @@ bool thing_is_dir_up(Thingp t) { return (t->dir == THING_DIR_UP); } void thing_dir_set_left(Thingp t) { - if (tp_is_animated_no_dir(tp(t))) { + if (tp_is_animated_no_dir(thing_tp(t))) { return; } @@ -69,7 +69,7 @@ bool thing_is_dir_left(Thingp t) { return (t->dir == THING_DIR_LEFT); } void thing_dir_set_right(Thingp t) { - if (tp_is_animated_no_dir(tp(t))) { + if (tp_is_animated_no_dir(thing_tp(t))) { return; } @@ -83,7 +83,7 @@ bool thing_is_dir_right(Thingp t) { return (t->dir == THING_DIR_RIGHT); } void thing_dir_set_tl(Thingp t) { - if (tp_is_animated_no_dir(tp(t))) { + if (tp_is_animated_no_dir(thing_tp(t))) { return; } @@ -97,7 +97,7 @@ bool thing_is_dir_tl(Thingp t) { return (t->dir == THING_DIR_TL); } void thing_dir_set_bl(Thingp t) { - if (tp_is_animated_no_dir(tp(t))) { + if (tp_is_animated_no_dir(thing_tp(t))) { return; } @@ -111,7 +111,7 @@ bool thing_is_dir_bl(Thingp t) { return (t->dir == THING_DIR_BL); } void thing_dir_set_tr(Thingp t) { - if (tp_is_animated_no_dir(tp(t))) { + if (tp_is_animated_no_dir(thing_tp(t))) { return; } @@ -125,7 +125,7 @@ bool thing_is_dir_tr(Thingp t) { return (t->dir == THING_DIR_TR); } void thing_dir_set_br(Thingp t) { - if (tp_is_animated_no_dir(tp(t))) { + if (tp_is_animated_no_dir(thing_tp(t))) { return; } @@ -184,7 +184,7 @@ void thing_set_dir_from_delta(Thingp t, int dx, int dy) } } -void level_thing_move(Levelp l, Thingp t, int new_x, int new_y, int new_z) +void thing_move(Levelp l, Thingp t, int new_x, int new_y, int new_z) { if (level_is_oob(l, new_x, new_y)) { return; @@ -194,7 +194,7 @@ void level_thing_move(Levelp l, Thingp t, int new_x, int new_y, int new_z) return; } - level_thing_pop(l, t); + thing_pop(l, t); t->pix_x = t->x * TILE_WIDTH; t->pix_y = t->y * TILE_HEIGHT; @@ -207,10 +207,10 @@ void level_thing_move(Levelp l, Thingp t, int new_x, int new_y, int new_z) t->y = new_y; t->z = new_z; - level_thing_push(l, t); + thing_push(l, t); } -bool level_thing_can_move_to(Levelp l, Thingp t, int new_loc_x, int new_loc_y, int new_loc_z) +bool thing_can_move_to(Levelp l, Thingp t, int new_loc_x, int new_loc_y, int new_loc_z) { if (level_is_oob(l, new_loc_x, new_loc_y)) { return false; @@ -224,7 +224,7 @@ bool level_thing_can_move_to(Levelp l, Thingp t, int new_loc_x, int new_loc_y, i auto dy = new_loc_y - t->y; thing_set_dir_from_delta(t, dx, dy); - auto my_tp = level_thing_tp(l, t); + auto my_tp = thing_tp(l, t); FOR_ALL_THINGS_AND_TPS_AT(l, it, it_tp, new_loc_x, new_loc_y, new_loc_z) { @@ -240,7 +240,7 @@ bool level_thing_can_move_to(Levelp l, Thingp t, int new_loc_x, int new_loc_y, i return true; } -void level_thing_interpolate(Level *l, Thingp t, float dt) +void thing_interpolate(Level *l, Thingp t, float dt) { if ((t->old_x == t->x) && (t->old_y == t->y)) { return; @@ -253,7 +253,7 @@ void level_thing_interpolate(Level *l, Thingp t, float dt) t->pix_y = pix_y * TILE_HEIGHT; } -void level_thing_push(Levelp l, Thingp t) +void thing_push(Levelp l, Thingp t) { TRACE_NO_INDENT(); @@ -278,7 +278,7 @@ void level_thing_push(Levelp l, Thingp t) // // Detach from the old location // - level_thing_pop(l, t); + thing_pop(l, t); // // Need to push to the new location. @@ -291,7 +291,7 @@ void level_thing_push(Levelp l, Thingp t) // // Keep track of tiles the player has been on. // - if (tp_is_player(tp(t))) { + if (tp_is_player(thing_tp(t))) { l->is_walked[ x ][ y ][ z ] = true; } @@ -310,7 +310,7 @@ void level_thing_push(Levelp l, Thingp t) ERR("out of thing slots"); } -void level_thing_pop(Levelp l, Thingp t) +void thing_pop(Levelp l, Thingp t) { TRACE_NO_INDENT();