Skip to content

Commit

Permalink
Added rendering for components and leds
Browse files Browse the repository at this point in the history
  • Loading branch information
gorbit99 committed Oct 24, 2019
1 parent 43c94a1 commit d5b8480
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ find_package(SDL2_TTF REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIR})

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)
Utility.c Utility.h Window.c Window.h Input.c Input.h Node.c Node.h Parser.c Parser.h NodeVector.c NodeVector.h)

target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2_TTF_LIBRARIES})

Expand Down
26 changes: 14 additions & 12 deletions Component.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ ComponentData component_create(float x, float y, char *name, float size, float t
sprintf(path, "res/%s.fun", name);
data.funData = parser_load_function(path);

data.type = CT_MODULE;

return data;
}

Expand All @@ -111,13 +113,14 @@ void component_free_data(ComponentData *dat) {
parser_free_function(&dat->funData);
}

void component_render(ComponentData *dat, SDL_Renderer *renderer, Point camPos, float zoom) {
void component_render(ComponentData *dat, SDL_Renderer *renderer, Point camPos, float zoom, Color color) {
SDL_FRect r = {
(dat->x - camPos.x),
(dat->y - camPos.y),
(float) dat->w,
(float) dat->h
};
SDL_SetTextureColorMod(dat->texture, (color & 0xff000000) >> 24, (color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8);
SDL_RenderCopyF(renderer, dat->texture, NULL, &r);
}

Expand Down Expand Up @@ -202,19 +205,18 @@ ComponentData component_create_wire_between(ComponentData *comp1, ComponentData
data.x = (comp1->x + comp1->pinData.pins[pin1].pos.x + comp2->x + comp2->pinData.pins[pin2].pos.x) / 2 - data.w / 2;
data.y = (comp1->y + comp1->pinData.pins[pin1].pos.y + comp2->y + comp2->pinData.pins[pin2].pos.y) / 2 - data.h / 2;
data.pinData.pinCount = 2;
data.pinData.pins = (Pin *)malloc(2 * sizeof(Pin));
data.pinData.pins[0].name[0] = '\0';
data.pinData.pins[0].angle = 0;
data.pinData.pins[0].type = comp2->pinData.pins[pin2].type;
data.pinData.pins[0].pos = comp1->pinData.pins[pin1].pos;

data.pinData.pins[1].name[0] = '\0';
data.pinData.pins[1].angle = 0;
data.pinData.pins[1].type = comp1->pinData.pins[pin1].type;
data.pinData.pins[1].pos = comp2->pinData.pins[pin2].pos;
data.pinData.pins = NULL;

data.funData.assignC = 0;
data.funData.assigns = NULL;

data.type = CT_WIRE;

return data;
}
}

ComponentData component_create_LED(float x, float y, float size, float thickness, SDL_Renderer *renderer) {
ComponentData data = component_create(x, y, "LED", size, thickness, NULL, renderer);
data.type = CT_LED;
return data;
}
12 changes: 11 additions & 1 deletion Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ typedef enum PinType {
PIN_OUT
} PinType;

typedef enum ComponentType {
CT_MODULE,
CT_WIRE,
CT_SWITCH,
CT_LED
} ComponentType;

typedef struct Pin {
char name[17];
PinType type;
Expand All @@ -32,6 +39,7 @@ typedef struct ComponentData {
SDL_Texture *texture;
PinData pinData;
FunctionData funData;
ComponentType type;
} ComponentData;

SDL_Surface *component_load_graphic(const char *path, float size, float thickness, TTF_Font *font);
Expand All @@ -45,7 +53,7 @@ void component_update_texture(ComponentData *dat, SDL_Texture *texture);

void component_free_data(ComponentData *dat);

void component_render(ComponentData *dat, SDL_Renderer *renderer, Point camPos, float zoom);
void component_render(ComponentData *dat, SDL_Renderer *renderer, Point camPos, float zoom, Color color);

void component_handle(ComponentData *dat);

Expand All @@ -54,4 +62,6 @@ SDL_Surface *component_create_wire_texture(Point V1, Point V2, float ang1, float
ComponentData component_create_wire_between(ComponentData *comp1, ComponentData *comp2, int pin1, int pin2,
float size, float thickness, SDL_Renderer *renderer);

ComponentData component_create_LED(float x, float y, float size, float thickness, SDL_Renderer *renderer);

#endif //HOMEWORK_COMPONENT_H
65 changes: 55 additions & 10 deletions Node.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
#include "Node.h"
#include "debugmalloc.h"

void node_create(Node *node, char *compName, Point pos, TTF_Font *font, SDL_Renderer *renderer) {
node->component = component_create(pos.x, pos.y, compName, 300, 15, font, renderer);
node->connections = (Connection *)malloc(sizeof(Connection) * 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 node_create(char *compName, Point pos, TTF_Font *font, SDL_Renderer *renderer) {
Node node;
node.component = component_create(pos.x, pos.y, compName, 300, 15, font, renderer);
node.connections = (Connection *)malloc(sizeof(Connection) * 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->inValues = (bool *)malloc(sizeof(bool) * node->component.funData.inC);
node->outValues = (bool *)malloc(sizeof(bool) * node->component.funData.outC);
node->dirty = true;
node.inValues = (bool *)malloc(sizeof(bool) * node.component.funData.inC);
node.outValues = (bool *)malloc(sizeof(bool) * node.component.funData.outC);
node.dirty = true;
node.renderer = renderer;
return node;
}

Node node_create_LED(Point pos, SDL_Renderer *renderer) {
Node node;
node.component = component_create_LED(pos.x, pos.y, 300, 15, renderer);
node.connections = (Connection *)malloc(sizeof(Connection) * 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.inValues = (bool *)malloc(sizeof(bool) * node.component.funData.inC);
node.outValues = (bool *)malloc(sizeof(bool) * node.component.funData.outC);
node.dirty = true;
node.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_set_inval(Node *node, int pinIn, bool value) {
if (node->inValues[pinIn] != value) {
node->inValues[pinIn] = value;
node->dirty = true;
node_update(node);
}
}

Expand All @@ -43,4 +69,23 @@ void node_free(Node *node) {
free(node->connections);
free(node->inValues);
free(node->outValues);
}
}

void node_render(Node *node, Point camPos, float zoom) {
for (int i = 0; i < node->component.funData.outC; i++) {
if (node->connections[i].other) {
if (node->outValues[i])
component_render(&node->connections[i].wire, node->renderer, camPos, zoom, 0xff7700ff);
else
component_render(&node->connections[i].wire, node->renderer, camPos, zoom, 0x0000aaff);
}
}
if (node->component.type == CT_MODULE)
component_render(&node->component, node->renderer, camPos, zoom, 0xffffffff);
else if (node->component.type == CT_LED) {
if (node->inValues[0])
component_render(&node->component, node->renderer, camPos, zoom, 0xffff00ff);
else
component_render(&node->component, node->renderer, camPos, zoom, 0x555555ff);
}
}
7 changes: 6 additions & 1 deletion Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct Node;
typedef struct Connection {
int pinB;
struct Node *other;
ComponentData wire;
} Connection;

typedef struct Node {
Expand All @@ -16,16 +17,20 @@ typedef struct Node {
bool *inValues;
bool *outValues;
bool dirty;
SDL_Renderer *renderer;
} Node;

void node_create(Node *node, char *compName, Point pos, TTF_Font *font, SDL_Renderer *renderer);
Node node_create(char *compName, Point pos, TTF_Font *font, SDL_Renderer *renderer);
Node node_create_LED(Point pos, SDL_Renderer *renderer);

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

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

void node_update(Node *node);

void node_render(Node *node, Point camPos, float zoom);

void node_free(Node *node);

#endif //HOMEWORK_NODE_H
37 changes: 37 additions & 0 deletions NodeVector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#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;
}
memcpy(&(vector->nodes[vector->count]), &node, sizeof(node));
vector->count++;
}

void nodev_erase(NodeVector *vector, int index) {
for (int i = index + 1; i < vector->count; i++) {
vector->nodes[i - 1] = vector->nodes[i];
}
vector->count--;
}

void nodev_free(NodeVector *vector) {
for (int i = 0; i < vector->count; i++)
node_free(&vector->nodes[i]);
free(vector->nodes);
}
21 changes: 21 additions & 0 deletions NodeVector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef HOMEWORK_NODEVECTOR_H
#define HOMEWORK_NODEVECTOR_H

#include <stddef.h>
#include "Node.h"

typedef struct NodeVector {
Node *nodes;
size_t count;
size_t capacity;
} NodeVector;

NodeVector nodev_create(size_t count);

void nodev_push_back(NodeVector *vector, Node node);

void nodev_erase(NodeVector *vector, int index);

void nodev_free(NodeVector *vector);

#endif //HOMEWORK_NODEVECTOR_H
4 changes: 2 additions & 2 deletions debugmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
* calls to debugmalloc. Usage is the same, malloc(size)
* gives the address of a new memory block, free(ptr)
* deallocates etc.
*
*
* If you use this file, make sure that you include this
* in *ALL* translation units (*.c) of your source. The
* builtin free() function cannot deallocate a memory block
* that was allocated via debugmalloc, yet the name of
* the functions are the same!
*
*
* If there is a conflict with the macro names used here,
* you can include debugmalloc-nomacro.h instead. */

Expand Down
28 changes: 16 additions & 12 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
#include "Component.h"
#include "Window.h"
#include "Input.h"
#include "Parser.h"
#include "Node.h"
#include "NodeVector.h"

int main(int argc, char **argv) {

debugmalloc_log_file("debugmalloclog.txt");

SDL_Window *window;
Expand All @@ -24,17 +22,14 @@ int main(int argc, char **argv) {

TTF_Font *font = TTF_OpenFont("res/SourceCodePro-Regular.ttf", 80);

Node testNode, testNode2;
node_create(&testNode, "XOR", (Point){100, 100}, font, renderer);
node_create(&testNode2, "NOT", (Point){100, 100}, font, renderer);
NodeVector vec = nodev_create(0);

node_set_connection(&testNode, 0, &testNode2, 0);

testNode.inValues[0] = true;
testNode.inValues[1] = true;
vec.nodes[0].inValues[0] = false;
vec.nodes[0].inValues[1] = true;

node_update(&testNode);
node_update(&testNode2);
for (size_t i = 0; i < vec.count; i++)
node_update(&vec.nodes[i]);

SDL_Cursor *cursor = SDL_GetCursor();
Point cameraPos = {0, 0};
Expand Down Expand Up @@ -67,7 +62,8 @@ int main(int argc, char **argv) {
cameraPos.y -= (float) input_get_mouse_delta_y() / zoom;
}

component_render(&testNode.component, renderer, cameraPos, zoom);
for (size_t i = 0; i < vec.count; i++)
node_render(&vec.nodes[i], cameraPos, zoom);

if (input_get_mouse_button(SDL_BUTTON_MIDDLE).isPressed) {
SDL_FreeCursor(cursor);
Expand All @@ -81,6 +77,12 @@ int main(int argc, char **argv) {
SDL_SetCursor(cursor);
SDL_CaptureMouse(false);
}
if (input_get_key(SDL_SCANCODE_SPACE).isPressed) {
node_set_inval(&vec.nodes[0], 0, rand() % 2);
node_set_inval(&vec.nodes[0], 1, rand() % 2);
node_set_inval(&vec.nodes[2], 0, rand() % 2);
node_set_inval(&vec.nodes[2], 1, rand() % 2);
}

zoom *= powf(0.9f, -(float)input_get_mouse_wheel_y());
if (zoom < 0.05) zoom = 0.05f;
Expand All @@ -91,5 +93,7 @@ int main(int argc, char **argv) {
}
window_cleanup(window, renderer);

nodev_free(&vec);

return 0;
}
4 changes: 4 additions & 0 deletions res/LED.cmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0.5 0.5
C 0.25,0.25 0.2 e
L 0.1086,0.1086 0.3914,0.3914
L 0.1086,0.3914 0.3914,0.1086
2 changes: 2 additions & 0 deletions res/LED.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
in A 0.05,0.25 3.1415
3 changes: 3 additions & 0 deletions res/LED.fun
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
assigns 0
in 1
out 0

0 comments on commit d5b8480

Please sign in to comment.