source: trunk/src/image.cpp @ 688

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

Add the Image class for better abstraction.

File size: 1.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 <cmath>
16#include <cstdio>
17
18#if defined USE_SDL
19#   include <SDL.h>
20#   include <SDL_image.h>
21#endif
22
23#include "core.h"
24#include "lolgl.h"
25
26namespace lol
27{
28
29/*
30 * Image implementation class
31 */
32
33class ImageData
34{
35    friend class Image;
36
37private:
38    vec2i size;
39    Image::format_t format;
40
41#if defined USE_SDL
42    SDL_Surface *img;
43#endif
44};
45
46/*
47 * Public Image class
48 */
49
50Image::Image(char const *path)
51  : data(new ImageData())
52{
53#if defined USE_SDL
54    for (char const *name = path; *name; name++)
55        if ((data->img = IMG_Load(name)))
56            break;
57
58    if (!data->img)
59    {
60#if !LOL_RELEASE
61        fprintf(stderr, "ERROR: could not load %s\n", path);
62#endif
63        SDL_Quit();
64        exit(1);
65    }
66
67    data->size = vec2i(data->img->w, data->img->h);
68    data->format = data->img->format->Amask ? FORMAT_RGBA : FORMAT_RGB;
69#else
70    data->size = 0;
71    data->format = FORMAT_UNKNOWN;
72#endif
73}
74
75vec2i Image::GetSize() const
76{
77    return data->size;
78}
79
80Image::format_t Image::GetFormat() const
81{
82    return data->format;
83}
84
85void * Image::GetData() const
86{
87    return data->img->pixels;
88}
89
90Image::~Image()
91{
92#if defined USE_LOL
93    SDL_FreeSurface(data->img);
94#endif
95    delete data;
96}
97
98} /* namespace lol */
99
Note: See TracBrowser for help on using the repository browser.