Skip to content

Commit

Permalink
std wave and texture loaderes, res mgr improved
Browse files Browse the repository at this point in the history
  • Loading branch information
mtiapko committed Jan 13, 2019
1 parent 6cea334 commit 7284482
Show file tree
Hide file tree
Showing 24 changed files with 423 additions and 44 deletions.
3 changes: 2 additions & 1 deletion include/audio/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __DK_AUDIO_SOUND_H__

#include <AL/al.h>
#include "status.h"
#include "audio/sound_data.h"

namespace dk::audio
Expand All @@ -18,7 +19,7 @@ class sound final: public resource
sound() noexcept;
~sound() noexcept override;

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

ALuint id() const noexcept { return m_id; }

Expand Down
6 changes: 3 additions & 3 deletions include/audio/sound_data.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef __DK_AUDIO_SOUND_DATA_H__
#define __DK_AUDIO_SOUND_DATA_H__

#include "status.h"
#include <stdint.h>
#include <stddef.h>
#include "resource.h"
#include "containers/string_view.h"

namespace dk::audio
{
Expand All @@ -21,7 +21,7 @@ class sound_data final: public resource
sound_data() noexcept;
~sound_data() noexcept override;

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

uint8_t* data() const noexcept { return m_data; }
size_t size() const noexcept { return m_size; }
Expand Down
1 change: 1 addition & 0 deletions include/audio/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class source

void play() const noexcept;
void pause() const noexcept;
void rewind() const noexcept;
void stop() const noexcept;

void set(const sound& snd) const noexcept;
Expand Down
14 changes: 0 additions & 14 deletions include/containers/pair.h

This file was deleted.

2 changes: 1 addition & 1 deletion include/graph/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class shader final: public resource
shader() noexcept;
~shader() noexcept override;

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

GLuint id() const noexcept { return m_id; }

Expand Down
3 changes: 2 additions & 1 deletion include/graph/shader_data.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __DK_GRAPH_SHADER_DATA_H__
#define __DK_GRAPH_SHADER_DATA_H__

#include "status.h"
#include "resource.h"
#include "containers/string.h"
#include "containers/string_view.h"
Expand All @@ -18,7 +19,7 @@ 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; }
static resource_type type() noexcept { 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
35 changes: 35 additions & 0 deletions include/graph/texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef __DK_GRAPH_TEXTURE_H__
#define __DK_GRAPH_TEXTURE_H__

#include <GL/gl.h>
#include "status.h"
#include "graph/texture_data.h"

namespace dk::graph
{

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

static GLenum convert_to_gl_fmt(uint8_t red_bits, uint8_t green_bits, uint8_t blue_bits, uint8_t alpha_bits) noexcept;

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

static resource_type type() noexcept { return resource_type::TEXTURE; }

void enable() const noexcept;
void disable() const noexcept;

GLuint id() const noexcept { return m_id; }

status create(const texture_data& data) noexcept;
void destroy() noexcept;
};

}

#endif // !__DK_GRAPH_TEXTURE_H__
48 changes: 48 additions & 0 deletions include/graph/texture_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef __DK_GRAPH_TEXTURE_DATA_H__
#define __DK_GRAPH_TEXTURE_DATA_H__

#include <stdint.h>
#include "resource.h"

namespace dk::graph
{

class texture_data final: public resource
{
private:
uint8_t* m_data;
uint32_t m_width;
uint32_t m_height;
uint8_t m_red_bits;
uint8_t m_green_bits;
uint8_t m_blue_bits;
uint8_t m_alpha_bits;

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

static resource_type type() noexcept { return resource_type::TEXTURE_DATA; }

uint8_t* data() const noexcept { return m_data; }
uint32_t width() const noexcept { return m_width; }
uint32_t height() const noexcept { return m_height; }
uint8_t red_bits() const noexcept { return m_red_bits; }
uint8_t green_bits() const noexcept { return m_green_bits; }
uint8_t blue_bits() const noexcept { return m_blue_bits; }
uint8_t alpha_bits() const noexcept { return m_alpha_bits; }

void set_data(uint8_t* data) noexcept { m_data = data; }
void set_width(uint32_t width) noexcept { m_width = width; }
void set_height(uint32_t height) noexcept { m_height = height; }
void set_red_bits(uint8_t red_bits) noexcept { m_red_bits = red_bits; }
void set_green_bits(uint8_t green_bits) noexcept { m_green_bits = green_bits; }
void set_blue_bits(uint8_t blue_bits) noexcept { m_blue_bits = blue_bits; }
void set_alpha_bits(uint8_t alpha_bits) noexcept { m_alpha_bits = alpha_bits; }

void destroy() noexcept;
};

}

#endif // !__DK_GRAPH_TEXTURE_DATA_H__
2 changes: 1 addition & 1 deletion include/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class resource
public:
virtual ~resource() noexcept = default;

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

}
Expand Down
4 changes: 4 additions & 0 deletions include/sys/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ enum class keyboard_btn
L_ALT,
R_ALT,

/* top row */ Q, W, E, R, T, Y, U, I, O, P,
/* middle row */ A, S, D, F, G, H, J, K, L,
/* bottom row */ Z, X, C, V, B, N, M,

ENUM_SIZE
};

Expand Down
22 changes: 22 additions & 0 deletions include/sys/loaders/texture_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __DK_SYS_TEXTURE_LOADER_H__
#define __DK_SYS_TEXTURE_LOADER_H__

#include "graph/texture.h"
#include "sys/resource_loader.h"

namespace dk::sys
{

class texture_loader final: public resource_loader
{
public:
status load(graph::texture_data& res, string_view file_path) noexcept;
status load(graph::texture& res, string_view file_path) noexcept;

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

}

#endif // !__DK_SYS_TEXTURE_LOADER_H__
6 changes: 4 additions & 2 deletions include/sys/loaders/wave_loader.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __DK_SYS_WAVE_DATA_LOADER_H__
#define __DK_SYS_WAVE_DATA_LOADER_H__

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

#define WAVE_CHUNK_ID(id) (id[0] + (id[1] << 8) + (id[2] << 16) + (id[3] << 24))
Expand Down Expand Up @@ -68,9 +68,11 @@ struct wave_header
class wave_loader final: public resource_loader
{
public:
status load(string_view file_path, audio::sound_data& res) noexcept;
status load(audio::sound_data& res, string_view file_path) noexcept;
status load(audio::sound& res, string_view file_path) noexcept;

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

}
Expand Down
2 changes: 2 additions & 0 deletions include/sys/resource_loader.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __DK_SYS_RESOURCE_LOADER_H__
#define __DK_SYS_RESOURCE_LOADER_H__

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

Expand All @@ -13,6 +14,7 @@ class resource_loader
virtual ~resource_loader() noexcept = default;

virtual resource* load(string_view file_path, resource_type type) noexcept = 0;
virtual status load(resource& res, string_view file_path, resource_type type) noexcept = 0;
};

}
Expand Down
12 changes: 9 additions & 3 deletions include/sys/resource_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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

namespace dk::sys
{
Expand All @@ -15,12 +14,19 @@ class resource_manager

public:
template<typename T>
static T* load(string_view file_path) noexcept
static T* load(string_view file_path /* , resource_type type = T::type() */) noexcept
{
return static_cast<T*>(load(file_path, T{}.type()));
return static_cast<T*>(load(file_path, T::type()));
}

template<typename T>
static status load(T& t, string_view file_path /* , resource_type type = T::type() */) noexcept
{
return load(static_cast<resource&>(t), file_path, T::type());
}

static resource* load(string_view file_path, resource_type type) noexcept;
static status load(resource& res, string_view file_path, resource_type type) noexcept;

static string_view mime(string_view file_path) noexcept;
static resource_loader* loader(string_view mime) noexcept;
Expand Down
4 changes: 1 addition & 3 deletions src/audio/sound_data.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "audio/sound_data.h"
#include "fs/file.h"
#include "log.h"
#include "mem.h"

namespace dk::audio
Expand All @@ -22,7 +20,7 @@ sound_data::~sound_data() noexcept /* override */
void sound_data::destroy() noexcept
{
if (m_data != nullptr) {
mem_destroy(m_data);
mem_dealloc(m_data);
m_data = nullptr;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/audio/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ void source::pause() const noexcept
AL_CALL(alSourcePause(m_id));
}

void source::rewind() const noexcept
{
AL_CALL(alSourceRewind(m_id));
this->play();
}

void source::stop() const noexcept
{
AL_CALL(alSourceStop(m_id));
Expand Down
8 changes: 7 additions & 1 deletion src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#include "util/ticker.h"
#include "sys/resource_manager.h"
#include "sys/loaders/wave_loader.h"
#include "sys/loaders/texture_loader.h"

namespace dk
{

namespace
{

sys::wave_loader wave_loader;
sys::wave_loader wave_loader;
sys::texture_loader texture_loader;

}

Expand Down Expand Up @@ -79,6 +81,10 @@ template<> /* static */ graph::renderer* core::active<graph::renderer>() noexcep

/* std loaders */
sys::resource_manager::add(&wave_loader, "wav");
sys::resource_manager::add(&texture_loader, "png");
sys::resource_manager::add(&texture_loader, "bmp");
sys::resource_manager::add(&texture_loader, "jpg");
sys::resource_manager::add(&texture_loader, "tga");

DK_LOG_OK("Engine core created");
return status::OK;
Expand Down
Loading

0 comments on commit 7284482

Please sign in to comment.