fixed 8bit writes to vmem, unaligned data buffers, and started on
[gba_blender] / src / sprites.c
1 /*
2 blender for the Gameboy Advance
3 Copyright (C) 2021  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <string.h>
19 #include "gbaregs.h"
20 #include "sprites.h"
21 #include "debug.h"
22
23 extern unsigned char sprites_pixels[];
24 extern struct { unsigned char r, g, b; } sprites_cmap[];
25
26 void init_sprites(void)
27 {
28         int i, j;
29         uint16_t *cptr;
30         uint16_t *dst, *src;
31
32         /* copy from cartridge to OBJ RAM */
33         dst = (uint16_t*)VRAM_LFB_OBJ_ADDR;
34         src = (uint16_t*)sprites_pixels;
35         for(i=0; i<256; i++) {  /* 256 tiles */
36                 for(j=0; j<8; j++) {
37                         *dst++ = src[j * 64];
38                         *dst++ = src[j * 64 + 1];
39                 }
40
41                 src += 2;
42
43                 if((i & 31) == 31) {
44                         src += 7 * 128; /* skip to the next row of tiles */
45                 }
46         }
47
48
49         /* setup OBJ colormap 0 */
50         cptr = (uint16_t*)CRAM_OBJ_ADDR;
51         for(i=0; i<16; i++) {
52                 unsigned char r = sprites_cmap[i].r >> 3;
53                 unsigned char g = sprites_cmap[i].g >> 3;
54                 unsigned char b = sprites_cmap[i].b >> 3;
55                 *cptr++ = r | (g << 5) | (b << 10);
56         }
57 }
58
59 void set_sprite(uint16_t *oam, int idx, int spr, int x, int y, int pal, unsigned int flags)
60 {
61         if(!oam) oam = (uint16_t*)OAM_ADDR;
62
63         oam += idx << 2;
64
65         oam[0] = (y & 0xff) | (flags & 0xff00);
66         oam[1] = (x & 0x1ff) | ((flags >> 8) & 0xfe00);
67         oam[2] = (spr & 0x3ff) | ((flags & 3) << 10) | ((pal & 0xf) << 12);
68 }