Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nightly #2

Merged
merged 14 commits into from
Nov 5, 2019
Prev Previous commit
Next Next commit
Simulation works well, added doxygen
  • Loading branch information
gorbit99 committed Nov 4, 2019
commit 29334eb750decd82d33901b349c166fad0394509
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ find_package(SDL2_image REQUIRED)
add_executable(${PROJECT_NAME} main.c Graphics.c Graphics.h debugmalloc.h debugmalloc-impl.h Component.c Component.h
Utility.c Utility.h Window.c Window.h Input.c Input.h Node.c Node.h Parser.c Parser.h
NodeVector.c NodeVector.h Camera.c Camera.h GUIGraphics.c GUIGraphics.h NodeEvent.c
NodeEvent.h)
NodeEvent.h Wire.c Wire.h)

set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 99)

Expand Down
3 changes: 3 additions & 0 deletions Component.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "debugmalloc.h"

const float COMPSIZE = 300;
const float COMPTHICKNESS = 15;

SDL_Surface *component_load_graphic(const char *path, float size, float thickness, TTF_Font *font) {

FILE *f = fopen(path, "r");
Expand Down
3 changes: 3 additions & 0 deletions Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "Graphics.h"
#include "Parser.h"

extern const float COMPSIZE;
extern const float COMPTHICKNESS;

typedef enum PinType {
PIN_IN,
PIN_OUT
Expand Down
33 changes: 14 additions & 19 deletions Node.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
#include "debugmalloc.h"

static void __setup_node(Node *node, SDL_Renderer *renderer) {
node->connections = (Connection *)malloc(sizeof(Connection) * node->component.funData.outC);
node->wires = (Wire *)malloc(sizeof(Wire) * node->component.funData.outC);
for (int i = 0; i < node->component.funData.outC; i++) {
node->connections[i].other = NULL;
node->connections[i].pinB = -1;
node->wires[i] = wire_create(node, i);
}
node->inValues = (bool *)calloc(node->component.funData.inC, sizeof(bool));
node->nextInValues = (bool *)calloc(node->component.funData.inC, sizeof(bool));
Expand Down Expand Up @@ -34,16 +33,8 @@ Node node_create_switch(Point pos, SDL_Renderer *renderer) {
return node;
}

void node_set_connection(Node *node, int pinA, Node *other, int pinB) {
node->connections[pinA].pinB = pinB;
node->connections[pinA].other = other;
node->connections[pinA].wire = component_create_wire_between(&node->component,
&other->component,
pinA + node->component.funData.inC,
pinB,
300,
15,
node->renderer);
void node_add_connection(Node *node, int pinA, Node *other, int pinB) {
wire_add(&node->wires[pinA], other, pinB);
}

void node_set_inval(Node *node, int pinIn, bool value) {
Expand All @@ -60,24 +51,28 @@ void node_update_values(Node *node) {
void node_run(Node *node) {
component_run(&node->component, node->inValues, node->outValues);
for (int i = 0; i < node->component.funData.outC; i++)
node_set_inval(node->connections[i].other, node->connections[i].pinB, node->outValues[i]);
wire_send_value(&node->wires[i], node->outValues[i]);
}

void node_free(Node *node) {
component_free_data(&node->component);
free(node->connections);
if (node->component.funData.inC > 0)
for (int i = 0; i < node->component.funData.outC; i++)
wire_free(&node->wires[i]);
free(node->wires);
if (node->component.funData.inC > 0) {
free(node->inValues);
free(node->nextInValues);
}
free(node->outValues);
}

void node_render(Node *node, Point camPos) {
for (int i = 0; i < node->component.funData.outC; i++) {
if (node->connections[i].other) {
for (size_t w = 0; w < node->wires[i].conCount; w++) {
if (node->outValues[i])
component_render(&node->connections[i].wire, node->renderer, camPos, 0xff7700ff);
component_render(&node->wires[i].connections[w].wireComp, node->renderer, camPos, 0xff7700ff);
else
component_render(&node->connections[i].wire, node->renderer, camPos, 0x0000aaff);
component_render(&node->wires[i].connections[w].wireComp, node->renderer, camPos, 0x0000aaff);
}
}
if (node->component.type == CT_MODULE)
Expand Down
12 changes: 4 additions & 8 deletions Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@

#include "Component.h"
#include "Input.h"
#include "Wire.h"

struct Node;

typedef struct Connection {
int pinB;
struct Node *other;
ComponentData wire;
} Connection;
typedef struct Wire Wire;

typedef struct Node {
ComponentData component;
Connection *connections;
Wire *wires;
bool *inValues;
bool *nextInValues;
bool *outValues;
Expand All @@ -25,7 +21,7 @@ Node node_create(char *compName, Point pos, TTF_Font *font, SDL_Renderer *render
Node node_create_LED(Point pos, SDL_Renderer *renderer);
Node node_create_switch(Point pos, SDL_Renderer *renderer);

void node_set_connection(Node *node, int pinA, Node *other, int pinB);
void node_add_connection(Node *node, int pinA, Node *other, int pinB);

void node_set_inval(Node *node, int pinIn, bool value);

Expand Down
7 changes: 0 additions & 7 deletions NodeEvent.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ EventQueue evqueue_create() {
}

void evqueue_push(EventQueue *queue, Event event) {
/*for (size_t i = 0; i < queue->count; i++)
if (queue->queue[i].node == event.node) {
for (size_t j = i + 1; j < queue->count; j++)
queue[j - 1] = queue[j];
queue->count--;
break;
}*/
if (queue->capacity <= queue->count) {
queue->capacity *= 2;
Event *newMem = (Event *)realloc(queue->queue, sizeof(Event) * queue->capacity);
Expand Down
9 changes: 7 additions & 2 deletions NodeVector.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ void nodev_push_back(NodeVector *vector, Node node) {
return;
}
vector->nodes = newmem;
for (int 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];
}
memcpy(&(vector->nodes[vector->count]), &node, sizeof(node));
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++;
}

Expand All @@ -35,7 +40,7 @@ Node *nodev_at(NodeVector *vector, int index) {
}

void nodev_connect(NodeVector *vector, int idA, int pinA, int idB, int pinB) {
node_set_connection(nodev_at(vector, idA), pinA, nodev_at(vector, idB), pinB);
node_add_connection(nodev_at(vector, idA), pinA, nodev_at(vector, idB), pinB);
}

void nodev_update(NodeVector *vector) {
Expand Down
51 changes: 51 additions & 0 deletions Wire.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Wire.h"
#include "debugmalloc.h"

Wire wire_create(Node *origin, int originPin) {
Wire wire;
wire.origin = origin;
wire.originPin = originPin;
wire.conCap = 1;
wire.conCount = 0;
wire.connections = (Connection *)malloc(sizeof(Connection));
return wire;
}

void wire_add(Wire *wire, Node *other, int otherPin) {
if (wire->conCap <= wire->conCount) {
wire->conCap *= 2;
Connection *newCon = (Connection *)realloc(wire->connections, sizeof(Connection) * wire->conCap);
if (!newCon) {
log_error("Couldn't allocate new memory!\n");
return;
}
wire->connections = newCon;
}
wire->connections[wire->conCount].dest = other;
wire->connections[wire->conCount].pin = otherPin;
wire->connections[wire->conCount].wireComp = component_create_wire_between(
&wire->origin->component,
&other->component,
wire->originPin + wire->origin->component.funData.inC,
otherPin,
COMPSIZE,
COMPTHICKNESS,
wire->origin->renderer
);
wire->conCount++;
}

void wire_erase(Wire *wire, int id) {
for (int i = id + 1; id < wire->conCount; i++)
wire->connections[i - 1] = wire->connections[i];
wire->conCount--;
}

void wire_send_value(Wire *wire, bool value) {
for (size_t i = 0; i < wire->conCount; i++)
node_set_inval(wire->connections[i].dest, wire->connections[i].pin, value);
}

void wire_free(Wire *wire) {
free(wire->connections);
}
32 changes: 32 additions & 0 deletions Wire.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef HOMEWORK_WIRE_H
#define HOMEWORK_WIRE_H

#include "Node.h"

typedef struct Node Node;

typedef struct Connection {
Node *dest;
int pin;
ComponentData wireComp;
} Connection;

typedef struct Wire {
Node *origin;
int originPin;
Connection *connections;
size_t conCount;
size_t conCap;
} Wire;

Wire wire_create(Node *origin, int originPin);

void wire_add(Wire *wire, Node *other, int otherPin);

void wire_erase(Wire *wire, int id);

void wire_send_value(Wire *wire, bool value);

void wire_free(Wire *wire);

#endif //HOMEWORK_WIRE_H
Loading