Changeset 689


Ignore:
Timestamp:
Feb 22, 2011, 2:39:26 PM (11 years ago)
Author:
sam
Message:

Use the new Image class instead of SDL_image in the TileSet class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tileset.cpp

    r686 r689  
    1616#include <cstdio>
    1717#include <cmath>
     18#include <cstring>
    1819
    1920#ifdef WIN32
     
    2223#endif
    2324
    24 #if defined USE_SDL
    25 #   include <SDL.h>
    26 #   include <SDL_image.h>
    27 #endif
    28 
    2925#include "core.h"
    3026#include "lolgl.h"
     
    4440    char *name, *path;
    4541    int *tiles, ntiles;
    46     vec2i size, count;
     42    vec2i size, isize, count;
    4743    float dilate, tx, ty;
    4844
    49 #if defined USE_SDL
    50     SDL_Surface *img;
    51 #endif
     45    Image *img;
    5246    GLuint texture;
    5347};
     
    6559
    6660    data->tiles = NULL;
    67 #if defined USE_SDL
    68     data->img = NULL;
    69 #endif
    7061    data->texture = 0;
    71 
    72 #if defined USE_SDL
    73     for (char const *name = path; *name; name++)
    74         if ((data->img = IMG_Load(name)))
    75             break;
    76 
    77     if (!data->img)
    78     {
    79 #if !LOL_RELEASE
    80         fprintf(stderr, "ERROR: could not load %s\n", path);
    81 #endif
    82         SDL_Quit();
    83         exit(1);
    84     }
     62    data->img = new Image(path);
     63    data->isize = data->img->GetSize();
    8564
    8665    if (count.i > 0 && count.j > 0)
    8766    {
    8867        data->count = count;
    89         data->size = vec2i(data->img->w, data->img->h) / count;
     68        data->size = data->isize / count;
    9069    }
    9170    else
     
    9372        if (size.x <= 0 || size.y <= 0)
    9473            size = 32;
    95         data->count.i = data->img->w > size.i ? data->img->w / size.i : 1;
    96         data->count.j = data->img->h > size.j ? data->img->h / size.j : 1;
     74        data->count.i = data->isize.x > size.i ? data->isize.x / size.i : 1;
     75        data->count.j = data->isize.y > size.j ? data->isize.y / size.j : 1;
    9776        data->size = size;
    9877    }
    9978
    100     data->tx = (float)data->size.x / PotUp(data->img->w);
    101     data->ty = (float)data->size.y / PotUp(data->img->h);
    102 #endif
     79    data->tx = (float)data->size.x / PotUp(data->isize.x);
     80    data->ty = (float)data->size.y / PotUp(data->isize.y);
    10381
    10482    data->dilate = dilate;
     
    11997    Entity::TickDraw(deltams);
    12098
    121 #if defined USE_SDL
    12299    if (IsDestroying())
    123100    {
    124101        if (data->img)
    125             SDL_FreeSurface(data->img);
     102            delete data->img;
    126103        else
    127104            glDeleteTextures(1, &data->texture);
     
    129106    else if (data->img)
    130107    {
    131         GLuint format = data->img->format->Amask ? GL_RGBA : GL_RGB;
    132         int planes = data->img->format->Amask ? 4 : 3;
    133 
    134         int w = PotUp(data->img->w);
    135         int h = PotUp(data->img->h);
    136 
    137         uint8_t *pixels = (uint8_t *)data->img->pixels;
    138         if (w != data->img->w || h != data->img->h)
     108        GLuint format;
     109        int planes;
     110
     111        switch (data->img->GetFormat())
     112        {
     113        case Image::FORMAT_RGB:
     114           format = GL_RGB;
     115           planes = 3;
     116           break;
     117        case Image::FORMAT_RGBA:
     118        default:
     119           format = GL_RGBA;
     120           planes = 4;
     121           break;
     122        }
     123
     124        int w = PotUp(data->isize.x);
     125        int h = PotUp(data->isize.y);
     126
     127        uint8_t *pixels = (uint8_t *)data->img->GetData();
     128        if (w != data->isize.x || h != data->isize.y)
    139129        {
    140130            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
    141             for (int line = 0; line < data->img->h; line++)
     131            for (int line = 0; line < data->isize.y; line++)
    142132                memcpy(tmp + planes * w * line,
    143                        pixels + planes * data->img->w * line,
    144                        planes * data->img->w);
     133                       pixels + planes * data->isize.x * line,
     134                       planes * data->isize.x);
    145135            pixels = tmp;
    146136        }
     
    155145        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    156146
    157         if (pixels != data->img->pixels)
     147        if (pixels != data->img->GetData())
    158148            free(pixels);
    159         SDL_FreeSurface(data->img);
     149        delete data->img;
    160150        data->img = NULL;
    161151    }
    162 #endif
    163152}
    164153
     
    180169void TileSet::Bind()
    181170{
    182 #if defined USE_SDL
    183     if (!data->img)
     171    if (!data->img && data->texture)
    184172        glBindTexture(GL_TEXTURE_2D, data->texture);
    185 #endif
    186173}
    187174
     
    197184    int dz = o ? data->size.y : 0;
    198185
    199 #if defined USE_SDL
    200     if (!data->img)
     186    if (!data->img && data->texture)
    201187    {
    202188        float tmp[10];
     
    239225    }
    240226    else
    241 #endif
    242227    {
    243228        memset(vertex, 0, 3 * sizeof(float));
Note: See TracChangeset for help on using the changeset viewer.