Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
matrix:
platform: [X86-64]
compiler: [Clang, GCC]
runner: [ubuntu-26.04, ubuntu-24.04, ubuntu-22.04,] # TODO: Add these once ARM builds are working: ubuntu-26.04-arm, ubuntu-24.04-arm, ubuntu-22.04-arm
runner: [ubuntu-26.04, ubuntu-24.04] # TODO: Add these once ARM builds are working: ubuntu-26.04-arm, ubuntu-24.04-arm, ubuntu-22.04-arm
runs-on: ${{ matrix.runner }}

steps:
Expand Down
132 changes: 78 additions & 54 deletions Code/Examples/4-CustomPaintingExample/EntryPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <optional>
#include <vector>

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
Expand All @@ -13,6 +14,8 @@

class CustomPaintingForm {
public:
static const int texture_height = 100;
static const int texture_width = 100;

// An escape hatch is provided so a user is not limited by the library.
// A user can implement whatever custom behavior they desire by interacting directly with the underlying API.
Expand All @@ -23,79 +26,100 @@ class CustomPaintingForm {
return 1; // defer erasing to WM_PAINT
case WM_PAINT:
{
PAINTSTRUCT paint_info;
HDC device_context = BeginPaint(form->window_handle_, &paint_info);

int width = paint_info.rcPaint.right - paint_info.rcPaint.left;
int height = paint_info.rcPaint.bottom - paint_info.rcPaint.top;
auto painting_context = maxGUI::CreatePaintingContext(*form);
if (!painting_context.has_value()) {
return 0;
}

HBITMAP bitmap_handle = CreateCompatibleBitmap(device_context, width, height);
DWORD buffer_size_in_bytes = static_cast<size_t>(width) * height * 4; // 3 color channels, 4th channel is unused
auto buffer = std::make_unique<uint8_t[]>(static_cast<size_t>(buffer_size_in_bytes));
auto dirty_rectangle = painting_context->GetDirtyRectangle();

if (black_texture_.has_value()) {
auto dirty_rectangle_bottom = dirty_rectangle.TopLeft.Y() + dirty_rectangle.Height;
for (int y = dirty_rectangle.TopLeft.Y(); y < dirty_rectangle_bottom; y += texture_height) {

// Create a black background with a white circle in it
int circle_center_y = 50;
int circle_center_x = 75;
double radius = 30.0;
auto dirty_rectangle_right = dirty_rectangle.TopLeft.X() + dirty_rectangle.Width;
for (int x = dirty_rectangle.TopLeft.X(); x < dirty_rectangle_right; x += texture_width) {
maxGUI::PaintTexture(*painting_context, *black_texture_, x, y);
}
}
}

for (int y = paint_info.rcPaint.top; y < paint_info.rcPaint.bottom; y++) {
int y_distance = abs(circle_center_y - y);
// We might overpaint the top-left but that's fine.
if (dirty_rectangle.TopLeft.Y() < texture_height && dirty_rectangle.TopLeft.X() < texture_width) {
if (circle_texture_.has_value()) {
maxGUI::PaintTexture(*painting_context, *circle_texture_, 0, 0);
}
}
}
return 0;
}

for (int x = paint_info.rcPaint.left; x < paint_info.rcPaint.right; x++) {
int x_distance = abs(circle_center_x - x);
return DefWindowProc(form->window_handle_, message, wparam, lparam);
}

double distance_from_pixel_to_circle_center = sqrt(static_cast<double>(x_distance) * x_distance + static_cast<double>(y_distance) * y_distance);

// Our paint rectangle might not be (0,0) top left.
// For example, if the user resizes the window to reveal more, our paint rectangle might only be the newly revealed area.
size_t buffer_y_offset = static_cast<size_t>(y) - paint_info.rcPaint.top;
size_t buffer_x_offset = static_cast<size_t>(x) - paint_info.rcPaint.left;
void OnCreated(maxGUI::FormConcept* form) noexcept {
const size_t buffer_size_in_bytes = texture_width * texture_height * 4; // 3 color channels, 4th channel is unused
auto image_data = std::vector<uint8_t>(buffer_size_in_bytes);

size_t i = ((buffer_y_offset * width) + buffer_x_offset) * 4;
if (distance_from_pixel_to_circle_center < radius) {
buffer[i+0] = 0xff; // blue channel
buffer[i+1] = 0xff; // green channel
buffer[i+2] = 0xff; // red channel
} else {
buffer[i+0] = 0; // blue channel
buffer[i+1] = 0; // green channel
buffer[i+2] = 0; // red channel

}
buffer[i+3] = 0; // unused channel
}
}
for (int y = texture_height - 1; y != 0; y--) {
for (int x = texture_width - 1; x != 0; x--) {
const size_t i = ((y * texture_width) + x) * 4; // 4 channels

image_data[i+0] = 0; // blue
image_data[i+1] = 0; // green
image_data[i+2] = 0; // red
image_data[i+3] = 0; // unused
}
}

auto black_texture_creation_result = maxGUI::CreateTextureForForm(*form, texture_width, texture_height, std::span(image_data.data(), texture_height * texture_width));
if (black_texture_creation_result.has_value()) {
black_texture_ = *std::move(black_texture_creation_result);
}


const int circle_center_y = 50;
const int circle_center_x = 70;
const double radius = 30.0;

for (int y = texture_height - 1; y != 0; y--) {
const int y_distance = abs(circle_center_y - y);

for (int x = texture_width - 1; x != 0; x--) {
const int x_distance = abs(circle_center_x - x);
const double distance_from_pixel_to_circle_center = sqrt(static_cast<double>(x_distance) * x_distance + static_cast<double>(y_distance) * y_distance);

BITMAPINFO bitmap_info = {{0}, {{0}}};
bitmap_info.bmiHeader.biSize = sizeof(bitmap_info);
bitmap_info.bmiHeader.biWidth = width;
bitmap_info.bmiHeader.biHeight = -height;
bitmap_info.bmiHeader.biPlanes = 1;
bitmap_info.bmiHeader.biBitCount = 32;
bitmap_info.bmiHeader.biCompression = BI_RGB;
bitmap_info.bmiHeader.biSizeImage = buffer_size_in_bytes;
HDC memory_dc = CreateCompatibleDC(device_context);
SetDIBits(memory_dc, bitmap_handle, 0, static_cast<UINT>(height), buffer.get(), &bitmap_info, DIB_RGB_COLORS);
HGDIOBJ old_bitmap_handle = SelectObject(memory_dc, bitmap_handle);
BitBlt(device_context, paint_info.rcPaint.left, paint_info.rcPaint.top, width, height, memory_dc, 0, 0, SRCCOPY);
SelectObject(device_context, old_bitmap_handle);
DeleteObject(bitmap_handle);
DeleteObject(memory_dc);

EndPaint(form->window_handle_, &paint_info);
const size_t i = ((y * texture_width) + x) * 4; // 4 channels

if (distance_from_pixel_to_circle_center < radius) {
image_data[i+0] = 0xff; // blue
image_data[i+1] = 0xff; // green
image_data[i+2] = 0xff; // red
} else {
image_data[i+0] = 0; // blue
image_data[i+1] = 0; // green
image_data[i+2] = 0; // red
}
image_data[i+3] = 0; // unused
}
return 0;
}

return DefWindowProc(form->window_handle_, message, wparam, lparam);
auto circle_texture_creation_result = maxGUI::CreateTextureForForm(*form, texture_width, texture_height, std::span(image_data.data(), texture_height * texture_width));
if (circle_texture_creation_result.has_value()) {
circle_texture_ = *std::move(circle_texture_creation_result);
}
}


