1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-05-28 08:41:30 +00:00
8bitworkshop/presets/c64/sprite_collision.c
2022-07-26 20:21:16 -05:00

153 lines
3.4 KiB
C

#include <stdio.h>
#include <conio.h>
#include <c64.h>
#include <cbm_petscii_charmap.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "common.h"
//#link "common.c"
#include "sprites.h"
//#link "sprites.c"
/*{w:12,h:21,bpp:2,brev:1}*/
const char SPRITEMC[3*21] = {
0x00,0xAA,0x80,0x02,0xAA,0xA0,0x0A,0xAA,0xA8,
0x0A,0xAE,0xA8,0x0A,0xBB,0xA8,0x0A,0xBA,0xA8,
0x0A,0xBB,0xA8,0x0A,0xAE,0xA8,0x0A,0xAA,0xA8,
0x09,0xAA,0x98,0x08,0x6A,0x48,0x08,0x1D,0x08,
0x02,0x0C,0x20,0x02,0x0C,0x20,0x02,0x0C,0x20,
0x00,0x8C,0x80,0x00,0x8C,0x80,0x00,0x55,0x40,
0x00,0x77,0x40,0x00,0x5D,0x40,0x00,0x15,0x00
};
int xpos[8];
int ypos[8];
int xvel[8];
int yvel[8];
void init_sprites(void) {
byte i;
// setup sprite positions
for (i=0; i<8; i++) {
xpos[i] = ((i & 3) * 0x2000) - 0x1000;
ypos[i] = (i * 0x1000) - 0x3000;
sprshad.spr_color[i] = i | 8;
}
}
void move_sprites(void) {
byte i;
for (i=0; i<8; i++) {
//VIC.bordercolor = i;
sprite_draw(i, (xpos[i]>>7)+0x80, (ypos[i]>>8)+0x80, 255);
// update position
xpos[i] += xvel[i];
ypos[i] += yvel[i];
}
}
void update_sprites(void) {
byte i;
for (i=0; i<8; i++) {
// update velocity
xvel[i] -= xpos[i] >> 12;
yvel[i] -= ypos[i] >> 12;
}
}
void collide_sprites(byte spr_coll) {
byte i;
byte mask = 1;
// exit if no collisions
if (!spr_coll) return;
// iterate all sprites that have their flag set
for (i=0; i<8; i++, mask<<=1) {
//VIC.bordercolor = i;
if (spr_coll & mask) {
// find the first sprite that intersects
byte j = sprite_get_closest_collision(i, spr_coll);
// returns 0..7 if a sprite was found
if (j < 8) {
xvel[i] = (xpos[i] - xpos[j]) >> 4;
yvel[i] = (ypos[i] - ypos[j]) >> 4;
}
}
}
}
void iterategame1(void) {
byte spr_coll;
// wait for vblank
wait_vblank();
// grab and reset sprite-sprite collision flags
spr_coll = VIC.spr_coll;
// then update sprite registers from shadow RAM
sprite_update(DEFAULT_SCREEN);
// draw sprites into shadow ram
move_sprites();
// and update velocity and position
update_sprites();
// if any flags are set in the collision register,
// process sprite collisions
collide_sprites(spr_coll);
}
void iterategame2(void) {
byte spr_coll;
// FIRST FRAME: move and update velocity
wait_vblank();
// grab and reset sprite-sprite collision flags
spr_coll = VIC.spr_coll;
// then update sprite registers from shadow RAM
sprite_update(DEFAULT_SCREEN);
// draw sprites into shadow ram
// and update posiitons
move_sprites();
// and update velocities
update_sprites();
// SECOND FRAME: move and process collisions
wait_vblank();
// grab and reset sprite-sprite collision flags
// combine with previous frame flags
spr_coll |= VIC.spr_coll;
// then update sprite registers from shadow RAM
sprite_update(DEFAULT_SCREEN);
// draw sprites into shadow ram
// and update posiitons
move_sprites();
// if any flags are set in the collision register,
// process sprite collisions
collide_sprites(spr_coll);
}
void main(void) {
clrscr();
VIC.bordercolor = 0;
// setup sprite library and copy sprite to VIC bank
sprite_clear();
sprite_shape((void*)0x0, 255, SPRITEMC);
// set colors
sprshad.spr_mcolor = 0xff;
VIC.spr_mcolor0 = 4;
VIC.spr_mcolor1 = 7;
// set sprite initial positions
init_sprites();
// game loop
while (1) {
iterategame2();
}
}