diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/3rdparty/wav/wav.c | 9 | ||||
-rw-r--r-- | src/libjin/audio/sdl/source.cpp | 7 | ||||
-rw-r--r-- | src/libjin/audio/sdl/source.h | 4 | ||||
-rw-r--r-- | src/libjin/audio/source.cpp | 10 | ||||
-rw-r--r-- | src/libjin/audio/source.h | 5 | ||||
-rw-r--r-- | src/libjin/render/image.cpp | 69 | ||||
-rw-r--r-- | src/libjin/render/image.h | 11 | ||||
-rw-r--r-- | src/libjin/utils/unittest.cpp | 7 | ||||
-rw-r--r-- | src/libjin/utils/utils.h | 2 | ||||
-rw-r--r-- | src/lua/graphics/luaopen_graphics.cpp | 2 |
10 files changed, 68 insertions, 58 deletions
diff --git a/src/3rdparty/wav/wav.c b/src/3rdparty/wav/wav.c index af33946..6f9b2ab 100644 --- a/src/3rdparty/wav/wav.c +++ b/src/3rdparty/wav/wav.c @@ -29,9 +29,7 @@ next: return p + 8; } -int wav_read(wav_t *w, const void *mem, size_t len) { - char* data = (char*)malloc(len); - memcpy(data, mem, len); +int wav_read(wav_t *w, const void *data, size_t len) { int bitdepth, channels, samplerate, format; size_t sz; const char *p = (const char*)data; @@ -57,8 +55,11 @@ int wav_read(wav_t *w, const void *mem, size_t len) { /* Find data subchunk */ p = findSubChunk((const char*)data, len, "data", &sz); if (!p) return WAV_ENODATA; + /* copy p to new buffer */ + char* buffer = (char*)malloc(sz); + memmove(buffer, p, sz); /* Init wav_t struct */ - w->data = p; + w->data = buffer; w->samplerate = samplerate; w->channels = channels; w->length = (sz / (bitdepth / 8)) / channels; diff --git a/src/libjin/audio/sdl/source.cpp b/src/libjin/audio/sdl/source.cpp index 2bb63f5..b647229 100644 --- a/src/libjin/audio/sdl/source.cpp +++ b/src/libjin/audio/sdl/source.cpp @@ -54,7 +54,7 @@ namespace audio shared std::vector<SDLSource*> Manager::sources; shared Manager* Manager::manager = nullptr; - SDLSource* SDLSource::createSource(SourceType format, const char* file) + SDLSource* SDLSource::createSource(const char* file) { std::ifstream fs; fs.open(file, std::ios::binary); @@ -69,12 +69,12 @@ namespace audio char* buffer = (char*)malloc(size); memset(buffer, 0, size); fs.read(buffer, size); - SDLSource* source = createSource(format, buffer, size); + SDLSource* source = createSource(buffer, size); free(buffer); return source; } - SDLSource* SDLSource::createSource(SourceType format, void* mem, size_t size) + SDLSource* SDLSource::createSource(void* mem, size_t size) { if (mem == nullptr) return nullptr; @@ -82,6 +82,7 @@ namespace audio #define read(FMT) case FMT : source->load##FMT(mem, size); break try { + SourceType format = getType(mem, size); switch (format) { read(OGG); diff --git a/src/libjin/audio/sdl/source.h b/src/libjin/audio/sdl/source.h index 900568f..743d4f0 100644 --- a/src/libjin/audio/sdl/source.h +++ b/src/libjin/audio/sdl/source.h @@ -22,8 +22,8 @@ namespace audio ~SDLSource(); - static SDLSource* createSource(SourceType format, const char* file); - static SDLSource* createSource(SourceType format, void* mem, size_t size); + static SDLSource* createSource(const char* file); + static SDLSource* createSource(void* mem, size_t size); /* ISource interface */ void play() override; diff --git a/src/libjin/audio/source.cpp b/src/libjin/audio/source.cpp index dd87718..ec80a0f 100644 --- a/src/libjin/audio/source.cpp +++ b/src/libjin/audio/source.cpp @@ -1,3 +1,4 @@ +#include <cstring> #include "source.h" namespace jin @@ -5,7 +6,16 @@ namespace jin namespace audio { + SourceType Source::getType(const void* mem, int size) + { + const char* p = (const char* )mem; + if (memcmp(p, "RIFF", 4) == 0 && memcmp(p + 8, "WAVE", 4) == 0) + return SourceType::WAV; + if (memcmp(p, "OggS", 4) == 0) + return SourceType::OGG; + return SourceType::INVALID; + } } }
\ No newline at end of file diff --git a/src/libjin/audio/source.h b/src/libjin/audio/source.h index ea82725..313ebde 100644 --- a/src/libjin/audio/source.h +++ b/src/libjin/audio/source.h @@ -10,6 +10,7 @@ namespace audio enum SourceType { + INVALID = 0, WAV, OGG, }; @@ -35,7 +36,9 @@ namespace audio virtual bool setLoop(bool loop) = 0; virtual void setRate(float rate) = 0; - protected: + protected: + + static SourceType getType(const void* mem, int size); }; diff --git a/src/libjin/render/image.cpp b/src/libjin/render/image.cpp index 7955aa2..564b54e 100644 --- a/src/libjin/render/image.cpp +++ b/src/libjin/render/image.cpp @@ -1,3 +1,4 @@ +#include <fstream> #include "image.h" #include "3rdparty/stb/stb_image.h" #include "../utils/utils.h" @@ -8,16 +9,40 @@ namespace jin namespace render { - Image::Image(const char* file) - : Drawable(), pixels(0) + Image* Image::createImage(const char* file) + { + std::ifstream fs; + fs.open(file, std::ios::binary); + Image* img = nullptr; + if (fs.is_open()) + { + fs.seekg(0, std::ios::end); + int size = fs.tellg(); + fs.seekg(0, std::ios::beg); + char* buffer = (char*)malloc(size); + memset(buffer, 0, size); + fs.read(buffer, size); + img = createImage(buffer, size); + free(buffer); + } + fs.close(); + return img; + } + + Image* Image::createImage(const void* mem, size_t size) { - loadf(file); + Image* img = new Image(); + if(!img->loadb(mem, size)) + { + delete img; + img = nullptr; + } + return img; } - Image::Image(const char* buffer, size_t size) + Image::Image() : Drawable(), pixels(0) { - loadb(buffer, size); } Image::~Image() @@ -34,39 +59,7 @@ namespace render return pixels[x + y * width]; } - bool Image::loadf(const char* f) - { - unsigned char* imageData = stbi_load(f, &width, &height, NULL, STBI_rgb_alpha); - if (imageData == 0) return false; - pixels = (color*)imageData; - - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, - height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); - - // set render vertices - Drawable::setVertices( - new float [DRAWABLE_V_SIZE] { - 0, 0, - 0, (float)height, - (float)width, (float)height, - (float)width, 0, - }, - new float [DRAWABLE_V_SIZE] { - 0, 0, - 0, 1, - 1, 1, - 1, 0 - } - ); - - return true; - } - - bool Image::loadb(const char* b, size_t size) + bool Image::loadb(const void* b, size_t size) { // ʹstbi_load_from_memory unsigned char* imageData = stbi_load_from_memory((unsigned char *)b, size, &width, &height, NULL, STBI_rgb_alpha); diff --git a/src/libjin/render/image.h b/src/libjin/render/image.h index 58ca04f..95b4e4f 100644 --- a/src/libjin/render/image.h +++ b/src/libjin/render/image.h @@ -11,9 +11,9 @@ namespace render { public: - - Image(const char* file); - Image(const char* buffer, size_t size); + + static Image* createImage(const char* file); + static Image* createImage(const void* mem, size_t size); ~Image(); @@ -21,8 +21,9 @@ namespace render private: - bool loadf(const char* file); - bool loadb(const char* buffer, size_t size); + Image(); + + bool loadb(const void* buffer, size_t size); color* pixels; diff --git a/src/libjin/utils/unittest.cpp b/src/libjin/utils/unittest.cpp index ecddffd..062604e 100644 --- a/src/libjin/utils/unittest.cpp +++ b/src/libjin/utils/unittest.cpp @@ -14,9 +14,10 @@ int main(int argc, char* argv[]) { Audio* audio = SDLAudio::get(); audio->init(0); - //SDLSource* source = SDLSource::createSource(SourceType::OGG, "a.ogg"); - SDLSource* source2 = SDLSource::createSource(SourceType::WAV, "a.wav"); - source2->play(); + SDLSource* source = SDLSource::createSource("a.ogg"); + SDLSource* source2 = SDLSource::createSource("a.wav"); + source->play(); + //source2->play(); source2->setLoop(true); //source->play(); //source->setLoop(true); diff --git a/src/libjin/utils/utils.h b/src/libjin/utils/utils.h index cf0920e..9073d59 100644 --- a/src/libjin/utils/utils.h +++ b/src/libjin/utils/utils.h @@ -4,6 +4,6 @@ #include "macros.h" #include "endian.h" -#define UNITTEST 0 +#define UNITTEST 1 #endif
\ No newline at end of file diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp index 2b6031b..0d06aca 100644 --- a/src/lua/graphics/luaopen_graphics.cpp +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -71,7 +71,7 @@ namespace lua fs->read(f, &b); Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_IMAGE, sizeof(Proxy)); - Image* img = new Image((const char*)b.data, b.size); + Image* img = Image::createImage(b.data, b.size); proxy->bind(img); return 1; } |