void OnClosed(maxGUI::FormConcept* /*form*/) noexcept {
maxGUI::PostExitMessage(0);
}


std::optional<maxGUI::Texture> black_texture_;
std::optional<maxGUI::Texture> circle_texture_;
};


Expand Down
2 changes: 1 addition & 1 deletion Code/maxGUI/FormContainer.inl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace maxGUI {
bool FormContainer::CreateForm(int width, int height, std::string title, FormAllocatorConcept* form_allocator, FormStyles styles) noexcept {
WNDCLASSEX wcx = {0};
wcx.cbSize = sizeof(wcx);
wcx.style = 0;
wcx.style = 0; // TODO: For OpenGL rendering, this *MUST* include CS_OWNDC
wcx.lpfnWndProc = BaseWindowProcedure;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
Expand Down
85 changes: 85 additions & 0 deletions Code/maxGUI/PaintingContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2026, The maxGUI Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "PaintingContext.hpp"


#include <utility>

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>

namespace maxGUI
{

#if defined(MAX_PLATFORM_WINDOWS)
PaintingContext::PaintingContext(HWND window_handle, PAINTSTRUCT paint_info, HDC window_device_context, HDC memory_device_context) noexcept
: window_handle_( std::move(window_handle))
, paint_info_( std::move(paint_info))
, window_device_context_(std::move(window_device_context))
, memory_device_context_(std::move(memory_device_context))
{}
#endif

PaintingContext::PaintingContext(PaintingContext&& rhs) noexcept
: window_handle_( std::move(rhs.window_handle_))
, paint_info_( std::move(rhs.paint_info_))
, window_device_context_(std::move(rhs.window_device_context_))
, memory_device_context_(std::move(rhs.memory_device_context_))
{
rhs.window_handle_ = NULL;
rhs.window_device_context_ = NULL;
rhs.memory_device_context_ = NULL;
}

PaintingContext::~PaintingContext() noexcept {
if (memory_device_context_ != NULL) {
auto result = DeleteDC(memory_device_context_);
if (result == 0) {
// There was an error.
// But 1.) we're in a destructor, and 2.) there isn't much we can do about it other than debug or signal.
}
}
if (window_handle_ != NULL) {
EndPaint(window_handle_, &paint_info_);
}
}

PaintingContext& PaintingContext::operator =(PaintingContext&& rhs) noexcept {
window_handle_ = std::move(rhs.window_handle_);
paint_info_ = std::move(rhs.paint_info_);
window_device_context_ = std::move(rhs.window_device_context_);
memory_device_context_ = std::move(rhs.memory_device_context_);

rhs.window_handle_ = NULL;
rhs.window_device_context_ = NULL;
rhs.memory_device_context_ = NULL;

return *this;
}

max::Containers::Rectangle<int, int> PaintingContext::GetDirtyRectangle() noexcept {
return max::Containers::MakeRectangle<int, int>(paint_info_.rcPaint.left, paint_info_.rcPaint.top, paint_info_.rcPaint.right - paint_info_.rcPaint.left, paint_info_.rcPaint.bottom - paint_info_.rcPaint.top);
}

std::expected<PaintingContext, CreatePaintingContextError> CreatePaintingContext(FormConcept& form) {
PAINTSTRUCT paint_info;
auto window_device_context = BeginPaint(form.window_handle_, &paint_info);

auto memory_device_context = CreateCompatibleDC(window_device_context);

return PaintingContext{form.window_handle_, std::move(paint_info), std::move(window_device_context), std::move(memory_device_context)};
}

void PaintTexture(PaintingContext& painting_context, Texture& texture, int x, int y) noexcept {
auto old_bitmap = SelectObject(painting_context.memory_device_context_, texture.bitmap_handle_);

BitBlt(painting_context.window_device_context_, x, y, texture.width_, texture.height_, painting_context.memory_device_context_, 0, 0, SRCCOPY);

SelectObject(painting_context.memory_device_context_, old_bitmap);
}

} // namespace maxGUI
61 changes: 61 additions & 0 deletions Code/maxGUI/PaintingContext.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2026, The maxGUI Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MAXGUI_PAINTINGCONTEXT_HPP
#define MAXGUI_PAINTINGCONTEXT_HPP


#include <expected>

#include <max/Compiling/Configuration.hpp>

#if defined(MAX_PLATFORM_WINDOWS)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <Windows.h>
#endif

#include <max/Containers/Rectangle.hpp>

#include <maxGUI/FormConcept.hpp>
#include <maxGUI/Texture.hpp>


namespace maxGUI
{

class PaintingContext
{
public:

#if defined(MAX_PLATFORM_WINDOWS)
explicit PaintingContext(HWND window_handle, PAINTSTRUCT paint_info, HDC window_device_context, HDC memory_device_context) noexcept;
#endif

PaintingContext(PaintingContext&& rhs) noexcept;
~PaintingContext() noexcept;

PaintingContext& operator =(PaintingContext&& rhs) noexcept;

max::Containers::Rectangle<int, int> GetDirtyRectangle() noexcept;

#if defined(MAX_PLATFORM_WINDOWS)
HWND window_handle_;
PAINTSTRUCT paint_info_;
HDC window_device_context_;
HDC memory_device_context_;
#endif
};

enum class CreatePaintingContextError {
};
std::expected<PaintingContext, CreatePaintingContextError> CreatePaintingContext(FormConcept& form);

void PaintTexture(PaintingContext& painting_context, Texture& texture, int x, int y) noexcept;

} // namespace maxGUI

#endif // #ifndef MAXGUI_PAINTINGCONTEXT_HPP
Loading
Loading