source: trunk/src/tileset.cpp @ 221

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

Complete Lol Engine / Deus Hax / Monsterz split.

  • Property svn:keywords set to Id
File size: 3.5 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 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)
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    /* FIXME: check for non-zero here */
82    data->nw = data->img->w / w;
83    data->nh = data->img->h / h;
84    data->ntiles = data->nw * data->nh;
85    data->tx = (float)w / data->img->w;
86    data->ty = (float)h / data->img->h;
87
88    drawgroup = DRAWGROUP_BEFORE;
89}
90
91TileSet::~TileSet()
92{
93    free(data->tiles);
94    free(data->name);
95    delete data;
96}
97
98void TileSet::TickDraw(float deltams)
99{
100    Entity::TickDraw(deltams);
101
102    if (destroy)
103    {
104        if (data->img)
105            SDL_FreeSurface(data->img);
106        else
107            glDeleteTextures(1, &data->texture);
108    }
109    else if (data->img)
110    {
111        GLuint format = data->img->format->Amask ? GL_RGBA : GL_RGB;
112        int planes = data->img->format->Amask ? 4 : 3;
113
114        glGenTextures(1, &data->texture);
115        glBindTexture(GL_TEXTURE_2D, data->texture);
116
117        glTexImage2D(GL_TEXTURE_2D, 0, planes, data->img->w, data->img->h, 0,
118                     format, GL_UNSIGNED_BYTE, data->img->pixels);
119
120        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
121        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
122
123        SDL_FreeSurface(data->img);
124        data->img = NULL;
125    }
126}
127
128char const *TileSet::GetName()
129{
130    return data->name;
131}
132
133void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o)
134{
135    float tx = data->tx * ((id & 0xffff) % data->nw);
136    float ty = data->ty * ((id & 0xffff) / data->nw);
137
138    float sqrt2 = sqrtf(2.0f);
139    int off = o ? data->h : 0;
140    int dx = data->w;
141    int dy = data->h * 38 / 32; /* Magic... fix this one day */
142    int dy2 = data->h * 70 / 32;
143
144    if (!data->img)
145    {
146        glBindTexture(GL_TEXTURE_2D, data->texture);
147        glBegin(GL_QUADS);
148            glTexCoord2f(tx, ty);
149            glVertex3f(x, sqrt2 * (y - dy - off), sqrt2 * (z + off));
150            glTexCoord2f(tx + data->tx, ty);
151            glVertex3f(x + dx, sqrt2 * (y - dy - off), sqrt2 * (z + off));
152            glTexCoord2f(tx + data->tx, ty + data->ty);
153            glVertex3f(x + dx, sqrt2 * (y - dy2), sqrt2 * z);
154            glTexCoord2f(tx, ty + data->ty);
155            glVertex3f(x, sqrt2 * (y - dy2), sqrt2 * z);
156        glEnd();
157    }
158}
159
Note: See TracBrowser for help on using the repository browser.