11#include "helpers/checkforkey.h"
12#include "helpers/cursor.h"
13#include "helpers/readkey.h"
14#include "helpers/sleep.h"
48 else if (numImages > 9)
61 size_t imageSize = (width * height) * numImages;
65 char* image = calloc(1, imageSize + 1);
68 const char imageBackgroundCharacter[] =
"0123456789";
70 resource->
data = image;
73 for (
int row = 0; row < height; ++row)
75 int imageColumnOffset = row * (width * numImages);
76 for (
int imageIndex = 0; imageIndex < numImages; imageIndex++)
78 char backgroundChar = imageBackgroundCharacter[imageIndex];
79 for (
int colIndex = 0; colIndex < width; colIndex++)
81 if (row == 0 || (row + 1) == height)
84 if (colIndex == 0 || (colIndex + 1) == width)
86 image[imageColumnOffset + colIndex] =
'+';
90 image[imageColumnOffset + colIndex] =
'-';
96 if (colIndex == 0 || (colIndex + 1) == width)
98 image[imageColumnOffset + colIndex] =
'|';
105 image[imageColumnOffset + colIndex] = backgroundChar;
109 imageColumnOffset += width;
114 if (resourceId == -1)
122 printf(
" Error! Failed to allocate memory for the big image: size = %zu\n", imageSize);
127 printf(
" Error! Failed to allocate memory for the big resource structure: size = %zu\n",
sizeof(
BigResource));
143 int display_width,
int display_height)
145 if (imageList != NULL)
147 for (
size_t imageIndex = 0; imageIndex < imageList->
images_count; imageIndex++)
156 if (newx < 0 || (newx + image_width) > display_width)
166 newx = display_width - image_width;
170 if (newy < 0 || (newy + image_height) > display_height)
180 newy = display_height - image_height;
198 if (imageList != NULL && displayArea != NULL)
202 for (
size_t imageIndex = 0; imageIndex < imageList->
images_count; imageIndex++)
225 double speed = ((rand() % 5) + 1) / 5.0;
226 double direction = ((rand() % 100) > 50) ? 1.0 : -1.0;
227 return speed * direction;
247 int image_width,
int image_height,
int display_width,
int display_height,
252 for (
int index = 0; index < numFlyweights; ++index)
279 if (display != NULL && display->
area != NULL)
281 for (
int rowIndex = 0; rowIndex < display->
height; rowIndex++)
283 char* row = display->
area[rowIndex];
284 for (
int colIndex = 0; colIndex < display->
width; ++colIndex)
313 if (display != NULL && display->
area != NULL)
315 for (
int rowIndex = 0; rowIndex < display->
height; rowIndex++)
317 printf(
" %s\n", display->
area[rowIndex]);
348 printf(
"\nFlyweight_Exercise\n");
351 const int DISPLAY_WIDTH = 80;
352 const int DISPLAY_HEIGHT = 20;
353 const int IMAGE_WIDTH = 30;
354 const int IMAGE_HEIGHT = 5;
355 const int NUMFLYWEIGHTS = 5;
356 const int NUM_ITERATIONS = 1000;
359 printf(
"bigResourceId = %d\n", bigResourceId);
363 IMAGE_WIDTH, IMAGE_HEIGHT, DISPLAY_WIDTH, DISPLAY_HEIGHT, &imageList);
373 printf(
" The image rendered %d times:\n", NUMFLYWEIGHTS);
384 if (cursorLeft != -1 && cursorTop != -1)
386 cursorTop -= DISPLAY_HEIGHT + 1;
389 for (
int index = 0; index < NUM_ITERATIONS; ++index)
391 if (cursorLeft != -1)
395 printf(
" %5d/%d iterations [press a key to exit early]\n", index + 1, NUM_ITERATIONS);
396 if (cursorLeft != -1)
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...
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.
Declaration of the Big Resource Manager functions, BigResourceManager_Clear(), BigResourceManager_Add...
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...
static double GenerateVelocity(void)
Generate a random velocity, which includes a speed and a direction. The velocity is 0....
static void _Flyweight_GenerateDisplay(Display *display, int width, int height)
Generate a display area in which to render the big resource.
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_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 objec...
static void _Flyweight_RenderFlyweights(Flyweight_ImageList *imageList, Display *displayArea)
Render the image into the display, once for each flyweight instance.
static void _Flyweight_ClearDisplay(Display *display)
Clear the "display" to a background image, erasing whatever was there before.
void Flyweight_Exercise(void)
Example of using the Flyweight design pattern.
static void _Flyweight_ShowDisplay(Display *display)
Render the display to the screen.
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,...
void Flyweight_ImageList_Clear(Flyweight_ImageList *imageList)
Clear the given Flyweight_ImageList object by freeing up all allocated Flyweight_image objects and re...
void Flyweight_ImageList_Add(Flyweight_ImageList *imageList, Flyweight_Image *image)
Add a Flyweight_Image object to the given Flyweight_ImageList object. The list takes ownership of the...
Declaration of the Flyweight_ImageList and Flyweight_Image structures, the latter which wraps the big...
bool checkforkey(void)
Determine if a key has been pressed.
Declaration of the Flyweight_Exercise() function as used in the Flyweight Pattern.
void disableinputecho(void)
Disable echoing input until enableinputecho() is called.
void enableinputecho(void)
Enable echoing input, which should be the default mode. Call this only after calling disableinputecho...
void setcursorposition(int row, int column)
Move the text cursor to the specified screen coordinates.
void getcursorposition(int *row, int *column)
Retrieve the current cursor position in the console window.
int readkey(void)
Read a key from the keyboard, blocking if no key is pressed. Use the checkforkey() function to see if...
void sleep(int milliseconds)
Sleep for the specified number of milliseconds. Does not return until after the sleep period.
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.
Represents the context for an instance of the Flyweight_Image structure. In this case,...
double Position_Y
Vertical position of upper left corner of image in a display.
int OffsetXToImage
Offset into big resource to left edge of image.
double Position_X
Horizontal position of upper left corner of image in a display.
int ImageHeight
Height of image.
int ImageWidth
Width of image.
double Velocity_X
Velocity to apply to the horizontal position.
double Velocity_Y
Velocity to apply to the vertical position.
Represents an image that associates a context with a big resource.
Flyweight_Context Context
The context associated with this image. The calling entity uses this context to manipulate the positi...
int BigResourceId
The big resource being referenced by this flyweight image. This is represented by a handle to the big...
Represents an expandable list of Flyweight_Image objects. This is managed by the Flyweight_ImageList_...
size_t images_count
Number of Flyweight_image objects in the list.
Flyweight_Image * images
Dynamic list of Flyweight_Image objects.