|
|
@ -1,5 +1,6 @@ |
|
|
|
#include "Map.h"
|
|
|
|
|
|
|
|
|
|
|
|
Tile tileIndex(int b, int g, int r) { |
|
|
|
if(b == 255 && g == 255 && r == 255) { |
|
|
|
return GRASS; |
|
|
@ -25,10 +26,10 @@ void Quad::boundstring() { |
|
|
|
} |
|
|
|
|
|
|
|
void Quad::free() { |
|
|
|
if(bound.w*bound.h == 1) { |
|
|
|
delete this; |
|
|
|
return; |
|
|
|
} |
|
|
|
//if(bound.w*bound.h == 1) {
|
|
|
|
// delete this;
|
|
|
|
// return;
|
|
|
|
//}
|
|
|
|
if(tl) |
|
|
|
tl->free(); |
|
|
|
tl = NULL; |
|
|
@ -41,6 +42,7 @@ void Quad::free() { |
|
|
|
if(br) |
|
|
|
br->free(); |
|
|
|
br = NULL; |
|
|
|
delete this; |
|
|
|
} |
|
|
|
|
|
|
|
inline bool Quad::inbounds(int x, int y) { |
|
|
@ -96,37 +98,39 @@ Tile Quad::lookup(int x, int y) { |
|
|
|
if(bound.w*bound.h == 1) { |
|
|
|
return data; |
|
|
|
} |
|
|
|
if(x - bound.x < bound.w/2) { |
|
|
|
if(y - bound.y < bound.h/2) { |
|
|
|
if(bound.x + bound.w/2 > x) { //left
|
|
|
|
if(bound.y + bound.h/2 > y) { //top
|
|
|
|
if(tl) |
|
|
|
return tl->lookup(x,y); |
|
|
|
return WALL; |
|
|
|
} |
|
|
|
if(tr) |
|
|
|
return tr->lookup(x,y); |
|
|
|
return WALL; |
|
|
|
} else { |
|
|
|
if(y - bound.y < bound.h/2) { |
|
|
|
} else { //bottom
|
|
|
|
if(bl) |
|
|
|
return bl->lookup(x,y); |
|
|
|
return WALL; |
|
|
|
} |
|
|
|
if(br) |
|
|
|
return br->lookup(x,y); |
|
|
|
return WALL; |
|
|
|
} else { //right
|
|
|
|
if(bound.y + bound.h/2 > y) { |
|
|
|
if(tr) |
|
|
|
return tr->lookup(x,y); |
|
|
|
} else { |
|
|
|
if(br) |
|
|
|
return br->lookup(x,y); |
|
|
|
} |
|
|
|
} |
|
|
|
return WALL; |
|
|
|
} |
|
|
|
|
|
|
|
void Quad::print(int spaces) { |
|
|
|
for(int i = 0; i < spaces; ++i) { |
|
|
|
std::cout << " "; |
|
|
|
} |
|
|
|
if(data == WALL) |
|
|
|
if(bound.w*bound.h == 1) { |
|
|
|
std::cout << bound.x << "," << bound.y << ":" << data << "\n"; |
|
|
|
} |
|
|
|
/*if(data == WALL)
|
|
|
|
std::cout << "WALL\n"; |
|
|
|
if(data == STONE) |
|
|
|
std::cout << "STONE\n"; |
|
|
|
if(data == GRASS) |
|
|
|
std::cout << "GRASS\n"; |
|
|
|
std::cout << "GRASS\n";*/ |
|
|
|
if(tl) |
|
|
|
tl->print(spaces+2); |
|
|
|
if(tr) |
|
|
@ -156,7 +160,21 @@ Map::~Map() { |
|
|
|
} |
|
|
|
|
|
|
|
void Map::loadFromFile(std::string fpath) { |
|
|
|
FILE* f = fopen(fpath.c_str(),"rb"); |
|
|
|
|
|
|
|
cv::Mat image = cv::imread(fpath.c_str()); |
|
|
|
std::cout << "Image size - Width: " << image.cols << " Height: " << image.rows << "\n"; |
|
|
|
tiles->init(image.cols,image.rows); |
|
|
|
for(int x = 0; x < image.cols; ++x) { |
|
|
|
for(int y = 0; y < image.rows; ++y) { |
|
|
|
cv::Vec3b pixel = image.at<cv::Vec3b>(y,x); //Transposed...
|
|
|
|
Tile t = tileIndex(pixel.val[0],pixel.val[1],pixel.val[2]); |
|
|
|
//std::cout << "Tile at " << x << "," << y << " is " << t << "\n";
|
|
|
|
tiles->insert(x,y,t); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* FILE* f = fopen(fpath.c_str(),"rb");
|
|
|
|
int w,h; |
|
|
|
fread(&w,sizeof(int),1,f); |
|
|
|
fread(&h,sizeof(int),1,f); |
|
|
@ -169,13 +187,13 @@ void Map::loadFromFile(std::string fpath) { |
|
|
|
tiles->insert(i/w,i%w,static_cast<Tile>(t)); |
|
|
|
//std::cout << "Loading " << t << "\n";
|
|
|
|
} |
|
|
|
fclose(f); |
|
|
|
numTilesX = w; |
|
|
|
numTilesY = h; |
|
|
|
fclose(f);*/ |
|
|
|
numTilesX = image.cols; |
|
|
|
numTilesY = image.rows; |
|
|
|
} |
|
|
|
|
|
|
|
void Map::render(SDL_Rect camera) { |
|
|
|
std::cout << "Rendering map\n"; |
|
|
|
//std::cout << "Rendering map\n";
|
|
|
|
for(int x = camera.x/TILE_SIZE; x < 1+(camera.x + camera.w)/TILE_SIZE; ++x){ |
|
|
|
for(int y = camera.y/TILE_SIZE; y < 1+(camera.y + camera.h)/TILE_SIZE; ++y) { |
|
|
|
Tile t = tiles->lookup(x,y); |
|
|
@ -183,6 +201,7 @@ void Map::render(SDL_Rect camera) { |
|
|
|
textures[t].render(x*TILE_SIZE - camera.x,y*TILE_SIZE - camera.y); |
|
|
|
} |
|
|
|
} |
|
|
|
//exit(1);
|
|
|
|
} |
|
|
|
|
|
|
|
void Map::print() { |
|
|
|