mirror of
https://github.com/Kiritow/MiniEngine.git
synced 2024-03-22 13:11:22 +08:00
Update MiniEngine_Simple.hpp
This commit is contained in:
parent
7ceff5263d
commit
2f665cba59
|
@ -1,24 +1,25 @@
|
||||||
#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
|
{
|
||||||
{
|
public:
|
||||||
public:
|
|
||||||
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,18 +28,22 @@ 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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Point
|
class Point
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
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()
|
||||||
{
|
{
|
||||||
|
@ -57,15 +62,18 @@ namespace MiniEngine
|
||||||
auto r=rect.toSDLRect();
|
auto r=rect.toSDLRect();
|
||||||
return SDL_PointInRect(&p,&r);
|
return SDL_PointInRect(&p,&r);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class RGBA
|
class RGBA
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
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,31 +82,35 @@ 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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class NonCopyable
|
class NonCopyable
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
NonCopyable() = default;
|
NonCopyable() = default;
|
||||||
~NonCopyable() = default;
|
~NonCopyable() = default;
|
||||||
NonCopyable(const NonCopyable&) = delete;
|
NonCopyable(const NonCopyable&) = delete;
|
||||||
NonCopyable& operator = (const NonCopyable&) = delete;
|
NonCopyable& operator = (const NonCopyable&) = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Window;
|
/// Forward Declaration
|
||||||
class Renderer;
|
class Window;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
@ -106,28 +118,28 @@ namespace MiniEngine
|
||||||
{
|
{
|
||||||
return str.c_str();
|
return str.c_str();
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
string str;
|
std::string str;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Surface
|
class Surface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
~Surface()
|
~Surface()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Texture
|
class Texture
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
~Texture()
|
~Texture()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -144,7 +156,7 @@ namespace MiniEngine
|
||||||
{
|
{
|
||||||
return rect.h;
|
return rect.h;
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
Texture()
|
Texture()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -154,18 +166,19 @@ namespace MiniEngine
|
||||||
SDL_QueryTexture(text.get(), NULL, NULL, &rect.w, &rect.h);
|
SDL_QueryTexture(text.get(), NULL, NULL, &rect.w, &rect.h);
|
||||||
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class Renderer
|
class Renderer
|
||||||
{
|
{
|
||||||
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());
|
||||||
|
@ -260,20 +273,20 @@ namespace MiniEngine
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Renderer()
|
Renderer()
|
||||||
{
|
{
|
||||||
/// 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,23 +324,28 @@ namespace MiniEngine
|
||||||
{
|
{
|
||||||
SDL_SetWindowSize(wnd.get(),w,h);
|
SDL_SetWindowSize(wnd.get(),w,h);
|
||||||
}
|
}
|
||||||
private:
|
|
||||||
|
void setTitle(std::string Title)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Font
|
class Font
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Font()
|
Font()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
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,20 +369,20 @@ 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()));
|
||||||
Texture t = rnd.render(surf);
|
Texture t = rnd.render(surf);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
shared_ptr<TTF_Font> font;
|
std::shared_ptr<TTF_Font> font;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SDLSystem
|
class SDLSystem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static int SDLInit()
|
static int SDLInit()
|
||||||
{
|
{
|
||||||
return SDL_Init(SDL_INIT_EVERYTHING);
|
return SDL_Init(SDL_INIT_EVERYTHING);
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user