source: trunk/src/tileset.cpp @ 222

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

Add support for rotated views and stretched tiles.

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16#include <cmath>
17
18#ifdef WIN32
19#   define WIN32_LEAN_AND_MEAN
20#   include <windows.h>
21#endif
22#if defined __APPLE__ && defined __MACH__
23#   include <OpenGL/gl.h>
24#else
25#   define GL_GLEXT_PROTOTYPES
26#   include <GL/gl.h>
27#endif
28
29#include <SDL.h>
30#include <SDL_image.h>
31
32#include "core.h"
33
34/*
35 * TileSet implementation class
36 */
37
38class TileSetData
39{
40    friend class TileSet;
41
42private:
43    char *name;
44    int *tiles;
45    int w, h, nw, nh, ntiles;
46    float dilate, tx, ty;
47
48    SDL_Surface *img;
49    GLuint texture;
50};
51
52/*
53 * Public TileSet class
54 */
55
56TileSet::TileSet(char const *path, int w, int h, float dilate)
57{
58    data = new TileSetData();
59    data->name = strdup(path);
60    data->tiles = NULL;
61    data->img = NULL;
62    data->texture = 0;
63
64    for (char const *name = path; *name; name++)
65        if ((data->img = IMG_Load(name)))
66            break;
67
68    if (!data->img)
69    {
70        SDL_Quit();
71        exit(1);
72    }
73
74    if (w <= 0)
75        w = 32;
76    if (h <= 0)
77        h = 32;
78
79    data->w = w;
80    data->h = h;
81    data->dilate = dilate;
82    /* FIXME: check for non-zero here */
83    data->nw = data->img->w / w;
84    data->nh = data->img->h / h;
85    data->ntiles = data->nw * data->nh;
86    data->tx = (float)w / data->img->w;
87    data->ty = (float)h / data->img->h;
88
89    drawgroup = DRAWGROUP_BEFORE;
90}
91
92TileSet::~TileSet()
93{
94    free(data->tiles);
95    free(data->name);
96    delete data;
97}
98
99void TileSet::TickDraw(float deltams)
100{
101    Entity::TickDraw(deltams);
102
103    if (destroy)
104    {
105        if (data->img)
106            SDL_FreeSurface(data->img);
107        else
108            glDeleteTextures(1, &data->texture);
109    }
110    else if (data->img)
111    {
112        GLuint format = data->img->format->Amask ? GL_RGBA : GL_RGB;
113        int planes = data->img->format->Amask ? 4 : 3;
114
115        glGenTextures(1, &data->texture);
116        glBindTexture(GL_TEXTURE_2D, data->texture);
117
118        glTexImage2D(GL_TEXTURE_2D, 0, planes, data->img->w, data->img->h, 0,
119                     format, GL_UNSIGNED_BYTE, data->img->pixels);
120
121        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
122        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
123
124        SDL_FreeSurface(data->img);
125        data->img = NULL;
126    }
127}
128
129char const *TileSet::GetName()
130{
131    return data->name;
132}
133
134void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o)
135{
136    float tx = data->tx * ((id & 0xffff) % data->nw);
137    float ty = data->ty * ((id & 0xffff) / data->nw);
138    float dilate = data->dilate;
139
140    int off = o ? data->h : 0;
141    int dx = data->w;
142    int dy = data->h * 38 / 32; /* Magic... fix this one day */
143    int dy2 = data->h * 70 / 32;
144
145    if (!data->img)
146    {
147        glBindTexture(GL_TEXTURE_2D, data->texture);
148        glBegin(GL_QUADS);
149            glTexCoord2f(tx, ty);
150            glVertex3f(x, dilate * (y - dy - off), dilate * (z + off));
151            glTexCoord2f(tx + data->tx, ty);
152            glVertex3f(x + dx, dilate * (y - dy - off), dilate * (z + off));
153            glTexCoord2f(tx + data->tx, ty + data->ty);
154            glVertex3f(x + dx, dilate * (y - dy2), dilate * z);
155            glTexCoord2f(tx, ty + data->ty);
156            glVertex3f(x, dilate * (y - dy2), dilate * z);
157        glEnd();
158    }
159}
160
Note: See TracBrowser for help on using the repository browser.