small cleanups cus I felt like it

This commit is contained in:
2022-11-16 03:00:20 +11:00
parent f4863ff56a
commit fc9a6571ec
4 changed files with 19 additions and 27 deletions

View File

@ -25,6 +25,7 @@ bool UseGamepad(int aJoyid)
return false; return false;
} }
#define FATAL(CONDITION, RETURN) if (CONDITION) { res = (RETURN); goto error; }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int res; int res;
@ -38,19 +39,10 @@ int main(int argc, char** argv)
int winw = WINDOW_WIDTH; int winw = WINDOW_WIDTH;
int winh = WINDOW_HEIGHT; int winh = WINDOW_HEIGHT;
window = SDL_CreateWindow(CAPTION, winpos, winpos, winw, winh, winflg); window = SDL_CreateWindow(CAPTION, winpos, winpos, winw, winh, winflg);
if (window == NULL) FATAL(window == NULL, -1)
{
res = -1;
goto error;
}
if (InitDraw(window)) FATAL(InitDraw(window), -1)
{ size rendSize = GetDrawSizeInPixels();
res = -1;
goto error;
}
rect rendRect = GetDrawSizeInPixels();
if ((res = SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt")) != -1) if ((res = SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt")) != -1)
printf("read %d mappings from gamecontrollerdb.txt\n", res); printf("read %d mappings from gamecontrollerdb.txt\n", res);
@ -127,7 +119,7 @@ int main(int argc, char** argv)
{ {
winw = event.window.data1; winw = event.window.data1;
winh = event.window.data2; winh = event.window.data2;
rendRect = GetDrawSizeInPixels(); rendSize = GetDrawSizeInPixels();
repaint = true; repaint = true;
} }
else if (event.window.event == SDL_WINDOWEVENT_EXPOSED) else if (event.window.event == SDL_WINDOWEVENT_EXPOSED)
@ -228,16 +220,16 @@ int main(int argc, char** argv)
SetDrawColour(0x1F1F1FFF); SetDrawColour(0x1F1F1FFF);
DrawClear(); DrawClear();
const int hrw = rendRect.w / 2; const int hrw = rendSize.w / 2;
DrawDigital(&(rect){0, 0, hrw, rendRect.h}, &stickl); DrawDigital(&(rect){ 0, 0, hrw, rendSize.h}, &stickl);
DrawAnalogue(&(rect){hrw, 0, hrw, rendRect.h}, &stickr); DrawAnalogue(&(rect){ hrw, 0, hrw, rendSize.h}, &stickr);
// test player thingo // test player thingo
if (showavatar) if (showavatar)
{ {
plrpos = VecAdd(plrpos, VecScale(stickl.compos, framedelta * 0.5)); plrpos = VecAdd(plrpos, VecScale(stickl.compos, framedelta * 0.5));
plrpos.x = pfmod(plrpos.x, rendRect.w); plrpos.x = pfmod(plrpos.x, rendSize.w);
plrpos.y = pfmod(plrpos.y, rendRect.h); plrpos.y = pfmod(plrpos.y, rendSize.h);
SetDrawColour(0xFF0000FF); SetDrawColour(0xFF0000FF);
const int plrSz = 32; const int plrSz = 32;

4
draw.c
View File

@ -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); SDL_GetRendererOutputSize(rend, &out.w, &out.h);
return out; return out;
} }

4
draw.h
View File

@ -26,9 +26,9 @@ void QuitDraw(void);
// Get the actual size of the canvas in pixels. // Get the actual size of the canvas in pixels.
// //
// Returns: // 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. // height of the canvas in actual pixels.
rect GetDrawSizeInPixels(void); size GetDrawSizeInPixels(void);
// Set the current draw colour. // Set the current draw colour.
// //

10
maths.h
View File

@ -6,14 +6,14 @@
#define PI 3.141592653589793238462643383279502884L #define PI 3.141592653589793238462643383279502884L
#define TAU 6.283185307179586476925286766559005768L #define TAU 6.283185307179586476925286766559005768L
#define MAX(LHS, RHS) ((LHS > RHS) ? (LHS) : (RHS)) #define MAX(A, B) ((A) > (B) ? (A) : (B))
#define MIN(LHS, RHS) ((LHS < RHS) ? (LHS) : (RHS)) #define MIN(A, B) ((A) < (B) ? (A) : (B))
#define CLAMP(X, A, B) (MIN(B, MAX(A, X))) #define CLAMP(X, A, B) (MIN((B), MAX((A), (X))))
#define SATURATE(X) (CLAMP(X, 0, 1)) #define SATURATE(X) (CLAMP((X), 0, 1))
typedef double vec_t; typedef double vec_t;
typedef struct { vec_t x, y; } vector; typedef struct { vec_t x, y; } vector;
typedef struct { int w, h; } size;
typedef struct { int x, y, w, h; } rect; typedef struct { int x, y, w, h; } rect;
static inline vector VecAdd(vector l, vector r) static inline vector VecAdd(vector l, vector r)