SDXFrameWork 0.13 SDXFrameWork

- Main Page
- Related Pages
- Classes

All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Renderer.h
1 //Copyright © 2014 SDXFramework
2 //[License]GNU Affero General Public License, version 3
3 //[Contact]http://sourceforge.jp/projects/dxframework/
4 #pragma once
5 #include <Multimedia/SDX.h>
6 #include <Multimedia/Color.h>
7 #include <Multimedia/Renderer.h>
8 #include <Framework/Shape.h>
9 #include <Framework/Camera.h>
10
11 namespace SDX
12 {
14 enum class BlendMode
15 {
16 NoBlend = SDL_BLENDMODE_NONE,
17 Alpha = SDL_BLENDMODE_BLEND,
18 Add = SDL_BLENDMODE_ADD,
19 Mula = SDL_BLENDMODE_MOD,
20 };
21
22 class Image;
23
27 class Renderer
28 {
29 friend class SubWindow;
30 friend class Drawing;
31 private:
32 SDL_Renderer* handle = nullptr;
33 SDL_Surface* surface = nullptr;
34 Image* target = nullptr;
35 bool isWindow;
36
37 /*Windowに対応した、Rendererを生成.*/
38 bool Create(SDL_Window* 元Window)
39 {
40 if (handle != nullptr){ return false ;}
41
42 isWindow = true;
43 //handle = SDL_CreateRenderer(元Window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);
44 handle = SDL_CreateRenderer(元Window, -1, SDL_RENDERER_PRESENTVSYNC);
45 surface = SDL_GetWindowSurface(元Window);
46 return true;
47 }
48 public:
49 static Renderer &mainRenderer;
50
51 BlendMode blendMode = BlendMode::NoBlend;
52 Color clearColor = Color(0, 0, 0);
53 Color rgba = Color(255, 255, 255, 255);
54
55 Renderer(){};
56
57 ~Renderer()
58 {
59 Destroy();
60 }
61
63 Renderer(int 幅, int 高さ)
64 {
65 Create(幅,高さ);
66 }
67
69 SDL_Renderer* GetHandle()
70 {
71 return handle;
72 }
73
76 bool Create(int 幅, int 高さ)
77 {
78 if (handle != nullptr) return false;
79
80 isWindow = false;
81 surface = SDL_CreateRGBSurface(0, 幅, 高さ, 32, 0, 0, 0, 0);
82
83 handle = SDL_CreateSoftwareRenderer(surface);
84 return true;
85 }
86
90 bool Destroy()
91 {
92 if ( isWindow){ return false; }
93 if (handle == 0){ return false; }
94 if (surface != nullptr)
95 {
96 SDL_FreeSurface(surface);
97 }
98
99 SDL_DestroyRenderer(handle);
100
101 surface = nullptr;
102 handle = nullptr;
103
104 return true;
105 }
106
109 bool SetTransColor(const Color &透過色)
110 {
111 if (!surface){ return false; }
112 return !SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 透過色.GetRed(), 透過色.GetBlue(), 透過色.GetGreen()));
113 }
114
117 bool ResetTransColor()
118 {
119 if (!surface){ return false; }
120 return !SDL_SetColorKey(surface, false, 0);
121 }
122
126 bool Draw(const Rect& 領域, Renderer& コピー先 = Renderer::mainRenderer)
127 {
128 if (!surface){ return false; }
129
130 SDL_Texture* image;
131 image = SDL_CreateTextureFromSurface(コピー先.GetHandle(), surface);
132
133 SDL_Rect srcrect = { 0, 0, (int)領域.GetW(), (int)領域.GetH() };
134 SDL_Rect dsrect = { (int)領域.GetX(), (int)領域.GetY(), (int)領域.GetW(), (int)領域.GetH() };
135 SDL_RenderCopy(コピー先.GetHandle(), image, &srcrect, &dsrect);
136 SDL_DestroyTexture(image);
137
138 return true;
139 }
140
144 bool DrawExtend(const Rect& 元範囲, const Rect& コピー先範囲, Renderer& コピー先 = Renderer::mainRenderer)
145 {
146 if (!surface){ return false; }
147
148 SDL_Texture* image = SDL_CreateTextureFromSurface(コピー先.GetHandle(), surface);
149
150 SDL_Rect srcrect = { (int)元範囲.GetX(), (int)元範囲.GetY(), (int)元範囲.GetW(), (int)元範囲.GetH() };
151 SDL_Rect dsrect = { (int)コピー先範囲.GetX(), (int)コピー先範囲.GetY(), (int)コピー先範囲.GetW(), (int)コピー先範囲.GetH() };
152
153 SDL_BlitScaled(surface, &srcrect, コピー先.surface, &dsrect);
154 SDL_RenderCopy(コピー先.GetHandle(), image, &srcrect, &dsrect);
155 SDL_DestroyTexture(image);
156
157 return true;
158 }
159
161 bool Clear()
162 {
163 if (handle == nullptr){ return false; }
164
165 SDL_SetRenderDrawColor
166 (
167 handle,
168 clearColor.GetRed(),
169 clearColor.GetGreen(),
170 clearColor.GetBlue(),
171 clearColor.GetAlpha()
172 );
173 SDL_RenderClear(handle);
174 return true;
175 }
176
180 bool SetTarget(Image *描画対象 = nullptr);
181
184 Image* GetTarget()
185 {
186 return target;
187 }
188
191 bool SetClip(const Rect &描画領域 = {0,0,0,0})
192 {
193 if (handle == nullptr){ return false; }
194
195 if (描画領域.GetW() == 0)
196 {
197 return !SDL_RenderSetClipRect(handle, 0);
198 }
199
200 SDL_Rect rect;
201 if (Camera::Get())
202 {
203 rect = Camera::Get()->TransRect(描画領域);
204 }
205 else
206 {
207 rect = 描画領域;
208 }
209
210 return !SDL_RenderSetClipRect(handle, &rect);
211 }
212
214 void SetBackColor(const Color &背景色)
215 {
216 clearColor.SetColor(背景色.GetRed(), 背景色.GetGreen(), 背景色.GetBlue() );
217 }
218
221 void SetBright(const Color &輝度)
222 {
223 rgba.SetColor(輝度.GetRed(), 輝度.GetGreen(), 輝度.GetBlue());
224 }
225
227 void SetBlendMode(BlendMode ブレンドモード, int α値)
228 {
229 blendMode = ブレンドモード;
230 if (α値 > 255 || ブレンドモード == BlendMode::NoBlend){ α値 = 255; }
231 else if (α値 < 0){ α値 = 0; }
232
233 rgba.SetColor(rgba.GetRed(),rgba.GetGreen(),rgba.GetBlue(),α値);
234 }
235
237 void SetDrawMode(const Color &輝度＋α値 = Color::White, BlendMode ブレンドモード = BlendMode::NoBlend)
238 {
239 SetBright(輝度＋α値);
240 SetBlendMode(ブレンドモード, 輝度＋α値.GetAlpha());
241 }
242 };
243 }
SDX::Rect::GetX
double GetX() const override
X座標を取得.
Definition: Rect.h:97
SDX::Color::GetGreen
int GetGreen() const
緑の要素を取得.
Definition: Color.h:36
SDX::Rect
矩形を表す図形クラス.
Definition: Rect.h:22
SDX::Renderer::rgba
Color rgba
描画輝度とα値
Definition: Renderer.h:53
SDX::Renderer::DrawExtend
bool DrawExtend(const Rect &元範囲, const Rect &コピー先範囲, Renderer &コピー先=Renderer::mainRenderer)
対象RenderHandleにコピー.
Definition: Renderer.h:144
SDX::Rect::GetY
double GetY() const override
Y座標を取得.
Definition: Rect.h:102
SDX::BlendMode::NoBlend
ブレンドしない
SDX::Color::GetBlue
int GetBlue() const
青の要素を取得.
Definition: Color.h:42
SDX::Drawing
リソースを読み込まずに描画を行う関数群.
Definition: Drawing.h:17
SDX::Renderer::clearColor
Color clearColor
消去時の色
Definition: Renderer.h:52
SDX::Camera::TransRect
SDL_Rect TransRect(const SDL_Rect &変換前矩形) const
領域を画面上のどこに相当するか変換.
Definition: Camera.h:102
SDX::Renderer::Renderer
Renderer(int 幅, int 高さ)
コンストラクタ.
Definition: Renderer.h:63
SDX::Renderer::blendMode
BlendMode blendMode
描画モード
Definition: Renderer.h:51
SDX::Renderer::Create
bool Create(int 幅, int 高さ)
Windowと独立した、Rendererを生成.
Definition: Renderer.h:76
SDX::Renderer::Draw
bool Draw(const Rect &領域, Renderer &コピー先=Renderer::mainRenderer)
対象RenderHandleにコピー.
Definition: Renderer.h:126
SDX::Renderer::Destroy
bool Destroy()
Rendererを削除.
Definition: Renderer.h:90
SDX::Renderer::SetClip
bool SetClip(const Rect &描画領域={0, 0, 0, 0})
描画領域を設定する、範囲外には描画されない.
Definition: Renderer.h:191
SDX::Color::SetColor
void SetColor(int 赤, int 緑, int 青, int α値=255)
色の各要素を設定.
Definition: Color.h:24
SDX::Renderer::GetHandle
SDL_Renderer * GetHandle()
描画ハンドルを取得.
Definition: Renderer.h:69
SDX::Color::White
static const Color White
白 [RGB]255,255,255
Definition: Color.h:81
SDX::Image
画像データを表すクラス.
Definition: Image.h:17
SDX::Renderer
描画先を表すクラス.
Definition: Renderer.h:27
SDX::Color
色を表すクラス.
Definition: Color.h:11
SDX::BlendMode::Mula
積算ブレンド
SDX::Renderer::Clear
bool Clear()
画面を消去する.
Definition: Renderer.h:161
SDX::Renderer::GetTarget
Image * GetTarget()
現在の描画先を取得する.
Definition: Renderer.h:184
SDX::Renderer::SetTarget
bool SetTarget(Image *描画対象=nullptr)
描画先を変更.
Definition: SDXafx.h:80
SDX::Renderer::ResetTransColor
bool ResetTransColor()
透過色を解除.
Definition: Renderer.h:117
SDX::SubWindow
ウィンドウを表すクラス.
Definition: SubWindow.h:14
SDX::Camera::Get
static Camera * Get()
現在アクティブなカメラを取得.
Definition: Camera.h:57
SDX::Color::GetRed
int GetRed() const
赤の要素を取得.
Definition: Color.h:30
SDX::BlendMode::Add
加算ブレンド
SDX::BlendMode::Alpha
αブレンド
SDX::Renderer::SetDrawMode
void SetDrawMode(const Color &輝度＋α値=Color::White, BlendMode ブレンドモード=BlendMode::NoBlend)
描画輝度と描画モードをまとめて設定
Definition: Renderer.h:237
SDX::Renderer::SetBlendMode
void SetBlendMode(BlendMode ブレンドモード, int α値)
描画モードを設定.
Definition: Renderer.h:227
SDX::Color::GetAlpha
int GetAlpha() const
透明度を取得.
Definition: Color.h:48
SDX::Renderer::mainRenderer
static Renderer & mainRenderer
メインウィンドウのレンダラー
Definition: Renderer.h:49
SDX::Renderer::SetBackColor
void SetBackColor(const Color &背景色)
Screen::Clear後の色を設定.
Definition: Renderer.h:214
SDX::Renderer::SetBright
void SetBright(const Color &輝度)
描画輝度を設定.
Definition: Renderer.h:221
SDX::BlendMode
BlendMode
ブレンドモード.
Definition: Renderer.h:14
SDX::Rect::GetW
double GetW() const override
幅を取得.
Definition: Rect.h:107
SDX::Rect::GetH
double GetH() const override
高さを取得.
Definition: Rect.h:112
SDX::Renderer::SetTransColor
bool SetTransColor(const Color &透過色)
透過色を設定.
Definition: Renderer.h:109