comparison libmlk-core/mlk/core/sprite.c @ 431:8f59201dc76b

core: cleanup hierarchy
author David Demelier <markand@malikania.fr>
date Sat, 15 Oct 2022 20:23:14 +0200
parents src/libmlk-core/core/sprite.c@460c78706989
children 773a082f0b91
comparison
equal deleted inserted replaced
430:1645433e008d 431:8f59201dc76b
1 /*
2 * sprite.c -- image sprites
3 *
4 * Copyright (c) 2020-2022 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 "sprite.h"
22 #include "texture.h"
23
24 void
25 sprite_init(struct sprite *sprite,
26 struct texture *tex,
27 unsigned int cellw,
28 unsigned int cellh)
29 {
30 assert(sprite);
31 assert(tex && texture_ok(tex));
32
33 sprite->texture = tex;
34 sprite->cellw = cellw;
35 sprite->cellh = cellh;
36 sprite->nrows = tex->h / cellh;
37 sprite->ncols = tex->w / cellw;
38 }
39
40 int
41 sprite_ok(const struct sprite *sprite)
42 {
43 return sprite && texture_ok(sprite->texture) && sprite->cellw != 0 && sprite->cellh != 0;
44 }
45
46 int
47 sprite_draw(const struct sprite *sprite, unsigned int r, unsigned int c, int x, int y)
48 {
49 return sprite_scale(sprite, r, c, x, y, sprite->cellw, sprite->cellh);
50 }
51
52 int
53 sprite_scale(const struct sprite *sprite,
54 unsigned int r,
55 unsigned int c,
56 int x,
57 int y,
58 unsigned int w,
59 unsigned int h)
60 {
61 assert(sprite_ok(sprite));
62 assert(r < sprite->nrows);
63 assert(c < sprite->ncols);
64
65 return texture_scale(
66 sprite->texture,
67 c * sprite->cellw, /* src y */
68 r * sprite->cellh, /* src x */
69 sprite->cellw, /* src width */
70 sprite->cellh, /* src height */
71 x, /* dst x */
72 y, /* dst y */
73 w, /* dst width */
74 h, /* dst height */
75 0.0 /* angle */
76 );
77 }