/
surface.cpp
104 lines (88 loc) · 2.29 KB
/
surface.cpp
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
/*
Copyright (C) 2003 - 2017 the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "sdl/surface.hpp"
#include "sdl/rect.hpp"
#include "sdl/utils.hpp"
#include "video.hpp"
void surface::free_surface()
{
if(surface_) {
/* Workaround for an SDL bug.
* SDL 2.0.6 frees the blit map unconditionally in SDL_FreeSurface() without checking
* if the reference count has fallen to zero. However, many SDL functions such as
* SDL_ConvertSurface() assume that the blit map is present.
* Thus, we only call SDL_FreeSurface() if this is the last reference to the surface.
* Otherwise we just decrement the reference count ourselves.
*
* - Jyrki, 2017-09-23
*/
if(surface_->refcount > 1 && sdl_get_version() >= version_info(2, 0, 6)) {
--surface_->refcount;
} else {
SDL_FreeSurface(surface_);
}
}
}
surface_restorer::surface_restorer()
: target_(nullptr)
, rect_(sdl::empty_rect)
, surface_(nullptr)
{
}
surface_restorer::surface_restorer(CVideo* target, const SDL_Rect& rect)
: target_(target)
, rect_(rect)
, surface_(nullptr)
{
update();
}
surface_restorer::~surface_restorer()
{
restore();
}
void surface_restorer::restore(SDL_Rect const &dst) const
{
if(surface_.null()) {
return;
}
SDL_Rect dst2 = sdl::intersect_rects(dst, rect_);
if(dst2.w == 0 || dst2.h == 0) {
return;
}
SDL_Rect src = dst2;
src.x -= rect_.x;
src.y -= rect_.y;
sdl_blit(surface_, &src, target_->getSurface(), &dst2);
}
void surface_restorer::restore() const
{
if(surface_.null()) {
return;
}
SDL_Rect dst = rect_;
sdl_blit(surface_, nullptr, target_->getSurface(), &dst);
}
void surface_restorer::update()
{
if(rect_.w <= 0 || rect_.h <= 0) {
surface_.assign(nullptr);
} else {
surface_.assign(::get_surface_portion(target_->getSurface(),rect_));
}
}
void surface_restorer::cancel()
{
surface_.assign(nullptr);
}
bool operator<(const surface& a, const surface& b)
{
return a.get() < b.get();
}