-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeVector.c
143 lines (132 loc) · 4.33 KB
/
NodeVector.c
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
#include "NodeVector.h"
#include "debugmalloc.h"
NodeVector nodev_create(size_t count) {
NodeVector result;
result.capacity = count * 2 + 1;
result.count = count;
result.nodes = (Node *)malloc(sizeof(Node) * result.capacity);
return result;
}
void nodev_push_back(NodeVector *vector, Node node) {
if (vector->count >= vector->capacity) {
vector->capacity *= 2;
Node *newmem = (Node *)realloc(vector->nodes, sizeof(Node) * vector->capacity);
if (newmem == NULL) {
log_error("Couldn't reallocate vector!\n");
return;
}
vector->nodes = newmem;
for (size_t i = 0; i < vector->count; i++)
for (int j = 0; j < vector->nodes[i].component.funData.outC; j++)
vector->nodes[i].wires[j].origin = &vector->nodes[i];
}
vector->nodes[vector->count] = node;
for (int i = 0; i < vector->nodes[vector->count].component.funData.outC; i++)
vector->nodes[vector->count].wires[i].origin = &vector->nodes[vector->count];
vector->count++;
}
void nodev_erase(NodeVector *vector, int index) {
node_free(nodev_at(vector, index));
for (size_t i = 0; i < vector->count; i++) {
if (i == index)
continue;
Node *node = nodev_at(vector, i);
for (int w = 0; w < node->component.funData.outC; w++) {
Wire *wire = &node->wires[w];
for (size_t c = 0; c < wire->conCount; c++) {
Connection *conn = &wire->connections[c];
if (conn->dest > index)
conn->dest--;
}
}
}
for (size_t i = index + 1; i < vector->count; i++) {
vector->nodes[i - 1] = vector->nodes[i];
for (int j = 0; j < vector->nodes[i].component.funData.outC; j++)
vector->nodes[i - 1].wires[j].origin = &vector->nodes[i - 1];
}
vector->count--;
}
Node *nodev_at(NodeVector *vector, int index) {
return &vector->nodes[index];
}
void nodev_connect(NodeVector *vector, int idA, int pinA, int idB, int pinB) {
node_add_connection(nodev_at(vector, idA), pinA, idB, pinB, vector->nodes);
}
void nodev_render(NodeVector *vector, Point camPos) {
for (size_t i = 0; i < vector->count; i++) {
node_render(nodev_at(vector, i), camPos);
}
}
void nodev_update(NodeVector *vector) {
for (size_t i = 0; i < vector->count; i++) {
node_run(nodev_at(vector, i), vector->nodes);
node_update_values(nodev_at(vector, i));
}
}
void nodev_check_clicks(NodeVector *vector, Point mousePos) {
for (size_t i = 0; i < vector->count; i++) {
if (nodev_at(vector, i)->component.type == CT_SWITCH)
if (node_is_over(nodev_at(vector, i), mousePos)) {
nodev_at(vector, i)->outValues[0] ^= 1u;
for (size_t w = 0; w < nodev_at(vector, i)->wires[0].conCount; w++)
node_set_inval(nodev_at(vector,nodev_at(vector, i)->wires[0].connections[w].dest),
nodev_at(vector, i)->wires[0].connections[w].pin,
nodev_at(vector, i)->outValues[0]);
}
}
}
void nodev_free(NodeVector *vector) {
for (size_t i = 0; i < vector->count; i++)
node_free(&vector->nodes[i]);
free(vector->nodes);
}
void nodev_reposition(NodeVector *vector, Node *node, Point position) {
if (node->component.x != position.x || node->component.y != position.y) {
node->component.x = position.x;
node->component.y = position.y;
node_reposition_wires(node, vector->nodes);
for (size_t i = 0; i < vector->count; i++) {
Node *origin = nodev_at(vector, i);
for (int w = 0; w < origin->component.funData.outC; w++) {
Wire *wire = &origin->wires[w];
for (size_t c = 0; c < wire->conCount; c++) {
Connection *con = &wire->connections[c];
if (nodev_at(vector, con->dest) == node) {
connection_reposition(con, origin, w, node);
}
}
}
}
}
}
void nodev_delete(NodeVector *vector, Node *node) {
for (int w = 0; w < node->component.funData.outC; w++) {
Wire *wire = &node->wires[w];
for (size_t c = 0; c < wire->conCount; c++) {
Connection *conn = &wire->connections[c];
nodev_at(vector, conn->dest)->component.pinData.pins[conn->pin].occupied = false;
}
}
for (size_t i = 0; i < vector->count; i++) {
if (nodev_at(vector, i) == node) {
for (size_t n = 0; n < vector->count; n++) {
if (n == i)
continue;
Node *other = nodev_at(vector, n);
for (int w = 0; w < other->component.funData.outC; w++) {
Wire *wire = &other->wires[w];
for (size_t c = 0; c < wire->conCount; c++) {
Connection *conn = &wire->connections[c];
if (conn->dest == i) {
wire_erase(wire, c);
c--;
}
}
}
}
nodev_erase(vector, i);
break;
}
}
}