Skip to content

Commit

Permalink
I should make smaller commits, #255 and other stuff
Browse files Browse the repository at this point in the history
Make (most?) flickering sprites flicker a bit slower #258
Attempt to mitigate #256, it's fixed for the booster on ceilings at least
  • Loading branch information
andwn committed Mar 22, 2018
1 parent 63e582a commit 9a7fbb5
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 144 deletions.
10 changes: 5 additions & 5 deletions inc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ uint8_t FPS;
// just match the index, and on NTSC they are roughly index*5/6 for speed and
// index*6/5 for time respectively.
uint16_t time_tab[0x100];
uint16_t speed_tab[0x100];
int16_t speed_tab[0x100];

// Default is a bit slow due to branching, but compensates in case x is too large
// Negative values are invalid. Always use -SPEED(x) instead of SPEED(-x)
Expand Down Expand Up @@ -81,19 +81,19 @@ const int16_t cos2[0x100];
// pixel - single dot on screen (1x1)
// tile - genesis VDP tile (8x8)
// block - Cave Story tile (16x16)
#define sub_to_pixel(x) ((x)>>CSF)
#define sub_to_pixel(x) ((x)>>9)
#define sub_to_tile(x) ((x)>>12)
#define sub_to_block(x) ((x)>>13)

#define pixel_to_sub(x) ((x)<<CSF)
#define pixel_to_sub(x) (((int32_t)(x))<<9)
#define pixel_to_tile(x) ((x)>>3)
#define pixel_to_block(x) ((x)>>4)

#define tile_to_sub(x) ((x)<<12)
#define tile_to_sub(x) (((int32_t)(x))<<12)
#define tile_to_pixel(x) ((x)<<3)
#define tile_to_block(x) ((x)>>1)

#define block_to_sub(x) ((x)<<13)
#define block_to_sub(x) (((int32_t)(x))<<13)
#define block_to_pixel(x) ((x)<<4)
#define block_to_tile(x) ((x)<<1)

Expand Down
2 changes: 1 addition & 1 deletion inc/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void player_draw();
uint8_t player_invincible();
// Inflict damage on the player, will start the animation, knockback, red numbers,
// sound, iframes, and check for death
uint8_t player_inflict_damage(int16_t damage);
uint8_t player_inflict_damage(uint16_t damage);

// Makes the player sprite visible/invisible
void player_show();
Expand Down
4 changes: 2 additions & 2 deletions inc/stdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long int32_t;

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint32_t;

// SGDK Compatibility

Expand Down
20 changes: 10 additions & 10 deletions inc/vdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,55 +228,55 @@
* \brief
* Set VDP command to read specified VRAM address.
*/
#define GFX_READ_VRAM_ADDR(adr) ((0x0000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0x00)
#define GFX_READ_VRAM_ADDR(adr) ((0x0000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0x00)
/**
* \brief
* Set VDP command to read specified CRAM address.
*/
#define GFX_READ_CRAM_ADDR(adr) ((0x0000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0x20)
#define GFX_READ_CRAM_ADDR(adr) ((0x0000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0x20)
/**
* \brief
* Set VDP command to read specified VSRAM address.
*/
#define GFX_READ_VSRAM_ADDR(adr) ((0x0000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0x10)
#define GFX_READ_VSRAM_ADDR(adr) ((0x0000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0x10)

/**
* \brief
* Set VDP command to write at specified VRAM address.
*/
#define GFX_WRITE_VRAM_ADDR(adr) ((0x4000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0x00)
#define GFX_WRITE_VRAM_ADDR(adr) ((0x4000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0x00)
/**
* \brief
* Set VDP command to write at specified CRAM address.
*/
#define GFX_WRITE_CRAM_ADDR(adr) ((0xC000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0x00)
#define GFX_WRITE_CRAM_ADDR(adr) ((0xC000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0x00)
/**
* \brief
* Set VDP command to write at specified VSRAM address.
*/
#define GFX_WRITE_VSRAM_ADDR(adr) ((0x4000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0x10)
#define GFX_WRITE_VSRAM_ADDR(adr) ((0x4000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0x10)

