source: trunk/src/tileset.cpp @ 81

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

Minor Win32 compilation fixes.

  • Property svn:keywords set to Id
File size: 2.2 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#endif
12
13#include <SDL.h>
14#include <SDL_image.h>
15
16#include <malloc.h>
17
18#include "tileset.h"
19
20/*
21 * TileSet implementation class
22 */
23
24class TileSetData
25{
26    friend class TileSet;
27
28private:
29    static int Compare(void const *p1, void const *p2)
30    {
31        int const *n1 = (int const *)p1;
32        int const *n2 = (int const *)p2;
33
34        return n1[2] + 32 * n1[3] - (n2[2] + 32 * n2[3]);
35    }
36
37    char *name;
38    int ref;
39    int *tiles;
40    int ntiles;
41
42    GLuint texture[1];
43};
44
45/*
46 * Public TileSet class
47 */
48
49TileSet::TileSet(char const *path)
50{
51    SDL_Surface *img = NULL;
52
53    data = new TileSetData();
54    data->name = strdup(path);
55    data->ref = 0;
56    data->tiles = NULL;
57    data->ntiles = 0;
58
59    /* One tile texture */
60    for (char const *name = path; *name; name++)
61        if ((img = IMG_Load(name)))
62            break;
63
64    if (!img)
65    {
66        SDL_Quit();
67        exit(1);
68    }
69
70    glGenTextures(1, data->texture);
71    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
72
73    glTexImage2D(GL_TEXTURE_2D, 0, 4, img->w, img->h, 0,
74                 GL_RGBA, GL_UNSIGNED_BYTE, img->pixels);
75
76    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
77    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
78}
79
80TileSet::~TileSet()
81{
82    glDeleteTextures(1, data->texture);
83
84    free(data->tiles);
85    free(data->name);
86    delete data;
87}
88
89void TileSet::Ref()
90{
91    data->ref++;
92}
93
94int TileSet::Unref()
95{
96    return --data->ref;
97}
98
99char const *TileSet::GetName()
100{
101    return data->name;
102}
103
104void TileSet::BlitTile(uint32_t id, int x, int y)
105{
106    float tx = .0625f * (id & 0xf);
107    float ty = .0625f * ((id >> 4) & 0xf);
108
109    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
110    glBegin(GL_QUADS);
111        glTexCoord2f(tx, ty);
112        glVertex2f(x, y);
113        glTexCoord2f(tx + .0625f, ty);
114        glVertex2f(x + 32, y);
115        glTexCoord2f(tx + .0625f, ty + .0625f);
116        glVertex2f(x + 32, y + 32);
117        glTexCoord2f(tx, ty + .0625f);
118        glVertex2f(x, y + 32);
119    glEnd();
120}
121
Note: See TracBrowser for help on using the repository browser.