comparison src/texture.c @ 3:c013f41a72a5

core: implement basic textures, closes #2442
author David Demelier <markand@malikania.fr>
date Mon, 06 Jan 2020 20:20:58 +0100
parents
children 65398df5fb5c
comparison
equal deleted inserted replaced
2:832c20d6cce9 3:c013f41a72a5
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 "texture.h"
22 #include "texture_p.h"
23 #include "window_p.h"
24
25 void
26 texture_draw(struct texture *tex, int x, int y)
27 {
28 SDL_Rect dst = {
29 .x = x,
30 .y = y,
31 .w = 0,
32 .h = 0
33 };
34
35 assert(tex);
36
37 /* We need to determine size */
38 SDL_QueryTexture(tex->handle, NULL, NULL, &dst.w, &dst.h);
39 SDL_RenderCopy(win.renderer, tex->handle, NULL, &dst);
40 }
41
42 void
43 texture_draw_ex(struct texture *tex,
44 int src_x,
45 int src_y,
46 unsigned src_w,
47 unsigned src_h,
48 int dst_x,
49 int dst_y,
50 unsigned dst_w,
51 unsigned dst_h,
52 double angle)
53 {
54 const SDL_Rect src = {
55 .x = src_x,
56 .y = src_y,
57 .w = src_w,
58 .h = src_h
59 };
60 const SDL_Rect dst = {
61 .x = dst_x,
62 .y = dst_y,
63 .w = dst_w,
64 .h = dst_h
65 };
66
67 SDL_RenderCopyEx(win.renderer, tex->handle, &src, &dst, angle, NULL, SDL_FLIP_NONE);
68 }
69
70 void
71 texture_close(struct texture *tex)
72 {
73 assert(tex);
74
75 SDL_DestroyTexture(tex->handle);
76 free(tex);
77 }
78
79 /* private */
80
81 struct texture *
82 texture_from_surface(SDL_Surface *surface)
83 {
84 struct texture *texture;
85
86 if (!(texture = calloc(1, sizeof (struct texture))))
87 return NULL;
88 if (!(texture->handle = SDL_CreateTextureFromSurface(win.renderer, surface))) {
89 free(texture);
90 return NULL;
91 }
92
93 return texture;
94 }