The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
surface.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
3 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY.
11
12 See the COPYING file for more details.
13*/
14
15#include "sdl/surface.hpp"
16
17#include "color.hpp"
18#include "sdl/rect.hpp"
19
20#include <utility>
21
22namespace
23{
24void add_refcount(surface& surf)
25{
26 if(surf) {
27 ++surf->refcount;
28 }
29}
30
31void free_surface(surface& surf)
32{
33 if(surf) {
34 SDL_DestroySurface(surf);
35 }
36}
37
38void make_neutral(surface& surf)
39{
40 if(surf && surf->format != SDL_PIXELFORMAT_ARGB8888) {
41 surf = surface(SDL_ConvertSurface(surf, SDL_PIXELFORMAT_ARGB8888));
42 }
43}
44
45} // namespace
46
48 : surface_(surf)
49{
50 make_neutral(*this); // EXTREMELY IMPORTANT!
51}
52
54 : surface_(nullptr)
55{
56 if (w < 0 || h < 0) {
57 throw std::invalid_argument("Creating surface with negative dimensions");
58 }
59
60 surface_ = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_ARGB8888);
61}
62
64 : surface_(s)
65{
66 add_refcount(*this);
67}
68
70 : surface_(std::exchange(s.surface_, nullptr))
71{
72}
73
75{
76 free_surface(*this);
77}
78
80{
81 if(this != &s) {
82 free_surface(*this);
83 surface_ = s;
84 add_refcount(*this);
85 }
86
87 return *this;
88}
89
91{
92 free_surface(*this);
93 surface_ = std::exchange(s.surface_, nullptr);
94 return *this;
95}
96
98{
99 if(!surface_) {
100 return {};
101 }
102 // Use SDL_DuplicateSurface to make a copy
103 return surface(SDL_DuplicateSurface(surface_));
104}
105
107{
108 if(!surface_) {
109 return {};
110 }
111
112 return point{surface_->w, surface_->h};
113}
114
115std::size_t surface::area() const
116{
117 return surface_ ? surface_->w * surface_->h : 0;
118}
119
120std::ostream& operator<<(std::ostream& stream, const surface& surf)
121{
122 if(!surf.get()) {
123 stream << "<null surface>";
124 } else if(!surf->format) {
125 stream << "<invalid surface>";
126 } else {
127 const SDL_PixelFormatDetails* details = SDL_GetPixelFormatDetails(surf->format);
128
129 stream << "{ " << surf->w << 'x' << surf->h << '@'
130 << details->bits_per_pixel << "bpp"
131 << (SDL_GetSurfacePalette(surf)->colors ? " indexed" : "")
132 << " refcount=" << surf->refcount
133 << " }";
134 }
135
136 return stream;
137}
~surface()
Definition surface.cpp:74
surface()=default
point size() const
Dimensions of the surface.
Definition surface.cpp:106
surface clone() const
Creates a new, duplicate surface in memory using the 'neutral' pixel format.
Definition surface.cpp:97
SDL_Surface * surface_
Definition surface.hpp:64
SDL_Surface * get() const
Definition surface.hpp:60
std::size_t area() const
Total area of the surface in square pixels.
Definition surface.cpp:115
surface & operator=(const surface &s)
Definition surface.cpp:79
int w
Definition pathfind.cpp:188
Contains the SDL_Rect helper code.
surface surf
Image.
Holds a 2D point.
Definition point.hpp:25
std::ostream & operator<<(std::ostream &stream, const surface &surf)
Definition surface.cpp:120
static map_location::direction s
#define h