From b7728a38e404edaa30390821b078d34fc6c15ec9 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 8 May 2017 20:04:42 +0800 Subject: [PATCH] Add Tiled Map --- TiledMap.cpp | 70 +++++++++++++++++++++++++++++++ TiledMap.h | 32 +++++++++++++++ main.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 212 insertions(+), 3 deletions(-) create mode 100644 TiledMap.cpp create mode 100644 TiledMap.h diff --git a/TiledMap.cpp b/TiledMap.cpp new file mode 100644 index 0000000..3b085d7 --- /dev/null +++ b/TiledMap.cpp @@ -0,0 +1,70 @@ +#include "TiledMap.h" + +TiledBox::TiledBox() +{ + tid=-1; +} + +struct TiledMap::impl +{ + int l,c; + TiledBox** data; + int rectsize; + + int vpline,vpcol; +}; + +TiledMap::TiledMap(int Line,int Col,int RectSize) +{ + sp.reset(new impl); + sp->l=Line; + sp->c=Col; + sp->rectsize=RectSize; + sp->vpline=0; + sp->vpcol=0; + sp->data=new TiledBox*[sp->l]; + for(int i=0;il;i++) + { + sp->data[i]=new TiledBox[sp->c]; + } +} + +int TiledMap::setBox(int LineID, int ColID, const TiledBox& box) +{ + if(LineID>0&&LineIDl&&ColID>0&&ColIDc) + { + sp->data[LineID][ColID]=box; + return 0; + } + return -1; +} + +TiledBox* TiledMap::operator[] (int LineID) +{ + return sp->data[LineID]; +} + +int TiledMap::getRectSize() +{ + return sp->rectsize; +} + +void TiledMap::setRectSize(int RectSize) +{ + if(RectSize<0) RectSize=0; + sp->rectsize=RectSize; +} + +void TiledMap::setViewPoint(int Line, int Col) +{ + Line=std::min(std::max(Line,0),sp->l); + Col=std::min(std::max(Col,0),sp->c); + sp->vpline=Line; + sp->vpcol=Col; +} + +void TiledMap::getViewPoint(int& refLine, int& refCol) +{ + refLine=sp->vpline; + refCol=sp->vpcol; +} diff --git a/TiledMap.h b/TiledMap.h new file mode 100644 index 0000000..4e7dda0 --- /dev/null +++ b/TiledMap.h @@ -0,0 +1,32 @@ +#pragma once +#include +#include "MiniEngine/MiniEngine.h" + +typedef int TextureID; +/** TextureID: +>=0 Valid Texture +-1 Empty +-2 Colored +*/ + +struct TiledBox +{ + TiledBox(); + TextureID tid; + MiniEngine::RGBA color; +}; + +class TiledMap +{ +public: + TiledMap(int Line,int Col,int RectSize); + int setBox(int LineID,int ColID,const TiledBox& box); + TiledBox* operator[](int LineID); + void setRectSize(int RectSize); + int getRectSize(); + void setViewPoint(int Line,int Col); + void getViewPoint(int& refLine,int& refCol); +private: + struct impl; + std::shared_ptr sp; +}; diff --git a/main.cpp b/main.cpp index b4392ec..4b96882 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,116 @@ -#include +#include "MiniEngine/MiniEngine.h" +#include "MiniEngine/MiniEngine_Event.h" +using namespace MiniEngine; +#include "TiledMap.h" +#include using namespace std; -int main() +int AppMain() { - cout << "Hello world!" << endl; + Window wnd("TiledMap",1024,768); + Renderer rnd=wnd.getRenderer(); + + TiledMap tmap(20,15,20); + for(int i=0;i<20;i++) + { + for(int j=0;j<15;j++) + { + TiledBox box; + box.tid=-1-rand()%2; + if(box.tid==-2) box.color=RGBA(255,0,0,0); + tmap[i][j]=box; + } + } + tmap.setViewPoint(10,7); + + Looper lp; + lp.updater=[&]() + { + Point center(1024/2,768/2); + int vl,vc; + tmap.getViewPoint(vl,vc); + int width=tmap.getRectSize(); + + rnd.clear(); + for(int i=0;i<20;i++) + { + for(int j=0;j<15;j++) + { + Rect rc; + rc.x=center.x-(vc-j)*width-width/2; + rc.y=center.y-(vl-i)*width-width/2; + rc.w=rc.h=width; + + SDL_Rect a=rc.toSDLRect(); + SDL_Rect b=wnd.getSize().toSDLRect(); + if(SDL_HasIntersection(&a,&b)==SDL_TRUE) + { + switch(tmap[i][j].tid) + { + case -1: + break; + case -2: + rnd.setColor(tmap[i][j].color); + rnd.fillRect(rc); + rnd.setColor(RGBA(0,0,0,0)); + break; + } + } + } + } + rnd.update(); + }; + lp.add(SDL_QUIT,[&](){lp.stop();}); + lp.add(SDL_KEYDOWN,[&](Event& e) + { + switch(e.key.keysym.sym) + { + case SDLK_KP_PLUS: + printf("Plus Pressed.\n"); + tmap.setRectSize(tmap.getRectSize()+10); + lp.needupdate(); + break; + case SDLK_KP_MINUS: + printf("Minus Presse.\n"); + tmap.setRectSize(tmap.getRectSize()-10); + lp.needupdate(); + break; + case SDLK_DOWN: + { + int vx,vy; + tmap.getViewPoint(vx,vy); + tmap.setViewPoint(vx+1,vy); + lp.needupdate(); + } + break; + case SDLK_UP: + { + int vx,vy; + tmap.getViewPoint(vx,vy); + tmap.setViewPoint(vx-1,vy); + lp.needupdate(); + } + break; + case SDLK_LEFT: + { + int vx,vy; + tmap.getViewPoint(vx,vy); + tmap.setViewPoint(vx,vy-1); + lp.needupdate(); + } + break; + case SDLK_RIGHT: + { + int vx,vy; + tmap.getViewPoint(vx,vy); + tmap.setViewPoint(vx,vy+1); + lp.needupdate(); + } + break; + } + }); + lp.run(); + return 0; }