WIP: encore un peu de cosmétique (variables globales > locales)

This commit is contained in:
Jean Sirmai 2023-09-27 10:59:48 +02:00
parent 98e95abbd6
commit 3f5d5eb6b7
Signed by: jean
GPG Key ID: FB3115C340E057E3
1 changed files with 218 additions and 228 deletions

View File

@ -41,221 +41,6 @@ static volatile int buffer_vertex_size = 0;
static volatile int buffer_lines_size = 0; static volatile int buffer_lines_size = 0;
static volatile int buffer_colors_size = 0; static volatile int buffer_colors_size = 0;
static volatile int buffer_plans_size = 0; static volatile int buffer_plans_size = 0;
static int pref_show_grids = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc
// xyz, 0, x, y, z, xy, xz, yz, xyz
static int space_X = 0;
static int space_Y = 0;
static int space_Z = 0;
static void show_arrows_array (GLuint *arrows, int arrows_nb, int address, int current_weigh, int weight, int site, int x, int y, int z);
static bool rewrite_arrow (GLuint *arrows, int arrows_nb, int address, int weight, int site, int x, int y, int z)
{
show_arrows_array(arrows, arrows_nb, address, *(arrows + address + 0), weight, site, x, y, z);
*(arrows + address + 0) = weight;
if (1) printf("the new arrow weight is %d at (%d, %d, %d, %d)\n", weight, site, x, y, z);
return 1; /* replaces the load of the existing arrow by the new load 'weight' */
}
static bool create_arrow (GLuint *arrows, int arrows_nb, int weight, int site, int x, int y, int z)
{
if (0) printf("\ncreating the new arrow (%d, %d, %d, %d, %d)",\
weight, site, x, y, z);
buffer_lines_size += (3 + 4) * 2;
buffer_lines_origin = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLuint));
*(arrows + arrows_nb * 5 + 0) = weight;
*(arrows + arrows_nb * 5 + 1) = site;
*(arrows + arrows_nb * 5 + 2) = x;
*(arrows + arrows_nb * 5 + 3) = y;
*(arrows + arrows_nb * 5 + 4) = z;
show_arrows_array(arrows, arrows_nb + 1, -1, -1, weight, site, x, y, z);
printf("buffer_lines_size = %d\tarrow (%d, %d, %d, %d, %d) was created",\
buffer_lines_size, weight, site, x, y, z);
return 1;
}
static bool erase_arrow (GLuint *arrows, int arrows_nb, int address, int site, int x, int y, int z)
{
arrows_nb --;
if (arrows_nb > 0) {
*(arrows + address + 0) = *(arrows + arrows_nb * 5 + 0);
*(arrows + address + 1) = *(arrows + arrows_nb * 5 + 1);
*(arrows + address + 2) = *(arrows + arrows_nb * 5 + 2);
*(arrows + address + 3) = *(arrows + arrows_nb * 5 + 3);
*(arrows + address + 4) = *(arrows + arrows_nb * 5 + 4);
}
buffer_lines_size -= (3 + 4) * 2;
buffer_lines_origin = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLuint));
show_arrows_array(arrows, arrows_nb, address, -1, 0, site, x, y, z);
if (1) printf("arrow previously at address (%d) with values (%d, %d, %d, %d) was erased;\
last arrow is now : (%d, %d, %d, %d, %d)\n", address / 5, site, x, y, z, *(arrows + (arrows_nb - 1) * 5 + 0),\
*(arrows + (arrows_nb - 1) * 5 + 1), *(arrows + (arrows_nb - 1) * 5 + 2), *(arrows + (arrows_nb - 1) * 5 + 3),\
*(arrows + (arrows_nb - 1) * 5 + 4));
return 1;
}
static void show_arrows_array (GLuint *arrows, int arrows_nb, int address, int current_weigh, int weight, int site, int x, int y, int z)
{
printf("\nset_arrow (%d | %2d %2d %2d %2d) <> ", weight, site, x, y, z);
if (arrows_nb == 0 && weight == 0) printf("buffer_lines_size = %d\tno arrow in space + no arrows to be created > end function set_arrow()\n", buffer_lines_size);
if (address == -1 && weight == 0) printf("buffer_lines_size = %d\tarrow not found + no arrows to be created > end function set_arrow()\n", buffer_lines_size);
if (address == -1 && weight > 0) printf("buffer_lines_size = %d\tarrow (%d, %d, %d, %d, %d) not found > new arrow will be created at line [%d]\n",\
buffer_lines_size, arrows_nb, weight, site, x, y, z);
if (address >= 0 && weight == 0) printf("buffer_lines_size = %d\tarrow found at line [%d] > will be erased\n", buffer_lines_size, address / 5);
if (address >= 0 && weight > 0) {
if (current_weigh != weight) printf("buffer_lines_size = %d\tarrow found at line [%d] > will be modified\n", buffer_lines_size, address / 5);
if (current_weigh == weight) printf("buffer_lines_size = %d\tarrow found at line [%d] > will not be modified\n", buffer_lines_size, address / 5);
}
printf(" [rank] load | site x y z\n");
for (int i = 0; i < arrows_nb; i++)
{
printf(" [%4d] = %2d | %2d, %2d, %2d, %2d ",\
i, *(arrows + i * 5 + 0), *(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\
*(arrows + i * 5 + 3), *(arrows + i * 5 + 4));
if (weight == 0 && i == address / 5) printf(" <<<| < - > (%d, %d, %d, %d) moved at [%2d]",\
*(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\
*(arrows + i * 5 + 3), *(arrows + i * 5 + 4), address / 5);
if (weight > 0 && current_weigh != weight && i == arrows_nb - 1 && address >= 0)
printf(" <<< < + > (%d, %d, %d, %d) added at [%2d]",\
*(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\
*(arrows + i * 5 + 3), *(arrows + i * 5 + 4), address / 5);
if (weight > 0 && current_weigh == weight && i == address / 5) printf(" <");
if (weight == 0 && i > address / 5 && i < arrows_nb - 1) printf(" |");
if (weight == 0 && i == arrows_nb - 1 && i != address / 5) printf(" |");
printf("\n");
}
}
static int set_arrow (GLuint *arrows, int arrows_nb, int weight, int site, int x, int y, int z)
{
// assert pas de doublons flèche(s)
int address = -1, current_weight = -1;
if (arrows_nb > 0)
for (int i = 0; i < arrows_nb; i++) {
if ((site == *(arrows + i * 5 + 1))
&& (x == *(arrows + i * 5 + 2))
&& (y == *(arrows + i * 5 + 3))
&& (z == *(arrows + i * 5 + 4)))
{
address = i * 5;
current_weight = *(arrows + i * 5 + 0);
break;
}
}
if (address == -1 && weight == 0) return 0;
if (address == -1 && weight > 0) return create_arrow (arrows, arrows_nb, weight, site, x, y, z);
if (address >= 0 && weight == 0) return erase_arrow (arrows, arrows_nb, address, site, x, y, z);
if (address >= 0 && current_weight != weight) return rewrite_arrow (arrows, arrows_nb, address, weight, site, x, y, z);
return 0;
}
/* /*
* Dynamic array of ptrs to dynamically allocated gl_area_entry * Dynamic array of ptrs to dynamically allocated gl_area_entry
@ -753,6 +538,215 @@ bool graphics_shutdown(const void *gl_area)
return true; return true;
} }
static void show_arrows_array (GLuint *arrows, int arrows_nb, int address, int current_weigh, int weight, int site, int x, int y, int z);
static bool rewrite_arrow (GLuint *arrows, int arrows_nb, int address, int weight, int site, int x, int y, int z)
{
show_arrows_array(arrows, arrows_nb, address, *(arrows + address + 0), weight, site, x, y, z);
*(arrows + address + 0) = weight;
if (1) printf("the new arrow weight is %d at (%d, %d, %d, %d)\n", weight, site, x, y, z);
return 1; /* replaces the load of the existing arrow by the new load 'weight' */
}
static bool create_arrow (GLuint *arrows, int arrows_nb, int weight, int site, int x, int y, int z)
{
if (0) printf("\ncreating the new arrow (%d, %d, %d, %d, %d)",\
weight, site, x, y, z);
buffer_lines_size += (3 + 4) * 2;
buffer_lines_origin = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLuint));
*(arrows + arrows_nb * 5 + 0) = weight;
*(arrows + arrows_nb * 5 + 1) = site;
*(arrows + arrows_nb * 5 + 2) = x;
*(arrows + arrows_nb * 5 + 3) = y;
*(arrows + arrows_nb * 5 + 4) = z;
show_arrows_array(arrows, arrows_nb + 1, -1, -1, weight, site, x, y, z);
printf("buffer_lines_size = %d\tarrow (%d, %d, %d, %d, %d) was created",\
buffer_lines_size, weight, site, x, y, z);
return 1;
}
static bool erase_arrow (GLuint *arrows, int arrows_nb, int address, int site, int x, int y, int z)
{
arrows_nb --;
if (arrows_nb > 0) {
*(arrows + address + 0) = *(arrows + arrows_nb * 5 + 0);
*(arrows + address + 1) = *(arrows + arrows_nb * 5 + 1);
*(arrows + address + 2) = *(arrows + arrows_nb * 5 + 2);
*(arrows + address + 3) = *(arrows + arrows_nb * 5 + 3);
*(arrows + address + 4) = *(arrows + arrows_nb * 5 + 4);
}
buffer_lines_size -= (3 + 4) * 2;
buffer_lines_origin = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLuint));
show_arrows_array(arrows, arrows_nb, address, -1, 0, site, x, y, z);
if (1) printf("arrow previously at address (%d) with values (%d, %d, %d, %d) was erased;\
last arrow is now : (%d, %d, %d, %d, %d)\n", address / 5, site, x, y, z, *(arrows + (arrows_nb - 1) * 5 + 0),\
*(arrows + (arrows_nb - 1) * 5 + 1), *(arrows + (arrows_nb - 1) * 5 + 2), *(arrows + (arrows_nb - 1) * 5 + 3),\
*(arrows + (arrows_nb - 1) * 5 + 4));
return 1;
}
static void show_arrows_array (GLuint *arrows, int arrows_nb, int address, int current_weigh, int weight, int site, int x, int y, int z)
{
printf("\nset_arrow (%d | %2d %2d %2d %2d) <> ", weight, site, x, y, z);
if (arrows_nb == 0 && weight == 0) printf("buffer_lines_size = %d\tno arrow in space + no arrows to be created > end function set_arrow()\n", buffer_lines_size);
if (address == -1 && weight == 0) printf("buffer_lines_size = %d\tarrow not found + no arrows to be created > end function set_arrow()\n", buffer_lines_size);
if (address == -1 && weight > 0) printf("buffer_lines_size = %d\tarrow (%d, %d, %d, %d, %d) not found > new arrow will be created at line [%d]\n",\
buffer_lines_size, arrows_nb, weight, site, x, y, z);
if (address >= 0 && weight == 0) printf("buffer_lines_size = %d\tarrow found at line [%d] > will be erased\n", buffer_lines_size, address / 5);
if (address >= 0 && weight > 0) {
if (current_weigh != weight) printf("buffer_lines_size = %d\tarrow found at line [%d] > will be modified\n", buffer_lines_size, address / 5);
if (current_weigh == weight) printf("buffer_lines_size = %d\tarrow found at line [%d] > will not be modified\n", buffer_lines_size, address / 5);
}
printf(" [rank] load | site x y z\n");
for (int i = 0; i < arrows_nb; i++)
{
printf(" [%4d] = %2d | %2d, %2d, %2d, %2d ",\
i, *(arrows + i * 5 + 0), *(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\
*(arrows + i * 5 + 3), *(arrows + i * 5 + 4));
if (weight == 0 && i == address / 5) printf(" <<<| < - > (%d, %d, %d, %d) moved at [%2d]",\
*(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\
*(arrows + i * 5 + 3), *(arrows + i * 5 + 4), address / 5);
if (weight > 0 && current_weigh != weight && i == arrows_nb - 1 && address >= 0)
printf(" <<< < + > (%d, %d, %d, %d) added at [%2d]",\
*(arrows + i * 5 + 1), *(arrows + i * 5 + 2),\
*(arrows + i * 5 + 3), *(arrows + i * 5 + 4), address / 5);
if (weight > 0 && current_weigh == weight && i == address / 5) printf(" <");
if (weight == 0 && i > address / 5 && i < arrows_nb - 1) printf(" |");
if (weight == 0 && i == arrows_nb - 1 && i != address / 5) printf(" |");
printf("\n");
}
}
static int set_arrow (GLuint *arrows, int arrows_nb, int weight, int site, int x, int y, int z)
{
// assert pas de doublons flèche(s)
int address = -1, current_weight = -1;
if (arrows_nb > 0)
for (int i = 0; i < arrows_nb; i++) {
if ((site == *(arrows + i * 5 + 1))
&& (x == *(arrows + i * 5 + 2))
&& (y == *(arrows + i * 5 + 3))
&& (z == *(arrows + i * 5 + 4)))
{
address = i * 5;
current_weight = *(arrows + i * 5 + 0);
break;
}
}
if (address == -1 && weight == 0) return 0;
if (address == -1 && weight > 0) return create_arrow (arrows, arrows_nb, weight, site, x, y, z);
if (address >= 0 && weight == 0) return erase_arrow (arrows, arrows_nb, address, site, x, y, z);
if (address >= 0 && current_weight != weight) return rewrite_arrow (arrows, arrows_nb, address, weight, site, x, y, z);
return 0;
}
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static void show_user_choices(GLuint *arrows, int arrows_nb, static void show_user_choices(GLuint *arrows, int arrows_nb,
@ -760,9 +754,9 @@ static void show_user_choices(GLuint *arrows, int arrows_nb,
int prefer, int show_all) int prefer, int show_all)
{ {
printf("model + user constraints :\tspace size x,y,z = %d,%d,%d\t", space_size_x, space_size_y, space_size_z); printf("model + user constraints :\tspace size x,y,z = %d,%d,%d\t", space_size_x, space_size_y, space_size_z);
if (prefer == 0) printf("prefer = %d <> show all grids", prefer); if (prefer == 0) printf("prefer = %d <> show all grids", prefer);
if (prefer == 1) printf("prefer = %d <> show no grid", prefer); if (prefer == 1) printf("prefer = %d <> show no grid", prefer);
if (prefer > 1) printf("pref_show_grids = %d", prefer); if (prefer > 1) printf("show grids according rule (%d)", prefer);
if (show_all) { if (show_all) {
printf("\n\t\t\t\t\t buffer_vertex_size = %d\t buffer_lines_size = %d", buffer_vertex_size, buffer_lines_size); printf("\n\t\t\t\t\t buffer_vertex_size = %d\t buffer_lines_size = %d", buffer_vertex_size, buffer_lines_size);
if (arrows_nb > 0) printf("\n [%2d] load | site x y z\ if (arrows_nb > 0) printf("\n [%2d] load | site x y z\
@ -774,21 +768,17 @@ static void show_user_choices(GLuint *arrows, int arrows_nb,
void main_test_graphics (void) void main_test_graphics (void)
{ {
if (0) printf("buffer_vertex_offset_before_grid = %d\n", buffer_vertex_size / 3);
// assert : space dimensions (x,y,z) > 0 TODO // assert : space dimensions (x,y,z) > 0 TODO
// assert : arrows localization within space and sites TODO // assert : arrows localization within space and sites TODO
// assert : no more than one arrow per address TODO // assert : no more than one arrow per address TODO
space_X = 4; space_Y = 1; space_Z = 1; int space_X = 4, space_Y = 1, space_Z = 1;
pref_show_grids = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc int pref_show_grids = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc
// xyz, 0, x, y, z, xy, xz, yz, xyz // xyz, 0, x, y, z, xy, xz, yz, xyz
GLuint arrows[] = { GLuint arrows[] = {
1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 0,
1, 1, 1, 0, 0, 1, 1, 1, 0, 0,
1, 0, 2, 0, 0, 1, 0, 2, 0, 0,
1, 1, 2, 0, 0,
1, 1, 3, 0, 0, 1, 1, 3, 0, 0,
// load, site, x, y, z // load, site, x, y, z
}; };
@ -797,9 +787,6 @@ void main_test_graphics (void)
show_user_choices(arrows, arrows_nb, space_X, space_Y, space_Z, pref_show_grids, 1); show_user_choices(arrows, arrows_nb, space_X, space_Y, space_Z, pref_show_grids, 1);
long weight = 0, site = 0,
stx = space_Z * space_Y, sty = space_Z, stz = 1, arrow_offset = 0;
/* GRID */ /* GRID */
grid_write_intersections (space_X, space_Y, space_Z); grid_write_intersections (space_X, space_Y, space_Z);
@ -824,6 +811,9 @@ void main_test_graphics (void)
// set_arrow (arrows, arrows_nb, 0, 0, 0, 0, 0); arrows_nb --; // set_arrow (arrows, arrows_nb, 0, 0, 0, 0, 0); arrows_nb --;
// set_arrow (arrows, arrows_nb, 1, 0, 5, 0, 0); arrows_nb ++; // set_arrow (arrows, arrows_nb, 1, 0, 5, 0, 0); arrows_nb ++;
int weight = 0, site = 0,
stx = space_Z * space_Y, sty = space_Z, stz = 1, arrow_offset = 0;
for (int i = 0; i < arrows_nb; i++) { for (int i = 0; i < arrows_nb; i++) {
weight = arrows[i * 5 + 0]; weight = arrows[i * 5 + 0];
site = arrows[i * 5 + 1]; site = arrows[i * 5 + 1];