Refactoring, license, Box UI framework

This commit is contained in:
2025-10-17 13:41:37 +02:00
parent 0222bc1f90
commit eeee5aba7d
10 changed files with 322 additions and 319 deletions

225
main.c
View File

@@ -1,214 +1,35 @@
#include <stdio.h>
#include <unistd.h>
/*
* Copyright (C) Artsiom D.
* Copyright (C) shit-co.de
*/
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
struct coin {
const char *name;
const char *description;
struct chart chart;
};
#include "box.h"
#include "ui.h"
struct chart {
double *prices;
size_t length;
};
struct chart new_chart(size_t length)
int
main(void)
{
double *prices = malloc(length * sizeof(double));
struct box *parent;
prices[0] = 20;
for (size_t i = 1; i < length; i++) {
prices[i] = prices[i - 1] + (rand() % 3 - 1.5);
}
ui_init();
return (struct chart) {
.length = length,
.prices = prices
};
}
parent = new_vertical_box();
enum game_state {
GAME_BEGIN,
GAME_IDLE,
GAME_BUY,
GAME_SELL,
GAME_END,
};
parent->length = 2;
parent->children[0] = new_button_box("hello!");
parent->children[0]->color = UI_BLUE;
parent->children[1] = new_button_box("quit");
enum color {
color_red,
color_green,
color_blue,
};
draw_box(parent, 0, 0, 20, 30);
ui_refresh();
struct button *new_button(char *name, enum color color, size_t width, size_t height)
{
struct button *button = malloc(sizeof(struct button));
button->name = name;
button->width = width;
button->height = height;
button->next = NULL;
button->color = color;
button->highlighted = false;
return button;
}
struct game {
enum game_state state;
int bank;
struct coin coin;
struct button *button;
};
void draw_button(struct button *b, size_t x, size_t y)
{
draw_box(x, y, b->width, b->height);
if (b->highlighted) {
attron(A_UNDERLINE);
}
attron(COLOR_PAIR(b->color));
mvaddstr(y + b->height / 2, x + b->width / 2 - strlen(b->name) / 2, b->name);
if (b->highlighted) {
attroff(A_UNDERLINE);
}
attroff(COLOR_PAIR(b->color));
}
void draw_chart(struct chart *chart, size_t x, size_t y)
{
const size_t height = 30;
const size_t width = chart->length * 2 + 1;
draw_box(x, y, width, height + 1);
for (size_t i = 0; i < chart->length; i++) {
for (size_t j = 0; j < chart->prices[i]; j++) {
mvaddch(30 - (y + j), x + (i * 2) + 1, '?');
}
}
const size_t button_width = (double)(width) / 3;
struct button *buy = new_button("buy", color_red, button_width, 5);
struct button *sell = new_button("sell", color_blue, button_width, 5);
struct button *scam = new_button("scam", color_green, button_width, 5);
buy->next = sell;
sell->next = scam;
scam->next = buy;
buy->highlighted = true;
draw_button(buy, x, y + height + 1);
draw_button(sell, x + button_width, y + height + 1);
draw_button(scam, x + button_width * 2, y + height + 1);
}
enum box_type {
BOX_TYPE_BUTTON,
BOX_TYPE_CHART,
BOX_TYPE_HORIZONTAL,
BOX_TYPE_VERTICAL,
};
struct box {
enum box_type type;
size_t width;
size_t height;
enum color color;
union {
/* chart */
struct {
double *prices;
size_t length;
};
/* button */
char *name;
/* horizontal/vertical box */
struct {
struct box *children;
size_t length;
};
}
};
void draw_game(struct game *game)
{
clear();
mvaddstr(0, 0, game->coin.name);
mvaddstr(1, 0, game->coin.description);
draw_chart(&game->coin.chart, 5, 5);
refresh();
}
struct box *new_chart_box()
struct box *new_container_box(enum box_type type)
{
struct box *box = malloc(sizeof(box));
box->width = 0;
box->height = 0;
box->type = type;
box->children = malloc(sizeof(box) * 100);
box->length = 0;
return box;
}
struct box *new_vertical_box(void)
{
return new_container_box(BOX_TYPE_VERTICAL);
}
struct box *new_horizontal_box(void)
{
return new_container_box(BOX_TYPE_HORIZONTAL);
}
struct box *new
int main(void)
{
srand(time(NULL));
initscr();
start_color();
init_pair(color_green, COLOR_GREEN, COLOR_BLACK);
init_pair(color_red, COLOR_RED, COLOR_BLACK);
init_pair(color_blue, COLOR_BLUE, COLOR_BLACK);
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
clear();
refresh();
struct game game = {
.state = GAME_BEGIN,
.bank = 1000,
.coin = (struct coin) {
.name = "Bitcoin",
.description = "A fucking scam.",
.chart = new_chart(30)
}
};
draw_game(&game);
refresh();
sleep(300);
endwin();
ui_end();
}