@@ -1,13 +1,13 @@ | |||
SRC = test-map.cpp video.cpp tiler.cpp | |||
SRC = test-map.cpp video.cpp tiler.cpp layer.cpp map.cpp | |||
all: test-map | |||
test-map: $(SRC:%.cpp=%.o) | |||
g++ -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image` | |||
g++ -g -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image` | |||
%.o: %.cpp | |||
g++ -Wall -O3 -c $^ -o $@ `pkg-config --cflags sdl gl SDL_image` | |||
g++ -g -Wall -O3 -c $^ -o $@ `pkg-config --cflags sdl gl SDL_image` | |||
clean: | |||
rm -f *.o test-map | |||
@@ -0,0 +1,28 @@ | |||
#include "layer.h" | |||
Layer::Layer(int w, int h, FILE *fp) | |||
{ | |||
width = w; | |||
height = h; | |||
data = new unsigned int[w * h]; | |||
fread(data, sizeof(unsigned int), width * height, fp); | |||
for (int n = 0; n < width * height; n++) | |||
{ | |||
unsigned int i = data[n]; | |||
// XXX: endianness swapping might be necessary here | |||
data[n] = i ? i - 1 : 0; | |||
} | |||
} | |||
Layer::~Layer() | |||
{ | |||
delete data; | |||
} | |||
unsigned int Layer::GetTile(int x, int y) | |||
{ | |||
return data[y * width + x]; | |||
} | |||
@@ -0,0 +1,21 @@ | |||
#if !defined __LAYER_H__ | |||
#define __LAYER_H__ | |||
#include <cstdio> | |||
class Layer | |||
{ | |||
public: | |||
Layer(int w, int h, FILE *fp); | |||
~Layer(); | |||
unsigned int GetTile(int x, int y); | |||
//private: | |||
int width, height; | |||
unsigned int *data; | |||
}; | |||
#endif // __LAYER_H__ | |||
@@ -0,0 +1,51 @@ | |||
#include <cstdio> | |||
#include <malloc.h> | |||
#include "map.h" | |||
#include "layer.h" | |||
Map::Map(char const *path) : | |||
layers(0), | |||
nlayers(0) | |||
{ | |||
char tmp[1024]; | |||
sprintf(tmp, "grep '\\(^ [^< ]\\|layer name\\)' %s | while read i; do echo \"$i\"; read i; echo -n \"$i\" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | gunzip; done", path); | |||
FILE *fp = popen(tmp, "r"); | |||
while (fp && !feof(fp)) | |||
{ | |||
char name[1024]; | |||
int width, height; | |||
fscanf(fp, "<layer name=\"%[^\"]\" ", name); | |||
if (feof(fp)) | |||
break; | |||
fscanf(fp, "width=\"%u\" ", &width); | |||
fscanf(fp, "height=\"%u\" ", &height); | |||
fgets(tmp, 1024, fp); // Ignore rest of line | |||
layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1)); | |||
layers[nlayers] = new Layer(width, height, fp); | |||
nlayers++; | |||
} | |||
pclose(fp); | |||
} | |||
Map::~Map() | |||
{ | |||
for (int i = 0; i < nlayers; i++) | |||
delete layers[i]; | |||
free(layers); | |||
} | |||
void Map::Draw(Tiler *tiler) | |||
{ | |||
for (int i = 0; i < nlayers; i++) | |||
for (int y = 0; y < 32; y++) | |||
for (int x = 0; x < 32; x++) | |||
tiler->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, i); | |||
} | |||
@@ -0,0 +1,19 @@ | |||
#include <cstdio> | |||
#include "layer.h" | |||
#include "tiler.h" | |||
class Map | |||
{ | |||
public: | |||
Map(char const *path); | |||
~Map(); | |||
void Draw(Tiler *tiler); | |||
private: | |||
Layer **layers; | |||
int nlayers; | |||
}; | |||
@@ -7,74 +7,29 @@ | |||
#include "video.h" | |||
#include "tiler.h" | |||
#include "map.h" | |||
/* Global objects */ | |||
Video *video; | |||
Tiler *tiler; | |||
/* Storage for map layers */ | |||
int *layers[128]; | |||
int width = 32, height = 32; | |||
int nlayers = 0; | |||
void LoadMap(void) | |||
{ | |||
FILE *fp = popen("grep '^ [^< ]' maps/testmap.tmx | while read i; do echo -n \"$i\" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | gunzip; done", "r"); | |||
while (fp && !feof(fp)) | |||
{ | |||
layers[nlayers] = (int *)malloc(width * height * sizeof(int)); | |||
fread(layers[nlayers], sizeof(int), width * height, fp); | |||
if (feof(fp)) | |||
{ | |||
free(layers[nlayers]); | |||
layers[nlayers] = 0; | |||
fclose(fp); | |||
break; | |||
} | |||
for (int n = 0; n < width * height; n++) | |||
{ | |||
unsigned int i = layers[nlayers][n]; | |||
//i = (i >> 24) | ((i >> 8) & 0xff00) | ((i << 8) & 0xff0000) | (i << 24); | |||
layers[nlayers][n] = i ? i - 1 : 0; | |||
} | |||
nlayers++; | |||
} | |||
} | |||
/* The main drawing function. */ | |||
void DrawScene() | |||
int main(int argc, char **argv) | |||
{ | |||
video->Clear(); | |||
for (int i = 0; i < nlayers; i++) | |||
for (int y = 0; y < height; y++) | |||
for (int x = 0; x < width; x++) | |||
tiler->AddTile(layers[i][y * width + x], x * 32, y * 32, i); | |||
Video *video = new Video("Deus Hax", 640, 480); | |||
Tiler *tiler = new Tiler(); | |||
Map *map = new Map("maps/testmap-coll.tmx"); | |||
/* Test stuff */ | |||
int playerx, playery; | |||
SDL_GetMouseState(&playerx, &playery); | |||
tiler->AddTile(50, playerx, playery, 1); | |||
tiler->AddTile(50, playerx + 64, playery + 32, 3); | |||
tiler->Render(); | |||
video->Refresh(33.33333f); | |||
} | |||
for (int done = 0; !done; ) | |||
{ | |||
video->Clear(); | |||
int main(int argc, char **argv) | |||
{ | |||
video = new Video("Deus Hax", 640, 480); | |||
tiler = new Tiler(); | |||
map->Draw(tiler); | |||
int done; | |||
/* Test stuff */ | |||
int playerx, playery; | |||
SDL_GetMouseState(&playerx, &playery); | |||
/* Loop, drawing and checking events */ | |||
LoadMap(); | |||
tiler->AddTile(50, playerx, playery, 1); | |||
tiler->AddTile(50, playerx + 64, playery + 32, 3); | |||
done = 0; | |||
while (!done) | |||
{ | |||
DrawScene(); | |||
tiler->Render(); | |||
video->Refresh(33.33333f); | |||
/* This could go in a separate function */ | |||
SDL_Event event; | |||
@@ -92,6 +47,7 @@ int main(int argc, char **argv) | |||
} | |||
} | |||
delete map; | |||
delete tiler; | |||
delete video; | |||
@@ -4,6 +4,9 @@ | |||
* The tile manager | |||
*/ | |||
#if !defined __TILER_H__ | |||
#define __TILER_H__ | |||
class TilerData; | |||
class Tiler | |||
@@ -20,3 +23,5 @@ private: | |||
TilerData *data; | |||
}; | |||
#endif // __TILER_H__ | |||