mirror of
https://github.com/GayPizzaSpecifications/padlab.git
synced 2025-08-03 05:10:56 +00:00
Completely separate drawing stuff into an internal api in case I wanna play with different renderers at some point
This commit is contained in:
parent
30cd5911a5
commit
f4863ff56a
23
analogue.c
23
analogue.c
@ -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)
|
||||
@ -231,16 +228,16 @@ int main(int argc, char** argv)
|
||||
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,7 +247,7 @@ int main(int argc, char** argv)
|
||||
plrSz, plrSz);
|
||||
}
|
||||
|
||||
SDL_RenderPresent(rend);
|
||||
DrawPresent();
|
||||
repaint = false;
|
||||
}
|
||||
}
|
||||
@ -258,7 +255,7 @@ int main(int argc, char** argv)
|
||||
res = 0;
|
||||
error:
|
||||
SDL_GameControllerClose(pad);
|
||||
SDL_DestroyRenderer(rend);
|
||||
QuitDraw();
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return res;
|
||||
|
29
draw.c
29
draw.c
@ -2,7 +2,29 @@
|
||||
#include "maths.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)
|
||||
{
|
||||
@ -60,3 +82,8 @@ void DrawCircleSteps(int x, int y, int r, int steps)
|
||||
lasty = ofsy;
|
||||
}
|
||||
}
|
||||
|
||||
void DrawPresent(void)
|
||||
{
|
||||
SDL_RenderPresent(rend);
|
||||
}
|
||||
|
51
draw.h
51
draw.h
@ -3,17 +3,64 @@
|
||||
|
||||
#define DISPLAY_SCALE 0.8889
|
||||
|
||||
#include "maths.h"
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user