Add ID to t_node so that it can be drawn easily.
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Fri, 16 Feb 2024 22:00:58 +0000 (23:00 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Fri, 16 Feb 2024 22:00:58 +0000 (23:00 +0100)
inc/FET_sim.h
src/build_helper.c
src/c_draw.c
src/colors.c
src/main.c

index 612fc5df5953b9323a7de4fc093428fc0b5eadf2..4647b11c640edda92a005a225214eb3a2f3140e3 100644 (file)
@@ -27,6 +27,7 @@ typedef enum e_terminal
 
 typedef struct s_node
 {
+       size_t  id;
        int             checked;
        t_state state;
        t_state set_state;
@@ -102,7 +103,7 @@ int         process_input(t_vec *nodes, t_vec *mosfets);
 int            get_input(t_input *input);
 
 const char     *state_color_escape(t_state state);
-void           draw_single(t_vec *nodes, t_vec *mosfets, size_t i);
+void           draw_single(t_vec *mosfets, size_t i);
 
 void   update_nodes(t_vec *nodes);
 int            sim_step(t_vec *nodes, t_vec *mosfets);
@@ -120,7 +121,7 @@ t_state     reduce_state(t_state state);
 int    c_addfet(t_input input, t_vec *mosfets);
 int    c_addnode(t_input input, t_vec *nodes);
 int    c_bind(t_input input, t_vec *nodes, t_vec *mosfets);
-int    c_draw(t_input input, t_vec *nodes, t_vec *mosfets);
+int    c_draw(t_input input, t_vec *mosfets);
 int    c_help(t_input input);
 int    c_next(t_input input, t_vec *nodes, t_vec *mosfets);
 int    c_setnode(t_input input, t_vec *nodes);
index 20a9f7db82e5e589ba79980685b0238ce26cbb3b..5a856f2cfabaa1495b9b83f1e821787ec61d5904 100644 (file)
@@ -36,6 +36,7 @@ void  add_node(t_vec *nodes, t_state set_state)
        t_node  node;
 
        ft_memset(&node, 0, sizeof(node));
+       node.id = nodes->size;
        node.checked = 0;
        node.state = set_state;
        node.set_state = set_state;
index b9d354c42e87af7b0ce9b2319578fd81bd8e2f23..b0d8a7e52a2979a618d0def96340c55e58d67eec 100644 (file)
@@ -1,7 +1,7 @@
 #include "FET_sim.h"
 #include "libft.h"
 
-int    c_draw(t_input input, t_vec *nodes, t_vec *mosfets)
+int    c_draw(t_input input, t_vec *mosfets)
 {
        size_t  i;
 
@@ -10,13 +10,13 @@ int c_draw(t_input input, t_vec *nodes, t_vec *mosfets)
                i = 0;
                while (i < mosfets->size)
                {
-                       draw_single(nodes, mosfets, i);
+                       draw_single(mosfets, i);
                        ++i;
                }
        }
        else if (input.argc == 1 && input.argv[0].type == num)
        {
-               draw_single(nodes, mosfets, input.argv[0].val.num);
+               draw_single(mosfets, input.argv[0].val.num);
        }
        /*else
        {
index deb0795560810ac41e6e198ecbfa4823254058bf..21f75cd8d734c4326e0156621417c014a32cf524 100644 (file)
@@ -20,15 +20,14 @@ const char  *state_color_escape(t_state state)
        return (g_red_ac);
 }
 
-void   draw_single(t_vec *nodes, t_vec *mosfets, size_t i)
+void   draw_single(t_vec *mosfets, size_t i)
 {
        t_mosfet        *mosfet;
-       size_t          index;
 
        mosfet = ft_vec_access(mosfets, i);
-       if (ft_vec_find_index(nodes, mosfet->source, &index) == success)
+       if (mosfet->source)
        {
-               ft_printf("      %u\n", index);
+               ft_printf("      %u\n", mosfet->source->id);
                ft_printf("      %s|%s\n", state_color_escape(mosfet->source->state), g_default_ac);
        }
        else
@@ -36,14 +35,14 @@ void        draw_single(t_vec *nodes, t_vec *mosfets, size_t i)
                ft_printf("      %sNULL%s\n", g_red_ac, g_default_ac);
                ft_printf("      %s|%s\n", g_red_ac, g_default_ac);
        }
-       if (ft_vec_find_index(nodes, mosfet->gate, &index) == success)
-               ft_printf("%4u%s--%s%c\n", index, state_color_escape(mosfet->gate->state), g_default_ac, mosfet->type);
+       if (mosfet->gate)
+               ft_printf("%4u%s--%s%c\n", mosfet->gate->id, state_color_escape(mosfet->gate->state), g_default_ac, mosfet->type);
        else
                ft_printf("%sNULL--%s%c\n", g_red_ac, g_default_ac, mosfet->type);
-       if (ft_vec_find_index(nodes, mosfet->drain, &index) == success)
+       if (mosfet->drain)
        {
                ft_printf("      %s|%s\n", state_color_escape(mosfet->drain->state), g_default_ac);
-               ft_printf("      %u\n\n", index);
+               ft_printf("      %u\n\n", mosfet->drain->id);
        }
        else
        {
index 9d51dfbc81b3db28315ced5ebd21d85ac4f36ee3..e7e9ab3240c674464a891e8167c187ad384f6412 100644 (file)
@@ -244,7 +244,7 @@ int process_input(t_vec *nodes, t_vec *mosfets)
        if (input.command == next)
                res = c_next(input, nodes, mosfets);
        else if (input.command == draw)
-               res = c_draw(input, nodes, mosfets);
+               res = c_draw(input, mosfets);
        else if (input.command == setnode)
                res = c_setnode(input, nodes);
        else if (input.command == addnode)