This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues/pull-requests.
SmartMap/main.cpp

133 lines
3.2 KiB
C++

#include "MiniEngine/MiniEngine.h"
#include "MiniEngine/MiniEngine_Event.h"
using namespace MiniEngine;
#include "TiledMap.h"
#include <cstdlib>
#include <ctime>
using namespace std;
int AppMain()
{
srand(time(NULL));
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;
if(rc.hasIntersection(wnd.getSize()))
{
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;
}
}
}
}
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:
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;
case SDLK_ESCAPE:
{
lp.stop();
}
break;
}
});
lp.run();
return 0;
}