/**
* \brief
* Set VDP command to issue a DMA transfert to specified VRAM address.
*/
#define GFX_DMA_VRAM_ADDR(adr) ((0x4000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0x80)
#define GFX_DMA_VRAM_ADDR(adr) ((0x4000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0x80)
/**
* \brief
* Set VDP command to issue a DMA transfert to specified CRAM address.
*/
#define GFX_DMA_CRAM_ADDR(adr) ((0xC000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0x80)
#define GFX_DMA_CRAM_ADDR(adr) ((0xC000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0x80)
/**
* \brief
* Set VDP command to issue a DMA transfert to specified VSRAM address.
*/
#define GFX_DMA_VSRAM_ADDR(adr) ((0x4000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0x90)
#define GFX_DMA_VSRAM_ADDR(adr) ((0x4000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0x90)

/**
* \brief
* Set VDP command to issue a DMA VRAM copy to specified VRAM address.
*/
#define GFX_DMA_VRAMCOPY_ADDR(adr) ((0x4000 + ((adr) & 0x3FFF)) << 16) + (((adr) >> 14) | 0xC0)
#define GFX_DMA_VRAMCOPY_ADDR(adr) ((0x4000 + (((uint32_t)(adr)) & 0x3FFF)) << 16) + ((((uint32_t)(adr)) >> 14) | 0xC0)

