Directory: | ./ |
---|---|
File: | sources/Component/component_position.c |
Date: | 2023-09-29 04:53:15 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 14 | 25 | 56.0% |
Branches: | 7 | 12 | 58.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Filename: sources/Component/position.c | ||
3 | * Path: sources/Component | ||
4 | * Created Date: Sunday, January 15th 2023, 3:59:05 pm | ||
5 | * Author: osvegn | ||
6 | * | ||
7 | * Copyright (c) 2023 our_rpg | ||
8 | */ | ||
9 | |||
10 | #include "components.h" | ||
11 | #include "json.h" | ||
12 | #include "world_logger.h" | ||
13 | #include <stdlib.h> | ||
14 | #include <string.h> | ||
15 | |||
16 | /// @brief Check if a component is a position component. | ||
17 | /// @param component Component to check. | ||
18 | /// @return Return 1 if the component is a position component, 0 otherwise. | ||
19 | /// @note This function is static. | ||
20 | static int component_is_position(const component_t *component) | ||
21 | { | ||
22 | 6 | if (component->type == C_POSITION) | |
23 | return 1; | ||
24 | ✗ | log_error("Component is not position."); | |
25 | return 0; | ||
26 | } | ||
27 | |||
28 | 3 | int component_position_constructor(component_t *component, void *data) | |
29 | { | ||
30 | int rvalue = 0; | ||
31 | |||
32 | 3 | component->type = C_POSITION; | |
33 | 3 | component->data = malloc(sizeof(ecs_vector2f_t)); | |
34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!component->data) { |
35 | ✗ | log_fatal("Could not allocate memory for position component."); | |
36 | ✗ | return -1; | |
37 | } | ||
38 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (!data) { |
39 | data = &(ecs_vector2f_t){0, 0}; | ||
40 | } | ||
41 | 3 | rvalue = component_position_set(component, data); | |
42 | 3 | log_info("Position component created."); | |
43 | 3 | return rvalue; | |
44 | } | ||
45 | |||
46 | ✗ | int component_position_constructor_from_json(component_t *component, void *data) | |
47 | { | ||
48 | ✗ | json_object *obj = json_tokener_parse(data); | |
49 | ✗ | ecs_vector2f_t v = {0}; | |
50 | |||
51 | ✗ | v.x = json_object_get_int(json_object_object_get(obj, "x")); | |
52 | ✗ | v.y = json_object_get_int(json_object_object_get(obj, "y")); | |
53 | ✗ | return component_position_constructor(component, &v); | |
54 | } | ||
55 | |||
56 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | int component_position_set(component_t *component, void *data) |
57 | { | ||
58 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (!component_is_position(component) || !component->data || !data) |
59 | ✗ | return -1; | |
60 | memcpy(component->data, data, sizeof(ecs_vector2f_t)); | ||
61 | 4 | return 0; | |
62 | } | ||
63 | |||
64 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | void *component_position_get(const component_t *component) |
65 | { | ||
66 | if (!component_is_position(component)) | ||
67 | ✗ | return 0; | |
68 | 2 | return component->data; | |
69 | } | ||
70 |