From 9e11919f4437f4d1a9ff91359aee550a9e42cd90 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Thu, 19 Jan 2017 12:00:59 +0800 Subject: [PATCH] [Update] SDL Engine SimpleProgressBar More Method with Renderer Brush added. --- App.cpp | 10 ++++- MiniEngine/Event.cpp | 0 MiniEngine/Event.h | 32 +++++++++++++++ MiniEngine/Widget.cpp | 80 ++++++++++++++----------------------- MiniEngine/Widget.h | 17 +++++++- MiniEngine/widget_brush.hpp | 69 ++++++++++++++++++++++++++++++++ makefile | 2 +- sdl_engine.cpp | 5 ++- sdl_engine.h | 38 ++++++++++++------ sdl_engine_renderer.hpp | 26 ++++++++++++ 10 files changed, 212 insertions(+), 67 deletions(-) create mode 100644 MiniEngine/Event.cpp create mode 100644 MiniEngine/Event.h create mode 100644 MiniEngine/widget_brush.hpp diff --git a/App.cpp b/App.cpp index 6e4f7a4..33348ae 100644 --- a/App.cpp +++ b/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 diff --git a/MiniEngine/Event.cpp b/MiniEngine/Event.cpp new file mode 100644 index 0000000..e69de29 diff --git a/MiniEngine/Event.h b/MiniEngine/Event.h new file mode 100644 index 0000000..f725328 --- /dev/null +++ b/MiniEngine/Event.h @@ -0,0 +1,32 @@ +#pragma once +#include "../config.h" +#include + +//#include +namespace MiniEngine +{ + +class MouseEvent +{ +private: + struct EventData + { + int x,y; + } +public: + int type; + EventData data; +}; +class MouseEventListener +{ +public: + MouseEventListener(); + + function onButtonDown; + function onButtonUp; + + function onMotion; + function onWheel; +}; + +}/// End of namespace MiniEngine diff --git a/MiniEngine/Widget.cpp b/MiniEngine/Widget.cpp index d0b8e6d..eab0250 100644 --- a/MiniEngine/Widget.cpp +++ b/MiniEngine/Widget.cpp @@ -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 diff --git a/MiniEngine/Widget.h b/MiniEngine/Widget.h index 6351a6f..a5d2e96 100644 --- a/MiniEngine/Widget.h +++ b/MiniEngine/Widget.h @@ -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. diff --git a/MiniEngine/widget_brush.hpp b/MiniEngine/widget_brush.hpp new file mode 100644 index 0000000..d0a4da7 --- /dev/null +++ b/MiniEngine/widget_brush.hpp @@ -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; +} diff --git a/makefile b/makefile index 0815b65..3a9ec43 100644 --- a/makefile +++ b/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) diff --git a/sdl_engine.cpp b/sdl_engine.cpp index f34370d..4fcb17f 100644 --- a/sdl_engine.cpp +++ b/sdl_engine.cpp @@ -136,6 +136,9 @@ public: /// Font #include "sdl_engine_font.hpp" - +void SDLSystem::delay(int ms) +{ + SDL_Delay(ms); +} }/// End of namespace Engine diff --git a/sdl_engine.h b/sdl_engine.h index 0776167..6a71dfc 100644 --- a/sdl_engine.h +++ b/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 + diff --git a/sdl_engine_renderer.hpp b/sdl_engine_renderer.hpp index 0794611..74e7882 100644 --- a/sdl_engine_renderer.hpp +++ b/sdl_engine_renderer.hpp @@ -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); +}