/**
* \brief
Expand Down
2 changes: 1 addition & 1 deletion src/ai/balfrog.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void ai_balfrog(Entity *e) {
if(e->timer <= TIME_8(150)) {
Entity *balrog = entity_find_by_type(OBJ_BALROG);
if(balrog) {
e->hidden ^= 1;
e->hidden = (e->timer & 2);
balrog->hidden = !e->hidden;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ai/ballos2.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ void ai_ballos_target(Entity *e) {
} /* fallthrough */
case 1:
{
e->hidden ^= 1;
if (++e->timer == TIME_8(20)) { // lightning attack
e->hidden = (++e->timer & 2);
if (e->timer == TIME_8(20)) { // lightning attack
if(!e->dir) entity_create(e->x_mark, e->y_mark - 0x2000, OBJ_LIGHTNING, NPC_OPTION2);
} else if(e->timer >= TIME_8(30)) {
e->state = STATE_DELETE;
Expand Down
6 changes: 3 additions & 3 deletions src/ai/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ void onspawn_teleLight(Entity *e) {
}

void ai_teleLight(Entity *e) {
if(e->state) e->hidden ^= 1;
if(e->state) e->hidden = (++e->timer & 2);
else e->hidden = TRUE;
}

Expand Down Expand Up @@ -408,8 +408,8 @@ void ai_player(Entity *e) {
} /* fallthrough */
case 21:
{
e->hidden ^= 1;
if(++e->timer > TIME_8(50)) {
e->hidden = (++e->timer & 2);
if(e->timer > TIME_8(50)) {
e->state = STATE_DELETE;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/ai/doctorm.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ void ai_muscle_doctor(Entity *e) {
case STATE_TELEPORT+1:
{
//if (dr_tp_out(o))
e->hidden ^= 1;
if(++e->timer > TIME(30)) {
e->hidden = (++e->timer & 2);
if(e->timer > TIME(30)) {
e->state++;
e->timer = 0;
e->hidden = TRUE;
Expand Down Expand Up @@ -363,8 +363,8 @@ void ai_muscle_doctor(Entity *e) {
case STATE_TELEPORT+4:
{
//if (dr_tp_in(o))
e->hidden ^= 1;
if(++e->timer > TIME(30)) {
e->hidden = (++e->timer & 2);
if(e->timer > TIME(30)) {
e->eflags |= NPC_SHOOTABLE;
e->attack = DAMAGE_NORMAL;
e->hidden = FALSE;
Expand Down
4 changes: 2 additions & 2 deletions src/ai/hell.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ void ai_bute_arrow(Entity *e) {

case 20: // hit something
{
e->hidden ^= 1;
if(++e->timer > TIME_8(30)) e->state = STATE_DELETE;
e->hidden = (++e->timer & 2);
if(e->timer > TIME_8(30)) e->state = STATE_DELETE;
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ai/item.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void ai_missile(Entity *e) {
e->state = STATE_DELETE;
return;
} else if(e->timer > TIME_10(350)) {
e->hidden ^= 1;
e->hidden = (e->timer & 2);
}
} else if(!e->state) {
// Hide the sprite when under a breakable block
Expand Down Expand Up @@ -155,7 +155,7 @@ void ai_heart(Entity *e) {
e->state = STATE_DELETE;
return;
} else if(e->timer > TIME_10(350)) {
e->hidden ^= 1;
e->hidden = (e->timer & 2);
}
} else if(!e->state) {
// Hide the sprite when under a breakable block
Expand Down
10 changes: 5 additions & 5 deletions src/ai/regu.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,11 @@ void ai_king(Entity *e) {
/* fallthrough */
case 41:
{
e->hidden ^= 1;
if (++e->timer > 100) {
e->hidden = (++e->timer & 2);
if (e->timer > TIME_8(100)) {
effect_create_smoke(e->x >> CSF, e->y >> CSF);
e->state = 42;
e->hidden = 0;
e->hidden = FALSE;
e->frame = 6; // Sword
}
}
Expand Down Expand Up @@ -713,8 +713,8 @@ void ai_booster_falling(Entity *e) {
/* fallthrough */
case 21:
{
e->hidden ^= 1;
if (++e->timer > TIME(100)) {
e->hidden = (++e->timer & 2);
if (e->timer > TIME(100)) {
//SmokeClouds(o, 4, 16, 16);
e->state = STATE_DELETE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ai/toroko.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ void ai_torokoBoss(Entity *e) {
/* fallthrough */
case 141:
{
e->hidden ^= 1;
if (++e->timer > TIME_8(100)) {
e->hidden = (++e->timer & 2);
if (e->timer > TIME_8(100)) {
SMOKE_AREA((e->x >> CSF) - 8, e->y >> CSF, 16, 16, 2);
e->state = STATE_DELETE;
}
Expand Down
28 changes: 14 additions & 14 deletions src/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ void camera_init() {

void camera_set_position(int32_t x, int32_t y) {
// Don't let the camera leave the stage
if(x > block_to_sub(stageWidth) - (SCREEN_HALF_W<<CSF))
x = block_to_sub(stageWidth) - (SCREEN_HALF_W<<CSF);
if(y > block_to_sub(stageHeight) - ((SCREEN_HALF_H+2)<<CSF))
y = block_to_sub(stageHeight) - ((SCREEN_HALF_H+2)<<CSF);
if(x < SCREEN_HALF_W<<CSF)
x = SCREEN_HALF_W<<CSF;
if(y < (SCREEN_HALF_H+2)<<CSF)
y = (SCREEN_HALF_H+2)<<CSF;
if(x > block_to_sub(stageWidth) - pixel_to_sub(SCREEN_HALF_W))
x = block_to_sub(stageWidth) - pixel_to_sub(SCREEN_HALF_W);
if(y > block_to_sub(stageHeight) - pixel_to_sub(SCREEN_HALF_H+2))
y = block_to_sub(stageHeight) - pixel_to_sub(SCREEN_HALF_H+2);
if(x < pixel_to_sub(SCREEN_HALF_W)) x = pixel_to_sub(SCREEN_HALF_W);
if(y < pixel_to_sub(SCREEN_HALF_H+2)) y = pixel_to_sub(SCREEN_HALF_H+2);
// Apply
camera.x = camera.x_mark = x;
camera.y = camera.y_mark = y;
Expand Down Expand Up @@ -123,14 +121,16 @@ void camera_update() {
x_next = pixel_to_sub(SCREEN_HALF_W + 8);
y_next = pixel_to_sub(SCREEN_HALF_H + 16);
} else {
if(x_next < pixel_to_sub(SCREEN_HALF_W + 2))
if(x_next < pixel_to_sub(SCREEN_HALF_W + 2)) {
x_next = pixel_to_sub(SCREEN_HALF_W + 2);
else if(x_next > block_to_sub(stageWidth) - pixel_to_sub(SCREEN_HALF_W + 2))
} else if(x_next > block_to_sub(stageWidth) - pixel_to_sub(SCREEN_HALF_W + 2)) {
x_next = block_to_sub(stageWidth) - pixel_to_sub(SCREEN_HALF_W + 2);
if(y_next < pixel_to_sub(SCREEN_HALF_H + 2))
}
if(y_next < pixel_to_sub(SCREEN_HALF_H + 2)) {
y_next = pixel_to_sub(SCREEN_HALF_H + 2);
else if(y_next > block_to_sub(stageHeight) - pixel_to_sub(SCREEN_HALF_H + 2))
} else if(y_next > block_to_sub(stageHeight) - pixel_to_sub(SCREEN_HALF_H + 2)) {
y_next = block_to_sub(stageHeight) - pixel_to_sub(SCREEN_HALF_H + 2);
}
}
// Shifted values
camera.x_shifted = (x_next >> CSF) - SCREEN_HALF_W;
Expand All @@ -154,7 +154,7 @@ void camera_update() {
} else {
int16_t x = sub_to_tile(x_next) + (morphingColumn == 1 ? 30 : -30);
int16_t y = sub_to_tile(y_next) - 16 /*+ morphingRow*/;
if(x >= 0 && x < stageWidth << 1) {
if(x >= 0 && x < (int16_t)(stageWidth) << 1) {
for(uint16_t i = 32; i--; ) {
// It's actually faster to just draw garbage than have these checks
//if(y >= stageHeight << 1) break;
Expand All @@ -180,7 +180,7 @@ void camera_update() {
} else {
int16_t y = sub_to_tile(y_next) + (morphingRow == 1 ? 15 : -15);
int16_t x = sub_to_tile(x_next) - 32 /*+ morphingColumn*/;
if(y >= 0 && y < stageHeight << 1) {
if(y >= 0 && y < (int16_t)(stageHeight) << 1) {
for(uint16_t i = 64; i--; ) {
//if(x >= stageWidth << 1) break;
//if(x >= 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/db/npc.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ const npc_info_def npc_info[NPC_COUNT + 9 + 20 + 13] = {
{ &SPR_BigDoorFrame,NOSHEET, PAL1, 1, &ai_null, &ai_null, &ai_null }, // Large Door (Frame)
{ &SPR_BigDoor, NOSHEET, PAL1, 1, &onspawn_doorway, &ai_doorway, &ai_null }, // Large Door
{ &SPR_Doctor, NOSHEET, PAL3, 1, &onspawn_snap, &ai_doctor, &ai_null }, // Doctor
{ &SPR_ToroBoss, NOSHEET, PAL3, 2, &onspawn_torokoBoss, &ai_torokoBoss, &ondeath_torokoBoss },
{ NULL, SHEET_BLOCK, PAL1, 1, &onspawn_persistent, &ai_torokoBlock, &ai_null },
{ NULL, SHEET_FLOWER, PAL3, 1, &onspawn_persistent, &ai_torokoFlower, &ondeath_default },
{ &SPR_ToroBoss, NOSHEET, PAL3, 1, &onspawn_torokoBoss, &ai_torokoBoss, &ondeath_torokoBoss }, // Toroko+
{ NULL, SHEET_BLOCK, PAL1, 1, &onspawn_persistent, &ai_torokoBlock, &ai_null }, // Block she throws
{ NULL, SHEET_FLOWER, PAL3, 1, &onspawn_persistent, &ai_torokoFlower, &ondeath_default }, // That turn into flowers
{ &SPR_Jenka, NOSHEET, PAL3, 1, &onspawn_jenka, &ai_null, &ai_null }, // Jenka
/* 0x090 (144) */
{ &SPR_Toroko, NOSHEET, PAL3, 1, &onspawn_persistent, &ai_toroko_tele_in, &ai_null },
Expand Down
40 changes: 20 additions & 20 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,38 +440,38 @@ void entity_update_collision(Entity *e) {
}

uint8_t collide_stage_leftwall(Entity *e) {
uint16_t block_x, block_y1, block_y2;
uint8_t pxa1, pxa2;
block_x = pixel_to_block(sub_to_pixel(e->x_next) -
(e->dir ? e->hit_box.right : e->hit_box.left));
block_y1 = pixel_to_block(sub_to_pixel(e->y_next) - e->hit_box.top + 3);
block_y2 = pixel_to_block(sub_to_pixel(e->y_next) + e->hit_box.bottom - 3);
pxa1 = stage_get_block_type(block_x, block_y1);
pxa2 = stage_get_block_type(block_x, block_y2);
int16_t xoff = e->dir ? e->hit_box.right : e->hit_box.left;
uint16_t pixel_x = (e->x_next >> CSF) - xoff;
uint16_t pixel_y = (e->y_next >> CSF);
uint16_t block_x = pixel_to_block(pixel_x);
uint16_t block_y1 = pixel_to_block(pixel_y - e->hit_box.top + 3);
uint16_t block_y2 = pixel_to_block(pixel_y + e->hit_box.bottom - 3);
uint8_t pxa1 = stage_get_block_type(block_x, block_y1);
uint8_t pxa2 = stage_get_block_type(block_x, block_y2);
if(pxa1 == 0x41 || pxa2 == 0x41 || pxa1 == 0x43 || pxa2 == 0x43 ||
(!((e->eflags|e->nflags)&NPC_IGNORE44) && (pxa1 == 0x44 || pxa2 == 0x44))) {
e->x_speed = 0;
e->x_next = pixel_to_sub(
block_to_pixel(block_x) + block_to_pixel(1) + e->hit_box.left);
e->x_next &= ~0x1FF; // Align to pixel
e->x_next += pixel_to_sub(min((pixel_x & ~0xF) + 16 - pixel_x, 3));
return TRUE;
}
return FALSE;
}

uint8_t collide_stage_rightwall(Entity *e) {
uint16_t block_x, block_y1, block_y2;
uint8_t pxa1, pxa2;
block_x = pixel_to_block(sub_to_pixel(e->x_next) +
(e->dir ? e->hit_box.left : e->hit_box.right));
block_y1 = pixel_to_block(sub_to_pixel(e->y_next) - e->hit_box.top + 3);
block_y2 = pixel_to_block(sub_to_pixel(e->y_next) + e->hit_box.bottom - 3);
pxa1 = stage_get_block_type(block_x, block_y1);
pxa2 = stage_get_block_type(block_x, block_y2);
int16_t xoff = e->dir ? e->hit_box.left : e->hit_box.right;
uint16_t pixel_x = (e->x_next >> CSF) + xoff;
uint16_t pixel_y = (e->y_next >> CSF);
uint16_t block_x = pixel_to_block(pixel_x);
uint16_t block_y1 = pixel_to_block(pixel_y - e->hit_box.top + 3);
uint16_t block_y2 = pixel_to_block(pixel_y + e->hit_box.bottom - 3);
uint8_t pxa1 = stage_get_block_type(block_x, block_y1);
uint8_t pxa2 = stage_get_block_type(block_x, block_y2);
if(pxa1 == 0x41 || pxa2 == 0x41 || pxa1 == 0x43 || pxa2 == 0x43 ||
(!((e->eflags|e->nflags)&NPC_IGNORE44) && (pxa1 == 0x44 || pxa2 == 0x44))) {
e->x_speed = 0;
e->x_next = pixel_to_sub(
block_to_pixel(block_x) - e->hit_box.right);
e->x_next &= ~0x1FF;
e->x_next -= pixel_to_sub(min(pixel_x - (pixel_x & ~0xF), 3));
return TRUE;
}
return FALSE;
Expand Down
Loading

0 comments on commit 9a7fbb5

Please sign in to comment.