Added draw_text() to NEDsim and refactored all the drawing functions to use it.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Sun, 11 Jul 2021 00:31:43 +0000 (17:31 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Sun, 11 Jul 2021 00:31:43 +0000 (17:31 -0700)
hacks/NEDsim/NEDsim.c

index 8331334..f710105 100644 (file)
@@ -338,6 +338,25 @@ draw_circular_area(struct NEDsim * nedsim, size_t x, size_t y, double diameter)
     XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc, x, y, diameter, diameter, 0, 360*64);
 }
 
     XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc, x, y, diameter, diameter, 0, 360*64);
 }
 
+// Draws text in a square area with upper left corner at (x_origin,y_origin).
+// Requires that set_font_size() has been run at least once. All values are in
+// units of 'cells'.
+static void
+draw_text(struct NEDsim * nedsim, const char * text, int x_origin, int y_origin, int x_size, int y_size, Bool horizontally_center)
+{
+    set_color(nedsim, &color_list[nedsim->color_index].text);
+
+    int text_x_size, text_y_size;
+    get_text_size(nedsim, text, &text_x_size, &text_y_size);
+
+    int local_y_offset = ((y_size * nedsim->cell_size) - text_y_size) / 2;
+    int local_x_offset = 0;
+    if (horizontally_center) local_x_offset = ((x_size * nedsim->cell_size) - text_x_size) / 2;
+
+    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, (x_origin * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
+            ((y_origin + y_size) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), text, strlen(text));
+}
+
 // Draws the panel itself. Not the lights/labels/etc, but the flat sheet of
 // metal that is the front panel.
 static void
 // Draws the panel itself. Not the lights/labels/etc, but the flat sheet of
 // metal that is the front panel.
 static void
@@ -421,22 +440,12 @@ draw_logo(struct NEDsim * nedsim)
     draw_rect_area(nedsim, LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT, LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, False, True, False, False);
 
     // Now draw the 'NED' text in the top box.
     draw_rect_area(nedsim, LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT, LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, False, True, False, False);
 
     // Now draw the 'NED' text in the top box.
-    set_color(nedsim, &color_list[nedsim->color_index].text);
     set_font_size(nedsim, LOGO_NAME_HEIGHT);
     set_font_size(nedsim, LOGO_NAME_HEIGHT);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "NED", &text_x_size, &text_y_size);
