source: trunk/src/scene.cpp @ 313

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

Rework tile handling. This possibly breaks deushax pretty rudely.

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16#include <cmath>
17
18#ifdef WIN32
19#   define WIN32_LEAN_AND_MEAN
20#   include <windows.h>
21#endif
22#if defined __APPLE__ && defined __MACH__
23#   include <OpenGL/gl.h>
24#else
25#   define GL_GLEXT_PROTOTYPES
26#   include <GL/gl.h>
27#endif
28
29#include "core.h"
30
31struct Tile
32{
33    uint32_t prio, code;
34    int x, y, z, o;
35};
36
37/*
38 * Scene implementation class
39 */
40
41class SceneData
42{
43    friend class Scene;
44
45private:
46    static int Compare(void const *p1, void const *p2)
47    {
48        Tile const *t1 = (Tile const *)p1;
49        Tile const *t2 = (Tile const *)p2;
50
51        return t2->prio - t1->prio;
52    }
53
54    Tile *tiles;
55    int ntiles;
56    float angle;
57
58    static Scene *scene;
59};
60
61Scene *SceneData::scene = NULL;
62
63/*
64 * Public Scene class
65 */
66
67Scene::Scene(float angle)
68  : data(new SceneData())
69{
70    data->tiles = 0;
71    data->ntiles = 0;
72    data->angle = angle;
73}
74
75Scene::~Scene()
76{
77    delete data;
78}
79
80Scene *Scene::GetDefault()
81{
82    if (!SceneData::scene)
83        SceneData::scene = new Scene(0.0f);
84    return SceneData::scene;
85}
86
87void Scene::Reset()
88{
89    if (SceneData::scene)
90        delete SceneData::scene;
91    SceneData::scene = NULL;
92}
93
94void Scene::AddTile(uint32_t code, int x, int y, int z, int o)
95{
96    if ((data->ntiles % 1024) == 0)
97        data->tiles = (Tile *)realloc(data->tiles,
98                                      (data->ntiles + 1024) * sizeof(Tile));
99    /* FIXME: this sorting only works for a 45-degree camera */
100    data->tiles[data->ntiles].prio = -y - 2 * 32 * z + (o ? 0 : 32);
101    data->tiles[data->ntiles].code = code;
102    data->tiles[data->ntiles].x = x;
103    data->tiles[data->ntiles].y = y;
104    data->tiles[data->ntiles].z = z;
105    data->tiles[data->ntiles].o = o;
106    data->ntiles++;
107}
108
109void Scene::Render() // XXX: rename to Blit()
110{
111#if 0
112    // Randomise, then sort.
113    for (int i = 0; i < data->ntiles; i++)
114    {
115        Tile tmp = data->tiles[i];
116        int j = rand() % data->ntiles;
117        data->tiles[i] = data->tiles[j];
118        data->tiles[j] = tmp;
119    }
120#endif
121    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
122
123    // XXX: debug stuff
124    glPushMatrix();
125    static float f = 0.0f;
126    f += 0.05f;
127    glTranslatef(320.0f, 240.0f, 0.0f);
128    glRotatef(-data->angle, 1.0f, 0.0f, 0.0f);
129#if 0
130    glRotatef(3.0f * sinf(f), 1.0f, 0.0f, 0.0f);
131    glRotatef(8.0f * cosf(f), 0.0f, 0.0f, 1.0f);
132#endif
133    glTranslatef(-320.0f, -240.0f, 0.0f);
134
135    for (int i = 0; i < data->ntiles; i++)
136        Tiler::BlitTile(data->tiles[i].code, data->tiles[i].x,
137                        data->tiles[i].y, data->tiles[i].z, data->tiles[i].o);
138
139    glPopMatrix();
140    // XXX: end of debug stuff
141
142    free(data->tiles);
143    data->tiles = 0;
144    data->ntiles = 0;
145}
146
Note: See TracBrowser for help on using the repository browser.