Design Pattern Examples
Overview of object-oriented design patterns
Flyweight_BigResourceManager.c
Go to the documentation of this file.
1
7
8#include <stdlib.h>
9
11
18typedef struct
19{
23
24//=============================================================================
25//=============================================================================
26
33
34//=============================================================================
35//=============================================================================
36
44static void BigResourceList_Clear(BigResourceList* resourceList)
45{
46 if (resourceList != NULL && resourceList->resources != NULL && resourceList->resources_count != 0)
47 {
48 for (size_t index = 0; index < resourceList->resources_count; index++)
49 {
50 BigResource_Clear(resourceList->resources[index]);
51 }
52 free(resourceList->resources);
53 resourceList->resources = NULL;
54 resourceList->resources_count = 0;
55 }
56}
57
69static int BigResourceList_AddResource(BigResourceList* resourceList, BigResource* resource)
70{
71 int resourceIndex = -1;
72 BigResource** new_list = NULL;
73
74 if (resourceList != NULL && resource != NULL)
75 {
76 if (resourceList->resources == NULL)
77 {
78 new_list = malloc(sizeof(BigResource*));
79 }
80 else
81 {
82 size_t newSize = (resourceList->resources_count + 1) * sizeof(BigResource*);
83 new_list = realloc(resourceList->resources, newSize);
84 }
85
86 if (new_list != NULL)
87 {
88 resourceIndex = (int)resourceList->resources_count;
89 resourceList->resources = new_list;
90 resourceList->resources[resourceIndex] = resource;
91 resourceList->resources_count++;
92 }
93 }
94 return resourceIndex;
95}
96
97
98//=============================================================================
99//=============================================================================
100
101
103// BigResourceManager_Clear()
106{
108}
109
110
112// BigResourceManager_AddResource()
115{
116 int handle = -1;
117 if (rawResource != NULL)
118 {
119 handle = (int)BigResourceList_AddResource(&_resources, rawResource);
120 }
121 return handle;
122}
123
125// BigResourceManager_GetResource()
128{
129 BigResource* resource = NULL;
130
131 if (bigResourceId >= 0 && (size_t)bigResourceId < _resources.resources_count)
132 {
133 resource = _resources.resources[bigResourceId];
134 }
135
136 return resource;
137}
void BigResource_Clear(BigResource *resource)
Clear the BigResource object, freeing any memory associated with it.
static int BigResourceList_AddResource(BigResourceList *resourceList, BigResource *resource)
Add a BigResource object to the given BigResourceList object. The list takes ownership of the BigReso...
static BigResourceList _resources
A list of BigResource objects. Initialized by a call to the BigResourceList_AddResource()....
int BigResourceManager_AddResource(BigResource *rawResource)
Add a new big resource and return the ID of the resource. If the resource is successfully added,...
void BigResourceManager_Clear(void)
Release all resources owned by the Big Resource Manager.
BigResource * BigResourceManager_GetResource(int bigResourceId)
Retrieve the requested big resource.
static void BigResourceList_Clear(BigResourceList *resourceList)
Clear the given BigResourceList object by freeing up all allocated BigResource objects and resetting ...
Declaration of the Big Resource Manager functions, BigResourceManager_Clear(), BigResourceManager_Add...
Represents a big image. Call the BigResource_Clear() function to release the memory used by each inst...
Represents an expandable list of pointers to BigResource objects. Use the BigResourceList_Clear() to ...
BigResource ** resources
Dynamic list of pointers to BigResource objects.
size_t resources_count
Number of BigResource objects in the list.