From ddee7a399d628302c355030e79b2cc9fa55fdd79 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 9 May 2017 11:06:44 +0800 Subject: [PATCH] Update code. Fix Bug in TiledMap --- TiledMap.cpp | 4 ++-- main.cpp | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/TiledMap.cpp b/TiledMap.cpp index 3b085d7..a0c0d7c 100644 --- a/TiledMap.cpp +++ b/TiledMap.cpp @@ -57,8 +57,8 @@ void TiledMap::setRectSize(int 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); + Line=std::min(std::max(Line,0),sp->l-1); + Col=std::min(std::max(Col,0),sp->c-1); sp->vpline=Line; sp->vpcol=Col; } diff --git a/main.cpp b/main.cpp index 4b96882..f1b004d 100644 --- a/main.cpp +++ b/main.cpp @@ -4,10 +4,12 @@ using namespace MiniEngine; #include "TiledMap.h" #include +#include using namespace std; int AppMain() { + srand(time(NULL)); Window wnd("TiledMap",1024,768); Renderer rnd=wnd.getRenderer(); @@ -42,9 +44,7 @@ int AppMain() 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) + if(rc.hasIntersection(wnd.getSize())) { switch(tmap[i][j].tid) { @@ -59,11 +59,22 @@ int AppMain() } } } + + Rect rc; + rc.x=center.x-width/2; + rc.y=center.y-width/2; + rc.w=rc.h=width; + rnd.setColor(RGBA(0,255,0,0)); + rnd.fillRect(rc); + rnd.setColor(RGBA(0,0,0,0)); rnd.update(); }; lp.add(SDL_QUIT,[&](){lp.stop();}); lp.add(SDL_KEYDOWN,[&](Event& e) { + int vl,vc; + tmap.getViewPoint(vl,vc); + printf("Position: Line %d Col %d\n",vl,vc); switch(e.key.keysym.sym) { case SDLK_KP_PLUS: @@ -108,6 +119,11 @@ int AppMain() lp.needupdate(); } break; + case SDLK_ESCAPE: + { + lp.stop(); + } + break; } }); lp.run();