The Battle for Wesnoth  1.17.4+dev
texture.cpp
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 #include "sdl/texture.hpp"
16 
17 #include "log.hpp"
18 #include "sdl/render_utils.hpp"
19 #include "sdl/surface.hpp"
20 #include "video.hpp"
21 
22 static lg::log_domain log_sdl("SDL");
23 #define ERR_SDL LOG_STREAM(err, log_sdl)
24 
25 namespace
26 {
27 // The default pixel format to create textures with.
28 const int default_texture_format = SDL_PIXELFORMAT_ARGB8888;
29 
30 void cleanup_texture(SDL_Texture* t)
31 {
32  if(t != nullptr) {
33  SDL_DestroyTexture(t);
34  ;
35  }
36 }
37 
38 } // namespace
39 
41  : texture_(nullptr)
42 {
43 }
44 
45 texture::texture(SDL_Texture* txt)
46  : texture_(txt, &cleanup_texture)
47 {
48  finalize();
49 }
50 
52  : texture_(nullptr)
53 {
54  SDL_Renderer* renderer = CVideo::get_singleton().get_renderer();
55  if(!renderer) {
56  return;
57  }
58 
59  texture_.reset(SDL_CreateTextureFromSurface(renderer, surf), &cleanup_texture);
60  if(!texture_) {
61  ERR_SDL << "When creating texture from surface: " << SDL_GetError() << std::endl;
62  }
63 }
64 
65 texture::texture(int w, int h, SDL_TextureAccess access)
66  : texture_(nullptr)
67 {
68  reset(w, h, access);
69 }
70 
72 {
73  set_texture_blend_mode(*this, SDL_BLENDMODE_BLEND);
74 }
75 
77 {
78  if(texture_) {
79  texture_.reset();
80  }
81 }
82 
83 void texture::reset(int w, int h, SDL_TextureAccess access)
84 {
85  // No-op if texture is null.
86  reset();
87 
88  SDL_Renderer* renderer = CVideo::get_singleton().get_renderer();
89  if(!renderer) {
90  return;
91  }
92 
93  texture_.reset(SDL_CreateTexture(renderer, default_texture_format, access, w, h), &cleanup_texture);
94  if(!texture_) {
95  ERR_SDL << "When creating texture: " << SDL_GetError() << std::endl;
96  }
97 
98  finalize();
99 }
100 
101 void texture::assign(SDL_Texture* t)
102 {
103  texture_.reset(t, &cleanup_texture);
104 }
105 
107 {
108  texture_ = std::move(t.texture_);
109  return *this;
110 }
111 
112 texture::info::info(SDL_Texture* t)
113  : format(0)
114  , access(0)
115  , w(0)
116  , h(0)
117 {
118  SDL_QueryTexture(t, &format, &access, &w, &h);
119 }
std::shared_ptr< SDL_Texture > texture_
Definition: texture.hpp:88
static CVideo & get_singleton()
Definition: video.hpp:52
#define h
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:27
info(SDL_Texture *t)
Definition: texture.cpp:112
void set_texture_blend_mode(texture &t, SDL_BlendMode mode)
static lg::log_domain log_sdl("SDL")
int w
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
Standard logging facilities (interface).
SDL_Renderer * get_renderer()
Returns a pointer to the underlying window&#39;s renderer.
Definition: video.cpp:475
#define ERR_SDL
Definition: texture.cpp:23
uint32_t format
Definition: texture.hpp:49