diff --git a/SDLWrapper/SDLSystem.cpp b/SDLWrapper/SDLSystem.cpp index 738cfc6..b84d86e 100644 --- a/SDLWrapper/SDLSystem.cpp +++ b/SDLWrapper/SDLSystem.cpp @@ -1,69 +1,73 @@ #include "SDLSystem.h" +#include "_caster.h" #include "begin_code.h" -//static -int SDLSystem::SDLInit() +SDLSystem::SDLSystem(const std::initializer_list& flag_sdl, + const std::initializer_list& flag_img, + const std::initializer_list& flag_mix, + bool init_ttf ) throw (ErrorViewer) { - return SDL_Init(SDL_INIT_EVERYTHING); + Uint32 sdl_flag=0; + for(auto& v:flag_sdl) + { + sdl_flag |= _internal::getUint32FromSDLInitFlag(v); + } + int img_flag=0; + for(auto& v:flag_img) + { + img_flag |= _internal::getIntFromIMGInitFlag(v); + } + int mix_flag=0; + for(auto& v:flag_mix) + { + mix_flag |= _internal::getIntFromMixInitFlag(v); + } + + int ret=SDL_Init(sdl_flag); + if(ret!=0) + { + ErrorViewer e; + e.fetch(); + throw e; + } + + ret=IMG_Init(img_flag); + if(ret!=0) + { + ErrorViewer e; + e.fetch(); + throw e; + } + + ret=Mix_Init(mix_flag); + if(ret!=0) + { + ErrorViewer e; + e.fetch(); + throw e; + } + + if(init_ttf) + { + ret=TTF_Init(); + if(ret!=0) + { + ErrorViewer e; + e.fetch(); + throw e; + } + } } -//static -void SDLSystem::SDLQuit() +SDLSystem::~SDLSystem() { - SDL_Quit(); -} + if(TTF_WasInit()) + { + TTF_Quit(); + } -//static -int SDLSystem::IMGInit() -{ - return IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG); -} - -//static -void SDLSystem::IMGQuit() -{ - IMG_Quit(); -} - -//static -int SDLSystem::TTFInit() -{ - return TTF_Init(); -} - -//static -void SDLSystem::TTFQuit() -{ - TTF_Quit(); -} - -//static -int SDLSystem::MixInit() -{ - return Mix_Init(MIX_INIT_MP3); -} - -//static -void SDLSystem::MixQuit() -{ Mix_Quit(); -} - -//static -void SDLSystem::Init() -{ - SDLInit(); - IMGInit(); - TTFInit(); - MixInit(); -} - -//static -void SDLSystem::Quit() -{ - MixQuit(); - TTFQuit(); - IMGQuit(); - SDLQuit(); + IMG_Quit(); + SDL_Quit(); } //static diff --git a/SDLWrapper/SDLSystem.h b/SDLWrapper/SDLSystem.h index 4a5422f..79d704b 100644 --- a/SDLWrapper/SDLSystem.h +++ b/SDLWrapper/SDLSystem.h @@ -2,23 +2,24 @@ #include "include.h" #include "_PowerState.h" #include "_Platform.h" +#include "_SDLInitFlag.h" +#include "_IMGInitFlag.h" +#include "_MixInitFlag.h" +#include "__Noncopyable.h" +#include "__Nonmoveable.h" +#include "ErrorViewer.h" #include #include #include "begin_code.h" -class SDLSystem + +class SDLSystem : public NonCopyable, public NonMoveable { public: - static int SDLInit(); - static void SDLQuit(); - static int IMGInit(); - static void IMGQuit(); - static int TTFInit(); - static void TTFQuit(); - static int MixInit(); - static void MixQuit(); - - static void Init(); - static void Quit(); + SDLSystem(const std::initializer_list& flag_sdl = {SDLInitFlag::All} , + const std::initializer_list& flag_img = {IMGInitFlag::JPG,IMGInitFlag::PNG} , + const std::initializer_list& flag_mix = {MixInitFlag::MP3} , + bool init_ttf = true ) throw (ErrorViewer); + ~SDLSystem(); static void Delay(int ms); diff --git a/SDLWrapper/_IMGInitFlag.h b/SDLWrapper/_IMGInitFlag.h new file mode 100644 index 0000000..ed9085e --- /dev/null +++ b/SDLWrapper/_IMGInitFlag.h @@ -0,0 +1,11 @@ +#pragma once +#include "begin_code.h" +enum class IMGInitFlag +{ + JPG, + PNG, + TIF, + WEBP, + ALL +}; +#include "end_code.h" diff --git a/SDLWrapper/_MixInitFlag.h b/SDLWrapper/_MixInitFlag.h new file mode 100644 index 0000000..8f70ef6 --- /dev/null +++ b/SDLWrapper/_MixInitFlag.h @@ -0,0 +1,12 @@ +#pragma once +#include "begin_code.h" +enum class MixInitFlag +{ + FLAC, + MOD, + MODPLUG, + MP3, + OGG, + FLUIDSYNTH +}; +#include "end_code.h" diff --git a/SDLWrapper/_SDLInitFlag.h b/SDLWrapper/_SDLInitFlag.h new file mode 100644 index 0000000..dfa67df --- /dev/null +++ b/SDLWrapper/_SDLInitFlag.h @@ -0,0 +1,14 @@ +#pragma once +#include "begin_code.h" +enum class SDLInitFlag +{ + Timer, + Audio, + Video, /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */ + Joystick, /**< SDL_INIT_JOYSTICK implies SDL_INIT_EVENTS */ + Haptic, + GameController, /**< SDL_INIT_GAMECONTROLLER implies SDL_INIT_JOYSTICK */ + Events, + All +}; +#include "end_code.h" diff --git a/SDLWrapper/__Nonmoveable.h b/SDLWrapper/__Nonmoveable.h index 6a6cce1..0d5bca4 100644 --- a/SDLWrapper/__Nonmoveable.h +++ b/SDLWrapper/__Nonmoveable.h @@ -3,6 +3,8 @@ class NonMoveable { public: + NonMoveable()=default; + ~NonMoveable()=default; NonMoveable(NonMoveable&&) =delete; NonMoveable& operator = (NonMoveable&&)=delete; }; diff --git a/SDLWrapper/_caster.cpp b/SDLWrapper/_caster.cpp index df00204..dfa1194 100644 --- a/SDLWrapper/_caster.cpp +++ b/SDLWrapper/_caster.cpp @@ -215,5 +215,66 @@ SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode) return SDL_FLIP_NONE; } } + +Uint32 getUint32FromSDLInitFlag(SDLInitFlag flag) +{ + switch(flag) + { + case SDLInitFlag::Timer: + return SDL_INIT_TIMER; + case SDLInitFlag::Audio: + return SDL_INIT_AUDIO; + case SDLInitFlag::Video: + return SDL_INIT_VIDEO; + case SDLInitFlag::Joystick: + return SDL_INIT_JOYSTICK; + case SDLInitFlag::Haptic: + return SDL_INIT_HAPTIC; + case SDLInitFlag::GameController: + return SDL_INIT_GAMECONTROLLER; + case SDLInitFlag::Events: + return SDL_INIT_EVENTS; + case SDLInitFlag::All: + return SDL_INIT_EVERYTHING; + } +} + + +int getIntFromIMGInitFlag(IMGInitFlag flag) +{ + switch(flag) + { + case IMGInitFlag::JPG: + return IMG_INIT_JPG; + case IMGInitFlag::PNG: + return IMG_INIT_PNG; + case IMGInitFlag::TIF: + return IMG_INIT_TIF; + case IMGInitFlag::WEBP: + return IMG_INIT_WEBP; + case IMGInitFlag::ALL: + return ( IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF | IMG_INIT_WEBP ); + } +} + +int getIntFromMixInitFlag(MixInitFlag flag) +{ + switch(flag) + { + case MixInitFlag::FLAC: + return MIX_INIT_FLAC; + case MixInitFlag::MOD: + return MIX_INIT_MOD; + case MixInitFlag::MODPLUG: + return MIX_INIT_MODPLUG; + case MixInitFlag::MP3: + return MIX_INIT_MP3; + case MixInitFlag::OGG: + return MIX_INIT_OGG; + case MixInitFlag::FLUIDSYNTH: + return MIX_INIT_FLUIDSYNTH; + } +} + }/// End of namespace _internal #include "end_code.h" diff --git a/SDLWrapper/_caster.h b/SDLWrapper/_caster.h index 646fa35..171c403 100644 --- a/SDLWrapper/_caster.h +++ b/SDLWrapper/_caster.h @@ -5,6 +5,9 @@ #include "_SystemCursorType.h" #include "_FontStyle.h" #include "_FlipMode.h" +#include "_SDLInitFlag.h" +#include "_IMGInitFlag.h" +#include "_MixInitFlag.h" #include #include "begin_code.h" namespace _internal @@ -18,5 +21,8 @@ SDL_SystemCursor getSDLSystemCursorFromSystemCursorType(SystemCursorType type); int getTTFFontStyleFromFontStyle(FontStyle style); std::vector getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style); SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode); +Uint32 getUint32FromSDLInitFlag(SDLInitFlag flag); +int getIntFromIMGInitFlag(IMGInitFlag flag); +int getIntFromMixInitFlag(MixInitFlag flag); }/// End of namespace _internal #include "end_code.h"