Mercurial > molko
comparison src/core/texture.c @ 94:ed72843a7194
core: simplify font/texture interfaces
Expose the structure to avoid calling functions and heap allocations. While
here remove some useless functions.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 30 Mar 2020 13:34:42 +0200 |
parents | 52792b863ff7 |
children | 58133933ea17 |
comparison
equal
deleted
inserted
replaced
93:d4a72fa16225 | 94:ed72843a7194 |
---|---|
23 #include "texture.h" | 23 #include "texture.h" |
24 #include "texture_p.h" | 24 #include "texture_p.h" |
25 #include "util.h" | 25 #include "util.h" |
26 #include "window_p.h" | 26 #include "window_p.h" |
27 | 27 |
28 struct texture * | 28 bool |
29 texture_new(unsigned int w, unsigned int h) | 29 texture_new(struct texture *tex, unsigned int w, unsigned int h) |
30 { | |
31 struct texture *tex = emalloc(sizeof (struct texture)); | |
32 | |
33 if (!(tex->handle = SDL_CreateTexture(win.renderer, | |
34 SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h))) { | |
35 error_sdl(); | |
36 free(tex); | |
37 return NULL; | |
38 } | |
39 | |
40 return tex; | |
41 } | |
42 | |
43 unsigned int | |
44 texture_width(struct texture *tex) | |
45 { | 30 { |
46 assert(tex); | 31 assert(tex); |
47 | 32 |
48 int width; | 33 tex->handle = SDL_CreateTexture(win.renderer, |
34 SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h); | |
49 | 35 |
50 if (SDL_QueryTexture(tex->handle, NULL, NULL, &width, NULL) < 0) | 36 if (!tex->handle) { |
51 return 0; | 37 tex->w = tex->h = 0; |
38 return error_sdl(); | |
39 } | |
52 | 40 |
53 return width; | 41 tex->w = w; |
42 tex->h = h; | |
43 | |
44 return true; | |
54 } | 45 } |
55 | 46 |
56 unsigned int | 47 bool |
57 texture_height(struct texture *tex) | 48 texture_ok(const struct texture *tex) |
58 { | 49 { |
59 assert(tex); | 50 assert(tex); |
60 | 51 |
61 int height; | 52 return tex->handle && tex->w && tex->h; |
62 | |
63 if (SDL_QueryTexture(tex->handle, NULL, NULL, NULL, &height) < 0) | |
64 return 0; | |
65 | |
66 return height; | |
67 } | 53 } |
68 | 54 |
69 void | 55 void |
70 texture_draw(struct texture *tex, int x, int y) | 56 texture_draw(struct texture *tex, int x, int y) |
71 { | 57 { |
58 assert(tex); | |
59 | |
72 SDL_Rect dst = { | 60 SDL_Rect dst = { |
73 .x = x, | 61 .x = x, |
74 .y = y, | 62 .y = y, |
75 .w = 0, | 63 .w = tex->w, |
76 .h = 0 | 64 .h = tex->h |
77 }; | 65 }; |
78 | 66 |
79 assert(tex); | |
80 | |
81 /* We need to determine size */ | |
82 SDL_QueryTexture(tex->handle, NULL, NULL, &dst.w, &dst.h); | |
83 SDL_RenderCopy(win.renderer, tex->handle, NULL, &dst); | 67 SDL_RenderCopy(win.renderer, tex->handle, NULL, &dst); |
84 } | 68 } |
85 | 69 |
86 void | 70 void |
87 texture_draw_ex(struct texture *tex, | 71 texture_scale(struct texture *tex, |
88 int src_x, | 72 int src_x, |
89 int src_y, | 73 int src_y, |
90 unsigned src_w, | 74 unsigned src_w, |
91 unsigned src_h, | 75 unsigned src_h, |
92 int dst_x, | 76 int dst_x, |
93 int dst_y, | 77 int dst_y, |
94 unsigned dst_w, | 78 unsigned dst_w, |
95 unsigned dst_h, | 79 unsigned dst_h, |
96 double angle) | 80 double angle) |
97 { | 81 { |
98 const SDL_Rect src = { | 82 const SDL_Rect src = { |
99 .x = src_x, | 83 .x = src_x, |
100 .y = src_y, | 84 .y = src_y, |
101 .w = src_w, | 85 .w = src_w, |
110 | 94 |
111 SDL_RenderCopyEx(win.renderer, tex->handle, &src, &dst, angle, NULL, SDL_FLIP_NONE); | 95 SDL_RenderCopyEx(win.renderer, tex->handle, &src, &dst, angle, NULL, SDL_FLIP_NONE); |
112 } | 96 } |
113 | 97 |
114 void | 98 void |
115 texture_close(struct texture *tex) | 99 texture_finish(struct texture *tex) |
116 { | 100 { |
117 assert(tex); | 101 assert(tex); |
118 | 102 |
119 SDL_DestroyTexture(tex->handle); | 103 if (tex->handle) |
120 free(tex); | 104 SDL_DestroyTexture(tex->handle); |
105 | |
106 memset(tex, 0, sizeof (*tex)); | |
121 } | 107 } |
122 | 108 |
123 /* private */ | 109 /* private */ |
124 | 110 |
125 struct texture * | 111 bool |
126 texture_from_surface(SDL_Surface *surface) | 112 texture_from_surface(struct texture *tex, SDL_Surface *surface) |
127 { | 113 { |
114 assert(tex); | |
128 assert(surface); | 115 assert(surface); |
129 | 116 |
130 struct texture *texture = ecalloc(1, sizeof (struct texture)); | 117 if (!(tex->handle = SDL_CreateTextureFromSurface(win.renderer, surface))) { |
131 | 118 tex->w = tex->h = 0; |
132 if (!(texture->handle = SDL_CreateTextureFromSurface(win.renderer, surface))) { | 119 return error_sdl(); |
133 error_sdl(); | |
134 free(texture); | |
135 return NULL; | |
136 } | 120 } |
137 | 121 |
138 return texture; | 122 tex->w = surface->w; |
123 tex->h = surface->h; | |
124 | |
125 return true; | |
139 } | 126 } |