Move SDL,IMG,Mix,TTF Init to SDLSystem

This commit is contained in:
Kirigaya Kazuto 2017-07-01 15:26:52 +08:00
parent b6f7e70cdd
commit 5eb66f6d0d
8 changed files with 181 additions and 70 deletions

View File

@ -1,69 +1,73 @@
#include "SDLSystem.h" #include "SDLSystem.h"
#include "_caster.h"
#include "begin_code.h" #include "begin_code.h"
//static SDLSystem::SDLSystem(const std::initializer_list<SDLInitFlag>& flag_sdl,
int SDLSystem::SDLInit() const std::initializer_list<IMGInitFlag>& flag_img,
const std::initializer_list<MixInitFlag>& 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 SDLSystem::~SDLSystem()
void SDLSystem::SDLQuit()
{ {
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(); Mix_Quit();
} IMG_Quit();
SDL_Quit();
//static
void SDLSystem::Init()
{
SDLInit();
IMGInit();
TTFInit();
MixInit();
}
//static
void SDLSystem::Quit()
{
MixQuit();
TTFQuit();
IMGQuit();
SDLQuit();
} }
//static //static

View File

@ -2,23 +2,24 @@
#include "include.h" #include "include.h"
#include "_PowerState.h" #include "_PowerState.h"
#include "_Platform.h" #include "_Platform.h"
#include "_SDLInitFlag.h"
#include "_IMGInitFlag.h"
#include "_MixInitFlag.h"
#include "__Noncopyable.h"
#include "__Nonmoveable.h"
#include "ErrorViewer.h"
#include <tuple> #include <tuple>
#include <string> #include <string>
#include "begin_code.h" #include "begin_code.h"
class SDLSystem
class SDLSystem : public NonCopyable, public NonMoveable
{ {
public: public:
static int SDLInit(); SDLSystem(const std::initializer_list<SDLInitFlag>& flag_sdl = {SDLInitFlag::All} ,
static void SDLQuit(); const std::initializer_list<IMGInitFlag>& flag_img = {IMGInitFlag::JPG,IMGInitFlag::PNG} ,
static int IMGInit(); const std::initializer_list<MixInitFlag>& flag_mix = {MixInitFlag::MP3} ,
static void IMGQuit(); bool init_ttf = true ) throw (ErrorViewer);
static int TTFInit(); ~SDLSystem();
static void TTFQuit();
static int MixInit();
static void MixQuit();
static void Init();
static void Quit();
static void Delay(int ms); static void Delay(int ms);

11
SDLWrapper/_IMGInitFlag.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "begin_code.h"
enum class IMGInitFlag
{
JPG,
PNG,
TIF,
WEBP,
ALL
};
#include "end_code.h"

12
SDLWrapper/_MixInitFlag.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include "begin_code.h"
enum class MixInitFlag
{
FLAC,
MOD,
MODPLUG,
MP3,
OGG,
FLUIDSYNTH
};
#include "end_code.h"

14
SDLWrapper/_SDLInitFlag.h Normal file
View File

@ -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"

View File

@ -3,6 +3,8 @@
class NonMoveable class NonMoveable
{ {
public: public:
NonMoveable()=default;
~NonMoveable()=default;
NonMoveable(NonMoveable&&) =delete; NonMoveable(NonMoveable&&) =delete;
NonMoveable& operator = (NonMoveable&&)=delete; NonMoveable& operator = (NonMoveable&&)=delete;
}; };

View File

@ -215,5 +215,66 @@ SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode)
return SDL_FLIP_NONE; 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 }/// End of namespace _internal
#include "end_code.h" #include "end_code.h"

View File

@ -5,6 +5,9 @@
#include "_SystemCursorType.h" #include "_SystemCursorType.h"
#include "_FontStyle.h" #include "_FontStyle.h"
#include "_FlipMode.h" #include "_FlipMode.h"
#include "_SDLInitFlag.h"
#include "_IMGInitFlag.h"
#include "_MixInitFlag.h"
#include <vector> #include <vector>
#include "begin_code.h" #include "begin_code.h"
namespace _internal namespace _internal
@ -18,5 +21,8 @@ SDL_SystemCursor getSDLSystemCursorFromSystemCursorType(SystemCursorType type);
int getTTFFontStyleFromFontStyle(FontStyle style); int getTTFFontStyleFromFontStyle(FontStyle style);
std::vector<FontStyle> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style); std::vector<FontStyle> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style);
SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode); SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode);
Uint32 getUint32FromSDLInitFlag(SDLInitFlag flag);
int getIntFromIMGInitFlag(IMGInitFlag flag);
int getIntFromMixInitFlag(MixInitFlag flag);
}/// End of namespace _internal }/// End of namespace _internal
#include "end_code.h" #include "end_code.h"