source: trunk/src/tileset.cpp @ 280

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

Print error reports before panicking when asset loads fail.

  • Property svn:keywords set to Id
File size: 4.2 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  : data(new TileSetData())
58{
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#if !FINAL_RELEASE
71        fprintf(stderr, "ERROR: could not load %s\n", path);
72#endif
73        SDL_Quit();
74        exit(1);
75    }
76
77    if (w <= 0)
78        w = 32;
79    if (h <= 0)
80        h = 32;
81
82    data->w = w;
83    data->h = h;
84    data->dilate = dilate;
85    /* FIXME: check for non-zero here */
86    data->nw = data->img->w / w;
87    data->nh = data->img->h / h;
88    data->ntiles = data->nw * data->nh;
89    data->tx = (float)w / PotUp(data->img->w);
90    data->ty = (float)h / PotUp(data->img->h);
91
92    drawgroup = DRAWGROUP_BEFORE;
93}
94
95TileSet::~TileSet()
96{
97    free(data->tiles);
98    free(data->name);
99    delete data;
100}
101
102void TileSet::TickDraw(float deltams)
103{
104    Entity::TickDraw(deltams);
105
106    if (destroy)
107    {
108        if (data->img)
109            SDL_FreeSurface(data->img);
110        else
111            glDeleteTextures(1, &data->texture);
112    }
113    else if (data->img)
114    {
115        GLuint format = data->img->format->Amask ? GL_RGBA : GL_RGB;
116        int planes = data->img->format->Amask ? 4 : 3;
117
118        int w = PotUp(data->img->w);
119        int h = PotUp(data->img->h);
120
121        uint8_t *pixels = (uint8_t *)data->img->pixels;
122        if (w != data->img->w || h != data->img->h)
123        {
124            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
125            for (int line = 0; line < data->img->h; line++)
126                memcpy(tmp + planes * w * line,
127                       pixels + planes * data->img->w * line,
128                       planes * data->img->w);
129            pixels = tmp;
130        }
131
132        glGenTextures(1, &data->texture);
133        glBindTexture(GL_TEXTURE_2D, data->texture);
134
135        glTexImage2D(GL_TEXTURE_2D, 0, planes, w, h, 0,
136                     format, GL_UNSIGNED_BYTE, pixels);
137
138        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
139        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
140
141        if (pixels != data->img->pixels)
142            free(pixels);
143        SDL_FreeSurface(data->img);
144        data->img = NULL;
145    }
146}
147
148char const *TileSet::GetName()
149{
150    return data->name;
151}
152
153void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o)
154{
155    float tx = data->tx * ((id & 0xffff) % data->nw);
156    float ty = data->ty * ((id & 0xffff) / data->nw);
157    float dilate = data->dilate;
158
159    int off = o ? data->h : 0;
160    int dx = data->w;
161    int dy = data->h * 38 / 32; /* Magic... fix this one day */
162    int dy2 = data->h * 70 / 32;
163
164    if (!data->img)
165    {
166        glBindTexture(GL_TEXTURE_2D, data->texture);
167        glBegin(GL_QUADS);
168            glTexCoord2f(tx, ty);
169            glVertex3f(x, dilate * (y - dy - off), dilate * (z + off));
170            glTexCoord2f(tx + data->tx, ty);
171            glVertex3f(x + dx, dilate * (y - dy - off), dilate * (z + off));
172            glTexCoord2f(tx + data->tx, ty + data->ty);
173            glVertex3f(x + dx, dilate * (y - dy2), dilate * z);
174            glTexCoord2f(tx, ty + data->ty);
175            glVertex3f(x, dilate * (y - dy2), dilate * z);
176        glEnd();
177    }
178}
179
Note: See TracBrowser for help on using the repository browser.