Sfoglia il codice sorgente

Support CSV .tmx files to get rid of that pathetic system() call.

legacy
Sam Hocevar sam 14 anni fa
parent
commit
4537534dcf
9 ha cambiato i file con 69 aggiunte e 75 eliminazioni
  1. +1
    -1
      src/Makefile
  2. +4
    -4
      src/layer.cpp
  3. +4
    -3
      src/layer.h
  4. +35
    -42
      src/map.cpp
  5. +2
    -2
      src/map.h
  6. +6
    -6
      src/test-map.cpp
  7. +11
    -11
      src/tileset.cpp
  8. +5
    -5
      src/tileset.h
  9. +1
    -1
      src/video.cpp

+ 1
- 1
src/Makefile Vedi File

@@ -1,5 +1,5 @@

SRC = test-map.cpp video.cpp tiler.cpp layer.cpp map.cpp
SRC = test-map.cpp video.cpp tileset.cpp layer.cpp map.cpp

all: test-map



+ 4
- 4
src/layer.cpp Vedi File

@@ -1,12 +1,12 @@

#include "layer.h"

Layer::Layer(int w, int h, int z, char const *base64)
Layer::Layer(int w, int h, int in_z, uint32_t *in_data)
{
width = w;
height = h;
level = z;
data = new unsigned int[w * h];
z = in_z;
data = in_data;

#if 0
fread(data, sizeof(unsigned int), width * height, fp);
@@ -26,7 +26,7 @@ Layer::~Layer()

int Layer::GetZ()
{
return level;
return z;
}

unsigned int Layer::GetTile(int x, int y)


+ 4
- 3
src/layer.h Vedi File

@@ -7,19 +7,20 @@
#define __DH_LAYER_H__

#include <cstdio>
#include <stdint.h>

class Layer
{
public:
Layer(int w, int h, int z, char const *base64);
Layer(int w, int h, int z, uint32_t *data);
~Layer();

int GetZ();
unsigned int GetTile(int x, int y);

private:
int width, height, level;
unsigned int *data;
int width, height, z;
uint32_t *data;
};

#endif // __DH_LAYER_H__


+ 35
- 42
src/map.cpp Vedi File

@@ -1,6 +1,9 @@

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <malloc.h>
#include <ctype.h>

#include "map.h"
#include "layer.h"
@@ -10,7 +13,9 @@ Map::Map(char const *path) :
nlayers(0)
{
char tmp[BUFSIZ];
int firstgid = 0, width = 0, height = 0, level = 0, data = 0;
uint32_t *data = NULL;
int width = 0, height = 0, level = 0, orientation = 0;
int firstgid = 0, ntiles = 0;

FILE *fp = fopen(path, "r");

@@ -27,63 +32,51 @@ Map::Map(char const *path) :

if (data)
{
if (--data == 0)
/* We are in the process of reading layer data. Only stop
* when we have read the expected number of tiles. */
char const *parser = tmp;
while (ntiles < width * height)
{
layers[nlayers] = new Layer(width, height, level, tmp);
data[ntiles++] = atoi(parser);
while (isdigit(*parser))
parser++;
if (*parser == ',')
parser++;
if (!isdigit(*parser))
break;
}

if (ntiles == width * height)
{
layers[nlayers] = new Layer(width, height, level, data);
nlayers++;
data = NULL;
}
}
else if (sscanf(tmp, " <tileset firstgid=\"%i\"", &i) == 1)
{
/* This is a tileset description. Remember its firstgid value. */
firstgid = i;
fprintf(stderr, "found tileset, firstgid %i\n", firstgid);
}
else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1)
{
fprintf(stderr, "image %s\n", str);
/* This is a tileset image file. Associate it with firstgid. */
}
else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" width=\"%i\" height=\"%i\"",
&a, &i, &b, &j, &k) == 5)
else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" "
"width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5)
{
fprintf(stderr, "%s layer, level %i, sublevel %c, %ix%i\n",
a == 'H' ? "horizontal" : "vertical", i, b, j, k);
layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1));
/* This is a layer description. Prepare to read the data. */
layers = (Layer **)realloc(layers,
sizeof(Layer **) * (nlayers + 1));
orientation = toupper(a) == 'V' ? 1 : 0;
width = j;
height = k;
data = 2;
}
else
{
fprintf(stderr, ".");
ntiles = 0;
data = (uint32_t *)malloc(width * height * sizeof(uint32_t));
}
}

fclose(fp);

