--- /dev/null
+/*
+blender for the Gameboy Advance
+Copyright (C) 2021 John Tsiombikas <nuclear@member.fsf.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+#include <string.h>
+#include "gbaregs.h"
+#include "sprites.h"
+#include "debug.h"
+
+extern unsigned char sprites_pixels[];
+extern struct { unsigned char r, g, b; } sprites_cmap[];
+
+void init_sprites(void)
+{
+ int i, j;
+ uint16_t *cptr;
+ uint16_t *dst, *src;
+
+ /* copy from cartridge to OBJ RAM */
+ dst = (uint16_t*)VRAM_LFB_OBJ_ADDR;
+ src = (uint16_t*)sprites_pixels;
+ for(i=0; i<256; i++) { /* 256 tiles */
+ for(j=0; j<8; j++) {
+ *dst++ = src[j * 64];
+ *dst++ = src[j * 64 + 1];
+ }
+
+ src += 2;
+
+ if((i & 31) == 31) {
+ src += 7 * 128; /* skip to the next row of tiles */
+ }
+ }
+
+
+ /* setup OBJ colormap 0 */
+ cptr = (uint16_t*)CRAM_OBJ_ADDR;
+ for(i=0; i<16; i++) {
+ unsigned char r = sprites_cmap[i].r >> 3;
+ unsigned char g = sprites_cmap[i].g >> 3;
+ unsigned char b = sprites_cmap[i].b >> 3;
+ *cptr++ = r | (g << 5) | (b << 10);
+ }
+}
+
+void set_sprite(uint16_t *oam, int idx, int spr, int x, int y, int pal, unsigned int flags)
+{
+ if(!oam) oam = (uint16_t*)OAM_ADDR;
+
+ oam += idx << 2;
+
+ oam[0] = (y & 0xff) | (flags & 0xff00);
+ oam[1] = (x & 0x1ff) | ((flags >> 8) & 0xfe00);
+ oam[2] = (spr & 0x3ff) | ((flags & 3) << 10) | ((pal & 0xf) << 12);
+}