5 #include "system/error.h"
8 int draw_triangle(SDL_Renderer *render,
13 if (SDL_RenderDrawLine(render,
17 (int) roundf(t.p2.y)) < 0) {
18 throw_error(ERROR_TYPE_SDL2);
22 if (SDL_RenderDrawLine(render,
26 (int) roundf(t.p3.y)) < 0) {
27 throw_error(ERROR_TYPE_SDL2);
31 if (SDL_RenderDrawLine(render,
35 (int) roundf(t.p1.y)) < 0) {
36 throw_error(ERROR_TYPE_SDL2);
43 static int fill_bottom_flat_triangle(SDL_Renderer *render,
48 const float invslope1 = (t.p2.x - t.p1.x) / (t.p2.y - t.p1.y);
49 const float invslope2 = (t.p3.x - t.p1.x) / (t.p3.y - t.p1.y);
51 const int y0 = (int) roundf(t.p1.y);
52 const int y1 = (int) roundf(t.p2.y);
57 for (int scanline = y0; scanline < y1; scanline++) {
58 if (SDL_RenderDrawLine(render,
72 static int fill_top_flat_triangle(SDL_Renderer *render,
77 const float invslope1 = (t.p3.x - t.p1.x) / (t.p3.y - t.p1.y);
78 const float invslope2 = (t.p3.x - t.p2.x) / (t.p3.y - t.p2.y);
80 const int y0 = (int) roundf(t.p3.y);
81 const int y1 = (int) roundf(t.p1.y);
86 for (int scanline = y0; scanline > y1; --scanline) {
87 if (SDL_RenderDrawLine(render,
102 int fill_triangle(SDL_Renderer *render,
105 t = triangle_sorted_by_y(t);
107 if (fabs(t.p2.y - t.p3.y) < 1e-6) {
108 if (fill_bottom_flat_triangle(render, t) < 0) {
111 } else if (fabs(t.p1.y - t.p2.y) < 1e-6) {
112 if (fill_top_flat_triangle(render, t) < 0) {
116 const point_t p4 = vec(t.p1.x + ((t.p2.y - t.p1.y) / (t.p3.y - t.p1.y)) * (t.p3.x - t.p1.x), t.p2.y);
118 if (fill_bottom_flat_triangle(render, triangle(t.p1, t.p2, p4)) < 0) {
122 if (fill_top_flat_triangle(render, triangle(t.p2, p4, t.p3)) < 0) {
126 if (SDL_RenderDrawLine(render,
127 (int) roundf(t.p2.x),
128 (int) roundf(t.p2.y),
130 (int) roundf(p4.y)) < 0) {
138 int render_text_shaded(SDL_Renderer *renderer,
149 lt_t * const lt = create_lt();
154 SDL_Surface * const surface = PUSH_LT(
156 TTF_RenderText_Shaded(font, text, color_for_sdl(color), color_for_sdl(bg)),
158 if (surface == NULL) {
159 throw_error(ERROR_TYPE_SDL2_TTF);
163 SDL_Texture * const texture = PUSH_LT(
165 SDL_CreateTextureFromSurface(renderer, surface),
167 if (texture == NULL) {
168 throw_error(ERROR_TYPE_SDL2);
175 if (SDL_QueryTexture(texture, NULL, NULL, &texW, &texH) < 0) {
176 throw_error(ERROR_TYPE_SDL2);
180 const SDL_Rect dstrect = { (int) position.x, (int) position.y, texW, texH };
182 if (SDL_RenderCopy(renderer, texture, NULL, &dstrect) < 0) {
183 throw_error(ERROR_TYPE_SDL2);