source: trunk/src/scene.cpp @ 221

Last change on this file since 221 was 221, checked in by sam, 9 years ago

Complete Lol Engine / Deus Hax / Monsterz split.

  • Property svn:keywords set to Id
File size: 2.6 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};
57
58/*
59 * Public Scene class
60 */
61
62Scene::Scene()
63{
64    data = new SceneData();
65    data->tiles = 0;
66    data->ntiles = 0;
67}
68
69Scene::~Scene()
70{
71    delete data;
72}
73
74void Scene::AddTile(uint32_t code, int x, int y, int z, int o)
75{
76    if ((data->ntiles % 1024) == 0)
77        data->tiles = (Tile *)realloc(data->tiles,
78                                      (data->ntiles + 1024) * sizeof(Tile));
79    data->tiles[data->ntiles].prio = -y - 2 * 32 * z + (o ? 0 : 32);
80    data->tiles[data->ntiles].code = code;
81    data->tiles[data->ntiles].x = x;
82    data->tiles[data->ntiles].y = y;
83    data->tiles[data->ntiles].z = z;
84    data->tiles[data->ntiles].o = o;
85    data->ntiles++;
86}
87
88void Scene::Render() // XXX: rename to Blit()
89{
90#if 0
91    // Randomise, then sort.
92    for (int i = 0; i < data->ntiles; i++)
93    {
94        Tile tmp = data->tiles[i];
95        int j = rand() % data->ntiles;
96        data->tiles[i] = data->tiles[j];
97        data->tiles[j] = tmp;
98    }
99#endif
100    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
101
102    // XXX: debug stuff
103    glPushMatrix();
104    static float f = 0.0f;
105    f += 0.05f;
106    glTranslatef(320.0f, 240.0f, 0.0f);
107    glRotatef(-45.0f, 1.0f, 0.0f, 0.0f);
108#if 0
109    glRotatef(3.0f * sinf(f), 1.0f, 0.0f, 0.0f);
110    glRotatef(8.0f * cosf(f), 0.0f, 0.0f, 1.0f);
111#endif
112    glTranslatef(-320.0f, -240.0f, 0.0f);
113
114    for (int i = 0; i < data->ntiles; i++)
115        Tiler::BlitTile(data->tiles[i].code, data->tiles[i].x,
116                        data->tiles[i].y, data->tiles[i].z, data->tiles[i].o);
117
118    glPopMatrix();
119    // XXX: end of debug stuff
120
121    free(data->tiles);
122    data->tiles = 0;
123    data->ntiles = 0;
124}
125
Note: See TracBrowser for help on using the repository browser.