mirror of
https://github.com/Kiritow/MiniEngine.git
synced 2024-03-22 13:11:22 +08:00
[Update] SDL Engine
SimpleProgressBar More Method with Renderer Brush added.
This commit is contained in:
parent
39c4d04d03
commit
9e11919f44
10
App.cpp
10
App.cpp
|
@ -1,4 +1,5 @@
|
|||
#include "App.h"
|
||||
#include "Game/Main.h"
|
||||
|
||||
using namespace Engine;
|
||||
using namespace MiniEngine;
|
||||
|
@ -8,8 +9,15 @@ namespace App
|
|||
/// Application Main Method
|
||||
void Main()
|
||||
{
|
||||
Window wnd(1024,768);
|
||||
Window wnd(1366,768);///16:9
|
||||
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
|
||||
|
|
0
MiniEngine/Event.cpp
Normal file
0
MiniEngine/Event.cpp
Normal file
32
MiniEngine/Event.h
Normal file
32
MiniEngine/Event.h
Normal 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
|
|
@ -5,63 +5,43 @@ using namespace std;
|
|||
|
||||
namespace MiniEngine
|
||||
{
|
||||
/// Brush
|
||||
#include "widget_brush.hpp"
|
||||
|
||||
struct Brush::impl
|
||||
struct SimpleProgressBar::impl
|
||||
{
|
||||
Renderer rnd;
|
||||
Rect area;
|
||||
impl(const Renderer& incrnd) : rnd(incrnd)
|
||||
{
|
||||
|
||||
}
|
||||
int percentage;
|
||||
int w,h;
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
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)
|
||||
SimpleProgressBar::SimpleProgressBar(int incw,int inch,RGBA upper_color,RGBA lower_color)
|
||||
{
|
||||
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);
|
||||
}
|
||||
pimpl=new impl;
|
||||
pimpl->w=incw;
|
||||
pimpl->h=inch;
|
||||
pimpl->ucolor=upper_color;
|
||||
pimpl->lcolor=lower_color;
|
||||
pimpl->percentage=0;
|
||||
}
|
||||
Brush::~Brush()
|
||||
void SimpleProgressBar::DrawAt(Renderer& rnd,int x,int y)
|
||||
{
|
||||
delete pimpl;
|
||||
RGBA prev=rnd.getColor();
|
||||
if(pimpl->percentage)
|
||||
{
|
||||
rnd.setColor(pimpl->ucolor);
|
||||
rnd.fillRect(Rect(x,y,pimpl->w*pimpl->percentage/100,pimpl->h));
|
||||
}
|
||||
if(pimpl->percentage-100)
|
||||
{
|
||||
rnd.setColor(pimpl->lcolor);
|
||||
rnd.fillRect(Rect(x+pimpl->w*pimpl->percentage/100,y,pimpl->w*(100-pimpl->percentage)/100,pimpl->h));
|
||||
}
|
||||
rnd.setColor(prev);
|
||||
}
|
||||
void SimpleProgressBar::setPercentage(int iPercentage)
|
||||
{
|
||||
pimpl->percentage=min(max(0,iPercentage),100);
|
||||
}
|
||||
|
||||
}/// End of namespace MiniEngine
|
||||
|
|
|
@ -16,8 +16,8 @@ public:
|
|||
_MINI_ENGINE_IMPL_COPY_DECL(Brush)
|
||||
|
||||
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 copyFill(Engine::Texture t,Engine::Rect src);
|
||||
void copyFullFill(Engine::Texture t);
|
||||
public:
|
||||
Brush(const Engine::Window& rnd,Engine::Rect DrawableArea);
|
||||
|
@ -40,7 +40,7 @@ public:
|
|||
virtual Engine::Rect getSize()=0;
|
||||
};
|
||||
|
||||
class BaseButton
|
||||
class BaseButton : public Drawable
|
||||
{
|
||||
public:
|
||||
BaseButton();
|
||||
|
@ -50,6 +50,7 @@ public:
|
|||
BaseButton& operator = (BaseButton&&);
|
||||
|
||||
virtual int Draw(const Brush& brush);
|
||||
virtual Engine::Rect getSize();
|
||||
|
||||
virtual ~BaseButton();
|
||||
private:
|
||||
|
@ -73,4 +74,16 @@ public:
|
|||
~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.
|
||||
|
|
69
MiniEngine/widget_brush.hpp
Normal file
69
MiniEngine/widget_brush.hpp
Normal 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;
|
||||
}
|
2
makefile
2
makefile
|
@ -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
|
||||
|
||||
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)
|
||||
|
||||
|
|
|
@ -136,6 +136,9 @@ public:
|
|||
/// Font
|
||||
#include "sdl_engine_font.hpp"
|
||||
|
||||
|
||||
void SDLSystem::delay(int ms)
|
||||
{
|
||||
SDL_Delay(ms);
|
||||
}
|
||||
|
||||
}/// End of namespace Engine
|
||||
|
|
38
sdl_engine.h
38
sdl_engine.h
|
@ -22,6 +22,17 @@ public:
|
|||
|
||||
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 Texture;
|
||||
|
@ -87,6 +98,14 @@ public:
|
|||
Texture loadImage(const char* FileName);
|
||||
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:
|
||||
Renderer();
|
||||
private:
|
||||
|
@ -108,18 +127,6 @@ private:
|
|||
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:
|
||||
|
@ -141,4 +148,11 @@ private:
|
|||
_SDL_ENGINE_IMPL
|
||||
};
|
||||
|
||||
class SDLSystem
|
||||
{
|
||||
public:
|
||||
static void delay(int ms);
|
||||
};
|
||||
|
||||
}/// End of namespace Engine
|
||||
|
||||
|
|
|
@ -70,3 +70,29 @@ int Renderer::copyFullFill(Texture t)
|
|||
{
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user