source: trunk/src/scene.cpp @ 222

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

Add support for rotated views and stretched tiles.

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