stuff sort of works

fixing selective rendering and tiling errors
This commit is contained in:
Alec Obradovich 2013-03-05 22:37:41 -06:00
parent 31e164385a
commit 790e6b8983
8 changed files with 196 additions and 2 deletions

91
rts/Map.cpp Normal file
View file

@ -0,0 +1,91 @@
#include "Map.h"
rts::Tile::Tile(sf::Sprite *spr, int h) {
this->img = spr;
this->height = h;
}
int rts::Tile::getHeight() {
return height;
}
void rts::Tile::setHeight(int h) {
this->height = h;
}
sf::Sprite* rts::Tile::getImage() {
return img;
}
void rts::Tile::setImage(sf::Sprite *nimg) {
this->img = nimg;
}
rts::Map::Map(int nTiles) {
this->initMap(nTiles);
}
void rts::Map::initMap(int nTiles) {
this->tileCount = nTiles;
this->tiles = new Tile*[nTiles];
for(int i = 0; i < nTiles; i++)
this->tiles[i] = new Tile;
static sf::Texture tmp;
tmp.loadFromFile("grid.png");
grass.setTexture(tmp);
this->viewx = viewy = 0;
this->w = nTiles * TILE_W;
this->h = nTiles * TILE_H;
}
rts::Tile rts::Map::getTile(int i, int j) {
return tiles[i][j];
}
void rts::Map::setTile(int i, int j, Tile t) {
tiles[i][j] = t;
}
void rts::Map::setTile(int i, int j, sf::Sprite *s, int h) {
tiles[i][j] = Tile(s, h);
}
void rts::Map::setView(long x, long y) {
viewx = x;
viewy = y;
}
void rts::Map::changeView(long x, long y) {
if(viewx + x >= 0 && viewx + x <= w)
viewx += x;
else
viewx = (viewx+x<0)?0:w;
if(viewy + y >= 0 && viewy + y <= h)
viewy += y;
else
viewy = (viewy+y<0)?0:h;
}
void rts::Map::Render(sf::RenderWindow *rw) {
int cw = rw->getSize().x;
int ch = rw->getSize().y;
for(int y = 0; y < tileCount; y++) {
for(int x = 0; x < tileCount; x++) {
long tx0 = (((w/2)-(TILE_W/2)) + ((TILE_W/2)*x) - ((TILE_W/2)*y)),
ty0 = ((TILE_H/2)*y+(TILE_H/2)*x),
tx1 = tx0 + TILE_W,
ty1 = tx1 + TILE_H;
//if((ty0 >= viewy && ty1 <= viewy+ch) && (tx1 >= viewx && tx0 <= viewx+cw)) {
grass.setPosition(tx0-viewx, ty0-viewy);
rw->draw(grass);
//}
}
}
}

52
rts/Map.h Normal file
View file

@ -0,0 +1,52 @@
#ifndef MAPH
#define MAPH
#define SFML_STATIC
#include <SFML/Graphics.hpp>
namespace rts {
const int TILE_W = 160;
const int TILE_H = 80;
const int HEIGHT_MULTIPLIER = 30;
class Tile {
int height;
sf::Sprite *img;
public:
Tile(){};
Tile(sf::Sprite*, int=0);
int getHeight();
void setHeight(int);
sf::Sprite* getImage();
void setImage(sf::Sprite*);
};
class Map {
long w, h; // width and height needed to display the entire map
long viewx, viewy; // topleftmost pixel of the view
int tileCount; // array is tileCount x tileCount
sf::Sprite grass;
Tile **tiles;
public:
Map(){};
Map(int);
void initMap(int);
Tile getTile(int,int);
void setTile(int,int,Tile);
void setTile(int,int,sf::Sprite*,int=0);
void setView(long,long);
void changeView(long,long);
void Render(sf::RenderWindow*);
};
};
#endif

BIN
rts/blades.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
rts/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -1,10 +1,45 @@
#define SFML_STATIC
#include <stdio.h>
#include "sockwrap.h"
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "sockwrap.h"
#include "Map.h"
int main() {
return 0;
sf::RenderWindow rw(sf::VideoMode(1024, 768), "test");
rw.setFramerateLimit(60);
rts::Map gameMap(24);
long clk = 0;
while(rw.isOpen()) {
sf::Event event;
while (rw.pollEvent(event)) {
if (event.type == sf::Event::Closed)
rw.close();
}
rw.clear();
if(true) {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
gameMap.changeView(0,-4);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
gameMap.changeView(0,4);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
gameMap.changeView(-4,0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
gameMap.changeView(4,0);
}
gameMap.Render(&rw);
rw.display();
}
}

View file

@ -69,6 +69,11 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="Map.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Map.h" />
<ClInclude Include="sockwrap.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View file

@ -18,5 +18,16 @@
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Map.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="sockwrap.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Map.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

BIN
rts/tile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB