diff --git a/box.c b/box.c index d5f23b6..e96d8ec 100644 --- a/box.c +++ b/box.c @@ -73,7 +73,6 @@ draw_rectangle(double fx, double fy, double fwidth, double fheight) static void draw_chart(struct box *chart, double x, double y, double width, double height) { - char c = '@'; long int actual_diff; double diff, price; enum ui_color color; @@ -89,23 +88,11 @@ draw_chart(struct box *chart, double x, double y, double width, double height) actual_diff = 0; for (size_t i = 0; i < width; i++) { - diff = (double)rand() / RAND_MAX - 0.6; - actual_diff = (size_t)(height + y - price) - - (size_t)(height + y - (price + diff)); + price = chart->chart->prices[i]; - if (actual_diff > 0) { - color = UI_GREEN; - } else if (actual_diff == 0) { - color = UI_NONE; - } else if (actual_diff < 0) { - color = UI_RED; - } - - ui_color_on(color); - ui_char(x + i, height + y - price, c); - ui_color_off(color); - - price += diff; + ui_color_on(UI_BLUE); + ui_char(x + i, height + y - price, '@'); + ui_color_off(UI_BLUE); } } @@ -163,16 +150,16 @@ draw_container(struct box *b, double x, double y, double width, double height) struct box * -new_chart_box(void) +new_chart_box(struct chart *chart) { - struct box *chart = malloc(sizeof(struct box)); + struct box *box = malloc(sizeof(struct box)); - chart->type = BOX_CHART; - chart->prices = emalloc(sizeof(double) * 100); - chart->fills = 1; - chart->color = UI_NONE; + box->type = BOX_CHART; + box->chart = chart; + box->fills = 1; + box->color = UI_NONE; - return chart; + return box; } diff --git a/box.h b/box.h index a0495c4..ca49bee 100644 --- a/box.h +++ b/box.h @@ -8,6 +8,7 @@ #pragma once +#include "chart.h" #include "ui.h" @@ -27,18 +28,13 @@ struct box { /* button */ char *name; - /* chart and vertical/horizontal box */ + /* chart */ + struct chart *chart; + + /* vertical/horizontal */ struct { - /* chart and vertical/horizontal box */ size_t length; - - union { - /* vertical/horizontal box */ - struct box **children; - - /* chart */ - double *prices; - }; + struct box **children; }; }; }; @@ -47,7 +43,7 @@ struct box { struct box *new_button_box(char *name); struct box *new_vertical_box(void); struct box *new_horizontal_box(void); -struct box *new_chart_box(void); +struct box *new_chart_box(struct chart *); void append_box(struct box *parent, struct box *child); diff --git a/chart.c b/chart.c new file mode 100644 index 0000000..e1c6da0 --- /dev/null +++ b/chart.c @@ -0,0 +1,32 @@ + +/* + * Copyright (C) Artsiom D. + * Copyright (C) shit-co.de + */ + + +#include +#include "chart.h" +#include "util.h" + + +struct chart new_chart(unsigned int length) +{ + unsigned int i; + double price; + struct chart chart; + + chart.prices = emalloc(sizeof(double) * length); + chart.length = length; + + /* populate with random prices */ + + price = 10; + + for (i = 0; i < length; i++) { + chart.prices[i] = price; + price += (double)rand() / RAND_MAX - 0.4; + } + + return chart; +} diff --git a/chart.h b/chart.h new file mode 100644 index 0000000..edc402c --- /dev/null +++ b/chart.h @@ -0,0 +1,17 @@ + +/* + * Copyright (C) Artsiom D. + * Copyright (C) shit-co.de + */ + + +#pragma once + + +struct chart { + double *prices; + unsigned int length; +}; + + +struct chart new_chart(unsigned int length); diff --git a/main.c b/main.c index d299ba1..b1394c2 100644 --- a/main.c +++ b/main.c @@ -16,7 +16,9 @@ int main(void) { - struct box *ui, *top, *bottom, *left, *right, *buy, *sell, *scam, *title, *desc; + struct box *ui, *top, *bottom, *left, *right, *buy, *sell, *scam, + *title, *desc; + struct chart chart; ui_init(); @@ -27,7 +29,8 @@ main(void) append_box(top, title); append_box(top, desc); - left = new_chart_box(); + chart = new_chart(400); + left = new_chart_box(&chart); left->fills = 5; buy = new_button_box("buy"); @@ -51,7 +54,7 @@ main(void) append_box(ui, top); append_box(ui, bottom); - draw_box(ui, 0, 0, ui_width()-1, ui_height()-1); + draw_box(ui, 0, 0, ui_width(), ui_height()); ui_refresh(); diff --git a/makefile b/makefile index 9c1a055..3e4e1f0 100644 --- a/makefile +++ b/makefile @@ -1,11 +1,12 @@ -CFLAGS = -std=c23 -Wextra -pedantic -Wall -pedantic -LDFLAGS = -lncurses +CFLAGS = -std=c23 -Wextra -pedantic -Wall -O0 -g3 -fsanitize=address +LDFLAGS = -lncurses -fsanitize=address -main: box.o main.o ui.o util.o +main: box.o main.o ui.o util.o chart.o .PHONY: clean box.o: box.c +chart.o: chart.c main.o: main.c ui.o: ui.c util.o: util.c diff --git a/util.c b/util.c index c3727b5..c0f6805 100644 --- a/util.c +++ b/util.c @@ -23,7 +23,7 @@ emalloc(size_t n) } -void +noreturn void die(char *fmt, ...) { va_list va; diff --git a/util.h b/util.h index d7f02bd..58c986e 100644 --- a/util.h +++ b/util.h @@ -8,7 +8,8 @@ #pragma once #include +#include void *emalloc(size_t n); -void die(char *fmt, ...); +noreturn void die(char *fmt, ...);