/
texture.cpp
132 lines (106 loc) · 2.57 KB
/
texture.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
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
/*
Copyright (C) 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/texture.hpp"
#include "log.hpp"
#include "sdl/surface.hpp"
#include "video.hpp"
#include <cassert>
#include <iostream>
static lg::log_domain log_sdl("SDL");
#define ERR_SDL LOG_STREAM(err, log_sdl)
// The default pixel format to create textures with.
static int default_texture_format = SDL_PIXELFORMAT_ARGB8888;
texture::texture()
: texture_(nullptr)
{
}
texture::texture(SDL_Texture* txt)
: texture_(txt)
{
finalize();
}
texture::texture(const surface& surf)
: texture_(nullptr)
{
SDL_Renderer* renderer = CVideo::get_singleton().get_renderer();
if(!renderer) {
return;
}
texture_ = SDL_CreateTextureFromSurface(renderer, surf);
if(!texture_) {
ERR_SDL << "When creating texture from surface: " << SDL_GetError() << std::endl;
}
}
texture::texture(int w, int h, SDL_TextureAccess access)
: texture_(nullptr)
{
reset(w, h, access);
}
texture::~texture()
{
destroy_texture();
}
void texture::finalize()
{
SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND);
}
void texture::reset(int w, int h, SDL_TextureAccess access)
{
// No-op if texture is null.
destroy_texture();
SDL_Renderer* renderer = CVideo::get_singleton().get_renderer();
if(!renderer) {
return;
}
texture_ = SDL_CreateTexture(renderer, default_texture_format, access, w, h);
if(!texture_) {
ERR_SDL << "When creating texture: " << SDL_GetError() << std::endl;
}
finalize();
}
void texture::destroy_texture()
{
if(texture_) {
SDL_DestroyTexture(texture_);
}
}
#if 0
texture& texture::operator=(texture&& t)
{
destroy_texture();
texture_ = t.texture_;
t.texture_ = nullptr;
return *this;
}
#endif
texture::info::info(const texture& t)
: format(0)
, access(0)
, w(0)
, h(0)
{
SDL_QueryTexture(t, &format, &access, &w, &h);
}
render_target_setter::render_target_setter(texture& t)
: renderer_(CVideo::get_singleton().get_renderer())
{
if(renderer_) {
// Validate we can render to this texture.
assert(t.get_info().access == SDL_TEXTUREACCESS_TARGET);
SDL_SetRenderTarget(renderer_, t);
}
}
render_target_setter::~render_target_setter()
{
if(renderer_) {
SDL_SetRenderTarget(renderer_, nullptr);
}
}