Skip to content

Commit

Permalink
Added audios to the SKAP file (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Jun 19, 2024
1 parent 1ca25c8 commit 5c9ea8c
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 9 deletions.
53 changes: 53 additions & 0 deletions tools/skap/include/skap_idx_audio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

#include <raylib.h>
#include <skap_defines.h>

#define SKAP_IDX_AUDIO_MD_NAME_MAX_LEN 512

typedef struct {
char name[SKAP_IDX_AUDIO_MD_NAME_MAX_LEN];
u32 frame_count;
u32 sample_rate;
u32 sample_size;
u32 channels;
} skap_idx_audio_md;

typedef struct {
skap_idx_audio_md metadata;
usz blob_offset;
usz blob_size;
} skap_idx_audio;

void skap_idx_audio_loadall(Wave *audios, const char **audio_paths, usz size);

void skap_idx_audio_unloadall(Wave *audios, usz size);

skap_idx_audio skap_idx_audio_create(const char *name, Wave *audio);

u8 skap_idx_audio_append(FILE *fd, skap_idx_audio *i);

void skap_idx_audio_link_blob(skap_idx_audio *i, usz blob_offset, usz blob_size);

u8 skap_idx_audio_blob_append(FILE *fd, const char *name, Wave *audio);
32 changes: 31 additions & 1 deletion tools/skap/src/skap.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#include <skap_file.h>
#include <skap_header.h>
#include <skap_idx_image.h>
#include <skap_idx_audio.h>

#define IMG_COUNT SKAP_ARRLEN(img_paths)
#define IMG_COUNT SKAP_ARRLEN(img_paths)
#define AUDIO_COUNT SKAP_ARRLEN(audio_paths)

static const char *img_paths[] = {
"assets/icon.png",
Expand All @@ -35,10 +37,24 @@ static Image imgs[IMG_COUNT] = {0};
static skap_idx_image img_idxs[IMG_COUNT] = {0};
static usz img_idx_locs[IMG_COUNT] = {0};

static const char *audio_paths[] = {
"assets/music/menu.mp3",
"assets/sounds/7mm/equip.wav",
"assets/sounds/7mm/reload.wav",
"assets/sounds/7mm/shoot.wav",
"assets/sounds/akm/equip.wav",
"assets/sounds/akm/reload.wav",
"assets/sounds/akm/shoot.wav"
};
static Wave audios[AUDIO_COUNT] = {0};
static skap_idx_audio audio_idxs[AUDIO_COUNT] = {0};
static usz audio_idx_locs[AUDIO_COUNT] = {0};

int main(void) {
int result = 0;
SetTraceLogLevel(LOG_WARNING);
skap_idx_image_loadall(imgs, img_paths, IMG_COUNT);
skap_idx_audio_loadall(audios, audio_paths, AUDIO_COUNT);
FILE *fd = skap_file_create();
skap_header header = skap_header_create(IMG_COUNT);
if (!skap_header_append(fd, &header)) skap_return_defer(1);
Expand All @@ -47,6 +63,11 @@ int main(void) {
img_idx_locs[i] = ftell(fd);
if (!skap_idx_image_append(fd, &img_idxs[i])) skap_return_defer(1);
}
for (usz i = 0; i < AUDIO_COUNT; ++i) {
audio_idxs[i] = skap_idx_audio_create(audio_paths[i], &audios[i]);
audio_idx_locs[i] = ftell(fd);
if (!skap_idx_audio_append(fd, &audio_idxs[i])) skap_return_defer(1);
}
for (usz i = 0; i < IMG_COUNT; ++i) {
usz blob_loc = ftell(fd);
skap_idx_image_link_blob(&img_idxs[i], blob_loc, (usz) imgs[i].width * imgs[i].height);
Expand All @@ -55,8 +76,17 @@ int main(void) {
fseek(fd, blob_loc, SEEK_SET);
if (!skap_idx_image_blob_append(fd, img_paths[i], &imgs[i])) skap_return_defer(1);
}
for (usz i = 0; i < AUDIO_COUNT; ++i) {
usz blob_loc = ftell(fd);
skap_idx_audio_link_blob(&audio_idxs[i], blob_loc, (usz) audios[i].frameCount * audios[i].channels * (audios[i].sampleSize / 8));
fseek(fd, audio_idx_locs[i], SEEK_SET);
if (!skap_idx_audio_append(fd, &audio_idxs[i])) skap_return_defer(1);
fseek(fd, blob_loc, SEEK_SET);
if (!skap_idx_audio_blob_append(fd, audio_paths[i], &audios[i])) skap_return_defer(1);
}
defer:
skap_file_destroy(fd);
skap_idx_audio_unloadall(audios, AUDIO_COUNT);
skap_idx_image_unloadall(imgs, IMG_COUNT);
return result;
}
93 changes: 93 additions & 0 deletions tools/skap/src/skap_idx_audio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <skap_idx_audio.h>

void skap_idx_audio_loadall(Wave *audios, const char **audio_paths, usz size) {
InitAudioDevice();
assert(IsAudioDeviceReady());
for (usz i = 0; i < size; ++i) {
printf(" LOAD %s\n", audio_paths[i]);
audios[i] = LoadWave(audio_paths[i]);
assert(IsWaveReady(audios[i]));
}
}

void skap_idx_audio_unloadall(Wave *audios, usz size) {
for (usz i = 0; i < size; ++i) UnloadWave(audios[i]);
CloseAudioDevice();
}

skap_idx_audio skap_idx_audio_create(const char *name, Wave *audio) {
skap_idx_audio i = {
.metadata = (skap_idx_audio_md) {
.frame_count = audio->frameCount,
.sample_rate = audio->sampleRate,
.sample_size = audio->sampleSize,
.channels = audio->channels
},
.blob_offset = 0,
.blob_size = 0
};
memset(i.metadata.name, 0, sizeof(i.metadata.name));
strncpy(i.metadata.name, name, sizeof(i.metadata.name) - 1);
return i;
}

u8 skap_idx_audio_append(FILE *fd, skap_idx_audio *i) {
if (!fd || !i) {
fprintf(stderr, "ERROR: skap_idx_audio_append :: `fd` and `i` need to be valid pointers\n");
return 0;
}
printf(" WRITE skap_idx_audio(%s) >> " SKAP_FILENAME "\n", i->metadata.name);
if (fwrite(i, sizeof(skap_idx_audio), 1, fd) != 1) {
fprintf(stderr, "ERROR: skap_idx_audio_append :: unable to write to file\n");
return 0;
}
return 1;
}

void skap_idx_audio_link_blob(skap_idx_audio *i, usz blob_offset, usz blob_size) {
if (!i || !blob_offset || !blob_size) {
printf("WARNING: skap_idx_audio_link_blob :: args need to be valid, skipping linkage\n");
return;
}
printf(" LINK skap_idx_audio(%s) -> {%zu, %zu}\n", i->metadata.name, blob_offset, blob_size);
i->blob_offset = blob_offset;
i->blob_size = blob_size;
}

u8 skap_idx_audio_blob_append(FILE *fd, const char *name, Wave *audio) {
if (!fd || !audio) {
fprintf(stderr, "ERROR: skap_idx_audio_blob_append :: `fd` and `audio` need to be valid pointers\n");
return 0;
}
printf(" WRITE %s >> " SKAP_FILENAME "\n", name);
size_t audio_len = audio->frameCount * audio->channels * (audio->sampleSize / 8);
if (fwrite(audio->data, sizeof(u8), audio_len, fd) != audio_len) {
fprintf(stderr, "ERROR: skap_idx_audio_blob_append :: unable to write to file\n");
return 0;
}
return 1;
}
14 changes: 6 additions & 8 deletions tools/skap/src/skap_idx_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,19 @@ void skap_idx_image_loadall(Image *imgs, const char **img_paths, usz size) {
}

void skap_idx_image_unloadall(Image *imgs, usz size) {
for (usz i = 0; i < size; ++i) {
UnloadImage(imgs[i]);
}
for (usz i = 0; i < size; ++i) UnloadImage(imgs[i]);
}

skap_idx_image skap_idx_image_create(const char *name, Image *img) {
skap_idx_image i = {
.metadata = (skap_idx_image_md) {
.width = img->width,
.height = img->height,
.width = img->width,
.height = img->height,
.mipmaps = img->mipmaps,
.format = img->format
.format = img->format
},
.blob_offset = 0,
.blob_size = 0
.blob_size = 0
};
memset(i.metadata.name, 0, sizeof(i.metadata.name));
strncpy(i.metadata.name, name, sizeof(i.metadata.name) - 1);
Expand All @@ -73,7 +71,7 @@ void skap_idx_image_link_blob(skap_idx_image *i, usz blob_offset, usz blob_size)
}
printf(" LINK skap_idx_image(%s) -> {%zu, %zu}\n", i->metadata.name, blob_offset, blob_size);
i->blob_offset = blob_offset;
i->blob_size = blob_size;
i->blob_size = blob_size;
}

u8 skap_idx_image_blob_append(FILE *fd, const char *name, Image *img) {
Expand Down

0 comments on commit 5c9ea8c

Please sign in to comment.