diff options
author | chai <chaifix@163.com> | 2018-07-28 00:50:12 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-07-28 00:50:12 +0800 |
commit | 92dab582ccac31be7fa410e7f4fb3789e88a0629 (patch) | |
tree | 008bbecc76a73aae38d77d1851f4230756356a33 /src | |
parent | b855ebb91ad8d97617ec1aa418b4add84670a07d (diff) |
*update
Diffstat (limited to 'src')
21 files changed, 100 insertions, 749 deletions
diff --git a/src/libjin/Audio/SDL/Audio.cpp b/src/libjin/Audio/SDL/SDLAudio.cpp index 47d8cf8..c154ae4 100644 --- a/src/libjin/Audio/SDL/Audio.cpp +++ b/src/libjin/Audio/SDL/SDLAudio.cpp @@ -2,8 +2,8 @@ #if JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO #include <iostream> -#include "audio.h" -#include "source.h" +#include "SDLAudio.h" +#include "SDLSource.h" #include "../../math/math.h" #include "../../utils/log.h" diff --git a/src/libjin/Audio/SDL/Audio.h b/src/libjin/Audio/SDL/SDLAudio.h index 6da6605..4c9ab34 100644 --- a/src/libjin/Audio/SDL/Audio.h +++ b/src/libjin/Audio/SDL/SDLAudio.h @@ -65,4 +65,4 @@ namespace audio } #endif // JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO -#endif
\ No newline at end of file +#endif // __JIN_AUDIO_SDL_H
\ No newline at end of file diff --git a/src/libjin/audio/sdl/source.cpp b/src/libjin/Audio/SDL/SDLSource.cpp index 0eedbba..e0791f6 100644 --- a/src/libjin/audio/sdl/source.cpp +++ b/src/libjin/Audio/SDL/SDLSource.cpp @@ -6,12 +6,12 @@ #include "../../math/math.h" #include "../../utils/macros.h" -#include "source.h" +#include "SDLSource.h" #include "3rdparty/wav/wav.h" #define STB_VORBIS_HEADER_ONLY #include "3rdparty/stb/stb_vorbis.c" -#include "audio.h" +#include "SDLAudio.h" namespace jin { diff --git a/src/libjin/Audio/SDL/Source.h b/src/libjin/Audio/SDL/SDLSource.h index 38f7ec4..38f7ec4 100644 --- a/src/libjin/Audio/SDL/Source.h +++ b/src/libjin/Audio/SDL/SDLSource.h diff --git a/src/libjin/Audio/SDL/Source.cpp b/src/libjin/Audio/SDL/Source.cpp deleted file mode 100644 index 0eedbba..0000000 --- a/src/libjin/Audio/SDL/Source.cpp +++ /dev/null @@ -1,399 +0,0 @@ -#include "../../modules.h" -#if JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO - -#include <exception> -#include <fstream> - -#include "../../math/math.h" -#include "../../utils/macros.h" -#include "source.h" -#include "3rdparty/wav/wav.h" -#define STB_VORBIS_HEADER_ONLY -#include "3rdparty/stb/stb_vorbis.c" - -#include "audio.h" - -namespace jin -{ -namespace audio -{ - -#define BITS 8 - - typedef struct SDLSourceCommand - { - typedef enum Action - { - Nothing = 0, - Play, - Stop, - Pause, - Resume, - Rewind, - SetVolume, - SetLoop, - SetRate, - }; - Action action; - union { - int _integer; - float _float; - bool _boolean; - const char* _string; - } parameter; - - SDLSource* source; - }; - - typedef enum CHANNEL - { - MONO = 1, // - STEREO = 2, // - }; - - typedef MASK enum STATUS - { - PLAYING = 1, - PAUSED = 2, - STOPPED = 4 - }; - -#define Command SDLSourceCommand -#define Action Command::Action -#define Manager SDLSourceManager - - shared std::queue<Command*> Manager::commands; - shared std::stack<Command*> Manager::commandsPool; - shared std::vector<SDLSource*> Manager::sources; - shared Manager* Manager::manager = nullptr; - - SDLSource* SDLSource::createSource(const char* file) - { - std::ifstream fs; - fs.open(file, std::ios::binary); - if (!fs.is_open()) - { - fs.close(); - return nullptr; - } - 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); - fs.close(); - SDLSource* source = createSource(buffer, size); - free(buffer); - return source; - } - - SDLSource* SDLSource::createSource(void* mem, size_t size) - { - if (mem == nullptr) - return nullptr; - SDLSource* source = new SDLSource(); - try - { - SourceType format = getType(mem, size); - switch (format) - { - case OGG: source->decode_ogg(mem, size); break; - case WAV: source->decode_wav(mem, size); break; - } - } - catch (SourceException& exp) - { - delete source; - return nullptr; - }; - return source; - } - - SDLSource::SDLSource() - { - memset(&status, 0, sizeof(status)); - memset(&raw, 0, sizeof(raw)); - } - - SDLSource::~SDLSource() - { - delete raw.data; - raw.end = 0; - raw.data = 0; - } - - void SDLSource::decode_wav(void* mem, int size) - { - wav_t wav; - if (wav_read(&wav, mem, size) == 0) - { - raw.data = wav.data; - raw.length = wav.length * wav.bitdepth / 8; - raw.end = (char*)raw.data + raw.length; - raw.samplerate = wav.samplerate; - raw.bitdepth = wav.bitdepth; - raw.samples = raw.length / (wav.bitdepth / 8.f) / wav.channels; - raw.channels = clamp(wav.channels, CHANNEL::MONO, CHANNEL::STEREO); - } - else - throw SourceException(); - } - - void SDLSource::decode_ogg(void* _mem, int size) - { - unsigned char* mem = (unsigned char*)_mem; - int channels; - int samplerate; - short* data = (short*)raw.data; - int samples = stb_vorbis_decode_memory( - mem, - size, - &channels, - &samplerate, - &data - ); - const int bitdepth = sizeof(short) * BITS; - raw.channels = channels; - raw.samplerate = samplerate; - raw.data = data; - raw.samples = samples; - raw.length = samples * channels * sizeof(short); - raw.bitdepth = bitdepth; - raw.end = (char*)data + raw.length; - } - -#define ActionNone(T)\ -do{\ -Command* cmd = Manager::get()->getCommand();\ -cmd->action = Action::T; \ -cmd->source = this; \ -Manager::get()->pushCommand(cmd); \ -} while (0) - -#define ActionArg(T, ARGT, ARG)\ -do{\ -Command* cmd = Manager::get()->getCommand();\ -cmd->action = Action::T; \ -cmd->parameter.ARGT = ARG; \ -cmd->source = this; \ -Manager::get()->pushCommand(cmd); \ -}while(0) - -#define ActionInt(T, INT) ActionArg(T, _integer, INT) -#define ActionFloat(T, FLT) ActionArg(T, _float, FLT) -#define ActionString(T, STR) ActionArg(T, _string, STR) -#define ActionBool(T, BOL) ActionArg(T, _boolean, BOL) - - void SDLSource::play() - { - ActionNone(Play); - } - - void SDLSource::stop() - { - ActionNone(Stop); - } - - void SDLSource::pause() - { - ActionNone(Pause); - } - - void SDLSource::resume() - { - ActionNone(Resume); - } - - void SDLSource::rewind() - { - ActionNone(Rewind); - } - - inline bool SDLSource::isStopped() const - { - return is(STOPPED); - } - - bool SDLSource::isPaused() const - { - return is(PAUSED); - } - - void SDLSource::setPitch(float pitch) - { - } - - void SDLSource::setVolume(float volume) - { - ActionFloat(SetVolume, volume); - } - - bool SDLSource::setLoop(bool loop) - { - ActionBool(SetLoop, loop); - return false; - } - - void SDLSource::setRate(float rate) - { - ActionFloat(SetRate, rate); - } - - inline void SDLSource::handle( - SDLSourceManager* manager, - SDLSourceCommand* cmd - ) - { - switch (cmd->action) - { - case Command::Action::Play: - manager->removeSource(this); - manager->pushSource(this); - status.state = PLAYING; - status.pos = 0; // rewind - break; - case Command::Action::Stop: - manager->removeSource(this); - status.state = STOPPED; - status.pos = 0; // rewind - break; - case Command::Action::Pause: - manager->removeSource(this); - status.state = PAUSED; - break; - case Command::Action::Resume: - manager->removeSource(this); - manager->pushSource(this); - status.state = PLAYING; - break; - case Command::Action::Rewind: - status.state = PLAYING; - status.pos = 0; - break; - case Command::Action::SetVolume: - //float cmd->parameter._float; - break; - case Command::Action::SetLoop: - status.loop = cmd->parameter._boolean; - break; - } - } - - inline void SDLSource::process(void* buf, size_t size) - { - short* buffer = (short*)buf; // AUDIO_S16SYS - unsigned int samples = size / SDLAUDIO_BYTEDEPTH; - short* sample; - short origin; - - const char bitdepth = raw.bitdepth; - const char channles = raw.channels; - - int pos = status.pos; - int pitch = status.pitch; - int state = status.state; - bool loop = status.loop; - int volume = status.volume; - short* clip16 = nullptr; - char* clip8 = nullptr; - int clip = 0; - - if (bitdepth == 8) - clip8 = (char*)raw.data; - else if (bitdepth == 16) - clip16 = (short*)raw.data; - - for (int i = 0; i < samples; i+=2 /*˫*/) - { - /* Ƶļsampleᱻ */ - sample = buffer + i * SDLAUDIO_BYTEDEPTH; - origin = *sample; - if (bitdepth == 8) - { - clip = *clip8; - } - else if (bitdepth == 16) - clip = *clip16; - - } - } - - Manager* Manager::get() - { - return (manager == nullptr ? manager = new Manager() : manager); - } - - shared void Manager::processCommands() - { - Command* cmd = nullptr; - SDLSource* source = nullptr; - while (!commands.empty()) - { - cmd = commands.front(); - if (cmd != nullptr) - { - source = cmd->source; - if (source != nullptr) - source->handle(manager, cmd); - } - commands.pop(); - } - } - - /* AUDIO_S16SYS[size>>1] buffer */ - shared void Manager::processSources(void* buf, size_t size) - { - /* clear render buffer */ - memset(buf, 0, size); - SDLSource* src = nullptr; - std::vector<SDLSource*>::iterator it = sources.begin(); - for (; it != sources.end();) - { - src = *it; - if (src != nullptr) - src->process(buf, size); - ++it; - } - } - - shared void Manager::removeSource(SDLSource* source) - { - std::vector<SDLSource*>::iterator it = sources.begin(); - for (it = sources.begin(); it != sources.end(); ) - { - if (*it == source) - { - it = sources.erase(it); - return; - } - ++it; - } - } - - shared void Manager::pushSource(SDLSource* source) - { - if(source != nullptr) - sources.push_back(source); - } - - shared void Manager::pushCommand(SDLSourceCommand* cmd) - { - commands.push(cmd); - } - - shared Command* Manager::getCommand() - { - if (!commandsPool.empty()) - { - Command* cmd = commandsPool.top(); - commandsPool.pop(); - return cmd; - } - return new Command(); - } - -} -} - -#endif // JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO
\ No newline at end of file diff --git a/src/libjin/Common/Subsystem.h b/src/libjin/Common/Subsystem.h index 35563da..1212abf 100644 --- a/src/libjin/Common/Subsystem.h +++ b/src/libjin/Common/Subsystem.h @@ -14,9 +14,10 @@ namespace jin struct Setting {}; typedef Setting SettingBase; - void init(const SettingBase* setting) + bool init(const SettingBase* setting) { - CALLONCE(initSystem(setting)); + static bool success = initSystem(setting); + return success; } void quit() diff --git a/src/libjin/Input/Event.h b/src/libjin/Input/Event.h index e09f422..4d7230a 100644 --- a/src/libjin/Input/Event.h +++ b/src/libjin/Input/Event.h @@ -1,13 +1,33 @@ #ifndef __JIN_EVENT_H #define __JIN_EVENT_H +#include "../modules.h" +#if JIN_MODULES_INPUT + namespace jin { namespace input { +#if JIN_INPUT_SDL +#include "SDL.h" + typedef SDL_Event Event; - - + inline int pollEvent(Event* e) + { + return SDL_PollEvent(e); + } + + enum EventType{ + QUIT = SDL_QUIT, + KEYDOWN = SDL_KEYDOWN , + KEYUP = SDL_KEYUP, + MOUSEMOTION = SDL_MOUSEMOTION, + MOUSEBUTTONDOWN = SDL_MOUSEBUTTONDOWN, + MOUSEBUTTONUP = SDL_MOUSEBUTTONUP, + MOUSEWHEEL = SDL_MOUSEWHEEL + }; +#endif // JIN_INPUT_SDL } } +#endif // JIN_MODULES_INPUT #endif
\ No newline at end of file diff --git a/src/libjin/Render/Window.cpp b/src/libjin/Render/Window.cpp index 6507691..dc2902e 100644 --- a/src/libjin/Render/Window.cpp +++ b/src/libjin/Render/Window.cpp @@ -6,7 +6,7 @@ #include "3rdparty/GLee/GLee.h" #include "canvas.h" #include "../utils/utils.h" -#include "../audio/sdl/audio.h" +#include "../audio/sdl/SDLAudio.h" #include "../utils/log.h" namespace jin diff --git a/src/libjin/audio/sdl/audio.cpp b/src/libjin/audio/sdl/audio.cpp deleted file mode 100644 index 47d8cf8..0000000 --- a/src/libjin/audio/sdl/audio.cpp +++ /dev/null @@ -1,121 +0,0 @@ -#include "../../modules.h" -#if JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO - -#include <iostream> -#include "audio.h" -#include "source.h" -#include "../../math/math.h" -#include "../../utils/log.h" - -namespace jin -{ -namespace audio -{ - - /* עcallbackƵ̵߳ */ - static void defaultCallback(void *userdata, Uint8 *stream, int size) - { - static SDLAudio* audio = static_cast<SDLAudio*>(userdata); - audio->lock(); - audio->processCommands(); - audio->processSources(stream, size); - audio->unlock(); - } - - onlyonce bool SDLAudio::initSystem(const SettingBase* s) - { - Loghelper::log(Loglevel::LV_INFO, "Init Audio System"); - - if (SDL_Init(SDL_INIT_AUDIO) < 0) - return false; - SDL_AudioSpec spec; - Setting* setting = (Setting*)s; - if (setting == nullptr) - return false; - - unsigned int samplerate = setting->samplerate; - unsigned int samples = clamp(setting->samples, 1, setting->samplerate); - - spec.freq = samplerate; // ÿsample,õ 11025, 22050, 44100 and 48000 Hz. - spec.format = AUDIO_S16SYS; // signed 16-bit samples in native byte order - spec.channels = SDLAUDIO_CHANNELS; // - spec.samples = samples; // ÿβʱһã=setting->samplerateÿֻ1 - spec.userdata = this; - spec.callback = defaultCallback; - - audioDevice = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0); - if (audioDevice == 0) - return false; - /* start audio */ - SDL_PauseAudioDevice(audioDevice, 0); - return true; - } - - onlyonce void SDLAudio::quitSystem() - { - SDL_CloseAudio(); - } - - void SDLAudio::lock() - { - SDL_LockAudioDevice(audioDevice); - } - - void SDLAudio::unlock() - { - SDL_UnlockAudioDevice(audioDevice); - } - - void SDLAudio::processCommands() - { - SDLSourceManager::get()->processCommands(); - } - - void SDLAudio::processSources(void* buffer, size_t len) - { - SDLSourceManager::get()->processSources(buffer, len); - } - - void SDLAudio::play() {} - - void SDLAudio::stop() {} - - bool SDLAudio::pause() - { - return false; - } - - bool SDLAudio::pause(Source* source) - { - return false; - } - - bool SDLAudio::resume() - { - return false; - } - - bool SDLAudio::resume(Source* source) - { - return false; - } - - void SDLAudio::rewind() - { - - } - - void SDLAudio::setVolume(float volume) - { - - } - - float SDLAudio::getVolume() - { - return 0.f; - } - -} -} - -#endif // JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO
\ No newline at end of file diff --git a/src/libjin/audio/sdl/audio.h b/src/libjin/audio/sdl/audio.h deleted file mode 100644 index 6da6605..0000000 --- a/src/libjin/audio/sdl/audio.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef __JIN_AUDIO_SDL_H -#define __JIN_AUDIO_SDL_H -#include "../../modules.h" -#if JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO - -#include <vector> -#include "../audio.h" - -namespace jin -{ -namespace audio -{ - -#define SDLAUDIO_BITDEPTH 16 -#define SDLAUDIO_BYTEDEPTH (SDLAUDIO_BITDEPTH >> 3) -#define SDLAUDIO_CHANNELS 2 - - class SDLAudio : public AudioSystem<SDLAudio> - { - - public: - - struct Setting : SettingBase - { - public: - int samplerate; // Ƶ - int samples; // sample<=samplerate - }; - - /* IAudio interface */ - void play() override; - void stop() override; - bool pause() override; - bool pause(Source* source) override; - bool resume() override; - bool resume(Source* source) override; - void rewind() override; - void setVolume(float volume) override; - float getVolume() override; - - /* process functions*/ - void processCommands(); - void processSources(void* buffer, size_t len); - - void lock(); - void unlock(); - - private: - - SDLAudio() {}; - ~SDLAudio() {}; - - SINGLETON(SDLAudio); - - onlyonce bool initSystem(const SettingBase* setting) override; - onlyonce void quitSystem() override; - - unsigned int audioDevice; - - }; - - typedef SDLAudio::Setting SDLAudioSetting; - -} -} - -#endif // JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO -#endif
\ No newline at end of file diff --git a/src/libjin/audio/sdl/source.h b/src/libjin/audio/sdl/source.h deleted file mode 100644 index 38f7ec4..0000000 --- a/src/libjin/audio/sdl/source.h +++ /dev/null @@ -1,116 +0,0 @@ -#ifndef __JIN_SOURCE_SDL_H -#define __JIN_SOURCE_SDL_H -#include "../../modules.h" -#if JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO - -#include <vector> -#include <queue> -#include <stack> -#include <exception> - -#include "../source.h" - -namespace jin -{ -namespace audio -{ - - typedef struct SDLSourceCommand; - class SDLSourceManager; - - class SDLSource : public Source - { - - public: - - ~SDLSource(); - - static SDLSource* createSource(const char* file); - static SDLSource* createSource(void* mem, size_t size); - - /* ISource interface */ - void play() override; - void stop() override; - void pause() override; - void resume() override; - void rewind() override; - bool isStopped() const override; - bool isPaused() const override; - void setPitch(float pitch) override; - // Ͻ - void setVolume(float volume) override; - bool setLoop(bool loop) override; - void setRate(float rate) override; - - inline void handle(SDLSourceManager* manager, SDLSourceCommand* cmd); - inline void process(void* buffer, size_t size); - - private: - - SDLSource(); - - void decode_wav(void* mem, int size); - void decode_ogg(void* mem, int size); - - inline bool is(int state) const { return (status.state & state) == state; } - - struct - { - const void* data; // Ƶ - int length; // dataֽڳ - const void* end; // dataβ = (unsigned char*)data + size - int samplerate; // Ƶ - unsigned char bitdepth; // ÿsampleıس - int samples; // sample = size / (bitdepth / 8) - unsigned char channels; // channel1(mono)2(stereo) - } raw; - - /* Procedure controller variable */ - struct - { - int pos; // ǰŵsample - int pitch; // pitch - int state; // ǰ״̬ - bool loop; // loop or not - int volume; // - } status; - - }; - - class SDLSourceManager - { - - public: - - static SDLSourceManager* get(); - - /* Process function */ - static void processCommands(); - static void processSources(void* buffer, size_t size); - - static void removeSource(SDLSource* source); - static void pushSource(SDLSource* source); - static SDLSourceCommand* getCommand(); - static void pushCommand(SDLSourceCommand* cmd); - - static SDLSourceManager* manager; - - static std::queue<SDLSourceCommand*> commands; - static std::stack<SDLSourceCommand*> commandsPool; - static std::vector<SDLSource*> sources; // processing sources - - }; - - class SourceException : public std::exception - { - const char * what() const throw () - { - return "Load Source Exception"; - } - }; - -} -} - -#endif // JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO -#endif
\ No newline at end of file diff --git a/src/libjin/common/subsystem.h b/src/libjin/common/subsystem.h index 35563da..1212abf 100644 --- a/src/libjin/common/subsystem.h +++ b/src/libjin/common/subsystem.h @@ -14,9 +14,10 @@ namespace jin struct Setting {}; typedef Setting SettingBase; - void init(const SettingBase* setting) + bool init(const SettingBase* setting) { - CALLONCE(initSystem(setting)); + static bool success = initSystem(setting); + return success; } void quit() diff --git a/src/libjin/input/event.h b/src/libjin/input/event.h index e09f422..4d7230a 100644 --- a/src/libjin/input/event.h +++ b/src/libjin/input/event.h @@ -1,13 +1,33 @@ #ifndef __JIN_EVENT_H #define __JIN_EVENT_H +#include "../modules.h" +#if JIN_MODULES_INPUT + namespace jin { namespace input { +#if JIN_INPUT_SDL +#include "SDL.h" + typedef SDL_Event Event; - - + inline int pollEvent(Event* e) + { + return SDL_PollEvent(e); + } + + enum EventType{ + QUIT = SDL_QUIT, + KEYDOWN = SDL_KEYDOWN , + KEYUP = SDL_KEYUP, + MOUSEMOTION = SDL_MOUSEMOTION, + MOUSEBUTTONDOWN = SDL_MOUSEBUTTONDOWN, + MOUSEBUTTONUP = SDL_MOUSEBUTTONUP, + MOUSEWHEEL = SDL_MOUSEWHEEL + }; +#endif // JIN_INPUT_SDL } } +#endif // JIN_MODULES_INPUT #endif
\ No newline at end of file diff --git a/src/libjin/jin.h b/src/libjin/jin.h index b1a1725..89d4c60 100644 --- a/src/libjin/jin.h +++ b/src/libjin/jin.h @@ -1,17 +1,21 @@ #ifndef __JIN_H #define __JIN_H -#include "utils/utils.h" -#include "audio/audio.h" -#include "core/core.h" +#include "modules.h" + +#include "Utils/utils.h" +#ifdef JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO +#include "Audio/SDL/SDLAudio.h" +#endif // JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO +#include "Core/Core.h" #include "Filesystem/Filesystem.h" -#include "input/input.h" -#include "net/net.h" -#include "render/render.h" +#include "Input/Input.h" +#include "Net/Net.h" +#include "Render/Render.h" #define JIN_VERSION "Jin 0.1" -#define JIN_RELEASE "Jin 0.1.0" -#define JIN_VERSION_NUM 100 // 00.01.00 +#define JIN_RELEASE "Jin 0.1.1" +#define JIN_VERSION_NUM 101 #define JIN_AUTHOR "Chai" -#endif
\ No newline at end of file +#endif // __JIN_H
\ No newline at end of file diff --git a/src/libjin/modules.h b/src/libjin/modules.h index b26c50d..34c1d11 100644 --- a/src/libjin/modules.h +++ b/src/libjin/modules.h @@ -1,10 +1,10 @@ #ifndef __JIN_COMMON_MODULES_H #define __JIN_COMMON_MODULES_H /* -* ģģı룬Ҫģ鲻 +* ģģı룬Ҫģرղ */ -#define JIN_MODULES_AUDIO 0 +#define JIN_MODULES_AUDIO 1 #define JIN_AUDIO_SDLAUDIO 1 #define JIN_AUDIO_OPENAL 1 @@ -15,6 +15,7 @@ #define JIN_MODULES_FILESYSTEM 1 #define JIN_MODULES_INPUT 1 +#define JIN_INPUT_SDL 1 #define JIN_MODULES_MATH 1 diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp index 6507691..dc2902e 100644 --- a/src/libjin/render/window.cpp +++ b/src/libjin/render/window.cpp @@ -6,7 +6,7 @@ #include "3rdparty/GLee/GLee.h" #include "canvas.h" #include "../utils/utils.h" -#include "../audio/sdl/audio.h" +#include "../audio/sdl/SDLAudio.h" #include "../utils/log.h" namespace jin diff --git a/src/lua/audio/luaopen_audio.cpp b/src/lua/audio/luaopen_audio.cpp index 992c3ec..73c5bea 100644 --- a/src/lua/audio/luaopen_audio.cpp +++ b/src/lua/audio/luaopen_audio.cpp @@ -1,5 +1,3 @@ -#include <SDL2/SDL.h> - #include "lua/luax.h" #include "libjin/jin.h" @@ -7,14 +5,19 @@ namespace jin { namespace lua { + using namespace jin::audio; + static int l_init(lua_State* L) { - if (SDL_Init(SDL_INIT_AUDIO) < 0) + SDLAudioSetting setting; + if (! SDLAudio::get()->init(&setting)) { luax_error(L, "could not init audio"); luax_pushboolean(L, false); return 1; } + luax_pushboolean(L, true); + return 1; } static int l_newSound(lua_State* L) diff --git a/src/lua/core/luaopen_core.cpp b/src/lua/core/luaopen_core.cpp index c8df1b6..0e79ff5 100644 --- a/src/lua/core/luaopen_core.cpp +++ b/src/lua/core/luaopen_core.cpp @@ -9,7 +9,8 @@ namespace lua static int l_running(lua_State* L) { - bool running = Game::get()->running(); + static Game* game = Game::get(); + bool running = game->running(); luax_pushboolean(L, running); return 1; } diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h index 21c1899..2a9cfc5 100644 --- a/src/lua/embed/boot.lua.h +++ b/src/lua/embed/boot.lua.h @@ -86,7 +86,9 @@ function jin.core.run() -- update if onUpdate then - onUpdate(dt) + -- while do + onUpdate(dt) + -- end end -- bind to default render buffer diff --git a/src/lua/event/luaopen_event.cpp b/src/lua/event/luaopen_event.cpp index 8a84c97..2d1e52f 100644 --- a/src/lua/event/luaopen_event.cpp +++ b/src/lua/event/luaopen_event.cpp @@ -1,8 +1,6 @@ /** * Event module */ -#include <SDl2/SDL.h> - #include "lua/luax.h" #include "libjin/jin.h" @@ -36,50 +34,50 @@ namespace lua { // table to store events luax_newtable(L); - SDL_Event e; + static Event e; int i = 1; poll: - while (SDL_PollEvent(&e)) + while (pollEvent(&e)) { // each event is a table luax_newtable(L); switch (e.type) { - case SDL_QUIT: + case EventType::QUIT: luax_setfield_string(L, "type", "quit"); break; - case SDL_KEYDOWN: + case EventType::KEYDOWN: luax_setfield_string(L, "type", "keydown"); luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); break; - case SDL_KEYUP: + case EventType::KEYUP: luax_setfield_string(L, "type", "keyup"); luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); break; - case SDL_MOUSEMOTION: + case EventType::MOUSEMOTION: luax_setfield_string(L, "type", "mousemotion"); luax_setfield_number(L, "x", e.motion.x); luax_setfield_number(L, "y", e.motion.y); break; - case SDL_MOUSEBUTTONDOWN: + case EventType::MOUSEBUTTONDOWN: luax_setfield_string(L, "type", "mousebuttondown"); luax_setfield_string(L, "button", buttonstr(e.button.button)); luax_setfield_number(L, "x", e.button.x); luax_setfield_number(L, "y", e.button.y); break; - case SDL_MOUSEBUTTONUP: + case EventType::MOUSEBUTTONUP: luax_setfield_string(L, "type", "mousebuttonup"); luax_setfield_string(L, "button", buttonstr(e.button.button)); luax_setfield_number(L, "x", e.button.x); luax_setfield_number(L, "y", e.button.y); break; - case SDL_MOUSEWHEEL: + case EventType::MOUSEWHEEL: luax_setfield_string(L, "type", "wheel"); if(e.wheel.x == -1) luax_setfield_string(L, "x", "left"); @@ -106,7 +104,7 @@ namespace lua } return 1; } - + static const luaL_Reg f[] = { {"poll", l_event_poll}, {0 ,0 } diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp index be40247..f0bae67 100644 --- a/src/lua/graphics/luaopen_graphics.cpp +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -33,8 +33,12 @@ namespace lua setting.height = luax_getfield_integer(L, 1, "height"); setting.title = luax_getfield_string(L, 1, "title"); setting.vsync = luax_getfield_bool(L, 1, "vsync"); - wnd->init(&setting); - luax_pushboolean(L, 1); + if (!wnd->init(&setting)) + { + luax_pushboolean(L, false); + return 1; + } + luax_pushboolean(L, true); return 1; } |