From 723f879f85f25c2d466146e08261e0ae993c72b6 Mon Sep 17 00:00:00 2001 From: Simponic Date: Tue, 11 Aug 2020 22:13:56 -0600 Subject: Added basic camera controls --- source/.toolbox.c.swp | Bin 12288 -> 0 bytes source/camera.c | 15 +++++++++++++++ source/main.c | 44 ++++++++++++++++++++++++++++++-------------- source/map.c | 15 +++------------ source/playerObject.c | 33 ++++++++++++++++----------------- 5 files changed, 64 insertions(+), 43 deletions(-) delete mode 100644 source/.toolbox.c.swp create mode 100644 source/camera.c (limited to 'source') diff --git a/source/.toolbox.c.swp b/source/.toolbox.c.swp deleted file mode 100644 index 6d0cad3..0000000 Binary files a/source/.toolbox.c.swp and /dev/null differ diff --git a/source/camera.c b/source/camera.c new file mode 100644 index 0000000..c905d74 --- /dev/null +++ b/source/camera.c @@ -0,0 +1,15 @@ +#include "../include/camera.h" + +CAMERA createCamera (int x, int y) { + // Create camera from a point + CAMERA temp; + temp.x = x; + temp.y = y; + return temp; +} + +void applyCameraShift (CAMERA *camera, int *x, int *y) { + // Apply camera shift to a coordinate + *x = *x - camera->x; + *y = *y - camera->y; +} diff --git a/source/main.c b/source/main.c index eca5ea2..589db1a 100644 --- a/source/main.c +++ b/source/main.c @@ -8,23 +8,27 @@ #include "../include/velocity.h" #include "../include/playerObject.h" #include "../include/map.h" +#include "../include/camera.h" #include "../sprites/player.h" #include "../sprites/block.h" +#include "../sprites/spike.h" OBJ_ATTR obj_buffer[128]; OBJ_AFFINE *obj_aff_buffer= (OBJ_AFFINE*)obj_buffer; // Object affine-buffer int main() { + memcpy(pal_obj_mem, blockPal, blockPalLen); memcpy(&tile_mem[4][0], playerTiles, playerTilesLen); - memcpy(pal_obj_mem, playerPal, playerPalLen); memcpy(&tile_mem[4][4], blockTiles, blockTilesLen); + memcpy(&tile_mem[4][8], spikeTiles, spikeTilesLen); oam_init(obj_buffer, 128); REG_DISPCNT= DCNT_OBJ | DCNT_OBJ_1D; playerObject player = createPlayerObject(&obj_buffer[0], &obj_aff_buffer[0],0, 0); + player.camera = createCamera(10, 0); while(1) { vid_vsync(); @@ -34,22 +38,34 @@ int main() { player.vel.dy -= 9 << FIX_SHIFT; } - - updatePlayer(&player, 120); + updatePlayer(&player, 80); obj_affine_copy(obj_aff_mem, player.affine, 1); obj_copy(obj_mem, player.obj, 1); - OBJ_ATTR tileObject; - for (int i = 0; i < 7; i++) { - for (int j = 0; j < 12; j++){ - obj_set_attr(&tileObject, - ATTR0_SQUARE, - ATTR1_SIZE_16, - ATTR2_PALBANK(0) | 4 - ); - obj_set_pos(&tileObject, (j * 16), (i * 16)); - if (map1[i][j]) { - obj_copy(obj_mem + (17 * i + j) + 1, &tileObject, 1); + OBJ_ATTR blockObject, spikeObject; + obj_set_attr(&blockObject, + ATTR0_SQUARE, + ATTR1_SIZE_16, + ATTR2_PALBANK(0) | 4 + ); + obj_set_attr(&spikeObject, + ATTR0_SQUARE, + ATTR1_SIZE_16, + ATTR2_PALBANK(0) | 8 + ); + int x, y; + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 15; j++){ + x = (j * 16); + y = (i * 16); + applyCameraShift(&player.camera, &x, &y); + if (map1[i][j] == 1) { + obj_set_pos(&blockObject, x, y); + obj_copy(obj_mem + (17 * i + j) + 1, &blockObject, 1); + } + else if (map1[i][j] == 2) { + obj_set_pos(&spikeObject, x, y); + obj_copy(obj_mem + (17 * i + j) + 1, &spikeObject, 1); } } } diff --git a/source/map.c b/source/map.c index 41048d0..88bf41c 100644 --- a/source/map.c +++ b/source/map.c @@ -1,21 +1,12 @@ #include "../include/map.h" -/* -const int map1[7][20] = { +const int map1[9][20] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, -}; -*/ -const int map1[7][20] = { - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, }; diff --git a/source/playerObject.c b/source/playerObject.c index 3d7ec05..98135a2 100644 --- a/source/playerObject.c +++ b/source/playerObject.c @@ -3,7 +3,7 @@ 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); + object->pt = createPoint(15 << FIX_SHIFT, 0 << FIX_SHIFT); } playerObject createPlayerObject (OBJ_ATTR *obj_buffer, OBJ_AFFINE *affine_buffer, int pallete_bank, int tile_id) { @@ -13,15 +13,15 @@ playerObject createPlayerObject (OBJ_ATTR *obj_buffer, OBJ_AFFINE *affine_buffer initializePlayerObject(&temp); temp.rotation = 0; temp.obj = obj_buffer; - temp.affine = affine_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 + ATTR0_SQUARE | ATTR0_AFF | ATTR0_AFF_DBL_BIT, // Set attribute 1 to be a square using affine and double size + 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; @@ -50,28 +50,27 @@ void rotatePlayer (playerObject *object) { object->affine->pc = sinAlpha; object->affine->pd = cosAlpha; } -u32 roundToNearest90Degrees(u32 rotation) { +void rotateToNearest90Degrees(playerObject *object) { // Round a rotation to the nearest 90 degree equivalent of "binary radians" // (Where 2pi = 0xFFFF and 0pi = 0) + u32 rotation = object->rotation; u32 twoPi = 0xFFFF << 4; u32 pi = 0x8000 << 4; u32 halfPi = 0x4000 << 4; u32 quarterPi = 0x2000 << 4; - if (rotation > quarterPi && rotation < 3 * quarterPi) { - return halfPi; + object->rotation = halfPi; } else if (rotation > 3 * quarterPi && rotation < (pi + quarterPi)) { - return pi; + object->rotation = pi; } else if (rotation > (pi + quarterPi) && rotation < (pi + 3*quarterPi)) { - return pi + halfPi; + object->rotation = pi + halfPi; } else if (rotation < quarterPi || rotation > (twoPi - quarterPi)) { - return 1; + object->rotation = 0; } - return 1; } void updatePlayer (playerObject *object, u32 GROUND_LEVEL) { @@ -82,7 +81,7 @@ void updatePlayer (playerObject *object, u32 GROUND_LEVEL) { object->isJumping = 0; object->pt.y = GROUND_LEVEL << FIX_SHIFT; // Don't go through ground object->vel.dy = 0; - object->rotation = roundToNearest90Degrees(object->rotation); + rotateToNearest90Degrees(object); } else { object->isJumping = 1; @@ -90,9 +89,9 @@ void updatePlayer (playerObject *object, u32 GROUND_LEVEL) { 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 object's attributes' position and subtract 8 pixels in + // both axes because the sprite is using the DBL_BIT for attr0 + obj_set_pos(object->obj, (object->pt.x >> FIX_SHIFT) - 8, (object->pt.y >> FIX_SHIFT) - 9); // Update the player's second attribute (tile and pallete bank) object->obj->attr2 = ATTR2_BUILD(object->tile_id, object->pallete_bank, 0); } - -- cgit v1.2.3-70-g09d2