56 void Flyweight_Class::Render(std::vector<std::vector<char>>& display,
int offset_x,
int image_width,
int image_height,
int position_x,
int position_y)
61 _resource->
Render(display, offset_x, image_width, image_height, position_x, position_y);
70 void BigResource::Render(std::vector<std::vector<char>>& display,
int offset_x,
int image_width,
int image_height,
int position_x,
int position_y)
72 size_t display_width = display[0].size();
73 size_t display_height = display.size();
74 int starting_position_x = position_x;
75 int starting_position_y = position_y;
79 size_t image_render_width = image_width;
80 size_t image_render_height = image_height;
84 int starting_row_in_image = 0;
85 int starting_col_in_image = offset_x;
88 if (starting_position_x < 0)
90 starting_col_in_image = -starting_position_x;
91 image_render_width += starting_position_x;
92 starting_position_x = 0;
94 else if (starting_position_x + image_render_width > display_width)
96 image_render_width = display_width - starting_position_x;
98 if (starting_position_y < 0)
100 starting_row_in_image = -starting_position_y;
101 image_render_height += starting_position_y;
102 starting_position_y = 0;
104 else if (starting_position_y + image_render_height > display_height)
106 image_render_height = display_height - starting_position_y;
110 if (image_render_width > 0 && image_render_height > 0)
112 int current_display_row = starting_position_y;
113 int current_image_row = starting_row_in_image;
114 for (
size_t row = 0; row < image_render_height; ++row)
116 for (
size_t col = 0; col < image_render_width; ++col)
118 display[current_display_row][starting_position_x + col] =
_resource[current_image_row][starting_col_in_image + col];
120 current_display_row += 1;
121 current_image_row += 1;
135 return static_cast<int>(
_resource[0].size());
142 return static_cast<int>(
_resource.size());
147 return std::make_unique<Flyweight_Class>(
this, context);
171 for (BigResourceList::iterator resourceIter = std::begin(
_resources);
175 if ((*resourceIter)->ResourceId() == resourceId)
177 foundResource = (*resourceIter).get();
182 return foundResource;
188 _resources.push_back(std::make_unique<BigResource>(rawResource, newResourceId));
190 return newResourceId;
197 if (bigResource !=
nullptr)
201 return flyweightClass;
Declaration of the Flyweight_Context struct, and the Flyweight_Class, BigResource,...
Represents some big resource. In this case, a text "image" rendered as a list of strings....
int ResourceId()
Retrieve the resource ID for this 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 big resource into the given display at the given position.
Flyweight_Class::unique_ptr_t CreateFlyweight(Flyweight_Context context)
Generate a Flyweight class that will represent this big resource in some context-dependent way.
std::vector< std::string > _resource
int ImageHeight()
Retrieve the "image" height of the resource.
int ImageWidth()
Retrieve the "image" width of the resource.
static int _nextResourceId
The next ID to assign a raw resource for management.
std::vector< std::unique_ptr< BigResource > > BigResourceList
static int GetNextResourceId()
Retrieve the next resource ID that can be used.
static BigResourceList _resources
A list of all big resources managed by this class.
static BigResource * FindResource(int resourceId)
Retrieve the BigResource corresponding to the specified ID.
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,...
Flyweight_Class()
Default constructor.
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_Class()
Destructor.
Flyweight_Context _context
The context associated with this class. The calling entity uses this context to manipulate the flywei...
BigResource * _resource
The big resource being referenced by this flyweight class. In C#, a class is always a reference to an...
Flyweight_Context Context()
Retrieve the context for this class instance.
void SetContext(Flyweight_Context context)
Set the context for this class instance.
std::unique_ptr< Flyweight_Class > unique_ptr_t
int ImageHeight()
Retrieve the "image" height from underlying big resource.
int ImageWidth()
Retrieve the "image" width from underlying big resource.
The namespace containing all Design Pattern Examples implemented in C++.
Represents the context for an instance of the Flyweight_Class. In this case, the context includes pos...