diff --git a/config.h b/config.h index 5492e9d2..560d2aa8 100644 --- a/config.h +++ b/config.h @@ -1,3 +1,5 @@ +#include + #ifndef S2D_CONFIG_H #define S2D_CONFIG_H @@ -24,10 +26,13 @@ extern Gfx *gdl_head; // an allocator function of the format void *alloc(size_t bytes) #define alloc alloc_display_list +extern void *alloc(size_t); // your init functions for the RDP/RSP #define my_rdp_init my_rdp_init #define my_rsp_init my_rsp_init +extern void my_rsp_init(void); +extern void my_rdp_init(void); // The frame timer that is used to time s2d_type_print #define s2d_timer gGlobalTimer diff --git a/fixed_point_math.c b/fixed_point_math.c index bff61c74..a9fe0b4a 100644 --- a/fixed_point_math.c +++ b/fixed_point_math.c @@ -13,8 +13,9 @@ // Convert the number to floating point as if it were an integer, in other words remove the binary point // Multiply by 2^−n -f32 qtof(int q) { - return ((float)q) * 0.00001525878f; +f32 qtof(int q, int decimal_n) { + f32 mul = (1.0f / (float) (1 << decimal_n)); + return ((float)q) * mul; } // Float to Q @@ -23,8 +24,8 @@ f32 qtof(int q) { // Multiply the floating point number by 2^n // Round to the nearest integer -int ftoq(f32 f) { - f *= (65536.0f); +int ftoq(f32 f, int decimal_n) { + f *= (float) (1 << decimal_n); return (int)f; } diff --git a/init.c b/init.c index ef3e5c11..27f8dcf4 100644 --- a/init.c +++ b/init.c @@ -1,8 +1,11 @@ #include #include +#include "config.h" #include "init.h" +#include "s2d_error.h" void s2d_init(void) { + s2d_error_y = TEX_HEIGHT; gSPLoadUcode(gdl_head++, s2d_text, s2d_data); } diff --git a/s2d_error.c b/s2d_error.c new file mode 100644 index 00000000..dfa25bbc --- /dev/null +++ b/s2d_error.c @@ -0,0 +1,23 @@ +#include "config.h" +#include "s2d_print.h" +#include "s2d_error.h" + +int s2d_error_y = TEX_HEIGHT; + +int s2d_check_align(int align) { + if (align < ALIGN_LEFT || align > ALIGN_RIGHT) { + s2d_print_alloc(TEX_WIDTH, s2d_error_y, ALIGN_LEFT, "ERROR: invalid alignment"); + s2d_error_y += TEX_HEIGHT; + return -1; + } + return 0; +} + +int s2d_check_str(const char *str) { + if (str == NULL || ((u32)str & 0x80000000) == 0) { + s2d_print_alloc(TEX_WIDTH, s2d_error_y, ALIGN_LEFT, "ERROR: bad string, or no string specified"); + s2d_error_y += TEX_HEIGHT; + return -1; + } + return 0; +} diff --git a/s2d_error.h b/s2d_error.h new file mode 100644 index 00000000..d4b5703a --- /dev/null +++ b/s2d_error.h @@ -0,0 +1,9 @@ +#ifndef S2D_ERROR_H +#define S2D_ERROR_H + +extern int s2d_check_align(int align); +extern int s2d_check_str(const char *str); + +extern int s2d_error_y; + +#endif \ No newline at end of file diff --git a/s2d_parse.c b/s2d_parse.c index af417984..7a874972 100644 --- a/s2d_parse.c +++ b/s2d_parse.c @@ -8,8 +8,11 @@ #include "s2d_draw.h" #include "s2d_print.h" #include "s2d_ustdlib.h" +#include "s2d_error.h" -void s2d_snprint(int x, int y, int align, const char *str, uObjMtx *buf, int len) { +static int s2d_width(const char *str, int line, int len); + +static void s2d_snprint(int x, int y, int align, const char *str, uObjMtx *buf, int len) { char *p = str; int tmp_len = 0; int orig_x = x; @@ -115,7 +118,7 @@ void s2d_snprint(int x, int y, int align, const char *str, uObjMtx *buf, int len default: if (current_char != '\0' && current_char != CH_SEPARATOR) { draw_s2d_glyph(current_char, x, y, (buf++)); - (x += (s2d_kerning_table[current_char] * myScale)); + (x += (s2d_kerning_table[(int) current_char] * myScale)); } } if (*p == '\0') break; @@ -127,16 +130,31 @@ void s2d_snprint(int x, int y, int align, const char *str, uObjMtx *buf, int len } void s2d_print(int x, int y, int align, const char *str, uObjMtx *buf) { + if (s2d_check_align(align) != 0) return; + if (s2d_check_str(str) != 0) return; + s2d_snprint(x, y, align, str, buf, s2d_strlen(str)); } void s2d_print_alloc(int x, int y, int align, const char *str) { - uObjMtx *b = alloc(sizeof(uObjMtx) * s2d_strlen(str)); - s2d_snprint(x, y, align, str, b, s2d_strlen(str)); + int len; + + if (s2d_check_align(align) != 0) return; + if (s2d_check_str(str) != 0) return; + + len = s2d_strlen(str); + + uObjMtx *b = alloc(sizeof(uObjMtx) * len); + s2d_snprint(x, y, align, str, b, len); } void s2d_type_print(int x, int y, int align, const char *str, uObjMtx *buf, int *pos) { - int len = s2d_strlen(str); + int len; + + if (s2d_check_align(align) != 0) return; + if (s2d_check_str(str) != 0) return; + + len = s2d_strlen(str); s2d_snprint(x, y, align, str, buf, *pos); if (s2d_timer % 2 == 0) { @@ -146,7 +164,7 @@ void s2d_type_print(int x, int y, int align, const char *str, uObjMtx *buf, int } } -int s2d_width(const char *str, int line, int len) { +static int s2d_width(const char *str, int line, int len) { char *p = str; int tmp_len = 0; int curLine = 0; @@ -200,7 +218,7 @@ int s2d_width(const char *str, int line, int len) { break; default: if (current_char != '\0' && curLine == line) - width += s2d_kerning_table[current_char] * scale; + width += s2d_kerning_table[(int) current_char] * scale; } if (*p == '\0') break; p++; @@ -218,7 +236,7 @@ int s2d_width(const char *str, int line, int len) { // if (last_chr >= 0) { // dst[last_chr] = '\0'; // } -// s2d_print(x, y, dst, buf); +// s2d_print(x, y, align, dst, buf); // } diff --git a/s2d_print.h b/s2d_print.h index d1b098e3..0929c3ae 100644 --- a/s2d_print.h +++ b/s2d_print.h @@ -29,7 +29,6 @@ #define ALIGN_CENTER 1 #define ALIGN_RIGHT 2 -extern void s2d_snprint(int x, int y, int align, const char *str, uObjMtx *buf, int len); extern void s2d_print(int x, int y, int align, const char *str, uObjMtx *buf); +extern void s2d_print_alloc(int x, int y, int align, const char *str); extern void s2d_type_print(int x, int y, int align, const char *str, uObjMtx *buf, int *pos); -extern void s2d_vsprint(int x, int y, int align, uObjMtx *buf, const char *str, ...);