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

Last change on this file since 22 was 22, checked in by sam, 10 years ago

Create a tile manager. There are no longer any GL calls in test-map.cpp or
in public headers. Good cleanup.

  • Property svn:keywords set to Id
File size: 2.2 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, nlayers);
58
59    tiler->Render();
60    video->Refresh(33.33333f);
61}
62
63int main(int argc, char **argv)
64{
65    video = new Video("Deus Hax", 640, 480);
66    tiler = new Tiler();
67
68    int done;
69
70    /* Loop, drawing and checking events */
71    LoadMap();
72
73    done = 0;
74    while (!done)
75    {
76        DrawScene();
77
78        /* This could go in a separate function */
79        SDL_Event event;
80        while (SDL_PollEvent(&event))
81        {
82            if (event.type == SDL_QUIT)
83                done = 1;
84            if (event.type == SDL_KEYDOWN)
85            {
86                if (event.key.keysym.sym == SDLK_RETURN)
87                    video->FullScreen();
88                else if (event.key.keysym.sym == SDLK_ESCAPE)
89                    done = 1;
90            }
91        }
92    }
93
94    delete tiler;
95    delete video;
96
97    return EXIT_SUCCESS;
98}
99
Note: See TracBrowser for help on using the repository browser.