Add window utilities
This commit is contained in:
74
src/window.c
Normal file
74
src/window.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "window.h"
|
||||
#include "SDL_render.h"
|
||||
#include "SDL_video.h"
|
||||
#include "aliases/aliases.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
bool init_window(window_t *wnd, const char *title, u64 width, u64 height) {
|
||||
wnd->window =
|
||||
SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||
width, height, SDL_WINDOW_SHOWN);
|
||||
if (!(wnd->window)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
wnd->renderer = SDL_CreateRenderer(
|
||||
wnd->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
if (!(wnd->renderer)) {
|
||||
cleanup_window(wnd);
|
||||
return false;
|
||||
}
|
||||
|
||||
wnd->title = title;
|
||||
wnd->width = width;
|
||||
wnd->height = height;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void cleanup_window(window_t *wnd) {
|
||||
if (wnd->renderer) {
|
||||
SDL_DestroyRenderer(wnd->renderer);
|
||||
wnd->renderer = NULL;
|
||||
}
|
||||
|
||||
if (wnd->window) {
|
||||
SDL_DestroyWindow(wnd->window);
|
||||
wnd->window = NULL;
|
||||
}
|
||||
|
||||
wnd->width = wnd->height = 0;
|
||||
}
|
||||
|
||||
void set_colour(const window_t *wnd, colour_t colour) {
|
||||
SDL_SetRenderDrawColor(wnd->renderer, colour.colour.r, colour.colour.g,
|
||||
colour.colour.b, colour.colour.a);
|
||||
}
|
||||
|
||||
void clear_window(const window_t *wnd, colour_t colour) {
|
||||
set_colour(wnd, colour);
|
||||
SDL_RenderClear(wnd->renderer);
|
||||
}
|
||||
|
||||
void swap_buffers(const window_t *wnd) { SDL_RenderPresent(wnd->renderer); }
|
||||
|
||||
void draw_point(const window_t *wnd, point_t p, colour_t colour) {
|
||||
set_colour(wnd, colour);
|
||||
SDL_RenderDrawPoint(wnd->renderer, p.x, p.y);
|
||||
}
|
||||
|
||||
void draw_line(const window_t *wnd, const line_t *ln, colour_t colour) {
|
||||
set_colour(wnd, colour);
|
||||
SDL_RenderDrawLine(wnd->renderer, ln->p0.x, ln->p0.y, ln->p1.x, ln->p1.y);
|
||||
}
|
||||
|
||||
void draw_triangle(const window_t *wnd, const triangle_t *triangle,
|
||||
colour_t colour) {
|
||||
line_t ln0 = {triangle->p0, triangle->p1};
|
||||
line_t ln1 = {triangle->p0, triangle->p2};
|
||||
line_t ln2 = {triangle->p1, triangle->p2};
|
||||
|
||||
draw_line(wnd, &ln0, colour);
|
||||
draw_line(wnd, &ln1, colour);
|
||||
draw_line(wnd, &ln2, colour);
|
||||
}
|
Reference in New Issue
Block a user