Skip to content

Commit

Permalink
Add option to select track in single race
Browse files Browse the repository at this point in the history
  • Loading branch information
chickendude committed Oct 20, 2021
1 parent 3b71f3c commit 71d00e0
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 49 deletions.
2 changes: 2 additions & 0 deletions include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#define NUM_CARS 7

#define NUM_TRACKS 2

#define SPRITE(n) ((n) * 4)

#endif //LAUGURPIL_CONSTANTS_H
6 changes: 6 additions & 0 deletions include/race.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ struct Race
OBJ_ATTR obj_buffer[128];
};

struct RaceData
{
RacecarData *car_data;
const Track *track;
};

extern State race_state;


Expand Down
3 changes: 2 additions & 1 deletion include/racecar.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ struct Racecar {
};

struct RacecarData {
int sprite_id;
int max_speed;
int turning_power, acceleration_power;
};

void load_car(Race *race, int car_id);
void load_car(Race *race, RacecarData *car_data);
void move_car(Race *race);

extern const RacecarData *cars[7];
Expand Down
3 changes: 3 additions & 0 deletions include/track.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef TEST_TRACK_H
#define TEST_TRACK_H

#include "constants.h"
#include "types.h"

struct Track
Expand Down Expand Up @@ -28,6 +29,8 @@ void update_tilemap(Race *race);
*/
int is_car_in_finish_line(Racecar *car, const Track *track);

extern const Track* tracks[NUM_TRACKS];

extern const Track track_1, track_2;

#endif //TEST_TRACK_H
1 change: 1 addition & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ typedef struct Camera Camera;
typedef struct Race Race;
typedef struct Racecar Racecar;
typedef struct RacecarData RacecarData;
typedef struct RaceData RaceData;
typedef struct ReturnParam ReturnParam;
typedef struct State State;
typedef struct StateStack StateStack;
Expand Down
26 changes: 15 additions & 11 deletions source/race.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@ static void initialize(StateType prev_state, void *parameter)

prepare_text(3, 29);

int car_id = *((int *) parameter);
load_car(&race, car_id);
load_track(&track_1, &race.camera);
RaceData *race_data = (RaceData *) parameter;
load_car(&race, race_data->car_data);
load_track(race_data->track, &race.camera);
load_timer(&race.timer);
print_time(se_mem[29], 1, 1, 0, 0, 0);

// Car sprite/affine info
obj_set_attr(race.obj_buffer,
ATTR0_SQUARE | ATTR0_4BPP | ATTR0_AFF | ATTR0_AFF_DBL_BIT,
ATTR1_SIZE_16x16 | ATTR1_AFF_ID(0),
ATTR2_PRIO(1) | ATTR2_PALBANK(car_id) | ATTR2_ID(car_id * 4));
ATTR2_PRIO(1) | ATTR2_PALBANK(race_data->car_data->sprite_id) |
ATTR2_ID(race_data->car_data->sprite_id * 4));

obj_aff_identity((OBJ_AFFINE *) &race.obj_buffer[0]);

Expand All @@ -80,7 +81,7 @@ static void initialize(StateType prev_state, void *parameter)
ATTR1_SIZE_16x16 | 200,
ATTR2_PALBANK(7) | 32);

race.track = &track_1;
race.track = race_data->track;
race.laps = 0;
race.laps_total = 3;
race.laps_remaining = 0;
Expand All @@ -91,17 +92,18 @@ static void initialize(StateType prev_state, void *parameter)

race.countdown = 60 * 3;
// * 16 (aka << 4) then shift left 12 because of the 12 point fixed point
race.car->x = track_1.start_x << 16;
race.car->y = track_1.start_y << 16;
race.car->angle = track_1.start_angle;
race.car->x = race.track->start_x << 16;
race.car->y = race.track->start_y << 16;
race.car->angle = race.track->start_angle;
race.car->slide_x = lu_sin(race.car->angle);
race.car->slide_y = lu_cos(race.car->angle);
obj_set_pos(race.car->oam,
(race.car->x >> 12) - 8 - race.camera.x,
(race.car->y >> 12) - 8 - race.camera.y);

vid_vsync();
REG_DISPCNT = DCNT_MODE0 | DCNT_BG0 | DCNT_BG1 | DCNT_BG3 | DCNT_OBJ | DCNT_OBJ_1D;
REG_DISPCNT = DCNT_MODE0 | DCNT_BG0 | DCNT_BG1 | DCNT_BG3 | DCNT_OBJ |
DCNT_OBJ_1D;

REG_BG0HOFS = race.camera.x;
REG_BG1HOFS = race.camera.x;
Expand Down Expand Up @@ -156,8 +158,10 @@ void update()
print_speed(se_mem[29], 10, 1, race.car->speed);

