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
RevLine 
[22]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
[74]18#include "tileset.h"
[22]19
20/*
[75]21 * TileSet implementation class
[22]22 */
23
[75]24class TileSetData
[22]25{
[75]26    friend class TileSet;
[22]27
28private:
[24]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
[75]37    char *name;
38    int ref;
[22]39    int *tiles;
40    int ntiles;
41
42    GLuint texture[1];
43};
44
45/*
[75]46 * Public TileSet class
[22]47 */
48
[75]49TileSet::TileSet(char const *path)
[22]50{
[75]51    SDL_Surface *img = NULL;
52
53    data = new TileSetData();
54    data->name = strdup(path);
55    data->ref = 0;
[22]56    data->tiles = NULL;
57    data->ntiles = 0;
58
59    /* One tile texture */
[75]60    for (char const *name = path; *name; name++)
61        if ((img = IMG_Load(name)))
62            break;
[22]63
64    if (!img)
65    {
66        SDL_Quit();
67        exit(1);
68    }
69
[75]70    glGenTextures(1, data->texture);
[22]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
[75]80TileSet::~TileSet()
[22]81{
[75]82    glDeleteTextures(1, data->texture);
83
[22]84    free(data->tiles);
[75]85    free(data->name);
[22]86    delete data;
87}
88
[75]89void TileSet::Ref()
[22]90{
[75]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
[79]104void TileSet::BlitTile(uint32_t id, int x, int y)
[75]105{
[79]106    float tx = .0625f * (id & 0xf);
107    float ty = .0625f * ((id >> 4) & 0xf);
[22]108
109    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
[79]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();
[22]120}
121
Note: See TracBrowser for help on using the repository browser.