Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
11 KB
Referenced Files
None
Subscribers
None
diff --git a/src/sfml_game/GameMap.cpp b/src/sfml_game/GameMap.cpp
index a601cd1..edfc0c7 100644
--- a/src/sfml_game/GameMap.cpp
+++ b/src/sfml_game/GameMap.cpp
@@ -1,125 +1,137 @@
/** This file is part of sfmlGame.
*
* FreeTumble is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTumble is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FreeTumble. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <fstream>
#include "GameMap.h"
GameMap::GameMap(int width, int height)
{
- this->width = width;
- this->height = height;
+ hasChanged = true;
- map = new int* [width];
- int i = 0;
- for ( i = 0 ; i < width ; i++)
- map[i] = new int [height];
+ this->width = width;
+ this->height = height;
- for ( i = 0 ; i < width ; i++)
- for ( int j = 0 ; j < height ; j++)
- map[i][j] = 0;
+ map = new int* [width];
+ int i = 0;
+ for ( i = 0 ; i < width ; i++)
+ map[i] = new int [height];
+
+ for ( i = 0 ; i < width ; i++)
+ for ( int j = 0 ; j < height ; j++)
+ map[i][j] = 0;
}
GameMap::~GameMap()
{
for ( int i = 0 ; i < width ; i++)
delete map [i];
delete map;
}
int GameMap::getWidth() { return width; }
int GameMap::getHeight() { return height; }
int GameMap::getTile(int x, int y)
{
if (!inMap(x, y)) return -1;
return map[x][y];
}
+bool GameMap::getChanged()
+{
+ bool result = hasChanged;
+ hasChanged = false;
+ return result;
+}
+
bool GameMap::inMap(int x, int y)
{
- if (x < 0) return false;
- if (y < 0) return false;
- if (x >= width) return false;
- if (y >= height) return false;
- return true;
+ if (x < 0) return false;
+ if (y < 0) return false;
+ if (x >= width) return false;
+ if (y >= height) return false;
+ return true;
}
bool GameMap::isDownBlocking(int x, int y)
{
- if (!inMap(x, y)) return false;
- return (map[x][y] > 0);
+ if (!inMap(x, y)) return false;
+ return (map[x][y] > 0);
}
bool GameMap::isUpBlocking(int x, int y)
{
- if (y < 0) return true;
- if (!inMap(x, y)) return false;
- return (map[x][y] > 0);
+ if (y < 0) return true;
+ if (!inMap(x, y)) return false;
+ return (map[x][y] > 0);
}
bool GameMap::isLeftBlocking(int x, int y)
{
- if (!inMap(x, y)) return false;
- return (map[x][y] > 0);
+ if (!inMap(x, y)) return false;
+ return (map[x][y] > 0);
}
bool GameMap::isRightBlocking(int x, int y)
{
- if (!inMap(x, y)) return false;
- return (map[x][y] > 0);
+ if (!inMap(x, y)) return false;
+ return (map[x][y] > 0);
}
void GameMap::setTile(int x, int y, int n)
{
- if (!inMap(x, y)) return;
- map[x][y] = n;
+ if (!inMap(x, y)) return;
+ map[x][y] = n;
+ hasChanged = true;
}
void GameMap::randomize(int n)
{
+ hasChanged = true;
for ( int i = 0 ; i < width ; i++)
for ( int j = 0 ; j < height ; j++)
map[i][j] = (int) (((float) rand() / (float)RAND_MAX * ((float)n)));
}
void GameMap::loadFromFile(const char* fileName)
{
- ifstream f(fileName);
- if (!f.is_open()) return;
+ hasChanged = true;
+ ifstream f(fileName);
+ if (!f.is_open()) return;
- int n;
- int i;
+ int n;
+ int i;
// dimensions
f >> n;
if (n != width) return;
f >> n;
if (n != height) return;
- for (int j = 0; j < height; j++)
+ for (int j = 0; j < height; j++)
+ {
+ for (i = 0; i < width; i++)
{
- for (i = 0; i < width; i++)
- {
- f >> n;
- map[i][j] = n;
- }
+ f >> n;
+ map[i][j] = n;
}
+ }
- f.close();
+ f.close();
}
diff --git a/src/sfml_game/GameMap.h b/src/sfml_game/GameMap.h
index 30c2e0b..36cf90b 100644
--- a/src/sfml_game/GameMap.h
+++ b/src/sfml_game/GameMap.h
@@ -1,50 +1,52 @@
/** This file is part of sfmlGame.
*
* FreeTumble is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTumble is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FreeTumble. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GAMEMAP_H_INCLUDED
#define GAMEMAP_H_INCLUDED
#include <string>
using namespace std ;
class GameMap
{
public:
GameMap(int width, int height);
virtual ~GameMap();
int getWidth();
int getHeight();
int getTile(int x, int y);
+ bool getChanged();
virtual bool isDownBlocking(int x, int y);
virtual bool isUpBlocking(int x, int y);
virtual bool isLeftBlocking(int x, int y);
virtual bool isRightBlocking(int x, int y);
bool inMap(int x, int y);
void setTile(int x, int y, int n);
virtual void randomize(int n);
virtual void loadFromFile(const char* fileName);
protected:
int width;
int height;
int** map;
+ bool hasChanged;
};
#endif // GAMEMAP_H_INCLUDED
diff --git a/src/sfml_game/TileMapEntity.cpp b/src/sfml_game/TileMapEntity.cpp
index ed93204..350b9d3 100644
--- a/src/sfml_game/TileMapEntity.cpp
+++ b/src/sfml_game/TileMapEntity.cpp
@@ -1,69 +1,91 @@
/** This file is part of sfmlGame.
*
* FreeTumble is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTumble is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FreeTumble. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TileMapEntity.h"
TileMapEntity::TileMapEntity(sf::Texture* image, GameMap* gameMap, int tileWidth, int tileHeight, int tilesProLine)
: GameEntity(0.0f, 0.0f)
{
this->image = image;
this->tileWidth = tileWidth;
this->tileHeight = tileHeight;
this->tilesProLine = tilesProLine;
this->gameMap = gameMap;
this->z = -1.0f;
type = 0;
+ renderStates.texture = image;
+ hasChanged = true;
}
TileMapEntity::~TileMapEntity()
{
}
void TileMapEntity::setMap(GameMap* gameMap)
{
this->gameMap = gameMap;
+ hasChanged = true;
}
int TileMapEntity::getTilesProLine()
{
return tilesProLine;
}
-void TileMapEntity::render(sf::RenderWindow* app)
+bool TileMapEntity::getChanged()
+{
+ bool result = hasChanged;
+ hasChanged = false;
+ return result;
+}
+
+void TileMapEntity::computeVertices()
{
- sf::Sprite tileSprite;
- tileSprite.setTexture(*image);
+ vertices.setPrimitiveType(sf::Quads);
+ vertices.resize(gameMap->getWidth() * gameMap->getHeight() * 4);
- for (int i = 0; i < gameMap->getWidth(); i++)
- for (int j = 0; j < gameMap->getHeight(); j++)
- {
- tileSprite.setPosition(x + (float)(i * tileWidth), y + (float)(j * tileHeight));
- int nx = gameMap->getTile(i, j) % tilesProLine;
- int ny = gameMap->getTile(i, j) / tilesProLine;
+ for (int i = 0; i < gameMap->getWidth(); i++)
+ for (int j = 0; j < gameMap->getHeight(); j++)
+ {
+ int nx = gameMap->getTile(i, j) % tilesProLine;
+ int ny = gameMap->getTile(i, j) / tilesProLine;
+ sf::Vertex* quad = &vertices[(i + j * gameMap->getWidth()) * 4];
- tileSprite.setTextureRect(sf::IntRect( nx * tileWidth, ny * tileHeight,
- /*(nx + 1) **/ tileWidth, /*(ny + 1) **/ tileHeight));
+ quad[0].position = sf::Vector2f(x + i * tileWidth, y + j * tileHeight);
+ quad[1].position = sf::Vector2f(x + (i + 1) * tileWidth, y + j * tileHeight);
+ quad[2].position = sf::Vector2f(x + (i + 1) * tileWidth, y + (j + 1) * tileHeight);
+ quad[3].position = sf::Vector2f(x + i * tileWidth, y + (j + 1) * tileHeight);
- app->draw(tileSprite);
- }
+ quad[0].texCoords = sf::Vector2f(nx * tileWidth, ny * tileHeight);
+ quad[1].texCoords = sf::Vector2f((nx + 1) * tileWidth, ny * tileHeight);
+ quad[2].texCoords = sf::Vector2f((nx + 1) * tileWidth, (ny + 1) * tileHeight);
+ quad[3].texCoords = sf::Vector2f(nx * tileWidth, (ny + 1) * tileHeight);
+ }
+}
+
+void TileMapEntity::render(sf::RenderWindow* app)
+{
+ app->draw(vertices, renderStates);
}
void TileMapEntity::animate(float delay)
{
age += delay;
+ bool needCompute = getChanged() || gameMap->getChanged();
+ if (needCompute) computeVertices();
}
diff --git a/src/sfml_game/TileMapEntity.h b/src/sfml_game/TileMapEntity.h
index bc0dd96..3497bf9 100644
--- a/src/sfml_game/TileMapEntity.h
+++ b/src/sfml_game/TileMapEntity.h
@@ -1,48 +1,54 @@
/** This file is part of sfmlGame.
*
* FreeTumble is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTumble is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FreeTumble. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TILEMAPENTITY_H_INCLUDED
#define TILEMAPENTITY_H_INCLUDED
#include "GameEntity.h"
#include "GameMap.h"
// Basis class for TileMap
class TileMapEntity : public GameEntity
{
public:
- // create a sprite with the entire image
- TileMapEntity(sf::Texture* image, GameMap* gameMap, int tileWidth, int tileHeight, int tilesProLine);
- ~TileMapEntity();
+ TileMapEntity(sf::Texture* image, GameMap* gameMap, int tileWidth, int tileHeight, int tilesProLine);
+ ~TileMapEntity();
- int getTilesProLine();
- void setMap(GameMap* gameMap);
+ int getTilesProLine();
+ void setMap(GameMap* gameMap);
- virtual void render(sf::RenderWindow* app);
+ virtual void render(sf::RenderWindow* app);
virtual void animate(float delay);
+ virtual void computeVertices();
protected:
- int width;
- int height;
- int tileWidth;
- int tileHeight;
- int tilesProLine;
-
- sf::Texture* image;
- GameMap* gameMap;
+ int width;
+ int height;
+ int tileWidth;
+ int tileHeight;
+ int tilesProLine;
+
+ sf::Texture* image;
+ GameMap* gameMap;
+
+ sf::VertexArray vertices;
+ sf::RenderStates renderStates;
+ bool hasChanged;
+
+ bool getChanged();
};
#endif // TILEMAPENTITY_H_INCLUDED

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 11:41 AM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68834
Default Alt Text
(11 KB)

Event Timeline