-    int local_x_offset = ((LOGO_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((LOGO_NAME_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, (LOGO_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((LOGO_Y_OFFSET+LOGO_NAME_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "NED", 3);
+    draw_text(nedsim, "NED", LOGO_X_OFFSET, LOGO_Y_OFFSET, LOGO_WIDTH, LOGO_NAME_HEIGHT, True);
 
     // And draw the 'subgeniuskitty.com' text in the bottom box.
     set_font_size(nedsim, LOGO_WEBSITE_HEIGHT);
 
     // And draw the 'subgeniuskitty.com' text in the bottom box.
     set_font_size(nedsim, LOGO_WEBSITE_HEIGHT);
-    get_text_size(nedsim, "subgeniuskitty.com", &text_x_size, &text_y_size);
-    local_x_offset = ((LOGO_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    local_y_offset = ((LOGO_WEBSITE_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, (LOGO_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((LOGO_Y_OFFSET+LOGO_NAME_HEIGHT+LOGO_WEBSITE_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "subgeniuskitty.com", 18);
+    draw_text(nedsim, "subgeniuskitty.com", LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT, LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, True);
 }
 
 // Draw the HALT indicator area on the front panel.
 }
 
 // Draw the HALT indicator area on the front panel.
@@ -450,13 +459,7 @@ draw_halt(struct NEDsim * nedsim)
     draw_rect_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT, HALT_WIDTH, HALT_LABEL_HEIGHT, False, True, False, False);
 
     // And finally, draw the label.
     draw_rect_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT, HALT_WIDTH, HALT_LABEL_HEIGHT, False, True, False, False);
 
     // And finally, draw the label.
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "HALT", &text_x_size, &text_y_size);
-    int local_x_offset = ((HALT_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((HALT_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, (HALT_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((HALT_Y_OFFSET+HALT_LIGHT_HEIGHT+HALT_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "HALT", 4);
+    draw_text(nedsim, "HALT", HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT, HALT_WIDTH, HALT_LABEL_HEIGHT, True);
 }
 
 // Draw the 32 lights corresponding to 'word' at coordinates ('x','y').
 }
 
 // Draw the 32 lights corresponding to 'word' at coordinates ('x','y').
@@ -508,13 +511,7 @@ draw_pc(struct NEDsim * nedsim)
     draw_wordline(nedsim, PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT);
 
     // Now draw the label text "PC".
     draw_wordline(nedsim, PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT);
 
     // Now draw the label text "PC".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "PC", &text_x_size, &text_y_size);
-    int local_x_offset = ((PC_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((PC_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, (PC_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((PC_Y_OFFSET+PC_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "PC", 2);
+    draw_text(nedsim, "PC", PC_X_OFFSET, PC_Y_OFFSET, PC_WIDTH, PC_LABEL_HEIGHT, True);
 }
 
 // Draw the Stack Counter area (but don't populate it yet).
 }
 
 // Draw the Stack Counter area (but don't populate it yet).
@@ -528,13 +525,7 @@ draw_sc(struct NEDsim * nedsim)
     draw_rect_area(nedsim, SC_X_OFFSET, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_WIDTH, SC_LIGHT_HEIGHT, False, True, False, False);
 
     // Now draw the label text "SC".
     draw_rect_area(nedsim, SC_X_OFFSET, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_WIDTH, SC_LIGHT_HEIGHT, False, True, False, False);
 
     // Now draw the label text "SC".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "SC", &text_x_size, &text_y_size);
-    int local_x_offset = ((SC_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((SC_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, (SC_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((SC_Y_OFFSET+SC_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "SC", 2);
+    draw_text(nedsim, "SC", SC_X_OFFSET, SC_Y_OFFSET, SC_WIDTH, SC_LABEL_HEIGHT, True);
 }
 
 // Draw areas for the two PSW flags, 'Z'ero and 'N'egative.
 }
 
 // Draw areas for the two PSW flags, 'Z'ero and 'N'egative.
@@ -551,22 +542,9 @@ draw_psw(struct NEDsim * nedsim)
     set_color(nedsim, &color_list[nedsim->color_index].tertiary);
     draw_rect_area(nedsim, (PSW_Z_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT, False, True, False, False);
 
     set_color(nedsim, &color_list[nedsim->color_index].tertiary);
     draw_rect_area(nedsim, (PSW_Z_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT, False, True, False, False);
 
-    // Now draw the label text "N".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "N", &text_x_size, &text_y_size);
-    int local_x_offset = ((PSW_LABEL_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((PSW_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, (PSW_N_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((PSW_Y_OFFSET+PSW_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "N", 1);
-
-    // Now draw the label text "Z".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    get_text_size(nedsim, "Z", &text_x_size, &text_y_size);
-    local_x_offset = ((PSW_LABEL_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    local_y_offset = ((PSW_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, (PSW_Z_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((PSW_Y_OFFSET+PSW_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "Z", 1);
+    // Now draw the label text.
+    draw_text(nedsim, "N", PSW_N_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True);
+    draw_text(nedsim, "Z", PSW_Z_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True);
 }
 
 // Draw the stack area (but don't populate it yet).
 }
 
 // Draw the stack area (but don't populate it yet).
@@ -581,12 +559,7 @@ draw_stack(struct NEDsim * nedsim)
     }
 
     // Now draw the label text "Stack Size:".
     }
 
     // Now draw the label text "Stack Size:".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "Stack Size:", &text_x_size, &text_y_size);
-    int local_y_offset = ((STACK_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, ((STACK_X_OFFSET + 1) * nedsim->cell_size + nedsim->origin_x_offset),
-            ((STACK_Y_OFFSET+STACK_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "Stack Size:", 11);
+    draw_text(nedsim, "Stack Size:", STACK_X_OFFSET+1, STACK_Y_OFFSET, STACK_WIDTH, STACK_LABEL_HEIGHT, False);
 }
 
 // Draw the heap area (but don't populate it yet).
 }
 
 // Draw the heap area (but don't populate it yet).
@@ -603,22 +576,13 @@ draw_heap(struct NEDsim * nedsim)
         draw_wordline(nedsim, HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i);
     }
 
         draw_wordline(nedsim, HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i);
     }
 
-    // Now draw the label text "RAM Base:".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "RAM Base:", &text_x_size, &text_y_size);
-    int local_y_offset = ((HEAP_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, ((HEAP_X_OFFSET + 1) * nedsim->cell_size + nedsim->origin_x_offset),
-            ((HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "RAM Base:", 9);
+    // Now draw the text label "RAM Base:".
+    draw_text(nedsim, "RAM Base:", HEAP_X_OFFSET+1, HEAP_Y_OFFSET, HEAP_WIDTH, HEAP_LABEL_HEIGHT, False);
 
     // Now draw the address text.
 
     // Now draw the address text.
-    set_color(nedsim, &color_list[nedsim->color_index].text);
     char address[11];
     snprintf(address, sizeof(address), "0x%08X", HEAP_START_ADDRESS);
     char address[11];
     snprintf(address, sizeof(address), "0x%08X", HEAP_START_ADDRESS);
-    get_text_size(nedsim, address, &text_x_size, &text_y_size);
-    local_y_offset = ((HEAP_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, ((HEAP_X_OFFSET + 1 + (HEAP_WIDTH / 2)) * nedsim->cell_size + nedsim->origin_x_offset),
-            ((HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), address, strlen(address));
+    draw_text(nedsim, address, (HEAP_X_OFFSET+(HEAP_WIDTH/2)+1), HEAP_Y_OFFSET, HEAP_WIDTH, HEAP_LABEL_HEIGHT, False);
 }
 
 // After the static front panel has been drawn at least once, this function
 }
 
 // After the static front panel has been drawn at least once, this function
@@ -679,14 +643,9 @@ update_display(struct NEDsim * nedsim)
     // Draw the stack size in text.
     set_color(nedsim, &color_list[nedsim->color_index].tertiary);
     draw_rect_area(nedsim, STACK_X_OFFSET+(STACK_WIDTH/2), STACK_Y_OFFSET, STACK_WIDTH/2, STACK_LABEL_HEIGHT, True, True, False, False);
     // Draw the stack size in text.
     set_color(nedsim, &color_list[nedsim->color_index].tertiary);
     draw_rect_area(nedsim, STACK_X_OFFSET+(STACK_WIDTH/2), STACK_Y_OFFSET, STACK_WIDTH/2, STACK_LABEL_HEIGHT, True, True, False, False);
-    set_color(nedsim, &color_list[nedsim->color_index].text);
     char stack_size[11];
     snprintf(stack_size, sizeof(stack_size), "0x%08X", nedsim->nedstate->active_thread->sp);
     char stack_size[11];
     snprintf(stack_size, sizeof(stack_size), "0x%08X", nedsim->nedstate->active_thread->sp);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, stack_size, &text_x_size, &text_y_size);
-    int local_y_offset = ((STACK_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, ((STACK_X_OFFSET + 1 + (STACK_WIDTH / 2)) * nedsim->cell_size + nedsim->origin_x_offset),
-            ((STACK_Y_OFFSET+STACK_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), stack_size, strlen(stack_size));
+    draw_text(nedsim, stack_size, (STACK_X_OFFSET+(STACK_WIDTH/2)+1), STACK_Y_OFFSET, STACK_WIDTH, STACK_LABEL_HEIGHT, False);
 
     // Draw the heap lights.
     for (i = 0; i < nedsim->num_data_rows; i++) {
 
     // Draw the heap lights.
     for (i = 0; i < nedsim->num_data_rows; i++) {