Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Filename: window_resource.c |
3 |
|
|
* Path: sources/Resource |
4 |
|
|
* Created Date: Sunday, February 12th 2023, 5:27:54 pm |
5 |
|
|
* Author: Thomas |
6 |
|
|
* |
7 |
|
|
* Copyright (c) 2023 our_rpg |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "json.h" |
11 |
|
|
#include "raylib.h" |
12 |
|
|
#include "resources.h" |
13 |
|
|
#include "world_logger.h" |
14 |
|
|
#include <stdio.h> |
15 |
|
|
#include <stdlib.h> |
16 |
|
|
#include <string.h> |
17 |
|
|
|
18 |
|
|
/// @brief Check if the resource is a R_WINDOW or not. |
19 |
|
|
/// @param resource The resource to be checked. |
20 |
|
|
/// @return 1 if the resource is a R_WINDOW, 0 otherwise. |
21 |
|
|
/// @note This function is static. |
22 |
|
|
static int resource_is_window(const resource_t *resource) |
23 |
|
|
{ |
24 |
|
✗ |
if (resource->type == R_WINDOW) |
25 |
|
|
return 1; |
26 |
|
✗ |
log_error("Resource is not window."); |
27 |
|
|
return 0; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
✗ |
int resource_window_constructor(resource_t *resource, void *data) |
31 |
|
|
{ |
32 |
|
|
int rvalue = 0; |
33 |
|
|
|
34 |
|
✗ |
resource->type = R_WINDOW; |
35 |
|
✗ |
resource->data = malloc(sizeof(window_t)); |
36 |
|
✗ |
if (!resource->data) { |
37 |
|
✗ |
log_fatal("Could not allocate memory for window resource."); |
38 |
|
✗ |
return -1; |
39 |
|
|
} |
40 |
|
✗ |
resource->destructor = &resource_window_destructor; |
41 |
|
✗ |
if (!data) { |
42 |
|
|
data = &(window_t){.fps=60, .height=640, .width=840, .title="Default title"}; |
43 |
|
|
} |
44 |
|
✗ |
rvalue = resource_window_set(resource, data); |
45 |
|
✗ |
log_info("Window resource created."); |
46 |
|
✗ |
return rvalue; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
✗ |
int resource_window_constructor_from_json(resource_t *resource, void *data) |
50 |
|
|
{ |
51 |
|
✗ |
window_t win = {0}; |
52 |
|
✗ |
json_object *obj = json_tokener_parse(data); |
53 |
|
|
|
54 |
|
✗ |
win.width = json_object_get_int(json_object_object_get(obj, "width")); |
55 |
|
✗ |
win.height = json_object_get_int(json_object_object_get(obj, "height")); |
56 |
|
✗ |
win.fps = json_object_get_int(json_object_object_get(obj, "fps")); |
57 |
|
✗ |
strncpy(win.title, json_object_get_string(json_object_object_get(obj, "title")), 255); |
58 |
|
✗ |
return resource_window_constructor(resource, &win); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
✗ |
int resource_window_set(resource_t *resource, void *data) |
62 |
|
|
{ |
63 |
|
|
window_t *win = 0; |
64 |
|
|
|
65 |
|
✗ |
if (!resource_is_window(resource) || !resource->data || !data) |
66 |
|
✗ |
return -1; |
67 |
|
|
memcpy(resource->data, data, sizeof(resource_t)); |
68 |
|
✗ |
win = resource->data; |
69 |
|
✗ |
InitWindow(win->width, win->height, win->title); |
70 |
|
✗ |
SetTargetFPS(win->fps); |
71 |
|
✗ |
return 0; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
✗ |
void *resource_window_get(const resource_t *resource) |
75 |
|
|
{ |
76 |
|
|
if (!resource_is_window(resource)) |
77 |
|
✗ |
return 0; |
78 |
|
✗ |
return resource->data; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
✗ |
int resource_window_destructor(resource_t *resource) |
82 |
|
|
{ |
83 |
|
✗ |
CloseWindow(); |
84 |
|
✗ |
if (resource->data) |
85 |
|
✗ |
free(resource->data); |
86 |
|
✗ |
log_info("Window resource destroyed."); |
87 |
|
✗ |
return 0; |
88 |
|
|
} |
89 |
|
|
|