2019-02-24 15:36:38 +00:00
|
|
|
|
2019-05-22 15:45:05 +00:00
|
|
|
/*
|
|
|
|
We demonstrate horizontal scrolling of two nametables.
|
|
|
|
Vertical mirroring is set, so nametables A and B are
|
|
|
|
to the left and right of each other.
|
|
|
|
|
|
|
|
New playfield data is randomly generated and updated
|
|
|
|
offscreen using the vrambuf module.
|
|
|
|
Every 32 pixels, we also update the attribute table.
|
|
|
|
We also use the split() function to create a status bar.
|
|
|
|
*/
|
|
|
|
|
2019-02-24 15:36:38 +00:00
|
|
|
#include "neslib.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// 0 = horizontal mirroring
|
|
|
|
// 1 = vertical mirroring
|
|
|
|
#define NES_MIRRORING 1
|
|
|
|
|
|
|
|
// VRAM update buffer
|
|
|
|
#include "vrambuf.h"
|
|
|
|
//#link "vrambuf.c"
|
|
|
|
|
|
|
|
// link the pattern table into CHR ROM
|
|
|
|
//#link "chr_generic.s"
|
|
|
|
|
|
|
|
// function to write a string into the name table
|
|
|
|
// adr = start address in name table
|
|
|
|
// str = pointer to string
|
|
|
|
void put_str(unsigned int adr, const char *str) {
|
|
|
|
vram_adr(adr); // set PPU read/write address
|
|
|
|
vram_write(str, strlen(str)); // write bytes to PPU
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:56:51 +00:00
|
|
|
word x_scroll; // X scroll amount in pixels
|
|
|
|
byte bldg_height; // building height in tiles
|
|
|
|
byte bldg_width; // building width in tiles
|
|
|
|
byte bldg_char; // character to draw
|
|
|
|
byte bldg_attr; // attribute table value
|
2019-02-24 15:36:38 +00:00
|
|
|
|
|
|
|
#define PLAYROWS 24
|
|
|
|
|
2019-05-22 15:45:05 +00:00
|
|
|
// convert from nametable address to attribute table address
|
2019-02-24 15:36:38 +00:00
|
|
|
word nt2attraddr(word a) {
|
|
|
|
return (a & 0x2c00) | 0x3c0 |
|
|
|
|
((a >> 4) & 0x38) | ((a >> 2) & 0x07);
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:45:05 +00:00
|
|
|
// generate new random building data
|
2019-02-26 15:56:51 +00:00
|
|
|
void new_building() {
|
|
|
|
bldg_height = (rand8() & 7) + 2;
|
|
|
|
bldg_width = (rand8() & 3) * 4 + 4;
|
|
|
|
bldg_char = (rand8() & 15);
|
|
|
|
bldg_attr = rand8();
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:45:05 +00:00
|
|
|
// update the nametable offscreen
|
|
|
|
// called every 8 horizontal pixels
|
2019-02-24 15:36:38 +00:00
|
|
|
void update_nametable() {
|
2019-04-09 14:24:23 +00:00
|
|
|
register word addr;
|
|
|
|
// a buffer drawn to the nametable vertically
|
2019-07-04 19:22:42 +00:00
|
|
|
char buf[PLAYROWS];
|
2019-02-24 15:36:38 +00:00
|
|
|
// divide x_scroll by 8
|
|
|
|
// to get nametable X position
|
2019-05-17 19:55:59 +00:00
|
|
|
byte x = (x_scroll/8 + 32) & 63;
|
2019-02-24 15:36:38 +00:00
|
|
|
if (x < 32)
|
|
|
|
addr = NTADR_A(x, 4);
|
|
|
|
else
|
|
|
|
addr = NTADR_B(x&31, 4);
|
|
|
|
// clear empty space
|
2019-07-04 19:22:42 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2019-02-26 15:56:51 +00:00
|
|
|
// draw a random star
|
2019-07-04 19:22:42 +00:00
|
|
|
buf[rand8() & 15] = '.';
|
2019-02-24 15:36:38 +00:00
|
|
|
// draw roof
|
|
|
|
buf[PLAYROWS-bldg_height-1] = bldg_char & 3;
|
|
|
|
// draw rest of building
|
|
|
|
memset(buf+PLAYROWS-bldg_height, bldg_char, bldg_height);
|
|
|
|
// draw vertical slice in name table
|
2019-07-16 13:55:41 +00:00
|
|
|
vrambuf_put(addr | VRAMBUF_VERT, buf, PLAYROWS);
|
2019-02-24 15:36:38 +00:00
|
|
|
// every 4 columns, update attribute table
|
|
|
|
if ((x & 3) == 1) {
|
|
|
|
// compute attribute table address
|
|
|
|
// of upper attribute block
|
|
|
|
addr = nt2attraddr(addr) + 8*4;
|
|
|
|
VRAMBUF_PUT(addr, bldg_attr, 0);
|
|
|
|
// put lower attribute block
|
|
|
|
addr += 8;
|
|
|
|
VRAMBUF_PUT(addr, bldg_attr, 0);
|
2019-04-07 16:33:01 +00:00
|
|
|
vrambuf_end();
|
2019-02-24 15:36:38 +00:00
|
|
|
}
|
|
|
|
// generate new building?
|
|
|
|
if (--bldg_width == 0) {
|
2019-02-26 15:56:51 +00:00
|
|
|
new_building();
|
2019-02-24 15:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// function to scroll window up and down until end
|
|
|
|
void scroll_demo() {
|
2019-02-26 15:56:51 +00:00
|
|
|
// make 1st building
|
|
|
|
new_building();
|
|
|
|
x_scroll = 0;
|
2019-02-24 15:36:38 +00:00
|
|
|
// infinite loop
|
|
|
|
while (1) {
|
|
|
|
// update nametable every 8 pixels
|
|
|
|
if ((x_scroll & 7) == 0) {
|
|
|
|
update_nametable();
|
|
|
|
}
|
2019-04-15 23:37:11 +00:00
|
|
|
// ensure VRAM buffer is cleared
|
2019-02-24 15:36:38 +00:00
|
|
|
ppu_wait_nmi();
|
2019-04-09 14:24:23 +00:00
|
|
|
vrambuf_clear();
|
2019-04-15 23:37:11 +00:00
|
|
|
// split at x_scroll
|
|
|
|
split(x_scroll, 0);
|
|
|
|
// increment x_scroll
|
|
|
|
++x_scroll;
|
2019-02-24 15:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 16:32:37 +00:00
|
|
|
/*{pal:"nes",layout:"nes"}*/
|
|
|
|
const char PALETTE[32] = {
|
2019-02-24 15:36:38 +00:00
|
|
|
0x03, // background color
|
|
|
|
|
2019-03-22 16:32:37 +00:00
|
|
|
0x25,0x30,0x27,0x00, // ladders and pickups
|
|
|
|
0x1C,0x20,0x2C,0x00, // floor blocks
|
|
|
|
0x00,0x10,0x20,0x00,
|
|
|
|
0x06,0x16,0x26,0x00,
|
2019-02-24 15:36:38 +00:00
|
|
|
|
2019-03-22 16:32:37 +00:00
|
|
|
0x16,0x35,0x24,0x00, // enemy sprites
|
|
|
|
0x00,0x37,0x25,0x00, // rescue person
|
|
|
|
0x0D,0x2D,0x1A,0x00,
|
|
|
|
0x0D,0x27,0x2A // player sprites
|
2019-02-24 15:36:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// main function, run after console reset
|
|
|
|
void main(void) {
|
|
|
|
// set palette colors
|
|
|
|
pal_all(PALETTE);
|
|
|
|
|
|
|
|
// write text to name table
|
|
|
|
put_str(NTADR_A(7,0), "Nametable A, Line 0");
|
|
|
|
put_str(NTADR_A(7,1), "Nametable A, Line 1");
|
|
|
|
put_str(NTADR_A(7,2), "Nametable A, Line 2");
|
|
|
|
vram_adr(NTADR_A(0,3));
|
|
|
|
vram_fill(5, 32);
|
|
|
|
put_str(NTADR_A(2,4), "Nametable A, Line 4");
|
|
|
|
put_str(NTADR_A(2,15),"Nametable A, Line 15");
|
|
|
|
put_str(NTADR_A(2,27),"Nametable A, Line 27");
|
|
|
|
put_str(NTADR_B(2,4), "Nametable B, Line 4");
|
|
|
|
put_str(NTADR_B(2,15),"Nametable B, Line 15");
|
|
|
|
put_str(NTADR_B(2,27),"Nametable B, Line 27");
|
|
|
|
|
|
|
|
// set attributes
|
|
|
|
vram_adr(0x23c0);
|
|
|
|
vram_fill(0x55, 8);
|
|
|
|
|
|
|
|
// set sprite 0
|
|
|
|
oam_clear();
|
2019-04-15 23:37:11 +00:00
|
|
|
oam_spr(1, 30, 0xa0, 0, 0);
|
2019-02-24 15:36:38 +00:00
|
|
|
|
|
|
|
// clear vram buffer
|
2019-04-07 16:33:01 +00:00
|
|
|
vrambuf_clear();
|
2019-04-15 23:37:11 +00:00
|
|
|
set_vram_update(updbuf);
|
2019-02-24 15:36:38 +00:00
|
|
|
|
|
|
|
// enable PPU rendering (turn on screen)
|
|
|
|
ppu_on_all();
|
|
|
|
|
|
|
|
// scroll window back and forth
|
|
|
|
scroll_demo();
|
|
|
|
}
|