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,24 +1,25 @@
#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
{
public:
class Rect
{
public:
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,18 +28,22 @@ 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;
}
};
};
class Point
{
public:
class Point
{
public:
int x,y;
Point(int X,int Y)
{
x=X;y=Y;
x=X;
y=Y;
}
Point()
{
@ -57,15 +62,18 @@ namespace MiniEngine
auto r=rect.toSDLRect();
return SDL_PointInRect(&p,&r);
}
};
};
class RGBA
{
public:
class RGBA
{
public:
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,31 +82,35 @@ 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;
}
};
};
class NonCopyable
{
protected:
class NonCopyable
{
protected:
NonCopyable() = default;
~NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator = (const NonCopyable&) = delete;
};
};
class Window;
class Renderer;
/// Forward Declaration
class Window;
class Renderer;
class ErrorViewer : public exception
{
public:
class ErrorViewer : public std::exception
{
public:
void fetch()
{
str = SDL_GetError();
}
string getError()
std::string getError()
{
return str;
}
@ -106,28 +118,28 @@ namespace MiniEngine
{
return str.c_str();
}
private:
string str;
};
private:
std::string str;
};
class Surface
{
public:
class Surface
{
public:
~Surface()
{
}
protected:
protected:
Surface() = default;
private:
shared_ptr<SDL_Surface> surf;
private:
std::shared_ptr<SDL_Surface> surf;
friend class Renderer;
friend class Font;
};
};
class Texture
{
public:
class Texture
{
public:
~Texture()
{
@ -144,7 +156,7 @@ namespace MiniEngine
{
return rect.h;
}
protected:
protected:
Texture()
{
@ -154,18 +166,19 @@ namespace MiniEngine
SDL_QueryTexture(text.get(), NULL, NULL, &rect.w, &rect.h);
rect.x = rect.y = 0;
}
private:
shared_ptr<SDL_Texture> text;
private:
std::shared_ptr<SDL_Texture> text;
Rect rect;
friend class Renderer;
};
};
class Renderer
{
public:
class Renderer
{
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());
@ -260,20 +273,20 @@ namespace MiniEngine
return t;
}
protected:
protected:
Renderer()
{
/// This function is called by Window ONLY.
}
private:
shared_ptr<SDL_Renderer> rnd;
private:
std::shared_ptr<SDL_Renderer> rnd;
friend class Window;
};
};
class Window
{
public:
Window(string Title, int Width, int Height) throw(ErrorViewer)
class Window
{
public:
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,23 +324,28 @@ namespace MiniEngine
{
SDL_SetWindowSize(wnd.get(),w,h);
}
private:
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;
};
};
class Font
{
public:
class Font
{
public:
Font()
{
}
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,20 +369,20 @@ 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()));
Texture t = rnd.render(surf);
return t;
}
private:
shared_ptr<TTF_Font> font;
};
private:
std::shared_ptr<TTF_Font> font;
};
class SDLSystem
{
public:
class SDLSystem
{
public:
static int SDLInit()
{
return SDL_Init(SDL_INIT_EVERYTHING);
@ -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;
}