| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
* Filename: /home/thomas/Documents/Perso/our_rpg/sources/Resource/resource_scene_filename.c |
| 3 |
|
|
* Path: /home/thomas/Documents/Perso/our_rpg/sources/Resource |
| 4 |
|
|
* Created Date: Tuesday, March 21st 2023, 9:45:13 am |
| 5 |
|
|
* Author: Thomas |
| 6 |
|
|
* |
| 7 |
|
|
* Copyright (c) 2023 our_rpg |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
#include "resources.h" |
| 11 |
|
|
#include <stdlib.h> |
| 12 |
|
|
#include <string.h> |
| 13 |
|
|
#include "json.h" |
| 14 |
|
|
#include "world_logger.h" |
| 15 |
|
|
#ifdef __linux__ |
| 16 |
|
|
#include <sys/time.h> |
| 17 |
|
|
#endif |
| 18 |
|
|
|
| 19 |
|
|
static int resource_is_game_clock(const resource_t *resource) |
| 20 |
|
|
{ |
| 21 |
|
✗ |
if (resource->type == R_GAME_CLOCK) |
| 22 |
|
|
return 1; |
| 23 |
|
✗ |
log_error("Resource is not game clock."); |
| 24 |
|
|
return 0; |
| 25 |
|
|
} |
| 26 |
|
|
|
| 27 |
|
✗ |
int resource_game_clock_constructor(resource_t *resource, void *data) |
| 28 |
|
|
{ |
| 29 |
|
|
int rvalue = 0; |
| 30 |
|
✗ |
resource->type = R_GAME_CLOCK; |
| 31 |
|
|
#ifdef __linux__ |
| 32 |
|
|
struct timeval start; |
| 33 |
|
|
|
| 34 |
|
✗ |
gettimeofday(&start, NULL); |
| 35 |
|
✗ |
resource->data = malloc(sizeof(struct timeval)); |
| 36 |
|
✗ |
if (!resource->data) { |
| 37 |
|
|
return -1; |
| 38 |
|
|
} |
| 39 |
|
✗ |
if (!data) { |
| 40 |
|
|
data = &start; |
| 41 |
|
|
} |
| 42 |
|
|
#endif |
| 43 |
|
✗ |
resource->destructor = &resource_game_clock_destructor; |
| 44 |
|
✗ |
rvalue = resource_game_clock_set(resource, data); |
| 45 |
|
✗ |
log_info("game_clock resource created."); |
| 46 |
|
✗ |
return rvalue; |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
|
✗ |
int resource_game_clock_constructor_from_json(resource_t *resource, void *data) |
| 50 |
|
|
{ |
| 51 |
|
|
void *new_data = 0; |
| 52 |
|
|
|
| 53 |
|
✗ |
return resource_game_clock_constructor(resource, new_data); |
| 54 |
|
|
} |
| 55 |
|
|
|
| 56 |
|
✗ |
int resource_game_clock_destructor(resource_t *resource) |
| 57 |
|
|
{ |
| 58 |
|
✗ |
if (resource->data) |
| 59 |
|
✗ |
free(resource->data); |
| 60 |
|
✗ |
log_info("game_clock resource destroyed."); |
| 61 |
|
✗ |
return 0; |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
✗ |
int resource_game_clock_set(resource_t *resource, void *data) |
| 65 |
|
|
{ |
| 66 |
|
|
if (!resource_is_game_clock(resource)) |
| 67 |
|
✗ |
return -1; |
| 68 |
|
|
#ifdef __linux__ |
| 69 |
|
✗ |
memcpy(resource->data, data, sizeof(struct timeval)); |
| 70 |
|
|
#endif |
| 71 |
|
✗ |
return 0; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
✗ |
void *resource_game_clock_get(const resource_t *resource) |
| 75 |
|
|
{ |
| 76 |
|
|
if (!resource_is_game_clock(resource)) |
| 77 |
|
✗ |
return 0; |
| 78 |
|
✗ |
return resource->data; |
| 79 |
|
|
} |
| 80 |
|
|
|