17 #include "../include/tscope5/graphics.h"
18 #include "../include/tscope5/graphics_internal.h"
19 #include "../include/tscope5/system_internal.h"
20 #include "../include/tscope5/textio_internal.h"
22 #include <allegro5/allegro_color.h>
62 ts5_check_graphics(
"ts5_set_coordinate_system");
63 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_coordinate_system(%d)\n", coordinate_system);
65 int retval = _ts5_status.graphics_status.coordinate_system;
67 if (coordinate_system != TS5_CARTESIAN_COORDINATES && coordinate_system != TS5_DISPLAY_COORDINATES) {
68 ts5_fatal(
"ts5_set_coordinate_system: coordinate_system should be 0 or 1 (is %d)\n",
72 _ts5_status.graphics_status.coordinate_system = coordinate_system;
74 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_coordinate_system: set coordinate_system for next display to %d (was %d)\n",
75 _ts5_status.graphics_status.coordinate_system, retval);
88 ts5_check_graphics(
"ts5_get_coordinate_system");
89 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_coordinate_system()\n");
91 return _ts5_status.graphics_status.coordinate_system;
114 ts5_check_graphics(
"ts5_set_coordinate_scale");
115 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_coordinate_scale(%d)\n", coordinate_scale);
117 int retval = _ts5_status.graphics_status.coordinate_scale;
119 if (coordinate_scale != TS5_RELATIVE_COORDINATES && coordinate_scale != TS5_ABSOLUTE_COORDINATES) {
120 ts5_fatal(
"ts5_set_coordinate_scale: coordinate_scale should be 0 or 1 (is %d)\n", coordinate_scale);
123 _ts5_status.graphics_status.coordinate_scale = coordinate_scale;
125 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_coordinate_scale: set coordinate_scale for next display to %d (was %d)\n",
126 _ts5_status.graphics_status.coordinate_scale, retval);
139 ts5_check_graphics(
"ts5_get_coordinate_scale");
140 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_coordinate_scale()\n");
142 return _ts5_status.graphics_status.coordinate_scale;
158 ts5_check_graphics(
"ts5_get_target_size");
159 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_target_size(%p,%p)\n", w, h);
163 if (_ts5_status.graphics_status.coordinate_scale==TS5_ABSOLUTE_COORDINATES) {
164 ww = _ts5_status.graphics_status.target_width;
165 hh = _ts5_status.graphics_status.target_height;
191 ts5_check_graphics(
"ts5_get_target_width");
192 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_target_width()\n");
210 ts5_check_graphics(
"ts5_get_target_height");
211 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_target_height()\n");
242 ts5_check_graphics(
"ts5_relative_to_absolute_coordinate_x");
243 ts5_log(TS5_LOGLEVEL_6,
"ts5_relative_to_absolute_coordinate_x(%f)\n", x);
247 if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
248 retval = (x * (_ts5_status.graphics_status.target_width) / 2.0);
251 retval = (x * _ts5_status.graphics_status.target_width);
271 ts5_check_graphics(
"ts5_relative_to_absolute_coordinate_y");
272 ts5_log(TS5_LOGLEVEL_6,
"ts5_relative_to_absolute_coordinate_y(%f)\n", y);
276 if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
277 retval = (y * (_ts5_status.graphics_status.target_height) / 2.0);
280 retval = (y * _ts5_status.graphics_status.target_height);
297 ts5_check_graphics(
"ts5_absolute_to_relative_coordinate_x");
298 ts5_log(TS5_LOGLEVEL_6,
"ts5_absolute_to_relative_coordinate_x(%f)\n", x);
302 if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
303 retval = (x / (_ts5_status.graphics_status.target_width) * 2.0);
306 retval = (x / _ts5_status.graphics_status.target_width);
324 ts5_check_graphics(
"ts5_absolute_to_relative_coordinate_y");
325 ts5_log(TS5_LOGLEVEL_6,
"ts5_absolute_to_relative_coordinate_y(%f)\n", y);
329 if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
330 retval = (y / (_ts5_status.graphics_status.target_height) * 2.0);
333 retval = (y / _ts5_status.graphics_status.target_height);
351 ts5_check_graphics(
"ts5_cartesian_to_display_coordinate_x");
352 ts5_log(TS5_LOGLEVEL_6,
"ts5_cartesian_to_display_coordinate_x(%f)\n", x);
354 return (_ts5_status.graphics_status.target_width) / 2.0 + x;
369 ts5_check_graphics(
"ts5_cartesian_to_display_coordinate_y");
370 ts5_log(TS5_LOGLEVEL_6,
"ts5_cartesian_to_display_coordinate_y(%f)\n", y);
372 return (_ts5_status.graphics_status.target_height) / 2.0 - y;
387 ts5_check_graphics(
"ts5_display_to_cartesian_coordinate_x");
388 ts5_log(TS5_LOGLEVEL_6,
"ts5_display_to_cartesian_coordinate_x(%f)\n", x);
390 return x - (_ts5_status.graphics_status.target_width) / 2.0;
405 ts5_check_graphics(
"ts5_display_to_cartesian_coordinate_y");
406 ts5_log(TS5_LOGLEVEL_6,
"ts5_display_to_cartesian_coordinate_y(%f)\n", y);
408 return (_ts5_status.graphics_status.target_height) / 2.0 - y;
444 ts5_check_graphics(
"ts5_make_rgb_color");
445 ts5_log(TS5_LOGLEVEL_5,
"ts5_make_rgb_color(%f,%f,%f,%f)\n", r, g, b, a);
447 if (r < 0.0|| g < 0.0 || b < 0.0 || a < 0.0 || r > 1.0 || g > 1.0 || b > 1.0 || a > 1.0) {
448 ts5_fatal(
"ts5_make_rgb_color: color values must be between 0.0 and 1.0\n");
451 return al_map_rgba_f(r, g, b, a);
467 ts5_check_graphics(
"ts5_make_hsl_color");
468 ts5_log(TS5_LOGLEVEL_5,
"ts5_make_hsl_color(%f,%f,%f,%f)\n", h, s, l, a);
470 if (h < 0.0 || s < 0.0 || l < 0.0 || a < 0.0 || h > 360.0 || s > 1.0 || l > 1.0 || a > 1.0) {
471 ts5_fatal(
"ts5_makehslcolor: h:0.0-360.0, s:0.0-1.0, l:0.0-1.0, a:0.0-1.0\n");
475 al_color_hsl_to_rgb(h, s, l, &r, &g, &b);
477 return al_map_rgba_f(r, g, b, a);
493 ts5_check_graphics(
"ts5_make_hsv_color");
494 ts5_log(TS5_LOGLEVEL_5,
"ts5_make_hsv_color(%f,%f,%f,%f)\n", h, s, v, a);
496 if (h < 0.0 || s < 0.0 || v < 0.0 || a < 0.0 || h > 360.0 || s > 1.0 || v > 1.0 || a > 1.0) {
497 ts5_fatal(
"ts5_makehsvcolor: h:0.0-360.0, s:0.0-1.0, v:0.0-1.0, a:0.0-1.0\n");
501 al_color_hsv_to_rgb(h, s, v, &r, &g, &b);
503 return al_map_rgba_f(r, g, b, a);
539 ts5_check_graphics(
"ts5_make_named_color");
540 ts5_log(TS5_LOGLEVEL_5,
"ts5_make_named_color(%s,%f)\n", name, a);
544 if (!al_color_name_to_rgb(name, &r, &g, &b)) {
545 ts5_fatal(
"ts5_make_named_color: color name %s not found\n", name);
548 return al_map_rgba_f(r, g, b, a);
592 ts5_check_graphics(
"ts5_set_foreground_color");
593 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_foreground_color((%f,%f,%f,%f))\n",
594 foreground_color.r, foreground_color.g, foreground_color.b,
597 TS5_COLOR oldcolor = _ts5_status.graphics_status.foreground_color;
598 _ts5_status.graphics_status.foreground_color = foreground_color;
600 unsigned char r, g, b, a;
601 al_unmap_rgba(foreground_color, &r, &g, &b, &a);
602 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_foreground_color r:%f, g:%f, b:%f, alpha:%f\n",
603 r/256, g/256, b/256, a/256);
616 ts5_check_graphics(
"ts5_get_foreground_color");
617 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_foreground_color()\n");
619 return _ts5_status.graphics_status.foreground_color;
636 ts5_check_graphics(
"ts5_set_background_color");
637 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_background_color((%f,%f,%f,%f))\n",
638 background_color.r, background_color.g, background_color.b,
641 TS5_COLOR oldcolor = _ts5_status.graphics_status.background_color;
642 _ts5_status.graphics_status.background_color = background_color;
644 unsigned char r, g, b, a;
645 al_unmap_rgba(background_color, &r, &g, &b, &a);
646 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_background_color r:%f, g:%f, b:%f, alpha:%f\n",
647 r/256, g/256, b/256, a/256);
660 ts5_check_graphics(
"ts5_get_background_color");
661 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_background_color()\n");
663 return _ts5_status.graphics_status.background_color;
680 ts5_check_graphics(
"ts5_set_drawing_thickness");
681 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_drawing_thickness(%f)\n", drawing_thickness);
683 double retval = _ts5_status.graphics_status.drawing_thickness;
685 if (drawing_thickness < 0.0) {
686 drawing_thickness = -1.0;
689 _ts5_status.graphics_status.drawing_thickness = drawing_thickness;
691 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_drawing_thickness: set drawing_thickness to %f (was %f)\n",
692 _ts5_status.graphics_status.drawing_thickness, retval);
705 ts5_check_graphics(
"ts5_get_drawing_thickness");
706 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_drawing_thickness()\n");
708 return _ts5_status.graphics_status.drawing_thickness;
725 ts5_check_graphics(
"ts5_set_fill_mode");
726 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_fill_mode(%d)\n", fill_mode);
728 int retval = _ts5_status.graphics_status.fill_mode;
730 if (fill_mode != TS5_FILL_ON && fill_mode != TS5_FILL_OFF) {
731 ts5_fatal(
"ts5_set_fill_mode: fill_mode should be TS5_FILL_OFF (off) or TS5_FILL_ON (on)\n");
734 _ts5_status.graphics_status.fill_mode = fill_mode;
736 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_fill_mode: set mode to %d (was %d)\n",
737 _ts5_status.graphics_status.fill_mode, retval);
750 ts5_check_graphics(
"ts5_get_fill_mode");
751 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_fill_mode()\n");
753 return _ts5_status.graphics_status.fill_mode;
768 ts5_check_graphics(
"ts5_set_font_type");
769 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_type(%d)\n", font_type);
771 int retval = _ts5_status.graphics_status.font_type;
773 if (font_type != TS5_COURIER && font_type != TS5_ARIAL && font_type != TS5_TIMES) {
774 ts5_fatal(
"ts5_set_font_type: font_type should be TS5_COURIER, TS5_ARIAL or TS5_TIMES\n");
777 _ts5_status.graphics_status.font_type = font_type;
779 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_type: set font type to %d (was %d)\n",
780 _ts5_status.graphics_status.font_type, retval);
782 _ts5_status.graphics_status.font_index =
783 _ts5_status.graphics_status.font_type * TS5_FONTSTYLES * TS5_FONTSIZES +
784 _ts5_status.graphics_status.font_style * TS5_FONTSIZES +
785 _ts5_status.graphics_status.font_size - 8;
798 ts5_check_graphics(
"ts5_get_font_type");
799 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_font_type()\n");
801 return _ts5_status.graphics_status.font_type;
816 ts5_check_graphics(
"ts5_set_font_style");
817 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_style(%d)\n", font_style);
819 int retval = _ts5_status.graphics_status.font_style;
821 if (font_style != TS5_REGULAR && font_style != TS5_BOLD &&
822 font_style != TS5_ITALIC && font_style != TS5_BOLD_ITALIC) {
823 ts5_fatal(
"ts5_set_font_style: font_style should be TS5_REGULAR, TS5_BOLD,",
824 " TS5_ITALIC or TS5_BOLD_ITALIC\n");
827 _ts5_status.graphics_status.font_style = font_style;
829 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_style: set font style to %d (was %d)\n",
830 _ts5_status.graphics_status.font_style, retval);
832 _ts5_status.graphics_status.font_index =
833 _ts5_status.graphics_status.font_type * TS5_FONTSTYLES * TS5_FONTSIZES +
834 _ts5_status.graphics_status.font_style * TS5_FONTSIZES +
835 _ts5_status.graphics_status.font_size - 8;
848 ts5_check_graphics(
"ts5_get_font_style");
849 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_font_style()\n");
851 return _ts5_status.graphics_status.font_style;
866 ts5_check_graphics(
"ts5_set_font_size");
867 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_size(%d)\n", font_size);
869 int retval = _ts5_status.graphics_status.font_size;
871 if (font_size < 8 || font_size > 107) {
872 ts5_fatal(
"ts5_set_font_size: font_size should be between 8 and 107\n");
875 _ts5_status.graphics_status.font_size = font_size;
877 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_size: set font size to %d (was %d)\n",
878 _ts5_status.graphics_status.font_size, retval);
880 _ts5_status.graphics_status.font_index =
881 _ts5_status.graphics_status.font_type * TS5_FONTSTYLES * TS5_FONTSIZES +
882 _ts5_status.graphics_status.font_style * TS5_FONTSIZES +
883 _ts5_status.graphics_status.font_size - 8;
896 ts5_check_graphics(
"ts5_get_font_size");
897 ts5_check_textio(
"ts5_get_font_size");
898 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_font_size()\n");
900 return _ts5_status.graphics_status.font_size;
915 ts5_check_graphics(
"ts5_set_font_index");
916 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_index(%d)\n", font_index);
918 int retval = _ts5_status.graphics_status.font_index;
920 if (font_index < 0 || font_index >= TS5_NFONTS) {
921 ts5_fatal(
"ts5_set_font_index: font_index should be between 0 and TS5_NFONTS-1\n");
924 _ts5_status.graphics_status.font_index = font_index;
927 _ts5_status.graphics_status.font_size = (font_index % TS5_FONTSIZES) + 8;
928 _ts5_status.graphics_status.font_style = (font_index / TS5_FONTSIZES) % TS5_FONTSTYLES;
929 _ts5_status.graphics_status.font_type = font_index / (TS5_FONTSIZES * TS5_FONTSTYLES);
931 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_index: set font index to %d (was %d)\n",
932 _ts5_status.graphics_status.font_index, retval);
945 ts5_check_graphics(
"ts5_get_font_index");
946 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_font_index()\n");
948 return _ts5_status.graphics_status.font_index;
963 ts5_check_graphics(
"ts5_set_text_alignment");
964 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_text_alignment(%d)\n", text_alignment);
966 int retval = _ts5_status.graphics_status.text_alignment;
968 if (text_alignment != TS5_ALIGN_LEFT && text_alignment != TS5_ALIGN_CENTER &&
969 text_alignment != TS5_ALIGN_RIGHT) {
971 ts5_fatal(
"ts5_set_text_alignment: text_alignment should be TS5_ALIGN_LEFT, TS5_ALIGN_CENTER",
972 " or TS5_ALIGN_RIGHT\n");
975 _ts5_status.graphics_status.text_alignment = text_alignment;
977 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_text_alignment: set alignment to %d (was %d)\n",
978 _ts5_status.graphics_status.text_alignment, retval);
991 ts5_check_graphics(
"ts5_get_text_alignment");
992 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_text_alignment()\n");
994 return _ts5_status.graphics_status.text_alignment;