Last active
April 11, 2023 00:33
-
-
Save JettMonstersGoBoom/1dd7bad1d2c21dd15f58baf4ea5b8d9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile with OSCAR64 | |
// | |
// oscar64.exe -tm=nes -n boot.c -O2 -o=bin\boot.nes | |
// https://github.com/drmortalwombat/oscar64 | |
#include <nes/nes.h> | |
#include <nes/neslib.h> | |
extern char PAL_BUF[32]; | |
extern char PAL_UPDATE; | |
#pragma section( tiles, 0 ) | |
#pragma region( tbank, 0x0000, 0x2000, , 0, { tiles } ) | |
#pragma data(tiles) | |
__export char tiles[] = { | |
#embed "data/rogue.chr" | |
}; | |
#pragma data(data) | |
const char PaletteData[] = { | |
#embed "data/rogue_bg.pal" | |
}; | |
#pragma data(data) | |
const char SprPaletteData[4][16] = { | |
{ | |
// blue player palette | |
#embed "data/rogue_spr.pal" | |
} | |
, | |
{ | |
// red player palette | |
#embed "data/rogue_spr2.pal" | |
} | |
}; | |
#pragma data(data) | |
const char NamData[] = { | |
#embed "data/rogue.nam" | |
}; | |
#pragma data(data) | |
const char PropData[] = { | |
#embed "data/rogue.tprop" | |
}; | |
typedef struct | |
{ | |
char x,y; | |
char direction; | |
char frame; | |
} _ent_t_; | |
__striped __zeropage _ent_t_ ents[2]; | |
__zeropage char ticker; | |
__zeropage unsigned char controller; | |
__zeropage unsigned char xx,yy,sprid; | |
__zeropage char *c; | |
__zeropage char animspeed,displayframe; | |
__zeropage unsigned char overchar; | |
__zeropage char lastX; | |
__zeropage char lastY; | |
// y step lookup table | |
__striped unsigned char *ycollision[30]; | |
// pass the pointer to the nametable | |
void generateCollisionLookup(unsigned char *ptr) | |
{ | |
for (char y=0;y<30;y++) | |
{ | |
ycollision[y]=ptr; | |
ptr+=32; | |
} | |
} | |
// get the tile from the map via the ycollision lookup table | |
inline unsigned char getMapChar(char p) | |
{ | |
xx = (ents[p].x+4)>>3; | |
yy = (ents[p].y+6)>>3; | |
c = ycollision[yy]; | |
return (c[xx]); | |
} | |
// get the property | |
unsigned char getPropertyChar(char p) | |
{ | |
return (PropData[getMapChar(p)]); | |
} | |
// set partial palette | |
void SetSpritePalette(char spr,char *pal) | |
{ | |
spr<<=2; | |
PAL_BUF[spr + 17] = pal[1]; | |
PAL_BUF[spr + 18] = pal[2]; | |
PAL_BUF[spr + 19] = pal[3]; | |
PAL_UPDATE++; | |
} | |
// player input and control | |
void Player(char p) | |
{ | |
lastX = ents[p].x; | |
lastY = ents[p].y; | |
controller = pad_poll(p); | |
animspeed = 0; | |
if(controller & PAD_LEFT) | |
{ | |
ents[p].direction=3; | |
ents[p].x-=1; | |
animspeed= 1; | |
} | |
if(controller & PAD_RIGHT) | |
{ | |
ents[p].direction=2; | |
ents[p].x+=1; | |
animspeed = 1; | |
} | |
// check for blocking char X movement | |
overchar = getPropertyChar(p); | |
if ((overchar&0x80)!=0) | |
{ | |
animspeed=0; | |
ents[p].x=lastX; | |
} | |
if(controller & PAD_UP) | |
{ | |
ents[p].direction=1; | |
ents[p].y-=1; | |
animspeed = 1; | |
} | |
if(controller & PAD_DOWN) | |
{ | |
ents[p].direction=0; | |
ents[p].y+=1; | |
animspeed = 1; | |
} | |
// check for blocking char Y movement | |
overchar = getPropertyChar(p); | |
if ((overchar&0x80)!=0) | |
{ | |
animspeed=0; | |
ents[p].y=lastY; | |
} | |
ents[p].frame+=animspeed; | |
overchar = getPropertyChar(p)&3; | |
SetSpritePalette(p,(char*)&SprPaletteData[p][overchar<<2]); | |
displayframe = (ents[p].direction<<4) + 1 + ((ents[p].frame>>2)&7); | |
sprid=oam_spr(ents[p].x,ents[p].y,displayframe,p,sprid); | |
} | |
void nes_game(void) | |
{ | |
ppu_wait_nmi(); | |
ppu_off(); | |
pal_bg(PaletteData); | |
oam_clear(); | |
generateCollisionLookup((unsigned char*)&NamData[0]); | |
// copy screen map, lazy | |
vram_adr(0x2000); | |
for(char i=0; i<32; i++) | |
{ | |
ppu_wait_nmi(); | |
vram_write(NamData + 32 * i, 32); | |
} | |
ticker =0; | |
// set up | |
ents[0].x = 48; | |
ents[0].y = 64; | |
ents[0].frame = 0; | |
ents[0].direction = 0; | |
ents[1].x = 192; | |
ents[1].y = 32; | |
ents[1].frame = 0; | |
ents[1].direction = 0; | |
ppu_on_all(); | |
ppu.mask=0x1f; | |
for(;;) | |
{ | |
ppu_wait_nmi(); | |
sprid = (ticker&1)<<2; | |
Player(ticker&1); | |
sprid=8; // 8 bytes on after the two player sprites | |
ticker++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment