source: trunk/src/tileset.cpp @ 95

Last change on this file since 95 was 95, checked in by sam, 10 years ago

TileSet is now a child of Asset.

  • Property svn:keywords set to Id
File size: 1.9 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    char *name;
30    int *tiles;
31    int ntiles;
32
33    SDL_Surface *img;
34    GLuint texture[1];
35};
36
37/*
38 * Public TileSet class
39 */
40
41TileSet::TileSet(char const *path)
42{
43    data = new TileSetData();
44    data->name = strdup(path);
45    data->tiles = NULL;
46    data->ntiles = 0;
47    data->img = NULL;
48
49    for (char const *name = path; *name; name++)
50        if ((data->img = IMG_Load(name)))
51            break;
52
53    if (!data->img)
54    {
55        SDL_Quit();
56        exit(1);
57    }
58
59    glGenTextures(1, data->texture);
60    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
61
62    glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
63                 GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
64
65    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
66    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
67}
68
69TileSet::~TileSet()
70{
71    glDeleteTextures(1, data->texture);
72
73    free(data->tiles);
74    free(data->name);
75    delete data;
76}
77
78char const *TileSet::GetName()
79{
80    return data->name;
81}
82
83void TileSet::BlitTile(uint32_t id, int x, int y)
84{
85    float tx = .0625f * (id & 0xf);
86    float ty = .0625f * ((id >> 4) & 0xf);
87
88    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
89    glBegin(GL_QUADS);
90        glTexCoord2f(tx, ty);
91        glVertex2f(x, y);
92        glTexCoord2f(tx + .0625f, ty);
93        glVertex2f(x + 32, y);
94        glTexCoord2f(tx + .0625f, ty + .0625f);
95        glVertex2f(x + 32, y + 32);
96        glTexCoord2f(tx, ty + .0625f);
97        glVertex2f(x, y + 32);
98    glEnd();
99}
100
Note: See TracBrowser for help on using the repository browser.