1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-05-28 08:41:30 +00:00
8bitworkshop/presets/c64/joymove.c
2022-08-09 13:28:55 -05:00

72 lines
2.0 KiB
C

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <peekpoke.h>
#include <string.h>
#include <c64.h>
#include <cbm_petscii_charmap.h>
#include <joystick.h>
/*{w:24,h:21,bpp:1,brev:1,count:1}*/
const char SPRITE_DATA[64] = {
0x0f,0xff,0x80,0x17,0xff,0x40,0x2b,0xfe,
0xa0,0x7f,0xff,0xf0,0xff,0xc0,0x3f,0xe0,
0x3f,0xc0,0x17,0xbe,0xc0,0x2d,0x7f,0xf0,
0x2b,0x7f,0xf8,0x2a,0xff,0xf8,0x15,0xf6,
0x00,0x3f,0xf8,0x00,0xfd,0xfc,0x00,0xfd,
0xff,0x00,0xfe,0xff,0x80,0xff,0x7f,0xc0,
0xff,0xff,0xc0,0xff,0xff,0xc0,0x0a,0xa8,
0x00,0x0f,0xf8,0x00,0x0f,0xff,0x80,0x03,
};
/*{w:12,h:21,bpp:2,brev:1,count:1,aspect:2}*/
const char SPRITE_MC_DATA[64] = {
0x0a,0xaa,0x80,0x0a,0xaa,0x80,0x2a,0xaa,
0xa0,0x2a,0xaa,0xa0,0xaa,0xaa,0xaa,0xff,
0xd5,0x40,0x0d,0xd7,0x40,0x3d,0xd5,0x54,
0x37,0x55,0x54,0x37,0x55,0x54,0x35,0x55,
0x00,0x3a,0xa0,0x00,0xea,0xa8,0x00,0xab,
0xaa,0x00,0xab,0xaa,0x00,0xab,0xaa,0x80,
0xaa,0xea,0x80,0xaa,0xaa,0x80,0x0f,0xfc,
0x00,0x0f,0xfc,0x00,0x0f,0xff,0xc0,0x83,
};
void main(void) {
// variables
int x = 172; // sprite X position (16-bit)
char y = 145; // sprite Y position (8-bit)
char bgcoll; // sprite background collision flags
char joy; // joystick flags
// install the joystick driver
joy_install (joy_static_stddrv);
// copy sprite pattern to RAM address 0x3800
memcpy((char*)0x3800, SPRITE_DATA, sizeof(SPRITE_DATA));
// set sprite #0 shape entry (224)
POKE(0x400 + 0x3f8, 0x3800 / 64);
// enable sprite #0
VIC.spr_ena = 0b00000001;
// loop forever
while (1) {
// get joystick bits
joy = joy_read(0);
// move sprite based on joystick
if (JOY_LEFT(joy)) --x;
if (JOY_UP(joy)) --y;
if (JOY_RIGHT(joy)) ++x;
if (JOY_DOWN(joy)) ++y;
// set sprite registers based on position
VIC.spr0_x = x;
VIC.spr0_y = y;
VIC.spr_hi_x = (x & 256) ? 1 : 0; // set X hi bit?
// grab and reset collision flags
bgcoll = VIC.spr_bg_coll;
// change color when we collide with background
VIC.spr0_color = (bgcoll & 1) ? 10 : 3;
// wait for end of frame
waitvsync();
}
}