summaryrefslogtreecommitdiff
path: root/src/draw.c
blob: a8c776f5c05691e4f494ed19c3684256cfc878dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <SFML/Graphics/RenderWindow.h>
#include <SFML/Graphics/RectangleShape.h>
#include <stdlib.h>

#include "tet_conf.h"
#include "field.h"
#include "draw.h"

struct field_drawable {
    sfRectangleShape *p[FLD_BOUND_Y][FLD_SIZE_X];
};

struct idlist {
    unsigned long id;
    void        *obj;
    struct idlist *next;
};

static struct idlist *list_new()
{
    struct idlist *list = calloc(1, sizeof(struct idlist));
    list->id = 1;
    return list;
}

static struct idlist *list_append(struct idlist *list)
{
    unsigned long id = list->id + 1;
    struct idlist *last = list;
    while (last->next)
        last = last->next;
    last->next = calloc(1, sizeof(struct idlist));
    last = last->next;
    last->id = id;
    return last;
}

static struct idlist *list_get(const struct idlist *list, unsigned long id)
{
    const struct idlist *sought = list;
    if (sought) {
        if (sought->id == id)
            return (struct idlist *)sought;
        while (sought->next) {
            sought = sought->next;
            if (sought->id == id)
                return (struct idlist *)sought;
        }
    }
    return NULL;
}

#define BUFSIZE 100

/* Must be static in future */
struct window w;

static struct idlist *fields_list = NULL;

void painter_init_window()
{
    w = (struct window){.mode = {450, 570, 32}};
    w.window = sfRenderWindow_create(w.mode, windowName_conf,
                   sfResize | sfClose, NULL);
    if (!w.window)
        exit(EXIT_FAILURE);
}

unsigned long painter_register_field()
{
    struct idlist *last;
    if (!fields_list) {
        last = fields_list = list_new();
    } else
        last = list_append(fields_list);

    struct field_drawable *f = calloc(1, sizeof(struct field_drawable));
    for (int j = 0; j < FLD_SIZE_Y; j++)
        for (int i = 0; i < FLD_SIZE_X; i++) {
            f->p[j][i] = sfRectangleShape_create();
            sfVector2f cell_pos;
            cell_pos.x = FLD_POS.x + (i * (CELL_SIZE.x + 2 * OUT_THICK));
            cell_pos.y = FLD_POS.y - (j * (CELL_SIZE.y + 2 * OUT_THICK));
            sfRectangleShape_setFillColor(f->p[j][i], UIBGCOLOR);
            sfRectangleShape_setSize(f->p[j][i], CELL_SIZE);
            sfRectangleShape_setPosition(f->p[j][i], cell_pos);
            sfRectangleShape_setOutlineColor(f->p[j][i], UIFGACTIVECOLOR);
            sfRectangleShape_setOutlineThickness(f->p[j][i], OUT_THICK);
        }

    last->obj = f;
    return last->id;
}

void painter_update_field(unsigned long id, struct field *fld)
{
    struct idlist *node = list_get(fields_list, id);
    if (!node)
        return;
    struct field_drawable *f = node->obj;
    for (int j = 0; j < fld->size.y; j++) {
        for (int i = 0; i < fld->size.x; i++) {
            switch (fld->c[j][i].color) {
                case 1 :
                    sfRectangleShape_setFillColor(f->p[j][i], LCOLOR);
                    break;
                case 2 :
                    sfRectangleShape_setFillColor(f->p[j][i], RLCOLOR);
                    break;
                case 3 :
                    sfRectangleShape_setFillColor(f->p[j][i], ZCOLOR);
                    break;
                case 4 :
                    sfRectangleShape_setFillColor(f->p[j][i], SCOLOR);
                    break;
                case 5 :
                    sfRectangleShape_setFillColor(f->p[j][i], BCOLOR);
                    break;
                case 6 :
                    sfRectangleShape_setFillColor(f->p[j][i], ICOLOR);
                    break;
                case 7 :
                    sfRectangleShape_setFillColor(f->p[j][i], TCOLOR);
                    break;
            }
            sfRectangleShape_setOutlineColor(f->p[j][i], UIFGACTIVECOLOR);
        }
    }
}

static void draw_field(struct field_drawable *f)
{
    for (unsigned int j = 0; j < FLD_SIZE_Y; j++)
        for (unsigned int i = 0; i < FLD_SIZE_X; i++)
            sfRenderWindow_drawRectangleShape(w.window, f->p[j][i], NULL);
}

static void draw_fields()
{
    struct idlist *node = fields_list;
    if (node) {
        if (node->obj)
            draw_field(node->obj);
        while (node->next) {
            node = node->next;
            if (node)
                if (node->obj)
                    draw_field(node->obj);
        }
    }
}

void painter_draw()
{
    draw_fields();
}

void painter_destroy_window()
{
    sfRenderWindow_destroy(w.window);
}