Skip to content

Commit

Permalink
Add rounded rect, parser instance once
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangfitus committed Dec 5, 2023
1 parent 0f681fc commit 5b5011c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
25 changes: 19 additions & 6 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ void Renderer::drawLine(Line* line) const {
}

void Renderer::drawRectangle(Rect* rectangle) const {
float x = rectangle->getPosition().x;
float y = rectangle->getPosition().y;
float width = rectangle->getWidth();
float height = rectangle->getHeight();
mColor fill_color = rectangle->getFillColor();
mColor outline_color = rectangle->getOutlineColor();

Expand All @@ -124,12 +128,21 @@ void Renderer::drawRectangle(Rect* rectangle) const {
Gdiplus::Matrix original;
graphics.GetTransform(&original);
applyTransform(rectangle->getTransforms());
graphics.FillRectangle(&RectFill, rectangle->getPosition().x,
rectangle->getPosition().y, rectangle->getWidth(),
rectangle->getHeight());
graphics.DrawRectangle(&RectOutline, rectangle->getPosition().x,
rectangle->getPosition().y, rectangle->getWidth(),
rectangle->getHeight());
if (rectangle->getRadius().x != 0 || rectangle->getRadius().y != 0) {
float dx = rectangle->getRadius().x * 2;
float dy = rectangle->getRadius().y * 2;
Gdiplus::GraphicsPath path;
path.AddArc(x, y, dx, dy, 180, 90);
path.AddArc(x + width - dx, y, dx, dy, 270, 90);
path.AddArc(x + width - dx, y + height - dy, dx, dy, 0, 90);
path.AddArc(x, y + height - dy, dx, dy, 90, 90);
path.CloseFigure();
graphics.FillPath(&RectFill, &path);
graphics.DrawPath(&RectOutline, &path);
} else {
graphics.FillRectangle(&RectFill, x, y, width, height);
graphics.DrawRectangle(&RectOutline, x, y, width, height);
}
graphics.SetTransform(&original);
}

Expand Down
21 changes: 11 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

using namespace rapidxml;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

Parser* parser = nullptr;

void OnPaint(HDC hdc, const std::string& filePath) {
Gdiplus::Graphics graphics(hdc);

Parser* parser = Parser::getInstance(filePath);
if (!parser) {
parser = Parser::getInstance(filePath);
}
Renderer* renderer = Renderer::getInstance(graphics);
SVGElement* root = parser->getRoot();
Group* group = dynamic_cast< Group* >(root);
group->render(*renderer);
delete parser;
}

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow) {
HWND hWnd;
MSG msg;
Expand Down Expand Up @@ -69,6 +69,7 @@ INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow) {
DispatchMessage(&msg);
}

if (parser) delete parser;
Gdiplus::GdiplusShutdown(gdiplusToken);
return msg.wParam;
}
Expand All @@ -77,14 +78,14 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) {
HDC hdc;
PAINTSTRUCT ps;
std::string filePath = "";
std::string filePath;
if (__argc > 1) {
filePath = __argv[1];
}

switch (message) {
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
if (__argc > 1) {
filePath = __argv[1];
}
OnPaint(hdc, filePath);
EndPaint(hWnd, &ps);
return 0;
Expand Down

0 comments on commit 5b5011c

Please sign in to comment.