Commit 6770873f authored by Robert Scott James's avatar Robert Scott James

Game object has a char** and nTiles variable that contains an unsigned char...

Game object has a char** and nTiles variable that contains an unsigned char array which has an RGBA element for each pixel row major order
Added an empty tiles drawing function.

 Changes to be committed:
	modified:   ../src/entity/vision/vision.cpp
	modified:   ../src/entity/vision/vision.h
	modified:   ../src/game/game.cpp
	modified:   ../src/game/game.h
parent 711a54d1
...@@ -168,3 +168,8 @@ void Vision::drawLevel(Display*& dpy,Window& w,GC& gc,int scale){ ...@@ -168,3 +168,8 @@ void Vision::drawLevel(Display*& dpy,Window& w,GC& gc,int scale){
} }
} }
} }
void Vision::drawTiles(Display *& dpy, Window &w, GC &gc,int scale, unsigned char **tiles){
}
...@@ -9,6 +9,7 @@ class Vision: public Entity { // This class is supposed to give the hero, mob, a ...@@ -9,6 +9,7 @@ class Vision: public Entity { // This class is supposed to give the hero, mob, a
char canSeeTile(int, int, int, int); char canSeeTile(int, int, int, int);
void updateFOW(); void updateFOW();
void drawLevel(Display*&,Window&,GC&,int); void drawLevel(Display*&,Window&,GC&,int);
void drawTiles(Display*&,Window&,GC&,int,unsigned char **);
protected: protected:
TILE_TYPES *fov; TILE_TYPES *fov;
int viewDistance; int viewDistance;
......
...@@ -48,7 +48,7 @@ void Game::loop(){ ...@@ -48,7 +48,7 @@ void Game::loop(){
// 50 Levels for now // 50 Levels for now
for(dungeonLevel = 0; dungeonLevel < 50; dungeonLevel++){ for(dungeonLevel = 0; dungeonLevel < 1; dungeonLevel++){
// Generate a dungeon for the first level // Generate a dungeon for the first level
unsigned long milliseconds_since_epoch = std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1); unsigned long milliseconds_since_epoch = std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
...@@ -116,7 +116,7 @@ void Game::loop(){ ...@@ -116,7 +116,7 @@ void Game::loop(){
theHero->draw(dpy,double_buffer,gc,scale); theHero->draw(dpy,double_buffer,gc,scale);
} else { } else {
// Need to draw using the tiles in here // Need to draw using the tiles in here
theHero->drawTiles(dpy,double_buffer,gc,scale,tiles);
} }
...@@ -143,7 +143,7 @@ void Game::loop(){ ...@@ -143,7 +143,7 @@ void Game::loop(){
} }
int Game::readTiles(std::string tilesFilename){ int Game::readTiles(std::string tilesFilename){
unsigned int error; unsigned int error, tileFileWidth, tileHeight;
unsigned char* image; unsigned char* image;
error = lodepng_decode32_file(&image, &tileFileWidth, &tileHeight, tilesFilename.c_str()); error = lodepng_decode32_file(&image, &tileFileWidth, &tileHeight, tilesFilename.c_str());
...@@ -151,40 +151,29 @@ int Game::readTiles(std::string tilesFilename){ ...@@ -151,40 +151,29 @@ int Game::readTiles(std::string tilesFilename){
std::cerr << "Error " << error << ": " << lodepng_error_text(error) << std::endl; std::cerr << "Error " << error << ": " << lodepng_error_text(error) << std::endl;
return(1); return(1);
} }
error = 0; // Reset the error variable so I can use it again later
// Now we need to split up the image into it's individual tiles
nTiles = floor(tileFileWidth/tileHeight);
// Allocate the memory for the tiles
tiles = new unsigned char *[nTiles];
for(unsigned int n = 0; n < nTiles; n++){
tiles[n] = new unsigned char[4 * tileHeight * tileHeight];
}
for(int n = 0; n < nTiles; n++){
/* this is the complete tiles image in an array */ for(unsigned int i = 0; i < tileHeight; i++){
/* Need to split this up into the individual tiles else it's becomes useless */ for(unsigned int j = 0; j < tileHeight; j++){
/* This splitting assumes that each tile in the image is in the order that the types are declared in the types header */ // Find the element in the tile char array for the start of the current pixel
/* Going to be horrible and assume that each tile in the file has a coded counterpart */ int tileOffset = 4 * j + 4 * (i * tileHeight); //
/* Each tile is assumed to be horizontally placed next to the next one */ // Find the respective element in the image char array for the start of the current pixel
unsigned char *tile; int imageOffset = ((4*j) + (n * tileHeight * 4)) + (4 * i * tileFileWidth);//
for(unsigned int img = 0; img < (tileFileWidth/tileHeight); img++){ // Memcpy 4 unsigned chars from the imageOffset address to the tileOffset address
// Going to try and create split versions of the tiles in the code and go from there memcpy(&tiles[n][tileOffset],&image[imageOffset],4 * sizeof(unsigned char));
tile = new unsigned char[tileHeight * tileHeight ];
for(unsigned int y = 0; y < tileHeight; y++){ //each line in this new tile calculate it's value in the tiledImage and assign the values to the temporary array
unsigned int offset = y * tileFileWidth;
memcpy (&tile[tileHeight * y], &image[offset], sizeof(char) * tileHeight ); // Assuming tileHeight is the same as tileWidth which it should be because square tiles
/*Encode the image*/
char *tmpfilename;
tmpfilename = new char[20];
snprintf(tmpfilename,20,"tile%u.png",img);
error = 0;
error = lodepng_encode32_file(tmpfilename, tile, tileHeight, tileHeight);
/*if there's an error, display it*/
if(error){
std::cerr << "Error " << error << ": " << lodepng_error_text(error) << std::endl;
return(1);
} }
} }
delete [] tile;
} }
// Now the individual tiles are stored we can free the original image
free(image);
// Now the individual tiles are stored we can free the original image return(0);
free(image);
return(0);
} }
...@@ -20,12 +20,12 @@ class Game { ...@@ -20,12 +20,12 @@ class Game {
int readTiles(std::string); int readTiles(std::string);
void loop(); void loop();
protected: protected:
int nTiles;
int const rootWindow; int const rootWindow;
unsigned int tileFileWidth, tileHeight;
std::vector<std::shared_ptr<Dungeon>> levels; std::vector<std::shared_ptr<Dungeon>> levels;
std::vector<std::shared_ptr<Entity>> entities; std::vector<std::shared_ptr<Entity>> entities;
Hero *theHero; Hero *theHero;
unsigned char **tiles;
int dungeonLevel; int dungeonLevel;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment