comparison libmlk-core/core/texture.c @ 243:71b3b7036de7

misc: lot of cleanups, - prefix libraries with libmlk, - move assets from source directories closes #2520, - prefix header guards closes #2519
author David Demelier <markand@malikania.fr>
date Sat, 28 Nov 2020 22:37:30 +0100
parents libcore/core/texture.c@f37b8e95aaaa
children 196264679079
comparison
equal deleted inserted replaced
242:4c24604efcab 243:71b3b7036de7
1 /*
2 * texture.c -- basic texture management
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20
21 #include "color.h"
22 #include "error.h"
23 #include "texture.h"
24 #include "texture_p.h"
25 #include "util.h"
26 #include "window.h"
27 #include "window_p.h"
28
29 bool
30 texture_new(struct texture *tex, unsigned int w, unsigned int h)
31 {
32 assert(tex);
33
34 tex->handle = SDL_CreateTexture(RENDERER(),
35 SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
36
37 if (!tex->handle) {
38 tex->w = tex->h = 0;
39 return errorf("%s", SDL_GetError());
40 }
41
42 tex->w = w;
43 tex->h = h;
44
45 return true;
46 }
47
48 bool
49 texture_ok(const struct texture *tex)
50 {
51 return tex && tex->handle && tex->w && tex->h;
52 }
53
54 bool
55 texture_set_blend_mode(struct texture *tex, enum texture_blend blend)
56 {
57 assert(tex);
58 assert(blend >= TEXTURE_BLEND_NONE && blend <= TEXTURE_BLEND_MODULATE);
59
60 static const SDL_BlendMode table[] = {
61 [TEXTURE_BLEND_NONE] = SDL_BLENDMODE_NONE,
62 [TEXTURE_BLEND_BLEND] = SDL_BLENDMODE_BLEND,
63 [TEXTURE_BLEND_ADD] = SDL_BLENDMODE_ADD,
64 [TEXTURE_BLEND_MODULATE] = SDL_BLENDMODE_MOD
65 };
66
67 if (SDL_SetTextureBlendMode(tex->handle, table[blend]) < 0)
68 return errorf("%s", SDL_GetError());
69
70 return true;
71 }
72
73 bool
74 texture_set_alpha_mod(struct texture *tex, unsigned int alpha)
75 {
76 assert(texture_ok(tex));
77 assert(alpha <= 255);
78
79 if (SDL_SetTextureAlphaMod(tex->handle, alpha) < 0)
80 return errorf("%s", SDL_GetError());
81
82 return true;
83 }
84
85 bool
86 texture_set_color_mod(struct texture *tex, unsigned long color)
87 {
88 assert(texture_ok(tex));
89
90 if (SDL_SetTextureColorMod(tex->handle, COLOR_R(color), COLOR_G(color), COLOR_B(color)) < 0)
91 return errorf("%s", SDL_GetError());
92
93 return true;
94 }
95
96 bool
97 texture_draw(const struct texture *tex, int x, int y)
98 {
99 assert(tex);
100
101 SDL_Rect dst = {
102 .x = x,
103 .y = y,
104 .w = tex->w,
105 .h = tex->h
106 };
107
108 if (SDL_RenderCopy(RENDERER(), tex->handle, NULL, &dst) < 0)
109 return errorf("%s", SDL_GetError());
110
111 return true;
112 }
113
114 bool
115 texture_scale(const struct texture *tex,
116 int src_x,
117 int src_y,
118 unsigned src_w,
119 unsigned src_h,
120 int dst_x,
121 int dst_y,
122 unsigned dst_w,
123 unsigned dst_h,
124 double angle)
125 {
126 const SDL_Rect src = {
127 .x = src_x,
128 .y = src_y,
129 .w = src_w,
130 .h = src_h
131 };
132 const SDL_Rect dst = {
133 .x = dst_x,
134 .y = dst_y,
135 .w = dst_w,
136 .h = dst_h
137 };
138
139 if (SDL_RenderCopyEx(RENDERER(), tex->handle, &src, &dst, angle, NULL, SDL_FLIP_NONE) < 0)
140 return errorf("%s", SDL_GetError());
141
142 return true;
143 }
144
145 void
146 texture_finish(struct texture *tex)
147 {
148 assert(tex);
149
150 if (tex->handle)
151 SDL_DestroyTexture(tex->handle);
152
153 memset(tex, 0, sizeof (*tex));
154 }
155
156 /* private */
157
158 bool
159 texture_from_surface(struct texture *tex, SDL_Surface *surface)
160 {
161 assert(tex);
162 assert(surface);
163
164 if (!(tex->handle = SDL_CreateTextureFromSurface(RENDERER(), surface))) {
165 tex->w = tex->h = 0;
166 return errorf("%s", SDL_GetError());
167 }
168
169 tex->w = surface->w;
170 tex->h = surface->h;
171
172 SDL_FreeSurface(surface);
173
174 return true;
175 }