diff --git a/arm9/source/common/hid.c b/arm9/source/common/hid.c index 0eecaa4..9eaed2a 100644 --- a/arm9/source/common/hid.c +++ b/arm9/source/common/hid.c @@ -30,21 +30,25 @@ u32 HID_ReadRawTouchState(void) static fixp_t ts_mult[2]; // ts_org indicates the coordinate system origin -static u32 ts_org[2]; +static int ts_org[2]; void HID_ReadTouchState(u16 *x, u16 *y) { u32 ts; + int xc, yc; fixp_t tx, ty; ts = HID_ReadRawTouchState(); tx = INT_TO_FIXP(HID_RAW_TX(ts) - HID_TOUCH_MIDPOINT); ty = INT_TO_FIXP(HID_RAW_TY(ts) - HID_TOUCH_MIDPOINT); - *x = FIXP_TO_INT(fixp_round(fixp_product(tx, ts_mult[0]))) + ts_org[0]; - *y = FIXP_TO_INT(fixp_round(fixp_product(ty, ts_mult[1]))) + ts_org[1]; + xc = FIXP_TO_INT(fixp_round(fixp_product(tx, ts_mult[0]))) + ts_org[0]; + yc = FIXP_TO_INT(fixp_round(fixp_product(ty, ts_mult[1]))) + ts_org[1]; + + *x = clamp(xc, 0, (ts_org[0] * 2) - 1); + *y = clamp(yc, 0, (ts_org[1] * 2) - 1); } -bool HID_SetCalibrationData(const HID_CalibrationData *calibs, int point_cnt, u32 screen_w, u32 screen_h) +bool HID_SetCalibrationData(const HID_CalibrationData *calibs, int point_cnt, int screen_w, int screen_h) { int mid_x, mid_y; fixp_t avg_x, avg_y; diff --git a/arm9/source/common/hid.h b/arm9/source/common/hid.h index 4b7b825..f3c8e62 100644 --- a/arm9/source/common/hid.h +++ b/arm9/source/common/hid.h @@ -24,7 +24,7 @@ typedef struct { u32 HID_ReadRawTouchState(void); void HID_ReadTouchState(u16 *x, u16 *y); -bool HID_SetCalibrationData(const HID_CalibrationData *calibs, int point_cnt, u32 screen_w, u32 screen_h); +bool HID_SetCalibrationData(const HID_CalibrationData *calibs, int point_cnt, int screen_w, int screen_h); u32 InputWait(u32 timeout_sec); bool CheckButton(u32 button); diff --git a/arm9/source/common/ui.c b/arm9/source/common/ui.c index 3e3284f..1c44052 100644 --- a/arm9/source/common/ui.c +++ b/arm9/source/common/ui.c @@ -1072,7 +1072,7 @@ static void DrawSimpleCircle(unsigned char *screen, int x, int y, int color, int bool ShowCalibrationDialog(void) { - int current_dot; + u32 current_dot; static const u32 dot_positions[][2] = { {16, 16}, {320 - 16, 240 - 16}, @@ -1089,7 +1089,7 @@ bool ShowCalibrationDialog(void) current_dot = 0; while(current_dot < countof(dot_positions)) { - for (int i = 0; i < current_dot; i++) + for (u32 i = 0; i < current_dot; i++) DrawSimpleCircle(BOT_SCREEN, dot_positions[i][0], dot_positions[i][1], COLOR_BRIGHTGREEN, COLOR_BLACK); DrawSimpleCircle(BOT_SCREEN, dot_positions[current_dot][0], dot_positions[current_dot][1], COLOR_RED, COLOR_BLACK); for (u32 i = current_dot+1; i < countof(dot_positions); i++) diff --git a/common/common.h b/common/common.h index 211e45d..39c400f 100755 --- a/common/common.h +++ b/common/common.h @@ -31,6 +31,9 @@ #define int_sign(x) \ (((x) > 0) - ((x) < 0)) +#define clamp(x, min, max) \ + ((x) < (max) ? ((x) > (min) ? (x) : (min)) : (max)) + #define getbe16(d) \ (((d)[0]<<8) | (d)[1]) #define getbe32(d) \