now: with audio

This commit is contained in:
Quinten Mennen 2025-06-11 19:20:51 +02:00
parent 280d1ac498
commit f60fb4c1b6
23 changed files with 93715 additions and 26 deletions

View File

@ -6,13 +6,13 @@
# By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/06/11 17:41:17 by qmennen ### ########.fr #
# Updated: 2025/06/11 18:43:00 by qmennen ### ########.fr #
# #
# **************************************************************************** #
NAME = cub3D
FLAGS="-DFULLSCREEN=1"
FLAGS="-DFULLSCREEN=0"
DIST_PATH = dist
SRC_PATH = src

BIN
assets/sounds/damage.wav Normal file

Binary file not shown.

BIN
assets/sounds/death.wav Normal file

Binary file not shown.

BIN
assets/sounds/enemy.mp3 Normal file

Binary file not shown.

BIN
assets/sounds/flash.wav Normal file

Binary file not shown.

BIN
assets/sounds/hum.wav Normal file

Binary file not shown.

BIN
assets/sounds/noise.wav Normal file

Binary file not shown.

BIN
assets/sounds/pickup.wav Normal file

Binary file not shown.

BIN
assets/sounds/walk.wav Normal file

Binary file not shown.

39
inc/audio.h Normal file
View File

@ -0,0 +1,39 @@
#include "miniaudio.h"
#define SND_SIZE 8
#define SUCCESS 1
#define FAILURE 0
typedef enum s_sound {
SND_WALK,
SND_FLASH,
SND_DAMAGE,
SND_DEATH,
SND_ENEMY,
SND_PICKUP,
SND_HUM,
SND_NOISE
} t_sound;
typedef struct s_audio {
ma_engine engine; // The audio engine instance
ma_sound sounds[SND_SIZE]; // Array of sounds
char play_queue[SND_SIZE]; // Queue for playing sounds
char stop_queue[SND_SIZE]; // Queue for stopping sounds
char is_playing[SND_SIZE]; // Array to check if sounds are playing
} t_audio;
t_audio *audio_init(void);
int audio_load_sounds(t_audio *audio);
int audio_stop(t_audio *audio, t_sound sound);
int audio_is_playing(t_audio *audio, t_sound sound);
int audio_loop(t_audio *audio, t_sound sound, int loop);
int audio_handle_queues(t_audio *audio);
void audio_uninit(t_audio *audio);
// typedef struct s_audio {
// ma_engine_config config; // Configuration for the audio engine
// ma_engine engine; // The audio engine instance
// } t_audio;

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/06/11 16:29:00 by qmennen ### ########.fr */
/* Updated: 2025/06/11 18:40:00 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -58,6 +58,7 @@
# define MENU_OPTION_EXIT 2
# include "MLX42.h"
# include "audio.h"
# include "allowed.h"
# include "libft.h"
# include "types.h"

93468
inc/miniaudio.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
/* Updated: 2025/06/11 17:49:35 by qmennen ### ########.fr */
/* Updated: 2025/06/11 18:38:36 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -181,6 +181,7 @@ typedef struct s_game
t_screen *screen;
t_keyboard *keyboard;
t_scoreboard *scoreboard;
t_audio *audio;
mlx_image_t *screenshots[MAX_SCREENSHOTS];
struct s_game_manager *manager;
int fps;

145
src/audio/audio.c Normal file
View File

@ -0,0 +1,145 @@
#include "audio.h"
#include "libft.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
t_audio *audio_init(void)
{
t_audio *audio;
ma_engine_config config;
audio = (t_audio *)malloc(sizeof(t_audio));
if (!audio)
return (NULL);
memset(audio, 0, sizeof(t_audio));
config = ma_engine_config_init();
// config.pUserData = audio;
// config.pLogCallback = NULL;
// config.logLevel = MA_LOG_LEVEL_INFO;
if (ma_engine_init(&config, &audio->engine) != MA_SUCCESS)
{
free(audio);
return (NULL);
}
return (audio);
}
void audio_uninit(t_audio *audio)
{
int i;
if (!audio)
return ;
i = 0;
while (i < SND_SIZE)
{
if (&audio->sounds[i])
{
ma_sound_uninit(&audio->sounds[i]);
}
i++;
}
ma_engine_uninit(&audio->engine);
free(audio);
}
int audio_load_sounds(t_audio *audio)
{
int i;
const char *sound_files[SND_SIZE] = {
"assets/sounds/walk.wav",
"assets/sounds/flash.wav",
"assets/sounds/damage.wav",
"assets/sounds/death.wav",
"assets/sounds/enemy.mp3",
"assets/sounds/pickup.wav",
"assets/sounds/hum.wav",
"assets/sounds/noise.wav"};
if (!audio)
return (FAILURE);
i = 0;
while (i < SND_SIZE)
{
printf("Loading sound: %s\n", sound_files[i]);
if (ma_sound_init_from_file(&audio->engine, sound_files[i],
0, NULL, NULL, &audio->sounds[i]) != MA_SUCCESS)
{
audio_uninit(audio);
return (FAILURE);
}
if (i == SND_WALK || i == SND_HUM || i == SND_NOISE)
ma_sound_set_looping(&audio->sounds[i], MA_TRUE);
else
ma_sound_set_looping(&audio->sounds[i], MA_FALSE);
i++;
}
ma_sound_set_volume(&audio->sounds[SND_HUM], 0.5f);
return (SUCCESS);
}
int audio_play(t_audio *audio, t_sound sound)
{
if (!audio || sound < 0 || sound >= SND_SIZE || !&audio->sounds[sound])
return (FAILURE);
ma_sound_seek_to_pcm_frame(&audio->sounds[sound], 0);
if (ma_sound_start(&audio->sounds[sound]) != MA_SUCCESS)
return (FAILURE);
return (SUCCESS);
}
int audio_stop(t_audio *audio, t_sound sound)
{
if (!audio || sound < 0 || sound >= SND_SIZE || !&audio->sounds[sound])
return (FAILURE);
ma_sound_seek_to_pcm_frame(&audio->sounds[sound], 0);
if (ma_sound_stop(&audio->sounds[sound]) != MA_SUCCESS)
return (FAILURE);
return (SUCCESS);
}
int audio_is_playing(t_audio *audio, t_sound sound)
{
if (!audio || sound < 0 || sound >= SND_SIZE || !&audio->sounds[sound])
return (FAILURE);
if (ma_sound_is_playing(&audio->sounds[sound]))
return (SUCCESS);
return (FAILURE);
}
int audio_loop(t_audio *audio, t_sound sound, int loop)
{
if (!audio || sound < 0 || sound >= SND_SIZE || !&audio->sounds[sound])
return (FAILURE);
ma_sound_set_looping(&audio->sounds[sound], loop);
return (SUCCESS);
}
int audio_handle_queues(t_audio *audio)
{
int i;
if (!audio)
return (FAILURE);
i = 0;
while (i < SND_SIZE)
{
if (audio->play_queue[i])
{
if (!audio_is_playing(audio, i))
audio_play(audio, i);
audio->play_queue[i] = 0;
}
if (audio->stop_queue[i])
{
audio_stop(audio, i);
audio->stop_queue[i] = 0;
}
audio->is_playing[i] = ma_sound_is_playing(&audio->sounds[i]);
i++;
}
return (SUCCESS);
}

2
src/audio/miniaudio.c Normal file
View File

@ -0,0 +1,2 @@
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* collision.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
/* Updated: 2025/06/10 19:34:32 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* collision.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
/* Updated: 2025/06/11 19:01:00 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@ static void collect(t_game *game, t_sprite *sprite)
player->battery += 0.5f;
if (player->battery > 1.f)
player->battery = 1.f;
game->audio->play_queue[SND_PICKUP] = 1;
sprite->type = SPRITE_TYPE_COLLECTED;
game->scoreboard->collectibles++;
}

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/03 15:53:32 by qmennen #+# #+# */
/* Updated: 2025/06/05 18:41:33 by qmennen ### ########.fr */
/* Updated: 2025/06/11 18:53:19 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,5 +21,6 @@ void handle_flash(t_sprite *sprite, t_game *game)
{
sprite->type = SPRITE_TYPE_DISABLED;
game->scoreboard->enemies++;
game->audio->stop_queue[SND_ENEMY] = 1;
}
}

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/06/11 17:44:59 by qmennen ### ########.fr */
/* Updated: 2025/06/11 19:05:43 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,9 +22,7 @@ int game_create(t_game **game)
(*game)->scoreboard = malloc(sizeof(t_scoreboard));
ft_memset((*game)->scoreboard, 0, sizeof(t_scoreboard));
if (!(*game)->scoreboard)
{
return (free(*game), FAILURE);
}
return (SUCCESS);
}
@ -39,14 +37,37 @@ void game_over(t_game_manager *manager)
game->scoreboard->end_time = mlx_get_time();
manager->state = GAME_STATE_END_SCREEN;
manager->active_menu = &manager->end_screen;
ft_memset(game->audio->stop_queue, 1, SND_SIZE * sizeof(char));
game->audio->stop_queue[SND_DEATH] = 0;
game->audio->play_queue[SND_DEATH] = 1;
audio_handle_queues(game->audio);
set_uniforms(game);
}
static void handle_sound(t_game *game)
{
t_audio *audio;
audio = game->audio;
if (!audio)
return ;
if (game->player->is_moving)
audio->play_queue[SND_WALK] = 1;
else
audio->stop_queue[SND_WALK] = 1;
if (game->player->hit_timer > 0)
audio->play_queue[SND_DAMAGE] = 1;
else
audio->stop_queue[SND_DAMAGE] = 1;
audio_handle_queues(audio);
}
void game_run(t_game *game)
{
static int fps = 0;
fps += (int)(1.f / game->screen->mlx->delta_time);
handle_sound(game);
set_uniforms(game);
player_update(game, game->screen->mlx->delta_time);
cast_rays(game);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* moves.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/05/14 20:08:27 by whaffman #+# #+# */
/* Updated: 2025/06/10 19:38:49 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* moves.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/14 20:08:27 by whaffman #+# #+# */
/* Updated: 2025/06/11 18:53:11 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -95,6 +95,7 @@ void player_update(t_game *game, double delta_time)
{
game->screen->flash = 3;
game->player->battery -= FLASH_BATTERY;
game->audio->play_queue[SND_FLASH] = 1;
interact_door(game);
}
if (game->player->hit_timer > 0)

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
/* Updated: 2025/06/11 14:58:56 by qmennen ### ########.fr */
/* Updated: 2025/06/11 18:42:53 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,6 +23,11 @@ int main(int argc, char **argv)
return (EXIT_FAILURE);
if (!initialize_cub3d(&game, argv[1]))
return (game_free(game), EXIT_FAILURE);
game->audio = audio_init();
if (!game->audio)
return (game_free(game), EXIT_FAILURE);
if (audio_load_sounds(game->audio) == FAILURE)
return (game_free(game), EXIT_FAILURE);
manager = game_manager_create(game, START_STATE);
manager->menu = create_main_menu(manager);
manager->end_screen = create_end_screen(manager);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 13:48:18 by qmennen #+# #+# */
/* Updated: 2025/06/11 15:00:38 by qmennen ### ########.fr */
/* Updated: 2025/06/11 18:35:03 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -64,6 +64,8 @@ void game_manager_destroy(void *param)
menu_free(manager->end_screen, manager->game->screen);
mlx_close_window(manager->game->screen->mlx);
mlx_terminate(manager->game->screen->mlx);
if (manager->game->audio)
audio_uninit(manager->game->audio);
if (manager->game)
game_free(manager->game);
free(manager);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/28 14:14:11 by qmennen #+# #+# */
/* Updated: 2025/06/11 14:55:20 by qmennen ### ########.fr */
/* Updated: 2025/06/11 19:05:55 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,6 +29,7 @@ static void game_start(struct s_menu_item *item, t_game_manager *manager)
game->scoreboard->start_time = mlx_get_time();
menu_toggle(*(manager->active_menu));
manager->state = GAME_STATE_PLAYING;
game->audio->play_queue[SND_HUM] = 1;
}
t_menu *create_main_menu(t_game_manager *manager)

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/28 17:15:52 by qmennen #+# #+# */
/* Updated: 2025/06/11 16:22:09 by qmennen ### ########.fr */
/* Updated: 2025/06/11 18:51:46 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,6 +33,7 @@ void update_monsters(t_game *game)
inv_dist = 1.0 / sqrt(dist_squared);
sprite->pos.x += d.x * inv_dist * .05f;
sprite->pos.y += d.y * inv_dist * .05f;
game->audio->play_queue[SND_ENEMY] = 1;
}
}
}