Skip to content

Instantly share code, notes, and snippets.

@mrk-its
Created January 28, 2022 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrk-its/d167829bce497cd31574c50087337d8c to your computer and use it in GitHub Desktop.
Save mrk-its/d167829bce497cd31574c50087337d8c to your computer and use it in GitHub Desktop.
Sprites
#include <string.h>
extern void init_sprites();
extern void multi_show_sprites();
extern void multi_animuj();
extern char shp0_01, shp0_23;
extern char shp1_01, shp1_23;
extern char shp2_01, shp2_23;
extern char shp3_01, shp3_23;
extern char shp4_01, shp4_23;
extern char shp5_01, shp5_23;
extern char shp6_01, shp6_23;
extern char shp7_01, shp7_23;
extern unsigned char sprite_c0[16];
extern unsigned char sprite_c1[16];
extern unsigned char sprite_shape[16];
extern unsigned char sprite_x[16];
extern unsigned char sprite_y[16];
extern unsigned char sprite_anim_speed[16];
extern unsigned char sprite_anim[16];
unsigned char *_charsets = (unsigned char *)0x3e0;
char **_shape_tab01 = (char **)0x100;
char **_shape_tab23 = (char **)0x200;
volatile unsigned char *SDMCTL = (volatile unsigned char *)0x22f;
volatile char **SDLPTR = (volatile char **)0x230;
volatile char **DLPTRS = (volatile char **)0xd402;
volatile unsigned char *DMACTL = (volatile unsigned char *)0xd400;
volatile unsigned char *CHBASE = (volatile unsigned char *)0xd409;
volatile unsigned char *RTCLK = (volatile unsigned char *) 20;
/*
.macro addShape (idx, nam)
mwa #%%nam._01 shape_tab01+%%idx*2
mwa #%%nam._23 shape_tab23+%%idx*2
.endm
.macro addSprite (num, c0, c1, shape, x, y, speed, anim)
mva #%%c0 sprite_c0+%%num
mva #%%c1 sprite_c1+%%num
mva #%%shape sprite_shape+%%num ;ksztalt startowy
mva #%%x sprite_x+%%num
mva #%%y sprite_y+%%num
mva #%%speed sprite_anim_speed+%%num ;predko�� animacji
mva #%%anim sprite_anim+%%num ;maska dla liczby klatek animacji
.endm
*/
static __attribute__((aligned(1024))) char dlist[] = {
0xF0, 0xc2, 0x40, 0xbc,
0x82, 0x82, 0x82, 0x82, 0x82,
0x82, 0x82, 0x82, 0x82, 0x82,
0x82, 0x82, 0x82, 0x82, 0x82,
0x82, 0x82, 0x82, 0x82, 0x82,
0x82, 0x82, 0x82, 0x82, 0x82,
0x41, 0x00, 0x36
};
void add_shape(int n, char *shp_01, char *shp_23) {
_shape_tab01[n] = shp_01;
_shape_tab23[n] = shp_23;
}
void add_sprite(int n, unsigned char color1, unsigned char color2, unsigned char shape, unsigned char x, unsigned char y, unsigned char anim_speed, unsigned char anim_mask) {
sprite_c0[n] = color1;
sprite_c1[n] = color2;
sprite_shape[n] = shape;
sprite_x[n] = x;
sprite_y[n] = y;
sprite_anim_speed[n] = anim_speed;
sprite_anim[n] = anim_mask;
}
void wait_vbl() {
unsigned char c = *RTCLK;
while(*RTCLK == c);
}
// [int(math.sin(2 * math.pi * i / 256) * 128) for i in range(256)]
static unsigned char SIN_TABLE[256] = {
0x00, 0x03, 0x06, 0x09, 0x0c, 0x0f, 0x12, 0x15, 0x18, 0x1b, 0x1e, 0x21, 0x24, 0x27, 0x2a, 0x2d,
0x30, 0x33, 0x36, 0x39, 0x3b, 0x3e, 0x41, 0x43, 0x46, 0x49, 0x4b, 0x4e, 0x50, 0x52, 0x55, 0x57,
0x59, 0x5b, 0x5e, 0x60, 0x62, 0x64, 0x66, 0x67, 0x69, 0x6b, 0x6c, 0x6e, 0x70, 0x71, 0x72, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7b, 0x7c, 0x7d, 0x7d, 0x7e, 0x7e, 0x7e, 0x7e, 0x7e,
0x7f, 0x7e, 0x7e, 0x7e, 0x7e, 0x7e, 0x7d, 0x7d, 0x7c, 0x7b, 0x7b, 0x7a, 0x79, 0x78, 0x77, 0x76,
0x75, 0x74, 0x72, 0x71, 0x70, 0x6e, 0x6c, 0x6b, 0x69, 0x67, 0x66, 0x64, 0x62, 0x60, 0x5e, 0x5b,
0x59, 0x57, 0x55, 0x52, 0x50, 0x4e, 0x4b, 0x49, 0x46, 0x43, 0x41, 0x3e, 0x3b, 0x39, 0x36, 0x33,
0x30, 0x2d, 0x2a, 0x27, 0x24, 0x21, 0x1e, 0x1b, 0x18, 0x15, 0x12, 0x0f, 0x0c, 0x09, 0x06, 0x03,
0x00, 0xfd, 0xfa, 0xf7, 0xf4, 0xf1, 0xee, 0xeb, 0xe8, 0xe5, 0xe2, 0xdf, 0xdc, 0xd9, 0xd6, 0xd3,
0xd0, 0xcd, 0xca, 0xc7, 0xc5, 0xc2, 0xbf, 0xbd, 0xba, 0xb7, 0xb5, 0xb2, 0xb0, 0xae, 0xab, 0xa9,
0xa7, 0xa5, 0xa2, 0xa0, 0x9e, 0x9c, 0x9a, 0x99, 0x97, 0x95, 0x94, 0x92, 0x90, 0x8f, 0x8e, 0x8c,
0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86, 0x85, 0x85, 0x84, 0x83, 0x83, 0x82, 0x82, 0x82, 0x82, 0x82,
0x81, 0x82, 0x82, 0x82, 0x82, 0x82, 0x83, 0x83, 0x84, 0x85, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a,
0x8b, 0x8c, 0x8e, 0x8f, 0x90, 0x92, 0x94, 0x95, 0x97, 0x99, 0x9a, 0x9c, 0x9e, 0xa0, 0xa2, 0xa5,
0xa7, 0xa9, 0xab, 0xae, 0xb0, 0xb2, 0xb5, 0xb7, 0xba, 0xbd, 0xbf, 0xc2, 0xc5, 0xc7, 0xca, 0xcd,
0xd0, 0xd3, 0xd6, 0xd9, 0xdc, 0xdf, 0xe2, 0xe5, 0xe8, 0xeb, 0xee, 0xf1, 0xf4, 0xf7, 0xfa, 0xfd,
};
signed char sin(unsigned char alpha) {
return SIN_TABLE[alpha];
}
int main() {
*(char **)&dlist[sizeof(dlist)-2] = dlist;
*SDLPTR = dlist;
wait_vbl();
// *CHBASE = 0xe0;
for(int i=0; i<32; i++) _charsets[i] = 0xe0;
init_sprites();
*DMACTL = 0x3d;
*SDMCTL = 0x3d;
add_shape(0, &shp0_01, &shp0_23);
add_shape(1, &shp1_01, &shp1_23);
add_shape(2, &shp2_01, &shp2_23);
add_shape(3, &shp3_01, &shp3_23);
add_shape(4, &shp4_01, &shp4_23);
add_shape(5, &shp5_01, &shp5_23);
add_shape(6, &shp6_01, &shp6_23);
add_shape(7, &shp7_01, &shp7_23);
for(int i=0; i<8; i++) {
add_sprite(i, i * 2 * 16 + 8, (i * 2 + 1) * 16 + 4, i % 8, 0, 0, 1, 0xff);
}
memset((void *)0xe000, 0, 1024);
unsigned int alpha = 0;
while(1) {
wait_vbl();
multi_show_sprites();
// multi_animuj();
sprite_x[0] += 1;
for(int i=0; i<8; i++) {
sprite_x[i] = 128 - 4 + sin(alpha + i * 20) / 2;
sprite_y[i] = 112 + sin(70 + alpha + i * 20) * 3 / 4;
}
alpha = alpha - 1;
// sprite_y += 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment