Skip to content

Commit

Permalink
Optimized some more shit, mode macros added
Browse files Browse the repository at this point in the history
CODE:        7516 → 7492 - diff:  24
ZEROPAGE:    165  → 164  - diff:  1
XCD_BANK_00: 7917 → 7735 - diff:  182
XCD_BANK_01: 2880 → 2719 - diff:  161
XCD_BANK_02: 1298 → 1273 - diff:  25

Total size reduction: 393 bytes
  • Loading branch information
ADM228 committed Jun 6, 2024
1 parent 9a6f6fe commit 525a88e
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 54 deletions.
36 changes: 28 additions & 8 deletions LIB/nesdash.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,43 @@ extern uint8_t parallax_scroll_column_start;
#define idx16_lo_NOC(arr, idx) (__A__ = idx<<1, __asm__("tay\n lda %v, y", arr), __A__)
#define idx16_hi_NOC(arr, idx) (__A__ = idx<<1, __asm__("tay\n lda %v+1, y", arr), __A__)

// store a word's high and low bytes into separate places
#define storeWordSeparately(word, low, high) \
(__AX__ = word, \
__asm__("STA %v", low), \
__asm__("STX %v", high))

#define pal_fade_to_withmusic(from, to) (++auto_fs_updates, pal_fade_to(from, to), auto_fs_updates = 0)
#define uint8_dec(arr, idx) ( \
__AX__ = idx << 8, \
__asm__("dec %v,x", arr), \
__asm__("lda %v,x", arr), \
__A__ \
)

#define uint8_inc(arr, idx) ( \
__AX__ = idx << 8, \
__asm__("inc %v,x", arr), \
__asm__("lda %v,x", arr), \
__A__ \
)

#define uint8_store(arr, idx, val) ( \
__A__ = idx, \
__asm__("tay"), \
__A__ = val, \
__asm__("sta %v, y", arr))

// Yes i had to actually fucking use inline asm to get this to run fast
#define store_short_arr_NOC(arr, idx, word) ( \
#define uint16_store_NOC(arr, idx, word) ( \
__A__ = idx<<1, \
__asm__("tay"), \
__A__ = LSB(word), \
__asm__("sta %v,y", arr), \
__A__ = MSB(word), \
__asm__("sta %v+1, y", arr))

// store a word's high and low bytes into separate places
#define storeWordSeparately(word, low, high) \
(__AX__ = word, \
__asm__("STA %v", low), \
__asm__("STX %v", high))

#define pal_fade_to_withmusic(from, to) (++auto_fs_updates, pal_fade_to(from, to), auto_fs_updates = 0)

// set palette color, index 0..31
// completely inlines and replaces neslib's
extern uint8_t PAL_UPDATE;
Expand Down
20 changes: 10 additions & 10 deletions SAUCE/famidash.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ unsigned char eject_U; // from up
unsigned short address;
unsigned char x; // room loader code
unsigned char y;
unsigned short index;
unsigned char index;
unsigned short index2;
unsigned char temp_x;
unsigned char temp_y;
Expand Down Expand Up @@ -280,15 +280,15 @@ unsigned char practice_bg_color_type;
// the funny uh uhhhhh um the uhh sprite storidge
// X_lowbyte, X_highbyte, Y_lowbyte, Y_highbyte, object id, unused, unused, unused
#define max_loaded_sprites 16
unsigned short activesprites_x[max_loaded_sprites];
unsigned short activesprites_y[max_loaded_sprites];
unsigned char activesprites_type[max_loaded_sprites];
unsigned char activesprites_anim_frame[max_loaded_sprites];
signed char activesprites_anim_frame_count[max_loaded_sprites];

unsigned char activesprites_realx[max_loaded_sprites];
unsigned char activesprites_realy[max_loaded_sprites];
unsigned char activesprites_active[max_loaded_sprites];
uint16_t activesprites_x[max_loaded_sprites];
uint16_t activesprites_y[max_loaded_sprites];
uint8_t activesprites_type[max_loaded_sprites];
uint8_t activesprites_anim_frame[max_loaded_sprites];
int8_t activesprites_anim_frame_count[max_loaded_sprites];

uint8_t activesprites_realx[max_loaded_sprites];
uint8_t activesprites_realy[max_loaded_sprites];
uint8_t activesprites_active[max_loaded_sprites];



