Design Pattern Examples
Overview of object-oriented design patterns
Flyweight_Exercise.c File Reference

Implementation of the Flyweight_Exercise() function as used in the Flyweight Pattern. More...

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "helpers/checkforkey.h"
#include "helpers/cursor.h"
#include "helpers/readkey.h"
#include "helpers/sleep.h"
#include "Flyweight_BigResourceManager.h"
#include "Flyweight_Display.h"
#include "Flyweight_Image.h"
#include "Flyweight_Exercise.h"
Include dependency graph for Flyweight_Exercise.c:

Go to the source code of this file.

Functions

static int _Flyweight_GenerateBigResource (int numImages, int width, int height)
 Generate a big resource, in this case, a text master "image" of the specified height, containing the specified number of smaller images laid out horizontally, using the given width for each image.
 
static void _Flyweight_MoveFlyweights (Flyweight_ImageList *imageList, int display_width, int display_height)
 Move the given flyweight instances within the display, bouncing them off the edges of the display.
 
static void _Flyweight_RenderFlyweights (Flyweight_ImageList *imageList, Display *displayArea)
 Render the image into the display, once for each flyweight instance.
 
static double GenerateVelocity (void)
 Generate a random velocity, which includes a speed and a direction. The velocity is 0.2 to 1.0 (in increments of 0.2) and the direction is either + or -.
 
static void _Flyweight_GenerateFlyweightClasses (int bigResourceId, int numFlyweights, int image_width, int image_height, int display_width, int display_height, Flyweight_ImageList *imageList)
 Helper function to generate the specified number of Flyweight_image objects and associate those objects with individual contexts and a single big resource.
 
static void _Flyweight_ClearDisplay (Display *display)
 Clear the "display" to a background image, erasing whatever was there before.
 
static void _Flyweight_GenerateDisplay (Display *display, int width, int height)
 Generate a display area in which to render the big resource.
 
static void _Flyweight_ShowDisplay (Display *display)
 Render the display to the screen.
 
void Flyweight_Exercise (void)
 Example of using the Flyweight design pattern.
 

Detailed Description

Implementation of the Flyweight_Exercise() function as used in the Flyweight Pattern.

Definition in file Flyweight_Exercise.c.

Function Documentation

◆ _Flyweight_ClearDisplay()

static void _Flyweight_ClearDisplay ( Display display)
static

Clear the "display" to a background image, erasing whatever was there before.

Parameters
displayA list of character arrays representing the display.

Definition at line 277 of file Flyweight_Exercise.c.

References Display::area, Display::height, and Display::width.

Referenced by _Flyweight_GenerateDisplay(), DesignPatternExamples_cpp::Flyweight_Exercise(), and Flyweight_Exercise().

◆ _Flyweight_GenerateBigResource()

static int _Flyweight_GenerateBigResource ( int  numImages,
int  width,
int  height 
)
static

Generate a big resource, in this case, a text master "image" of the specified height, containing the specified number of smaller images laid out horizontally, using the given width for each image.

If there are 5 images requested, then create a single image that is 5 * width wide and 1 * height tall.

Parameters
numImagesNumber of images to images to store in the single big resource (horizontally), between 1 and 9.
widthWidth of the "text" image, in characters. Minimum width is 3.
heightHeight of the "text" image, in characters. Minimum height is 3.
Returns
An index to the generated index in the BigResourceManager. Returns -1 if something went wrong (out of memory error).

Definition at line 40 of file Flyweight_Exercise.c.

References BigResourceManager_AddResource(), BigResource::data, and BigResource::numImages.

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise(), and Flyweight_Exercise().

◆ _Flyweight_GenerateDisplay()

static void _Flyweight_GenerateDisplay ( Display display,
int  width,
int  height 
)
static

Generate a display area in which to render the big resource.

Parameters
displayA Display object to initialize as the "display" window.
widthWidth of the display area.
heightHeight of the display area.

Definition at line 299 of file Flyweight_Exercise.c.

References _Flyweight_ClearDisplay(), and Display_Create().

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise(), and Flyweight_Exercise().

◆ _Flyweight_GenerateFlyweightClasses()

static void _Flyweight_GenerateFlyweightClasses ( int  bigResourceId,
int  numFlyweights,
int  image_width,
int  image_height,
int  display_width,
int  display_height,
Flyweight_ImageList imageList 
)
static

