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>
#undef main
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <string>
#include <memory>
namespace MiniEngine
{
using namespace std;
class Rect
{
@ -18,7 +16,10 @@ namespace MiniEngine
int x, y, w, 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()
{
@ -27,7 +28,10 @@ namespace MiniEngine
SDL_Rect toSDLRect()
{
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;
}
};
@ -38,7 +42,8 @@ namespace MiniEngine
int x,y;
Point(int X,int Y)
{
x=X;y=Y;
x=X;
y=Y;
}
Point()
{
@ -65,7 +70,10 @@ namespace MiniEngine
int r, g, b, 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()
{
@ -74,7 +82,10 @@ namespace MiniEngine
SDL_Color toSDLColor()
{
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;
}
};
@ -88,17 +99,18 @@ namespace MiniEngine
NonCopyable& operator = (const NonCopyable&) = delete;
};
/// Forward Declaration
class Window;
class Renderer;
class ErrorViewer : public exception
class ErrorViewer : public std::exception
{
public:
void fetch()
{
str = SDL_GetError();
}
string getError()
std::string getError()
{
return str;
}
@ -107,7 +119,7 @@ namespace MiniEngine
return str.c_str();
}
private:
string str;
std::string str;
};
class Surface
@ -120,7 +132,7 @@ namespace MiniEngine
protected:
Surface() = default;
private:
shared_ptr<SDL_Surface> surf;
std::shared_ptr<SDL_Surface> surf;
friend class Renderer;
friend class Font;
};
@ -155,7 +167,7 @@ namespace MiniEngine
rect.x = rect.y = 0;
}
private:
shared_ptr<SDL_Texture> text;
std::shared_ptr<SDL_Texture> text;
Rect rect;
friend class Renderer;
};
@ -165,7 +177,8 @@ namespace MiniEngine
{
public:
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)
{
@ -218,7 +231,7 @@ namespace MiniEngine
{
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;
SDL_Surface* temp = IMG_Load(FileName.c_str());
@ -245,7 +258,7 @@ namespace MiniEngine
t.updateInfo();
return t;
}
Texture loadTexture(string FileName) throw(ErrorViewer)
Texture loadTexture(std::string FileName) throw(ErrorViewer)
{
Texture t;
SDL_Texture* temp = IMG_LoadTexture(rnd.get(), FileName.c_str());
@ -266,14 +279,14 @@ namespace MiniEngine
/// This function is called by Window ONLY.
}
private:
shared_ptr<SDL_Renderer> rnd;
std::shared_ptr<SDL_Renderer> rnd;
friend class Window;
};
class Window
{
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);
if (temp == NULL)
@ -311,12 +324,17 @@ namespace MiniEngine
{
SDL_SetWindowSize(wnd.get(),w,h);
}
void setTitle(std::string Title)
{
}
private:
void _setRenderer_Real(Uint32 flags)
{
winrnd.rnd.reset(SDL_CreateRenderer(wnd.get(), -1, flags),SDL_DestroyRenderer);
}
shared_ptr<SDL_Window> wnd;
std::shared_ptr<SDL_Window> wnd;
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)
{
@ -336,14 +354,14 @@ namespace MiniEngine
throw e;
}
}
int use(string FontFileName, int size)
int use(std::string FontFileName, int size)
{
TTF_Font* temp = TTF_OpenFont(FontFileName.c_str(), size);
if (temp == NULL) return -1;
font.reset(temp, TTF_CloseFont);
return 0;
}
Texture renderText(Renderer rnd, string Text, RGBA fg)
Texture renderText(Renderer rnd, std::string Text, RGBA fg)
{
Surface surf;
surf.surf.reset(TTF_RenderText_Blended(font.get(), Text.c_str(), fg.toSDLColor()));
@ -351,7 +369,7 @@ namespace MiniEngine
return t;
}
Texture renderUTF8(Renderer rnd, string Text, RGBA fg)
Texture renderUTF8(Renderer rnd, std::string Text, RGBA fg)
{
Surface surf;
surf.surf.reset(TTF_RenderUTF8_Blended(font.get(), Text.c_str(), fg.toSDLColor()));
@ -359,7 +377,7 @@ namespace MiniEngine
return t;
}
private:
shared_ptr<TTF_Font> font;
std::shared_ptr<TTF_Font> font;
};
class SDLSystem
@ -413,12 +431,28 @@ namespace MiniEngine
SDLQuit();
}
static void delay(int ms)
static void Delay(int ms)
{
SDL_Delay(ms);
}
};
}/// 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;
}