SDLPainter 1.2.0
SDL3 + OpenGL/Vulkan 2B ç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
25// SDL forward declaration
26struct SDL_Window;
27
28// Windows.h DrawText makrosu (DrawTextA/DrawTextW) ile ad çakışmasını önle.
29#ifdef DrawText
30#undef DrawText
31#endif
32
33namespace sdl_painter {
34
35class IRenderer;
36class RenderBatcher;
37
40 glm::mat3 transform{1.0F};
41 Pen pen;
42 Brush brush;
43 float opacity{1.0F};
44 BlendMode blend_mode{BlendMode::kAlpha};
45 Rect clip_rect;
46 bool has_clip{false};
47};
48
60class SDLPAINTER_API Painter {
61 public:
63 Painter(SDL_Window* window, RendererBackend backend);
64
78 Painter(std::unique_ptr<IRenderer> renderer, int32_t viewport_width,
79 int32_t viewport_height);
80
81 ~Painter();
82
83 // Non-copyable, movable
84 Painter(const Painter&) = delete;
85 Painter& operator=(const Painter&) = delete;
86 Painter(Painter&& other) noexcept;
87 Painter& operator=(Painter&& other) noexcept;
88
90 [[nodiscard]] bool IsValid() const noexcept { return mRenderer != nullptr; }
91
92 // --- Yaşam döngüsü ---
93
95 void Begin();
96
98 void End();
99
110 void SetDrawableSize(int32_t width, int32_t height);
111
112 // --- Görüntü alanı (viewport) ---
113
133 void SetViewport(int32_t x, int32_t y, int32_t width, int32_t height);
134
137
138 // --- Çizim hedefi (dokuya çizim) ---
139
151 int32_t width, int32_t height,
152 TextureFilter filter = TextureFilter::kLinear);
153
166 bool SetRenderTarget(const RenderTarget& target);
167
170
174 void DrawRenderTarget(const RenderTarget& target, float x, float y,
175 const Color& tint = Color::White(),
176 ImageFlip flip = ImageFlip::kNone);
177
179 void DrawRenderTarget(const RenderTarget& target, const Rect& dest_rect,
180 const Color& tint = Color::White(),
181 ImageFlip flip = ImageFlip::kNone);
182
196 bool ReadRenderTarget(const RenderTarget& target,
197 std::vector<uint8_t>& out_rgba);
198
199 // --- Temizlik ---
200
202 void Clear(const Color& color);
203
204 // --- Profilleme ---
205
213 [[nodiscard]] const FrameStats& GetFrameStats() const noexcept {
214 return mLastStats;
215 }
216
217 // --- Stil ---
218
220 void SetPen(const Pen& pen);
221
223 void SetBrush(const Brush& brush);
224
226 void SetFont(std::shared_ptr<Font> font);
227
233 [[nodiscard]] const std::shared_ptr<Font>& GetFont() const noexcept {
234 return mCurrentFont;
235 }
236
238 void SetOpacity(float alpha);
239
249 void SetBlendMode(BlendMode mode);
250
252 [[nodiscard]] BlendMode GetBlendMode() const noexcept {
253 return mCurrentState.blend_mode;
254 }
255
256 // --- Primitifler ---
257
259 void DrawLine(float x1, float y1, float x2, float y2);
260
262 void DrawRect(float x, float y, float w, float h);
263
265 void FillRect(float x, float y, float w, float h);
266
276 void DrawRoundedRect(float x, float y, float w, float h, float radius);
277
281 void FillRoundedRect(float x, float y, float w, float h, float radius);
282
284 void DrawCircle(float cx, float cy, float radius);
285
287 void FillCircle(float cx, float cy, float radius);
288
290 void DrawEllipse(float cx, float cy, float rx, float ry);
291
293 void FillEllipse(float cx, float cy, float rx, float ry);
294
303 void DrawArc(float cx, float cy, float rx, float ry, float start_degrees,
304 float sweep_degrees);
305
307 void DrawPie(float cx, float cy, float rx, float ry, float start_degrees,
308 float sweep_degrees);
309
311 void FillPie(float cx, float cy, float rx, float ry, float start_degrees,
312 float sweep_degrees);
313
315 void DrawChord(float cx, float cy, float rx, float ry, float start_degrees,
316 float sweep_degrees);
317
319 void FillChord(float cx, float cy, float rx, float ry, float start_degrees,
320 float sweep_degrees);
321
323 void DrawPolygon(const std::vector<Point>& points);
324
326 void FillPolygon(const std::vector<Point>& points);
327
329 void DrawPolyline(const std::vector<Point>& points);
330
331 // --- Yol (path) ---
332
343 void DrawPath(const Path& path);
344
354 void FillPath(const Path& path);
355
356 // --- Görüntü ---
357
363 void DrawImage(const Image& image, float x, float y,
364 const Color& tint = Color::White(),
365 ImageFlip flip = ImageFlip::kNone);
366
368 void DrawImage(const Image& image, const Rect& dest_rect,
369 const Color& tint = Color::White(),
370 ImageFlip flip = ImageFlip::kNone);
371
389 void DrawImageMesh(const Image& image, int32_t cols, int32_t rows,
390 const std::vector<Point>& points,
391 const Color& tint = Color::White());
392
411 void UpdateImage(const Image& image, const uint8_t* rgba);
412
417 void DrawImage(const Image& image, const Rect& src_rect,
418 const Rect& dest_rect, const Color& tint = Color::White(),
419 ImageFlip flip = ImageFlip::kNone);
420
421 // --- Metin
422
428 void DrawText(float x, float y, const std::string& text);
429
440 void DrawText(const Rect& rect, const std::string& text,
441 Alignment alignment = Alignment::kLeft,
442 TextWrap wrap = TextWrap::kNone);
443
450 [[nodiscard]] std::size_t CountTextLines(const std::string& text,
451 float max_width,
452 TextWrap wrap) const;
453
454 // --- Transform stack ---
455
457 void Save();
458
460 void Restore();
461
463 void Translate(float dx, float dy);
464
466 void Rotate(float angle_degrees);
467
469 void Scale(float sx, float sy);
470
473
474 // --- Kırpma ---
475
477 void SetClipRect(const Rect& rect);
478
480 void ClearClip();
481
482 private:
488 void QueryDrawableSize(int32_t& out_width, int32_t& out_height) const;
489
491 void ApplyDrawableSize(int32_t width, int32_t height);
492
494 void UpdateProjection();
495
506 [[nodiscard]] bool YAxisMatchesGpu() const;
507
509 [[nodiscard]] int32_t SurfaceHeight() const;
510
512 void ApplyViewport();
513
518 void PushTexturedQuad(TextureHandle texture, float u0, float v0, float u1,
519 float v1, const Rect& dest_rect, const Color& tint,
520 ImageFlip flip);
521
524 void PushStroke(const std::vector<Vertex>& verts, const Color& color);
525
530 void PushFilled(std::vector<Vertex>& verts);
531
536 void DrawTextLine(float x, float y, const std::string& text);
537
539 [[nodiscard]] float AlignedX(const Rect& rect, const std::string& line,
540 Alignment alignment) const;
541
545 void ApplyScissor(const Rect& rect);
546
548 void ClearScissorCounted();
549
550 [[nodiscard]] bool CanDrawPen() const noexcept {
551 return mRenderer && mBatcher && mCurrentState.pen.IsVisible();
552 }
553 [[nodiscard]] bool CanDrawBrush() const noexcept {
554 return mRenderer && mBatcher && mCurrentState.brush.IsVisible();
555 }
556
557 SDL_Window* mWindow{nullptr};
558
559 std::unique_ptr<IRenderer> mRenderer;
560 std::unique_ptr<RenderBatcher> mBatcher;
561 std::vector<RenderState> mStateStack;
562 RenderState mCurrentState;
563 std::shared_ptr<Font> mCurrentFont;
564
566 FrameStats mStats;
567 FrameStats mLastStats;
568 uint64_t mFrameStartNs{0};
569
571 int32_t mDrawableWidth{0};
572 int32_t mDrawableHeight{0};
573
577 int32_t mViewportX{0};
578 int32_t mViewportY{0};
579 int32_t mViewportWidth{0};
580 int32_t mViewportHeight{0};
581
584 bool mCustomViewport{false};
585
587 RenderTargetHandle mActiveTarget{kInvalidRenderTarget};
588 int32_t mTargetWidth{0};
589 int32_t mTargetHeight{0};
590
592 int32_t mSavedViewportX{0};
593 int32_t mSavedViewportY{0};
594 int32_t mSavedViewportWidth{0};
595 int32_t mSavedViewportHeight{0};
596 bool mSavedCustomViewport{false};
597
600 bool mAutoDrawableSize{true};
601};
602
603} // namespace sdl_painter
Dolgu stili — düz renk veya gradient.
Definition brush.h:34
Yüklenmiş görüntü — texture sarmalayıcı.
Definition image.h:39
Ana çizim sınıfı
Definition painter.h:60
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:233
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 [0.0, 1.0].
const FrameStats & GetFrameStats() const noexcept
Son tamamlanan karenin çizim istatistikleri.
Definition painter.h:213
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)
Ekranı 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:252
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:90
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
Ekran yerine çizilebilen offscreen bir yüzey (dokuya çizim).
Definition render_target.h:51
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:39
glm::mat3 transform
3x3 affine dönüşüm (column-major, birim).
Definition painter.h:40