2022-11-15 05:10:26 +11:00
|
|
|
#include "draw.h"
|
|
|
|
#include "maths.h"
|
|
|
|
#include <SDL_render.h>
|
|
|
|
|
2022-11-16 02:48:43 +11:00
|
|
|
static SDL_Renderer* rend = NULL;
|
|
|
|
|
2022-11-16 04:46:33 +11:00
|
|
|
void DrawWindowHints(void) {}
|
|
|
|
|
2022-11-16 02:48:43 +11:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-16 03:00:20 +11:00
|
|
|
size GetDrawSizeInPixels(void)
|
2022-11-16 02:48:43 +11:00
|
|
|
{
|
2022-11-16 03:00:20 +11:00
|
|
|
size out = {0, 0};
|
2022-11-16 02:48:43 +11:00
|
|
|
SDL_GetRendererOutputSize(rend, &out.w, &out.h);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2022-11-16 09:20:44 +11:00
|
|
|
void SetDrawViewport(size size) {}
|
|
|
|
|
2022-11-15 05:10:26 +11:00
|
|
|
|
|
|
|
void SetDrawColour(uint32_t c)
|
|
|
|
{
|
|
|
|
SDL_SetRenderDrawColor(rend,
|
|
|
|
(c & 0xFF000000) >> 24,
|
|
|
|
(c & 0x00FF0000) >> 16,
|
|
|
|
(c & 0x0000FF00) >> 8,
|
|
|
|
(c & 0x000000FF));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawClear(void)
|
|
|
|
{
|
|
|
|
SDL_RenderClear(rend);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawPoint(int x, int y)
|
|
|
|
{
|
|
|
|
SDL_RenderDrawPoint(rend, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawRect(int x, int y, int w, int h)
|
|
|
|
{
|
|
|
|
SDL_Rect dst = {
|
|
|
|
.x = x, .y = y,
|
|
|
|
.w = w, .h = h };
|
|
|
|
SDL_RenderDrawRect(rend, &dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawLine(int x1, int y1, int x2, int y2)
|
|
|
|
{
|
|
|
|
SDL_RenderDrawLine(rend, x1, y1, x2, y2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawCircle(int x, int y, int r)
|
|
|
|
{
|
|
|
|
DrawCircleSteps(x, y, r, (int)(sqrt((double)r) * 8.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawCircleSteps(int x, int y, int r, int steps)
|
|
|
|
{
|
|
|
|
double stepsz = (double)TAU / steps;
|
|
|
|
int lastx = r;
|
|
|
|
int lasty = 0;
|
2022-11-16 04:46:33 +11:00
|
|
|
for (int i = 1; i <= steps; ++i)
|
2022-11-15 05:10:26 +11:00
|
|
|
{
|
|
|
|
const double mag = (double)r;
|
|
|
|
int ofsx = (int)round(cos(stepsz * i) * mag);
|
|
|
|
int ofsy = (int)round(sin(stepsz * i) * mag);
|
|
|
|
|
|
|
|
SDL_RenderDrawLine(rend,
|
|
|
|
x + lastx, y + lasty,
|
|
|
|
x + ofsx, y + ofsy);
|
|
|
|
|
|
|
|
lastx = ofsx;
|
|
|
|
lasty = ofsy;
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 02:48:43 +11:00
|
|
|
|
2022-11-18 05:44:30 +11:00
|
|
|
void DrawArc(int x, int y, int r, int startAng, int endAng)
|
|
|
|
{
|
|
|
|
const int steps = (int)(sqrt((double)r) * (double)abs(endAng - startAng) / 360.0 * 8.0);
|
|
|
|
DrawArcSteps(x, y, r, startAng, endAng, steps);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawArcSteps(int x, int y, int r, int startAng, int endAng, int steps)
|
|
|
|
{
|
|
|
|
const double fstart = (double)startAng * DEG2RAD;
|
|
|
|
const double fstepSz = (double)(endAng - startAng) / abs(steps) * DEG2RAD;
|
|
|
|
const double mag = (double)r;
|
|
|
|
|
|
|
|
int lastx = (int)round(cos(fstart) * mag);
|
|
|
|
int lasty = (int)round(-sin(fstart) * mag);
|
|
|
|
for (int i = 1; i <= steps; ++i)
|
|
|
|
{
|
|
|
|
const double theta = fstart + fstepSz * (double)i;
|
|
|
|
int ofsx = (int)round(cos(theta) * mag);
|
|
|
|
int ofsy = (int)round(-sin(theta) * mag);
|
|
|
|
|
|
|
|
SDL_RenderDrawLine(rend,
|
|
|
|
x + lastx, y + lasty,
|
|
|
|
x + ofsx, y + ofsy);
|
|
|
|
|
|
|
|
lastx = ofsx;
|
|
|
|
lasty = ofsy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 02:48:43 +11:00
|
|
|
void DrawPresent(void)
|
|
|
|
{
|
|
|
|
SDL_RenderPresent(rend);
|
|
|
|
}
|