Completely separate drawing stuff into an internal api in case I wanna play with different renderers at some point

This commit is contained in:
a dinosaur 2022-11-16 02:48:43 +11:00
parent 30cd5911a5
commit f4863ff56a
3 changed files with 90 additions and 19 deletions

View File

@ -32,7 +32,7 @@ int main(int argc, char** argv)
res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER); res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
if (res < 0) if (res < 0)
goto error; goto error;
const int winpos = SDL_WINDOWPOS_CENTERED; const int winpos = SDL_WINDOWPOS_CENTERED;
const int winflg = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; const int winflg = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
int winw = WINDOW_WIDTH; int winw = WINDOW_WIDTH;
@ -44,16 +44,13 @@ int main(int argc, char** argv)
goto error; goto error;
} }
const int rendflags = SDL_RENDERER_PRESENTVSYNC; if (InitDraw(window))
rend = SDL_CreateRenderer(window, -1, rendflags);
if (rend == NULL)
{ {
res = -1; res = -1;
goto error; goto error;
} }
int rw, rh; rect rendRect = GetDrawSizeInPixels();
SDL_GetRendererOutputSize(rend, &rw, &rh);
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);
@ -130,7 +127,7 @@ int main(int argc, char** argv)
{ {
winw = event.window.data1; winw = event.window.data1;
winh = event.window.data2; winh = event.window.data2;
SDL_GetRendererOutputSize(rend, &rw, &rh); rendRect = GetDrawSizeInPixels();
repaint = true; repaint = true;
} }
else if (event.window.event == SDL_WINDOWEVENT_EXPOSED) else if (event.window.event == SDL_WINDOWEVENT_EXPOSED)
@ -224,23 +221,23 @@ int main(int argc, char** argv)
} }
while (SDL_PollEvent(&event) > 0); while (SDL_PollEvent(&event) > 0);
} }
if (repaint) if (repaint)
{ {
// background // background
SetDrawColour(0x1F1F1FFF); SetDrawColour(0x1F1F1FFF);
DrawClear(); DrawClear();
const int hrw = rw / 2; const int hrw = rendRect.w / 2;
DrawDigital(&(rect){0, 0, hrw, rh}, &stickl); DrawDigital(&(rect){0, 0, hrw, rendRect.h}, &stickl);
DrawAnalogue(&(rect){hrw, 0, hrw, rh}, &stickr); DrawAnalogue(&(rect){hrw, 0, hrw, rendRect.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, rw); plrpos.x = pfmod(plrpos.x, rendRect.w);
plrpos.y = pfmod(plrpos.y, rh); plrpos.y = pfmod(plrpos.y, rendRect.h);
SetDrawColour(0xFF0000FF); SetDrawColour(0xFF0000FF);
const int plrSz = 32; const int plrSz = 32;
@ -250,15 +247,15 @@ int main(int argc, char** argv)
plrSz, plrSz); plrSz, plrSz);
} }
SDL_RenderPresent(rend); DrawPresent();
repaint = false; repaint = false;
} }
} }
res = 0; res = 0;
error: error:
SDL_GameControllerClose(pad); SDL_GameControllerClose(pad);
SDL_DestroyRenderer(rend); QuitDraw();
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
return res; return res;

29
draw.c
View File

@ -2,7 +2,29 @@
#include "maths.h" #include "maths.h"
#include <SDL_render.h> #include <SDL_render.h>
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) void SetDrawColour(uint32_t c)
{ {
@ -60,3 +82,8 @@ void DrawCircleSteps(int x, int y, int r, int steps)
lasty = ofsy; lasty = ofsy;
} }
} }
void DrawPresent(void)
{
SDL_RenderPresent(rend);
}

51
draw.h
View File

@ -3,17 +3,64 @@
#define DISPLAY_SCALE 0.8889 #define DISPLAY_SCALE 0.8889
#include "maths.h"
#include <stdint.h> #include <stdint.h>
typedef struct SDL_Renderer SDL_Renderer; typedef struct SDL_Window SDL_Window;
extern SDL_Renderer* rend;
// 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); void SetDrawColour(uint32_t c);
// Clear the entire screen using the current draw colour.
void DrawClear(void); void DrawClear(void);
// Draw a single pixel point at x, y w/ the draw colour.
void DrawPoint(int x, int y); void DrawPoint(int x, int y);
// Draw rectangle outline.
void DrawRect(int x, int y, int w, int h); 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); void DrawLine(int x1, int y1, int x2, int y2);
// Draw outline circle.
void DrawCircle(int x, int y, int r); 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); void DrawCircleSteps(int x, int y, int r, int steps);
// Present the current buffer to the screen.
void DrawPresent(void);
#endif//DRAW_H #endif//DRAW_H