add draw_rectangle

This commit is contained in:
opfez 2021-09-28 17:34:53 +02:00
parent 8ae094dd98
commit 5d752080f4
2 changed files with 29 additions and 10 deletions

2
TODO Normal file
View File

@ -0,0 +1,2 @@
Lines have small spaces sometimes, but it doesn't show up in the
screenshots. This is probably a bug with the canvas -> texture conversion.

37
main.c
View File

@ -8,7 +8,7 @@
#define W 800
#define H 600
#define PI acos(-1)
#define PI 3.14159265358979323844
typedef struct {
unsigned char r, g, b, a;
@ -117,6 +117,7 @@ render(sdl_state_t state)
SDL_RenderPresent(state.renderer);
}
void
plot_rgb(rgb_t canvas[], uint16_t x, uint16_t y, rgb_t colour)
{
@ -127,11 +128,8 @@ plot_rgb(rgb_t canvas[], uint16_t x, uint16_t y, rgb_t colour)
void
clear_canvas(rgb_t canvas[], rgb_t colour)
{
for (size_t y = 0; y < H; y++) {
for (size_t x = 0; x < W; x++) {
plot_rgb(canvas, x, y, colour);
}
}
for (size_t i = 0; i < W * H; i++)
canvas[i] = colour;
}
void
@ -380,22 +378,41 @@ cool_effect(rgb_t canvas[])
}
}
void
draw_square(rgb_t canvas[], rgb_t col, vec2_t a, vec2_t b, vec2_t c, vec2_t d)
{
draw_triangle(canvas, col, (triangle_t){a, b, c});
draw_triangle(canvas, col, (triangle_t){a, c, d});
}
int
main(void)
{
sdl_state_t state = init_sdl();
SDL_Event event;
vec2_t p = {0,0};
vec2_t q = {100, 0};
int selec = 1;
for (;;) {
uint32_t buttons = SDL_GetMouseState(NULL, NULL);
uint32_t buttons;
if (selec)
buttons = SDL_GetMouseState(&p.x, &p.y);
else
buttons = SDL_GetMouseState(&q.x, &q.y);
if (buttons & SDL_BUTTON(3))
break;
selec = !selec;
else if (buttons & SDL_BUTTON(1))
screenshot(state.canvas);
clear_canvas(state.canvas, BLACK);
cool_effect(state.canvas);
draw_square(state.canvas,
WHITE,
(vec2_t){50, 0},
q,
p,
(vec2_t){0, 100});
render(state);
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)