mirror of
https://github.com/GayPizzaSpecifications/padlab.git
synced 2025-08-03 21:21:33 +00:00
Modularise drawing code a little
This commit is contained in:
@ -4,6 +4,7 @@ set(CMAKE_C_STANDARD 99)
|
|||||||
set(TARGET padlab)
|
set(TARGET padlab)
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
maths.c maths.h
|
maths.c maths.h
|
||||||
|
draw.c draw.h
|
||||||
analogue.c)
|
analogue.c)
|
||||||
|
|
||||||
find_package(SDL2 REQUIRED)
|
find_package(SDL2 REQUIRED)
|
||||||
|
188
analogue.c
188
analogue.c
@ -1,4 +1,5 @@
|
|||||||
#include "maths.h"
|
#include "maths.h"
|
||||||
|
#include "draw.h"
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -10,7 +11,6 @@
|
|||||||
#define DISPLAY_SCALE 0.8889
|
#define DISPLAY_SCALE 0.8889
|
||||||
|
|
||||||
SDL_Window* window = NULL;
|
SDL_Window* window = NULL;
|
||||||
SDL_Renderer* rend = NULL;
|
|
||||||
SDL_JoystickID joyid = -1;
|
SDL_JoystickID joyid = -1;
|
||||||
SDL_GameController* pad = NULL;
|
SDL_GameController* pad = NULL;
|
||||||
|
|
||||||
@ -26,26 +26,6 @@ bool UseGamepad(int a_joyid)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawCircle(int x, int y, int r, int steps)
|
|
||||||
{
|
|
||||||
double stepsz = (TAU) / steps;
|
|
||||||
int lastx = r;
|
|
||||||
int lasty = 0;
|
|
||||||
for (int i = 0; i <= steps; ++i)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vector RadialDeadzone(vector v, double min, double max)
|
vector RadialDeadzone(vector v, double min, double max)
|
||||||
{
|
{
|
||||||
double mag = sqrt(v.x * v.x + v.y * v.y);
|
double mag = sqrt(v.x * v.x + v.y * v.y);
|
||||||
@ -145,28 +125,28 @@ void DrawAnalogue(const SDL_Rect* win, StickState* p)
|
|||||||
p->recalc = false;
|
p->recalc = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Rect rect;
|
|
||||||
double size = (double)(win->w > win->h ? win->h : win->w) * DISPLAY_SCALE;
|
double size = (double)(win->w > win->h ? win->h : win->w) * DISPLAY_SCALE;
|
||||||
|
|
||||||
// range rect
|
// range rect
|
||||||
SDL_SetRenderDrawColor(rend, 0x3F, 0x3F, 0x3F, 0xFF);
|
SetDrawColour(0x3F3F3FFF);
|
||||||
rect.w = rect.h = (int)round(size);
|
const int rectSz = (int)round(size);
|
||||||
rect.x = win->x + (win->w - rect.w) / 2;
|
DrawRect(
|
||||||
rect.y = win->y + (win->h - rect.h) / 2;
|
win->x + (win->w - rectSz) / 2,
|
||||||
SDL_RenderDrawRect(rend, &rect);
|
win->y + (win->h - rectSz) / 2,
|
||||||
|
rectSz, rectSz);
|
||||||
|
|
||||||
const int ox = win->x + win->w / 2;
|
const int ox = win->x + win->w / 2;
|
||||||
const int oy = win->y + win->h / 2;
|
const int oy = win->y + win->h / 2;
|
||||||
|
|
||||||
// acceleration curve
|
// acceleration curve
|
||||||
SDL_SetRenderDrawColor(rend, 0x4F, 0x4F, 0x4F, 0xFF);
|
SetDrawColour(0x4F4F4FFF);
|
||||||
const int accelsamp = (int)(sqrt(size) * 4.20);
|
const int accelsamp = (int)(sqrt(size) * 4.20);
|
||||||
const double step = 1.0 / (double)accelsamp;
|
const double step = 1.0 / (double)accelsamp;
|
||||||
double y1 = AccelCurve(0.0, p->accelpow);
|
double y1 = AccelCurve(0.0, p->accelpow);
|
||||||
for (int i = 1; i <= accelsamp; ++i)
|
for (int i = 1; i <= accelsamp; ++i)
|
||||||
{
|
{
|
||||||
double y2 = AccelCurve(step * i, p->accelpow);
|
double y2 = AccelCurve(step * i, p->accelpow);
|
||||||
SDL_RenderDrawLine(rend,
|
DrawLine(
|
||||||
win->x + (int)(step * (i - 1) * size) + (win->w - (int)round(size)) / 2,
|
win->x + (int)(step * (i - 1) * size) + (win->w - (int)round(size)) / 2,
|
||||||
win->y + (int)((1.0 - y1) * size) + (win->h - (int)round(size)) / 2,
|
win->y + (int)((1.0 - y1) * size) + (win->h - (int)round(size)) / 2,
|
||||||
win->x + (int)(step * i * size) + (win->w - (int)round(size)) / 2,
|
win->x + (int)(step * i * size) + (win->w - (int)round(size)) / 2,
|
||||||
@ -175,61 +155,53 @@ void DrawAnalogue(const SDL_Rect* win, StickState* p)
|
|||||||
}
|
}
|
||||||
const int tickerx = (int)((p->preaccel - 0.5) * size);
|
const int tickerx = (int)((p->preaccel - 0.5) * size);
|
||||||
const int tickery = (int)((0.5 - p->postacel) * size);
|
const int tickery = (int)((0.5 - p->postacel) * size);
|
||||||
SDL_SetRenderDrawColor(rend, 0x2F, 0x3F, 0x1F, 0xFF);
|
SetDrawColour(0x2F3F1FFF);
|
||||||
SDL_RenderDrawLine(rend,
|
DrawLine(
|
||||||
ox + tickerx,
|
ox + tickerx,
|
||||||
win->y + (win->h - (int)round(size)) / 2,
|
win->y + (win->h - (int)round(size)) / 2,
|
||||||
ox + tickerx,
|
ox + tickerx,
|
||||||
win->y + (win->h + (int)round(size)) / 2);
|
win->y + (win->h + (int)round(size)) / 2);
|
||||||
SDL_SetRenderDrawColor(rend, 0x2F, 0x5F, 0x2F, 0xFF);
|
SetDrawColour(0x2F5F2FFF);
|
||||||
SDL_RenderDrawLine(rend,
|
DrawLine(
|
||||||
win->x + (win->w - (int)round(size)) / 2,
|
win->x + (win->w - (int)round(size)) / 2,
|
||||||
oy + tickery,
|
oy + tickery,
|
||||||
win->x + (win->w + (int)round(size)) / 2,
|
win->x + (win->w + (int)round(size)) / 2,
|
||||||
oy + tickery);
|
oy + tickery);
|
||||||
|
|
||||||
// guide circle
|
// guide circle
|
||||||
SDL_SetRenderDrawColor(rend, 0x4F, 0x4F, 0x4F, 0xFF);
|
SetDrawColour(0x4F4F4FFF);
|
||||||
DrawCircle(
|
DrawCircle(ox, oy, (int)round(size) / 2);
|
||||||
ox, oy,
|
|
||||||
(int)round(size) / 2, (int)(sqrt(size) * 8.0));
|
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(rend, 0x3F, 0x3F, 0x3F, 0xFF);
|
SetDrawColour(0x3F3F3FFF);
|
||||||
DrawCircle(
|
DrawCircle(ox, oy, (int)round(p->deadzone * size) / 2);
|
||||||
ox, oy,
|
|
||||||
(int)round(p->deadzone * size) / 2, (int)(sqrt(size) * 2.0));
|
|
||||||
|
|
||||||
// 0,0 line axis'
|
// 0,0 line axis'
|
||||||
SDL_SetRenderDrawColor(rend, 0x2F, 0x2F, 0x2F, 0xFF);
|
SetDrawColour(0x2F2F2FFF);
|
||||||
SDL_RenderDrawLine(rend,
|
DrawLine(
|
||||||
win->x,
|
win->x, oy,
|
||||||
oy,
|
win->x + win->w, oy);
|
||||||
win->x + win->w,
|
DrawLine(
|
||||||
oy);
|
ox, win->y,
|
||||||
SDL_RenderDrawLine(rend,
|
ox, win->y + win->h);
|
||||||
ox,
|
|
||||||
win->y,
|
|
||||||
ox,
|
|
||||||
win->y + win->h);
|
|
||||||
|
|
||||||
// compensated position
|
// compensated position
|
||||||
SDL_SetRenderDrawColor(rend, 0x1F, 0xFF, 0x1F, 0xFF);
|
SetDrawColour(0x1FFF1FFF);
|
||||||
DrawCircle(
|
DrawCircleSteps(
|
||||||
ox + (int)round(p->compos.x * size / 2.0),
|
ox + (int)round(p->compos.x * size / 2.0),
|
||||||
oy + (int)round(p->compos.y * size / 2.0),
|
oy + (int)round(p->compos.y * size / 2.0),
|
||||||
8, 16);
|
8, 16);
|
||||||
SDL_RenderDrawPoint(rend,
|
DrawPoint(
|
||||||
ox + (int)round(p->compos.x * size / 2.0),
|
ox + (int)round(p->compos.x * size / 2.0),
|
||||||
oy + (int)round(p->compos.y * size / 2.0));
|
oy + (int)round(p->compos.y * size / 2.0));
|
||||||
|
|
||||||
// raw position
|
// raw position
|
||||||
SDL_SetRenderDrawColor(rend, 0xFF, 0xFF, 0xFF, 0xFF);
|
SetDrawColour(0xFFFFFFFF);
|
||||||
SDL_RenderDrawLine(rend,
|
DrawLine(
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0) - 4,
|
ox + (int)round(p->rawpos.x * size / 2.0) - 4,
|
||||||
oy + (int)round(p->rawpos.y * size / 2.0),
|
oy + (int)round(p->rawpos.y * size / 2.0),
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0) + 4,
|
ox + (int)round(p->rawpos.x * size / 2.0) + 4,
|
||||||
oy + (int)round(p->rawpos.y * size / 2.0));
|
oy + (int)round(p->rawpos.y * size / 2.0));
|
||||||
SDL_RenderDrawLine(rend,
|
DrawLine(
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0),
|
ox + (int)round(p->rawpos.x * size / 2.0),
|
||||||
oy + (int)round(p->rawpos.y * size / 2.0) - 4,
|
oy + (int)round(p->rawpos.y * size / 2.0) - 4,
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0),
|
ox + (int)round(p->rawpos.x * size / 2.0),
|
||||||
@ -244,37 +216,32 @@ void DrawDigital(const SDL_Rect* win, StickState* p)
|
|||||||
p->recalc = false;
|
p->recalc = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Rect rect;
|
|
||||||
const double size = (double)(win->w > win->h ? win->h : win->w) * DISPLAY_SCALE;
|
const double size = (double)(win->w > win->h ? win->h : win->w) * DISPLAY_SCALE;
|
||||||
|
|
||||||
// range rect
|
// range rect
|
||||||
SDL_SetRenderDrawColor(rend, 0x3F, 0x3F, 0x3F, 0xFF);
|
SetDrawColour(0x3F3F3FFF);
|
||||||
rect.w = rect.h = (int)round(size);
|
const int rectSz = (int)round(size);
|
||||||
rect.x = win->x + (win->w - rect.w) / 2;
|
DrawRect(
|
||||||
rect.y = win->y + (win->h - rect.h) / 2;
|
win->x + (win->w - rectSz) / 2,
|
||||||
SDL_RenderDrawRect(rend, &rect);
|
win->y + (win->h - rectSz) / 2,
|
||||||
|
rectSz, rectSz);
|
||||||
|
|
||||||
// window centre
|
// window centre
|
||||||
const int ox = win->x + win->w / 2;
|
const int ox = win->x + win->w / 2;
|
||||||
const int oy = win->y + win->h / 2;
|
const int oy = win->y + win->h / 2;
|
||||||
|
|
||||||
// guide circle
|
// guide circle
|
||||||
SDL_SetRenderDrawColor(rend, 0x4F, 0x4F, 0x4F, 0xFF);
|
SetDrawColour(0x4F4F4FFF);
|
||||||
DrawCircle(ox, oy,
|
DrawCircle(ox, oy, (int)round(size) / 2);
|
||||||
(int)round(size) / 2, (int)(sqrt(size) * 8.0));
|
|
||||||
|
|
||||||
// 0,0 line axis'
|
// 0,0 line axis'
|
||||||
SDL_SetRenderDrawColor(rend, 0x2F, 0x2F, 0x2F, 0xFF);
|
SetDrawColour(0x2F2F2FFF);
|
||||||
SDL_RenderDrawLine(rend,
|
DrawLine(
|
||||||
win->x,
|
win->x, oy,
|
||||||
oy,
|
win->x + win->w, oy);
|
||||||
win->x + win->w,
|
DrawLine(
|
||||||
oy);
|
ox, win->y,
|
||||||
SDL_RenderDrawLine(rend,
|
ox, win->y + win->h);
|
||||||
ox,
|
|
||||||
win->y,
|
|
||||||
ox,
|
|
||||||
win->y + win->h);
|
|
||||||
|
|
||||||
// calcuate points for the zone previews
|
// calcuate points for the zone previews
|
||||||
const double outerinvmag = 1.0 / sqrt(1.0 + p->digiangle * p->digiangle);
|
const double outerinvmag = 1.0 / sqrt(1.0 + p->digiangle * p->digiangle);
|
||||||
@ -283,47 +250,47 @@ void DrawDigital(const SDL_Rect* win, StickState* p)
|
|||||||
const int innh = (int)round(p->digideadzone * size / 2.0);
|
const int innh = (int)round(p->digideadzone * size / 2.0);
|
||||||
const int innq = (int)round(p->digideadzone * size / 2.0 * p->digiangle);
|
const int innq = (int)round(p->digideadzone * size / 2.0 * p->digiangle);
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(rend, 0x3F, 0x3F, 0x3F, 0xFF);
|
SetDrawColour(0x3F3F3FFF);
|
||||||
|
|
||||||
// angles preview
|
// angles preview
|
||||||
SDL_RenderDrawLine(rend, ox - outq, oy - outh, ox - innq, oy - innh);
|
DrawLine(ox - outq, oy - outh, ox - innq, oy - innh);
|
||||||
SDL_RenderDrawLine(rend, ox + outq, oy - outh, ox + innq, oy - innh);
|
DrawLine(ox + outq, oy - outh, ox + innq, oy - innh);
|
||||||
SDL_RenderDrawLine(rend, ox + outh, oy - outq, ox + innh, oy - innq);
|
DrawLine(ox + outh, oy - outq, ox + innh, oy - innq);
|
||||||
SDL_RenderDrawLine(rend, ox + outh, oy + outq, ox + innh, oy + innq);
|
DrawLine(ox + outh, oy + outq, ox + innh, oy + innq);
|
||||||
SDL_RenderDrawLine(rend, ox + outq, oy + outh, ox + innq, oy + innh);
|
DrawLine(ox + outq, oy + outh, ox + innq, oy + innh);
|
||||||
SDL_RenderDrawLine(rend, ox - outq, oy + outh, ox - innq, oy + innh);
|
DrawLine(ox - outq, oy + outh, ox - innq, oy + innh);
|
||||||
SDL_RenderDrawLine(rend, ox - outh, oy + outq, ox - innh, oy + innq);
|
DrawLine(ox - outh, oy + outq, ox - innh, oy + innq);
|
||||||
SDL_RenderDrawLine(rend, ox - outh, oy - outq, ox - innh, oy - innq);
|
DrawLine(ox - outh, oy - outq, ox - innh, oy - innq);
|
||||||
|
|
||||||
// deadzone octagon
|
// deadzone octagon
|
||||||
SDL_RenderDrawLine(rend, ox - innq, oy - innh, ox + innq, oy - innh);
|
DrawLine(ox - innq, oy - innh, ox + innq, oy - innh);
|
||||||
SDL_RenderDrawLine(rend, ox + innq, oy - innh, ox + innh, oy - innq);
|
DrawLine(ox + innq, oy - innh, ox + innh, oy - innq);
|
||||||
SDL_RenderDrawLine(rend, ox + innh, oy - innq, ox + innh, oy - innq);
|
DrawLine(ox + innh, oy - innq, ox + innh, oy - innq);
|
||||||
SDL_RenderDrawLine(rend, ox + innh, oy - innq, ox + innh, oy + innq);
|
DrawLine(ox + innh, oy - innq, ox + innh, oy + innq);
|
||||||
SDL_RenderDrawLine(rend, ox + innh, oy + innq, ox + innq, oy + innh);
|
DrawLine(ox + innh, oy + innq, ox + innq, oy + innh);
|
||||||
SDL_RenderDrawLine(rend, ox + innq, oy + innh, ox - innq, oy + innh);
|
DrawLine(ox + innq, oy + innh, ox - innq, oy + innh);
|
||||||
SDL_RenderDrawLine(rend, ox - innq, oy + innh, ox - innh, oy + innq);
|
DrawLine(ox - innq, oy + innh, ox - innh, oy + innq);
|
||||||
SDL_RenderDrawLine(rend, ox - innh, oy + innq, ox - innh, oy - innq);
|
DrawLine(ox - innh, oy + innq, ox - innh, oy - innq);
|
||||||
SDL_RenderDrawLine(rend, ox - innh, oy - innq, ox - innq, oy - innh);
|
DrawLine(ox - innh, oy - innq, ox - innq, oy - innh);
|
||||||
|
|
||||||
// compensated position
|
// compensated position
|
||||||
SDL_SetRenderDrawColor(rend, 0x1F, 0xFF, 0x1F, 0xFF);
|
SetDrawColour(0x1FFF1FFF);
|
||||||
DrawCircle(
|
DrawCircleSteps(
|
||||||
ox + (int)round(p->compos.x * size / 2.0),
|
ox + (int)round(p->compos.x * size / 2.0),
|
||||||
oy + (int)round(p->compos.y * size / 2.0),
|
oy + (int)round(p->compos.y * size / 2.0),
|
||||||
8, 16);
|
8, 16);
|
||||||
SDL_RenderDrawPoint(rend,
|
DrawPoint(
|
||||||
ox + (int)round(p->compos.x * size / 2.0),
|
ox + (int)round(p->compos.x * size / 2.0),
|
||||||
oy + (int)round(p->compos.y * size / 2.0));
|
oy + (int)round(p->compos.y * size / 2.0));
|
||||||
|
|
||||||
// raw position
|
// raw position
|
||||||
SDL_SetRenderDrawColor(rend, 0xFF, 0xFF, 0xFF, 0xFF);
|
SetDrawColour(0xFFFFFFFF);
|
||||||
SDL_RenderDrawLine(rend,
|
DrawLine(
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0) - 4,
|
ox + (int)round(p->rawpos.x * size / 2.0) - 4,
|
||||||
oy + (int)round(p->rawpos.y * size / 2.0),
|
oy + (int)round(p->rawpos.y * size / 2.0),
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0) + 4,
|
ox + (int)round(p->rawpos.x * size / 2.0) + 4,
|
||||||
oy + (int)round(p->rawpos.y * size / 2.0));
|
oy + (int)round(p->rawpos.y * size / 2.0));
|
||||||
SDL_RenderDrawLine(rend,
|
DrawLine(
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0),
|
ox + (int)round(p->rawpos.x * size / 2.0),
|
||||||
oy + (int)round(p->rawpos.y * size / 2.0) - 4,
|
oy + (int)round(p->rawpos.y * size / 2.0) - 4,
|
||||||
ox + (int)round(p->rawpos.x * size / 2.0),
|
ox + (int)round(p->rawpos.x * size / 2.0),
|
||||||
@ -533,8 +500,8 @@ int main(int argc, char** argv)
|
|||||||
if (repaint)
|
if (repaint)
|
||||||
{
|
{
|
||||||
// background
|
// background
|
||||||
SDL_SetRenderDrawColor(rend, 0x1F, 0x1F, 0x1F, 0xFF);
|
SetDrawColour(0x1F1F1FFF);
|
||||||
SDL_RenderClear(rend);
|
DrawClear();
|
||||||
|
|
||||||
const int hrw = rw / 2;
|
const int hrw = rw / 2;
|
||||||
DrawDigital(&(SDL_Rect){ 0, 0, hrw, rh }, &stickl);
|
DrawDigital(&(SDL_Rect){ 0, 0, hrw, rh }, &stickl);
|
||||||
@ -543,17 +510,16 @@ int main(int argc, char** argv)
|
|||||||
// test player thingo
|
// test player thingo
|
||||||
if (showavatar)
|
if (showavatar)
|
||||||
{
|
{
|
||||||
SDL_Rect rect;
|
|
||||||
|
|
||||||
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, rw);
|
||||||
plrpos.y = pfmod(plrpos.y, rh);
|
plrpos.y = pfmod(plrpos.y, rh);
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(rend, 0xFF, 0x00, 0x00, 0xFF);
|
SetDrawColour(0xFF0000FF);
|
||||||
rect.w = rect.h = 32;
|
const int plrSz = 32;
|
||||||
rect.x = (int)plrpos.x - rect.w / 2;
|
DrawRect(
|
||||||
rect.y = (int)plrpos.y - rect.h / 2;
|
(int)plrpos.x - plrSz / 2,
|
||||||
SDL_RenderDrawRect(rend, &rect);
|
(int)plrpos.y - plrSz / 2,
|
||||||
|
plrSz, plrSz);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_RenderPresent(rend);
|
SDL_RenderPresent(rend);
|
||||||
|
62
draw.c
Normal file
62
draw.c
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#include "draw.h"
|
||||||
|
#include "maths.h"
|
||||||
|
#include <SDL_render.h>
|
||||||
|
|
||||||
|
SDL_Renderer* rend = NULL;
|
||||||
|
|
||||||
|
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;
|
||||||
|
for (int i = 0; i <= steps; ++i)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
17
draw.h
Normal file
17
draw.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef DRAW_H
|
||||||
|
#define DRAW_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct SDL_Renderer SDL_Renderer;
|
||||||
|
extern SDL_Renderer* rend;
|
||||||
|
|
||||||
|
void SetDrawColour(uint32_t c);
|
||||||
|
void DrawClear(void);
|
||||||
|
void DrawPoint(int x, int y);
|
||||||
|
void DrawRect(int x, int y, int w, int h);
|
||||||
|
void DrawLine(int x1, int y1, int x2, int y2);
|
||||||
|
void DrawCircle(int x, int y, int r);
|
||||||
|
void DrawCircleSteps(int x, int y, int r, int steps);
|
||||||
|
|
||||||
|
#endif//DRAW_H
|
Reference in New Issue
Block a user