GCC Code Coverage Report


Directory: ./
File: sources/System/system_handle_click.c
Date: 2023-09-29 04:53:15
Exec Total Coverage
Lines: 0 29 0.0%
Branches: 0 18 0.0%

Line Branch Exec Source
1 /*
2 * Filename: system_handle_click.c
3 * Path: sources/System
4 * Created Date: Friday, September 29th 2023, 12:56:22 am
5 * Author: osvegn
6 *
7 * Copyright (c) 2023 our_rpg
8 */
9
10 #include "systems.h"
11 #include "vector.h"
12 #include "world_entity.h"
13 #include "world_resource.h"
14 #include "resources.h"
15 #include "components.h"
16 #include <raylib.h>
17
18 int system_handle_click_constructor(system_t *system)
19 {
20 system->type = S_HANDLE_CLICK;
21 system->run = &system_handle_click;
22 system->active = true;
23 return 0;
24 }
25
26 int system_handle_click(void *world)
27 {
28 vector_t entities = {0};
29 int rvalue = world_join_entities(world, &entities, 1, C_CLICKABLE);
30 resource_t *r = world_get_resource_by_type(world, R_CAMERA);
31 entity_t *e = 0;
32 component_t *c = 0;
33 Vector2 mouse_position = GetMousePosition();
34 Vector2 old_mouse_position = {0};
35 if (r) {
36 Vector2 camera_position = (Camera2D *){r->data}->target;
37
38 mouse_position.x += camera_position.x;
39 mouse_position.y += camera_position.y;
40 camera_position = (Camera2D *){r->data}->offset;
41 mouse_position.x -= camera_position.x;
42 mouse_position.y -= camera_position.y;
43 }
44 old_mouse_position = mouse_position;
45
46 if (!IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
47 return (0);
48 }
49 for (unsigned int i = 0; i < entities.size(&entities); i++) {
50 mouse_position = old_mouse_position;
51 e = *(entity_t **)entities.at(&entities, i);
52 if (entity_contains_component_by_type(e, C_POSITION) && entity_contains_component_by_type(e, C_SIZE)) {
53 c = entity_get_component(e, C_POSITION);
54 mouse_position.x -= (ecs_vector2f_t *){c->data}->x;
55 mouse_position.y -= (ecs_vector2f_t *){c->data}->y;
56 if (mouse_position.x < 0 || mouse_position.y < 0)
57 continue;
58 c = entity_get_component(e, C_SIZE);
59 if (mouse_position.x > (ecs_vector2i_t *){c->data}->x || mouse_position.y > (ecs_vector2i_t *){c->data}->y)
60 continue;
61 printf("Click!\n");
62
63 } else {
64 printf("Click! (without position or size)\n");
65 }
66 }
67 return 0;
68 }
69