@@ -1,12 +1,25 @@ | |||||
#include "layer.h" | #include "layer.h" | ||||
Layer::Layer(int w, int h, FILE *fp) | |||||
Layer::Layer(char const *name, int w, int h, FILE *fp) | |||||
{ | { | ||||
width = w; | width = w; | ||||
height = h; | height = h; | ||||
data = new unsigned int[w * h]; | data = new unsigned int[w * h]; | ||||
if (sscanf(name, "Ground %d", &z) == 1) | |||||
; | |||||
else if (sscanf(name, "Ground Decal %d", &z) == 1) | |||||
; | |||||
else if (sscanf(name, "Object %d", &z) == 1) | |||||
; | |||||
else if (sscanf(name, "Wall %d", &z) == 1) | |||||
z++; | |||||
else if (sscanf(name, "Wall Decal %d", &z) == 1) | |||||
z++; | |||||
else | |||||
z = -1; | |||||
fread(data, sizeof(unsigned int), width * height, fp); | fread(data, sizeof(unsigned int), width * height, fp); | ||||
for (int n = 0; n < width * height; n++) | for (int n = 0; n < width * height; n++) | ||||
{ | { | ||||
@@ -21,6 +34,11 @@ Layer::~Layer() | |||||
delete data; | delete data; | ||||
} | } | ||||
int Layer::GetZ() | |||||
{ | |||||
return z; | |||||
} | |||||
unsigned int Layer::GetTile(int x, int y) | unsigned int Layer::GetTile(int x, int y) | ||||
{ | { | ||||
return data[y * width + x]; | return data[y * width + x]; | ||||
@@ -1,21 +1,26 @@ | |||||
#if !defined __LAYER_H__ | |||||
#define __LAYER_H__ | |||||
/* | |||||
* The layer object | |||||
*/ | |||||
#if !defined __DH_LAYER_H__ | |||||
#define __DH_LAYER_H__ | |||||
#include <cstdio> | #include <cstdio> | ||||
class Layer | class Layer | ||||
{ | { | ||||
public: | public: | ||||
Layer(int w, int h, FILE *fp); | |||||
Layer(char const *name, int w, int h, FILE *fp); | |||||
~Layer(); | ~Layer(); | ||||
int GetZ(); | |||||
unsigned int GetTile(int x, int y); | unsigned int GetTile(int x, int y); | ||||
//private: | |||||
int width, height; | |||||
private: | |||||
int width, height, z; | |||||
unsigned int *data; | unsigned int *data; | ||||
}; | }; | ||||
#endif // __LAYER_H__ | |||||
#endif // __DH_LAYER_H__ | |||||
@@ -27,7 +27,7 @@ Map::Map(char const *path) : | |||||
fscanf(fp, "height=\"%u\" ", &height); | fscanf(fp, "height=\"%u\" ", &height); | ||||
fgets(tmp, 1024, fp); // Ignore rest of line | fgets(tmp, 1024, fp); // Ignore rest of line | ||||
layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1)); | layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1)); | ||||
layers[nlayers] = new Layer(width, height, fp); | |||||
layers[nlayers] = new Layer(name, width, height, fp); | |||||
nlayers++; | nlayers++; | ||||
} | } | ||||
@@ -44,8 +44,12 @@ Map::~Map() | |||||
void Map::Draw(Tiler *tiler) | void Map::Draw(Tiler *tiler) | ||||
{ | { | ||||
for (int i = 0; i < nlayers; i++) | for (int i = 0; i < nlayers; i++) | ||||
{ | |||||
int z = layers[i]->GetZ(); | |||||
for (int y = 0; y < 32; y++) | for (int y = 0; y < 32; y++) | ||||
for (int x = 0; x < 32; x++) | for (int x = 0; x < 32; x++) | ||||
tiler->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, i); | |||||
tiler->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z); | |||||
} | |||||
} | } | ||||
@@ -1,4 +1,11 @@ | |||||
/* | |||||
* The map object | |||||
*/ | |||||
#if !defined __DH_MAP_H__ | |||||
#define __DH_MAP_H__ | |||||
#include <cstdio> | #include <cstdio> | ||||
#include "layer.h" | #include "layer.h" | ||||
@@ -17,3 +24,5 @@ private: | |||||
int nlayers; | int nlayers; | ||||
}; | }; | ||||
#endif // __DH_MAP_H__ | |||||
@@ -13,7 +13,7 @@ int main(int argc, char **argv) | |||||
{ | { | ||||
Video *video = new Video("Deus Hax", 640, 480); | Video *video = new Video("Deus Hax", 640, 480); | ||||
Tiler *tiler = new Tiler(); | Tiler *tiler = new Tiler(); | ||||
Map *map = new Map("maps/testmap-coll.tmx"); | |||||
Map *map = new Map("maps/testmap-grass.tmx"); | |||||
for (int done = 0; !done; ) | for (int done = 0; !done; ) | ||||
{ | { | ||||
@@ -24,9 +24,10 @@ int main(int argc, char **argv) | |||||
/* Test stuff */ | /* Test stuff */ | ||||
int playerx, playery; | int playerx, playery; | ||||
SDL_GetMouseState(&playerx, &playery); | SDL_GetMouseState(&playerx, &playery); | ||||
playerx = playerx * (640 - 32) / 640; | |||||
playery = playery * (480 - 32) / 480; | |||||
tiler->AddTile(50, playerx, playery, 1); | tiler->AddTile(50, playerx, playery, 1); | ||||
tiler->AddTile(50, playerx + 64, playery + 32, 3); | |||||
tiler->Render(); | tiler->Render(); | ||||
video->Refresh(33.33333f); | video->Refresh(33.33333f); | ||||
@@ -1,11 +1,10 @@ | |||||
/* | /* | ||||
* The tile manager | * The tile manager | ||||
*/ | */ | ||||
#if !defined __TILER_H__ | |||||
#define __TILER_H__ | |||||
#if !defined __DH_TILER_H__ | |||||
#define __DH_TILER_H__ | |||||
class TilerData; | class TilerData; | ||||
@@ -23,5 +22,5 @@ private: | |||||
TilerData *data; | TilerData *data; | ||||
}; | }; | ||||
#endif // __TILER_H__ | |||||
#endif // __DH_TILER_H__ | |||||
@@ -1,4 +1,11 @@ | |||||
/* | |||||
* The video driver | |||||
*/ | |||||
#if !defined __DH_VIDEO_H__ | |||||
#define __DH_VIDEO_H__ | |||||
class VideoData; | class VideoData; | ||||
class Video | class Video | ||||
@@ -16,3 +23,6 @@ public: | |||||
private: | private: | ||||
VideoData *data; | VideoData *data; | ||||
}; | }; | ||||
#endif // __DH_VIDEO_H__ | |||||