summaryrefslogtreecommitdiffstats
path: root/client/gui/softtexture.cpp
blob: 562edeabfa9771bf327723127577b761c22f323c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "common.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


#include "softtexture.h"
#include "softrenderer.h"

#include "CEGUIImageCodec.h"
#include "CEGUISystem.h"
#include "CEGUIExceptions.h"

namespace CEGUI
{


SoftTexture::SoftTexture(Renderer* owner)
    : Texture (owner)
    , _surf (NULL)
    , _width (0)
    , _height (0)
{

}

SoftTexture::SoftTexture(Renderer* owner, uint size)
    : Texture (owner)
    , _surf (new uint32[size * size])
    , _width (size)
    , _height (size)
{

}

SoftTexture::SoftTexture(Renderer* owner, const String& filename,
                         const String& resourceGroup)
    : Texture (owner)
    , _surf (NULL)
    , _width (0)
    , _height (0)
{
    loadFromFile(filename, resourceGroup);
}


SoftTexture::~SoftTexture()
{
    freeSurf();
}

void SoftTexture::freeSurf()
{
    _width = _height = 0;
    delete[] _surf;
    _surf = NULL;
}

void SoftTexture::loadFromFile(const String& filename, const String& resourceGroup)
{
    freeSurf();
    SoftRenderer* renderer = static_cast<SoftRenderer*>(getRenderer());
    RawDataContainer texture_file;
    ResourceProvider* resource_provider = System::getSingleton().getResourceProvider();
    resource_provider->loadRawDataContainer(filename, texture_file, resourceGroup);
    ImageCodec *codec = renderer->getImageCodec();
    Texture* res = codec->load(texture_file, this);
    resource_provider->unloadRawDataContainer(texture_file);
    if (!res) {
        throw RendererException("load from file failed");
    }
}

void SoftTexture::loadFromMemory(const void* buffPtr, uint buffWidth,
                                 uint buffHeight,
                                 PixelFormat pixelFormat)
{
    freeSurf();
    _surf = new uint32[buffWidth * buffHeight];
    _width = buffWidth;
    _height = buffHeight;

    switch (pixelFormat) {
    case PF_RGBA: {
        const uint32_t *src = static_cast<const uint32_t *>(buffPtr);
        uint32* line = _surf;
        uint32* end_line = _surf + _width * _height;
        for (int i = 0; line != end_line; line += _width, i++) {
            uint32* pixel = line;
            uint32* end_pixel = pixel + _width;
            for (; pixel != end_pixel; pixel++, src++) {
                ((uint8_t*)pixel)[0] = ((uint8_t*)src)[2];
                ((uint8_t*)pixel)[1] = ((uint8_t*)src)[1];
                ((uint8_t*)pixel)[2] = ((uint8_t*)src)[0];
                ((uint8_t*)pixel)[3] = ((uint8_t*)src)[3];
            }
        }
        break;
    }
    case PF_RGB: {
        const uint8_t *src = static_cast<const uint8_t *>(buffPtr);
        uint32* line = _surf;
        uint32* end_line = _surf + _width * _height;
        for (int i = 0; line != end_line; line += _width, i++) {
            uint8* pixel = (uint8*)line;
            uint8* end_pixel = (uint8*)(line + _width);
            for (; pixel != end_pixel; pixel += 4, src += 3) {
                pixel[2] = src[0];
                pixel[1] = src[1];
                pixel[0] = src[2];
                pixel[3] = 0xff;
            }
        }
        break;
    }
    default:
        throw RendererException("invalid pixel format");
    }
}


}