Page MenuHomePhabricator (Chris)

No OneTemporary

Size
22 KB
Referenced Files
None
Subscribers
None
diff --git a/boardelem.cpp b/boardelem.cpp
new file mode 100644
index 0000000..41165f0
--- /dev/null
+++ b/boardelem.cpp
@@ -0,0 +1,33 @@
+#include "boardelem.h"
+
+BoardElem::BoardElem(TextureHolder *textures,int pos, int type, const sf::Texture *textureElem)
+{
+// this->textureElem = textureElem;
+ this->pos = pos;
+ this->textures = textures;
+ this->type = type;
+ // setPosition()
+
+ int y = (int) pos / efc::TILE_SIZE;
+ int x = pos % efc::TILE_SIZE;
+
+
+
+
+ int y_type = (int) type / 16;
+ int x_type = type % 16;
+ this->textureElem = std::move(textureElem);
+
+
+ std::cout << "POS " << pos << " x" << x << " : " << x * efc::TILE_SIZE << " y" << y << " : " << y * efc::TILE_SIZE << std::endl;
+//std::cout << "x_type " << x_type << " : " << x_type * efc::TILE_SIZE << " y_type " << y_type << " : " << y_type * efc::TILE_SIZE << std::endl;
+
+
+
+ setPosition(x*efc::TILE_SIZE,y*efc::TILE_SIZE);
+ setTexture(myHolder.textureTiles);
+ setTextureRect(sf::IntRect(x_type*efc::TILE_SIZE, y_type*efc::TILE_SIZE, efc::TILE_SIZE, efc::TILE_SIZE));
+
+
+}
+
diff --git a/boardelem.h b/boardelem.h
new file mode 100644
index 0000000..9a5136e
--- /dev/null
+++ b/boardelem.h
@@ -0,0 +1,29 @@
+#ifndef BOARDELEM_H
+#define BOARDELEM_H
+#include <SFML/Window.hpp>
+#include <SFML/Graphics.hpp>
+#include "tilemap.h"
+#include "textureholder.h"
+
+class BoardElem: public sf::Sprite
+{
+public:
+ BoardElem();
+ BoardElem(TextureHolder *textures,int pos, int type, const sf::Texture *textureElem);
+
+
+
+ int pos;
+ int type;
+
+
+
+private:
+// sf::Sprite spriteElem;
+ const sf::Texture* textureElem;
+ static const sf::Texture& textureElem2;
+ TextureHolder *textures;
+ TextureHolder myHolder;
+};
+
+#endif // BOARDELEM_H
diff --git a/boardelems.cpp b/boardelems.cpp
new file mode 100644
index 0000000..0dfc5a9
--- /dev/null
+++ b/boardelems.cpp
@@ -0,0 +1,21 @@
+#include "boardelems.h"
+
+BoardElems::BoardElems()
+{
+}
+void BoardElems::draw(sf::RenderTarget& target, sf::RenderStates states) const
+{
+
+ // apply the transform
+ states.transform *= getTransform();
+ // apply the tileset texture
+
+ // draw the vertex array
+ for (BoardElem i: items)
+ {
+ target.draw(i, states);
+// std::cout << "POS: "<< i.getPosition().x << " " << i.getPosition().y << std::endl;
+ }
+
+
+}
diff --git a/boardelems.h b/boardelems.h
new file mode 100644
index 0000000..47ce3f2
--- /dev/null
+++ b/boardelems.h
@@ -0,0 +1,15 @@
+#ifndef BOARDELEMS_H
+#define BOARDELEMS_H
+#include "boardelem.h"
+
+class BoardElems: public sf::Drawable, public sf::Transformable
+{
+public:
+ BoardElems();
+ std::vector<BoardElem> items;
+private:
+ virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
+
+};
+
+#endif // BOARDELEMS_H
diff --git a/enfucraft.pro b/enfucraft.pro
index 13029e2..712da65 100644
--- a/enfucraft.pro
+++ b/enfucraft.pro
@@ -1,27 +1,33 @@
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
CONFIG += c++11
SOURCES += main.cpp \
game.cpp \
tilemap.cpp \
selector.cpp \
- playerhud.cpp
+ playerhud.cpp \
+ boardelem.cpp \
+ boardelems.cpp \
+ textureholder.cpp
LIBS += -lsfml-window -lsfml-system -lsfml-graphics
DESTDIR = ../build_release_enfucraft
assets.path = $${DESTDIR}/assets
assets.files = assets/*
INSTALLS += assets
HEADERS += \
game.h \
tilemap.h \
selector.h \
- playerhud.h
+ playerhud.h \
+ boardelem.h \
+ boardelems.h \
+ textureholder.h
RESOURCES += \
images.qrc
diff --git a/game.cpp b/game.cpp
index 2de92a6..c4878f7 100644
--- a/game.cpp
+++ b/game.cpp
@@ -1,199 +1,172 @@
#include <stdlib.h>
#include <iostream>
#include "game.h"
#include <time.h> /* time */
-#include "playerhud.h"
-
-
namespace efc {
-Game::Game()
-{
- int tileSize = 25;
- int mapSize = 16;
- std::srand (time(NULL));
-
- int turn = 0;
-
-
-
- sf::Texture textureFaces;
-
- if (!textureFaces.loadFromFile("assets/img/faces.jpg"))
- std::exit(1);
-
- sf::Font gameFont;
- if (!gameFont.loadFromFile("assets/fnt/8bitOperatorPlus-Regular.ttf"))
- {
- std::exit(1);
- }
-
-
- sf::RenderWindow window(sf::VideoMode(512, 400), "EnFuCraft");
+void Game::initBoard()
+{
// Grass tile starts at 342 and has 11 tiles
- int level[256];
- int levelElems[256];
- /* =
+ // int level[256];
+ // int level[256];
+
+ // Structure of the board
+ /*
{
342, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
- 1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
- 0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
- 0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
- 0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
- 2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
- 0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
- 342, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 80, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
- 1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
- 0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
- 0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
- 0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
- 2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
- 0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
- };*/
-
+ 1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3 }
+ */
// Fill the array
for (int i=0;i<256;i++)
{
int grass = (rand() % 10) + 1;
level[i] = 342 + grass;
- levelElems[i] = level[i];
+ level[i] = level[i];
}
- sf::View viewGui(sf::FloatRect(00, 00, 112, 400));
- viewGui.setViewport(sf::FloatRect(0.8f,0, 1.0f, 1.0f));
+ // level[0] = 441;
+ // level[240] = 0;
+ // level[255] = 0;
+ // level[15] = 0;
+ level[8] = 813;
+ level[24] = 803;
+ level[40] = 803;
+ level[56] = 803;
+ level[72] = 803;
- sf::View viewTiles(sf::FloatRect(00, 00, 400, 400));
- viewTiles.setViewport(sf::FloatRect(0,0, 0.8f, 1.0f));
+ level[88] = 801;
+
+ level[167] = 809;
+ level[183] = 803;
+ level[199] = 803;
+ level[215] = 803;
+ level[231] = 803;
+ level[247] = 812;
+ level[112] = 811;
+ for (int i=113;i<117;i++)
+ level[i] = 816;
+ level[117] = 815;
- // create the tilemap from the level definition
- TileMap map;
- Selector selector(tileSize);
- TileMap mapElem;
+ level[138] = 800;
+ for (int i=139;i<143;i++)
+ level[i] = 816;
+ level[143] = 814;
- selector.changeColor(turn);
- PlayerHud playerHud1(std::rand() % 80, &textureFaces, &gameFont, 32,0);
- PlayerHud playerHud2(std::rand() % 30, &textureFaces, &gameFont, 32,1);
- PlayerHud playerHud3(std::rand() % 60, &textureFaces, &gameFont, 32,2);
- PlayerHud playerHud4(std::rand() % 50, &textureFaces, &gameFont, 32,3);
- PlayerHud players[4] = {playerHud1, playerHud2, playerHud3, playerHud4};
+ map.load(&textureTiles, sf::Vector2u(efc::TILE_SIZE, efc::TILE_SIZE), level, mapSize, mapSize);
+ PlayerHud playerHud1(&textures, std::rand() % 80, &textureFaces, &textureTiles, &gameFont, 32,0);
+ PlayerHud playerHud2(&textures, std::rand() % 30, &textureFaces, &textureTiles, &gameFont, 32,1);
+ PlayerHud playerHud3(&textures, std::rand() % 60, &textureFaces, &textureTiles, &gameFont, 32,2);
+ PlayerHud playerHud4(&textures, std::rand() % 50, &textureFaces, &textureTiles, &gameFont, 32,3);
+ players[0] = playerHud1;
+ players[1] = playerHud2;
+ players[2] = playerHud3;
+ players[3] = playerHud4;
- if (!map.load("assets/img/zw-tilesets/_MAP.png", sf::Vector2u(tileSize, tileSize), level, mapSize, mapSize))
+}
+
+void Game::loadAssets()
+{
+ if (!textureFaces.loadFromFile("assets/img/faces.jpg"))
std::exit(1);
+ if (!textureTiles.loadFromFile("assets/img/zw-tilesets/_MAP.png"))
+ std::exit(1);
- levelElems[0] = 0;
- levelElems[15] = 0;
- levelElems[8] = 813;
- levelElems[24] = 803;
- levelElems[40] = 803;
- levelElems[56] = 803;
- levelElems[72] = 803;
-
- levelElems[88] = 801;
-// levelElems[104] = 80;
-// levelElems[120] = 803;
-// levelElems[135] = 803;
-// levelElems[151] = 803;
-// levelElems[161] = 803;
- levelElems[167] = 809;
- levelElems[183] = 803;
- levelElems[199] = 803;
- levelElems[215] = 803;
- levelElems[231] = 803;
- levelElems[247] = 812;
-
- levelElems[112] = 811;
- for (int i=113;i<117;i++)
- levelElems[i] = 816;
- levelElems[117] = 815;
+ if (!gameFont.loadFromFile("assets/fnt/8bitOperatorPlus-Regular.ttf"))
+ {
+ std::exit(1);
+ }
+}
- levelElems[138] = 800;
- for (int i=139;i<143;i++)
- levelElems[i] = 816;
- levelElems[143] = 814;
+Game::Game()
+{
+ mapSize = 16;
+ std::srand (time(NULL));
+ int turn = 0;
- levelElems[240] = 0;
- levelElems[255] = 0;
+ loadAssets();
- if (!mapElem.load("assets/img/zw-tilesets/_MAP.png", sf::Vector2u(tileSize, tileSize), levelElems, mapSize, mapSize))
- std::exit(1);
+ sf::RenderWindow window(sf::VideoMode(512, 400), "EnFuCraft");
+ sf::View viewGui(sf::FloatRect(00, 00, 112, 400));
+ viewGui.setViewport(sf::FloatRect(0.8f,0, 1.0f, 1.0f));
+ sf::View viewTiles(sf::FloatRect(00, 00, 400, 400));
+ viewTiles.setViewport(sf::FloatRect(0,0, 0.8f, 1.0f));
+
+ Selector selector(efc::TILE_SIZE);
+ selector.changeColor(turn); //This is only for the test TODO: remove
+
+ // TileMap mapElem;
+ // if (!mapElem.load(&textureTiles, sf::Vector2u(tileSize, tileSize), levelElems, mapSize, mapSize))
+ // std::exit(1);
+ initBoard();
+
// run the main loop
while (window.isOpen())
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
std::cout << "the right button was pressed" << std::endl;
std::cout << "mouse x: " << event.mouseButton.x << std::endl;
std::cout << "mouse y: " << event.mouseButton.y << std::endl;
turn++;
if (turn==4)
turn = 0;
selector.changeColor(turn);
}
}
-
-
}
+
+ //
sf::Vector2i localPositionTmp = sf::Mouse::getPosition(window);
sf::Vector2f localPosition = window.mapPixelToCoords(localPositionTmp,viewTiles);
-
-
-
- if ((localPosition.x>=0) && (localPosition.y>=0) && (localPosition.x<=mapSize*tileSize) && (localPosition.y<=mapSize*tileSize))
+ if ((localPosition.x>=0) && (localPosition.y>=0) && (localPosition.x<=mapSize*efc::TILE_SIZE) && (localPosition.y<=mapSize*efc::TILE_SIZE))
{
- std::cout << localPosition.x << " " << localPosition.y << " " << localPosition.x / tileSize << " " << localPosition.y / tileSize << std::endl;
- selector.setPosition((int) (localPosition.x / tileSize)*tileSize, ((int) localPosition.y / tileSize)*tileSize);
+// std::cout << localPosition.x << " " << localPosition.y << " " << localPosition.x / efc::TILE_SIZE << " " << localPosition.y / efc::TILE_SIZE << std::endl;
+ selector.setPosition((int) (localPosition.x / efc::TILE_SIZE)*efc::TILE_SIZE, ((int) localPosition.y / efc::TILE_SIZE)*efc::TILE_SIZE);
}
-
-
// draw the map
window.clear();
-
-
window.setView(viewTiles);
-
window.draw(map);
- window.draw(mapElem);
+ // window.draw(mapElem);
+
+
+ for (int i=0;i<4;i++)
+ {
+ window.draw(players[i].elems);
+ }
window.draw(selector);
window.setView(viewGui);
- window.draw(playerHud1);
- window.draw(playerHud2);
- window.draw(playerHud3);
- window.draw(playerHud4);
+ for (int i=0;i<4;i++)
+ {
+ window.draw(players[i]);
+ }
window.display();
-
-
-
-
-
-
-
}
}
}
diff --git a/game.h b/game.h
index c3985f9..1038d7c 100644
--- a/game.h
+++ b/game.h
@@ -1,16 +1,40 @@
#ifndef GAME_H
#define GAME_H
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "tilemap.h"
#include "selector.h"
+#include "playerhud.h"
+#include "textureholder.h"
namespace efc {
class Game
{
public:
Game();
sf::RenderWindow window;
+
+private:
+ void initBoard();
+ void loadAssets();
+ enum state {
+ state_menu,
+ state_game,
+ state_quit
+ };
+
+ sf::Texture textureTiles;
+ sf::Texture textureFaces;
+ sf::Font gameFont;
+
+ TileMap map;
+ PlayerHud players[4];
+
+ int mapSize;
+ int level[256];
+ int levelElems[256];
+ TextureHolder textures;
+
};
}
#endif // GAME_H
diff --git a/playerhud.cpp b/playerhud.cpp
index 7a6faca..29b3ddb 100644
--- a/playerhud.cpp
+++ b/playerhud.cpp
@@ -1,106 +1,125 @@
#include "playerhud.h"
+#include "textureholder.h"
+PlayerHud::PlayerHud()
+{
+
+}
-PlayerHud::PlayerHud(int faceNumber, sf::Texture *faces, sf::Font *gameFont, int tileSize, int pos)
+PlayerHud::PlayerHud(TextureHolder *textures, int faceNumber, sf::Texture *faces, sf::Texture *textureTiles, sf::Font *gameFont, int faceSize, int pos)
{
- this->tileSize = tileSize;
- spriteFace.setTexture(*faces);
+
+
+ // level[0] = 441;
+ // level[240] = 0;
+ // level[255] = 0;
+ // level[15] = 0;
+
+ int startPositions[4] = {0,15,240, 255};
+
+ this->textures = textures;
+
+
+ BoardElem startElem(textures, startPositions[pos],441, std::move(textureTiles));
+
+// const sf::Texture& refTexture = sf::Texture(*textureTiles);
+ startElem.setTexture(this->textures->textureTiles);
+
+ elems.items.push_back(startElem);
+
+ this->faceSize = faceSize;
+ spriteFace.setTexture(textures->textureFaces);
this->pos = pos;
food = 0;
cash = 0;
energy = 0;
-
- int y = (int) faceNumber /10;
int x = faceNumber % 10;
-// if (!gameFontloadFromFile("assets/fnt/8bitOperatorPlus-Regular.ttf"))
-// {
-// std::exit(1);
-// }
-
+ int y = (int) faceNumber /10;
txtCash.setFont(*gameFont);
txtFood.setFont(*gameFont);
txtEnergy.setFont(*gameFont);
-
txtCash.setPosition(1,(pos*100)+40);
txtCash.setString("Cash: " + std::to_string(cash));
txtCash.setCharacterSize(10);
txtCash.setScale(sf::Vector2f(0.25f, 1.f));
-
txtFood.setPosition(1,(pos*100)+55);
txtFood.setString("Food: " + std::to_string(food));
txtFood.setCharacterSize(10);
txtFood.setScale(sf::Vector2f(0.25f, 1.f));
txtEnergy.setPosition(1,(pos*100)+70);
txtEnergy.setString("Enrg: " + std::to_string(energy));
+ txtEnergy.setString("Enrg: " + std::to_string(energy));
txtEnergy.setCharacterSize(10);
txtEnergy.setScale(sf::Vector2f(0.25f, 1.f));
std::cout << "playerHud" << faceNumber << " " << x << " " << y << std::endl;
- spriteFace.setTextureRect(sf::IntRect(x*tileSize, y*tileSize, tileSize, tileSize));
+ spriteFace.setTextureRect(sf::IntRect(x*faceSize, y*faceSize, faceSize, faceSize));
spriteFace.setScale(sf::Vector2f(0.25f, 1.f));
spriteFace.setPosition(0,pos*100);
}
void PlayerHud::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// // apply the transform
states.transform *= getTransform();
- sf::RectangleShape rectangle(sf::Vector2f(tileSize, tileSize));
+ sf::RectangleShape rectangle(sf::Vector2f(faceSize, faceSize));
- sf::RectangleShape rectangle2(sf::Vector2f(tileSize, (tileSize*2)+3));
+ sf::RectangleShape rectangle2(sf::Vector2f(faceSize, (faceSize*2)+3));
if (pos==0)
{
rectangle.setFillColor(sf::Color(50, 50, 150,168));
rectangle2.setFillColor(sf::Color(100, 100, 150,168));
rectangle.setOutlineColor(sf::Color(0,0,128));
}
else if (pos==1)
{
rectangle.setFillColor(sf::Color(50, 150, 50,168));
rectangle2.setFillColor(sf::Color(100, 150,100,168));
rectangle.setOutlineColor(sf::Color(0,128,0));
}
else if (pos==2)
{
rectangle.setFillColor(sf::Color(150, 50, 50,168));
rectangle2.setFillColor(sf::Color(150, 100,100,168));
rectangle.setOutlineColor(sf::Color(128,0,0));
}
else if (pos==3)
{
rectangle.setFillColor(sf::Color(150, 150, 150,168));
rectangle2.setFillColor(sf::Color(200, 200,200,168));
rectangle.setOutlineColor(sf::Color(128,128,128));
}
-
rectangle.setOutlineThickness(1);
rectangle.setPosition(0, pos*100);
- rectangle2.setPosition(0, (pos*100)+tileSize+1);
+ rectangle2.setPosition(0, (pos*100)+faceSize+1);
target.draw(rectangle, states);
target.draw(rectangle2, states);
- target.draw(txtCash);
- target.draw(txtFood);
- target.draw(txtEnergy);
-
+ target.draw(txtCash, states);
+ target.draw(txtFood, states);
+ target.draw(txtEnergy, states);
+ target.draw(spriteFace, states);
- target.draw(spriteFace);
+ for (const BoardElem &i: elems.items)
+ {
+// std::cout << i.pos << std::endl;
+ }
}
diff --git a/playerhud.h b/playerhud.h
index eaa1e6d..e3e33b4 100644
--- a/playerhud.h
+++ b/playerhud.h
@@ -1,31 +1,40 @@
#ifndef PLAYERHUD_H
#define PLAYERHUD_H
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
+#include "boardelems.h"
+#include "textureholder.h"
class PlayerHud : public sf::Drawable, public sf::Transformable
{
public:
- PlayerHud(int faceNumber, sf::Texture *faces, sf::Font *gameFont, int tileSize, int pos);
+ PlayerHud();
+ PlayerHud(TextureHolder *textures, int faceNumber, sf::Texture *faces, sf::Texture *textureTiles, sf::Font *gameFont, int tileSize, int pos);
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
int pos;
int cash;
int energy;
int food;
+ BoardElems elems;
+
+
std::vector<int> properties;
+
private:
// sf::Font gameFont;
sf::Sprite spriteFace;
sf::Text txtCash;
sf::Text txtEnergy;
sf::Text txtFood;
+ int faceSize;
int tileSize;
+ TextureHolder *textures;
};
#endif // PLAYERHUD_H
diff --git a/textureholder.cpp b/textureholder.cpp
new file mode 100644
index 0000000..65e4a0e
--- /dev/null
+++ b/textureholder.cpp
@@ -0,0 +1,9 @@
+#include "textureholder.h"
+
+TextureHolder::TextureHolder()
+{
+ if (!textureTiles.loadFromFile("assets/img/zw-tilesets/_MAP.png"))
+ std::exit(1);
+ if (!textureFaces.loadFromFile("assets/img/faces.jpg"))
+ std::exit(1);
+}
diff --git a/textureholder.h b/textureholder.h
new file mode 100644
index 0000000..8979284
--- /dev/null
+++ b/textureholder.h
@@ -0,0 +1,13 @@
+#ifndef TEXTUREHOLDER_H
+#define TEXTUREHOLDER_H
+#include <SFML/Window.hpp>
+#include <SFML/Graphics.hpp>
+class TextureHolder
+{
+public:
+ TextureHolder();
+ sf::Texture textureTiles;
+ sf::Texture textureFaces;
+};
+
+#endif // TEXTUREHOLDER_H
diff --git a/tilemap.cpp b/tilemap.cpp
index 90ba1ae..b67f6ec 100644
--- a/tilemap.cpp
+++ b/tilemap.cpp
@@ -1,62 +1,58 @@
#include "tilemap.h"
TileMap::TileMap()
{
}
- bool TileMap::load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
+ bool TileMap::load(sf::Texture *m_tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{
- // load the tileset texture
- if (!m_tileset.loadFromFile(tileset))
- return false;
-
+ this->m_tileset = m_tileset;
// resize the vertex array to fit the level size
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(width * height * 4);
// populate the vertex array, with one quad per tile
for (unsigned int i = 0; i < width; ++i)
for (unsigned int j = 0; j < height; ++j)
{
// get the current tile number
int tileNumber = tiles[i + j * width];
// find its position in the tileset texture
- int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
- int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
+ int tu = tileNumber % (m_tileset->getSize().x / tileSize.x);
+ int tv = tileNumber / (m_tileset->getSize().x / tileSize.x);
+
+// std::cout << "tu " << tu << " tv " << tv << std::endl;
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
}
return true;
}
-
-
void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the transform
states.transform *= getTransform();
-
// apply the tileset texture
- states.texture = &m_tileset;
+ states.texture = m_tileset;
// draw the vertex array
target.draw(m_vertices, states);
}
sf::VertexArray m_vertices;
sf::Texture m_tileset;
diff --git a/tilemap.h b/tilemap.h
index 9a9d119..a914e2f 100644
--- a/tilemap.h
+++ b/tilemap.h
@@ -1,20 +1,25 @@
#ifndef TILEMAP_H
#define TILEMAP_H
+#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
-
+namespace efc {
+enum {
+ TILE_SIZE = 25
+};
+}
class TileMap : public sf::Drawable, public sf::Transformable
{
public:
- bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height);
+ bool load(sf::Texture *m_tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height);
TileMap();
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
sf::VertexArray m_vertices;
- sf::Texture m_tileset;
+ sf::Texture *m_tileset;
};
#endif // TILEMAP_H

File Metadata

Mime Type
text/x-diff
Expires
Mon, Feb 2, 9:12 PM (2 d, 4 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55587
Default Alt Text
(22 KB)

Event Timeline