Commit 2ff243fe authored by Chris's avatar Chris
Browse files

small refactor

parent 768f2239
Loading
Loading
Loading
Loading
+13 −8
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ typedef struct game_context_st {

FILE *get_urandom();
unsigned int generate_single_die_roll(FILE *urandom_file);
void generate_roll(game_context *ctx, unsigned int *out_dice_values, int save_in_game_state);
void generate_dice_roll(game_context *ctx, unsigned int *out_dice_values, int save_in_game_state);
int set_die_held(game_context *ctx, unsigned int index, unsigned int held);
void sleep_ms(unsigned int ms);
void roll_and_print(game_context *ctx, unsigned int *out_dice_values, int animation_seconds);
@@ -50,9 +50,7 @@ int main() {
	// first roll
	roll_and_print(&ctx, next_roll, 0);

	sleep(1);

	printf("\n");
	sleep_ms(1000);

	// set some dice held (debug)
	set_die_held(&ctx, 0, 1);
@@ -62,6 +60,11 @@ int main() {

	roll_and_print(&ctx, next_roll, 0);

	// sanity check that we're actually saving to game state in the roll_and_print()
	// for (int i = 0; i < NUM_DICE; i++) {
	// 	printf("%d ", ctx.game_state.dice[i].value);
	// }
	// printf("\n");

	fclose(ctx.random_device_fd);
	return 0;
@@ -77,17 +80,19 @@ void roll_and_print(game_context *ctx, unsigned int *out_dice_values, int animat
	unsigned int temporary_next_roll[NUM_DICE];
	// do a little dice animation
	for (int i = 0; i < 30; i++) {
		generate_roll(ctx, temporary_next_roll, 0);
		generate_dice_roll(ctx, temporary_next_roll, 0);
		print_multiple_dice(temporary_next_roll, NUM_DICE);
		sleep_ms(50);
		clear_dice();
	}
	// the last roll will be the one that is saved in game state
	generate_roll(ctx, out_dice_values, 1);
	generate_dice_roll(ctx, out_dice_values, 1);
	print_multiple_dice(out_dice_values, NUM_DICE);

	printf("\n");
}

void generate_roll(game_context *ctx, unsigned int *out_dice_values, int save_in_game_state) {
void generate_dice_roll(game_context *ctx, unsigned int *out_dice_values, int save_in_game_state) {
	for (int i = 0; i < NUM_DICE; i++) {
		if (ctx->game_state.dice[i].held) {
			out_dice_values[i] = ctx->game_state.dice[i].value;
@@ -122,7 +127,7 @@ int set_die_held(game_context *ctx, unsigned int index, unsigned int held) {
	return error;
}

void sleep_ms(unsigned int ms) {
void sleep_ms(const unsigned int ms) {
	struct timespec ts;
	ts.tv_sec = ms / 1000;
	ts.tv_nsec = (ms % 1000) * 1000000;