Helper function to generate the specified number of Flyweight_image objects and associate those objects with individual contexts and a single big resource.

The image and display sizes are provided so as to randomize the position of each flyweight within the display.

Parameters
bigResourceIdID of the big resource to use.
numFlyweightsNumber of flyweight instances to create.
image_widthWidth of the big resource image.
image_heightHeight of the big resource image.
display_widthWidth of the display in which the flyweight is to be rendered.
display_heightHeight of the display in which the flyweight is to be rendered.
imageListA Flyweight_ImageList object to be filled in with the generated Flyweight_Image objects.

Definition at line 246 of file Flyweight_Exercise.c.

References Flyweight_Image::BigResourceId, Flyweight_Image::Context, Flyweight_ImageList_Add(), GenerateVelocity(), Flyweight_Context::ImageHeight, Flyweight_Context::ImageWidth, Flyweight_Context::OffsetXToImage, Flyweight_Context::Position_X, Flyweight_Context::Position_Y, Flyweight_Context::Velocity_X, and Flyweight_Context::Velocity_Y.

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise(), and Flyweight_Exercise().

◆ _Flyweight_MoveFlyweights()

static void _Flyweight_MoveFlyweights ( Flyweight_ImageList imageList,
int  display_width,
int  display_height 
)
static

Move the given flyweight instances within the display, bouncing them off the edges of the display.

The display size and image size are provided here

Parameters
imageListList of Flyweight_Class instances to move.
display_widthWidth of display.
display_heightHeight of display.

Definition at line 142 of file Flyweight_Exercise.c.

References Flyweight_Image::Context, Flyweight_Context::ImageHeight, Flyweight_ImageList::images, Flyweight_ImageList::images_count, Flyweight_Context::ImageWidth, Flyweight_Context::Position_X, Flyweight_Context::Position_Y, Flyweight_Context::Velocity_X, and Flyweight_Context::Velocity_Y.

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise(), and Flyweight_Exercise().

◆ _Flyweight_RenderFlyweights()

static void _Flyweight_RenderFlyweights ( Flyweight_ImageList imageList,
Display displayArea 
)
static

◆ _Flyweight_ShowDisplay()

static void _Flyweight_ShowDisplay ( Display display)
static

Render the display to the screen.

Parameters
displayThe Display object to output to the console.

Definition at line 311 of file Flyweight_Exercise.c.

References Display::area, and Display::height.

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise(), and Flyweight_Exercise().

◆ Flyweight_Exercise()

void Flyweight_Exercise ( void  )

Example of using the Flyweight design pattern.

The Flyweight pattern is used when a large object needs to be represented by a much lighter weight class, possibly multiple instances of said light-weight class.

In this example, a large object is represented by a so-called "big resource" (a two-dimensional array of text characters) containing multiple images, one associated with each flyweight class. Flyweight classes that represent offset into the big resource, along with position and velocity, are attached to the big resource image so they all share the same image but have different positions and velocities. The image is rendered to a display area through the Flyweight class. The Flyweight class instances then have their positions updated, bouncing off the edges of the display area 60 times a second. This continues for 1000 iterations or until a key is pressed.

Definition at line 346 of file Flyweight_Exercise.c.

References _Flyweight_ClearDisplay(), _Flyweight_GenerateBigResource(), _Flyweight_GenerateDisplay(), _Flyweight_GenerateFlyweightClasses(), _Flyweight_MoveFlyweights(), _Flyweight_RenderFlyweights(), _Flyweight_ShowDisplay(), BigResourceManager_Clear(), checkforkey(), disableinputecho(), Display_Destroy(), enableinputecho(), Flyweight_ImageList_Clear(), getcursorposition(), readkey(), setcursorposition(), and sleep().

◆ GenerateVelocity()

static double GenerateVelocity ( void  )
static

Generate a random velocity, which includes a speed and a direction. The velocity is 0.2 to 1.0 (in increments of 0.2) and the direction is either + or -.

Returns
Returns the velocity.

Definition at line 223 of file Flyweight_Exercise.c.

Referenced by Flyweight_Exercise::_Flyweight_GenerateFlyweightClasses(), and _Flyweight_GenerateFlyweightClasses().