Ignore:
Timestamp:
Jul 4, 2010, 12:27:40 AM (11 years ago)
Author:
sam
Message:

Dynamically allocate layers when loading maps.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/test-map.cpp

    r33 r38  
    88#include "video.h"
    99#include "tiler.h"
    10 
    11 /* Global objects */
    12 Video *video;
    13 Tiler *tiler;
    14 
    15 /* Storage for map layers */
    16 int *layers[128];
    17 int width = 32, height = 32;
    18 int nlayers = 0;
    19 
    20 void LoadMap(void)
    21 {
    22     FILE *fp = popen("grep '^   [^< ]' maps/testmap.tmx | while read i; do echo -n \"$i\" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | gunzip; done", "r");
    23     while (fp && !feof(fp))
    24     {
    25         layers[nlayers] = (int *)malloc(width * height * sizeof(int));
    26         fread(layers[nlayers], sizeof(int), width * height, fp);
    27         if (feof(fp))
    28         {
    29             free(layers[nlayers]);
    30             layers[nlayers] = 0;
    31             fclose(fp);
    32             break;
    33         }
    34         for (int n = 0; n < width * height; n++)
    35         {
    36             unsigned int i = layers[nlayers][n];
    37             //i = (i >> 24) | ((i >> 8) & 0xff00) | ((i << 8) & 0xff0000) | (i << 24);
    38             layers[nlayers][n] = i ? i - 1 : 0;
    39         }
    40         nlayers++;
    41     }
    42 }
    43 
    44 /* The main drawing function. */
    45 void DrawScene()
    46 {
    47     video->Clear();
    48 
    49     for (int i = 0; i < nlayers; i++)
    50         for (int y = 0; y < height; y++)
    51             for (int x = 0; x < width; x++)
    52                 tiler->AddTile(layers[i][y * width + x], x * 32, y * 32, i);
    53 
    54     /* Test stuff */
    55     int playerx, playery;
    56     SDL_GetMouseState(&playerx, &playery);
    57     tiler->AddTile(50, playerx, playery, 1);
    58     tiler->AddTile(50, playerx + 64, playery + 32, 3);
    59 
    60     tiler->Render();
    61     video->Refresh(33.33333f);
    62 }
     10#include "map.h"
    6311
    6412int main(int argc, char **argv)
    6513{
    66     video = new Video("Deus Hax", 640, 480);
    67     tiler = new Tiler();
     14    Video *video = new Video("Deus Hax", 640, 480);
     15    Tiler *tiler = new Tiler();
     16    Map *map = new Map("maps/testmap-coll.tmx");
    6817
    69     int done;
     18    for (int done = 0; !done; )
     19    {
     20        video->Clear();
    7021
    71     /* Loop, drawing and checking events */
    72     LoadMap();
     22        map->Draw(tiler);
    7323
    74     done = 0;
    75     while (!done)
    76     {
    77         DrawScene();
     24        /* Test stuff */
     25        int playerx, playery;
     26        SDL_GetMouseState(&playerx, &playery);
     27
     28        tiler->AddTile(50, playerx, playery, 1);
     29        tiler->AddTile(50, playerx + 64, playery + 32, 3);
     30
     31        tiler->Render();
     32        video->Refresh(33.33333f);
    7833
    7934        /* This could go in a separate function */
     
    9348    }
    9449
     50    delete map;
    9551    delete tiler;
    9652    delete video;
Note: See TracChangeset for help on using the changeset viewer.