source: trunk/src/scene.cpp @ 150

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

Replace mouse scrolling with a joystick simulated by the e/s/d/f keys.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstdlib>
11#include <cmath>
12
13#ifdef WIN32
14#   define WIN32_LEAN_AND_MEAN
15#   include <windows.h>
16#endif
17#if defined __APPLE__ && defined __MACH__
18#   include <OpenGL/gl.h>
19#else
20#   define GL_GLEXT_PROTOTYPES
21#   include <GL/gl.h>
22#endif
23
24#include "core.h"
25
26struct Tile
27{
28    uint32_t prio, code;
29    int x, y, z, o;
30};
31
32/*
33 * Scene implementation class
34 */
35
36class SceneData
37{
38    friend class Scene;
39
40private:
41    static int Compare(void const *p1, void const *p2)
42    {
43        Tile const *t1 = (Tile const *)p1;
44        Tile const *t2 = (Tile const *)p2;
45
46        return t2->prio - t1->prio;
47    }
48
49    Tile *tiles;
50    int ntiles;
51};
52
53/*
54 * Public Scene class
55 */
56
57Scene::Scene()
58{
59    data = new SceneData();
60    data->tiles = 0;
61    data->ntiles = 0;
62}
63
64Scene::~Scene()
65{
66    delete data;
67}
68
69void Scene::AddTile(uint32_t code, int x, int y, int z, int o)
70{
71    if ((data->ntiles % 1024) == 0)
72        data->tiles = (Tile *)realloc(data->tiles,
73                                      (data->ntiles + 1024) * sizeof(Tile));
74    data->tiles[data->ntiles].prio = -y - 2 * 32 * z + (o ? 0 : 32);
75    data->tiles[data->ntiles].code = code;
76    data->tiles[data->ntiles].x = x;
77    data->tiles[data->ntiles].y = y;
78    data->tiles[data->ntiles].z = z;
79    data->tiles[data->ntiles].o = o;
80    data->ntiles++;
81}
82
83void Scene::Render() // XXX: rename to Blit()
84{
85#if 0
86    // Randomise, then sort.
87    for (int i = 0; i < data->ntiles; i++)
88    {
89        Tile tmp = data->tiles[i];
90        int j = rand() % data->ntiles;
91        data->tiles[i] = data->tiles[j];
92        data->tiles[j] = tmp;
93    }
94#endif
95    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
96
97    // XXX: debug stuff
98    glPushMatrix();
99    static float f = 0.0f;
100    f += 0.05f;
101    glTranslatef(320.0f, 240.0f, 0.0f);
102    glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
103#if 0
104    glRotatef(3.0f * sinf(f), 1.0f, 0.0f, 0.0f);
105    glRotatef(8.0f * cosf(f), 0.0f, 0.0f, 1.0f);
106#endif
107    glTranslatef(-320.0f, -240.0f, 0.0f);
108
109    for (int i = 0; i < data->ntiles; i++)
110        Tiler::BlitTile(data->tiles[i].code, data->tiles[i].x,
111                        data->tiles[i].y, data->tiles[i].z, data->tiles[i].o);
112
113    glPopMatrix();
114    // XXX: end of debug stuff
115
116    free(data->tiles);
117    data->tiles = 0;
118    data->ntiles = 0;
119}
120
Note: See TracBrowser for help on using the repository browser.