Update MiniEngine_Simple.hpp

This commit is contained in:
Kirigaya Kazuto 2017-02-23 16:51:13 +08:00 committed by GitHub
parent 7ceff5263d
commit 2f665cba59

View File

@ -1,16 +1,14 @@
#pragma once
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#undef main #undef main
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h> #include <SDL2/SDL_mixer.h>
#include <string>
#include <memory> #include <memory>
namespace MiniEngine namespace MiniEngine
{ {
using namespace std;
class Rect class Rect
{ {
@ -18,7 +16,10 @@ namespace MiniEngine
int x, y, w, h; int x, y, w, h;
Rect(int X, int Y, int W, int H) Rect(int X, int Y, int W, int H)
{ {
x = X;y = Y;w = W;h = H; x = X;
y = Y;
w = W;
h = H;
} }
Rect() Rect()
{ {
@ -27,7 +28,10 @@ namespace MiniEngine
SDL_Rect toSDLRect() SDL_Rect toSDLRect()
{ {
SDL_Rect r; SDL_Rect r;
r.x = x;r.y = y;r.w = w;r.h = h; r.x = x;
r.y = y;
r.w = w;
r.h = h;
return r; return r;
} }
}; };
@ -38,7 +42,8 @@ namespace MiniEngine
int x,y; int x,y;
Point(int X,int Y) Point(int X,int Y)
{ {
x=X;y=Y; x=X;
y=Y;
} }
Point() Point()
{ {
@ -65,7 +70,10 @@ namespace MiniEngine
int r, g, b, a; int r, g, b, a;
RGBA(int R, int G, int B, int A) RGBA(int R, int G, int B, int A)
{ {
r = R;g = G;b = B;a = A; r = R;
g = G;
b = B;
a = A;
} }
RGBA() RGBA()
{ {
@ -74,7 +82,10 @@ namespace MiniEngine
SDL_Color toSDLColor() SDL_Color toSDLColor()
{ {
SDL_Color c; SDL_Color c;
c.r = r;c.g = g;c.b = b;c.a = a; c.r = r;
c.g = g;
c.b = b;
c.a = a;
return c; return c;
} }
}; };
@ -88,17 +99,18 @@ namespace MiniEngine
NonCopyable& operator = (const NonCopyable&) = delete; NonCopyable& operator = (const NonCopyable&) = delete;
}; };
/// Forward Declaration
class Window; class Window;
class Renderer; class Renderer;
class ErrorViewer : public exception class ErrorViewer : public std::exception
{ {
public: public:
void fetch() void fetch()
{ {
str = SDL_GetError(); str = SDL_GetError();
} }
string getError() std::string getError()
{ {
return str; return str;
} }
@ -107,7 +119,7 @@ namespace MiniEngine
return str.c_str(); return str.c_str();
} }
private: private:
string str; std::string str;
}; };
class Surface class Surface
@ -120,7 +132,7 @@ namespace MiniEngine
protected: protected:
Surface() = default; Surface() = default;
private: private:
shared_ptr<SDL_Surface> surf; std::shared_ptr<SDL_Surface> surf;
friend class Renderer; friend class Renderer;
friend class Font; friend class Font;
}; };
@ -155,7 +167,7 @@ namespace MiniEngine
rect.x = rect.y = 0; rect.x = rect.y = 0;
} }
private: private:
shared_ptr<SDL_Texture> text; std::shared_ptr<SDL_Texture> text;
Rect rect; Rect rect;
friend class Renderer; friend class Renderer;
}; };
@ -165,7 +177,8 @@ namespace MiniEngine
{ {
public: public:
enum class Type { Software=SDL_RENDERER_SOFTWARE, Accelerated=SDL_RENDERER_ACCELERATED, enum class Type { Software=SDL_RENDERER_SOFTWARE, Accelerated=SDL_RENDERER_ACCELERATED,
PresentSync=SDL_RENDERER_PRESENTVSYNC, TargetTexture=SDL_RENDERER_TARGETTEXTURE }; PresentSync=SDL_RENDERER_PRESENTVSYNC, TargetTexture=SDL_RENDERER_TARGETTEXTURE
};
int setColor(RGBA pack) int setColor(RGBA pack)
{ {
@ -218,7 +231,7 @@ namespace MiniEngine
{ {
return SDL_RenderCopy(rnd.get(), t.text.get(), NULL, NULL); return SDL_RenderCopy(rnd.get(), t.text.get(), NULL, NULL);
} }
Surface loadSurface(string FileName) throw(ErrorViewer) Surface loadSurface(std::string FileName) throw(ErrorViewer)
{ {
Surface surf; Surface surf;
SDL_Surface* temp = IMG_Load(FileName.c_str()); SDL_Surface* temp = IMG_Load(FileName.c_str());
@ -245,7 +258,7 @@ namespace MiniEngine
t.updateInfo(); t.updateInfo();
return t; return t;
} }
Texture loadTexture(string FileName) throw(ErrorViewer) Texture loadTexture(std::string FileName) throw(ErrorViewer)
{ {
Texture t; Texture t;
SDL_Texture* temp = IMG_LoadTexture(rnd.get(), FileName.c_str()); SDL_Texture* temp = IMG_LoadTexture(rnd.get(), FileName.c_str());
@ -266,14 +279,14 @@ namespace MiniEngine
/// This function is called by Window ONLY. /// This function is called by Window ONLY.
} }
private: private:
shared_ptr<SDL_Renderer> rnd; std::shared_ptr<SDL_Renderer> rnd;
friend class Window; friend class Window;
}; };
class Window class Window
{ {
public: public:
Window(string Title, int Width, int Height) throw(ErrorViewer) Window(std::string Title, int Width, int Height) throw(ErrorViewer)
{ {
SDL_Window* temp = SDL_CreateWindow(Title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_WINDOW_SHOWN); SDL_Window* temp = SDL_CreateWindow(Title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_WINDOW_SHOWN);
if (temp == NULL) if (temp == NULL)
@ -311,12 +324,17 @@ namespace MiniEngine
{ {
SDL_SetWindowSize(wnd.get(),w,h); SDL_SetWindowSize(wnd.get(),w,h);
} }
void setTitle(std::string Title)
{
}
private: private:
void _setRenderer_Real(Uint32 flags) void _setRenderer_Real(Uint32 flags)
{ {
winrnd.rnd.reset(SDL_CreateRenderer(wnd.get(), -1, flags),SDL_DestroyRenderer); winrnd.rnd.reset(SDL_CreateRenderer(wnd.get(), -1, flags),SDL_DestroyRenderer);
} }
shared_ptr<SDL_Window> wnd; std::shared_ptr<SDL_Window> wnd;
Renderer winrnd; Renderer winrnd;
}; };
@ -327,7 +345,7 @@ namespace MiniEngine
{ {
} }
Font(string FontFileName, int size) throw(ErrorViewer) Font(std::string FontFileName, int size) throw(ErrorViewer)
{ {
if (use(FontFileName, size) != 0) if (use(FontFileName, size) != 0)
{ {
@ -336,14 +354,14 @@ namespace MiniEngine
throw e; throw e;
} }
} }
int use(string FontFileName, int size) int use(std::string FontFileName, int size)
{ {
TTF_Font* temp = TTF_OpenFont(FontFileName.c_str(), size); TTF_Font* temp = TTF_OpenFont(FontFileName.c_str(), size);
if (temp == NULL) return -1; if (temp == NULL) return -1;
font.reset(temp, TTF_CloseFont); font.reset(temp, TTF_CloseFont);
return 0; return 0;
} }
Texture renderText(Renderer rnd, string Text, RGBA fg) Texture renderText(Renderer rnd, std::string Text, RGBA fg)
{ {
Surface surf; Surface surf;
surf.surf.reset(TTF_RenderText_Blended(font.get(), Text.c_str(), fg.toSDLColor())); surf.surf.reset(TTF_RenderText_Blended(font.get(), Text.c_str(), fg.toSDLColor()));
@ -351,7 +369,7 @@ namespace MiniEngine
return t; return t;
} }
Texture renderUTF8(Renderer rnd, string Text, RGBA fg) Texture renderUTF8(Renderer rnd, std::string Text, RGBA fg)
{ {
Surface surf; Surface surf;
surf.surf.reset(TTF_RenderUTF8_Blended(font.get(), Text.c_str(), fg.toSDLColor())); surf.surf.reset(TTF_RenderUTF8_Blended(font.get(), Text.c_str(), fg.toSDLColor()));
@ -359,7 +377,7 @@ namespace MiniEngine
return t; return t;
} }
private: private:
shared_ptr<TTF_Font> font; std::shared_ptr<TTF_Font> font;
}; };
class SDLSystem class SDLSystem
@ -413,12 +431,28 @@ namespace MiniEngine
SDLQuit(); SDLQuit();
} }
static void delay(int ms) static void Delay(int ms)
{ {
SDL_Delay(ms); SDL_Delay(ms);
} }
}; };
}/// End of namespace MiniEngine }/// End of namespace MiniEngine
namespace App
{
int main(int argc,char* argv[]);
}/// End of namespace App
/// Default Setup Code
int main(int argc,char* argv[])
{
MiniEngine::SDLSystem::Init();
int ret=App::main(argc,argv);
MiniEngine::SDLSystem::Quit();
return ret;
}