Changeset 20 for trunk


Ignore:
Timestamp:
Jun 29, 2010, 8:53:30 AM (13 years ago)
Author:
sam
Message:

Add mediocre support for .tmx tiled maps.

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile

    r18 r20  
    1111
    1212clean:
    13         rm -f test-map
     13        rm -f *.o test-map
    1414
  • trunk/src/test-map.cpp

    r18 r20  
    1616#include <SDL_image.h>
    1717
     18#include <stdio.h>
    1819#include <math.h>
    1920
     
    2526/* Storage for 3 vertex buffers */
    2627GLuint buflist[3];
     28
     29/* Storage for map layers */
     30int *layers[128];
     31int width = 32, height = 32;
     32int nlayers = 0;
    2733
    2834// Load Bitmaps And Convert To Textures
     
    5258}
    5359
    54 void PutMap(int const *themap)
     60void LoadMap(void)
     61{
     62    FILE *fp = popen("grep '^   ' maps/testmap.tmx | while read i; do echo -n \"$i\" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | gunzip; done", "r");
     63    while (fp && !feof(fp))
     64    {
     65        layers[nlayers] = (int *)malloc(width * height * sizeof(int));
     66        fread(layers[nlayers], sizeof(int), width * height, fp);
     67        if (feof(fp))
     68        {
     69            free(layers[nlayers]);
     70            layers[nlayers] = 0;
     71            fclose(fp);
     72            break;
     73        }
     74        for (int n = 0; n < width * height; n++)
     75        {
     76            unsigned int i = layers[nlayers][n];
     77            //i = (i >> 24) | ((i >> 8) & 0xff00) | ((i << 8) & 0xff0000) | (i << 24);
     78            layers[nlayers][n] = i ? i - 1 : 0;
     79        }
     80        nlayers++;
     81    }
     82}
     83
     84void PutMap(int const *themap, int width, int height)
    5585{
    5686    // Put map
    57     float uvs[8 * 20 * 15];
    58 
    59     for (int y = 0; y < 15; y++)
    60         for (int x = 0; x < 20; x++)
    61         {
    62             int tile = themap[20 * y + x];
     87    float uvs[8 * width * height];
     88
     89    for (int y = 0; y < height; y++)
     90        for (int x = 0; x < width; x++)
     91        {
     92            int tile = themap[width * y + x];
    6393            float ty = .0625f * (tile / 16);
    6494            float tx = .0625f * (tile % 16);
    65             uvs[8 * (20 * y + x) + 0] = tx;
    66             uvs[8 * (20 * y + x) + 1] = ty;
    67             uvs[8 * (20 * y + x) + 2] = tx + .0625f;
    68             uvs[8 * (20 * y + x) + 3] = ty;
    69             uvs[8 * (20 * y + x) + 4] = tx + .0625f;
    70             uvs[8 * (20 * y + x) + 5] = ty + .0625f;
    71             uvs[8 * (20 * y + x) + 6] = tx;
    72             uvs[8 * (20 * y + x) + 7] = ty + .0625f;
     95            uvs[8 * (width * y + x) + 0] = tx;
     96            uvs[8 * (width * y + x) + 1] = ty;
     97            uvs[8 * (width * y + x) + 2] = tx + .0625f;
     98            uvs[8 * (width * y + x) + 3] = ty;
     99            uvs[8 * (width * y + x) + 4] = tx + .0625f;
     100            uvs[8 * (width * y + x) + 5] = ty + .0625f;
     101            uvs[8 * (width * y + x) + 6] = tx;
     102            uvs[8 * (width * y + x) + 7] = ty + .0625f;
    73103        }
    74104    glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
    75105    glBufferData(GL_ARRAY_BUFFER,
    76                  8 * 20 * 15 * sizeof(float), uvs, GL_STATIC_DRAW);
    77 
    78     float vertices[8 * 20 * 15];
    79     for (int y = 0; y < 15; y++)
    80         for (int x = 0; x < 20; x++)
    81         {
    82             vertices[8 * (20 * y + x) + 0] = x * 32;
    83             vertices[8 * (20 * y + x) + 1] = y * 32;
    84             vertices[8 * (20 * y + x) + 2] = x * 32 + 32;
    85             vertices[8 * (20 * y + x) + 3] = y * 32;
    86             vertices[8 * (20 * y + x) + 4] = x * 32 + 32;
    87             vertices[8 * (20 * y + x) + 5] = y * 32 + 32;
    88             vertices[8 * (20 * y + x) + 6] = x * 32;
    89             vertices[8 * (20 * y + x) + 7] = y * 32 + 32;
     106                 8 * width * height * sizeof(float), uvs, GL_STATIC_DRAW);
     107
     108    float vertices[8 * width * height];
     109    for (int y = 0; y < height; y++)
     110        for (int x = 0; x < width; x++)
     111        {
     112            vertices[8 * (width * y + x) + 0] = x * 32;
     113            vertices[8 * (width * y + x) + 1] = y * 32;
     114            vertices[8 * (width * y + x) + 2] = x * 32 + 32;
     115            vertices[8 * (width * y + x) + 3] = y * 32;
     116            vertices[8 * (width * y + x) + 4] = x * 32 + 32;
     117            vertices[8 * (width * y + x) + 5] = y * 32 + 32;
     118            vertices[8 * (width * y + x) + 6] = x * 32;
     119            vertices[8 * (width * y + x) + 7] = y * 32 + 32;
    90120        }
    91121    glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
    92122    glBufferData(GL_ARRAY_BUFFER,
    93                  8 * 20 * 15 * sizeof(float), vertices, GL_STATIC_DRAW);
    94 
    95     int indices[4 * 20 * 15];
    96     for (int n = 0; n < 4 * 20 * 15; n++)
     123                 8 * width * height * sizeof(float), vertices, GL_STATIC_DRAW);
     124
     125    int indices[4 * width * height];
     126    for (int n = 0; n < 4 * width * height; n++)
    97127        indices[n] = n;
    98128    glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
    99129    glBufferData(GL_ARRAY_BUFFER,
    100                  4 * 20 * 15 * sizeof(int), indices, GL_STATIC_DRAW);
     130                 4 * width * height * sizeof(int), indices, GL_STATIC_DRAW);
    101131
    102132    glEnableClientState(GL_VERTEX_ARRAY);
     
    113143    glIndexPointer(GL_INT, 0, NULL);
    114144
    115     glDrawArrays(GL_QUADS, 0, 4 * 20 * 15);
     145    glDrawArrays(GL_QUADS, 0, 4 * width * height);
    116146
    117147    glDisableClientState(GL_VERTEX_ARRAY);
     
    123153void DrawScene()
    124154{
     155/*
    125156    int ground[20 * 15] =
    126157    {
     
    152183 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    153184 0,  0,  0,  0, 49, 49, 49, 49, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    154  0,  0,  0,  0,  0, 49, 49, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    155  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 48,  0, 32, 49,  0,  0,  0,
    156  0,  0,  0, 49, 49, 32,  0, 50,  0,  0,  0, 48,  0, 64,  0, 49, 49,  0,  0,  0,
    157  0,  0,  0,  0,  0,  0,  0,  0,  0, 32,  0, 64,  0,  0,  0,  0,  0,  0,  0,  0,
    158  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 32,  0,  0,  0,  0,  0,
    159  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     185 0,  0,  0,  0,  0, 49, 49, 49, 49,  0,  0,  0,  0, 25,  9,  9,  9,  8,  0,  0,
     186 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 54, 40, 39, 39, 41,  0,  0,
     187 0,  0,  0, 49, 49, 32,  0, 50,  0,  0,  0, 48,  0, 24, 23, 54, 40, 55,  0,  0,
     188 0,  0,  0,  0,  0,  0,  0,  0,  0, 32,  0, 64,  0, 24, 23, 24, 23,  7,  0,  0,
     189 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6, 38, 24, 23,  7,  0,  0,
     190 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6, 38, 22,  0,  0,
    160191 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    161192    };
     193*/
    162194
    163195    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    164196    glLoadIdentity();
    165197
    166     PutMap(ground);
    167     //glTranslatef(10.0f * sinf(0.16f * frames), 10.0f * cosf(0.16f * frames), 0.0f);
    168     PutMap(l1objects);
     198    for (int i = 0; i < nlayers; i++)
     199        PutMap(layers[i], width, height);
    169200}
    170201
     
    178209    LoadGLTextures();
    179210    MakeVBOs();
     211    LoadMap();
    180212
    181213    done = 0;
Note: See TracChangeset for help on using the changeset viewer.