Skip to content

Commit

Permalink
Added wire handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gorbit99 committed Oct 16, 2019
1 parent b487faa commit 1aad009
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
39 changes: 39 additions & 0 deletions Component.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,43 @@ void component_render(ComponentData *dat, SDL_Renderer *renderer, Point camPos)
dat->h
};
SDL_RenderCopy(renderer, dat->texture, NULL, &r);
}

SDL_Surface *component_create_wire(Point V1, Point V2, float ang1, float ang2, float size, float thickness) {
float dx1 = cosf(ang1) * size / 5;
float dy1 = sinf(ang1) * size / 5;
float dx2 = cosf(ang2) * size / 5;
float dy2 = sinf(ang2) * size / 5;

Point C1 = {V1.x + dx1, V1.y + dy1};
Point C2 = {V2.x + dx2, V2.y + dy2};
float minX = V1.x;
if (V2.x < minX) minX = V2.x;
if (C1.x < minX) minX = C1.x;
if (C2.x < minX) minX = C2.x;
float maxX = V1.x;
if (V2.x > maxX) maxX = V2.x;
if (C1.x > maxX) maxX = C1.x;
if (C2.x > maxX) maxX = C2.x;
float minY = V1.y;
if (V2.y < minY) minY = V2.y;
if (C1.y < minY) minY = C1.y;
if (C2.y < minY) minY = C2.y;
float maxY = V1.y;
if (V2.y > maxY) maxY = V2.y;
if (C1.y > maxY) maxY = C1.y;
if (C2.y > maxY) maxY = C2.y;

SDL_Surface *wire = get_rgba_surface((int)(3 * thickness + maxX - minX), (int)(3 * thickness + maxY - minY));
V1.x += 1.5f * thickness - minX;
V1.y += 1.5f * thickness - minY;
V2.x += 1.5f * thickness - minX;
V2.y += 1.5f * thickness - minY;
C1.x += 1.5f * thickness - minX;
C1.y += 1.5f * thickness - minY;
C2.x += 1.5f * thickness - minX;
C2.y += 1.5f * thickness - minY;

gfx_draw_bezier_cubic(wire, V1, V2, C1, C2, thickness, 0xffffffff);
return wire;
}
3 changes: 3 additions & 0 deletions Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <SDL.h>
#include <stdio.h>
#include <math.h>

#include "Utility.h"
#include "Graphics.h"
Expand All @@ -25,4 +26,6 @@ void component_free_data(ComponentData *dat);

void component_render(ComponentData *dat, SDL_Renderer *renderer, Point camPos);

SDL_Surface *component_create_wire(Point V1, Point V2, float ang1, float ang2, float size, float thickness);

#endif //HOMEWORK_COMPONENT_H
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main(int argc, char **argv) {

TTF_Font *font = TTF_OpenFont("res/SourceCodePro-Regular.ttf", 25);
SDL_Texture *testT;
SDL_Surface *test = component_load_graphic("res/XNOR.cmp", 100, 3, font);
SDL_Surface *test = component_load_graphic("res/AND.cmp", 100, 3, font);
testT = SDL_CreateTextureFromSurface(renderer, test);
SDL_FreeSurface(test);

Expand Down Expand Up @@ -82,7 +82,7 @@ int main(int argc, char **argv) {
SDL_DestroyTexture(testT);
TTF_CloseFont(font);
font = TTF_OpenFont("res/SourceCodePro-Regular.ttf", (int)(25 * zoom));
SDL_Surface *surf = component_load_graphic("res/XNOR.cmp", 100 * zoom, 3 * zoom, font);
SDL_Surface *surf = component_load_graphic("res/AND.cmp", 100 * zoom, 3 * zoom, font);
testT = SDL_CreateTextureFromSurface(renderer, surf);
SDL_FreeSurface(surf);
component_update_texture(&data,testT);
Expand Down
1 change: 1 addition & 0 deletions res/AND.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
in

0 comments on commit 1aad009

Please sign in to comment.