comparison libmlk-core/core/painter.c @ 243:71b3b7036de7

misc: lot of cleanups, - prefix libraries with libmlk, - move assets from source directories closes #2520, - prefix header guards closes #2519
author David Demelier <markand@malikania.fr>
date Sat, 28 Nov 2020 22:37:30 +0100
parents libcore/core/painter.c@c679e08b32b2
children d01e83210ca2
comparison
equal deleted inserted replaced
242:4c24604efcab 243:71b3b7036de7
1 /*
2 * painter.c -- basic drawing routines
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <math.h>
20
21 #include "color.h"
22 #include "painter.h"
23 #include "texture.h"
24 #include "window.h"
25 #include "window_p.h"
26
27 /* Current texture renderer. */
28 static struct texture *renderer;
29
30 struct texture *
31 painter_get_target(void)
32 {
33 return renderer;
34 }
35
36 void
37 painter_set_target(struct texture *tex)
38 {
39 renderer = tex;
40 SDL_SetRenderTarget(RENDERER(), tex ? tex->handle : NULL);
41 }
42
43 unsigned long
44 painter_get_color(void)
45 {
46 Uint8 r = 0, g = 0, b = 0, a = 0;
47
48 SDL_GetRenderDrawColor(RENDERER(), &r, &g, &b, &a);
49
50 return COLOR_HEX(r, g, b, a);
51 }
52
53 void
54 painter_set_color(unsigned long color)
55 {
56 SDL_SetRenderDrawColor(
57 RENDERER(),
58 COLOR_R(color),
59 COLOR_G(color),
60 COLOR_B(color),
61 COLOR_A(color)
62 );
63 }
64
65 void
66 painter_draw_line(int x1, int y1, int x2, int y2)
67 {
68 SDL_RenderDrawLine(RENDERER(), x1, y1, x2, y2);
69 }
70
71 void
72 painter_draw_point(int x1, int y1)
73 {
74 SDL_RenderDrawPoint(RENDERER(), x1, y1);
75 }
76
77 void
78 painter_draw_rectangle(int x, int y, unsigned int width, unsigned int height)
79 {
80 const SDL_Rect rect = {
81 .w = width,
82 .h = height,
83 .x = x,
84 .y = y
85 };
86
87 SDL_RenderFillRect(RENDERER(), &rect);
88 }
89
90 void
91 painter_draw_circle(int x, int y, int radius)
92 {
93 // Note that there is more to altering the bitrate of this
94 // method than just changing this value. See how pixels are
95 // altered at the following web page for tips:
96 // http://www.libsdl.org/intro.en/usingvideo.html
97 for (double dy = 1; dy <= radius; dy += 1.0) {
98 double dx = floor(sqrt((2.0 * radius * dy) - (dy * dy)));
99
100 SDL_RenderDrawLine(RENDERER(), x - dx, y + dy - radius, x + dx, y + dy - radius);
101 SDL_RenderDrawLine(RENDERER(), x - dx, y - dy + radius, x + dx, y - dy + radius);
102 }
103 }
104
105 void
106 painter_clear(void)
107 {
108 SDL_RenderClear(RENDERER());
109 }
110
111 void
112 painter_present(void)
113 {
114 SDL_RenderPresent(RENDERER());
115 }