diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/.toolbox.c.swp (renamed from source/.main.c.swp) | bin | 12288 -> 12288 bytes | |||
-rw-r--r-- | source/main.c | 30 | ||||
-rw-r--r-- | source/playerObject.c | 98 | ||||
-rw-r--r-- | source/sinlut.c | 73 | ||||
-rw-r--r-- | source/toolbox.c | 82 |
5 files changed, 257 insertions, 26 deletions
diff --git a/source/.main.c.swp b/source/.toolbox.c.swp Binary files differindex b3f5f6d..6d0cad3 100644 --- a/source/.main.c.swp +++ b/source/.toolbox.c.swp diff --git a/source/main.c b/source/main.c index ae0cf05..d0a60b1 100644 --- a/source/main.c +++ b/source/main.c @@ -13,28 +13,6 @@ OBJ_ATTR obj_buffer[128]; OBJ_AFFINE *obj_aff_buffer= (OBJ_AFFINE*)obj_buffer; // Object affine-buffer -void obj_test() { - int x, y; - x = 10; y = 10; - u32 tid= 0, pb= 0; - OBJ_ATTR *player = &obj_buffer[0]; - - obj_set_attr(player, - ATTR0_SQUARE, - ATTR1_SIZE_16, - ATTR2_PALBANK(pb) | tid - ); - - obj_set_pos(player, x, y); - - while(1) { - vid_vsync(); - key_poll(); - - oam_copy(oam_mem, obj_buffer, 1); - } -} - int main() { memcpy(&tile_mem[4][0], playerTiles, playerTilesLen); memcpy(pal_obj_mem, playerPal, playerPalLen); @@ -43,8 +21,7 @@ int main() { REG_DISPCNT= DCNT_OBJ | DCNT_OBJ_1D; - playerObject player = createPlayerObject(&obj_buffer[0], 0, 0); - + playerObject player = createPlayerObject(&obj_buffer[0], &obj_aff_buffer[0],0, 0); while(1) { vid_vsync(); @@ -54,8 +31,9 @@ int main() { player.vel.dy -= 9 << FIX_SHIFT; } - updatePlayer(&player, 140); - oam_copy(oam_mem, obj_buffer, 1); + updatePlayer(&player, 120); + obj_affine_copy(obj_aff_mem, player.affine, 1); + obj_copy(obj_mem, player.obj, 1); } return 0; diff --git a/source/playerObject.c b/source/playerObject.c new file mode 100644 index 0000000..3d7ec05 --- /dev/null +++ b/source/playerObject.c @@ -0,0 +1,98 @@ +#include "../include/playerObject.h" + +void initializePlayerObject (playerObject *object) { + // Initialize the point and velocity of a player object + object->vel = createVelocity(0, 0); + object->pt = createPoint(20 << FIX_SHIFT, 100 << FIX_SHIFT); +} + +playerObject createPlayerObject (OBJ_ATTR *obj_buffer, OBJ_AFFINE *affine_buffer, int pallete_bank, int tile_id) { + // Create a player object from data + obj_affine_identity(affine_buffer); + playerObject temp; + initializePlayerObject(&temp); + temp.rotation = 0; + temp.obj = obj_buffer; + temp.affine = affine_buffer; + temp.pallete_bank = pallete_bank; + temp.tile_id = tile_id; + temp.isJumping = 0; + obj_set_attr(temp.obj, + ATTR0_SQUARE | ATTR0_AFF | ATTR0_AFF_DBL_BIT, // Set attribute 1 to be a square using affine + ATTR1_SIZE_16 | ATTR1_AFF_ID(0), // Set size to 16 x 16 + ATTR2_PALBANK(pallete_bank) | tile_id // Which pallete to use, as we are in + // 16-color mode + ); + obj_set_pos(temp.obj, temp.pt.x >> FIX_SHIFT, temp.pt.y >> FIX_SHIFT); + return temp; +} + +void applyGravity (playerObject *object) { + // Apply a gravity constant to a player + object->vel.dy += float_to_fixed(GRAVITY); + // This is a weird convention to have gravity in the positive direction, + // but I don't want to deal with coordinate changing. Too lazy +} + +void scalePlayer (playerObject *object, u32 scaleX, u32 scaleY) { + // Scale the player's sprite + // Maximum 0.5 + object->affine->pa = scaleX; object->affine->pb = 0 ; + object->affine->pc = 0 ; object->affine->pd = scaleY; +} + +void rotatePlayer (playerObject *object) { + // Rotate the player's sprite + int sinAlpha = lu_sin(object->rotation >> 4) >> 4; + int cosAlpha = lu_cos(object->rotation >> 4) >> 4; + + object->affine->pa = cosAlpha; object->affine->pb =-sinAlpha; + object->affine->pc = sinAlpha; object->affine->pd = cosAlpha; +} + +u32 roundToNearest90Degrees(u32 rotation) { + // Round a rotation to the nearest 90 degree equivalent of "binary radians" + // (Where 2pi = 0xFFFF and 0pi = 0) + u32 twoPi = 0xFFFF << 4; + u32 pi = 0x8000 << 4; + u32 halfPi = 0x4000 << 4; + u32 quarterPi = 0x2000 << 4; + + + if (rotation > quarterPi && rotation < 3 * quarterPi) { + return halfPi; + } + else if (rotation > 3 * quarterPi && rotation < (pi + quarterPi)) { + return pi; + } + else if (rotation > (pi + quarterPi) && rotation < (pi + 3*quarterPi)) { + return pi + halfPi; + } + else if (rotation < quarterPi || rotation > (twoPi - quarterPi)) { + return 1; + } + return 1; +} + +void updatePlayer (playerObject *object, u32 GROUND_LEVEL) { + // Update the position of a player object + updatePoint(&object->pt, &object->vel); + if (object->pt.y >> FIX_SHIFT >= GROUND_LEVEL) { + // Only apply gravity if player is not touching the ground + object->isJumping = 0; + object->pt.y = GROUND_LEVEL << FIX_SHIFT; // Don't go through ground + object->vel.dy = 0; + object->rotation = roundToNearest90Degrees(object->rotation); + } + else { + object->isJumping = 1; + applyGravity(object); + object->rotation += 100 << 8; + } + rotatePlayer(object); + // Update the player object's attributes' position + obj_set_pos(object->obj, object->pt.x >> FIX_SHIFT, object->pt.y >> FIX_SHIFT); + // Update the player's second attribute (tile and pallete bank) + object->obj->attr2 = ATTR2_BUILD(object->tile_id, object->pallete_bank, 0); +} + diff --git a/source/sinlut.c b/source/sinlut.c new file mode 100644 index 0000000..e380a3e --- /dev/null +++ b/source/sinlut.c @@ -0,0 +1,73 @@ +#include "../include/sinlut.h" +// +// Sine lut; 512 entries, 12 fixeds +// + + +const short sin_lut[512]= +{ + 0x0000, 0x0032, 0x0064, 0x0096, 0x00C8, 0x00FB, 0x012D, 0x015F, + 0x0191, 0x01C3, 0x01F5, 0x0227, 0x0259, 0x028A, 0x02BC, 0x02ED, + 0x031F, 0x0350, 0x0381, 0x03B2, 0x03E3, 0x0413, 0x0444, 0x0474, + 0x04A5, 0x04D5, 0x0504, 0x0534, 0x0563, 0x0593, 0x05C2, 0x05F0, + 0x061F, 0x064D, 0x067B, 0x06A9, 0x06D7, 0x0704, 0x0731, 0x075E, + 0x078A, 0x07B7, 0x07E2, 0x080E,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, 0x0D31, 0x0D14, 0x0CF7, 0x0CD9, 0x0CBB, 0x0C9D, 0x0C7D, + 0x0C5E, 0x0C3E, 0x0C1D, 0x0BFC, 0x0BDA, 0x0BB8, 0x0B96, 0x0B73, + 0x0B50, 0x0B2C, 0x0B08, 0x0AE3, 0x0ABE, 0x0A99, 0x0A73, 0x0A4D, + 0x0A26, 0x09FF, 0x09D7, 0x09B0, 0x0987, 0x095F, 0x0936, 0x090D, + 0x08E3, 0x08B9, 0x088F, 0x0864, 0x0839, 0x080E, 0x07E2, 0x07B7, + 0x078A, 0x075E, 0x0731, 0x0704, 0x06D7, 0x06A9, 0x067B, 0x064D, + 0x061F, 0x05F0, 0x05C2, 0x0593, 0x0563, 0x0534, 0x0504, 0x04D5, + 0x04A5, 0x0474, 0x0444, 0x0413, 0x03E3, 0x03B2, 0x0381, 0x0350, + 0x031F, 0x02ED, 0x02BC, 0x028A, 0x0259, 0x0227, 0x01F5, 0x01C3, + 0x0191, 0x015F, 0x012D, 0x00FB, 0x00C8, 0x0096, 0x0064, 0x0032, + 0x0000, 0xFFCE, 0xFF9C, 0xFF6A, 0xFF38, 0xFF05, 0xFED3, 0xFEA1, + 0xFE6F, 0xFE3D, 0xFE0B, 0xFDD9, 0xFDA7, 0xFD76, 0xFD44, 0xFD13, + 0xFCE1, 0xFCB0, 0xFC7F, 0xFC4E, 0xFC1D, 0xFBED, 0xFBBC, 0xFB8C, + 0xFB5B, 0xFB2B, 0xFAFC, 0xFACC, 0xFA9D, 0xFA6D, 0xFA3E, 0xFA10, + 0xF9E1, 0xF9B3, 0xF985, 0xF957, 0xF929, 0xF8FC, 0xF8CF, 0xF8A2, + 0xF876, 0xF849, 0xF81E, 0xF7F2, 0xF7C7, 0xF79C, 0xF771, 0xF747, + 0xF71D, 0xF6F3, 0xF6CA, 0xF6A1, 0xF679, 0xF650, 0xF629, 0xF601, + 0xF5DA, 0xF5B3, 0xF58D, 0xF567, 0xF542, 0xF51D, 0xF4F8, 0xF4D4, + 0xF4B0, 0xF48D, 0xF46A, 0xF448, 0xF426, 0xF404, 0xF3E3, 0xF3C2, + 0xF3A2, 0xF383, 0xF363, 0xF345, 0xF327, 0xF309, 0xF2EC, 0xF2CF, + 0xF2B3, 0xF297, 0xF27C, 0xF261, 0xF247, 0xF22E, 0xF215, 0xF1FC, + 0xF1E4, 0xF1CD, 0xF1B6, 0xF1A0, 0xF18A, 0xF175, 0xF160, 0xF14C, + 0xF138, 0xF125, 0xF113, 0xF101, 0xF0F0, 0xF0DF, 0xF0CF, 0xF0C0, + 0xF0B1, 0xF0A3, 0xF095, 0xF088, 0xF07B, 0xF06F, 0xF064, 0xF059, + 0xF04F, 0xF046, 0xF03D, 0xF035, 0xF02D, 0xF026, 0xF01F, 0xF019, + 0xF014, 0xF010, 0xF00C, 0xF008, 0xF005, 0xF003, 0xF002, 0xF001, + 0xF001, 0xF001, 0xF002, 0xF003, 0xF005, 0xF008, 0xF00C, 0xF010, + 0xF014, 0xF019, 0xF01F, 0xF026, 0xF02D, 0xF035, 0xF03D, 0xF046, + 0xF04F, 0xF059, 0xF064, 0xF06F, 0xF07B, 0xF088, 0xF095, 0xF0A3, + 0xF0B1, 0xF0C0, 0xF0CF, 0xF0DF, 0xF0F0, 0xF101, 0xF113, 0xF125, + 0xF138, 0xF14C, 0xF160, 0xF175, 0xF18A, 0xF1A0, 0xF1B6, 0xF1CD, + 0xF1E4, 0xF1FC, 0xF215, 0xF22E, 0xF247, 0xF261, 0xF27C, 0xF297, + 0xF2B3, 0xF2CF, 0xF2EC, 0xF309, 0xF327, 0xF345, 0xF363, 0xF383, + 0xF3A2, 0xF3C2, 0xF3E3, 0xF404, 0xF426, 0xF448, 0xF46A, 0xF48D, + 0xF4B0, 0xF4D4, 0xF4F8, 0xF51D, 0xF542, 0xF567, 0xF58D, 0xF5B3, + 0xF5DA, 0xF601, 0xF629, 0xF650, 0xF679, 0xF6A1, 0xF6CA, 0xF6F3, + 0xF71D, 0xF747, 0xF771, 0xF79C, 0xF7C7, 0xF7F2, 0xF81E, 0xF849, + 0xF876, 0xF8A2, 0xF8CF, 0xF8FC, 0xF929, 0xF957, 0xF985, 0xF9B3, + 0xF9E1, 0xFA10, 0xFA3E, 0xFA6D, 0xFA9D, 0xFACC, 0xFAFC, 0xFB2B, + 0xFB5B, 0xFB8C, 0xFBBC, 0xFBED, 0xFC1D, 0xFC4E, 0xFC7F, 0xFCB0, + 0xFCE1, 0xFD13, 0xFD44, 0xFD76, 0xFDA7, 0xFDD9, 0xFE0B, 0xFE3D, + 0xFE6F, 0xFEA1, 0xFED3, 0xFF05, 0xFF38, 0xFF6A, 0xFF9C, 0xFFCE, +}; diff --git a/source/toolbox.c b/source/toolbox.c new file mode 100644 index 0000000..32042a0 --- /dev/null +++ b/source/toolbox.c @@ -0,0 +1,82 @@ +// +// toolbox.c +// +// Tools source for obj_demo +// +// (20060922-20060924, cearn) +// +// === NOTES === +// * This is a _small_ set of typedefs, #defines and inlines that can +// be found in tonclib, and might not represent the +// final forms. + +#include "../include/toolbox.h" + +// === (tonc_core.c) ================================================== + +u16 __key_curr= 0, __key_prev= 0; + +// === (tonc_oam.c) =================================================== + +void oam_init(OBJ_ATTR *obj, u32 count) +{ + u32 nn= count; + u32 *dst= (u32*)obj; + + // Hide each object + while(nn--) + { + *dst++= ATTR0_HIDE; + *dst++= 0; + } + // init oam + oam_copy(oam_mem, obj, count); +} + +void oam_copy(OBJ_ATTR *dst, const OBJ_ATTR *src, u32 count) +{ + +// NOTE: while struct-copying is the Right Thing to do here, +// there's a strange bug in DKP that sometimes makes it not work +// If you see problems, just use the word-copy version. +#if 1 + while(count--) + *dst++ = *src++; +#else + u32 *dstw= (u32*)dst, *srcw= (u32*)src; + while(count--) + { + *dstw++ = *srcw++; + *dstw++ = *srcw++; + } +#endif + +} + +void obj_copy(OBJ_ATTR *dst, const OBJ_ATTR *src, u32 count) +{ + int ii; + for(ii=0; ii<count; ii++) + { + dst[ii].attr0= src[ii].attr0; + dst[ii].attr1= src[ii].attr1; + dst[ii].attr2= src[ii].attr2; + } +} + +void obj_affine_copy (OBJ_AFFINE *dst, const OBJ_AFFINE *src, u32 count) { + // Copy affine attributes from one pointer to another + for (int i = 0; i < count; i++) { + dst->pa = src->pa; dst->pb = src->pb; + dst->pc = src->pc; dst->pd = src->pd; + + dst++; + src++; + } +} + +void obj_affine_identity(OBJ_AFFINE *aff) { + // Make a pointer point to a identity affine matrix + aff->pa = 1 << 8; aff->pb = 0; + aff->pc = 0 ; aff->pd = 1 << 8; +} |