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

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

Fix test map layers.

  • Property svn:keywords set to Id
File size: 2.3 KB
RevLine 
[13]1// Test stuff
2
3#include <SDL.h>
4
[20]5#include <stdio.h>
[14]6#include <math.h>
[13]7
[18]8#include "video.h"
[22]9#include "tiler.h"
[14]10
[22]11/* Global objects */
12Video *video;
13Tiler *tiler;
[13]14
[20]15/* Storage for map layers */
16int *layers[128];
17int width = 32, height = 32;
18int nlayers = 0;
19
20void LoadMap(void)
[13]21{
[20]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
[14]44/* The main drawing function. */
45void DrawScene()
46{
[22]47    video->Clear();
[13]48
[22]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);
[13]53
[22]54    /* Test stuff */
55    int playerx, playery;
56    SDL_GetMouseState(&playerx, &playery);
[24]57    tiler->AddTile(50, playerx, playery, 1);
[25]58    tiler->AddTile(50, playerx + 64, playery + 32, 3);
[13]59
[22]60    tiler->Render();
61    video->Refresh(33.33333f);
[13]62}
63
64int main(int argc, char **argv)
65{
[22]66    video = new Video("Deus Hax", 640, 480);
67    tiler = new Tiler();
[13]68
[18]69    int done;
[13]70
[18]71    /* Loop, drawing and checking events */
[20]72    LoadMap();
[13]73
[18]74    done = 0;
75    while (!done)
76    {
77        DrawScene();
[13]78
[18]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        }
[13]93    }
[14]94
[22]95    delete tiler;
[18]96    delete video;
[14]97
[18]98    return EXIT_SUCCESS;
[13]99}
100
Note: See TracBrowser for help on using the repository browser.