Skip to content

Commit 774cef2

Browse files
committedNov 21, 2019
Saving is complete
1 parent c9ee317 commit 774cef2

File tree

8 files changed

+101
-1
lines changed

8 files changed

+101
-1
lines changed
 

Diff for: ‎Component.c

+6
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,17 @@ ComponentData component_create(float x, float y, char *name, float size, float t
112112

113113
data.type = CT_MODULE;
114114

115+
data.name = (char *)malloc(strlen(name) + 1);
116+
strcpy(data.name, name);
117+
115118
return data;
116119
}
117120

118121
void component_free_data(ComponentData *dat) {
119122
free(dat->pinData.pins);
120123
SDL_DestroyTexture(dat->texture);
121124
parser_free_function(&dat->funData);
125+
free(dat->name);
122126
}
123127

124128
void component_render(ComponentData *dat, SDL_Renderer *renderer, Point camPos, Color color) {
@@ -233,6 +237,8 @@ ComponentData component_create_wire_between(ComponentData *comp1, ComponentData
233237

234238
data.type = CT_WIRE;
235239

240+
data.name = NULL;
241+
236242
return data;
237243
}
238244

Diff for: ‎Component.h

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ typedef struct ComponentData {
7676
PinData pinData; /**< Pins of the component */
7777
FunctionData funData; /**< Function of the component */
7878
ComponentType type; /**< Type of the component */
79+
char *name; /**< Name of the component */
7980
} ComponentData;
8081

8182
/**

Diff for: ‎NodeVector.c

+32
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ void nodev_push_back(NodeVector *vector, Node node) {
2929
}
3030

3131
void nodev_erase(NodeVector *vector, int index) {
32+
node_free(nodev_at(vector, index));
3233
for (size_t i = index + 1; i < vector->count; i++) {
3334
vector->nodes[i - 1] = vector->nodes[i];
35+
for (int j = 0; j < vector->nodes[i].component.funData.outC; j++)
36+
vector->nodes[i - 1].wires[j].origin = &vector->nodes[i - 1];
3437
}
3538
vector->count--;
3639
}
@@ -93,4 +96,33 @@ void nodev_reposition(NodeVector *vector, Node *node, Point position) {
9396
}
9497
}
9598
}
99+
}
100+
101+
void nodev_delete(NodeVector *vector, Node *node) {
102+
for (int w = 0; w < node->component.funData.outC; w++) {
103+
Wire *wire = &node->wires[w];
104+
for (size_t c = 0; c < wire->conCount; c++) {
105+
Connection *conn = &wire->connections[c];
106+
nodev_at(vector, conn->dest)->component.pinData.pins[conn->pin].occupied = false;
107+
}
108+
}
109+
for (size_t i = 0; i < vector->count; i++) {
110+
if (nodev_at(vector, i) == node) {
111+
nodev_erase(vector, i);
112+
113+
for (size_t n = 0; n < vector->count; n++) {
114+
Node *other = nodev_at(vector, n);
115+
for (int w = 0; w < other->component.funData.outC; w++) {
116+
Wire *wire = &other->wires[w];
117+
for (size_t c = 0; c < wire->conCount; c++) {
118+
Connection *conn = &wire->connections[c];
119+
if (conn->dest == i) {
120+
wire_erase(wire, c);
121+
}
122+
}
123+
}
124+
}
125+
break;
126+
}
127+
}
96128
}

Diff for: ‎NodeVector.h

+8
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,12 @@ void nodev_free(NodeVector *vector);
107107
*/
108108
void nodev_reposition(NodeVector *vector, Node *node, Point position);
109109

110+
/**
111+
* @brief Delete a node and erase the connections
112+
*
113+
* @param vector Vector to delete node from
114+
* @param node Node to delete
115+
*/
116+
void nodev_delete(NodeVector *vector, Node *node);
117+
110118
#endif //HOMEWORK_NODEVECTOR_H

Diff for: ‎Save.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "Save.h"
2+
#include "debugmalloc.h"
3+
4+
void save_node(FILE *file, Node *node) {
5+
fprintf(file, "\t%s %f,%f\n", node->component.name, node->component.x, node->component.y);
6+
}
7+
8+
bool save_vector(NodeVector *vector, char *fileName) {
9+
FILE *file = fopen(fileName, "wt");
10+
if (file == NULL) {
11+
log_error("Couldn't open file %s!", fileName);
12+
return false;
13+
}
14+
15+
fprintf(file, "Nodes\n");
16+
for (size_t n = 0; n < vector->count; n++)
17+
save_node(file, nodev_at(vector, n));
18+
fprintf(file, "Connections\n");
19+
for (size_t n = 0; n < vector->count; n++) {
20+
Node *node = nodev_at(vector, n);
21+
for (int w = 0; w < node->component.funData.outC; w++) {
22+
Wire *wire = &node->wires[w];
23+
for (size_t c = 0; c < wire->conCount; c++) {
24+
Connection *conn = &wire->connections[c];
25+
fprintf(file, "\t%d %d %d %d\n", n, w, conn->dest, conn->pin);
26+
}
27+
}
28+
}
29+
}

Diff for: ‎Save.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef HOMEWORK_SAVE_H
2+
#define HOMEWORK_SAVE_H
3+
4+
#include <stdio.h>
5+
#include <stdbool.h>
6+
#include "NodeVector.h"
7+
#include "Utility.h"
8+
9+
void save_node(FILE *file, Node *node);
10+
11+
void save_connections(FILE *file, Node *node);
12+
13+
bool save_vector(NodeVector *vector, char *fileName);
14+
15+
#endif

Diff for: ‎debugmalloclog.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
debugmalloc: free @ C:\Users\gorbi\Documents\Projects\School\LogicSim\LogicSim\Component.c:125: olyan teruletet akarsz felszabaditani, ami nincs lefoglalva!

Diff for: ‎main.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Windowing.h"
1212
#include "Search.h"
1313
#include "WireDrawing.h"
14+
#include "Save.h"
1415

1516
enum ProgramState {
1617
VIEWING_CIRCUIT,
@@ -165,8 +166,13 @@ int main(int argc, char **argv) {
165166
camera_update(&camera, &mainWindow.input, mainWindow.renderer);
166167
nodev_reposition(&vec, moved, mouseWS);
167168

169+
if (input_get_key(&mainWindow.input, SDL_SCANCODE_DELETE).isPressed) {
170+
nodev_delete(&vec, moved);
171+
moved = NULL;
172+
}
173+
168174
//Transitions
169-
if (input_get_mouse_button(&mainWindow.input, SDL_BUTTON_LEFT).isPressed) {
175+
if (input_get_mouse_button(&mainWindow.input, SDL_BUTTON_LEFT).isPressed || moved == NULL) {
170176
state = VIEWING_CIRCUIT;
171177
}
172178
break;
@@ -215,6 +221,8 @@ int main(int argc, char **argv) {
215221

216222
window_quit_SDL();
217223

224+
save_vector(&vec, "test.sav");
225+
218226
nodev_free(&vec);
219227

220228
return 0;

0 commit comments

Comments
 (0)
Please sign in to comment.