Move to more memory safe implementation of widget holding
This commit is contained in:
parent
ddc7898a78
commit
8e276ab2ab
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include "../json.hpp"
|
#include "../json.hpp"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
class Widget
|
class Widget
|
||||||
{
|
{
|
||||||
|
|||||||
@ -97,7 +97,7 @@ void WidgetRect::draw()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget* WidgetRect::builder(const nlohmann::json& j)
|
std::unique_ptr<Widget> WidgetRect::builder(const nlohmann::json& j)
|
||||||
{
|
{
|
||||||
int x = 0;
|
int x = 0;
|
||||||
int y = 0;
|
int y = 0;
|
||||||
@ -115,7 +115,7 @@ Widget* WidgetRect::builder(const nlohmann::json& j)
|
|||||||
json_extract(j, "stroke", stroke_size);
|
json_extract(j, "stroke", stroke_size);
|
||||||
json_extract(j, "color", color);
|
json_extract(j, "color", color);
|
||||||
|
|
||||||
return new WidgetRect(x, y, width, height, radius, stroke_size, color);
|
return std::make_unique<WidgetRect>(x, y, width, height, radius, stroke_size, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetRect::draw_circle_corner(int x, int y, int quadrant)
|
void WidgetRect::draw_circle_corner(int x, int y, int quadrant)
|
||||||
|
|||||||
@ -28,7 +28,7 @@ public:
|
|||||||
|
|
||||||
virtual void draw() override;
|
virtual void draw() override;
|
||||||
|
|
||||||
static Widget* builder(const nlohmann::json& j);
|
static std::unique_ptr<Widget> builder(const nlohmann::json& j);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// x, y - center of circle
|
// x, y - center of circle
|
||||||
|
|||||||
@ -262,7 +262,7 @@ void WidgetText::draw()
|
|||||||
SDL_FreeSurface(txt_surface);
|
SDL_FreeSurface(txt_surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget* WidgetText::builder(const nlohmann::json& j)
|
std::unique_ptr<Widget> WidgetText::builder(const nlohmann::json& j)
|
||||||
{
|
{
|
||||||
int x = 0;
|
int x = 0;
|
||||||
int y = 0;
|
int y = 0;
|
||||||
@ -299,7 +299,7 @@ Widget* WidgetText::builder(const nlohmann::json& j)
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new WidgetText(x, y, width, height,
|
return std::make_unique<WidgetText>(x, y, width, height,
|
||||||
text, fit, should_wrap,
|
text, fit, should_wrap,
|
||||||
halign, valign,
|
halign, valign,
|
||||||
halign_via_visible, valign_via_visible,
|
halign_via_visible, valign_via_visible,
|
||||||
|
|||||||
@ -73,7 +73,7 @@ public:
|
|||||||
|
|
||||||
virtual void draw() override;
|
virtual void draw() override;
|
||||||
|
|
||||||
static Widget* builder(const nlohmann::json& j);
|
static std::unique_ptr<Widget> builder(const nlohmann::json& j);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // WIDGET_TEXT_H_
|
#endif // WIDGET_TEXT_H_
|
||||||
|
|||||||
20
main.cpp
20
main.cpp
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -17,14 +18,16 @@
|
|||||||
using std::cout;
|
using std::cout;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
using std::map;
|
using std::map;
|
||||||
|
using std::shared_ptr;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
using std::unique_ptr;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
void init_builders(map<string, Widget*(*)(const json&)>& widget_builders)
|
void init_builders(map<string, unique_ptr<Widget>(*)(const json&)>& widget_builders)
|
||||||
{
|
{
|
||||||
widget_builders["text"] = &WidgetText::builder;
|
|
||||||
widget_builders["rect"] = &WidgetRect::builder;
|
widget_builders["rect"] = &WidgetRect::builder;
|
||||||
|
widget_builders["text"] = &WidgetText::builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
@ -41,8 +44,8 @@ int main(int argc, char **argv)
|
|||||||
SDL_Surface* main_surface = nullptr;
|
SDL_Surface* main_surface = nullptr;
|
||||||
|
|
||||||
vector<json> widgets_cfg;
|
vector<json> widgets_cfg;
|
||||||
vector<Widget*> widgets;
|
vector<shared_ptr<Widget>> widgets;
|
||||||
map<string, Widget*(*)(const json&)> widget_builders;
|
map<string, unique_ptr<Widget>(*)(const json&)> widget_builders;
|
||||||
|
|
||||||
ok = init_sdl();
|
ok = init_sdl();
|
||||||
if (!ok)
|
if (!ok)
|
||||||
@ -102,7 +105,7 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Construct and add widget to vector
|
// Construct and add widget to vector
|
||||||
Widget* widget = widget_builders[widget_name](j);
|
shared_ptr<Widget> widget = widget_builders[widget_name](j);
|
||||||
if (nullptr != widget)
|
if (nullptr != widget)
|
||||||
{
|
{
|
||||||
widgets.push_back(widget);
|
widgets.push_back(widget);
|
||||||
@ -113,7 +116,7 @@ int main(int argc, char **argv)
|
|||||||
SDL_FillRect(main_surface, nullptr, SDL_MapRGBA(main_surface->format, 255, 255, 255, SDL_ALPHA_OPAQUE));
|
SDL_FillRect(main_surface, nullptr, SDL_MapRGBA(main_surface->format, 255, 255, 255, SDL_ALPHA_OPAQUE));
|
||||||
|
|
||||||
// Draw and apply all widgets
|
// Draw and apply all widgets
|
||||||
for (Widget* widget : widgets)
|
for (shared_ptr<Widget>& widget : widgets)
|
||||||
{
|
{
|
||||||
widget->draw();
|
widget->draw();
|
||||||
SDL_Rect rect = widget->get_rect();
|
SDL_Rect rect = widget->get_rect();
|
||||||
@ -125,11 +128,6 @@ int main(int argc, char **argv)
|
|||||||
IMG_SavePNG(main_surface, output_filename.c_str());
|
IMG_SavePNG(main_surface, output_filename.c_str());
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
// Destroy All Widgets
|
|
||||||
for (Widget* widget : widgets)
|
|
||||||
{
|
|
||||||
delete widget;
|
|
||||||
}
|
|
||||||
widgets.clear();
|
widgets.clear();
|
||||||
|
|
||||||
clean_sdl();
|
clean_sdl();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user