2026 Renovation #3
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "common.h"
|
||||
#include <stddef.h>
|
||||
#define GRAPH_DAYS 30
|
||||
#include <time.h>
|
||||
|
||||
enum mode_t {
|
||||
WINDOW = 0,
|
||||
@@ -21,4 +21,8 @@ size_t get_intention_size();
|
||||
Mode get_mode();
|
||||
void set_mode(Mode mode);
|
||||
|
||||
// returns 1 for win, 0 for loss
|
||||
U32 get_result(struct tm *time);
|
||||
void set_result(struct tm *time, U32 result);
|
||||
|
||||
#endif
|
||||
|
||||
+15
-5
@@ -1,10 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define RAYGUI_IMPLEMENTATION
|
||||
#include <raygui.h>
|
||||
#include <raylib.h>
|
||||
#include "window_state.h"
|
||||
#include "common.h"
|
||||
|
||||
#define WINDOW_HEIGHT 700
|
||||
#define WINDOW_WIDTH 550
|
||||
@@ -34,7 +36,7 @@ typedef struct cell_t {
|
||||
} Cell;
|
||||
|
||||
int cell_number(Cell c) {
|
||||
return (c.row + 1) * (c.column + 1);
|
||||
return c.row + 1 + (c.column * 7);
|
||||
}
|
||||
|
||||
int distance(Cell a, Cell b) {
|
||||
@@ -46,22 +48,23 @@ int distance(Cell a, Cell b) {
|
||||
time_t now;
|
||||
struct tm local_time;
|
||||
|
||||
void get_date_days_ago(int days_ago, char *buffer, size_t buffer_size) {
|
||||
// Returns the time info struct which is a static variable within time.h stdlib
|
||||
struct tm *get_date_days_ago(int days_ago, char *buffer, size_t buffer_size) {
|
||||
time_t raw_time;
|
||||
struct tm *time_info;
|
||||
|
||||
time(&raw_time);
|
||||
time_info = localtime(&raw_time); // caution: localtime() overwrites the static struct internally as opposed to localtime_r
|
||||
time_info->tm_mday -= days_ago;
|
||||
mktime(time_info);
|
||||
|
||||
strftime(buffer, buffer_size, "%B %d", time_info);
|
||||
|
||||
return time_info;
|
||||
}
|
||||
|
||||
void draw_graph(int x, int y) {
|
||||
int num_columns = NUM_WEEKS;
|
||||
int num_rows = 7; // days in a week
|
||||
bool checked = false;
|
||||
|
||||
int cell_number_today = cell_number((Cell) {local_time.tm_wday, num_columns - 1});
|
||||
|
||||
@@ -73,16 +76,23 @@ void draw_graph(int x, int y) {
|
||||
|
||||
int days_ago = cell_number_today - curr_cell_number;
|
||||
char tooltip_text[128];
|
||||
get_date_days_ago(days_ago, tooltip_text, sizeof(tooltip_text));
|
||||
struct tm *time = get_date_days_ago(days_ago, tooltip_text, sizeof(tooltip_text));
|
||||
|
||||
U32 result = get_result(time);
|
||||
GuiSetTooltip(tooltip_text);
|
||||
|
||||
// full size of a cell is spacing * 2 + size, and then need to go little more
|
||||
int cell_x = CELL_SPACING + (((CELL_SPACING * 2) + CELL_SIZE) * c);
|
||||
int cell_y = CELL_SPACING + (((CELL_SPACING * 2) + CELL_SIZE) * r);
|
||||
|
||||
if (result) {
|
||||
GuiSetStyle(BUTTON, BASE_COLOR_NORMAL, ColorToInt(GREEN));
|
||||
} else {
|
||||
GuiSetStyle(BUTTON, BASE_COLOR_NORMAL, ColorToInt(GRAY));
|
||||
}
|
||||
if (GuiButton((Rectangle) {cell_x + x, cell_y + y, CELL_SIZE, CELL_SIZE}, " ")) {
|
||||
printf("clicked row %d | column %d\n", r, c);
|
||||
set_result(time, abs(result - 1));
|
||||
}
|
||||
|
||||
if (c == 0 && r == 1) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "window_state.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
@@ -7,9 +8,17 @@ struct state *s = NULL;
|
||||
|
||||
#define INTENTION_MAX_SIZE 2048
|
||||
|
||||
typedef struct record_t {
|
||||
char *key;
|
||||
U32 value;
|
||||
} Record;
|
||||
|
||||
struct state {
|
||||
Mode mode;
|
||||
char intention[INTENTION_MAX_SIZE];
|
||||
Record **records; // array of records
|
||||
size_t size_records;
|
||||
size_t capacity_records;
|
||||
};
|
||||
|
||||
void init_window_state() {
|
||||
@@ -17,12 +26,77 @@ void init_window_state() {
|
||||
s->mode = WINDOW;
|
||||
|
||||
memset(s->intention, 0, sizeof(s->intention));
|
||||
|
||||
s->size_records = 0;
|
||||
s->capacity_records = 1000; //TODO: make dynamic
|
||||
s->records = malloc(sizeof(Record *) * s->capacity_records);
|
||||
}
|
||||
|
||||
static void add_record(char *key, U32 value) {
|
||||
if (s->size_records >= s->capacity_records) {
|
||||
printf("MAX capacity");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
char *cpy = strdup(key);
|
||||
|
||||
Record *new_record = malloc(sizeof(Record));
|
||||
new_record->key = cpy;
|
||||
new_record->value = value;
|
||||
|
||||
*(s->records + s->size_records) = new_record;
|
||||
s->size_records++;
|
||||
}
|
||||
|
||||
static void upsert_record(char *key, U32 value) {
|
||||
bool found = false;
|
||||
for (int i = 0; i < s->size_records; i++) {
|
||||
Record *rec = *(s->records + i);
|
||||
if (strcmp(rec->key, key) == 0) {
|
||||
found = true;
|
||||
rec->value = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
add_record(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
static Record *get_record(char *key) {
|
||||
for (int i = 0; i < s->size_records; i++) {
|
||||
Record *rec = *(s->records + i);
|
||||
if (strcmp(rec->key, key) == 0) {
|
||||
return rec;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void print_records() {
|
||||
for (int i = 0; i < s->size_records; i++) {
|
||||
Record *rec = *(s->records + i);
|
||||
printf("record: %s -> %d\n", rec->key, rec->value);
|
||||
}
|
||||
}
|
||||
|
||||
static void generate_key(struct tm *time, char *buffer, size_t buffer_size) {
|
||||
snprintf(buffer, buffer_size, "%d", time->tm_yday);
|
||||
}
|
||||
|
||||
void cleanup_window_state() {
|
||||
if (!s) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < s->size_records; i++) {
|
||||
Record *rec = *(s->records + s->size_records);
|
||||
free(rec);
|
||||
}
|
||||
free(s->records);
|
||||
|
||||
free(s);
|
||||
s = NULL;
|
||||
}
|
||||
@@ -52,3 +126,22 @@ Mode get_mode() {
|
||||
void set_mode(Mode new_mode) {
|
||||
s->mode = new_mode;
|
||||
}
|
||||
|
||||
U32 get_result(struct tm *time) {
|
||||
char key[10];
|
||||
generate_key(time, key, sizeof(key));
|
||||
Record *rec = get_record(key);
|
||||
if (!rec) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return rec->value;
|
||||
}
|
||||
|
||||
void set_result(struct tm *time, U32 result) {
|
||||
char key[10];
|
||||
generate_key(time, key, sizeof(key));
|
||||
upsert_record(key, result);
|
||||
|
||||
print_records();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user