Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scene by name #792

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/audio/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "audio/backends/audio_backend.h"
#include "resources/pathmanager.h"
#include "resources/sounds_loader.h"
#include "utils/c_array_util.h"
#include "utils/log.h"

// If-def the includes here
Expand All @@ -28,7 +29,7 @@ static audio_backend_init all_backends[] = {
null_audio_backend_set_callbacks,
#endif
};
static int all_backends_count = sizeof(all_backends) / sizeof(audio_backend_init);
static int all_backends_count = N_ELEMENTS(all_backends);

// All backends that are available. This is filled by audio_scan_backends().
static struct available_backend {
Expand Down
5 changes: 3 additions & 2 deletions src/audio/backends/null/null_backend.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "audio/backends/null/null_backend.h"
#include "audio/backends/audio_backend.h"
#include "utils/c_array_util.h"
#include "utils/log.h"

#include <assert.h>
Expand All @@ -9,12 +10,12 @@
static const audio_sample_rate supported_sample_rates[] = {
{44100, 1, "44100Hz"},
};
static const int supported_sample_rate_count = sizeof(supported_sample_rates) / sizeof(audio_resampler);
static const int supported_sample_rate_count = N_ELEMENTS(supported_sample_rates);

static const audio_resampler supported_resamplers[] = {
{0, 1, "Linear"},
};
static const int supported_resamplers_count = sizeof(supported_resamplers) / sizeof(audio_resampler);
static const int supported_resamplers_count = N_ELEMENTS(supported_resamplers);

static bool is_available(void) {
return true; // This is always available if compiled in.
Expand Down
5 changes: 3 additions & 2 deletions src/audio/backends/sdl/sdl_backend.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "audio/backends/sdl/sdl_backend.h"
#include "audio/backends/audio_backend.h"
#include "utils/allocator.h"
#include "utils/c_array_util.h"
#include "utils/log.h"
#include "utils/miscmath.h"

Expand All @@ -19,14 +20,14 @@ static const audio_sample_rate supported_sample_rates[] = {
{44100, 0, "44100Hz"},
{48000, 1, "48000Hz"},
};
static const int supported_sample_rate_count = sizeof(supported_sample_rates) / sizeof(audio_resampler);
static const int supported_sample_rate_count = N_ELEMENTS(supported_sample_rates);

static const audio_resampler supported_resamplers[] = {
{XMP_INTERP_NEAREST, 0, "Nearest"},
{XMP_INTERP_LINEAR, 1, "Linear" },
{XMP_INTERP_SPLINE, 0, "Cubic" },
};
static const int supported_resamplers_count = sizeof(supported_resamplers) / sizeof(audio_resampler);
static const int supported_resamplers_count = N_ELEMENTS(supported_resamplers);

typedef struct sdl_audio_context {
int sample_rate;
Expand Down
3 changes: 3 additions & 0 deletions src/console/console_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ int console_cmd_scene(game_state *gs, int argc, char **argv) {
if(strtoint(argv[1], &i) && is_scene(i)) {
game_state_set_next(gs, i);
return 0;
} else if((i = scene_get_id(argv[1])) > 0) {
game_state_set_next(gs, i);
return 0;
}
}
return 1;
Expand Down
3 changes: 1 addition & 2 deletions src/controller/ai_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "resources/af_loader.h"
#include "resources/ids.h"
#include "utils/allocator.h"
#include "utils/c_array_util.h"
#include "utils/log.h"
#include "utils/random.h"
#include "utils/vec.h"
Expand Down Expand Up @@ -37,8 +38,6 @@
/* likelihood of attempting a random attack/tactic (lower is more likely) */
#define RANDOM_ATTACK_CHANCE 10

#define N_ELEMENTS(array) (sizeof(array) / sizeof((array)[0]))

typedef struct {
int max_hit_dist;
int min_hit_dist;
Expand Down
9 changes: 5 additions & 4 deletions src/formats/score.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "formats/internal/reader.h"
#include "formats/internal/writer.h"
#include "formats/score.h"
#include "utils/c_array_util.h"

int sd_score_create(sd_score *score) {
if(score == NULL) {
Expand All @@ -26,8 +27,8 @@ int sd_score_load(sd_score *score, const char *filename) {
return SD_FILE_OPEN_ERROR;
}

for(unsigned i = 0; i < sizeof(score->scores) / sizeof(score->scores[0]); i++) {
for(unsigned j = 0; j < sizeof(score->scores[0]) / sizeof(score->scores[0][0]); j++) {
for(unsigned i = 0; i < N_ELEMENTS(score->scores); i++) {
for(unsigned j = 0; j < N_ELEMENTS(score->scores[0]); j++) {
sd_score_entry *e = &score->scores[i][j];
e->score = sd_read_udword(r);
sd_read_buf(r, e->name, sizeof(e->name));
Expand Down Expand Up @@ -59,8 +60,8 @@ int sd_score_save(const sd_score *score, const char *filename) {
return SD_FILE_OPEN_ERROR;
}

for(unsigned i = 0; i < sizeof(score->scores) / sizeof(score->scores[0]); i++) {
for(unsigned j = 0; j < sizeof(score->scores[0]) / sizeof(score->scores[0][0]); j++) {
for(unsigned i = 0; i < N_ELEMENTS(score->scores); i++) {
for(unsigned j = 0; j < N_ELEMENTS(score->scores[0]); j++) {
const sd_score_entry *e = &score->scores[i][j];
sd_write_udword(w, e->score);
sd_write_buf(w, e->name, sizeof(e->name));
Expand Down
8 changes: 8 additions & 0 deletions src/game/common_defines.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "game/common_defines.h"
#include "resources/ids.h"
#include "utils/c_array_util.h"
#include "utils/random.h"
#include <stddef.h>

Expand Down Expand Up @@ -66,6 +67,13 @@ const char *scene_get_name(unsigned int id) {
return scene_type_names[id];
}

int scene_get_id(const char *name) {
for(unsigned i = 0; i < N_ELEMENTS(scene_type_names); ++i)
if(strcmp(scene_type_names[i], name) == 0)
return (int)i;
return -1;
}

// For these to work, the resources table has to match the ID tables
int har_to_resource(unsigned int id) {
return AF_JAGUAR + id;
Expand Down
2 changes: 2 additions & 0 deletions src/game/common_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const char *pilot_get_name(unsigned int id);
const char *round_get_name(unsigned int id);
const char *scene_get_name(unsigned int id);

int scene_get_id(const char *);

int har_to_resource(unsigned int id);
int scene_to_resource(unsigned int id);

Expand Down
3 changes: 2 additions & 1 deletion src/game/game_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "game/utils/ticktimer.h"
#include "resources/pilots.h"
#include "utils/allocator.h"
#include "utils/c_array_util.h"
#include "utils/log.h"
#include "utils/miscmath.h"
#include "video/vga_state.h"
Expand Down Expand Up @@ -779,7 +780,7 @@ game_player *game_state_get_player(const game_state *gs, int player_id) {
}

int game_state_num_players(game_state *gs) {
return sizeof(gs->players) / sizeof(game_player *);
return N_ELEMENTS(gs->players);
}

void _setup_keyboard(game_state *gs, int player_id) {
Expand Down
5 changes: 3 additions & 2 deletions src/game/scenes/mainmenu/menu_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "game/gui/gui.h"
#include "game/utils/settings.h"
#include "utils/allocator.h"
#include "utils/c_array_util.h"
#include "utils/c_string_util.h"
#include "video/video.h"

Expand Down Expand Up @@ -54,7 +55,7 @@ resolution *find_resolution_by_settings(settings *s) {
int w = s->video.screen_w;
int h = s->video.screen_h;

for(unsigned i = 0; i < sizeof(_resolutions) / sizeof(resolution); i++) {
for(unsigned i = 0; i < N_ELEMENTS(_resolutions); i++) {
if(w == _resolutions[i].w && h == _resolutions[i].h) {
return &_resolutions[i];
}
Expand Down Expand Up @@ -177,7 +178,7 @@ component *menu_video_create(scene *s) {
}

// Add standard resolutions
for(unsigned i = 0; i < sizeof(_resolutions) / sizeof(resolution); i++) {
for(unsigned i = 0; i < N_ELEMENTS(_resolutions); i++) {
textselector_add_option(res_selector, _resolutions[i].name);
if(!local->is_custom_resolution && _resolutions[i].w == res->w && _resolutions[i].h == res->h) {
textselector_set_pos(res_selector, i);
Expand Down
7 changes: 4 additions & 3 deletions src/game/scenes/mechlab.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "resources/pathmanager.h"
#include "resources/sgmanager.h"
#include "utils/allocator.h"
#include "utils/c_array_util.h"
#include "utils/log.h"
#include "video/video.h"

Expand Down Expand Up @@ -130,7 +131,7 @@ void mechlab_free(scene *scene) {
PERROR("Failed to save pilot %s", player1->chr->pilot.name);
}

for(unsigned i = 0; i < sizeof(local->bg_obj) / sizeof(object); i++) {
for(unsigned i = 0; i < N_ELEMENTS(local->bg_obj); i++) {
object_free(&local->bg_obj[i]);
}

Expand Down Expand Up @@ -381,7 +382,7 @@ int mechlab_event(scene *scene, SDL_Event *event) {
void mechlab_render(scene *scene) {
mechlab_local *local = scene_get_userdata(scene);

for(unsigned i = 0; i < sizeof(local->bg_obj) / sizeof(object); i++) {
for(unsigned i = 0; i < N_ELEMENTS(local->bg_obj); i++) {
if(local->dashtype == DASHBOARD_SELECT_TOURNAMENT && i > 0) {
continue;
}
Expand Down Expand Up @@ -471,7 +472,7 @@ int mechlab_create(scene *scene) {
animation *bg_ani[3];

// Init the background
for(unsigned i = 0; i < sizeof(bg_ani) / sizeof(animation *); i++) {
for(unsigned i = 0; i < N_ELEMENTS(bg_ani); i++) {
sprite *spr = sprite_copy(animation_get_sprite(&bk_get_info(scene->bk_data, 14)->ani, i));
bg_ani[i] = create_animation_from_single(spr, spr->pos);
object_create(&local->bg_obj[i], scene->gs, vec2i_create(0, 0), vec2f_create(0, 0));
Expand Down
3 changes: 2 additions & 1 deletion src/game/scenes/mechlab/lab_menu_customize.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "game/utils/formatting.h"
#include "resources/bk.h"
#include "resources/languages.h"
#include "utils/c_array_util.h"
#include "utils/log.h"

// TODO put these somewhere central
Expand Down Expand Up @@ -813,7 +814,7 @@ int sell_highest_value_upgrade(sd_pilot *pilot, char *sold) {
};
int max_idx = -1;
int32_t max_price = 0;
for(unsigned i = 0; i < sizeof(prices) / sizeof(prices[0]); ++i) {
for(unsigned i = 0; i < N_ELEMENTS(prices); ++i) {
if(prices[i] > max_price) {
max_price = prices[i];
max_idx = i;
Expand Down
9 changes: 5 additions & 4 deletions src/game/utils/settings.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "game/utils/settings.h"
#include "controller/controller.h"
#include "utils/allocator.h"
#include "utils/c_array_util.h"
#include "utils/c_string_util.h"
#include "utils/config.h"
#include "utils/log.h"
Expand Down Expand Up @@ -276,22 +277,22 @@ int settings_write_defaults(const char *path) {
int settings_init(const char *path) {
settings_path = path;
memset(&_settings, 0, sizeof(settings));
for(unsigned i = 0; i < sizeof(struct_to_fields) / sizeof(struct_to_field); i++) {
for(unsigned i = 0; i < N_ELEMENTS(struct_to_fields); i++) {
const struct_to_field *s2f = &struct_to_fields[i];
settings_add_fields(s2f->fields, s2f->num_fields);
}
return conf_init(settings_path);
}

void settings_load(void) {
for(unsigned i = 0; i < sizeof(struct_to_fields) / sizeof(struct_to_field); i++) {
for(unsigned i = 0; i < N_ELEMENTS(struct_to_fields); i++) {
const struct_to_field *s2f = &struct_to_fields[i];
settings_load_fields(s2f->_struct, s2f->fields, s2f->num_fields);
}
}

void settings_save(void) {
for(unsigned i = 0; i < sizeof(struct_to_fields) / sizeof(struct_to_field); i++) {
for(unsigned i = 0; i < N_ELEMENTS(struct_to_fields); i++) {
const struct_to_field *s2f = &struct_to_fields[i];
settings_save_fields(s2f->_struct, s2f->fields, s2f->num_fields);
}
Expand All @@ -301,7 +302,7 @@ void settings_save(void) {
}

void settings_free(void) {
for(unsigned i = 0; i < sizeof(struct_to_fields) / sizeof(struct_to_field); i++) {
for(unsigned i = 0; i < N_ELEMENTS(struct_to_fields); i++) {
const struct_to_field *s2f = &struct_to_fields[i];
settings_free_strings(s2f->_struct, s2f->fields, s2f->num_fields);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "resources/pathmanager.h"
#include "resources/sgmanager.h"
#include "utils/allocator.h"
#include "utils/c_array_util.h"
#include "utils/c_string_util.h"
#include "utils/log.h"
#include "utils/msgbox.h"
Expand Down Expand Up @@ -258,7 +259,7 @@ int main(int argc, char *argv[]) {
if(trace_file) {
omf_free(trace_file);
}
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
arg_freetable(argtable, N_ELEMENTS(argtable));
pm_free();
return ret;
}
3 changes: 2 additions & 1 deletion src/resources/languages.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "game/utils/settings.h"
#include "resources/pathmanager.h"
#include "utils/allocator.h"
#include "utils/c_array_util.h"
#include "utils/log.h"
#include "utils/str.h"
#include <string.h>
Expand Down Expand Up @@ -38,7 +39,7 @@ bool lang_init(void) {
// OMF 2.1 added netplay, and with it 23 new localization strings
unsigned new_ids[] = {149, 150, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
182, 183, 184, 185, 267, 269, 270, 271, 284, 295, 305};
unsigned *new_ids_end = new_ids + sizeof(new_ids) / sizeof(new_ids[0]);
unsigned *new_ids_end = new_ids + N_ELEMENTS(new_ids);

// insert dummy entries
sd_lang_string *expanded_strings = omf_malloc(LANG_STR_COUNT * sizeof(sd_lang_string));
Expand Down
6 changes: 6 additions & 0 deletions src/utils/c_array_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef C_ARRAY_UTIL_H
#define C_ARRAY_UTIL_H

#define N_ELEMENTS(array) (sizeof(array) / sizeof((array)[0]))

#endif // C_ARRAY_UTIL_H
4 changes: 2 additions & 2 deletions src/utils/cp437.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cp437.h"
#include "utils/c_array_util.h"
#include <assert.h>

char const *cp437_result_to_string(cp437_result result) {
Expand Down Expand Up @@ -40,8 +41,7 @@ char32_t const cp437_toutf32_lookup[] = {
// clang-format on
};

static_assert(256 == (sizeof cp437_toutf32_lookup) / sizeof cp437_toutf32_lookup[0],
"cp437 lookup table must be 256 entries long");
static_assert(256 == N_ELEMENTS(cp437_toutf32_lookup), "cp437 lookup table must be 256 entries long");

inline static size_t code_utf8len(char32_t utf32) {
if(utf32 <= 0x7F) {
Expand Down
3 changes: 2 additions & 1 deletion src/video/video.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <SDL.h>

#include "utils/c_array_util.h"
#include "utils/log.h"
#include "video/renderers/renderer.h"
#include "video/video.h"
Expand All @@ -26,7 +27,7 @@ static renderer_init all_renderers[] = {
null_renderer_set_callbacks,
#endif
};
static int all_renderers_count = sizeof(all_renderers) / sizeof(renderer_init);
static int all_renderers_count = N_ELEMENTS(all_renderers);

// All renderers that are available. This is filled by video_scan_renderers().
static struct available_renderer {
Expand Down
3 changes: 2 additions & 1 deletion testing/misc/parser_test_strings.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "parser_test_strings.h"
#include "utils/c_array_util.h"
#include <assert.h>

const char *test_strings[] = {
Expand Down Expand Up @@ -2784,4 +2785,4 @@ const char *test_strings[] = {
"A1-B1-C1-D1-E1-F1-G1-H1",
};

static_assert(sizeof(test_strings) / sizeof(test_strings[0]) == TEST_STRING_COUNT, "array size mismatch");
static_assert(N_ELEMENTS(test_strings) == TEST_STRING_COUNT, "array size mismatch");
3 changes: 2 additions & 1 deletion tools/afdiff/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "formats/af.h"
#include "formats/error.h"
#include "utils/c_array_util.h"

#define VNAME(n) a->n, b->n

Expand Down Expand Up @@ -224,6 +225,6 @@ int main(int argc, char *argv[]) {
exit_1:
sd_af_free(&af_a);
exit_0:
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
arg_freetable(argtable, N_ELEMENTS(argtable));
return 0;
}
Loading
Loading