SDLPainter 1.4.0
SDL3 + OpenGL/Vulkan 2D çizim kütüphanesi
Yüklüyor...
Arıyor...
Eşleşme Yok
painter.h
1#pragma once
2
3#include "sdl_painter/brush.h"
4#include "sdl_painter/color.h"
5#include "sdl_painter/export.h"
6#include "sdl_painter/font.h"
7#include "sdl_painter/frame_stats.h"
8#include "sdl_painter/geometry.h"
9// ImageFlip için — Image'ın kendisi ileri bildirimle yetiyordu, ancak
10// DrawImage'ın varsayılan argümanı enum'un tam tanımını gerektiriyor.
11#include "sdl_painter/image.h"
12#include "sdl_painter/path.h"
13#include "sdl_painter/pen.h"
14#include "sdl_painter/render_target.h"
15#include "sdl_painter/renderer.h"
16#include "sdl_painter/vertex.h"
17
18#include <cstddef>
19#include <cstdint>
20#include <glm/glm.hpp>
21#include <memory>
22#include <string>
23#include <vector>
24
25struct SDL_Window;
26
27// Windows.h DrawText makrosu (DrawTextA/DrawTextW) ile ad çakışmasını önle.
28#ifdef DrawText
29#undef DrawText
30#endif
31
32namespace sdl_painter {
33
34class IRenderer;
35class RenderBatcher;
36
39 glm::mat3 transform{1.0F};
40 Pen pen;
41 Brush brush;
42 float opacity{1.0F};
43 BlendMode blend_mode{BlendMode::kAlpha};
44 Rect clip_rect;
45 bool has_clip{false};
46};
47
63class SDLPAINTER_API Painter {
64 public:
66 Painter(SDL_Window* window, RendererBackend backend);
67
81 Painter(std::unique_ptr<IRenderer> renderer, int32_t viewport_width,
82 int32_t viewport_height);
83
84 ~Painter();
85
86 // Non-copyable, movable
87 Painter(const Painter&) = delete;
88 Painter& operator=(const Painter&) = delete;
89 Painter(Painter&& other) noexcept;
90 Painter& operator=(Painter&& other) noexcept;
91
93 [[nodiscard]] bool IsValid() const noexcept { return mRenderer != nullptr; }
94
96 void Begin();
97
99 void End();
100
111 void SetDrawableSize(int32_t width, int32_t height);
112
132 void SetViewport(int32_t x, int32_t y, int32_t width, int32_t height);
133
136
147 [[nodiscard]] RenderTarget CreateRenderTarget(
148 int32_t width, int32_t height,
149 TextureFilter filter = TextureFilter::kLinear);
150
163 bool SetRenderTarget(const RenderTarget& target);
164
167
176 void DrawRenderTarget(const RenderTarget& target, float x, float y,
177 const Color& tint = Color::White(),
178 ImageFlip flip = ImageFlip::kNone);
179
181 void DrawRenderTarget(const RenderTarget& target, const Rect& dest_rect,
182 const Color& tint = Color::White(),
183 ImageFlip flip = ImageFlip::kNone);
184
202 bool ReadRenderTarget(const RenderTarget& target,
203 std::vector<uint8_t>& out_rgba);
204
217 void Clear(const Color& color);
218
226 [[nodiscard]] const FrameStats& GetFrameStats() const noexcept {
227 return mLastStats;
228 }
229
231 void SetPen(const Pen& pen);
232
234 void SetBrush(const Brush& brush);
235
237 void SetFont(std::shared_ptr<Font> font);
238
244 [[nodiscard]] const std::shared_ptr<Font>& GetFont() const noexcept {
245 return mCurrentFont;
246 }
247
250 void SetOpacity(float alpha);
251
261 void SetBlendMode(BlendMode mode);
262
264 [[nodiscard]] BlendMode GetBlendMode() const noexcept {
265 return mCurrentState.blend_mode;
266 }
267
269 void DrawLine(float x1, float y1, float x2, float y2);
270
272 void DrawRect(float x, float y, float w, float h);
273
275 void FillRect(float x, float y, float w, float h);
276
286 void DrawRoundedRect(float x, float y, float w, float h, float radius);
287
291 void FillRoundedRect(float x, float y, float w, float h, float radius);
292
294 void DrawCircle(float cx, float cy, float radius);
295
297 void FillCircle(float cx, float cy, float radius);
298
300 void DrawEllipse(float cx, float cy, float rx, float ry);
301
303 void FillEllipse(float cx, float cy, float rx, float ry);
304
313 void DrawArc(float cx, float cy, float rx, float ry, float start_degrees,
314 float sweep_degrees);
315
317 void DrawPie(float cx, float cy, float rx, float ry, float start_degrees,
318 float sweep_degrees);
319
321 void FillPie(float cx, float cy, float rx, float ry, float start_degrees,
322 float sweep_degrees);
323
325 void DrawChord(float cx, float cy, float rx, float ry, float start_degrees,
326 float sweep_degrees);
327
329 void FillChord(float cx, float cy, float rx, float ry, float start_degrees,
330 float sweep_degrees);
331
333 void DrawPolygon(const std::vector<Point>& points);
334
336 void FillPolygon(const std::vector<Point>& points);
337
339 void DrawPolyline(const std::vector<Point>& points);
340
351 void DrawPath(const Path& path);
352
362 void FillPath(const Path& path);
363
369 void DrawImage(const Image& image, float x, float y,
370 const Color& tint = Color::White(),
371 ImageFlip flip = ImageFlip::kNone);
372
374 void DrawImage(const Image& image, const Rect& dest_rect,
375 const Color& tint = Color::White(),
376 ImageFlip flip = ImageFlip::kNone);
377
395 void DrawImageMesh(const Image& image, int32_t cols, int32_t rows,
396 const std::vector<Point>& points,
397 const Color& tint = Color::White());
398
417 void UpdateImage(const Image& image, const uint8_t* rgba);
418
423 void DrawImage(const Image& image, const Rect& src_rect,
424 const Rect& dest_rect, const Color& tint = Color::White(),
425 ImageFlip flip = ImageFlip::kNone);
426
432 void DrawText(float x, float y, const std::string& text);
433
444 void DrawText(const Rect& rect, const std::string& text,
445 Alignment alignment = Alignment::kLeft,
446 TextWrap wrap = TextWrap::kNone);
447
454 [[nodiscard]] std::size_t CountTextLines(const std::string& text,
455 float max_width,
456 TextWrap wrap) const;
457
463 void Save();
464
466 void Restore();
467
469 void Translate(float dx, float dy);
470
472 void Rotate(float angle_degrees);
473
475 void Scale(float sx, float sy);
476
479
488 void SetClipRect(const Rect& rect);
489
491 void ClearClip();
492
493 private:
494 friend class RenderTarget;
495
501 void OnRenderTargetDestroyed(RenderTargetHandle handle);
502
508 void QueryDrawableSize(int32_t& out_width, int32_t& out_height) const;
509
511 void ApplyDrawableSize(int32_t width, int32_t height);
512
514 void UpdateProjection();
515
526 [[nodiscard]] bool YAxisMatchesGpu() const;
527
529 [[nodiscard]] int32_t SurfaceWidth() const;
530
532 [[nodiscard]] int32_t SurfaceHeight() const;
533
539 [[nodiscard]] bool OwnsTarget(const RenderTarget& target) const;
540
542 void ApplyViewport();
543
548 void PushTexturedQuad(TextureHandle texture, float u0, float v0, float u1,
549 float v1, const Rect& dest_rect, const Color& tint,
550 ImageFlip flip);
551
554 void PushStroke(const std::vector<Vertex>& verts, const Color& color);
555
560 void PushFilled(std::vector<Vertex>& verts);
561
566 void DrawTextLine(float x, float y, const std::string& text);
567
569 [[nodiscard]] float AlignedX(const Rect& rect, const std::string& line,
570 Alignment alignment) const;
571
575 void ApplyScissor(const Rect& rect);
576
578 void ClearScissorCounted();
579
580 [[nodiscard]] bool CanDrawPen() const noexcept {
581 return mRenderer && mBatcher && mCurrentState.pen.IsVisible();
582 }
583 [[nodiscard]] bool CanDrawBrush() const noexcept {
584 return mRenderer && mBatcher && mCurrentState.brush.IsVisible();
585 }
586
587 SDL_Window* mWindow{nullptr};
588
589 std::unique_ptr<IRenderer> mRenderer;
590 std::unique_ptr<RenderBatcher> mBatcher;
591 std::vector<RenderState> mStateStack;
592 RenderState mCurrentState;
593 std::shared_ptr<Font> mCurrentFont;
594
596 FrameStats mStats;
597 FrameStats mLastStats;
598 uint64_t mFrameStartNs{0};
599
601 int32_t mDrawableWidth{0};
602 int32_t mDrawableHeight{0};
603
607 int32_t mViewportX{0};
608 int32_t mViewportY{0};
609 int32_t mViewportWidth{0};
610 int32_t mViewportHeight{0};
611
614 bool mCustomViewport{false};
615
617 RenderTargetHandle mActiveTarget{kInvalidRenderTarget};
618 int32_t mTargetWidth{0};
619 int32_t mTargetHeight{0};
620
622 int32_t mSavedViewportX{0};
623 int32_t mSavedViewportY{0};
624 int32_t mSavedViewportWidth{0};
625 int32_t mSavedViewportHeight{0};
626 bool mSavedCustomViewport{false};
627
630 bool mAutoDrawableSize{true};
631
638 std::shared_ptr<Painter*> mSelf;
639};
640
641} // namespace sdl_painter
Dolgu stili — düz renk veya gradient.
Definition brush.h:36
Backend-agnostik soyut renderer arayüzü.
Definition renderer.h:77
Yüklenmiş görüntü — texture sarmalayıcı.
Definition image.h:38
void DrawPolygon(const std::vector< Point > &points)
Çok kenarlı kapalı şeklin çerçevesini çiz.
void ClearClip()
Kırpma dikdörtgenini kaldır.
void DrawChord(float cx, float cy, float rx, float ry, float start_degrees, float sweep_degrees)
Kiriş (chord) çerçevesi — yay artı uçlarını birleştiren doğru.
void FillPie(float cx, float cy, float rx, float ry, float start_degrees, float sweep_degrees)
Dilimi doldur. Çizelgelerin (pasta grafik) temel yapı taşı.
void SetBrush(const Brush &brush)
Aktif fırçayı ayarla (dolgu rengi).
void Translate(float dx, float dy)
Öteleme uygula.
void Begin()
Frame başlangıcı — her frame başında çağrılmalı.
Painter(std::unique_ptr< IRenderer > renderer, int32_t viewport_width, int32_t viewport_height)
Hazır bir renderer ile pencere olmadan Painter oluştur.
void End()
Frame sonu — her frame sonunda çağrılmalı, ekrana sunar.
void DrawImage(const Image &image, const Rect &src_rect, const Rect &dest_rect, const Color &tint=Color::White(), ImageFlip flip=ImageFlip::kNone)
Görüntünün kaynak dikdörtgenini hedef dikdörtgene çiz.
void UpdateImage(const Image &image, const uint8_t *rgba)
Yüklenmiş bir görüntünün doku içeriğini yerinde güncelle.
void DrawPolyline(const std::vector< Point > &points)
Açık çizgi dizisi çiz.
void DrawEllipse(float cx, float cy, float rx, float ry)
Elips çerçeve çiz.
const std::shared_ptr< Font > & GetFont() const noexcept
Aktif font (yoksa nullptr).
Definition painter.h:244
void FillPath(const Path &path)
Yolun içini doldur (ear clipping tessellation).
void Restore()
Son kaydedilen durumu geri yükle.
void Save()
Mevcut render durumunu stack'e kaydet.
void FillRoundedRect(float x, float y, float w, float h, float radius)
Yuvarlatılmış köşeli dikdörtgeni doldur.
void SetOpacity(float alpha)
Global opaklığı ayarla.
const FrameStats & GetFrameStats() const noexcept
Son tamamlanan karenin çizim istatistikleri.
Definition painter.h:226
void DrawImage(const Image &image, const Rect &dest_rect, const Color &tint=Color::White(), ImageFlip flip=ImageFlip::kNone)
Görüntüyü hedef dikdörtgene ölçekleyerek çiz.
void DrawCircle(float cx, float cy, float radius)
Daire çerçeve çiz.
void ResetTransform()
Transform'u birim matrise sıfırla.
void Clear(const Color &color)
Çizim yüzeyini belirtilen renkle temizle.
std::size_t CountTextLines(const std::string &text, float max_width, TextWrap wrap) const
Sarmalanmış metnin kaç satır tutacağını hesapla (çizmeden).
void DrawImage(const Image &image, float x, float y, const Color &tint=Color::White(), ImageFlip flip=ImageFlip::kNone)
Görüntüyü orijinal boyutuyla çiz.
void SetViewport(int32_t x, int32_t y, int32_t width, int32_t height)
Çizimi pencerenin bir alt dikdörtgeniyle sınırla.
void DrawRoundedRect(float x, float y, float w, float h, float radius)
Yuvarlatılmış köşeli dikdörtgen çerçevesi çiz.
BlendMode GetBlendMode() const noexcept
Yürürlükteki karıştırma modu.
Definition painter.h:264
void ResetRenderTarget()
Çizimi ekrana geri al.
void DrawArc(float cx, float cy, float rx, float ry, float start_degrees, float sweep_degrees)
Yay çiz (açık — uçları birleştirilmez).
Painter(SDL_Window *window, RendererBackend backend)
Belirtilen pencere ve backend ile Painter oluştur.
void SetClipRect(const Rect &rect)
Scissor kırpma dikdörtgeni ayarla.
void DrawText(const Rect &rect, const std::string &text, Alignment alignment=Alignment::kLeft, TextWrap wrap=TextWrap::kNone)
Dikdörtgen içine hizalanmış metin çiz.
void FillPolygon(const std::vector< Point > &points)
Çok kenarlıyı doldur (ear clipping tessellation).
void ResetViewport()
Viewport'u tüm çizim yüzeyine geri al.
void DrawRenderTarget(const RenderTarget &target, float x, float y, const Color &tint=Color::White(), ImageFlip flip=ImageFlip::kNone)
Hedefin içeriğini orijinal boyutuyla çiz.
bool ReadRenderTarget(const RenderTarget &target, std::vector< uint8_t > &out_rgba)
Hedefin piksellerini ana belleğe oku.
void DrawImageMesh(const Image &image, int32_t cols, int32_t rows, const std::vector< Point > &points, const Color &tint=Color::White())
Dokuyu serbest biçimli bir ızgara üzerine çiz.
void SetPen(const Pen &pen)
Aktif kalemi ayarla (çizgi rengi ve kalınlığı).
void Scale(float sx, float sy)
Ölçekleme uygula.
void DrawPie(float cx, float cy, float rx, float ry, float start_degrees, float sweep_degrees)
Dilim (pie) çerçevesi — yay artı merkeze giden iki yarıçap.
void DrawLine(float x1, float y1, float x2, float y2)
İki nokta arasına çizgi çiz.
void DrawPath(const Path &path)
Yolun çerçevesini çiz.
void FillCircle(float cx, float cy, float radius)
Daireyi doldur.
bool IsValid() const noexcept
Renderer başarıyla başlatıldı mı?
Definition painter.h:93
void FillRect(float x, float y, float w, float h)
Dikdörtgeni doldur.
bool SetRenderTarget(const RenderTarget &target)
Sonraki çizimleri verilen hedefe yönlendir.
void DrawRenderTarget(const RenderTarget &target, const Rect &dest_rect, const Color &tint=Color::White(), ImageFlip flip=ImageFlip::kNone)
Hedefin içeriğini hedef dikdörtgene ölçekleyerek çiz.
RenderTarget CreateRenderTarget(int32_t width, int32_t height, TextureFilter filter=TextureFilter::kLinear)
Ekran yerine çizilebilen offscreen bir yüzey oluştur.
void SetBlendMode(BlendMode mode)
Renk karıştırma modunu ayarla.
void FillEllipse(float cx, float cy, float rx, float ry)
Elipsi doldur.
void DrawText(float x, float y, const std::string &text)
Noktaya metin çiz.
void FillChord(float cx, float cy, float rx, float ry, float start_degrees, float sweep_degrees)
Kirişi doldur.
void DrawRect(float x, float y, float w, float h)
Dikdörtgen çerçeve çiz (dolgu yok).
void SetDrawableSize(int32_t width, int32_t height)
Çizim yüzeyi boyutunu bildir (viewport + projeksiyon güncellenir).
void SetFont(std::shared_ptr< Font > font)
Aktif fontu ayarla.
void Rotate(float angle_degrees)
Döndürme uygula (derece).
Doğru parçaları ve Bézier eğrilerinden oluşan çizim yolu.
Definition path.h:66
Çizgi stili — renk, kalınlık ve isteğe bağlı dış kontur.
Definition pen.h:49
RGBA renk temsili. Her kanal [0, 255] aralığında.
Definition color.h:8
Bir karenin çizim maliyeti — profilleme ve ekran üstü gösterim için.
Definition frame_stats.h:16
2B dikdörtgen — sol üst köşe + genişlik/yükseklik.
Definition geometry.h:34
Aktif render durumu — transform stack elemanı.
Definition painter.h:38
glm::mat3 transform
3x3 affine dönüşüm (column-major, birim).
Definition painter.h:39