source: trunk/src/tiler.cpp @ 33

Last change on this file since 33 was 33, checked in by sam, 13 years ago

Test collisions.

File size: 4.3 KB
Line 
1
2#ifdef WIN32
3#   define WIN32_LEAN_AND_MEAN
4#   include <windows.h>
5#endif
6#if defined __APPLE__ && defined __MACH__
7#   include <OpenGL/gl.h>
8#else
9#   define GL_GLEXT_PROTOTYPES
10#   include <GL/gl.h>
11#   include <GL/glext.h>
12#endif
13
14#include <SDL.h>
15#include <SDL_image.h>
16
17#include <malloc.h>
18
19#include "tiler.h"
20
21/*
22 * Tiler implementation class
23 */
24
25class TilerData
26{
27    friend class Tiler;
28
29private:
30    static int Compare(void const *p1, void const *p2)
31    {
32        int const *n1 = (int const *)p1;
33        int const *n2 = (int const *)p2;
34
35        return n1[2] + 32 * n1[3] - (n2[2] + 32 * n2[3]);
36    }
37
38    int *tiles;
39    int ntiles;
40
41    GLuint texture[1];
42    GLuint buflist[3];
43};
44
45/*
46 * Public Tiler class
47 */
48
49Tiler::Tiler()
50{
51    data = new TilerData();
52    data->tiles = NULL;
53    data->ntiles = 0;
54
55    /* One tile texture */
56    SDL_Surface *img = IMG_Load("art/test/groundtest.png");
57
58    if (!img)
59    {
60        SDL_Quit();
61        exit(1);
62    }
63
64    glGenTextures(1, &data->texture[0]);
65    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
66
67    glTexImage2D(GL_TEXTURE_2D, 0, 4, img->w, img->h, 0,
68                 GL_RGBA, GL_UNSIGNED_BYTE, img->pixels);
69
70    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
71    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
72
73    /* Three GPU buffers */
74    glGenBuffers(3, data->buflist);
75}
76
77Tiler::~Tiler()
78{
79    free(data->tiles);
80    delete data;
81}
82
83void Tiler::AddTile(int n, int x, int y, int z)
84{
85    if ((data->ntiles % 1024) == 0)
86    {
87        data->tiles = (int *)realloc(data->tiles,
88                                     (data->ntiles + 1024) * 4 * sizeof(int));
89    }
90
91    data->tiles[4 * data->ntiles] = n;
92    data->tiles[4 * data->ntiles + 1] = x;
93    data->tiles[4 * data->ntiles + 2] = y;
94    data->tiles[4 * data->ntiles + 3] = z;
95
96    data->ntiles++;
97}
98
99void Tiler::Render()
100{
101    /* Sort tiles */
102    qsort(data->tiles, data->ntiles, 4 * sizeof(int), TilerData::Compare);
103
104    /* Texture coord buffer */
105    float uvs[8 * data->ntiles];
106    for (int n = 0; n < data->ntiles; n++)
107    {
108        int tile = data->tiles[4 * n];
109        float ty = .03125f * (tile / 16);
110        float tx = .0625f * (tile % 16);
111        uvs[8 * n + 0] = tx;
112        uvs[8 * n + 1] = ty;
113        uvs[8 * n + 2] = tx + .0625f;
114        uvs[8 * n + 3] = ty;
115        uvs[8 * n + 4] = tx + .0625f;
116        uvs[8 * n + 5] = ty + .03125f;
117        uvs[8 * n + 6] = tx;
118        uvs[8 * n + 7] = ty + .03125f;
119    }
120    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
121    glBufferData(GL_ARRAY_BUFFER,
122                 8 * data->ntiles * sizeof(float), uvs, GL_STATIC_DRAW);
123
124    /* Vertex buffer */
125    float vertices[8 * data->ntiles];
126    for (int n = 0; n < data->ntiles; n++)
127    {
128        int x = data->tiles[4 * n + 1];
129        int y = data->tiles[4 * n + 2];
130        vertices[8 * n + 0] = x;
131        vertices[8 * n + 1] = y;
132        vertices[8 * n + 2] = x + 32;
133        vertices[8 * n + 3] = y;
134        vertices[8 * n + 4] = x + 32;
135        vertices[8 * n + 5] = y + 32;
136        vertices[8 * n + 6] = x;
137        vertices[8 * n + 7] = y + 32;
138    }
139    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
140    glBufferData(GL_ARRAY_BUFFER, 8 * data->ntiles * sizeof(float),
141                 vertices, GL_STATIC_DRAW);
142
143    /* Index buffer */
144    int indices[4 * data->ntiles];
145    for (int n = 0; n < 4 * data->ntiles; n++)
146        indices[n] = n;
147    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
148    glBufferData(GL_ARRAY_BUFFER, 4 * data->ntiles * sizeof(int),
149                 indices, GL_STATIC_DRAW);
150
151    /* Draw everything */
152    glEnableClientState(GL_VERTEX_ARRAY);
153    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
154    glEnableClientState(GL_INDEX_ARRAY);
155
156    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
157
158    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
159    glVertexPointer(2, GL_FLOAT, 0, NULL);
160    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
161    glTexCoordPointer(2, GL_FLOAT, 0, NULL);
162    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
163    glIndexPointer(GL_INT, 0, NULL);
164
165    glDrawArrays(GL_QUADS, 0, 4 * data->ntiles);
166
167    glDisableClientState(GL_VERTEX_ARRAY);
168    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
169    glDisableClientState(GL_INDEX_ARRAY);
170
171    /* Empty our shit */
172    free(data->tiles);
173    data->tiles = NULL;
174    data->ntiles = 0;
175}
176
Note: See TracBrowser for help on using the repository browser.