main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2020, Nico Weber <thakis@chromium.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Application.h>
  27. #include <LibGUI/Icon.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/Widget.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibGfx/Bitmap.h>
  32. #include <LibGfx/Font.h>
  33. #include <LibGfx/Painter.h>
  34. #include <LibGfx/Palette.h>
  35. #include <LibGfx/Path.h>
  36. #include <LibGfx/SystemTheme.h>
  37. #include <LibGfx/WindowTheme.h>
  38. #include <unistd.h>
  39. const int WIDTH = 300;
  40. const int HEIGHT = 200;
  41. class Canvas final : public GUI::Widget {
  42. C_OBJECT(Canvas)
  43. public:
  44. virtual ~Canvas() override;
  45. private:
  46. Canvas();
  47. RefPtr<Gfx::Bitmap> m_bitmap_1x;
  48. RefPtr<Gfx::Bitmap> m_bitmap_2x;
  49. RefPtr<Gfx::Bitmap> m_bitmap_2x_as_1x;
  50. void draw(Gfx::Painter& painter);
  51. virtual void paint_event(GUI::PaintEvent&) override;
  52. };
  53. Canvas::Canvas()
  54. {
  55. m_bitmap_1x = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { WIDTH, HEIGHT }, 1);
  56. m_bitmap_2x = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { WIDTH, HEIGHT }, 2);
  57. // m_bitmap_1x and m_bitmap_2x have the same logical size, so LibGfx will try to draw them at the same physical size:
  58. // When drawing on a 2x backing store it'd scale m_bitmap_1x up 2x and paint m_bitmap_2x at its physical size.
  59. // When drawing on a 1x backing store it'd draw m_bitmap_1x at its physical size, and it would have to scale down m_bitmap_2x to 0.5x its size.
  60. // But the system can't current scale down, and we want to draw the 2x bitmap at twice the size of the 1x bitmap in this particular application,
  61. // so make a 1x alias of the 2x bitmap to make LibGfx paint it without any scaling at paint time, mapping once pixel to one pixel.
  62. m_bitmap_2x_as_1x = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::BGRx8888, m_bitmap_2x->physical_size(), 1, m_bitmap_2x->pitch(), m_bitmap_2x->scanline(0));
  63. Gfx::Painter painter_1x(*m_bitmap_1x);
  64. draw(painter_1x);
  65. Gfx::Painter painter_2x(*m_bitmap_2x);
  66. draw(painter_2x);
  67. update();
  68. }
  69. Canvas::~Canvas()
  70. {
  71. }
  72. void Canvas::paint_event(GUI::PaintEvent& event)
  73. {
  74. GUI::Painter painter(*this);
  75. painter.add_clip_rect(event.rect());
  76. painter.fill_rect(event.rect(), Color::Magenta);
  77. painter.blit({ 0, 0 }, *m_bitmap_1x, m_bitmap_1x->rect());
  78. painter.blit({ 0, HEIGHT }, *m_bitmap_2x_as_1x, m_bitmap_2x_as_1x->rect());
  79. }
  80. void Canvas::draw(Gfx::Painter& painter)
  81. {
  82. auto active_window_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png");
  83. Gfx::WindowTheme::current().paint_normal_frame(painter, Gfx::WindowTheme::WindowState::Active, { 4, 18, WIDTH - 8, HEIGHT - 29 }, "Well hello friends 🐞", *active_window_icon, palette(), { WIDTH - 20, 6, 16, 16 }, 0);
  84. painter.draw_rect({ 20, 34, WIDTH - 40, HEIGHT - 45 }, palette().color(Gfx::ColorRole::Selection), true);
  85. painter.draw_rect({ 24, 38, WIDTH - 48, HEIGHT - 53 }, palette().color(Gfx::ColorRole::Selection));
  86. // buggie.png has an alpha channel.
  87. auto buggie = Gfx::Bitmap::load_from_file("/res/graphics/buggie.png");
  88. painter.blit({ 25, 39 }, *buggie, { 2, 30, 62, 20 });
  89. painter.draw_scaled_bitmap({ 88, 39, 62 * 2, 20 * 2 }, *buggie, Gfx::IntRect { 2, 30, 62, 20 });
  90. painter.draw_scaled_bitmap({ 202, 39, 80, 40 }, *buggie, Gfx::IntRect { 2, 30, 62, 20 });
  91. painter.draw_tiled_bitmap({ 25, 60, WIDTH - 50, 40 }, *buggie);
  92. painter.blit({ 25, 101 }, *buggie, { 2, 30, 3 * buggie->width(), 20 });
  93. // grid does not have an alpha channel.
  94. auto grid = Gfx::Bitmap::load_from_file("/res/wallpapers/grid.png");
  95. VERIFY(!grid->has_alpha_channel());
  96. painter.fill_rect({ 25, 122, 62, 20 }, Color::Green);
  97. painter.blit({ 25, 122 }, *grid, { (grid->width() - 62) / 2, (grid->height() - 20) / 2 + 40, 62, 20 }, 0.9);
  98. painter.blit_brightened({ 88, 122 }, *buggie, { 2, 30, 62, 20 });
  99. painter.blit_dimmed({ 140, 122 }, *buggie, { 2, 30, 62, 20 });
  100. painter.blit_disabled({ 192, 122 }, *buggie, { 2, 30, 62, 20 }, palette());
  101. }
  102. int main(int argc, char** argv)
  103. {
  104. auto app = GUI::Application::construct(argc, argv);
  105. if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
  106. perror("pledge");
  107. return 1;
  108. }
  109. if (unveil("/res", "r") < 0) {
  110. perror("unveil");
  111. return 1;
  112. }
  113. if (unveil(nullptr, nullptr) < 0) {
  114. perror("unveil");
  115. return 1;
  116. }
  117. auto window = GUI::Window::construct();
  118. window->set_title("LibGfx Scale Demo");
  119. window->set_resizable(false);
  120. window->resize(WIDTH * 2, HEIGHT * 3);
  121. auto app_icon = GUI::Icon::default_icon("app-libgfx-demo");
  122. window->set_icon(app_icon.bitmap_for_size(16));
  123. window->set_main_widget<Canvas>();
  124. window->show();
  125. return app->exec();
  126. }