Design Pattern Examples
Overview of object-oriented design patterns
Flyweight_Display.c
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8
9#include "Flyweight_Display.h"
10
12// Display_Create()
14bool Display_Create(Display* display, int width, int height)
15{
16 bool created = false;
17
18 if (display != NULL)
19 {
20 char** rows = calloc(1, height * sizeof(char*));
21 if (rows != NULL)
22 {
23 display->area = rows;
24 display->width = width;
25 display->height = height;
26 created = true;
27 for (int rowIndex = 0; rowIndex < height; rowIndex++)
28 {
29 char* row = calloc(1, width + 1);
30 if (row == NULL)
31 {
32 created = false;
33 break;
34 }
35 rows[rowIndex] = row;
36 }
37
38 if (!created)
39 {
40 Display_Destroy(display);
41 }
42 }
43 }
44 return created;
45}
46
48// Display_Create()
51{
52 if (display != NULL)
53 {
54 if (display->area != NULL)
55 {
56 for (int rowIndex = 0; rowIndex < display->height; rowIndex++)
57 {
58 free(display->area[rowIndex]);
59 }
60 free(display->area);
61 display->area = NULL;
62 }
63 display->width = 0;
64 display->height = 0;
65 }
66}
67
bool Display_Create(Display *display, int width, int height)
Create a "display" window in the given Display object, with the given width and height.
void Display_Destroy(Display *display)
Destroy the "display" window in the given Display object by releasing the memory associated with it....
Declaration of the Display_Create() and Display_Destroy() functions for managing the Display structur...
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.