10#include "helpers/sleep.h"
11#include "helpers/cursor.h"
12#include "helpers/readkey.h"
13#include "helpers/checkforkey.h"
14#include "helpers/formatstring.h"
44 else if (numImages > 9)
57 std::vector<std::string> image;
58 for (
int row = 0; row < height; ++row)
60 std::string image_row(
"");
61 for (
int imageIndex = 0; imageIndex < numImages; imageIndex++)
63 if (row == 0 || (row + 1) == height)
77 image.push_back(image_row);
91 for (std::vector<char>& row : display)
93 for (
size_t index = 0; index < row.size(); ++index)
109 std::vector<std::vector<char>> display;
111 for (
int row = 0; row < height; ++row)
113 display.push_back(std::vector<char>(width));
127 std::ostringstream output;
128 for (
auto& row : display)
131 for (
size_t col = 0; col < row.size(); col++)
137 std::cout << output.str() << std::endl;
152 int display_width,
int display_height)
154 for (FlyweightClassList::iterator flyweightIter = std::begin(flyweightInstances);
155 flyweightIter != std::end(flyweightInstances);
165 if (newx < 0 || (newx + image_width) > display_width)
175 newx = display_width - image_width;
179 if (newy < 0 || (newy + image_height) > display_height)
189 newy = display_height - image_height;
209 for (FlyweightClassList::iterator flyweightIter = std::begin(flyweightInstances);
210 flyweightIter != std::end(flyweightInstances);
215 flyweight->
Render(displayArea,
232 double speed = ((rand() % 5) + 1) / 5.0;
233 double direction = ((rand() % 100) > 50) ? 1.0 : -1.0;
234 return speed * direction;
254 int image_width,
int image_height,
int display_width,
int display_height)
260 for (
int index = 0; index < numFlyweights; ++index)
267 context.
Position_X = rand() % (display_width - image_width);
268 context.
Position_Y = rand() % (display_height - image_height);
277 return flyweightInstances;
308 std::cout << std::endl;
309 std::cout <<
"Flyweight Exercise" << std::endl;
312 const int DISPLAY_WIDTH = 80;
313 const int DISPLAY_HEIGHT = 20;
314 const int IMAGE_WIDTH = 30;
315 const int IMAGE_HEIGHT = 5;
316 const int NUMFLYWEIGHTS = 5;
317 const int NUM_ITERATIONS = 1000;
322 IMAGE_WIDTH, IMAGE_HEIGHT, DISPLAY_WIDTH, DISPLAY_HEIGHT);
332 std::cout << std::endl;
342 if (cursorLeft != -1 && cursorTop != -1)
344 cursorTop -= DISPLAY_HEIGHT + 1;
346 for (
int index = 0; index < NUM_ITERATIONS; ++index)
348 if (cursorLeft != -1)
352 std::cout <<
Helpers::formatstring(
" %5d/%d iterations [press a key to exit early]", index + 1, NUM_ITERATIONS) << std::endl;
353 if (cursorLeft != -1)
371 std::cout <<
" Done." << std::endl;
Declaration of the Flyweight_Context struct, and the Flyweight_Class, BigResource,...
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.
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,...
static int AddResource(std::vector< std::string > rawResource)
Add a new big resource and return the ID of the resource.
static Flyweight_Class::unique_ptr_t CreateFlyweight(int bigResourceId, Flyweight_Context context)
Create a new instance of the Flyweight_Class associated with the given big resource and a context,...
Associates a context with a big resource.
void Render(std::vector< std::vector< char > > &display, int offset_x, int image_width, int image_height, int position_x, int position_y)
Render the image associated with this flyweight instance into the given display at the given position...
Flyweight_Context Context()
Retrieve the context for this class instance.
void SetContext(Flyweight_Context context)
Set the context for this class instance.
int ImageHeight()
Retrieve the "image" height from underlying big resource.
int ImageWidth()
Retrieve the "image" width from underlying big resource.
Declaration of the Flyweight_Exercise() function as used in the Flyweight Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
std::vector< Flyweight_Class::unique_ptr_t > FlyweightClassList
void Flyweight_Exercise()
Example of using the Flyweight design pattern.
void enableinputecho()
Enable echoing input, which should be the default mode. Call this only after calling disableinputecho...
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
void disableinputecho()
Disable echoing input until enableinputecho() is called.
int readkey()
Read a key from the keyboard, blocking if no key is pressed.
void sleep(int milliseconds)
Sleep for the specified number of milliseconds. Does not return until after the sleep period.
bool checkforkey()
Determine if a key has been pressed.
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.
Represents the context for an instance of the Flyweight_Class. In this case, the context includes pos...