SDL Engine Update

This commit is contained in:
Kirigaya Kazuto 2017-01-04 09:50:16 +08:00
parent df0695a9d8
commit 57f0e87542
8 changed files with 315 additions and 125 deletions

10
App.cpp
View File

@ -1,10 +1,18 @@
#include "App.h"
using namespace Engine;
namespace App
{
/// Application Main Method
void Main()
{
mlog("In App::Main()");
Window wnd(1024,768);
Renderer rnd=wnd.getRenderer();
Texture text=rnd.loadImage("sample.jpg");
rnd.clear();
rnd.copyFullFill(text);
rnd.update();
SDL_Delay(3000);
}
}

View File

@ -1,83 +0,0 @@
#include "WindowManager.h"
WindowManager* default_WindowManager=nullptr;
struct WindowManager::impl
{
SDL_Renderer* rnd;
SDL_Window* wnd;
int winw,winh;
};
WindowManager::WindowManager()
{
pimpl=new impl;
Init();
}
WindowManager* WindowManager::getDefaultInstance() /// static
{
if(default_WindowManager) return default_WindowManager;
default_WindowManager=new WindowManager;
return default_WindowManager;
}
int WindowManager::releaseDefaultInstance() /// static
{
if(!default_WindowManager) return -1;
delete default_WindowManager;
default_WindowManager=nullptr;
return 0;
}
void WindowManager::Init()
{
mlog("Called WindowSizeManager::Init()");
if(pimpl->wnd!=NULL) return;
pimpl->wnd=SDL_CreateWindow("MiniEngine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,1024,768,SDL_WINDOW_SHOWN);
updateSize();
pimpl->rnd=SDL_CreateRenderer(pimpl->wnd,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE);
}
void WindowManager::Quit()
{
mlog("Called WindowSizeManager::Quit()");
if(pimpl->rnd==NULL) return;
SDL_DestroyRenderer(pimpl->rnd);
pimpl->rnd=nullptr;
SDL_DestroyWindow(pimpl->wnd);
pimpl->wnd=nullptr;
pimpl->winw=pimpl->winh=0;
}
WindowManager::~WindowManager()
{
Quit();
delete pimpl;
}
int WindowManager::getwinw()
{
return pimpl->winw;
}
int WindowManager::getwinh()
{
return pimpl->winh;
}
SDL_Window* WindowManager::getWindow()
{
return pimpl->wnd;
}
SDL_Renderer* WindowManager::getRender()
{
return pimpl->rnd;
}
void WindowManager::updateSize()
{
SDL_GetWindowSize(pimpl->wnd,&pimpl->winw,&pimpl->winh);
}

View File

@ -1,23 +0,0 @@
#pragma once
#include "config.h"
class WindowManager
{
public:
WindowManager();
~WindowManager();
int getwinw();
int getwinh();
SDL_Renderer* getRender();
SDL_Window* getWindow();
void updateSize();
static WindowManager* getDefaultInstance();
static int releaseDefaultInstance();
protected:
void Init();
void Quit();
private:
struct impl;
impl* pimpl;
};

View File

@ -2,6 +2,7 @@
#include "basic_config.h"
#include "sdl_engine.h"
namespace Global
{
void ErrorQuit(const char* ErrorMessage);

View File

@ -3,11 +3,11 @@
int main()
{
/// Initialize SDL2...
AllInit();
/// Create The Default Window Instance
WindowManager::getDefaultInstance();
/// Call Application Main
App::Main();
WindowManager::releaseDefaultInstance();
/// Clean Up SDL2
AllQuit();
return 0;
}

View File

@ -6,9 +6,6 @@
/// InitManager
#include "InitManager.h"
/// Window Manager
#include "WindowManager.h"
/******************************************************************************/
/// Application
#include "App.h"

View File

@ -1,5 +1,7 @@
#include "sdl_engine.h"
#include "unistd.h"
#include <memory>
using namespace std;
SDL_Texture* RenderText(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph)
{
@ -34,17 +36,6 @@ void TextureDraw(SDL_Renderer* rnd,SDL_Texture* texture,int dstx,int dsty)
SDL_RenderCopy(rnd,texture,NULL,&rect);
}
SDL_Texture* MyLoadImage(SDL_Renderer* rnd,const char* FileName,int* pw,int* ph)
{
SDL_Surface* surf=IMG_Load(FileName);
if(surf==NULL) return NULL;
SDL_Texture* texture=SDL_CreateTextureFromSurface(rnd,surf);
SDL_FreeSurface(surf);
if(texture==NULL) return NULL;
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));
@ -71,3 +62,191 @@ int MyChangeDir(const char* DirName)
mlog("Change Dir returns %d",ret);
return ret;
}
namespace Engine
{
SDL_Rect Rect::toSDLRect()
{
SDL_Rect rect;
rect.x=x;
rect.y=y;
rect.w=w;
rect.h=h;
return rect;
}
Rect::Rect()
{
x=y=w=h=0;
}
Rect::Rect(int incx,int incy,int incw,int inch)
{
x=incx;
y=incy;
w=incw;
h=inch;
}
Rect::~Rect()
{
}
struct Window::impl
{
shared_ptr<SDL_Window> sWnd;
Renderer rnd;
};
struct Renderer::impl
{
shared_ptr<SDL_Renderer> sRnd;
};
struct Texture::impl
{
shared_ptr<SDL_Texture> sText;
int w,h;
};
Window::Window(int winw,int winh)
{
pimpl=new impl;
pimpl->sWnd.reset(SDL_CreateWindow("Engine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,winw,winh,SDL_WINDOW_SHOWN),SDL_DestroyWindow);
pimpl->rnd.pimpl->sRnd.reset(SDL_CreateRenderer(pimpl->sWnd.get(),-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE),SDL_DestroyRenderer);
}
Window::~Window()
{
delete pimpl;
}
Renderer Window::getRenderer()
{
return pimpl->rnd;
}
Renderer::Renderer()
{
pimpl=new impl;
}
Renderer::~Renderer()
{
delete pimpl;
}
int Renderer::clear()
{
return SDL_RenderClear(pimpl->sRnd.get());
}
Texture Renderer::loadImage(const char* FileName)
{
Texture t;
t.pimpl->sText.reset(IMG_LoadTexture(pimpl->sRnd.get(),FileName),SDL_DestroyTexture);
t.updateSize();
return t;
}
Texture Renderer::render(Surface surface)
{
Texture t;
t.pimpl->sText.reset(SDL_CreateTextureFromSurface(pimpl->sRnd.get(),surface.pimpl->sSurf.get()));
t.updateSize();
return t;
}
void Renderer::update()
{
SDL_RenderPresent(pimpl->sRnd.get());
}
int Renderer::copy(Texture t,Rect src,Rect dst)
{
SDL_Rect s=src.toSDLRect();
SDL_Rect d=dst.toSDLRect();
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),&s,&d);
}
int Renderer::copyTo(Texture t,Rect dst)
{
SDL_Rect d=dst.toSDLRect();
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),NULL,&d);
}
int Renderer::copyFill(Texture t,Rect src)
{
SDL_Rect s=src.toSDLRect();
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),&s,NULL);
}
int Renderer::copyFullFill(Texture t)
{
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),NULL,NULL);
}
Texture::Texture()
{
pimpl=new impl;
}
int Texture::getw()
{
return pimpl->w;
}
int Texture::geth()
{
return pimpl->h;
}
Texture::~Texture()
{
delete pimpl;
}
int Texture::updateSize()
{
return SDL_QueryTexture(pimpl->sText.get(),NULL,NULL,&pimpl->w,&pimpl->h);
}
RGBA::RGBA()
{
r=g=b=a=0;
}
RGBA::RGBA(int incr,int incg,int incb,int inca)
{
r=incr;
g=incg;
b=incb;
a=inca;
}
SDL_Color RGBA::toSDLColor()
{
SDL_Color color;
color.r=r;
color.g=g;
color.b=b;
color.a=a;
return color;
}
struct Font::impl
{
shared_ptr<TTF_Font> sTTF;
};
Font::Font()
{
pimpl=new impl;
}
Font::Font(const char* FontFileName,int sz) : Font()
{
use(FontFileName,sz);
}
int Font::use(const char* FontFileName,int sz)
{
TTF_Font* font=TTF_OpenFont(FontFileName,sz);
if(font==NULL) return -1;
pimpl->sTTF.reset(font,TTF_CloseFont);
return 0;
}
Font::~Font()
{
delete pimpl;
}
Texture
}/// End of namespace Engine

