From: cboissie Date: Thu, 12 May 2011 05:17:20 +0000 (+0200) Subject: Tests with Qt X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=a42aa6106c7f459b355b545ebd7ad94faf6c61d3;p=Bug_Attack.git Tests with Qt --- diff --git a/Bug_Attack.pro b/Bug_Attack.pro index 35561fe..0253ae5 100644 --- a/Bug_Attack.pro +++ b/Bug_Attack.pro @@ -12,9 +12,11 @@ TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp \ - tile.cpp + tile.cpp \ + map.cpp HEADERS += mainwindow.h \ - tile.h + tile.h \ + map.h FORMS += mainwindow.ui diff --git a/mainwindow.cpp b/mainwindow.cpp index f8847ff..91d14cf 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -14,19 +14,10 @@ MainWindow::MainWindow(QWidget *parent) : _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() diff --git a/mainwindow.h b/mainwindow.h index 5342c79..cd2ed0c 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -6,6 +6,8 @@ #include #include +#include "map.h" + namespace Ui { class MainWindow; } @@ -24,7 +26,9 @@ private: QGraphicsScene *_scene; QGraphicsView *_view; - QGraphicsPixmapItem *_grassTile; + + Map *_currentMap; + }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 1c681f6..9d1d5c9 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -199,9 +199,6 @@ Qt::ScrollBarAlwaysOff - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - diff --git a/map.cpp b/map.cpp new file mode 100644 index 0000000..f8a872b --- /dev/null +++ b/map.cpp @@ -0,0 +1,63 @@ +#include +#include +#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 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(){ + + +} diff --git a/map.h b/map.h new file mode 100644 index 0000000..02c7803 --- /dev/null +++ b/map.h @@ -0,0 +1,25 @@ +#ifndef MAP_H +#define MAP_H + +#include +#include +#include +#include "tile.h" + +class Map +{ + +private: + + // QList* _ennemyList; + Tile* _mapTiles[16][16]; + QGraphicsScene* _scene; + + +public: + Map(QGraphicsScene* scene); + ~Map(); + void LoadBackground(void); +}; + +#endif // MAP_H diff --git a/media/maps/map.txt b/media/maps/map.txt new file mode 100644 index 0000000..b8715a2 --- /dev/null +++ b/media/maps/map.txt @@ -0,0 +1,16 @@ +0 0 18 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 6 4 4 4 4 4 4 4 4 6 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 6 4 6 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 +0 0 10 8 8 8 8 8 8 8 8 8 8 10 0 0 +0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 2 0 0 0 64 64 64 0 0 0 0 0 0 0 +0 0 6 4 6 0 64 64 64 64 0 0 0 0 0 0 +0 0 0 0 2 0 0 64 64 64 0 0 0 0 0 0 +0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 6 4 4 4 4 4 4 4 4 6 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 32 0 0 diff --git a/media/textures/goal.png b/media/textures/goal.png new file mode 100644 index 0000000..531163d Binary files /dev/null and b/media/textures/goal.png differ diff --git a/media/textures/hpath.jpg b/media/textures/hpath.jpg new file mode 100644 index 0000000..992566b Binary files /dev/null and b/media/textures/hpath.jpg differ diff --git a/media/textures/mud.jpg b/media/textures/mud.jpg new file mode 100644 index 0000000..ea47f91 Binary files /dev/null and b/media/textures/mud.jpg differ diff --git a/media/textures/nepath.jpg b/media/textures/nepath.jpg new file mode 100644 index 0000000..236d708 Binary files /dev/null and b/media/textures/nepath.jpg differ diff --git a/media/textures/nopath.jpg b/media/textures/nopath.jpg new file mode 100644 index 0000000..5b380a4 Binary files /dev/null and b/media/textures/nopath.jpg differ diff --git a/media/textures/vpath.jpg b/media/textures/vpath.jpg new file mode 100644 index 0000000..ef351c8 Binary files /dev/null and b/media/textures/vpath.jpg differ diff --git a/tile.cpp b/tile.cpp index 2b5a245..7683c9c 100644 --- a/tile.cpp +++ b/tile.cpp @@ -1,8 +1,43 @@ #include "tile.h" +#include +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(){} diff --git a/tile.h b/tile.h index 562cff2..b591939 100644 --- a/tile.h +++ b/tile.h @@ -6,7 +6,13 @@ class Tile : public QGraphicsPixmapItem { public: - Tile(); + + Tile(int tileNumber); + ~Tile(void); + + QString TexName(int n); + + }; #endif // TILE_H