From fc9a6571ec2fb557b7d72b5017169668aaf78bbd Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Wed, 16 Nov 2022 03:00:20 +1100 Subject: [PATCH] small cleanups cus I felt like it --- analogue.c | 28 ++++++++++------------------ draw.c | 4 ++-- draw.h | 4 ++-- maths.h | 10 +++++----- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/analogue.c b/analogue.c index 45a29f2..dc3ccec 100644 --- a/analogue.c +++ b/analogue.c @@ -25,6 +25,7 @@ bool UseGamepad(int aJoyid) return false; } +#define FATAL(CONDITION, RETURN) if (CONDITION) { res = (RETURN); goto error; } int main(int argc, char** argv) { int res; @@ -38,19 +39,10 @@ int main(int argc, char** argv) int winw = WINDOW_WIDTH; int winh = WINDOW_HEIGHT; window = SDL_CreateWindow(CAPTION, winpos, winpos, winw, winh, winflg); - if (window == NULL) - { - res = -1; - goto error; - } + FATAL(window == NULL, -1) - if (InitDraw(window)) - { - res = -1; - goto error; - } - - rect rendRect = GetDrawSizeInPixels(); + FATAL(InitDraw(window), -1) + size rendSize = GetDrawSizeInPixels(); if ((res = SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt")) != -1) printf("read %d mappings from gamecontrollerdb.txt\n", res); @@ -127,7 +119,7 @@ int main(int argc, char** argv) { winw = event.window.data1; winh = event.window.data2; - rendRect = GetDrawSizeInPixels(); + rendSize = GetDrawSizeInPixels(); repaint = true; } else if (event.window.event == SDL_WINDOWEVENT_EXPOSED) @@ -228,16 +220,16 @@ int main(int argc, char** argv) SetDrawColour(0x1F1F1FFF); DrawClear(); - const int hrw = rendRect.w / 2; - DrawDigital(&(rect){0, 0, hrw, rendRect.h}, &stickl); - DrawAnalogue(&(rect){hrw, 0, hrw, rendRect.h}, &stickr); + const int hrw = rendSize.w / 2; + DrawDigital(&(rect){ 0, 0, hrw, rendSize.h}, &stickl); + DrawAnalogue(&(rect){ hrw, 0, hrw, rendSize.h}, &stickr); // test player thingo if (showavatar) { plrpos = VecAdd(plrpos, VecScale(stickl.compos, framedelta * 0.5)); - plrpos.x = pfmod(plrpos.x, rendRect.w); - plrpos.y = pfmod(plrpos.y, rendRect.h); + plrpos.x = pfmod(plrpos.x, rendSize.w); + plrpos.y = pfmod(plrpos.y, rendSize.h); SetDrawColour(0xFF0000FF); const int plrSz = 32; diff --git a/draw.c b/draw.c index 45fd736..2ade969 100644 --- a/draw.c +++ b/draw.c @@ -18,9 +18,9 @@ void QuitDraw(void) } -rect GetDrawSizeInPixels(void) +size GetDrawSizeInPixels(void) { - rect out = {0, 0, 0, 0}; + size out = {0, 0}; SDL_GetRendererOutputSize(rend, &out.w, &out.h); return out; } diff --git a/draw.h b/draw.h index 56160c4..c660f89 100644 --- a/draw.h +++ b/draw.h @@ -26,9 +26,9 @@ void QuitDraw(void); // Get the actual size of the canvas in pixels. // // Returns: -// rectangle struct with 'w' and 'h' set to the width & +// size struct with 'w' and 'h' set to the width & // height of the canvas in actual pixels. -rect GetDrawSizeInPixels(void); +size GetDrawSizeInPixels(void); // Set the current draw colour. // diff --git a/maths.h b/maths.h index a1d7233..5bf4860 100644 --- a/maths.h +++ b/maths.h @@ -6,14 +6,14 @@ #define PI 3.141592653589793238462643383279502884L #define TAU 6.283185307179586476925286766559005768L -#define MAX(LHS, RHS) ((LHS > RHS) ? (LHS) : (RHS)) -#define MIN(LHS, RHS) ((LHS < RHS) ? (LHS) : (RHS)) -#define CLAMP(X, A, B) (MIN(B, MAX(A, X))) -#define SATURATE(X) (CLAMP(X, 0, 1)) +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define CLAMP(X, A, B) (MIN((B), MAX((A), (X)))) +#define SATURATE(X) (CLAMP((X), 0, 1)) typedef double vec_t; typedef struct { vec_t x, y; } vector; - +typedef struct { int w, h; } size; typedef struct { int x, y, w, h; } rect; static inline vector VecAdd(vector l, vector r)