SDLPainter 1.2.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/pen.h"
13#include "sdl_painter/renderer.h"
14#include "sdl_painter/vertex.h"
15
16#include <cstddef>
17#include <cstdint>
18#include <glm/glm.hpp>
19#include <memory>
20#include <string>
21#include <vector>
22
23// SDL forward declaration
24struct SDL_Window;
25
26// Windows.h DrawText makrosu (DrawTextA/DrawTextW) ile ad çakışmasını önle.
27#ifdef DrawText
28#undef DrawText
29#endif
30
31namespace sdl_painter {
32
33class IRenderer;
34class RenderBatcher;
35
38 glm::mat3 transform{1.0F};
39 Pen pen;
40 Brush brush;
41 float opacity{1.0F};
42 BlendMode blend_mode{BlendMode::kAlpha};
43 Rect clip_rect;
44 bool has_clip{false};
45};
46
58class SDLPAINTER_API Painter {
59 public:
61 Painter(SDL_Window* window, RendererBackend backend);
62
76 Painter(std::unique_ptr<IRenderer> renderer, int32_t viewport_width,
77 int32_t viewport_height);
78
79 ~Painter();
80
81 // Non-copyable, movable
82 Painter(const Painter&) = delete;
83 Painter& operator=(const Painter&) = delete;
84 Painter(Painter&& other) noexcept;
85 Painter& operator=(Painter&& other) noexcept;
86
88 [[nodiscard]] bool IsValid() const noexcept { return mRenderer != nullptr; }
89
90 // --- Yaşam döngüsü ---
91
93 void Begin();
94
96 void End();
97
108 void SetDrawableSize(int32_t width, int32_t height);
109
110 // --- Görüntü alanı (viewport) ---
111
131 void SetViewport(int32_t x, int32_t y, int32_t width, int32_t height);
132
135
136 // --- Temizlik ---
137
139 void Clear(const Color& color);
140
141 // --- Profilleme ---
142
150 [[nodiscard]] const FrameStats& GetFrameStats() const noexcept {
151 return mLastStats;
152 }
153
154 // --- Stil ---
155
157 void SetPen(const Pen& pen);
158
160 void SetBrush(const Brush& brush);
161
163 void SetFont(std::shared_ptr<Font> font);
164
170 [[nodiscard]] const std::shared_ptr<Font>& GetFont() const noexcept {
171 return mCurrentFont;
172 }
173
175 void SetOpacity(float alpha);
176
186 void SetBlendMode(BlendMode mode);
187
189 [[nodiscard]] BlendMode GetBlendMode() const noexcept {
190 return mCurrentState.blend_mode;
191 }
192
193 // --- Primitifler ---
194
196 void DrawLine(float x1, float y1, float x2, float y2);
197
199 void DrawRect(float x, float y, float w, float h);
200
202 void FillRect(float x, float y, float w, float h);
203
213 void DrawRoundedRect(float x, float y, float w, float h, float radius);
214
218 void FillRoundedRect(float x, float y, float w, float h, float radius);
219
221 void DrawCircle(float cx, float cy, float radius);
222
224 void FillCircle(float cx, float cy, float radius);
225
227 void DrawEllipse(float cx, float cy, float rx, float ry);
228
230 void FillEllipse(float cx, float cy, float rx, float ry);
231
240 void DrawArc(float cx, float cy, float rx, float ry, float start_degrees,
241 float sweep_degrees);
242
244 void DrawPie(float cx, float cy, float rx, float ry, float start_degrees,
245 float sweep_degrees);
246
248 void FillPie(float cx, float cy, float rx, float ry, float start_degrees,
249 float sweep_degrees);
250
252 void DrawChord(float cx, float cy, float rx, float ry, float start_degrees,
253 float sweep_degrees);
254
256 void FillChord(float cx, float cy, float rx, float ry, float start_degrees,
257 float sweep_degrees);
258
260 void DrawPolygon(const std::vector<Point>& points);
261
263 void FillPolygon(const std::vector<Point>& points);
264
266 void DrawPolyline(const std::vector<Point>& points);
267
268 // --- Görüntü ---
269
275 void DrawImage(const Image& image, float x, float y,
276 const Color& tint = Color::White(),
277 ImageFlip flip = ImageFlip::kNone);
278
280 void DrawImage(const Image& image, const Rect& dest_rect,
281 const Color& tint = Color::White(),
282 ImageFlip flip = ImageFlip::kNone);
283
301 void DrawImageMesh(const Image& image, int32_t cols, int32_t rows,
302 const std::vector<Point>& points,
303 const Color& tint = Color::White());
304
323 void UpdateImage(const Image& image, const uint8_t* rgba);
324
329 void DrawImage(const Image& image, const Rect& src_rect,
330 const Rect& dest_rect, const Color& tint = Color::White(),
331 ImageFlip flip = ImageFlip::kNone);
332
333 // --- Metin
334
340 void DrawText(float x, float y, const std::string& text);
341
352 void DrawText(const Rect& rect, const std::string& text,
353 Alignment alignment = Alignment::kLeft,
354 TextWrap wrap = TextWrap::kNone);
355
362 [[nodiscard]] std::size_t CountTextLines(const std::string& text,
363 float max_width,
364 TextWrap wrap) const;
365
366 // --- Transform stack ---
367
369 void Save();
370
372 void Restore();
373
375 void Translate(float dx, float dy);
376
378 void Rotate(float angle_degrees);
379
381 void Scale(float sx, float sy);
382
385
386 // --- Kırpma ---
387
389 void SetClipRect(const Rect& rect);
390
392 void ClearClip();
393
394 private:
400 void QueryDrawableSize(int32_t& out_width, int32_t& out_height) const;
401
403 void ApplyDrawableSize(int32_t width, int32_t height);
404
406 void UpdateProjection();
407
410 void PushStroke(const std::vector<Vertex>& verts, const Color& color);
411
416 void PushFilled(std::vector<Vertex>& verts);
417
422 void DrawTextLine(float x, float y, const std::string& text);
423
425 [[nodiscard]] float AlignedX(const Rect& rect, const std::string& line,
426 Alignment alignment) const;
427
431 void ApplyScissor(const Rect& rect);
432
434 void ClearScissorCounted();
435
436 [[nodiscard]] bool CanDrawPen() const noexcept {
437 return mRenderer && mBatcher && mCurrentState.pen.IsVisible();
438 }
439 [[nodiscard]] bool CanDrawBrush() const noexcept {
440 return mRenderer && mBatcher && mCurrentState.brush.IsVisible();
441 }
442
443 SDL_Window* mWindow{nullptr};
444
445 std::unique_ptr<IRenderer> mRenderer;
446 std::unique_ptr<RenderBatcher> mBatcher;
447 std::vector<RenderState> mStateStack;
448 RenderState mCurrentState;
449 std::shared_ptr<Font> mCurrentFont;
450
452 FrameStats mStats;
453 FrameStats mLastStats;
454 uint64_t mFrameStartNs{0};
455
457 int32_t mDrawableWidth{0};
458 int32_t mDrawableHeight{0};
459
463 int32_t mViewportX{0};
464 int32_t mViewportY{0};
465 int32_t mViewportWidth{0};
466 int32_t mViewportHeight{0};
467
470 bool mCustomViewport{false};
471
474 bool mAutoDrawableSize{true};
475};
476
477} // namespace sdl_painter
Dolgu stili — düz renk veya gradient.
Definition brush.h:34
Backend-agnostik soyut renderer arayüzü.
Definition renderer.h:64
Yüklenmiş görüntü — texture sarmalayıcı.
Definition image.h:39
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:170
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:150
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:189
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 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 FillCircle(float cx, float cy, float radius)
Daireyi doldur.
bool IsValid() const noexcept
Renderer başarıyla başlatıldı mı?
Definition painter.h:88
void FillRect(float x, float y, float w, float h)
Dikdörtgeni doldur.
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).
Ç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:37
glm::mat3 transform
3x3 affine dönüşüm (column-major, birim).
Definition painter.h:38