Skip to content

Commit

Permalink
resource mgr, loaders + std wave loader (improved)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtiapko committed Jan 13, 2019
1 parent 41b61af commit 6cea334
Show file tree
Hide file tree
Showing 26 changed files with 581 additions and 271 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CC_DEFINES :=
LD_FLAGS :=
LD_LIBS := -lEGL -lX11 -lGLEW -lGL -lopenal -lsoil2

SRC_DIR := src src/fs src/sys src/util src/allocs src/graph src/audio src/audio/fmts src/math
SRC_DIR := src src/fs src/sys src/sys/loaders src/util src/allocs src/graph src/audio src/math
OBJ_DIR := obj

SRC := $(wildcard $(addsuffix /*.cpp, $(SRC_DIR)))
Expand Down
2 changes: 1 addition & 1 deletion include/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#define DK_ASSERT(expr, ...) \
if (!(expr)) { \
DK_LOG_PRINT(LIGHT_RED, "ASSERT!", __FILE__, __func__, __LINE__, \
DK_LOG_TITLE(LIGHT_RED, "ASSERT!", __FILE__, __func__, __LINE__, \
"Assertion failed: '", #expr, "'", ##__VA_ARGS__, '\n'); \
std::exit(1); \
}
Expand Down
3 changes: 2 additions & 1 deletion include/audio/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class sound final: public resource
sound() noexcept;
~sound() noexcept override;

resource_type type() const noexcept override { return resource_type::SOUND; }

ALuint id() const noexcept { return m_id; }

status create(string_view file_path, sound_data_fmt fmt = sound_data_fmt::AUTO) noexcept;
status create(const sound_data& data) noexcept;
void destroy() noexcept;
};
Expand Down
23 changes: 11 additions & 12 deletions include/audio/sound_data.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
#ifndef __DK_AUDIO_SOUND_DATA_H__
#define __DK_AUDIO_SOUND_DATA_H__

#include "status.h"
#include "resource.h"
#include "containers/string_view.h"

namespace dk::audio
{

enum class sound_data_fmt
{
AUTO = 0,
WAVE
};

class sound_data final: public resource
{
private:
friend class resource_manager;

uint8_t* m_data;
size_t m_size;
uint32_t m_num_channels;
uint32_t m_sample_rate;
uint32_t m_bits_per_sample;

status load_wave(string_view file_path) noexcept;

public:
sound_data() noexcept;
~sound_data() noexcept override;

const uint8_t* data() const noexcept { return m_data; }
resource_type type() const noexcept override { return resource_type::SOUND_DATA; }

uint8_t* data() const noexcept { return m_data; }
size_t size() const noexcept { return m_size; }
uint32_t num_channels() const noexcept { return m_num_channels; }
uint32_t sample_rate() const noexcept { return m_sample_rate; }
uint32_t bits_per_sample() const noexcept { return m_bits_per_sample; }

status create(string_view file_path, sound_data_fmt fmt = sound_data_fmt::AUTO) noexcept;
void set_data(uint8_t* data) noexcept { m_data = data; }
void set_size(size_t size) noexcept { m_size = size; }
void set_num_channels(uint32_t num_channels) noexcept { m_num_channels = num_channels; }
void set_sample_rate(uint32_t sample_rate) noexcept { m_sample_rate = sample_rate; }
void set_bits_per_sample(uint32_t bits_per_sample) noexcept { m_bits_per_sample = bits_per_sample; }

void destroy() noexcept;
};

}
Expand Down
14 changes: 14 additions & 0 deletions include/containers/pair.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __DK_PAIR_H__
#define __DK_PAIR_H__

#include <utility>

namespace dk
{

template<typename FirstT, typename SecondT>
using pair = std::pair<FirstT, SecondT>;

}

#endif // !__DK_PAIR_H__
2 changes: 2 additions & 0 deletions include/graph/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class shader final: public resource
shader() noexcept;
~shader() noexcept override;

resource_type type() const noexcept override { return resource_type::SHADER; }

GLuint id() const noexcept { return m_id; }

status create(string_view file_path, shader_type type) noexcept;
Expand Down
2 changes: 2 additions & 0 deletions include/graph/shader_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class shader_data final: public resource
shader_data() noexcept = default;
~shader_data() noexcept override = default;

resource_type type() const noexcept override { return resource_type::SHADER_DATA; }

const string& data() const noexcept { return m_data; }
string_view file_path() const noexcept { return m_file_path; }

Expand Down
3 changes: 2 additions & 1 deletion include/graph/shader_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
namespace dk::graph
{

class shader_program : public resource
class shader_program final: public resource
{
private:
GLuint m_id;

public:
shader_program() noexcept;
~shader_program() noexcept override;

void enable() const noexcept;
Expand Down
12 changes: 7 additions & 5 deletions include/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "status.h"
#include "util/term.h"

#define DK_LOG_PRINT(...) dk::log::print(__VA_ARGS__, '\n')

#define DK_LOG_COLOR(color, ...) \
dk::util::term_text_color::color, dk::util::term_text_attrib::BOLD, \
__VA_ARGS__, \
Expand All @@ -16,13 +18,13 @@
"[", DK_LOG_COLOR(color, title), "] ", \
DK_LOG_COLOR(GRAY, dk::log_timestamp{}, " - ", file, "::", func, " (", line, ") - ")

#define DK_LOG_PRINT(color, title, file, func, line, ...) \
#define DK_LOG_TITLE(color, title, file, func, line, ...) \
dk::log::print(DK_LOG_HEADER(color, title, file, func, line), __VA_ARGS__)

#define DK_LOG_IMPL(file, func, line, ...) DK_LOG_PRINT(LIGHT_BLUE, " MSG ", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_OK_IMPL(file, func, line, ...) DK_LOG_PRINT(LIGHT_GREEN, " OK! ", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_WARNING_IMPL(file, func, line, ...) DK_LOG_PRINT(LIGHT_YELLOW, "WARNING", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_ERROR_IMPL(file, func, line, ...) DK_LOG_PRINT(LIGHT_RED, " ERROR ", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_IMPL(file, func, line, ...) DK_LOG_TITLE(LIGHT_BLUE, " MSG ", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_OK_IMPL(file, func, line, ...) DK_LOG_TITLE(LIGHT_GREEN, " OK! ", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_WARNING_IMPL(file, func, line, ...) DK_LOG_TITLE(LIGHT_YELLOW, "WARNING", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_ERROR_IMPL(file, func, line, ...) DK_LOG_TITLE(LIGHT_RED, " ERROR ", file, func, line, __VA_ARGS__, '\n')

#define DK_LOG(...) DK_LOG_IMPL(__FILE__, __func__, __LINE__, __VA_ARGS__)
#define DK_LOG_OK(...) DK_LOG_OK_IMPL(__FILE__, __func__, __LINE__, __VA_ARGS__)
Expand Down
2 changes: 1 addition & 1 deletion include/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define mem_dealloc(ptr) mem::dealloc(__FILE__, __func__, __LINE__, ptr)
#define mem_realloc(ptr, size) mem::realloc(__FILE__, __func__, __LINE__, ptr, size)

#define mem_create(T, ...) mem::create<T>(__FILE__, __func__, __LINE__, __VA_ARGS__)
#define mem_create(T, ...) mem::create<T>(__FILE__, __func__, __LINE__ __VA_OPT__(,) __VA_ARGS__)
#define mem_destroy(ptr) mem::destroy(__FILE__, __func__, __LINE__, ptr)

namespace dk
Expand Down
16 changes: 10 additions & 6 deletions include/resource.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#ifndef __DK_RESOURCE_H__
#define __DK_RESOURCE_H__

#include "status.h"

namespace dk
{

enum class resource_type
{
AUTO = 0,
UNKNOWN,
AUDIO_DATA,
UNKNOWN = 0,
SOUND_DATA,
SOUND,
SHADER_DATA,
SHADER,
TEXTURE_DATA,
TEXTURE,
MESH_DATA, // TODO: is it possible (is it needed) to load only mesh?
MODEL_DATA
MODEL_DATA,
ENUM_SIZE
};

class resource
{
public:
virtual ~resource() noexcept = default;

virtual resource_type type() const noexcept { return resource_type::UNKNOWN; }
};

}
Expand Down
42 changes: 42 additions & 0 deletions include/resource_group.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef __DK_RESOURCE_GROUP_H__
#define __DK_RESOURCE_GROUP_H__

#include "resource.h"
#include "containers/vector.h"
#include "containers/hash_table.h"
#include "containers/string_view.h"

namespace dk
{

class resource_group final: public resource
{
private:
hash_table<string_view, resource*> m_res_table;
vector<resource*> m_resources;

public:
~resource_group() noexcept override;

resource* operator[](string_view tag) const noexcept;

bool is_exists(const resource* res) const noexcept;

void add(resource* res) noexcept;
void add(resource* res, string_view tag) noexcept;

bool try_add(resource* res) noexcept;
bool try_add(resource* res, string_view tag) noexcept;

void remove(resource* res) noexcept;
void remove(string_view tag) noexcept;
void remove_all() noexcept;

void destroy(resource* res) noexcept;
void destroy(string_view tag) noexcept;
void destroy_all() noexcept;
};

}

#endif // !__DK_RESOURCE_GROUP_H__
1 change: 1 addition & 0 deletions include/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class status
static constexpr int ERROR = 2;
static constexpr int FATAL = 3;

constexpr status() noexcept : m_val(OK) {}
constexpr status(int val) noexcept : m_val(val) {}

constexpr /* explicit */ operator int() const noexcept { return m_val; }
Expand Down
78 changes: 78 additions & 0 deletions include/sys/loaders/wave_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef __DK_SYS_WAVE_DATA_LOADER_H__
#define __DK_SYS_WAVE_DATA_LOADER_H__

#include "audio/sound_data.h"
#include "sys/resource_loader.h"

#define WAVE_CHUNK_ID(id) (id[0] + (id[1] << 8) + (id[2] << 16) + (id[3] << 24))

namespace dk::sys
{

struct wave_header
{
struct chunk_header
{
uint32_t id;
uint32_t size;
};

struct RIFF_chunk
{
static constexpr uint32_t id = WAVE_CHUNK_ID("RIFF"); /* 0x46464952 */
static constexpr uint32_t WAVE_format = WAVE_CHUNK_ID("WAVE"); /* 0x45564157 */
uint32_t format;
};

struct LIST_chunk
{
static constexpr uint32_t id = WAVE_CHUNK_ID("LIST"); /* 0x5453494C */
static constexpr uint32_t INFO_type = WAVE_CHUNK_ID("INFO"); /* 0x4F464E49 */

/* sub chunk ids */
static constexpr uint32_t artist_info = WAVE_CHUNK_ID("IART");
static constexpr uint32_t comments_info = WAVE_CHUNK_ID("ICMT");
static constexpr uint32_t copyright_info = WAVE_CHUNK_ID("ICOP");
static constexpr uint32_t creation_date_info = WAVE_CHUNK_ID("ICRD");
static constexpr uint32_t name_info = WAVE_CHUNK_ID("INAM");
static constexpr uint32_t product_info = WAVE_CHUNK_ID("IPRD");
static constexpr uint32_t software_info = WAVE_CHUNK_ID("ISFT");

uint32_t type;
};

struct fmt_chunk
{
static constexpr uint32_t id = WAVE_CHUNK_ID("fmt "); /* 0x20746D66 */

uint16_t audio_fmt; /* PCM = 1 */
uint16_t num_channels; /* mono = 1, stereo = 2, ... */
uint32_t sample_rate; /* 8000, 44100, ... */
uint32_t byte_rate; /* sample_rate * num_channels * bits_per_sample / 8 */
uint16_t block_align; /* num_channels * bits_per_sample / 8 */
uint16_t bits_per_sample; /* 8, 16, ... */
};

struct data_chunk
{
/* size = num_samples (yeah, samples) * num_channels * bits_per_sample / 8 */
static constexpr uint32_t id = WAVE_CHUNK_ID("data"); /* 0x61746164 */
};

static size_t align_chunk(size_t size) noexcept
{
return size + (size & 1); /* every chunk must me aligned by 16-bit boundaries */
}
};

class wave_loader final: public resource_loader
{
public:
status load(string_view file_path, audio::sound_data& res) noexcept;

resource* load(string_view file_path, resource_type type) noexcept override;
};

}

#endif // !__DK_SYS_WAVE_DATA_LOADER_H__
8 changes: 2 additions & 6 deletions include/sys/resource_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ namespace dk::sys

class resource_loader
{
private:
static hash_table<string, > s_loaders;

public:
static resource_type get_type(string_view file_path) noexcept;
static resource* load(string_view file_path, resource_type type = resource_type::AUTO) noexcept;
virtual ~resource_loader() noexcept = default;

static status load(string_view file_path, audio::sound_data& data) noexcept;
virtual resource* load(string_view file_path, resource_type type) noexcept = 0;
};

}
Expand Down
28 changes: 15 additions & 13 deletions include/sys/resource_manager.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
#ifndef __DK_SYS_RESOURCE_MANAGER_H__
#define __DK_SYS_RESOURCE_MANAGER_H__

#include "resource.h"
#include "containers/vector.h"
#include "sys/resource_loader.h"
#include "containers/hash_table.h"
#include "containers/pair.h"

namespace dk::sys
{

class resource_manager
{
public:
vector<resource*> m_resources;
private:
static hash_table<string_view, resource_loader*> s_loaders;

public:
resource_manager() noexcept = default;
~resource_manager() noexcept;

bool is_exists(const resource* res) const noexcept;
template<typename T>
static T* load(string_view file_path) noexcept
{
return static_cast<T*>(load(file_path, T{}.type()));
}

void add(resource* res) noexcept;
static resource* load(string_view file_path, resource_type type) noexcept;

void remove(resource* res) noexcept;
void remove_all() noexcept;
static string_view mime(string_view file_path) noexcept;
static resource_loader* loader(string_view mime) noexcept;

void destroy(resource* res) noexcept;
void destroy_all() noexcept;
static void add(resource_loader* loader, string_view mime) noexcept;
static void remove(string_view mime) noexcept;
};

}
Expand Down
Loading

0 comments on commit 6cea334

Please sign in to comment.