Skip to content

Commit

Permalink
Add finished counter to splits
Browse files Browse the repository at this point in the history
  • Loading branch information
Craven committed Aug 29, 2024
1 parent ba10e05 commit 00b2b35
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/component/title.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ typedef struct _LSTitle {
GtkWidget* header;
GtkWidget* title;
GtkWidget* attempt_count;
GtkWidget* finished_count;
} LSTitle;
extern LSComponentOps ls_title_operations; // defined at the end of the file

Expand Down Expand Up @@ -35,6 +36,13 @@ LSComponent* ls_component_title_new()
gtk_widget_set_valign(self->attempt_count, GTK_ALIGN_START);
gtk_container_add(GTK_CONTAINER(self->header), self->attempt_count);
gtk_widget_show(self->attempt_count);

self->finished_count = gtk_label_new(NULL);
add_class(self->finished_count, "finished_count");
gtk_widget_set_margin_start(self->finished_count, 8);
gtk_widget_set_valign(self->finished_count, GTK_ALIGN_START);
gtk_container_add(GTK_CONTAINER(self->header), self->finished_count);
gtk_widget_show(self->finished_count);

return (LSComponent*)self;
}
Expand All @@ -53,13 +61,16 @@ static void title_resize(LSComponent* self_, int win_width, int win_height)
{
GdkRectangle rect;
int attempt_count_width;
int finished_count_width;
int title_width;
LSTitle* self = (LSTitle*)self_;

gtk_widget_hide(self->title);
gtk_widget_get_allocation(self->attempt_count, &rect);
attempt_count_width = rect.width;
title_width = win_width - attempt_count_width;
gtk_widget_get_allocation(self->finished_count, &rect);
finished_count_width = rect.width;
title_width = win_width - (attempt_count_width + finished_count_width);
rect.width = title_width;
gtk_widget_show(self->title);
gtk_widget_set_allocation(self->title, &rect);
Expand All @@ -77,10 +88,16 @@ static void title_show_game(LSComponent* self_, ls_game* game,

static void title_draw(LSComponent* self_, ls_game* game, ls_timer* timer)
{
char str[64];
char attempt_str[64];
char finished_str[64];
char combi_str[64];
LSTitle* self = (LSTitle*)self_;
sprintf(str, "#%d", game->attempt_count);
gtk_label_set_text(GTK_LABEL(self->attempt_count), str);
sprintf(attempt_str, "%d", game->attempt_count);
sprintf(finished_str, "#%d", game->finished_count);
strcpy(combi_str, finished_str);
strcat(combi_str, "/");
strcat(combi_str, attempt_str);
gtk_label_set_text(GTK_LABEL(self->attempt_count), combi_str);
}

LSComponentOps ls_title_operations = {
Expand Down
12 changes: 12 additions & 0 deletions src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ int ls_game_create(ls_game** game_ptr, const char* path, char** error_msg)
if (ref) {
game->attempt_count = json_integer_value(ref);
}
//get finished count
ref = json_object_get(json, "finished_count");
if (ref) {
game->finished_count = json_integer_value(ref);
}
// get width
ref = json_object_get(json, "width");
if (ref) {
Expand Down Expand Up @@ -377,6 +382,10 @@ int ls_game_save(const ls_game* game)
json_object_set_new(json, "attempt_count",
json_integer(game->attempt_count));
}
if (game->finished_count) {
json_object_set_new(json, "finished_count",
json_integer(game->finished_count));
}
if (game->world_record) {
ls_time_string_serialized(str, game->world_record);
json_object_set_new(json, "world_record", json_string(str));
Expand Down Expand Up @@ -486,6 +495,7 @@ int ls_timer_create(ls_timer** timer_ptr, ls_game* game)
}
timer->game = game;
timer->attempt_count = &game->attempt_count;
timer->finished_count = &game->finished_count;
// alloc splits
timer->split_times = calloc(timer->game->split_count,
sizeof(long long));
Expand Down Expand Up @@ -638,6 +648,8 @@ int ls_timer_split(ls_timer* timer)
++timer->curr_split;
// stop timer if last split
if (timer->curr_split == timer->game->split_count) {
//Increment finished_count
++*timer->finished_count;
ls_timer_stop(timer);
ls_game_update_splits((ls_game*)timer->game, timer);
}
Expand Down
2 changes: 2 additions & 0 deletions src/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct ls_game {
char* theme;
char* theme_variant;
int attempt_count;
int finished_count;
int width;
int height;
long long world_record;
Expand Down Expand Up @@ -44,6 +45,7 @@ struct ls_timer {
long long* best_segments;
const ls_game* game;
int* attempt_count;
int* finished_count;
};
typedef struct ls_timer ls_timer;

Expand Down

0 comments on commit 00b2b35

Please sign in to comment.