Expand Down
23 changes: 12 additions & 11 deletions SAUCE/functions/draw_sprites.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ void draw_sprites(void){
}
index = shuffle_offset;
if (index < 0xF0) {
if (activesprites_type[low_byte(index)] & 0x80) continue;
if (activesprites_type[index] & 0x80) continue;
}

temp_y = activesprites_realy[low_byte(index)];
if (!activesprites_active[low_byte(index)]) continue;
temp_x = activesprites_realx[low_byte(index)];
temp_y = activesprites_realy[index];
if (!activesprites_active[index]) continue;
temp_x = activesprites_realx[index];
if (temp_x == 0) temp_x = 1;
if (temp_x > 0xf0) continue;

Expand All @@ -67,35 +67,36 @@ void draw_sprites(void){
#define needs_reload tmp4
#define animation_ptr tmpptr1
#define animation_data_ptr tmpptr2
if (activesprites_type[low_byte(index)] > 0x80) {}
if (activesprites_type[index] > 0x80) {}
else {
if (temp_y < 0xf0) {
needs_reload = 0;
spr_type = activesprites_type[low_byte(index)];
spr_type = activesprites_type[index];
animation_ptr = (unsigned char * const)animation_frame_list[spr_type];
// If this sprite has animations, then this pointer will not be null
if (animation_ptr) {
// Reduce the frame counter by one to see if we need to move to the next frame
// If this frame has expired, then move to the next animation frame
animation_frame_count = --activesprites_anim_frame_count[low_byte(index)];
animation_frame_count = uint8_dec(activesprites_anim_frame_count, index);
if (animation_frame_count >= 0x80) {
animation_frame = ++activesprites_anim_frame[low_byte(index)];
animation_frame = uint8_inc(activesprites_anim_frame, index);
// if the animation frame is past the length, wrap it around back to zero
if (animation_frame >= animation_frame_length[spr_type]) {
activesprites_anim_frame[low_byte(index)] = animation_frame = 0;
activesprites_anim_frame[index] = 0;
animation_frame = 0;
}
// and then set the animation_frame_count to be reloaded
needs_reload = 1;
} else {
animation_frame = activesprites_anim_frame[low_byte(index)];
animation_frame = activesprites_anim_frame[index];
}

// Now load the data for this frame of animation
// The fastest way to convince cc65 to read all of the data is to force
// it to read all the bytes at once
tmplong = ((unsigned long int*)animation_ptr)[animation_frame];
if (needs_reload) {
activesprites_anim_frame_count[low_byte(index)] = low_byte(tmplong);
uint8_store(activesprites_anim_frame_count, index, low_byte(tmplong));
}
// And finally, load the pointer for this animation
animation_data_ptr = (unsigned char*)high_word(tmplong);
Expand Down
16 changes: 8 additions & 8 deletions SAUCE/functions/sprite_loading.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,15 @@ char sprite_height_lookup(){
}
lastbgcolortype = type;
}
activesprites_type[low_byte(index)] = 0xFF;
activesprites_type[index] = 0xFF;
return 0;
}

else if (type >= COINGOTTEN1 && type <= COINGOTTEN3) return 0x17; // Coin
else if (type >= SPEED_05_PORTAL && type <= SPEED_20_PORTAL) // Speed portals
return 0x1F;
else if ((type >= 0x2A && type <= 0x43) || (type >= 0x47 && type <= 0x4A)) { // Decorations
if (twoplayer || !decorations) activesprites_type[low_byte(index)] = 0xFF;
if (twoplayer || !decorations) activesprites_type[index] = 0xFF;
return 0;
}

Expand Down Expand Up @@ -266,17 +266,17 @@ char sprite_height_lookup(){
return 0x2f;
case COIN1:
if (coin1_obtained[level]) {
activesprites_type[low_byte(index)] = COINGOTTEN1;
activesprites_type[index] = COINGOTTEN1;
}
return 0x17;
case COIN2:
if (coin2_obtained[level]) {
activesprites_type[low_byte(index)] = COINGOTTEN2;
activesprites_type[index] = COINGOTTEN2;
}
return 0x17;
case COIN3:
if (coin3_obtained[level]) {
activesprites_type[low_byte(index)] = COINGOTTEN3;
activesprites_type[index] = COINGOTTEN3;
}
return 0x17;
case SPEED_30_PORTAL:
Expand Down Expand Up @@ -389,7 +389,7 @@ static void sprite_gamemode_main() {

static void sprite_gamemode_controller_check() {
if (pad_new[controllingplayer] & PAD_A) {
cube_data[currplayer] &= 0x01;
uint8_store(cube_data, currplayer, cube_data[currplayer] & 0x01);
if (collided == BLUE_ORB) {
currplayer_gravity ^= 0x01;
currplayer_vel_y = (!currplayer_gravity) ? PAD_HEIGHT_BLUE^0xFFFF : PAD_HEIGHT_BLUE;
Expand Down Expand Up @@ -419,7 +419,7 @@ void sprite_collide_lookup() {
case BIG_SPIKE_TOP:
case SMALL_SPIKE_BOTTOM:
case SMALL_SPIKE_TOP:
cube_data[currplayer] |= 0x01; return;
uint8_store(cube_data, currplayer, cube_data[currplayer] | 0x01); return;

// Portal game mode switches
case S_BLOCK: dashing[currplayer] = 0; return;
Expand All @@ -437,7 +437,7 @@ void sprite_collide_lookup() {
case SHIP_MODE:
gamemode = collided;
robotjumptime[currplayer] = 0;
target_scroll_y = activesprites_y[index];
target_scroll_y = activesprites_y[index & 0x7F];
// return;
// fallthrough
case NOSPRITE:
Expand Down
4 changes: 2 additions & 2 deletions SAUCE/functions/x_movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ void x_movement(){


if (currplayer_y < 0x0600 && scroll_y == 0x08){
cube_data[currplayer] |= 0x01; //DIE if player goes too high
uint8_store(cube_data, currplayer, cube_data[currplayer] | 0x01); //DIE if player goes too high
}


else if (!(pad[controllingplayer] & PAD_A)) cube_data[currplayer] &= 1;
else if (!(pad[controllingplayer] & PAD_A)) uint8_store(cube_data, currplayer, cube_data[currplayer] & 1);
}

#pragma code-name(pop)
Expand Down
10 changes: 5 additions & 5 deletions SAUCE/gamemodes/gamemode_ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void ball_movement(void){
// currplayer_gravity
// currplayer_vel_y is signed

if ((pad_new[controllingplayer] & PAD_A) && currplayer_vel_y != 0) cube_data[currplayer] |= 0x02;
if ((pad_new[controllingplayer] & PAD_A) && currplayer_vel_y != 0) uint8_store(cube_data, currplayer, cube_data[currplayer] | 0x02);

if (!dashing[currplayer]) {
if(!mini){
Expand Down Expand Up @@ -51,20 +51,20 @@ void ball_movement(void){
Generic.y = high_byte(currplayer_y) + 1;
}

if (pad_new[controllingplayer] & PAD_A) cube_data[currplayer] |= 2;
if (pad_new[controllingplayer] & PAD_A) uint8_store(cube_data, currplayer, cube_data[currplayer] | 2);

if(high_byte(currplayer_vel_y) & 0x80){
if(bg_coll_U()){ // check collision above
high_byte(currplayer_y) = high_byte(currplayer_y) - eject_U;
currplayer_vel_y = 0;
cube_data[currplayer] &= 1; //fix for orb
uint8_store(cube_data, currplayer, cube_data[currplayer] & 1); //fix for orb
}
}
else{
if(bg_coll_D()){ // check collision below
high_byte(currplayer_y) = high_byte(currplayer_y) - eject_D;
currplayer_vel_y = 0;
cube_data[currplayer] &= 1; //fix for orb
uint8_store(cube_data, currplayer, cube_data[currplayer] & 1); //fix for orb
}
}

Expand All @@ -82,7 +82,7 @@ void ball_movement(void){
}
if(kandotemp2[currplayer] == 1){
if (!(pad[controllingplayer] & PAD_A)){
cube_data[currplayer] &= 1;
uint8_store(cube_data, currplayer, cube_data[currplayer] & 1);
kandotemp2[currplayer] = 0;
}
}
Expand Down
10 changes: 5 additions & 5 deletions SAUCE/gamemodes/gamemode_cube.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void cube_movement(void){
// currplayer_vel_y is signed
//if(currplayer_vel_y < 0x400){

if ((pad_new[controllingplayer] & PAD_A) && currplayer_vel_y != 0) cube_data[currplayer] |= 0x02;
if ((pad_new[controllingplayer] & PAD_A) && currplayer_vel_y != 0) uint8_store(cube_data, currplayer, cube_data[currplayer] | 0x02);

if (!dashing[currplayer]) {
if(!mini){
Expand Down Expand Up @@ -67,7 +67,7 @@ void cube_movement(void){

if (gamemode == 0 && currplayer_vel_y == 0 && dashing[currplayer] == 0){
//if(bg_coll_D2()) {
cube_data[currplayer] &= 1;
uint8_store(cube_data, currplayer, cube_data[currplayer] & 1);
if(pad[controllingplayer] & PAD_A) {
if (!currplayer_gravity) {
if (!mini) currplayer_vel_y = JUMP_VEL; // JUMP
Expand All @@ -81,7 +81,7 @@ void cube_movement(void){
}
}
else if (gamemode == 4 && !retro_mode && (currplayer_vel_y == 0)){
cube_data[currplayer] &= 1;
uint8_store(cube_data, currplayer, cube_data[currplayer] & 1);
if(pad_new[controllingplayer] & PAD_A) {
if (!currplayer_gravity) {
if (!mini) currplayer_vel_y = ROBOT_JUMP_VEL; // JUMP
Expand All @@ -97,7 +97,7 @@ void cube_movement(void){
}

else if (gamemode == 4 && retro_mode && (currplayer_vel_y == 0)){
cube_data[currplayer] &= 1;
uint8_store(cube_data, currplayer, cube_data[currplayer] & 1);
if(pad[controllingplayer] & PAD_A) {
if (!currplayer_gravity) {
if (!mini) currplayer_vel_y = ROBOT_JUMP_VEL; // JUMP
Expand All @@ -114,7 +114,7 @@ void cube_movement(void){

else if (gamemode == 4 && robotjumptime[currplayer]) {
cube_data[currplayer] = 0;
if (robotjumptime[currplayer]) robotjumptime[currplayer]--;
if (robotjumptime[currplayer]) uint8_dec(robotjumptime, currplayer);
if(pad[controllingplayer] & PAD_A) {
if (robotjumpframe[0]) robotjumpframe[0]++;
if ( robotjumpframe[0] > 3 ) robotjumpframe[0] = 3;
Expand Down
2 changes: 1 addition & 1 deletion SAUCE/gamemodes/gamemode_spider.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void spider_movement(void){

if (currplayer_vel_y != 0){
if(pad_new[controllingplayer] & PAD_A) {
cube_data[currplayer] |= 2;
uint8_store(cube_data, currplayer, cube_data[currplayer] | 0x02);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions SAUCE/gamemodes/gamemode_wave.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ void wave_movement(void){
if(bg_coll_U() && kandowavewalk){ // check collision above
high_byte(currplayer_y) = high_byte(currplayer_y) - eject_U;
currplayer_vel_y = 0;
cube_data[currplayer] &= 1;
uint8_store(cube_data, currplayer, cube_data[currplayer] & 1);
}
}
else{
if(bg_coll_D() && kandowavewalk){ // check collision below
high_byte(currplayer_y) = high_byte(currplayer_y) - eject_D;
currplayer_vel_y = 0;
cube_data[currplayer] &= 1;
uint8_store(cube_data, currplayer, cube_data[currplayer] & 1);
}
}
kandowavewalk = 0;
Expand All @@ -58,7 +58,7 @@ void wave_movement(void){

if (currplayer_vel_y != 0){
if(pad_new[controllingplayer] & PAD_A) {
cube_data[currplayer] |= 2;
uint8_store(cube_data, currplayer, cube_data[currplayer] | 0x02);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion SAUCE/gamestates/state_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void x_movement_coll() {
if (high_byte(currplayer_x) > 0x10) {
bg_coll_floor_spikes(); // check for spikes at the left of the player (fixes standing on spikes)
if (bg_coll_R()) {
if (!(options & platformer)) {cube_data[currplayer] |= 0x01; }
if (!(options & platformer)) {uint8_store(cube_data, currplayer, cube_data[currplayer] | 0x01);}
else {currplayer_vel_x = 0; }
}
}
Expand Down

0 comments on commit 525a88e

Please sign in to comment.