| Directory: | ./ |
|---|---|
| File: | sources/Component/component_color.c |
| Date: | 2023-09-29 04:53:15 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 15 | 27 | 55.6% |
| Branches: | 7 | 12 | 58.3% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Filename: sources/Component/color.c | ||
| 3 | * Path: sources/Component | ||
| 4 | * Created Date: Friday, March 17th 2023, 9:27:54 am | ||
| 5 | * Author: Thomas | ||
| 6 | * | ||
| 7 | * Copyright (c) 2023 our_rpg | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include <stdlib.h> | ||
| 11 | #include <string.h> | ||
| 12 | #include "components.h" | ||
| 13 | #include "json.h" | ||
| 14 | #include "world_logger.h" | ||
| 15 | |||
| 16 | /// @brief Check if component is color type. | ||
| 17 | /// @param component Component to check. Must be a valid component_t. | ||
| 18 | /// @return Return 1 if component is C_COLOR, 0 otherwise. | ||
| 19 | /// @note This function is static. | ||
| 20 | static int component_is_color(const component_t *component) | ||
| 21 | { | ||
| 22 | 4 | if (component->type == C_COLOR) | |
| 23 | return 1; | ||
| 24 | ✗ | log_error("Component is not color."); | |
| 25 | return 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | 3 | int component_color_constructor(component_t *component, void *data) | |
| 29 | { | ||
| 30 | int rvalue = 0; | ||
| 31 | |||
| 32 | 3 | component->type = C_COLOR; | |
| 33 | 3 | component->data = malloc(sizeof(ecs_color_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 color component."); | |
| 36 | ✗ | return -1; | |
| 37 | } | ||
| 38 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (!data) { |
| 39 | data = &(ecs_color_t){0, 0, 0, 0}; | ||
| 40 | } | ||
| 41 | 3 | rvalue = component_set_color(component, data); | |
| 42 | 3 | log_info("Color component created."); | |
| 43 | 3 | return rvalue; | |
| 44 | } | ||
| 45 | |||
| 46 | ✗ | int component_color_constructor_from_json(component_t *component, void *data) | |
| 47 | { | ||
| 48 | ✗ | ecs_color_t color = {0}; | |
| 49 | ✗ | json_object *obj = json_tokener_parse(data); | |
| 50 | |||
| 51 | ✗ | color.r = json_object_get_int(json_object_object_get(obj, "r")); | |
| 52 | ✗ | color.g = json_object_get_int(json_object_object_get(obj, "g")); | |
| 53 | ✗ | color.b = json_object_get_int(json_object_object_get(obj, "b")); | |
| 54 | ✗ | color.a = json_object_get_int(json_object_object_get(obj, "a")); | |
| 55 | ✗ | return component_color_constructor(component, &color); | |
| 56 | } | ||
| 57 | |||
| 58 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | int component_set_color(component_t *component, void *data) |
| 59 | { | ||
| 60 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (!component_is_color(component) || !component->data || !data) |
| 61 | ✗ | return -1; | |
| 62 | memcpy(component->data, data, sizeof(ecs_color_t)); | ||
| 63 | 4 | return 0; | |
| 64 | } | ||
| 65 | |||
| 66 | 3 | void *component_get_color(const component_t *component) | |
| 67 | { | ||
| 68 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (component->type != C_COLOR) |
| 69 | return 0; | ||
| 70 | 3 | return component->data; | |
| 71 | } | ||
| 72 |