source: trunk/src/image/codec/sdl-image.cpp @ 955

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

core: implement dynamic registration of image loaders.

  • Property svn:keywords set to Id
File size: 1.4 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#if defined USE_SDL_IMAGE
16
17#include <cmath>
18
19#include <SDL.h>
20#include <SDL_image.h>
21
22#include "core.h"
23#include "image/image-private.h"
24
25using namespace std;
26
27namespace lol
28{
29
30/*
31 * Image implementation class
32 */
33
34DECLARE_IMAGE_LOADER(SdlImageData, 50)
35{
36public:
37    virtual bool Open(char const *);
38    virtual bool Close();
39
40    virtual void *GetData() const;
41
42private:
43    SDL_Surface *img;
44};
45
46/*
47 * Public Image class
48 */
49
50bool SdlImageData::Open(char const *path)
51{
52    for (char const *name = path; *name; name++)
53        if ((img = IMG_Load(name)))
54            break;
55
56    if (!img)
57    {
58#if !LOL_RELEASE
59        Log::Error("could not load %s\n", path);
60#endif
61        SDL_Quit();
62        exit(1);
63    }
64
65    size = ivec2(img->w, img->h);
66    format = img->format->Amask ? Image::FORMAT_RGBA : Image::FORMAT_RGB;
67
68    return true;
69}
70
71bool SdlImageData::Close()
72{
73    SDL_FreeSurface(img);
74
75    return true;
76}
77
78void * SdlImageData::GetData() const
79{
80    return img->pixels;
81}
82
83} /* namespace lol */
84
85#endif /* defined USE_SDL_IMAGE */
86
Note: See TracBrowser for help on using the repository browser.