summaryrefslogtreecommitdiffstats
path: root/engine/flatsurface.cpp
blob: 1970b7110c4a4b12e7332ecc5f9ffb158138a499 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "flatsurface.h"

#include <iostream>

using namespace std;

FlatSurface::FlatSurface(const char *filename, Uint32 format, SDL_Surface *parent)

    : FlatObject(), parent(parent), hide(false)
{
    setID(filename);

    cout << "FlatSurface: loading " << filename << endl;
    surface = loadOptimizedSurface(filename, format);

    if (!surface)
    {
        cout << "Warning: could not load surface " << filename << endl;
    }

    offset = new SDL_Rect;

    offset->x = 0;
    offset->y = 0;
    offset->w = surface->w;
    offset->h = surface->h;

    viewport = new SDL_Rect;

    viewport->x = 0;
    viewport->y = 0;
    viewport->w = surface->w;
    viewport->h = surface->h;
}

FlatSurface::FlatSurface(SDL_Surface *surface, SDL_Surface *parent)

    : FlatObject(), parent(parent), hide(false)
{
    this->surface = new SDL_Surface(*surface);

    offset = new SDL_Rect;

    offset->x = 0;
    offset->y = 0;
    offset->w = surface->w;
    offset->h = surface->h;

    viewport = new SDL_Rect;

    viewport->x = 0;
    viewport->y = 0;
    viewport->w = surface->w;
    viewport->h = surface->h;
}

FlatSurface::FlatSurface(const FlatSurface &sprite)

    : FlatObject(sprite), parent(sprite.parent),
        hide(sprite.hide)
{
    offset = new SDL_Rect(*sprite.offset);

    viewport = new SDL_Rect(*sprite.viewport);

    surface = copySurface(sprite.surface);
}

FlatSurface::~FlatSurface()
{
    SDL_FreeSurface(surface);

    delete offset;
    delete viewport;
}

void FlatSurface::setOffset(int x, int y, int w, int h)
{
    offset->x = x;
    offset->y = y;

    if (w > 0)
        offset->w = w;

    if (h > 0)
        offset->h = h;
}

void FlatSurface::setViewport(int x, int y, int w, int h)
{
    viewport->x = x;
    viewport->y = y;
    viewport->w = w;
    viewport->h = h;
}

void FlatSurface::setViewport(const SDL_Rect &rect)
{
    *viewport = rect;
}


void FlatSurface::setOffset(const SDL_Rect &offset)
{
    *this->offset = offset;
}

const SDL_Rect * FlatSurface::getOffset() const
{
    return offset;
}

const SDL_Rect * FlatSurface::getViewport() const
{
    return viewport;
}

void FlatSurface::setParent(SDL_Surface *parent)
{
    this->parent = parent;
}

SDL_Surface * FlatSurface::getParent()
{
    return parent;
}

SDL_Surface * FlatSurface::getSurface()
{
    return surface;
}

void FlatSurface::setHidden(bool flag)
{
    hide = flag;
}

bool FlatSurface::isHidden() const
{
    return hide;
}

void FlatSurface::blit()
{
    if (!hide)
        SDL_BlitSurface(surface, viewport, parent, offset);
}

SDL_Surface * FlatSurface::loadOptimizedSurface(const char *filename, Uint32 format)
{
    SDL_Surface * optimized = 0;

    SDL_Surface * loaded = SDL_LoadBMP(filename);

    if (loaded)
    {
        optimized = SDL_ConvertSurfaceFormat(loaded, format, 0);
        SDL_FreeSurface(loaded);
    }

    return optimized;
}

SDL_Surface * FlatSurface::copySurface(SDL_Surface *src)
{
    return SDL_ConvertSurface(src, src->format, src->flags);
}