Changeset 38


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

Dynamically allocate layers when loading maps.

Location:
trunk/src
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile

    r22 r38  
    11
    2 SRC = test-map.cpp video.cpp tiler.cpp
     2SRC = test-map.cpp video.cpp tiler.cpp layer.cpp map.cpp
    33
    44all: test-map
    55
    66test-map: $(SRC:%.cpp=%.o)
    7         g++ -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image`
     7        g++ -g -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image`
    88
    99%.o: %.cpp
    10         g++ -Wall -O3 -c $^ -o $@ `pkg-config --cflags sdl gl SDL_image`
     10        g++ -g -Wall -O3 -c $^ -o $@ `pkg-config --cflags sdl gl SDL_image`
    1111
    1212clean:
  • 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;
  • trunk/src/tiler.h

    r22 r38  
    44 * The tile manager
    55 */
     6
     7#if !defined __TILER_H__
     8#define __TILER_H__
    69
    710class TilerData;
     
    2124};
    2225
     26#endif // __TILER_H__
     27
Note: See TracChangeset for help on using the changeset viewer.