// Update timer every other frame
if (race.timer.frames & 2) {
print_time(se_mem[29], 1, 1, race.timer.minutes, race.timer.seconds, race.timer.millis);
if (race.timer.frames & 2)
{
print_time(se_mem[29], 1, 1, race.timer.minutes, race.timer.seconds,
race.timer.millis);
}

REG_BG0HOFS = race.camera.x;
Expand Down
6 changes: 3 additions & 3 deletions source/race_single.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "racecar_select.h"
#include "state.h"

int selected_car;
RaceData *race_data;

StateType prev_state;

Expand Down Expand Up @@ -39,7 +39,7 @@ void initialize(StateType ls, void *parameter)

if (prev_state == TRACK_SELECT)
{
selected_car = *((int *) parameter);
race_data = (RaceData *) parameter;
}
}

Expand All @@ -56,7 +56,7 @@ void input(StateStack *state_stack)
push_state(state_stack, &racecar_select_state, RACE_SINGLE, NULL);
break;
case TRACK_SELECT:
push_state(state_stack, &race_state, RACE_SINGLE, &selected_car);
push_state(state_stack, &race_state, RACE_SINGLE, race_data);
break;
default:
pop_state(state_stack, RACE_SINGLE, NULL);
Expand Down
28 changes: 14 additions & 14 deletions source/racecar.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#include <malloc.h>
#include <tonc.h>

#include "race.h"
#include "racecar.h"
#include "constants.h"
#include "race.h"
#include "track.h"

const RacecarData car1 = {7, 7, 7};
const RacecarData car2 = {8, 6, 7};
const RacecarData car3 = {5, 8, 8};
const RacecarData car4 = {8, 8, 5};
const RacecarData car5 = {2, 10, 9};
const RacecarData car6 = {5, 7, 9};
const RacecarData car7 = {10, 2, 9};
const RacecarData car1 = {0, 7, 7, 7};
const RacecarData car2 = {1, 8, 6, 7};
const RacecarData car3 = {2, 5, 8, 8};
const RacecarData car4 = {3, 8, 8, 5};
const RacecarData car5 = {4, 2, 10, 9};
const RacecarData car6 = {5, 5, 7, 9};
const RacecarData car7 = {6, 10, 2, 9};

const RacecarData *cars[7] = {&car1, &car2, &car3, &car4, &car5, &car6, &car7};
const RacecarData *cars[NUM_CARS] = {&car1, &car2, &car3, &car4, &car5, &car6, &car7};

// -----------------------------------------------------------------------------
// Private function declarations
Expand All @@ -29,7 +30,7 @@ void slow_down(Racecar *car);
// Public function definitions
// -----------------------------------------------------------------------------

void load_car(Race *race, int car_id)
void load_car(Race *race, RacecarData *car_data)
{
Racecar *car = race->car;

Expand All @@ -41,10 +42,9 @@ void load_car(Race *race, int car_id)
}

// Load chosen car's data
const RacecarData *data = cars[car_id];
car->max_speed = 0x3A00 + data->max_speed * 0x0100;
car->turning_power = 0xC0 + data->turning_power * 0x05;
car->acceleration_power = 0x15 + data->acceleration_power * 0x05;
car->max_speed = 0x3A00 + car_data->max_speed * 0x0100;
car->turning_power = 0xC0 + car_data->turning_power * 0x05;
car->acceleration_power = 0x15 + car_data->acceleration_power * 0x05;

// Load car defaults
car->speed = 0;
Expand Down
9 changes: 5 additions & 4 deletions source/racecar_select.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <tonc.h>
#include "racecar_select.h"
#include "constants.h"
#include "racecar.h"
#include "state.h"
#include "text.h"
Expand Down Expand Up @@ -50,7 +51,7 @@ void initialize(StateType _ls, void *_)

prepare_text(3, 29);

for (int i = 0; i < 7; i++)
for (int i = 0; i < NUM_CARS; i++)
{
obj_set_attr(&obj_mem[i],
ATTR0_4BPP | ATTR0_SQUARE | ATTR0_AFF | ATTR0_AFF_DBL_BIT |
Expand Down Expand Up @@ -85,12 +86,12 @@ void input(StateStack *state_stack)
if (key_hit(KEY_RIGHT))
{
selected_car_index++;
if (selected_car_index == 7) selected_car_index = 0;
if (selected_car_index == NUM_CARS) selected_car_index = 0;
update_stats();
} else if (key_hit(KEY_LEFT))
{
selected_car_index--;
if (selected_car_index < 0) selected_car_index = 6;
if (selected_car_index < 0) selected_car_index = NUM_CARS - 1;
update_stats();
}

Expand All @@ -104,7 +105,7 @@ void input(StateStack *state_stack)
if (key_hit(KEY_A))
{
push_state(state_stack, &track_select_state, RACECAR_SELECT,
&selected_car_index);
cars[selected_car_index]);
}
}

