Design Pattern Examples
Overview of object-oriented design patterns
Flyweight_BigResource.c
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8
11
12
14// BigResource_Clear()
17{
18 if (resource != NULL)
19 {
20 free(resource->data);
21 resource->data = NULL;
22 }
23}
24
26// BigResource_Render()
28void BigResource_Render(Display* display, int bigResourceId, int offset_x,
29 int image_width, int image_height, int position_x, int position_y)
30{
31 if (display != NULL)
32 {
33 BigResource* resource = BigResourceManager_GetResource(bigResourceId);
34 if (resource != NULL)
35 {
36 size_t display_width = display->width;
37 size_t display_height = display->height;
38 int starting_position_x = position_x;
39 int starting_position_y = position_y;
40
41 // Size of image to render (can be smaller than actual image if image
42 // lies partially of right or bottom of display).
43 size_t image_render_width = image_width;
44 size_t image_render_height = image_height;
45
46 // Position into image to start rendering from (non-zero if
47 // image is off the left or top edge of display).
48 //int starting_row_in_image = 0;
49 int starting_col_in_image = offset_x;
50
51 // Clip the image to the display.
52 if (starting_position_x < 0)
53 {
54 starting_col_in_image = -starting_position_x;
55 image_render_width += starting_position_x;
56 starting_position_x = 0;
57 }
58 else if (starting_position_x + image_render_width > display_width)
59 {
60 image_render_width = display_width - starting_position_x;
61 }
62 if (starting_position_y < 0)
63 {
64 //starting_row_in_image = -starting_position_y;
65 image_render_height += starting_position_y;
66 starting_position_y = 0;
67 }
68 else if (starting_position_y + image_render_height > display_height)
69 {
70 image_render_height = display_height - starting_position_y;
71 }
72
73 // If the image is even partially visible, render it
74 if (image_render_width > 0 && image_render_height > 0)
75 {
76 int current_display_row = starting_position_y;
77 char* resourceRow = resource->data + starting_col_in_image;
78 for (size_t row = 0; row < image_render_height; ++row)
79 {
80 char* areaRow = display->area[current_display_row];
81 for (size_t col = 0; col < image_render_width; ++col)
82 {
83 areaRow[starting_position_x + col] = resourceRow[col];
84 }
85 current_display_row += 1;
86 resourceRow += resource->numImages * image_width;
87 }
88 }
89 }
90 }
91}
void BigResource_Render(Display *display, int bigResourceId, int offset_x, int image_width, int image_height, int position_x, int position_y)
Render the specified portion of the big resource into the given display at the given coordinates in t...
void BigResource_Clear(BigResource *resource)
Clear the BigResource object, freeing any memory associated with it.
Declaration of the BigResource structure along with the supporting BigResource_Clear function....
BigResource * BigResourceManager_GetResource(int bigResourceId)
Retrieve the requested big resource.
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...
char * data
Image data, row-oriented.
int numImages
Number of images represented in the big image.
Represents a "display" window, in which to render Flyweight images. This "display" window is then pri...
int width
Width of each row.
int height
Height of each row.
char ** area
2-dimensional array of strings, representing rows. Each row is zero-terminated.