diff options
author | crupest <crupest@outlook.com> | 2021-11-28 22:32:55 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-11-28 22:32:55 +0800 |
commit | 80096172330bc0148320384f4b01f3ea62e52c92 (patch) | |
tree | b67e57541189503861f4df0dd621a6b2fcec6c7d /demos | |
parent | 48a70ee60683d6ced778ad82f4510975c74dc4c7 (diff) | |
download | cru-80096172330bc0148320384f4b01f3ea62e52c92.tar.gz cru-80096172330bc0148320384f4b01f3ea62e52c92.tar.bz2 cru-80096172330bc0148320384f4b01f3ea62e52c92.zip |
...
Diffstat (limited to 'demos')
-rw-r--r-- | demos/CMakeLists.txt | 3 | ||||
-rw-r--r-- | demos/graphics_experiments/1/main.cpp | 76 | ||||
-rw-r--r-- | demos/graphics_experiments/CMakeLists.txt | 4 |
3 files changed, 82 insertions, 1 deletions
diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt index 3edabd0a..cb9d3f6e 100644 --- a/demos/CMakeLists.txt +++ b/demos/CMakeLists.txt @@ -2,7 +2,7 @@ add_library(cru_demo_base INTERFACE) target_link_libraries(cru_demo_base INTERFACE cru_platform_bootstrap) -if(WIN32) # Currently only enable tests on Windows. +if(WIN32) add_subdirectory(main) add_subdirectory(scroll_view) @@ -12,6 +12,7 @@ elseif(APPLE) add_subdirectory(scroll_view) add_subdirectory(input_method) + add_subdirectory(graphics_experiments) elseif(UNIX) add_subdirectory(xcb) endif() diff --git a/demos/graphics_experiments/1/main.cpp b/demos/graphics_experiments/1/main.cpp new file mode 100644 index 00000000..926557e7 --- /dev/null +++ b/demos/graphics_experiments/1/main.cpp @@ -0,0 +1,76 @@ +#include "cru/platform/Color.hpp" +#include "cru/platform/bootstrap/Bootstrap.hpp" +#include "cru/platform/graphics/Factory.hpp" +#include "cru/platform/graphics/Painter.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/platform/gui/Window.hpp" + +#include <functional> + +void BresenhamDrawLine(int x1, int y1, int x2, int y2, + const std::function<void(int, int)>& draw_pixel) { + // calculating range for line between start and end point + int dx = x2 - x1; + int dy = y2 - y1; + + int x = x1; + int y = y1; + + // this is the case when slope(m) < 1 + if (abs(dx) > abs(dy)) { + draw_pixel(x, y); // this putpixel is for very first pixel of the line + int pk = (2 * abs(dy)) - abs(dx); + + for (int i = 0; i < abs(dx); i++) { + x = x + 1; + if (pk < 0) + pk = pk + (2 * abs(dy)); + else { + y = y + 1; + pk = pk + (2 * abs(dy)) - (2 * abs(dx)); + } + draw_pixel(x, y); + } + } else { + // this is the case when slope is greater than or equal to 1 i.e: m>=1 + draw_pixel(x, y); // this putpixel is for very first pixel of the line + int pk = (2 * abs(dx)) - abs(dy); + + for (int i = 0; i < abs(dy); i++) { + y = y + 1; + if (pk < 0) + pk = pk + (2 * abs(dx)); + else { + x = x + 1; + pk = pk + (2 * abs(dx)) - (2 * abs(dy)); + } + + draw_pixel(x, y); // display pixel at coordinate (x, y) + } + } +} + +int main() { + auto application = cru::platform::bootstrap::CreateUiApplication(); + auto window = application->CreateWindow(); + + auto brush = application->GetGraphicsFactory()->CreateSolidColorBrush( + cru::platform::colors::black); + + window->SetClientSize(cru::platform::Size(400, 200)); + + window->PaintEvent()->AddHandler([window, &brush](nullptr_t) { + auto painter = window->BeginPaint(); + auto draw_pixel = [&painter, &brush](int x, int y) { + painter->FillRectangle({static_cast<float>(x) - 0.5f, + static_cast<float>(y) - 0.5f, 1.0f, 1.0f}, + brush.get()); + }; + BresenhamDrawLine(50, 50, 100, 200, draw_pixel); + BresenhamDrawLine(50, 50, 200, 100, draw_pixel); + }); + + window->SetVisibility(cru::platform::gui::WindowVisibilityType::Show); + + return application->Run(); +} diff --git a/demos/graphics_experiments/CMakeLists.txt b/demos/graphics_experiments/CMakeLists.txt new file mode 100644 index 00000000..cc0ce99a --- /dev/null +++ b/demos/graphics_experiments/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(cru_graphics_experiment_1 + 1/main.cpp +) +target_link_libraries(cru_graphics_experiment_1 PRIVATE cru_demo_base) |