I don't think I'll make a game out of exactly this... I might just want to make a scrolling platformer or a overhead view adventure or something else. This is just a demo program I made to test things, and I've made the same thing for the Spectrum and MSX as well (these can all be found here in the forums.)
I feel like perhaps it's time to make a separate board for this target, because this target is very promising!
Anyway, this post here is to update my code... It seems turning off and on the display every frame is probably too much, so instead I use wait_vbl_done() instead. With this change, windowing is working too!
So here's a new version, with windowing.
Code: Select all
/*
* Timmy's first gb demo, version 20200227
* February 2020
*
* Compile with:
* zcc +gb -create-app -o gbdemo.o gbdemo.c
*
* Usage:
* Use joypad to move sprite
* Hold A and move joypad to move background
* Hold B and move joypad to move window
*/
#include <gb/gb.h>
// some tile data
const unsigned char backwalls[] = {
247, 247, 247, 247, 247, 247, 0, 0, 223, 223, 223, 223, 223, 223, 0, 0,
248, 248, 227, 227, 207, 207, 159, 159, 191, 191, 63, 63, 127, 127, 127, 127,
127, 127, 127, 127, 63, 63, 191, 191, 159, 159, 207, 207, 227, 227, 248, 248,
31, 31, 199, 199, 243, 243, 249, 249, 253, 253, 252, 252, 254, 254, 254, 254,
254, 254, 254, 254, 252, 252, 253, 253, 249, 249, 243, 243, 199, 199, 31, 31,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
0,0,63,63,12,12,12,12,12,12,12,12,12,12,0,0,
0,0,63,63,12,12,12,12,12,12,12,12,63,63,0,0,
0,0,99,99,119,119,127,127,107,107,99,99,99,99,0,0,
0,0,51,51,51,51,30,30,12,12,12,12,12,12,0,0
};
// several backgrounds
const unsigned char bkg_tiles[] =
{
4,3,2,1
};
const unsigned char bkg_tiles2[] =
{
0,0,0,0
};
const unsigned char bkg_tiles3[] =
{
7,8,9,9,10
};
// sprite
const unsigned char sprites[] =
{
7, 0, 31, 3, 63, 12, 127, 16, 127, 32, 255, 32, 255, 64, 255, 64, 255, 70, 255, 73, 255, 75, 255, 70, 255, 0, 255, 127, 255, 6, 15, 0,
224, 0, 240, 192, 252, 48, 254, 8, 254, 196, 255, 36, 255, 18, 255, 18, 255, 210, 255, 34, 255, 98, 255, 194, 255, 0, 255, 234, 255, 160, 240, 0
};
uint8_t px1[4];
uint8_t py1[4];
int8_t pdx[4];
int8_t pdy[4];
uint8_t winx, winy;
void main()
{
uint8_t i, j, x, y, px, py;
disable_interrupts();
DISPLAY_OFF;
LCDC_REG = 0x67;
/* Set palettes */
BGP_REG = 94;
OBP0_REG = OBP1_REG = 30;
/* Initialise background */
set_bkg_data(0, 11, backwalls);
for(i = 0; i < 32; i+=2)
for(j = 0; j < 32; j+=2)
if ((i+j)&2)
set_bkg_tiles(i, j, 2, 2, bkg_tiles);
else
set_bkg_tiles(i, j, 2, 2, bkg_tiles2);
set_bkg_tiles(14, 16, 5, 1, bkg_tiles3);
// Set Screen back to top left corner
SCX_REG = 0;
SCY_REG = 0;
/* Push Sprite data to VRAM */
set_sprite_data(0, 4, sprites);
// Regular sprites
set_sprite_tile(0, 0);
set_sprite_tile(1, 2);
move_sprite(0, 8, 16);
move_sprite(1, 8+8, 16);
set_sprite_prop(0,0);
set_sprite_prop(1,0);
// Y flipped sprites
set_sprite_tile(2, 0);
set_sprite_tile(3, 2);
move_sprite(2, 8+16, 32);
move_sprite(3, 8+16+8, 32);
set_sprite_prop(2,S_FLIPY);
set_sprite_prop(3,S_FLIPY);
// X flipped sprites
// Note: you have to flip the x positions of these 2 sprites too!
set_sprite_tile(4, 2);
set_sprite_tile(5, 0);
move_sprite(4, 8+32+8, 48);
move_sprite(5, 8+32, 48);
set_sprite_prop(4,S_FLIPX);
set_sprite_prop(5,S_FLIPX);
set_sprite_tile(6, 0);
set_sprite_tile(7, 2);
set_sprite_tile(8, 0);
set_sprite_tile(9, 2);
winx = 7 + 40;
winy = 128;
WX_REG = winx;
WY_REG = winy;
set_win_tiles(1, 1, 5, 1, bkg_tiles3);
// Ok! Done!
// Let's open the display again
DISPLAY_ON;
enable_interrupts();
x = 0; y = 0;
px = 8, py = 16;
px1[0] = 100; py1[0] = 32; pdx[0]= 1; pdy[0]=-2;
px1[1] = 64; py1[1] = 80; pdx[1]= 2; pdy[1]= 2;
px1[2] = 32; py1[2] = 100; pdx[2]=-2; pdy[2]=-1;
px1[3] = 140; py1[3] = 64; pdx[3]=-1; pdy[3]= 1;
while(1)
{
//wait_vbl_done();
i = joypad();
if((i & J_A))
{
if(i & J_UP)
{
if (y>0) y--;
}
if(i & J_DOWN)
{
if (y<128) y++;
}
if(i & J_LEFT)
{
if (x>0) x--;
}
if(i & J_RIGHT)
{
if (x<128) x++;
}
}
else
if((i & J_B))
{
if(i & J_UP)
{
if (winy>0) winy-=2;
}
if(i & J_DOWN)
{
if (winy<160) winy+=2;
}
if(i & J_LEFT)
{
if (winx>8) winx-=2;
}
if(i & J_RIGHT)
{
if (winx<160+8) winx+=2;
}
}
else
{
if(i & J_UP)
{
if (py>0) py-=2;
}
if(i & J_DOWN)
{
if (py<160) py+=2;
}
if(i & J_LEFT)
{
if (px>0) px-=2;
}
if(i & J_RIGHT)
{
if (px<160+8) px+=2;
}
}
for (j=0; j<4; j++)
{
px1[j] += pdx[j];
py1[j] += pdy[j];
if (px1[j]<8) pdx[j]=-pdx[j];
if (px1[j]>8+144) pdx[j]=-pdx[j];
if (py1[j]<16) pdy[j]=-pdy[j];
if (py1[j]>16+128) pdy[j]=-pdy[j];
}
wait_vbl_done();
SCX_REG = x;
SCY_REG = y;
WX_REG = winx;
WY_REG = winy;
move_sprite(0, px, py);
move_sprite(1, px+8, py);
for (j=0; j<4; j++)
{
move_sprite(j*2+2, px1[j], py1[j]);
move_sprite(j*2+3, px1[j]+8, py1[j]);
}
}
}