diff --git a/rts/Map.cpp b/rts/Map.cpp new file mode 100644 index 0000000..82042a8 --- /dev/null +++ b/rts/Map.cpp @@ -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); + //} + } + } +} \ No newline at end of file diff --git a/rts/Map.h b/rts/Map.h new file mode 100644 index 0000000..f9ec7fc --- /dev/null +++ b/rts/Map.h @@ -0,0 +1,52 @@ +#ifndef MAPH +#define MAPH + +#define SFML_STATIC +#include + +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 \ No newline at end of file diff --git a/rts/blades.png b/rts/blades.png new file mode 100644 index 0000000..14409c5 Binary files /dev/null and b/rts/blades.png differ diff --git a/rts/grass.png b/rts/grass.png new file mode 100644 index 0000000..077ad35 Binary files /dev/null and b/rts/grass.png differ diff --git a/rts/main.cpp b/rts/main.cpp index 79d52cd..226c36f 100644 --- a/rts/main.cpp +++ b/rts/main.cpp @@ -1,10 +1,45 @@ #define SFML_STATIC #include -#include "sockwrap.h" #include #include +#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(); + } } \ No newline at end of file diff --git a/rts/rts.vcxproj b/rts/rts.vcxproj index 0d5ce40..b980eff 100644 --- a/rts/rts.vcxproj +++ b/rts/rts.vcxproj @@ -69,6 +69,11 @@ + + + + + diff --git a/rts/rts.vcxproj.filters b/rts/rts.vcxproj.filters index 8755a3a..c25a31f 100644 --- a/rts/rts.vcxproj.filters +++ b/rts/rts.vcxproj.filters @@ -18,5 +18,16 @@ Source Files + + Source Files + + + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/rts/tile.png b/rts/tile.png new file mode 100644 index 0000000..6575907 Binary files /dev/null and b/rts/tile.png differ