SOURCES += main.cpp\
mainwindow.cpp \
- tile.cpp
+ tile.cpp \
+ map.cpp
HEADERS += mainwindow.h \
- tile.h
+ tile.h \
+ map.h
FORMS += mainwindow.ui
_view = ui->graphicsView;
_view->setScene(_scene);
- QPixmap grass("media/textures/grass.jpg");
- grass = grass.scaled(32,32,Qt::IgnoreAspectRatio);
- for(int i = 0; i < 16; ++i){
- for(int j = 0; j < 16 ; ++j){
-
- _grassTile = _scene->addPixmap(grass);
- _grassTile->setPos(32*i,32*j);
-
- }
- }
+ _currentMap = new Map(_scene);
+ _currentMap->LoadBackground();
_view->show();
-
}
MainWindow::~MainWindow()
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
+#include "map.h"
+
namespace Ui {
class MainWindow;
}
QGraphicsScene *_scene;
QGraphicsView *_view;
- QGraphicsPixmapItem *_grassTile;
+
+ Map *_currentMap;
+
};
#endif // MAINWINDOW_H
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
- <property name="alignment">
- <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
- </property>
</widget>
</widget>
</widget>
--- /dev/null
+#include <iostream>
+#include <fstream>
+#include "map.h"
+
+
+
+Map::Map(QGraphicsScene* scene)
+{
+
+ // Container
+ _scene = scene;
+
+ /* // Loads map file
+ QFile mapFile("media/maps/map.txt");
+ if(!mapFile.open(QIODevice::ReadOnly | QIODevice::Text))
+ return;
+
+
+
+ // Reads map data and creates corresponding map tiles
+ int y = 0;
+ while(!mapFile.atEnd()){
+ QByteArray line = mapFile.readLine();
+ QList<QByteArray> tileNumber = line.split(' ');
+ for(int x = 0; x < tileNumber.size(); ++x)
+ _mapTiles[x][y] = new Tile(tileNumber[x].toInt());
+ y++;
+ }
+
+ // Close file
+ mapFile.close();*/
+
+ std::ifstream fichier("media/maps/map.txt");
+ int i, j;
+ int num;
+ if(fichier)
+ {
+ for(i = 0; i < 16; i++)
+ {
+ for(j = 0; j < 16; j++)
+ {
+ fichier >> num;
+ std::cout << num << std::endl;
+ _mapTiles[i][j] = new Tile(num);
+ }
+ }
+ }
+}
+
+void Map::LoadBackground(void) {
+
+ for(int x = 0 ; x < 16 ; ++x){
+ for(int y = 0 ; y < 16 ; ++y){
+ _mapTiles[x][y]->setPos(y*32,x*32);
+ _scene->addItem(_mapTiles[x][y]);
+ }
+ }
+}
+
+Map::~Map(){
+
+
+}
--- /dev/null
+#ifndef MAP_H
+#define MAP_H
+
+#include <QFile>
+#include <QList>
+#include <QGraphicsScene>
+#include "tile.h"
+
+class Map
+{
+
+private:
+
+ // QList<int>* _ennemyList;
+ Tile* _mapTiles[16][16];
+ QGraphicsScene* _scene;
+
+
+public:
+ Map(QGraphicsScene* scene);
+ ~Map();
+ void LoadBackground(void);
+};
+
+#endif // MAP_H
--- /dev/null
+0 0 18 0 0 0 0 0 0 0 0 0 0 0 0 0 \r
+0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 \r
+0 0 6 4 4 4 4 4 4 4 4 6 0 0 0 0 \r
+0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 \r
+0 0 0 0 0 0 0 0 0 0 0 6 4 6 0 0 \r
+0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 \r
+0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 \r
+0 0 10 8 8 8 8 8 8 8 8 8 8 10 0 0 \r
+0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 \r
+0 0 2 0 0 0 64 64 64 0 0 0 0 0 0 0 \r
+0 0 6 4 6 0 64 64 64 64 0 0 0 0 0 0 \r
+0 0 0 0 2 0 0 64 64 64 0 0 0 0 0 0 \r
+0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 \r
+0 0 0 0 6 4 4 4 4 4 4 4 4 6 0 0 \r
+0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 \r
+0 0 0 0 0 0 0 0 0 0 0 0 0 32 0 0 \r
#include "tile.h"
+#include <iostream>
+using namespace std;
-Tile::Tile():QGraphicsPixmapItem()
+Tile::Tile(int tileNumber)
{
- this->setPixmap(QPixmap("media/textures/grass.jpg"));
+ QString filePath("media/textures/");
+ filePath+= TexName(tileNumber); // TEST
+ filePath+= QString(".jpg");
+ cout << filePath.toStdString() << endl;
+ QPixmap texFile(filePath);
+ texFile.scaled(32,32,Qt::IgnoreAspectRatio);
+ this->setPixmap(texFile);
+}
+
+QString Tile::TexName(int n){
+
+ // Normal textures
+ if(n>0 && n<32){
+
+ n%=16; // To keep a value between 0 and 10
+ if(n==0)
+ return "grass";
+ if(n==1 || n==2)
+ return "hpath";
+ if(n==4 || n==8)
+ return "vpath";
+ if(n==5 || n==10)
+ return "nepath";
+ if(n==6 || n==9)
+ return "nopath";
+
+
+ }
+ if(n==64)
+ return "mud";
+ // Special textures
+ return "grass";
}
+
+Tile::~Tile(){}
class Tile : public QGraphicsPixmapItem
{
public:
- Tile();
+
+ Tile(int tileNumber);
+ ~Tile(void);
+
+ QString TexName(int n);
+
+
};
#endif // TILE_H