|
6 | 6 |
|
7 | 7 | #include <stdbool.h> |
8 | 8 | #include "SDL.h" |
| 9 | + |
9 | 10 | #include "canvas.h" |
| 11 | +#include "screen.h" |
10 | 12 |
|
11 | 13 |
|
12 | 14 | /* Draw a single pixel on the canvas */ |
13 | 15 | void draw_pixel(int x, int y, Uint8 r, Uint8 g, Uint8 b) |
14 | 16 | { |
15 | | - struct canvas_coordinate coord; |
16 | | - coord.x = x; |
17 | | - coord.y = y; |
18 | | - |
19 | | - if (coordinate_validate(&coord) == false) |
20 | | - return; |
21 | | - |
22 | | - struct canvas_pixel *pixel = canvas_getpixel(coord); |
| 17 | + SDL_Renderer * renderer = screen_getrenderer(); |
23 | 18 |
|
24 | | - pixel->r = r; |
25 | | - pixel->g = g; |
26 | | - pixel->b = b; |
| 19 | + SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF); |
| 20 | + SDL_RenderDrawPoint(renderer, x, y); |
27 | 21 | } |
28 | 22 |
|
29 | 23 |
|
30 | 24 | /* Draw a rectangle of pixels on the canvas */ |
31 | 25 | void draw_rectangle(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b) |
32 | 26 | { |
33 | | - int x, y; |
34 | | - struct canvas_coordinate coord; |
35 | | - struct canvas_pixel * pixel; |
36 | | - |
37 | | - for (y = y1; y < y2; y++) |
38 | | - for (x = x1; x < x2; x++) { |
39 | | - coord.x = x; |
40 | | - coord.y = y; |
41 | | - |
42 | | - if (coordinate_validate(&coord) == false) |
43 | | - continue; |
44 | | - |
45 | | - pixel = canvas_getpixel(coord); |
46 | | - |
47 | | - pixel->r = r; |
48 | | - pixel->g = g; |
49 | | - pixel->b = b; |
50 | | - } |
| 27 | + SDL_Rect rect; |
| 28 | + |
| 29 | + if (x2 > x1) { |
| 30 | + rect.x = x1; |
| 31 | + rect.w = x2 - x1; |
| 32 | + } else { |
| 33 | + rect.x = x2; |
| 34 | + rect.w = x1 - x2; |
| 35 | + } |
| 36 | + |
| 37 | + if (y2 > y1) { |
| 38 | + rect.y = y1; |
| 39 | + rect.h = y2 - y1; |
| 40 | + } else { |
| 41 | + rect.y = y2; |
| 42 | + rect.h = y1 - y2; |
| 43 | + } |
| 44 | + |
| 45 | + SDL_Renderer * renderer = screen_getrenderer(); |
| 46 | + |
| 47 | + SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF); |
| 48 | + SDL_RenderDrawRect(renderer, &rect); |
51 | 49 | } |
52 | 50 |
|
53 | 51 |
|
54 | 52 | /* Draw over the whole canvas */ |
55 | 53 | void clear(Uint8 r, Uint8 g, Uint8 b) |
56 | 54 | { |
57 | | - Canvas *canvas = canvas_get(); |
58 | | - |
59 | | - int x, y; |
60 | | - struct canvas_coordinate coord; |
61 | | - struct canvas_pixel * pixel; |
62 | | - |
63 | | - for (y = 0; y < canvas->size_y; y++) |
64 | | - for (x = 0; x < canvas->size_x; x++) { |
65 | | - coord.x = x; |
66 | | - coord.y = y; |
67 | | - |
68 | | - pixel = canvas_getpixel(coord); |
69 | | - |
70 | | - pixel->r = r; |
71 | | - pixel->g = g; |
72 | | - pixel->b = b; |
73 | | - } |
| 55 | + SDL_Renderer * renderer = screen_getrenderer(); |
| 56 | + |
| 57 | + SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF); |
| 58 | + SDL_RenderClear(renderer); |
74 | 59 | } |
0 commit comments