1 /*
2  * This software is licensed under the terms of the MIT License.
3  * See COPYING for further information.
4  * ---
5  * Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
6  * Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
7  */
8 
9 #ifndef IGUARD_resource_sprite_h
10 #define IGUARD_resource_sprite_h
11 
12 #include "taisei.h"
13 
14 #include "resource.h"
15 #include "texture.h"
16 
17 typedef struct SpriteMargin {
18 	float top, bottom, left, right;
19 } SpriteMargin;
20 
21 typedef struct Sprite {
22 	Texture *tex;
23 	FloatRect tex_area;
24 	union {
25 		FloatExtent extent;
26 		struct { float w, h; };
27 	};
28 	SpriteMargin padding;
29 } Sprite;
30 
sprite_padded_width(const Sprite * restrict spr)31 INLINE float sprite_padded_width(const Sprite *restrict spr) {
32 	return spr->extent.w + spr->padding.left + spr->padding.right;
33 }
34 
sprite_padded_height(const Sprite * restrict spr)35 INLINE float sprite_padded_height(const Sprite *restrict spr) {
36 	return spr->extent.h + spr->padding.top + spr->padding.bottom;
37 }
38 
sprite_padded_extent(const Sprite * restrict spr)39 INLINE FloatExtent sprite_padded_extent(const Sprite *restrict spr) {
40 	FloatExtent e;
41 	e.w = sprite_padded_width(spr);
42 	e.h = sprite_padded_height(spr);
43 	return e;
44 }
45 
sprite_padded_offset_x(const Sprite * restrict spr)46 INLINE float sprite_padded_offset_x(const Sprite *restrict spr) {
47 	return (spr->padding.left - spr->padding.right) * 0.5;
48 }
49 
sprite_padded_offset_y(const Sprite * restrict spr)50 INLINE float sprite_padded_offset_y(const Sprite *restrict spr) {
51 	return (spr->padding.top - spr->padding.bottom) * 0.5;
52 }
53 
sprite_padded_offset(const Sprite * restrict spr)54 INLINE FloatOffset sprite_padded_offset(const Sprite *restrict spr) {
55 	FloatOffset o;
56 	o.x = sprite_padded_offset_x(spr);
57 	o.y = sprite_padded_offset_y(spr);
58 	return o;
59 }
60 
61 char *sprite_path(const char *name);
62 void *load_sprite_begin(const char *path, uint flags);
63 void *load_sprite_end(void *opaque, const char *path, uint flags);
64 bool check_sprite_path(const char *path);
65 
66 void draw_sprite(float x, float y, const char *name);
67 void draw_sprite_p(float x, float y, Sprite *spr);
68 
get_sprite(const char * name)69 INLINE Sprite *get_sprite(const char *name) {
70 	return get_resource_data(RES_SPRITE, name, RESF_DEFAULT | RESF_UNSAFE);
71 }
72 
73 Sprite *prefix_get_sprite(const char *name, const char *prefix);
74 
75 extern ResourceHandler sprite_res_handler;
76 
77 #define SPRITE_PATH_PREFIX "res/gfx/"
78 #define SPRITE_EXTENSION ".spr"
79 
80 #endif // IGUARD_resource_sprite_h
81