]> git.lizzy.rs Git - nothing.git/blob - src/game/proto_rect.c
(#278) Implement ProtoRect
[nothing.git] / src / game / proto_rect.c
1 #include <stdbool.h>
2
3 #include <SDL2/SDL.h>
4
5 #include "system/stacktrace.h"
6 #include "system/log.h"
7 #include "game/camera.h"
8
9 #include "./proto_rect.h"
10
11 int proto_rect_render(const ProtoRect *proto_rect,
12                       Camera *camera)
13 {
14     trace_assert(proto_rect);
15     trace_assert(camera);
16
17     if (proto_rect->active) {
18         if (camera_fill_rect(
19                 camera,
20                 rect_from_points(
21                     proto_rect->begin,
22                     proto_rect->end),
23                 proto_rect->color) < 0) {
24             return -1;
25         }
26     }
27
28     return 0;
29 }
30
31 int proto_rect_update(ProtoRect *proto_rect,
32                       float delta_time)
33 {
34     trace_assert(proto_rect);
35     (void) delta_time;
36     return 0;
37 }
38
39 int proto_rect_event(ProtoRect *proto_rect,
40                      const SDL_Event *event,
41                      const Camera *camera)
42 {
43     trace_assert(proto_rect);
44     trace_assert(event);
45
46     if (proto_rect->active) {
47         // Active
48         switch (event->type) {
49         case SDL_MOUSEBUTTONUP: {
50             switch (event->button.button) {
51             case SDL_BUTTON_LEFT: {
52                 proto_rect->active = false;
53             } break;
54             }
55         } break;
56         }
57     } else {
58         // Inactive
59         switch (event->type) {
60         case SDL_MOUSEBUTTONDOWN: {
61             switch (event->button.button) {
62             case SDL_BUTTON_LEFT: {
63                 proto_rect->active = true;
64                 proto_rect->begin = camera_map_screen(
65                     camera,
66                     event->button.x,
67                     event->button.y);
68                 proto_rect->end = proto_rect->begin;
69             } break;
70             }
71         } break;
72         }
73     }
74
75     return 0;
76 }