MiniEngine/sdl_engine.cpp

142 lines
2.7 KiB
C++
Raw Normal View History

2017-01-03 23:32:35 +08:00
#include "sdl_engine.h"
#include "unistd.h"
2017-01-04 09:50:16 +08:00
#include <memory>
using namespace std;
2017-01-03 23:32:35 +08:00
SDL_Texture* RenderUTF8(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph)
{
SDL_Surface* surf=TTF_RenderUTF8_Blended(font,Text,color);
if(surf==NULL) return NULL;
SDL_Texture* texture=SDL_CreateTextureFromSurface(rnd,surf);
SDL_FreeSurface(surf);
if(pw&&ph) SDL_QueryTexture(texture,NULL,NULL,pw,ph);
return texture;
}
bool isInRect(int x,int y,SDL_Rect rect)
{
return ((x>=rect.x&&x<=rect.x+rect.w)&&(y>=rect.y&&y<=rect.y+rect.h));
}
bool isInRect(int x,int y,int LU_x,int LU_y,int RD_x,int RD_y)
{
return ((x>=LU_x&&x<=RD_x)&&(y>=LU_y&&y<=RD_y));
}
void ClearMessageQueue()
{
/// Clear Message Queue
while(SDL_PeepEvents(NULL,1,SDL_GETEVENT,SDL_FIRSTEVENT,SDL_LASTEVENT));
}
SDL_Color color_white { 255,255,255 };
SDL_Color color_black { 0,0,0 };
int MyChangeDir(const char* DirName)
{
mlog("Change Dir to \"%s\"",DirName);
int ret=chdir(DirName);
mlog("Change Dir returns %d",ret);
return ret;
}
2017-01-04 09:50:16 +08:00
namespace Engine
{
2017-01-05 13:25:09 +08:00
/// Rect
#include "sdl_engine_rect.hpp"
2017-01-04 09:50:16 +08:00
2017-01-07 09:52:56 +08:00
struct Renderer::impl
2017-01-04 09:50:16 +08:00
{
2017-01-07 09:52:56 +08:00
private:
friend class Renderer;
shared_ptr<SDL_Renderer> sRnd;
public:
void set(SDL_Renderer* rnd)
{
sRnd.reset(rnd,SDL_DestroyRenderer);
}
2017-01-04 09:50:16 +08:00
};
2017-01-07 09:52:56 +08:00
struct Window::impl
2017-01-04 09:50:16 +08:00
{
2017-01-07 09:52:56 +08:00
private:
friend class Window;
shared_ptr<SDL_Window> sWnd;
Renderer rnd;
public:
void set(SDL_Window* wnd)
{
sWnd.reset(wnd,SDL_DestroyWindow);
rnd.pimpl->set(SDL_CreateRenderer(wnd,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE));
}
SDL_Window* getRawWindow()
{
return sWnd.get();
}
2017-01-04 09:50:16 +08:00
};
struct Texture::impl
{
2017-01-07 09:52:56 +08:00
private:
friend class Texture;
2017-01-04 09:50:16 +08:00
shared_ptr<SDL_Texture> sText;
int w,h;
2017-01-07 09:52:56 +08:00
public:
void set(SDL_Texture* text)
{
sText.reset(text,SDL_DestroyTexture);
SDL_QueryTexture(text,NULL,NULL,&w,&h);
}
SDL_Texture* getRawTexture()
{
return sText.get();
}
2017-01-04 09:50:16 +08:00
};
2017-01-05 13:25:09 +08:00
struct Surface::impl
2017-01-04 09:50:16 +08:00
{
2017-01-07 09:52:56 +08:00
private:
friend class Surface;
2017-01-05 13:25:09 +08:00
shared_ptr<SDL_Surface> sSurf;
2017-01-07 09:52:56 +08:00
public:
void set(SDL_Surface* surf)
{
sSurf.reset(surf,SDL_FreeSurface);
}
SDL_Surface* getRawSurface()
{
return sSurf.get();
}
2017-01-05 13:25:09 +08:00
};
2017-01-04 09:50:16 +08:00
struct Font::impl
{
2017-01-07 09:52:56 +08:00
private:
friend class Font;
2017-01-04 09:50:16 +08:00
shared_ptr<TTF_Font> sTTF;
2017-01-07 09:52:56 +08:00
public:
void set(TTF_Font* font)
{
sTTF.reset(font,TTF_CloseFont);
}
2017-01-04 09:50:16 +08:00
};
2017-01-05 13:25:09 +08:00
/// Window
#include "sdl_engine_window.hpp"
/// Renderer
#include "sdl_engine_renderer.hpp"
/// Surface
#include "sdl_engine_surface.hpp"
/// Texture
#include "sdl_engine_texture.hpp"
/// RGBA
#include "sdl_engine_rgba.hpp"
/// Font
#include "sdl_engine_font.hpp"
2017-01-04 09:50:16 +08:00
}/// End of namespace Engine