View File

@ -4,7 +4,6 @@
SDL_Texture* RenderText(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph);
SDL_Texture* RenderUTF8(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph);
void TextureDraw(SDL_Renderer* rnd,SDL_Texture* texture,int dstx,int dsty);
SDL_Texture* MyLoadImage(SDL_Renderer* rnd,const char* FileName,int* pw,int* ph);
bool isInRect(int x,int y,SDL_Rect rect);
bool isInRect(int x,int y,int LU_x,int LU_y,int RD_x,int RD_y);
void ClearMessageQueue();
@ -13,3 +12,115 @@ int MyChangeDir(const char* DirName);
extern SDL_Color color_white;
extern SDL_Color color_black;
namespace Engine
{
class Rect
{
public:
Rect();
Rect(int incx,int incy,int incw,int inch);
SDL_Rect toSDLRect();
~Rect();
int x,y,w,h;
};
class Renderer;
class Texture;
class Window
{
public:
Window(int winw,int winh);
~Window();
Renderer getRenderer();
Rect getSize();
void setSize(Rect r);
private:
struct impl;
impl* pimpl;
};
class Surface
{
public:
~Surface();
protected:
Surface();
private:
struct impl;
impl* pimpl;
friend class Renderer;
};
class Renderer
{
public:
~Renderer();
int clear();
void update();
int copy(Texture t,Rect src,Rect dst);
int copyTo(Texture t,Rect dst);
int copyFill(Texture t,Rect src);
int copyFullFill(Texture t);
Texture loadImage(const char* FileName);
Texture render(Surface surface);
protected:
Renderer();
private:
struct impl;
impl* pimpl;
friend class Window;
};
class Texture
{
public:
~Texture();
int getw();
int geth();
protected:
Texture();
int updateSize();
private:
struct impl;
impl* pimpl;
friend class Renderer;
};
class RGBA
{
public:
RGBA();
RGBA(int incr,int incg,int incb,int inca);
~RGBA();
SDL_Color toSDLColor();
int r,g,b,a;
};
class Font
{
public:
Font();
Font(const char* FontFileName,int sz);
~Font();
int use(const char* FontFileName,int sz);
Texture renderText(Renderer rnd,const char* Word,RGBA fg);
Texture renderTextShaded(Renderer rnd,const char* Word,RGBA fg,RGBA bg);
Texture renderTextSolid(Renderer rnd,const char* Word,RGBA fg);
Texture renderUTF8(Renderer rnd,const char* Word,RGBA fg);
Texture renderUTF8Shaded(Renderer rnd,const char* Word,RGBA fg,RGBA bg);
Texture renderUTF8Solid(Renderer rnd,const char* Word,RGBA fg);
private:
struct impl;
impl* pimpl;
};
}/// End of namespace Engine