comparison src/core/sprite.c @ 59:52792b863ff7

misc: separate core from game
author David Demelier <markand@malikania.fr>
date Tue, 21 Jan 2020 12:42:33 +0100
parents src/sprite.c@b815621df3e3
children ed72843a7194
comparison
equal deleted inserted replaced
58:d7d88ac30611 59:52792b863ff7
1 /*
2 * sprite.c -- image sprites
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 "sprite.h"
22 #include "texture_p.h"
23 #include "texture.h"
24
25 void
26 sprite_init(struct sprite *sprite,
27 struct texture *tex,
28 unsigned int cellw,
29 unsigned int cellh)
30 {
31 int w = 0;
32 int h = 0;
33
34 assert(sprite);
35 assert(tex);
36
37 /* TODO: use texture_get_size */
38 SDL_QueryTexture(tex->handle, NULL, NULL, &w, &h);
39
40 sprite->texture = tex;
41 sprite->cellw = cellw;
42 sprite->cellh = cellh;
43 sprite->nrows = h / cellh;
44 sprite->ncols = w / cellw;
45 }
46
47 void
48 sprite_draw(struct sprite *sprite, unsigned int r, unsigned int c, int x, int y)
49 {
50 assert(sprite);
51
52 texture_draw_ex(
53 sprite->texture,
54 c * sprite->cellw, /* src y */
55 r * sprite->cellh, /* src x */
56 sprite->cellw, /* src width */
57 sprite->cellh, /* src height */
58 x, /* dst x */
59 y, /* dst y */
60 sprite->cellw, /* dst width */
61 sprite->cellh, /* dst height */
62 0.0 /* angle */
63 );
64 }