/*
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))
{
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(name, width, height, fp);
nlayers++;
}

pclose(fp);
*/
}

Map::~Map()
@@ -93,7 +86,7 @@ Map::~Map()
free(layers);
}

void Map::Draw(Tiler *tiler)
void Map::Draw(Tileset *tileset)
{
for (int i = 0; i < nlayers; i++)
{
@@ -101,7 +94,7 @@ void Map::Draw(Tiler *tiler)

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, z);
tileset->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z);
}
}


+ 2
- 2
src/map.h Vedi File

@@ -9,7 +9,7 @@
#include <cstdio>

#include "layer.h"
#include "tiler.h"
#include "tileset.h"

class Map
{
@@ -17,7 +17,7 @@ public:
Map(char const *path);
~Map();

void Draw(Tiler *tiler);
void Draw(Tileset *tileset);

private:
Layer **layers;


+ 6
- 6
src/test-map.cpp Vedi File

@@ -6,20 +6,20 @@
#include <math.h>

#include "video.h"
#include "tiler.h"
#include "tileset.h"
#include "map.h"

int main(int argc, char **argv)
{
Video *video = new Video("Deus Hax", 640, 480);
Tiler *tiler = new Tiler();
Tileset *tileset = new Tileset();
Map *map = new Map("maps/testmap-grass.tmx");

for (int done = 0; !done; )
{
video->Clear();

map->Draw(tiler);
map->Draw(tileset);

/* Test stuff */
int playerx, playery;
@@ -27,9 +27,9 @@ int main(int argc, char **argv)
playerx = playerx * (640 - 32) / 640;
playery = playery * (480 - 32) / 480;

tiler->AddTile(50, playerx, playery, 1);
tileset->AddTile(50, playerx, playery, 1);

tiler->Render();
tileset->Render();
video->Refresh(33.33333f);

/* This could go in a separate function */
@@ -49,7 +49,7 @@ int main(int argc, char **argv)
}

delete map;
delete tiler;
delete tileset;
delete video;

return EXIT_SUCCESS;


src/tiler.cpp → src/tileset.cpp Vedi File

@@ -16,15 +16,15 @@

#include <malloc.h>

#include "tiler.h"
#include "tileset.h"

/*
* Tiler implementation class
* Tileset implementation class
*/

class TilerData
class TilesetData
{
friend class Tiler;
friend class Tileset;

private:
static int Compare(void const *p1, void const *p2)
@@ -43,12 +43,12 @@ private:
};

/*
* Public Tiler class
* Public Tileset class
*/

Tiler::Tiler()
Tileset::Tileset()
{
data = new TilerData();
data = new TilesetData();
data->tiles = NULL;
data->ntiles = 0;

@@ -74,13 +74,13 @@ Tiler::Tiler()
glGenBuffers(3, data->buflist);
}

Tiler::~Tiler()
Tileset::~Tileset()
{
free(data->tiles);
delete data;
}

void Tiler::AddTile(int n, int x, int y, int z)
void Tileset::AddTile(int n, int x, int y, int z)
{
if ((data->ntiles % 1024) == 0)
{
@@ -96,10 +96,10 @@ void Tiler::AddTile(int n, int x, int y, int z)
data->ntiles++;
}

void Tiler::Render()
void Tileset::Render()
{
/* Sort tiles */
qsort(data->tiles, data->ntiles, 4 * sizeof(int), TilerData::Compare);
qsort(data->tiles, data->ntiles, 4 * sizeof(int), TilesetData::Compare);

/* Texture coord buffer */
float uvs[8 * data->ntiles];

src/tiler.h → src/tileset.h Vedi File

@@ -6,20 +6,20 @@
#if !defined __DH_TILER_H__
#define __DH_TILER_H__

class TilerData;
class TilesetData;

class Tiler
class Tileset
{
public:
Tiler();
~Tiler();
Tileset();
~Tileset();

void AddTile(int n, int x, int y, int z);

void Render();

private:
TilerData *data;
TilesetData *data;
};

#endif // __DH_TILER_H__

+ 1
- 1
src/video.cpp Vedi File

@@ -54,7 +54,7 @@ Video::Video(char const *title, int width, int height)

SDL_WM_SetCaption(title, NULL);
SDL_ShowCursor(0);
SDL_WM_GrabInput(SDL_GRAB_ON);
//SDL_WM_GrabInput(SDL_GRAB_ON);

/* Initialise OpenGL */
glViewport(0, 0, data->video->w, data->video->h);


Caricamento…
Annulla
Salva