summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-08-19 14:59:47 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-08-19 15:14:12 (EDT)
commit1cf6751f42be611dbd0545d91a645c23bb203ee8 (patch)
treea7381670a7b1f6a8fdf7f1402e802c8950668c38 /src
parent904ab84cebdda512c553855d9268af5e73fcfcfb (diff)
downloadmazefight-1cf6751f42be611dbd0545d91a645c23bb203ee8.zip
mazefight-1cf6751f42be611dbd0545d91a645c23bb203ee8.tar.gz
mazefight-1cf6751f42be611dbd0545d91a645c23bb203ee8.tar.bz2
game: Separate player struct from enemies array
Diffstat (limited to 'src')
-rw-r--r--src/game.c32
1 files changed, 19 insertions, 13 deletions
diff --git a/src/game.c b/src/game.c
index 0c94202..702397f 100644
--- a/src/game.c
+++ b/src/game.c
@@ -144,6 +144,7 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
SDL_Renderer *renderer)
{
struct mf_maze *maze = NULL;
+ struct mf_char *player = NULL;
struct mf_char **chars = NULL;
int i;
SDL_Color maze_color;
@@ -172,18 +173,18 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
maze_color.b = MF_COLOR_MAZE_B;
maze_color.a = MF_COLOR_MAZE_A;
- chars = calloc(1 + enemies, sizeof(*chars));
+ chars = calloc(enemies, sizeof(*chars));
if (chars == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't allocate characters: %s",
strerror(errno));
goto err;
}
- chars[0] = mf_player_new(maze, MF_WINDOW_H / size);
- if (chars[0] == NULL) {
+ player = mf_player_new(maze, MF_WINDOW_H / size);
+ if (player == NULL) {
goto err;
}
- for (i = 1; i < 1 + enemies; ++i) {
+ for (i = 0; i < enemies; ++i) {
chars[i] = mf_enemy_new(maze, MF_WINDOW_H / size, size);
if (chars[i] == NULL) {
goto err;
@@ -233,7 +234,7 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
default:
break;
}
- mf_player_key_event(chars[0], &event);
+ mf_player_key_event(player, &event);
switch (mftk_window_event(win, &event)) {
case 0:
break;
@@ -244,16 +245,17 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
}
}
if (won == SDL_FALSE) {
- for (i = 0; i < 1 + enemies; ++i) {
+ mf_char_update(player);
+ for (i = 0; i < enemies; ++i) {
mf_char_update(chars[i]);
}
- for (i = 1; i < 1 + enemies; ++i) {
- for (j = i + 1; j < 1 + enemies; ++j) {
+ for (i = 0; i < enemies; ++i) {
+ for (j = i + 1; j < enemies; ++j) {
mf_char_collision(chars[i], chars[j],
SDL_TRUE);
}
}
- mf_char_get_vector(chars[0], &game.player_cx,
+ mf_char_get_vector(player, &game.player_cx,
&game.player_cy, &game.player_travel,
&game.player_dx, &game.player_dy);
game.player_x = game.player_cx * MF_WINDOW_H / size;
@@ -269,7 +271,8 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
MF_COLOR_BACK_R, MF_COLOR_BACK_G,
MF_COLOR_BACK_B, MF_COLOR_BACK_A);
SDL_RenderClear(renderer);
- for (i = 0; i < 1 + enemies; ++i) {
+ mf_char_render(player, renderer);
+ for (i = 0; i < enemies; ++i) {
mf_char_render(chars[i], renderer);
}
if (fow == SDL_TRUE && _mf_game_fow(renderer, &game, maze,
@@ -309,8 +312,9 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
TTF_CloseFont(text_font);
text_font = NULL;
mf_maze_destroy(&maze);
+ mf_char_destroy(&player);
if (chars != NULL) {
- for (i = 0; i < 1 + enemies; ++i) {
+ for (i = 0; i < enemies; ++i) {
mf_char_destroy(&chars[i]);
}
free(chars);
@@ -322,8 +326,9 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
TTF_CloseFont(text_font);
text_font = NULL;
mf_maze_destroy(&maze);
+ mf_char_destroy(&player);
if (chars != NULL) {
- for (i = 0; i < 1 + enemies; ++i) {
+ for (i = 0; i < enemies; ++i) {
mf_char_destroy(&chars[i]);
}
free(chars);
@@ -339,8 +344,9 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
}
mftk_window_destroy(&win);
mf_maze_destroy(&maze);
+ mf_char_destroy(&player);
if (chars != NULL) {
- for (i = 0; i < 1 + enemies; ++i) {
+ for (i = 0; i < enemies; ++i) {
mf_char_destroy(&chars[i]);
}
free(chars);