diff --git a/gba/cartest/src/cartest.cpp b/gba/cartest/src/cartest.cpp index 8823845..998e3f3 100644 --- a/gba/cartest/src/cartest.cpp +++ b/gba/cartest/src/cartest.cpp @@ -32,10 +32,8 @@ typedef s32 FIXED; // 32bit FIXED in 24.8 format #define NUMSMOKES 30 -u16 PaletteBuffer[256]; OBJATTR oe_buffer[2+NUMSMOKES+10]; OBJAFFINE *const oa_buffer = (OBJAFFINE*)oe_buffer; -s8 smoke_frame[NUMSMOKES]; unsigned int frame; @@ -45,21 +43,122 @@ void VblankInterrupt() ScanKeys(); } -typedef struct { +class Smoke { + OBJATTR* oam; + int num_entries; +public: + s8* smoke_frame; + u8 next_smoke; + Smoke(int num_entries, OBJATTR* start) { + oam = start; + smoke_frame = new s8[num_entries]; + next_smoke = 0; + this->num_entries = num_entries; + for (u16 i=0; i=num_entries) { + next_smoke = 0; + } + } + + void update_sprites() { + for (u8 i=0; ix = INT2FIX(x); + this->y = INT2FIX(y); + speed = 0; + angle = 0; + aff_matrix = aff; + oam = entry; + index = aff_index; + oam->attr2 = OBJ_CHAR(sprite_index); + } + void rotate(int alpha) { + angle += alpha; + if (angle < 0) { + angle += 512; + } else if (angle >= 512) { + angle -= 512; + } + } + void speedup() { + if (speed<30) { + speed += 1; + } + } + void slowdown() { + if (speed>0) { + speed -= 1; + } + } + + void update_position() { + x -= (speed >> 3)*lut_sin(angle); + y -= (speed >> 3)*lut_cos(angle); + + if (x>=INT2FIX(232)) { + x -= INT2FIX(256); + } else if (x=INT2FIX(152)) { + y -= INT2FIX(176); + } else if (ypa = lut_cos(angle); + aff_matrix->pb = -lut_sin(angle); + aff_matrix->pc = lut_sin(angle); + aff_matrix->pd = lut_cos(angle); + } + + void update_oam() { + oam->attr1 = OBJ_SIZE(1) | OBJ_ROT_SCALE(index) | OBJ_X(FIX2INT(x)); + oam->attr0 = OBJ_256_COLOR | OBJ_DOUBLE | OBJ_ROT_SCALE_ON | OBJ_Y(FIX2INT(y)); + } + FIXED x, y; int speed, angle; -} Car; - +}; int main(void) { - OBJATTR*red_car = &oe_buffer[0]; - OBJATTR*blue_car = &oe_buffer[1]; - OBJAFFINE*red_car_aff = &oa_buffer[0]; - OBJAFFINE*blue_car_aff = &oa_buffer[1]; - // We'll make a few smoke clouds - OBJATTR*smokes = &oe_buffer[2]; - // Set up the interrupt handlers InitInterrupt(); @@ -90,178 +189,60 @@ int main(void) CpuFastSet(smoke_bin, (void*)free_space, COPY32 | smoke_bin_size/4); free_space += smoke_bin_size; - red_car->attr2 = OBJ_CHAR(512); - blue_car->attr2 = OBJ_CHAR(520); - - u8 next_smoke = 0; - - u8 i; - for (i=0; i= 512) { - red.angle -= 512; - } - if (KeysHeld() & KEY_A) { - blue.angle -= 8; + blue.rotate(-8); } else if (KeysHeld() & KEY_B) { - blue.angle += 8; - } - if (blue.angle < 0) { - blue.angle += 512; - } else if (blue.angle >= 512) { - blue.angle -= 512; + blue.rotate(8); } if (KeysHeld() & KEY_L) { - if ((red.speed<30) && (red.speed>=0)) { - // slow speedup - red.speed += 1; - } else if (red.speed<0) { - // break if we're going backward - red.speed += 2; - } + red.speedup(); } if (KeysHeld() & KEY_R) { - if ((blue.speed<30) && (blue.speed>=0)) { - // slow speedup - blue.speed += 1; - } else if (blue.speed<0) { - // break if we're going backward - blue.speed += 2; - } + blue.speedup(); } - - /* Disabled */ -/* if (KeysHeld() & KEY_B) { */ -/* if ((red.speed>=-5) && (red.speed<=0)) { */ -/* // slow speedup */ -/* red.speed -= 1; */ -/* } else if (red.speed>0) { */ -/* // break if we're going forward */ -/* red.speed -= 2; */ -/* } */ -/* } */ - - // slow down if we're not doing anything if (!(KeysHeld() & KEY_L)) { - if (red.speed>0) { - red.speed -= 1; - } else if (red.speed<0) { - red.speed += 1; - } + red.slowdown(); } - if (!(KeysHeld() & KEY_R)) { - if (blue.speed>0) { - blue.speed -= 1; - } else if (blue.speed<0) { - blue.speed += 1; - } + blue.slowdown(); } - red.x -= (red.speed >> 3)*lut_sin(red.angle); - red.y -= (red.speed >> 3)*lut_cos(red.angle); + red.update_position(); + red.update_matrix(); + red.update_oam(); - if (red.x>=INT2FIX(232)) { - red.x -= INT2FIX(256); - } else if (red.x=INT2FIX(152)) { - red.y -= INT2FIX(176); - } else if (red.ypa = lut_cos(red.angle); - red_car_aff->pb = -lut_sin(red.angle); - red_car_aff->pc = lut_sin(red.angle); - red_car_aff->pd = lut_cos(red.angle); - - red_car->attr1 = OBJ_SIZE(1) | OBJ_X(FIX2INT(red.x)); - red_car->attr0 = OBJ_256_COLOR | OBJ_DOUBLE | OBJ_ROT_SCALE_ON | OBJ_Y(FIX2INT(red.y)); - - blue.x -= (blue.speed >> 3)*lut_sin(blue.angle); - blue.y -= (blue.speed >> 3)*lut_cos(blue.angle); - - if (blue.x>=INT2FIX(232)) { - blue.x -= INT2FIX(256); - } else if (blue.x=INT2FIX(152)) { - blue.y -= INT2FIX(176); - } else if (blue.ypa = lut_cos(blue.angle); - blue_car_aff->pb = -lut_sin(blue.angle); - blue_car_aff->pc = lut_sin(blue.angle); - blue_car_aff->pd = lut_cos(blue.angle); - - blue_car->attr1 = OBJ_SIZE(1) | OBJ_ROT_SCALE(1) | OBJ_X(FIX2INT(blue.x)); - blue_car->attr0 = OBJ_256_COLOR | OBJ_DOUBLE | OBJ_ROT_SCALE_ON | OBJ_Y(FIX2INT(blue.y)); - - // Update smoke cloud frames if ((frame & 3) == 3) { - for (i=0; i=NUMSMOKES) { - next_smoke = 0; - } - for (i=0; i