Changeset 74


Ignore:
Timestamp:
Jul 6, 2010, 11:37:47 PM (10 years ago)
Author:
sam
Message:

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

Location:
trunk/src
Files:
7 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile

    r38 r74  
    11
    2 SRC = test-map.cpp video.cpp tiler.cpp layer.cpp map.cpp
     2SRC = test-map.cpp video.cpp tileset.cpp layer.cpp map.cpp
    33
    44all: test-map
  • trunk/src/layer.cpp

    r70 r74  
    22#include "layer.h"
    33
    4 Layer::Layer(int w, int h, int z, char const *base64)
     4Layer::Layer(int w, int h, int in_z, uint32_t *in_data)
    55{
    66    width = w;
    77    height = h;
    8     level = z;
    9     data = new unsigned int[w * h];
     8    z = in_z;
     9    data = in_data;
    1010
    1111#if 0
     
    2727int Layer::GetZ()
    2828{
    29     return level;
     29    return z;
    3030}
    3131
  • trunk/src/layer.h

    r70 r74  
    88
    99#include <cstdio>
     10#include <stdint.h>
    1011
    1112class Layer
    1213{
    1314public:
    14     Layer(int w, int h, int z, char const *base64);
     15    Layer(int w, int h, int z, uint32_t *data);
    1516    ~Layer();
    1617
     
    1920
    2021private:
    21     int width, height, level;
    22     unsigned int *data;
     22    int width, height, z;
     23    uint32_t *data;
    2324};
    2425
  • trunk/src/map.cpp

    r70 r74  
    11
    22#include <cstdio>
     3#include <cstring>
     4#include <cstdlib>
    35#include <malloc.h>
     6#include <ctype.h>
    47
    58#include "map.h"
     
    1114{
    1215    char tmp[BUFSIZ];
    13     int firstgid = 0, width = 0, height = 0, level = 0, data = 0;
     16    uint32_t *data = NULL;
     17    int width = 0, height = 0, level = 0, orientation = 0;
     18    int firstgid = 0, ntiles = 0;
    1419
    1520    FILE *fp = fopen(path, "r");
     
    2833        if (data)
    2934        {
    30             if (--data == 0)
     35            /* We are in the process of reading layer data. Only stop
     36             * when we have read the expected number of tiles. */
     37            char const *parser = tmp;
     38            while (ntiles < width * height)
    3139            {
    32                 layers[nlayers] = new Layer(width, height, level, tmp);
     40                data[ntiles++] = atoi(parser);
     41                while (isdigit(*parser))
     42                    parser++;
     43                if (*parser == ',')
     44                    parser++;
     45                if (!isdigit(*parser))
     46                    break;
     47            }
     48
     49            if (ntiles == width * height)
     50            {
     51                layers[nlayers] = new Layer(width, height, level, data);
    3352                nlayers++;
     53                data = NULL;
    3454            }
    3555        }
    3656        else if (sscanf(tmp, " <tileset firstgid=\"%i\"", &i) == 1)
    3757        {
     58            /* This is a tileset description. Remember its firstgid value. */
    3859            firstgid = i;
    39             fprintf(stderr, "found tileset, firstgid %i\n", firstgid);
    4060        }
    4161        else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1)
    4262        {
    43             fprintf(stderr, "image %s\n", str);
     63            /* This is a tileset image file. Associate it with firstgid. */
    4464        }
    45         else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" width=\"%i\" height=\"%i\"",
    46                         &a, &i, &b, &j, &k) == 5)
     65        else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" "
     66                        "width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5)
    4767        {
    48             fprintf(stderr, "%s layer, level %i, sublevel %c, %ix%i\n",
    49                     a == 'H' ? "horizontal" : "vertical", i, b, j, k);
    50             layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1));
     68            /* This is a layer description. Prepare to read the data. */
     69            layers = (Layer **)realloc(layers,
     70                                       sizeof(Layer **) * (nlayers + 1));
     71            orientation = toupper(a) == 'V' ? 1 : 0;
    5172            width = j;
    5273            height = k;
    53             data = 2;
    54         }
    55         else
    56         {
    57             fprintf(stderr, ".");
     74            ntiles = 0;
     75            data = (uint32_t *)malloc(width * height * sizeof(uint32_t));
    5876        }
    5977    }
    6078
    6179    fclose(fp);
    62 
    63 /*
    64     char tmp[1024];
    65 
    66     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);
    67 
    68     FILE *fp = popen(tmp, "r");
    69 
    70     while (fp && !feof(fp))
    71     {
    72         int width, height;
    73 
    74         fscanf(fp, "<layer name=\"%[^\"]\" ", name);
    75         if (feof(fp))
    76             break;
    77         fscanf(fp, "width=\"%u\" ", &width);
    78         fscanf(fp, "height=\"%u\" ", &height);
    79         fgets(tmp, 1024, fp); // Ignore rest of line
    80         layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1));
    81         layers[nlayers] = new Layer(name, width, height, fp);
    82         nlayers++;
    83     }
    84 
    85     pclose(fp);
    86 */
    8780}
    8881
     
    9487}
    9588
    96 void Map::Draw(Tiler *tiler)
     89void Map::Draw(Tileset *tileset)
    9790{
    9891    for (int i = 0; i < nlayers; i++)
     
    10295        for (int y = 0; y < 32; y++)
    10396            for (int x = 0; x < 32; x++)
    104                 tiler->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z);
     97                tileset->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z);
    10598    }
    10699}
  • trunk/src/map.h

    r47 r74  
    1010
    1111#include "layer.h"
    12 #include "tiler.h"
     12#include "tileset.h"
    1313
    1414class Map
     
    1818    ~Map();
    1919
    20     void Draw(Tiler *tiler);
     20    void Draw(Tileset *tileset);
    2121
    2222private:
  • trunk/src/test-map.cpp

    r47 r74  
    77
    88#include "video.h"
    9 #include "tiler.h"
     9#include "tileset.h"
    1010#include "map.h"
    1111
     
    1313{
    1414    Video *video = new Video("Deus Hax", 640, 480);
    15     Tiler *tiler = new Tiler();
     15    Tileset *tileset = new Tileset();
    1616    Map *map = new Map("maps/testmap-grass.tmx");
    1717
     
    2020        video->Clear();
    2121
    22         map->Draw(tiler);
     22        map->Draw(tileset);
    2323
    2424        /* Test stuff */
     
    2828        playery = playery * (480 - 32) / 480;
    2929
    30         tiler->AddTile(50, playerx, playery, 1);
     30        tileset->AddTile(50, playerx, playery, 1);
    3131
    32         tiler->Render();
     32        tileset->Render();
    3333        video->Refresh(33.33333f);
    3434
     
    5050
    5151    delete map;
    52     delete tiler;
     52    delete tileset;
    5353    delete video;
    5454
  • trunk/src/tileset.cpp

    • Property svn:keywords set to Id
    r73 r74  
    1717#include <malloc.h>
    1818
    19 #include "tiler.h"
     19#include "tileset.h"
    2020
    2121/*
    22  * Tiler implementation class
     22 * Tileset implementation class
    2323 */
    2424
    25 class TilerData
     25class TilesetData
    2626{
    27     friend class Tiler;
     27    friend class Tileset;
    2828
    2929private:
     
    4444
    4545/*
    46  * Public Tiler class
     46 * Public Tileset class
    4747 */
    4848
    49 Tiler::Tiler()
     49Tileset::Tileset()
    5050{
    51     data = new TilerData();
     51    data = new TilesetData();
    5252    data->tiles = NULL;
    5353    data->ntiles = 0;
     
    7575}
    7676
    77 Tiler::~Tiler()
     77Tileset::~Tileset()
    7878{
    7979    free(data->tiles);
     
    8181}
    8282
    83 void Tiler::AddTile(int n, int x, int y, int z)
     83void Tileset::AddTile(int n, int x, int y, int z)
    8484{
    8585    if ((data->ntiles % 1024) == 0)
     
    9797}
    9898
    99 void Tiler::Render()
     99void Tileset::Render()
    100100{
    101101    /* Sort tiles */
    102     qsort(data->tiles, data->ntiles, 4 * sizeof(int), TilerData::Compare);
     102    qsort(data->tiles, data->ntiles, 4 * sizeof(int), TilesetData::Compare);
    103103
    104104    /* Texture coord buffer */
  • trunk/src/tileset.h

    • Property svn:keywords set to Id
    r73 r74  
    77#define __DH_TILER_H__
    88
    9 class TilerData;
     9class TilesetData;
    1010
    11 class Tiler
     11class Tileset
    1212{
    1313public:
    14     Tiler();
    15     ~Tiler();
     14    Tileset();
     15    ~Tileset();
    1616
    1717    void AddTile(int n, int x, int y, int z);
     
    2020
    2121private:
    22     TilerData *data;
     22    TilesetData *data;
    2323};
    2424
  • trunk/src/video.cpp

    r22 r74  
    5555    SDL_WM_SetCaption(title, NULL);
    5656    SDL_ShowCursor(0);
    57     SDL_WM_GrabInput(SDL_GRAB_ON);
     57    //SDL_WM_GrabInput(SDL_GRAB_ON);
    5858
    5959    /* Initialise OpenGL */
Note: See TracChangeset for help on using the changeset viewer.