source: trunk/src/image.cpp @ 690

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

Create a fake Image if SDL_image is not available, so that we can test
stuff anyway.

File size: 2.1 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#else
44    uint8_t *dummy;
45#endif
46};
47
48/*
49 * Public Image class
50 */
51
52Image::Image(char const *path)
53  : data(new ImageData())
54{
55#if defined USE_SDL
56    for (char const *name = path; *name; name++)
57        if ((data->img = IMG_Load(name)))
58            break;
59
60    if (!data->img)
61    {
62#if !LOL_RELEASE
63        fprintf(stderr, "ERROR: could not load %s\n", path);
64#endif
65        SDL_Quit();
66        exit(1);
67    }
68
69    data->size = vec2i(data->img->w, data->img->h);
70    data->format = data->img->format->Amask ? FORMAT_RGBA : FORMAT_RGB;
71#else
72    data->size = 256;
73    data->format = FORMAT_RGBA;
74    data->dummy = (uint8_t *)malloc(256 * 256 * 4 * sizeof(*data->dummy));
75    uint8_t *parser = data->dummy;
76    for (int j = 0; j < 256; j++)
77        for (int i = 0; i < 256; i++)
78        {
79            *parser++ = ((i ^ j) & 1) * 0xff;
80            *parser++ = (uint8_t)i;
81            *parser++ = (uint8_t)j;
82            *parser++ = (((i >> 4) ^ (j >> 4)) & 1) * 0xff;
83        }
84#endif
85}
86
87vec2i Image::GetSize() const
88{
89    return data->size;
90}
91
92Image::format_t Image::GetFormat() const
93{
94    return data->format;
95}
96
97void * Image::GetData() const
98{
99#if defined USE_SDL
100    return data->img->pixels;
101#else
102    return data->dummy;
103#endif
104}
105
106Image::~Image()
107{
108#if defined USE_SDL
109    SDL_FreeSurface(data->img);
110#else
111    free(data->dummy);
112#endif
113    delete data;
114}
115
116} /* namespace lol */
117
Note: See TracBrowser for help on using the repository browser.