source: trunk/src/tiler.cpp @ 22

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

Create a tile manager. There are no longer any GL calls in test-map.cpp or
in public headers. Good cleanup.

File size: 4.0 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    int *tiles;
31    int ntiles;
32
33    GLuint texture[1];
34    GLuint buflist[3];
35};
36
37/*
38 * Public Tiler class
39 */
40
41Tiler::Tiler()
42{
43    data = new TilerData();
44    data->tiles = NULL;
45    data->ntiles = 0;
46
47    /* One tile texture */
48    SDL_Surface *img = IMG_Load("art/test/groundtest.png");
49
50    if (!img)
51    {
52        SDL_Quit();
53        exit(1);
54    }
55
56    glGenTextures(1, &data->texture[0]);
57    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
58
59    glTexImage2D(GL_TEXTURE_2D, 0, 4, img->w, img->h, 0,
60                 GL_RGBA, GL_UNSIGNED_BYTE, img->pixels);
61
62    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
63    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
64
65    /* Three GPU buffers */
66    glGenBuffers(3, data->buflist);
67}
68
69Tiler::~Tiler()
70{
71    free(data->tiles);
72    delete data;
73}
74
75void Tiler::AddTile(int n, int x, int y, int z)
76{
77    if ((data->ntiles % 1024) == 0)
78    {
79        data->tiles = (int *)realloc(data->tiles,
80                                     (data->ntiles + 1024) * 4 * sizeof(int));
81    }
82
83    data->tiles[4 * data->ntiles] = n;
84    data->tiles[4 * data->ntiles + 1] = x;
85    data->tiles[4 * data->ntiles + 2] = y;
86    data->tiles[4 * data->ntiles + 3] = z;
87
88    data->ntiles++;
89}
90
91void Tiler::Render()
92{
93    /* Texture coord buffer */
94    float uvs[8 * data->ntiles];
95    for (int n = 0; n < data->ntiles; n++)
96    {
97        int tile = data->tiles[4 * n];
98        float ty = .0625f * (tile / 16);
99        float tx = .0625f * (tile % 16);
100        uvs[8 * n + 0] = tx;
101        uvs[8 * n + 1] = ty;
102        uvs[8 * n + 2] = tx + .0625f;
103        uvs[8 * n + 3] = ty;
104        uvs[8 * n + 4] = tx + .0625f;
105        uvs[8 * n + 5] = ty + .0625f;
106        uvs[8 * n + 6] = tx;
107        uvs[8 * n + 7] = ty + .0625f;
108    }
109    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
110    glBufferData(GL_ARRAY_BUFFER,
111                 8 * data->ntiles * sizeof(float), uvs, GL_STATIC_DRAW);
112
113    /* Vertex buffer */
114    float vertices[8 * data->ntiles];
115    for (int n = 0; n < data->ntiles; n++)
116    {
117        int x = data->tiles[4 * n + 1];
118        int y = data->tiles[4 * n + 2];
119        vertices[8 * n + 0] = x;
120        vertices[8 * n + 1] = y;
121        vertices[8 * n + 2] = x + 32;
122        vertices[8 * n + 3] = y;
123        vertices[8 * n + 4] = x + 32;
124        vertices[8 * n + 5] = y + 32;
125        vertices[8 * n + 6] = x;
126        vertices[8 * n + 7] = y + 32;
127    }
128    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
129    glBufferData(GL_ARRAY_BUFFER, 8 * data->ntiles * sizeof(float),
130                 vertices, GL_STATIC_DRAW);
131
132    /* Index buffer */
133    int indices[4 * data->ntiles];
134    for (int n = 0; n < 4 * data->ntiles; n++)
135        indices[n] = n;
136    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
137    glBufferData(GL_ARRAY_BUFFER, 4 * data->ntiles * sizeof(int),
138                 indices, GL_STATIC_DRAW);
139
140    /* Draw everything */
141    glEnableClientState(GL_VERTEX_ARRAY);
142    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
143    glEnableClientState(GL_INDEX_ARRAY);
144
145    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
146
147    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
148    glVertexPointer(2, GL_FLOAT, 0, NULL);
149    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
150    glTexCoordPointer(2, GL_FLOAT, 0, NULL);
151    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
152    glIndexPointer(GL_INT, 0, NULL);
153
154    glDrawArrays(GL_QUADS, 0, 4 * data->ntiles);
155
156    glDisableClientState(GL_VERTEX_ARRAY);
157    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
158    glDisableClientState(GL_INDEX_ARRAY);
159
160    /* Empty our shit */
161    free(data->tiles);
162    data->tiles = NULL;
163    data->ntiles = 0;
164}
165
Note: See TracBrowser for help on using the repository browser.