Skip to content

Instantly share code, notes, and snippets.

@keyboardspecialist
Created August 24, 2013 00:50
Show Gist options
  • Save keyboardspecialist/6325328 to your computer and use it in GitHub Desktop.
Save keyboardspecialist/6325328 to your computer and use it in GitHub Desktop.
#include "neslib.h"
#define ORIGIN_X 128
#define ORIGIN_Y 120
#define MAX_DEPTH 32
#define NT_ADR1 0x2000
#define NT_ADR2 0x2400
#define NT_ADR3 0x2800
#define NT_ADR4 0x2C00
#define NT_END 0x3C0
#define MAX_STARS 32
#define FP_BITS 4
#define NTADR(x,y) ((0x2800|((y)<<5)|x))
extern const unsigned char music_main[];
static unsigned char wait = 0;
static unsigned char star_x[MAX_STARS];
static unsigned char star_y[MAX_STARS];
static unsigned char star_z[MAX_STARS];
static unsigned char star_atr[MAX_STARS];
static unsigned char kval[MAX_DEPTH+1];
static unsigned char _frame = 0;
const unsigned char sine_wave[256] = {
0x80, 0x83, 0x86, 0x89, 0x8C, 0x90, 0x93, 0x96,
0x99, 0x9C, 0x9F, 0xA2, 0xA5, 0xA8, 0xAB, 0xAE,
0xB1, 0xB3, 0xB6, 0xB9, 0xBC, 0xBF, 0xC1, 0xC4,
0xC7, 0xC9, 0xCC, 0xCE, 0xD1, 0xD3, 0xD5, 0xD8,
0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8,
0xEA, 0xEB, 0xED, 0xEF, 0xF0, 0xF1, 0xF3, 0xF4,
0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC,
0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFD,
0xFD, 0xFC, 0xFB, 0xFA, 0xFA, 0xF9, 0xF8, 0xF6,
0xF5, 0xF4, 0xF3, 0xF1, 0xF0, 0xEF, 0xED, 0xEB,
0xEA, 0xE8, 0xE6, 0xE4, 0xE2, 0xE0, 0xDE, 0xDC,
0xDA, 0xD8, 0xD5, 0xD3, 0xD1, 0xCE, 0xCC, 0xC9,
0xC7, 0xC4, 0xC1, 0xBF, 0xBC, 0xB9, 0xB6, 0xB3,
0xB1, 0xAE, 0xAB, 0xA8, 0xA5, 0xA2, 0x9F, 0x9C,
0x99, 0x96, 0x93, 0x90, 0x8C, 0x89, 0x86, 0x83,
0x80, 0x7D, 0x7A, 0x77, 0x74, 0x70, 0x6D, 0x6A,
0x67, 0x64, 0x61, 0x5E, 0x5B, 0x58, 0x55, 0x52,
0x4F, 0x4D, 0x4A, 0x47, 0x44, 0x41, 0x3F, 0x3C,
0x39, 0x37, 0x34, 0x32, 0x2F, 0x2D, 0x2B, 0x28,
0x26, 0x24, 0x22, 0x20, 0x1E, 0x1C, 0x1A, 0x18,
0x16, 0x15, 0x13, 0x11, 0x10, 0x0F, 0x0D, 0x0C,
0x0B, 0x0A, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04,
0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03,
0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0A,
0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x11, 0x13, 0x15,
0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24,
0x26, 0x28, 0x2B, 0x2D, 0x2F, 0x32, 0x34, 0x37,
0x39, 0x3C, 0x3F, 0x41, 0x44, 0x47, 0x4A, 0x4D,
0x4F, 0x52, 0x55, 0x58, 0x5B, 0x5E, 0x61, 0x64,
0x67, 0x6A, 0x6D, 0x70, 0x74, 0x77, 0x7A, 0x7D
};
//for bottom logo
const unsigned char bounce_out[60] = {0,8,11,12,19,21,24,26,35,38,42,45,57,60,63,66,69,84,87,84,82,76,75,
72,71,68,67,67,66,66,66,66,66,68,69,70,71,76,77,80,81,87,86,84,84,82,82,82,82,83,83,84,85,87,87,86,86,87,87,88};
//GFX tables
unsigned char LD27[32*10]={
0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,
0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,
0x80,0x81,0x82,0x50,0x50,0x50,0x86,0x87,0x88,0x89,0x8a,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,
0xf0,0x91,0x92,0x93,0x50,0x50,0x96,0x97,0x98,0x99,0x9a,0x9b,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,
0xf0,0xa1,0xa2,0xa3,0xa4,0x50,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,
0xf0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0x50,0xba,0xbb,0xbc,0xbd,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
0xf0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0x50,0xca,0xcb,0xcc,0xcd,0xce,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
0xf0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
0xf0,0xe1,0xe2,0xe2,0xe2,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
0xf0,0xf0,0xf0,0xf0,0xf0,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x50,0x79,0x7a,0x7b,0x7c,0x50,0x7e,0x7f
};
unsigned char str1[32] = " GREETINGS ALL ";
unsigned char str2[32] = " WELCOME TO LUDUM DARE 2013 ";
unsigned char str3[32] = " THE SECOND TIME ";
unsigned char str4[32] = " ";
unsigned char str5[32] = " CODE GFX MUSIC ";
unsigned char str6[32] = " KEYBOARDSPECIALIST ";
unsigned char str7[32] = " WHO WE ARE ";
//unsigned char str4[32] = " ";
unsigned char str8[32] = " KEYBOARDSPECIALIST ";
unsigned char str9[32] = " SQUIRRELS ";
//unsigned char strA[32] = " ";
unsigned char strB[32] = " WHAT WE DO ";
//unsigned char str4[32] = " ";
unsigned char strC[32] = " NINTENDO ENTERTAINMENT SYSTEM ";
unsigned char strD[32] = " FAMITRACKER ";
unsigned char strE[32] = " YYCHR GRAPHICSGALE ";
unsigned char strF[32] = " CC65 COMPILER ";
unsigned char *str_list[8] = {NULL};
const unsigned char sppal[16]={ 0x01,0x12,0x23,0x30,
0x01,0x15,0x25,0x35,
0x01,0x19,0x29,0x39,
0x01,0x1C,0x2C,0x3C };
const unsigned char bgpal[16]={ 0x01,0x31,0x30,0x11,
0x01,0x11,0x21,0x02,
0x01,0x11,0x02,0x01,
0x01,0x01,0x01,0x01 };
#pragma bss-name (push,"ZEROPAGE")
#pragma data-name (push,"ZEROPAGE")
static unsigned char spr_id; //oam sprite offset
static unsigned char pbright;
static unsigned char i, si;
static unsigned char _sx,_x, _y, _t; //utility vars, volatile
static unsigned char scrolly;
static unsigned char iter;
unsigned char
randr(unsigned char min, unsigned char max)
{
return min + (rand8() % (max - min + 1)); //slloooowww
}
void
delay(unsigned char time)
{
for(wait=0;wait<time;++wait) ppu_waitnmi();
}
void
pal_trans_bright(unsigned char lvl)
{
while(pbright != lvl)
{
delay(4);
if(pbright > lvl) --pbright;
else ++pbright;
pal_bright(pbright);
}
if(!pbright)
{
ppu_off();
set_vram_update(0,0);
scroll(0,0);
}
}
void
init_stars(void)
{
set_rand(0xFD);
for(i = 0; i < MAX_STARS; ++i)
{
_x = randr(ORIGIN_X - 5, ORIGIN_X + 5);
_y = randr(ORIGIN_Y - 5, ORIGIN_Y + 5);
star_x[i] = ORIGIN_X - _x;
star_y[i] = ORIGIN_Y - _y;
star_z[i] = randr(1, MAX_DEPTH);
star_atr[i] = randr(0, 4) | OAM_BEHIND;
spr_id = oam_spr(star_x[i], star_y[i], star_z[i]>>3, star_atr[i], spr_id);
}
for(i = 1; i < MAX_DEPTH+1; ++i)
{
kval[i] = 128 / i;
}
}
void
starfield(void)
{
spr_id = 0;
ppu_waitnmi();
for(i = 0; i < MAX_STARS; ++i)
{
--star_z[i];
if(star_z[i] < 1)
{
star_z[i] = MAX_DEPTH;
}
_t = kval[star_z[i]];
_x = star_x[i] * _t + ORIGIN_X ;
_y = star_y[i] * _t + ORIGIN_Y ;
if(_x > 250 || _x < 10) _x = 0xFE;
if(_y > 250 || _y < 10) _y = 0xFE;
spr_id = oam_spr( _x, _y, star_z[i]>>3, star_atr[i], spr_id );
}
//delay(1);
}
void
sine_star(void)
{
++_sx;
_y = (sine_wave[_sx]>>2) - ORIGIN_Y;
oam_spr(_sx, _y, 0x10, 0, spr_id);
}
static unsigned char bg_atr;
static unsigned char byt;
void
bg_fade_out(void)
{
ppu_off();
++bg_atr;
byt = (bg_atr<<6) | (bg_atr<<4) | (bg_atr<<2) | bg_atr;
vram_adr( NT_ADR3 + NT_END );
vram_fill( byt, 64 );
ppu_on_all();
delay(2);
starfield();
ppu_off();
++bg_atr;
byt = (bg_atr<<6) | (bg_atr<<4) | (bg_atr<<2) | bg_atr;
vram_adr( NT_ADR3 + NT_END );
vram_fill( byt, 64 );
ppu_on_all();
delay(2);
starfield();
ppu_off();
++bg_atr;
byt = (bg_atr<<6) | (bg_atr<<4) | (bg_atr<<2) | bg_atr;
vram_adr( NT_ADR3 + NT_END );
vram_fill( byt, 64 );
ppu_on_all();
delay(2);
}
void
bg_reset_atr(void)
{
byt = 0;
bg_atr = 0;
ppu_off();
vram_adr( NT_ADR3 + NT_END );
vram_fill( byt, 64 );
ppu_on_all();
}
void
scroll_bg_in(void)
{
scroll(0, bounce_out[iter]);
++iter;
}
unsigned char buf[3];
static unsigned int txt_off;
static unsigned char *str;
void
put_str_seq(void)
{
if(*str)
{
buf[0] = txt_off >> 8;
buf[1] = txt_off & 0x00FF;
if( *str == ' ')
buf[2] = *str - 0x20;
else if( *str < 'A' )
buf[2] = *str - 0x30 + 1;
else
buf[2] = *str - 0x30;
//buf[2] = (*str < 'A') ? (*str)-0x20 : (*str) - 0x30;
set_vram_update( 1, buf );
++str;
}
++txt_off;
}
void
init(void)
{
_x = _y = _t = _sx = spr_id = bg_atr = si = 0;
bank_spr( 0 );
bank_bg( 1 );
pal_spr( sppal );
pal_bg( bgpal );
pal_bright( 0 );
vram_adr( NT_ADR1 );
vram_fill( 0x2B, NT_END );
vram_adr( NT_ADR2 );
vram_fill( 0x2B, NT_END );
vram_adr( NT_ADR3 );
vram_fill( 0x2B, NT_END );
vram_adr( NT_ADR4 );
vram_fill( 0x2B, NT_END );
vram_write( LD27, NT_ADR3, 32*10 );
scrolly = 0;
iter = 0;
scroll(0, scrolly);
ppu_on_all();
}
void
main(void)
{
init_stars();
init();
pal_trans_bright( 4 );
pal_bright( 1 );
starfield();
pal_bright( 2 );
starfield();
pal_bright( 3 );
starfield();
pal_bright( 4 );
music_play(music_main);
while(TRUE)
{
while(iter < 60)
{
scroll_bg_in();
starfield();
}
txt_off = NT_ADR1 + 0x220;
iter = 32;
si = 0;
str_list[0] = str1;
str_list[1] = str2;
str_list[2] = str3;
str_list[3] = str4;
str_list[4] = str5;
str_list[5] = str6;
str = str_list[si];
while(TRUE)
{
starfield();
if( si < 6 )
{
if(iter)
{
put_str_seq();
--iter;
}
else
{
iter = 32;
++si;
str = str_list[si];
}
}
else
{
break;
}
}
str_list[0] = str4;
str_list[1] = str4;
str_list[2] = str4;
str_list[3] = str4;
str_list[4] = str4;
str_list[5] = str4;
si = 0;
str = str_list[0];
txt_off = NT_ADR1 + 0x220;
while(TRUE)
{
starfield();
if( si < 6 )
{
if(iter)
{
put_str_seq();
--iter;
}
else
{
iter = 32;
++si;
str = str_list[si];
}
}
else
{
// bg_fade_out();
// scroll(0,0);
// bg_reset_atr();
// iter = 0;
// _frame = 0;
break;
}
}
str_list[0] = str7;
str_list[1] = str4;
str_list[2] = str8;
str_list[3] = str9;
si = 0;
str = str_list[0];
txt_off = NT_ADR1 + 0x220;
while(TRUE)
{
starfield();
if( si < 4 )
{
if(iter)
{
put_str_seq();
--iter;
}
else
{
iter = 32;
++si;
str = str_list[si];
}
}
else
{
break;
}
}
str_list[0] = str4;
str_list[1] = str4;
str_list[2] = str4;
str_list[3] = str4;
si = 0;
str = str_list[0];
txt_off = NT_ADR1 + 0x220;
while(TRUE)
{
starfield();
if( si < 4 )
{
if(iter)
{
put_str_seq();
--iter;
}
else
{
iter = 32;
++si;
str = str_list[si];
}
}
else
{
// bg_fade_out();
// scroll(0,0);
// bg_reset_atr();
// iter = 0;
// _frame = 0;
break;
}
}
str_list[0] = strB;
str_list[1] = str4;
str_list[2] = strC;
str_list[3] = strD;
str_list[4] = strE;
str_list[5] = strF;
si = 0;
str = str_list[0];
txt_off = NT_ADR1 + 0x220;
while(TRUE)
{
starfield();
if( si < 6 )
{
if(iter)
{
put_str_seq();
--iter;
}
else
{
iter = 32;
++si;
str = str_list[si];
}
}
else
{
// bg_fade_out();
// scroll(0,0);
// bg_reset_atr();
// iter = 0;
// _frame = 0;
break;
}
}
str_list[0] = str4;
str_list[1] = str4;
str_list[2] = str4;
str_list[3] = str4;
str_list[4] = str4;
str_list[5] = str4;
si = 0;
str = str_list[0];
txt_off = NT_ADR1 + 0x220;
while(TRUE)
{
starfield();
if( si < 6 )
{
if(iter)
{
put_str_seq();
--iter;
}
else
{
iter = 32;
++si;
str = str_list[si];
}
}
else
{
bg_fade_out();
scroll(0,0);
bg_reset_atr();
iter = 0;
_frame = 0;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment