summaryrefslogtreecommitdiff
path: root/src/spman
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2020-12-30 19:07:31 +0000
committerJuan J. Martinez <jjm@usebox.net>2020-12-30 19:23:41 +0000
commit2682bc5d1d864341aaeb42a449db73c3ecd16d70 (patch)
tree9116764364b4ee0ce7f6037305077807b57776de /src/spman
downloadubox-msx-lib-ca9b663c147340e92804979a96eee4113ab0b27f.tar.gz
ubox-msx-lib-ca9b663c147340e92804979a96eee4113ab0b27f.zip
Initial import1.0
Diffstat (limited to 'src/spman')
-rw-r--r--src/spman/Makefile20
-rw-r--r--src/spman/spman.c118
2 files changed, 138 insertions, 0 deletions
diff --git a/src/spman/Makefile b/src/spman/Makefile
new file mode 100644
index 0000000..b2bc5df
--- /dev/null
+++ b/src/spman/Makefile
@@ -0,0 +1,20 @@
+LIB=../../lib/spman.lib
+all: $(LIB)
+
+CC=sdcc
+AR=sdar
+CFLAGS=-mz80 --Werror -I../../include --fsigned-char --std-sdcc99 --opt-code-speed
+
+SOURCES=$(wildcard *.c)
+OBJS=$(patsubst %.c,%.rel,$(SOURCES))
+
+$(LIB): $(OBJS)
+ $(AR) -rcD $(LIB) $(OBJS)
+
+%.rel: %.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -c $<
+
+.PHONY: clean
+clean:
+ rm -f $(OBJS) $(LIB)
+
diff --git a/src/spman/spman.c b/src/spman/spman.c
new file mode 100644
index 0000000..0c7d7d1
--- /dev/null
+++ b/src/spman/spman.c
@@ -0,0 +1,118 @@
+#include <stdint.h>
+#include <string.h>
+
+#include "spman.h"
+
+#define SPMAN_PAT_UNUSED 0xff
+#define SPMAN_MAX_SPRITES 32
+#define SPMAN_MAX_PATTERNS 64
+
+uint8_t sp_last_sprite, sp_last_fixed_sprite, sp_idx;
+struct sprite_attr sp_fixed[SPMAN_MAX_SPRITES];
+struct sprite_attr sp_buffer[SPMAN_MAX_SPRITES * 2];
+
+uint8_t sp_pat_map[SPMAN_MAX_PATTERNS];
+uint8_t sp_last_pat;
+
+void spman_init()
+{
+ sp_last_pat = 0;
+ memset(sp_pat_map, SPMAN_PAT_UNUSED, SPMAN_MAX_PATTERNS);
+ spman_sprite_flush();
+}
+
+uint8_t spman_alloc_pat(uint8_t type, uint8_t *data, uint8_t len, uint8_t flip)
+{
+ uint8_t i;
+
+ if (sp_pat_map[type] == SPMAN_PAT_UNUSED)
+ {
+ sp_pat_map[type] = sp_last_pat;
+ for (i = 0; i < len; ++i)
+ {
+ if (flip)
+ ubox_set_sprite_pat16_flip(data, sp_last_pat);
+ else
+ ubox_set_sprite_pat16(data, sp_last_pat);
+
+ data += 32;
+ sp_last_pat++;
+ }
+#ifdef SPMAN_DEBUG
+ if (sp_last_pat > 63)
+ ubox_set_colors(15, 0, 0);
+#endif
+ }
+
+ return sp_pat_map[type] * 4;
+}
+
+void spman_sprite_flush()
+{
+ sp_last_fixed_sprite = 0;
+ sp_last_sprite = 0;
+}
+
+void spman_alloc_fixed_sprite(struct sprite_attr *sp)
+{
+#ifdef SPMAN_DEBUG
+ if (sp_last_fixed_sprite + sp_last_sprite > 30)
+ {
+ ubox_set_colors(15, 0, 0);
+ return;
+ }
+#endif
+ memcpy(&sp_fixed[sp_last_fixed_sprite++], sp, 4);
+}
+
+void spman_alloc_sprite(struct sprite_attr *sp)
+{
+#ifdef SPMAN_DEBUG
+ if (sp_last_fixed_sprite + sp_last_sprite > 30)
+ {
+ ubox_set_colors(15, 0, 0);
+ return;
+ }
+#endif
+ memcpy(&sp_buffer[sp_last_sprite++], sp, 4);
+}
+
+static const struct sprite_attr hide = { 208, 0, 0, 0 };
+
+static uint8_t *p;
+
+void spman_update()
+{
+ p = (uint8_t*) 0x1b00;
+
+ if (sp_last_sprite)
+ {
+ memcpy(&sp_buffer[sp_last_sprite], sp_buffer, sp_last_sprite * 4);
+
+ if (sp_last_sprite > 2)
+ sp_idx += 2;
+
+ if (sp_idx >= sp_last_sprite)
+ sp_idx -= sp_last_sprite;
+ }
+ else
+ sp_idx = 0;
+
+ memcpy(&sp_buffer[sp_idx + sp_last_sprite], &hide, 4);
+
+ ubox_wait_vsync();
+ if (sp_last_fixed_sprite)
+ {
+ ubox_write_vm(p, sp_last_fixed_sprite * 4, (uint8_t *)sp_fixed);
+ p += sp_last_fixed_sprite * 4;
+ }
+ ubox_write_vm(p, 4 + sp_last_sprite * 4, (uint8_t *)&sp_buffer[sp_idx]);
+
+ spman_sprite_flush();
+}
+
+void spman_hide_all_sprites()
+{
+ ubox_wait_vsync();
+ ubox_write_vm((uint8_t *)0x1b00, 4, (uint8_t *)hide);
+}