[Update] SDL Engine

SimpleProgressBar
More Method with Renderer
Brush added.
This commit is contained in:
Kirigaya Kazuto 2017-01-19 12:00:59 +08:00
parent 39c4d04d03
commit 9e11919f44
10 changed files with 212 additions and 67 deletions

10
App.cpp
View File

@ -1,4 +1,5 @@
#include "App.h" #include "App.h"
#include "Game/Main.h"
using namespace Engine; using namespace Engine;
using namespace MiniEngine; using namespace MiniEngine;
@ -8,8 +9,15 @@ namespace App
/// Application Main Method /// Application Main Method
void Main() void Main()
{ {
Window wnd(1024,768); Window wnd(1366,768);///16:9
Renderer rnd=wnd.getRenderer(); Renderer rnd=wnd.getRenderer();
Font bigf;
if(bigf.use("msyh.ttf",72)<0)
{
mlog("Failed to open Font.");
return;
}
Game::Main(wnd,rnd,bigf);
/* /*
/// Sample Code of Brush /// Sample Code of Brush

0
MiniEngine/Event.cpp Normal file
View File

32
MiniEngine/Event.h Normal file
View File

@ -0,0 +1,32 @@
#pragma once
#include "../config.h"
#include <functional>
//#include <SDL2/SDL_events.h>
namespace MiniEngine
{
class MouseEvent
{
private:
struct EventData
{
int x,y;
}
public:
int type;
EventData data;
};
class MouseEventListener
{
public:
MouseEventListener();
function<void(const MouseEvent&)> onButtonDown;
function<void(const MouseEvent&)> onButtonUp;
function<void(const MouseEvent&)> onMotion;
function<void(const MouseEvent&)> onWheel;
};
}/// End of namespace MiniEngine

View File

@ -5,63 +5,43 @@ using namespace std;
namespace MiniEngine namespace MiniEngine
{ {
/// Brush
#include "widget_brush.hpp"
struct Brush::impl struct SimpleProgressBar::impl
{ {
Renderer rnd; int percentage;
Rect area; int w,h;
impl(const Renderer& incrnd) : rnd(incrnd) RGBA ucolor,lcolor;
{
}
}; };
Brush::Brush(const Window& wnd,Rect DrawableArea) /// Protected
{
pimpl=new impl(wnd.getRenderer());
pimpl->area=DrawableArea;
}
void Brush::copy(Texture t,Rect src,Rect dst,bool autoZoom)
{
dst.x+=pimpl->area.x;
dst.y+=pimpl->area.y;
if(dst.w>pimpl->area.w) dst.w=pimpl->area.w;
if(dst.h>pimpl->area.h) dst.h=pimpl->area.h;
if(autoZoom)
{
} SimpleProgressBar::SimpleProgressBar(int incw,int inch,RGBA upper_color,RGBA lower_color)
else
{ {
if(src.w>pimpl->area.w) src.w=pimpl->area.w; pimpl=new impl;
if(src.h>pimpl->area.h) src.h=pimpl->area.h; pimpl->w=incw;
pimpl->h=inch;
pimpl->ucolor=upper_color;
pimpl->lcolor=lower_color;
pimpl->percentage=0;
} }
pimpl->rnd.copy(t,src,dst); void SimpleProgressBar::DrawAt(Renderer& rnd,int x,int y)
}
void Brush::copyTo(Texture t,Rect dst,bool autoZoom)
{ {
dst.x+=pimpl->area.x; RGBA prev=rnd.getColor();
dst.y+=pimpl->area.y; if(pimpl->percentage)
if(dst.w>pimpl->area.w) dst.w=pimpl->area.w;
if(dst.h>pimpl->area.h) dst.h=pimpl->area.h;
if(autoZoom)
{ {
pimpl->rnd.copyTo(t,dst); rnd.setColor(pimpl->ucolor);
rnd.fillRect(Rect(x,y,pimpl->w*pimpl->percentage/100,pimpl->h));
} }
else if(pimpl->percentage-100)
{ {
int w=t.getw(); rnd.setColor(pimpl->lcolor);
int h=t.geth(); rnd.fillRect(Rect(x+pimpl->w*pimpl->percentage/100,y,pimpl->w*(100-pimpl->percentage)/100,pimpl->h));
if(w>pimpl->area.w) w=pimpl->area.w;
if(h>pimpl->area.h) h=pimpl->area.h;
Rect src(0,0,w,h);
pimpl->rnd.copy(t,src,dst);
} }
rnd.setColor(prev);
} }
Brush::~Brush() void SimpleProgressBar::setPercentage(int iPercentage)
{ {
delete pimpl; pimpl->percentage=min(max(0,iPercentage),100);
} }
}/// End of namespace MiniEngine }/// End of namespace MiniEngine

View File

@ -16,8 +16,8 @@ public:
_MINI_ENGINE_IMPL_COPY_DECL(Brush) _MINI_ENGINE_IMPL_COPY_DECL(Brush)
void copy(Engine::Texture t,Engine::Rect src,Engine::Rect dst,bool autoZoom); void copy(Engine::Texture t,Engine::Rect src,Engine::Rect dst,bool autoZoom);
void copyFill(Engine::Texture t,Engine::Rect src,bool autoZoom);
void copyTo(Engine::Texture t,Engine::Rect dst,bool autoZoom); void copyTo(Engine::Texture t,Engine::Rect dst,bool autoZoom);
void copyFill(Engine::Texture t,Engine::Rect src);
void copyFullFill(Engine::Texture t); void copyFullFill(Engine::Texture t);
public: public:
Brush(const Engine::Window& rnd,Engine::Rect DrawableArea); Brush(const Engine::Window& rnd,Engine::Rect DrawableArea);
@ -40,7 +40,7 @@ public:
virtual Engine::Rect getSize()=0; virtual Engine::Rect getSize()=0;
}; };
class BaseButton class BaseButton : public Drawable
{ {
public: public:
BaseButton(); BaseButton();
@ -50,6 +50,7 @@ public:
BaseButton& operator = (BaseButton&&); BaseButton& operator = (BaseButton&&);
virtual int Draw(const Brush& brush); virtual int Draw(const Brush& brush);
virtual Engine::Rect getSize();
virtual ~BaseButton(); virtual ~BaseButton();
private: private:
@ -73,4 +74,16 @@ public:
~SimpleButton(); ~SimpleButton();
}; };
class SimpleProgressBar
{
public:
SimpleProgressBar(int incw,int inch,Engine::RGBA upper_color,Engine::RGBA lower_color);
void DrawAt(Engine::Renderer& rnd,int x,int y);
void setPercentage(int iPercentage);
~SimpleProgressBar();
private:
struct impl;
impl* pimpl;
};
}/// End of namespace MiniEngine. }/// End of namespace MiniEngine.

View File

@ -0,0 +1,69 @@
struct Brush::impl
{
Renderer rnd;
Rect area;
impl(const Renderer& incrnd) : rnd(incrnd)
{
}
};
Brush::Brush(const Window& wnd,Rect DrawableArea) /// Protected
{
pimpl=new impl(wnd.getRenderer());
pimpl->area=DrawableArea;
}
void Brush::copy(Texture t,Rect src,Rect dst,bool autoZoom)
{
dst.x+=pimpl->area.x;
dst.y+=pimpl->area.y;
if(dst.w>pimpl->area.w) dst.w=pimpl->area.w;
if(dst.h>pimpl->area.h) dst.h=pimpl->area.h;
if(autoZoom)
{
}
else
{
if(src.w>pimpl->area.w) src.w=pimpl->area.w;
if(src.h>pimpl->area.h) src.h=pimpl->area.h;
}
pimpl->rnd.copy(t,src,dst);
}
void Brush::copyTo(Texture t,Rect dst,bool autoZoom)
{
dst.x+=pimpl->area.x;
dst.y+=pimpl->area.y;
if(dst.w>pimpl->area.w) dst.w=pimpl->area.w;
if(dst.h>pimpl->area.h) dst.h=pimpl->area.h;
if(autoZoom)
{
pimpl->rnd.copyTo(t,dst);
}
else
{
int w=t.getw();
int h=t.geth();
if(w>pimpl->area.w) w=pimpl->area.w;
if(h>pimpl->area.h) h=pimpl->area.h;
Rect src(0,0,w,h);
pimpl->rnd.copy(t,src,dst);
}
}
void Brush::copyFill(Texture t,Rect src)
{
Rect dst=pimpl->area;
pimpl->rnd.copy(t,src,dst);
}
void Brush::copyFullFill(Texture t)
{
Rect dst=pimpl->area;
pimpl->rnd.copyTo(t,dst);
}
Brush::~Brush()
{
delete pimpl;
}

View File

@ -3,7 +3,7 @@ LDFLAGS =
LDLIBS = -lSDL2_image -lSDL2_net -ltiff -ljpeg -lpng -lz -lSDL2_ttf -lfreetype -lSDL2_mixer -lSDL2_test -lsmpeg2 -lvorbisfile -lvorbis -logg -lstdc++ -lSDL2 -lEGL -lGLESv1_CM -lGLESv2 -landroid -Wl,--no-undefined -shared LDLIBS = -lSDL2_image -lSDL2_net -ltiff -ljpeg -lpng -lz -lSDL2_ttf -lfreetype -lSDL2_mixer -lSDL2_test -lsmpeg2 -lvorbisfile -lvorbis -logg -lstdc++ -lSDL2 -lEGL -lGLESv1_CM -lGLESv2 -landroid -Wl,--no-undefined -shared
PROG = program_name PROG = program_name
OBJS = basic_config.o sdl_engine.o config.o InitManager.o mini_engine.o App.o main.o OBJS = basic_config.o sdl_engine.o config.o InitManager.o mini_engine.o App.o main.o MiniEngine/Widget.o
all: $(PROG) all: $(PROG)

View File

@ -136,6 +136,9 @@ public:
/// Font /// Font
#include "sdl_engine_font.hpp" #include "sdl_engine_font.hpp"
void SDLSystem::delay(int ms)
{
SDL_Delay(ms);
}
}/// End of namespace Engine }/// End of namespace Engine

View File

@ -22,6 +22,17 @@ public:
int x,y,w,h; int x,y,w,h;
}; };
class RGBA
{
public:
RGBA();
RGBA(int incr,int incg,int incb,int inca);
~RGBA();
SDL_Color toSDLColor();
int r,g,b,a;
};
class Renderer; class Renderer;
class Texture; class Texture;
@ -87,6 +98,14 @@ public:
Texture loadImage(const char* FileName); Texture loadImage(const char* FileName);
Texture render(Surface surface); Texture render(Surface surface);
int setColor(RGBA pack);
RGBA getColor(int* pStatus=nullptr);
int fillRect(Rect rect);
int drawRect(Rect rect);
/// Not Recommended
SDL_Renderer* getDirectRenderer();
protected: protected:
Renderer(); Renderer();
private: private:
@ -108,18 +127,6 @@ private:
friend class Renderer; 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 class Font
{ {
public: public:
@ -141,4 +148,11 @@ private:
_SDL_ENGINE_IMPL _SDL_ENGINE_IMPL
}; };
class SDLSystem
{
public:
static void delay(int ms);
};
}/// End of namespace Engine }/// End of namespace Engine

View File

@ -70,3 +70,29 @@ int Renderer::copyFullFill(Texture t)
{ {
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->getRawTexture(),NULL,NULL); return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->getRawTexture(),NULL,NULL);
} }
int Renderer::setColor(RGBA pack)
{
return SDL_SetRenderDrawColor(pimpl->sRnd.get(),pack.r,pack.g,pack.b,pack.a);
}
RGBA Renderer::getColor(int* pstatus)
{
Uint8 r,g,b,a;
int ret=SDL_GetRenderDrawColor(pimpl->sRnd.get(),&r,&g,&b,&a);
RGBA pack(r,g,b,a);
if(pstatus) *pstatus=ret;
return pack;
}
int Renderer::fillRect(Rect rect)
{
auto inr=rect.toSDLRect();
return SDL_RenderFillRect(pimpl->sRnd.get(),&inr);
}
int Renderer::drawRect(Rect rect)
{
auto inr=rect.toSDLRect();
return SDL_RenderDrawRect(pimpl->sRnd.get(),&inr);
}