source: trunk/src/test-map.cpp @ 25

Last change on this file since 25 was 25, checked in by sam, 11 years ago

Fix test map layers.

  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1// Test stuff
2
3#include <SDL.h>
4
5#include <stdio.h>
6#include <math.h>
7
8#include "video.h"
9#include "tiler.h"
10
11/* Global objects */
12Video *video;
13Tiler *tiler;
14
15/* Storage for map layers */
16int *layers[128];
17int width = 32, height = 32;
18int nlayers = 0;
19
20void 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. */
45void 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}
63
64int main(int argc, char **argv)
65{
66    video = new Video("Deus Hax", 640, 480);
67    tiler = new Tiler();
68
69    int done;
70
71    /* Loop, drawing and checking events */
72    LoadMap();
73
74    done = 0;
75    while (!done)
76    {
77        DrawScene();
78
79        /* This could go in a separate function */
80        SDL_Event event;
81        while (SDL_PollEvent(&event))
82        {
83            if (event.type == SDL_QUIT)
84                done = 1;
85            if (event.type == SDL_KEYDOWN)
86            {
87                if (event.key.keysym.sym == SDLK_RETURN)
88                    video->FullScreen();
89                else if (event.key.keysym.sym == SDLK_ESCAPE)
90                    done = 1;
91            }
92        }
93    }
94
95    delete tiler;
96    delete video;
97
98    return EXIT_SUCCESS;
99}
100
Note: See TracBrowser for help on using the repository browser.