added error handling; fixed a few warnings

This commit is contained in:
farisawan-2000
2021-03-11 16:50:55 -05:00
parent 6c19190175
commit 4eed305ebc
7 changed files with 72 additions and 14 deletions

View File

@@ -1,3 +1,5 @@
#include <ultra64.h>
#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

View File

@@ -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;
}

3
init.c
View File

@@ -1,8 +1,11 @@
#include <ultra64.h>
#include <PR/gs2dex.h>
#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);
}

23
s2d_error.c Normal file
View File

@@ -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;
}

9
s2d_error.h Normal file
View File

@@ -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

View File

@@ -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);
// }

View File

@@ -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, ...);