diff --git a/analogue.c b/analogue.c index c06f5d7..45a29f2 100644 --- a/analogue.c +++ b/analogue.c @@ -32,7 +32,7 @@ int main(int argc, char** argv) res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER); if (res < 0) goto error; - + const int winpos = SDL_WINDOWPOS_CENTERED; const int winflg = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; int winw = WINDOW_WIDTH; @@ -44,16 +44,13 @@ int main(int argc, char** argv) goto error; } - const int rendflags = SDL_RENDERER_PRESENTVSYNC; - rend = SDL_CreateRenderer(window, -1, rendflags); - if (rend == NULL) + if (InitDraw(window)) { res = -1; goto error; } - int rw, rh; - SDL_GetRendererOutputSize(rend, &rw, &rh); + rect rendRect = GetDrawSizeInPixels(); if ((res = SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt")) != -1) printf("read %d mappings from gamecontrollerdb.txt\n", res); @@ -130,7 +127,7 @@ int main(int argc, char** argv) { winw = event.window.data1; winh = event.window.data2; - SDL_GetRendererOutputSize(rend, &rw, &rh); + rendRect = GetDrawSizeInPixels(); repaint = true; } else if (event.window.event == SDL_WINDOWEVENT_EXPOSED) @@ -224,23 +221,23 @@ int main(int argc, char** argv) } while (SDL_PollEvent(&event) > 0); } - + if (repaint) { // background SetDrawColour(0x1F1F1FFF); DrawClear(); - const int hrw = rw / 2; - DrawDigital(&(rect){0, 0, hrw, rh}, &stickl); - DrawAnalogue(&(rect){hrw, 0, hrw, rh}, &stickr); + const int hrw = rendRect.w / 2; + DrawDigital(&(rect){0, 0, hrw, rendRect.h}, &stickl); + DrawAnalogue(&(rect){hrw, 0, hrw, rendRect.h}, &stickr); // test player thingo if (showavatar) { plrpos = VecAdd(plrpos, VecScale(stickl.compos, framedelta * 0.5)); - plrpos.x = pfmod(plrpos.x, rw); - plrpos.y = pfmod(plrpos.y, rh); + plrpos.x = pfmod(plrpos.x, rendRect.w); + plrpos.y = pfmod(plrpos.y, rendRect.h); SetDrawColour(0xFF0000FF); const int plrSz = 32; @@ -250,15 +247,15 @@ int main(int argc, char** argv) plrSz, plrSz); } - SDL_RenderPresent(rend); + DrawPresent(); repaint = false; } } - + res = 0; error: SDL_GameControllerClose(pad); - SDL_DestroyRenderer(rend); + QuitDraw(); SDL_DestroyWindow(window); SDL_Quit(); return res; diff --git a/draw.c b/draw.c index 21cdf4d..45fd736 100644 --- a/draw.c +++ b/draw.c @@ -2,7 +2,29 @@ #include "maths.h" #include -SDL_Renderer* rend = NULL; +static SDL_Renderer* rend = NULL; + +int InitDraw(SDL_Window* window) +{ + const int rendflags = SDL_RENDERER_PRESENTVSYNC; + rend = SDL_CreateRenderer(window, -1, rendflags); + return (rend == NULL) ? -1 : 0; +} + +void QuitDraw(void) +{ + SDL_DestroyRenderer(rend); + rend = NULL; +} + + +rect GetDrawSizeInPixels(void) +{ + rect out = {0, 0, 0, 0}; + SDL_GetRendererOutputSize(rend, &out.w, &out.h); + return out; +} + void SetDrawColour(uint32_t c) { @@ -60,3 +82,8 @@ void DrawCircleSteps(int x, int y, int r, int steps) lasty = ofsy; } } + +void DrawPresent(void) +{ + SDL_RenderPresent(rend); +} diff --git a/draw.h b/draw.h index 51fab08..56160c4 100644 --- a/draw.h +++ b/draw.h @@ -3,17 +3,64 @@ #define DISPLAY_SCALE 0.8889 +#include "maths.h" #include -typedef struct SDL_Renderer SDL_Renderer; -extern SDL_Renderer* rend; +typedef struct SDL_Window SDL_Window; +// Initialise the drawing subsystem. +// +// Params: +// window - The application's SDL window. +// +// Returns: +// 0 on success, -1 on failure. +int InitDraw(SDL_Window* window); + +// Quit the drawing subsystem. +// +// This is safe (but pointless) to call when the drawing +// subsystem is uninitialised. +void QuitDraw(void); + +// Get the actual size of the canvas in pixels. +// +// Returns: +// rectangle struct with 'w' and 'h' set to the width & +// height of the canvas in actual pixels. +rect GetDrawSizeInPixels(void); + +// Set the current draw colour. +// +// Params: +// c - Colour in the unsigned 32-bit int packed format of: +// 0xFF000000 - red channel +// 0x00FF0000 - blue channel +// 0x0000FF00 - green channel +// 0x000000FF - alpha channel void SetDrawColour(uint32_t c); + +// Clear the entire screen using the current draw colour. void DrawClear(void); + +// Draw a single pixel point at x, y w/ the draw colour. void DrawPoint(int x, int y); + +// Draw rectangle outline. void DrawRect(int x, int y, int w, int h); + +// Draw straight line between x1,y1 and x2,y2. void DrawLine(int x1, int y1, int x2, int y2); + +// Draw outline circle. void DrawCircle(int x, int y, int r); + +// Draw outline circle made of lines w/ a discrete number of steps. +// +// Can be used to draw regular convex polygons such as an octagon. void DrawCircleSteps(int x, int y, int r, int steps); +// Present the current buffer to the screen. +void DrawPresent(void); + #endif//DRAW_H