The Battle for Wesnoth  1.17.4+dev
texture.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2022
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 #pragma once
16 
17 #include <SDL2/SDL_render.h>
18 
19 #include <memory>
20 
21 class surface;
22 
23 /**
24  * Wrapper class to encapsulate creation and management of an SDL_Texture.
25  * Supports free creation and creation from a surface.
26  */
27 class texture
28 {
29 public:
30  /** Default ctor. Texture will be a nullptr. */
31  texture();
32 
33  texture(const texture&) = default;
34 
35  /** Assigns the given texture to this one. */
36  explicit texture(SDL_Texture* txt);
37 
38  /** Construct a texture from a surface. */
39  explicit texture(const surface& surf);
40 
41  /** Construct a texture of the specified size and access type. */
42  texture(int w, int h, SDL_TextureAccess access);
43 
44  /** Small wrapper that queries metadata about the provided texture. */
45  struct info
46  {
47  explicit info(SDL_Texture* t);
48 
49  uint32_t format;
50  int access;
51  int w;
52  int h;
53  };
54 
55  /** Queries metadata about the texture, such as its dimensions. */
56  const info get_info() const
57  {
58  return info(*this);
59  }
60 
61  /** Releases ownership of the managed texture and resets the ptr to null. */
62  void reset();
63 
64  /** Releases ownership of the managed texture and creates a new one. */
65  void reset(int w, int h, SDL_TextureAccess access);
66 
67  /** Replaces ownership of the managed texture with the given one. */
68  void assign(SDL_Texture* t);
69 
70  texture& operator=(const texture& t) = default;
71 
72  /** Move assignment. Releases ownership of the managed texture from the passed object. */
74 
75  operator SDL_Texture*() const
76  {
77  return texture_.get();
78  }
79 
80  bool null() const
81  {
82  return texture_ == nullptr;
83  }
84 
85 private:
86  void finalize();
87 
88  std::shared_ptr<SDL_Texture> texture_;
89 };
std::shared_ptr< SDL_Texture > texture_
Definition: texture.hpp:88
#define h
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:27
bool null() const
Definition: texture.hpp:80
info(SDL_Texture *t)
Definition: texture.cpp:112
int w
const info get_info() const
Queries metadata about the texture, such as its dimensions.
Definition: texture.hpp:56
texture & operator=(const texture &t)=default
void reset()
Releases ownership of the managed texture and resets the ptr to null.
Definition: texture.cpp:76
texture()
Default ctor.
Definition: texture.cpp:40
double t
Definition: astarsearch.cpp:65
void assign(SDL_Texture *t)
Replaces ownership of the managed texture with the given one.
Definition: texture.cpp:101
void finalize()
Definition: texture.cpp:71
Small wrapper that queries metadata about the provided texture.
Definition: texture.hpp:45
uint32_t format
Definition: texture.hpp:49