Expand Down
5 changes: 3 additions & 2 deletions source/track.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
// tilemap data
#include "tracks.h"

const Track* tracks[2] = {&track_1, &track_2};

const Track track_2 = {
const Track track_1 = {
18, 1,
0x4000,
track1Width, track1Height,
Expand All @@ -19,7 +20,7 @@ const Track track_2 = {
track1_tilemap
};

const Track track_1 = {
const Track track_2 = {
7, 24,
0x8000,
track2Width, track2Height,
Expand Down
41 changes: 27 additions & 14 deletions source/track_select.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <tonc.h>
#include "track_select.h"
#include "race.h"
#include "state.h"
#include "text.h"
#include "track.h"

int racecar_id;
static RaceData race_data;

int selected_track_index;

Expand Down Expand Up @@ -36,7 +37,12 @@ State track_select_state = {
// -----------------------------------------------------------------------------
void initialize(StateType _prev_state, void *parameter)
{
racecar_id = *(int *) parameter;
// Save car passed in from [racecar_select]
race_data.car_data = (RacecarData *) parameter;
race_data.track = tracks[0];

selected_track_index = 0;

// Disable display until we're ready
REG_DISPCNT = 0;
oam_init(obj_mem, 128);
Expand Down Expand Up @@ -73,29 +79,35 @@ void input(StateStack *state_stack)
if (key_hit(KEY_RIGHT))
{
selected_track_index++;
if (selected_track_index == 7) selected_track_index = 0;
if (selected_track_index == NUM_TRACKS) selected_track_index = 0;
race_data.track = tracks[selected_track_index];
build_track_sprites();
} else if (key_hit(KEY_LEFT))
{
selected_track_index--;
if (selected_track_index < 0) selected_track_index = 6;
if (selected_track_index < 0) selected_track_index = NUM_TRACKS - 1;
race_data.track = tracks[selected_track_index];
build_track_sprites();
}

if (key_hit(KEY_B))
{
pop_state(state_stack, TRACK_SELECT, &racecar_id);
pop_state(state_stack, TRACK_SELECT, race_data.car_data);
} else if (key_hit(KEY_A))
{
pop_state(state_stack, TRACK_SELECT, &racecar_id);
pop_state(state_stack, TRACK_SELECT, &racecar_id);
pop_state(state_stack, TRACK_SELECT, NULL);
pop_state(state_stack, TRACK_SELECT, &race_data);
}
}

// -----------------------------------------------------------------------------
// Private functions definitions
// -----------------------------------------------------------------------------
static int grab_tile_id(int x, int y)
static int grab_tile_id(const int x, const int y)
{
int id = track_1.tilemap[y * track_1.width + x];
const Track *track = race_data.track;

int id = track->tilemap[y * track->width + x];
if (id == 0) return 0;
if (id == 9 || id == 2 || id == 17 || (id >= 22 && id <= 24)) return 3;
if (id == 64) return 2;
Expand All @@ -104,8 +116,10 @@ static int grab_tile_id(int x, int y)

static void build_track_sprites()
{
const Track *track = race_data.track;

TILE4 clear_tile = {{0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000}};
0x00000000, 0x00000000, 0x00000000, 0x00000000}};

// Clear out tile memory data in case a track is less than 64x64 tiles
for (int i = 0; i < 8; i++)
Expand All @@ -118,11 +132,10 @@ static void build_track_sprites()

// Go through the tiles 8 at a time and pack them into a 4bpp sprite (one
// pixel per tile)
for (int y = 0; y < track_1.height; y++)
for (int y = 0; y < track->height; y++)
{
for (int x = 0; x < track_1.width; x += 8)
for (int x = 0; x < track->width; x += 8)
{

int t1 = grab_tile_id(x, y);
int t2 = grab_tile_id(x + 1, y);
int t3 = grab_tile_id(x + 2, y);
Expand All @@ -138,6 +151,6 @@ static void build_track_sprites()
}

// Center the sprite horizontally based on the width of the track
int x = SCREEN_WIDTH / 2 - track_1.width / 2;
int x = SCREEN_WIDTH / 2 - track->width / 2;
obj_mem[0].attr1 = ATTR1_SIZE_64x64 | x;
}

0 comments on commit 71d00e0

Please sign in to comment.