20 #include "../include/tscope5/textio.h"
21 #include "../include/tscope5/display.h"
22 #include "../include/tscope5/graphics.h"
23 #include "../include/tscope5/textio_internal.h"
24 #include "../include/tscope5/system_internal.h"
25 #include "../include/tscope5/keyboard_internal.h"
26 #include "../include/tscope5/graphics_internal.h"
29 #include <allegro5/allegro_font.h>
30 #include <allegro5/allegro_ttf.h>
66 double ts5_printf(
double x,
double y,
const char *format, ...)
69 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf(%f,%f,...)\n", x, y);
71 if (_ts5_status.graphics.coordinate_scale
72 == TS5_RELATIVE_COORDINATES) {
77 if (_ts5_status.graphics.coordinate_system
78 == TS5_CARTESIAN_COORDINATES) {
81 + _ts5_status.graphics.font_size / 2.0);
89 buf = al_ustr_new(
"");
90 al_ustr_vappendf(buf, format, ap);
93 al_draw_ustr(_ts5_status.graphics.font,
94 _ts5_status.graphics.foreground_color,
95 x, y, _ts5_status.graphics.text_alignment, buf);
97 width = al_get_ustr_width(_ts5_status.graphics.font, buf);
100 if (_ts5_status.graphics.coordinate_scale
101 == TS5_RELATIVE_COORDINATES) {
102 width/=_ts5_status.graphics.target_width;
120 const char *format, ...)
123 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf_justify(%f,%f,%f,%f,...)\n",
126 if (_ts5_status.graphics.coordinate_scale
127 == TS5_RELATIVE_COORDINATES) {
133 if (_ts5_status.graphics.coordinate_system
134 == TS5_CARTESIAN_COORDINATES) {
138 + _ts5_status.graphics.font_size / 2.0);
144 va_start(ap, format);
145 buf = al_ustr_new(
"");
146 al_ustr_vappendf(buf, format, ap);
149 al_draw_justified_ustr(_ts5_status.graphics.font,
150 _ts5_status.graphics.foreground_color,
152 _ts5_status.graphics.text_alignment, buf);
168 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf_ustr(%f,%f,...)\n", x, y);
170 if (_ts5_status.graphics.coordinate_scale
171 == TS5_RELATIVE_COORDINATES) {
176 if (_ts5_status.graphics.coordinate_system
177 == TS5_CARTESIAN_COORDINATES) {
180 + _ts5_status.graphics.font_size / 2.0);
184 al_draw_ustr(_ts5_status.graphics.font,
185 _ts5_status.graphics.foreground_color,
186 x, y, _ts5_status.graphics.text_alignment, ustr);
188 width = al_get_ustr_width(_ts5_status.graphics.font, ustr);
190 if (_ts5_status.graphics.coordinate_scale
191 == TS5_RELATIVE_COORDINATES) {
192 width/=_ts5_status.graphics.target_width;
210 const TS5_USTR *ustr)
213 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf_justify_ustr(%f,%f,%f,%f,...)\n",
216 if (_ts5_status.graphics.coordinate_scale
217 == TS5_RELATIVE_COORDINATES) {
223 if (_ts5_status.graphics.coordinate_system
224 == TS5_CARTESIAN_COORDINATES) {
228 + _ts5_status.graphics.font_size / 2.0);
231 al_draw_justified_ustr(_ts5_status.graphics.font,
232 _ts5_status.graphics.foreground_color,
234 _ts5_status.graphics.text_alignment, ustr);
247 int ts5_scanf(
double x,
double y,
const char *format, ...)
251 ts5_log(TS5_LOGLEVEL_5,
"ts5_scanf(%f,%f,...)\n", x, y);
253 char str[TS5_MAX_CHAR];
254 int done = 0, pos = 0;
256 TS5_COLOR fg = _ts5_status.graphics.foreground_color;
257 TS5_COLOR bg = _ts5_status.graphics.background_color;
259 ALLEGRO_EVENT_QUEUE *event_queue;
260 event_queue = al_create_event_queue();
263 ts5_fatal(
"ts5_scanf: could not create event cue for keyboard\n");
267 al_register_event_source(event_queue, al_get_keyboard_event_source());
272 al_wait_for_event(event_queue, &event);
274 if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
279 ascii =
event.keyboard.unichar;
280 if (ascii == 3 || ascii == 13) {
283 else if (ascii == 8 || ascii == 127) {
287 else if (ascii == 0) {
295 if (pos > TS5_MAX_CHAR) {
305 al_unregister_event_source(event_queue, al_get_keyboard_event_source());
306 al_destroy_event_queue(event_queue);
310 va_start(args, format);
311 i = vsscanf(str, format, args);
328 ts5_log(TS5_LOGLEVEL_5,
"ts5_get_text_width(...)\n");
334 va_start(ap, format);
335 buf = al_ustr_new(
"");
336 al_ustr_vappendf(buf, format, ap);
339 width = al_get_ustr_width(_ts5_status.graphics.font, buf);
342 if (_ts5_status.graphics.coordinate_scale
343 == TS5_RELATIVE_COORDINATES) {
344 width/=_ts5_status.graphics.target_width;
361 ts5_log(TS5_LOGLEVEL_5,
"ts5_get_text_width_ustr(...)\n");
364 width = al_get_ustr_width(_ts5_status.graphics.font, ustr);
366 if (_ts5_status.graphics.coordinate_scale
367 == TS5_RELATIVE_COORDINATES) {
368 width/=_ts5_status.graphics.target_width;
402 ts5_log(TS5_LOGLEVEL_4,
"ts5_read_font(%s,%f)\n", fontfile, size);
404 ts5_log(TS5_LOGLEVEL_2,
"ts5_read_font: loading font %s %f\n",
408 font = al_load_ttf_font(fontfile, -size, 0);
411 ts5_fatal(
"ts5_read_font: could not load font %s %d\n", fontfile,
412 _ts5_status.graphics.font_size);
429 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font(%p)\n", font);
432 ts5_fatal(
"ts5_set_font: font is a NULL pointer\n");
435 TS5_FONT *retval = _ts5_status.graphics.font;
436 _ts5_status.graphics.font = font;
437 _ts5_status.graphics.font_index = TS5_USERFONT;
439 _ts5_status.graphics.font_size =
440 al_get_font_line_height(_ts5_status.graphics.font);
457 ts5_log(TS5_LOGLEVEL_2,
"ts5_free_font(%p)\n", font);
460 al_destroy_font(font);
461 ts5_log(TS5_LOGLEVEL_2,
"ts5_free_font: removed font\n");
void ts5_printf_justify(double x1, double x2, double y, const double diff, const char *format,...)
Write justified text to the active bitmap.
void ts5_printf_justify_ustr(double x1, double x2, double y, const double diff, const TS5_USTR *ustr)
Write justified UTF-8 encoded text to the active bitmap.
TS5_FONT * ts5_set_font(TS5_FONT *font)
Set a user defined font.
int ts5_scanf(double x, double y, const char *format,...)
Read text from the keyboard.
void ts5_check_textio(char *calling_function)
Do some checks at the start of each textio function.
double ts5_get_text_width_ustr(TS5_USTR *ustr)
Get the width of a UTF-8 string without printing it.
double ts5_flip_display()
Make what has been drawn visible on the screen.
double ts5_printf(double x, double y, const char *format,...)
Write text to the active bitmap.
TS5_COLOR ts5_set_foreground_color(const TS5_COLOR foreground_color)
Set the foreground color.
TS5_FONT * ts5_read_font(const char *fontfile, double size)
Load a user defined font.
double ts5_printf_ustr(double x, double y, const TS5_USTR *ustr)
Write UTF-8 encoded text to the active bitmap.
double ts5_relative_to_absolute_coordinate_x(const double x)
Convert a relative horizontal coordinate into an absolute horizontal coordinate.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
double ts5_cartesian_to_display_coordinate_x(const double x)
Convert a horizontal Cartesian coordinate into a horizontal display coordinate.
double ts5_cartesian_to_display_coordinate_y(const double y)
Convert a vertical Cartesian coordinate into a vertical display coordinate.
void ts5_check_keyboard(char *calling_function)
Do some checks at the start of each keyboard function.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
double ts5_relative_to_absolute_coordinate_y(const double y)
Convert a relative vertical coordinate into an absolute vertical coordinate.
double ts5_get_text_width(const char *format,...)
Get the width of a string without printing it.
void ts5_free_font(TS5_FONT *font)
Free the memory used by a user defined font.