SDXFrameWork 0.10 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 surface = SDL_GetWindowSurface(元Window);
45 return true;
46 }
47 public:
48 static Renderer &mainRenderer;
49
50 BlendMode blendMode = BlendMode::NoBlend;
51 Color clearColor = Color(0, 0, 0);
52 Color rgba = Color(255, 255, 255, 255);
53
54 Renderer(){};
55
56 ~Renderer()
57 {
58 Destroy();
59 }
60
62 Renderer(int 幅, int 高さ)
63 {
64 Create(幅,高さ);
65 }
66
68 SDL_Renderer* GetHandle()
69 {
70 return handle;
71 }
72
75 bool Create(int 幅, int 高さ)
76 {
77 if (handle != nullptr) return false;
78
79 isWindow = false;
80 surface = SDL_CreateRGBSurface(0, 幅, 高さ, 32, 0, 0, 0, 0);
81
82 handle = SDL_CreateSoftwareRenderer(surface);
83 return true;
84 }
85
89 bool Destroy()
90 {
91 if ( isWindow){ return false; }
92 if (handle == 0){ return false; }
93 if (surface != nullptr)
94 {
95 SDL_FreeSurface(surface);
96 }
97
98 SDL_DestroyRenderer(handle);
99
100 surface = nullptr;
101 handle = nullptr;
102
103 return true;
104 }
105
108 bool SetTransColor(const Color &透過色)
109 {
110 if (!surface){ return false; }
111 return !SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 透過色.GetRed(), 透過色.GetBlue(), 透過色.GetGreen()));
112 }
113
116 bool ResetTransColor()
117 {
118 if (!surface){ return false; }
119 return !SDL_SetColorKey(surface, false, 0);
120 }
121
125 bool Draw(const Rect& 領域, Renderer& コピー先 = Renderer::mainRenderer)
126 {
127 if (!surface){ return false; }
128
129 SDL_Texture* image;
130 image = SDL_CreateTextureFromSurface(コピー先.GetHandle(), surface);
131
132 SDL_Rect srcrect = { 0, 0, (int)領域.GetW(), (int)領域.GetH() };
133 SDL_Rect dsrect = { (int)領域.GetX(), (int)領域.GetY(), (int)領域.GetW(), (int)領域.GetH() };
134 SDL_RenderCopy(コピー先.GetHandle(), image, &srcrect, &dsrect);
135 SDL_DestroyTexture(image);
136
137 return true;
138 }
139
143 bool DrawExtend(const Rect& 元範囲, const Rect& コピー先範囲, Renderer& コピー先 = Renderer::mainRenderer)
144 {
145 if (!surface){ return false; }
146
147 SDL_Texture* image = SDL_CreateTextureFromSurface(コピー先.GetHandle(), surface);
148
149 SDL_Rect srcrect = { (int)元範囲.GetX(), (int)元範囲.GetY(), (int)元範囲.GetW(), (int)元範囲.GetH() };
150 SDL_Rect dsrect = { (int)コピー先範囲.GetX(), (int)コピー先範囲.GetY(), (int)コピー先範囲.GetW(), (int)コピー先範囲.GetH() };
151
152 SDL_BlitScaled(surface, &srcrect, コピー先.surface, &dsrect);
153 SDL_RenderCopy(コピー先.GetHandle(), image, &srcrect, &dsrect);
154 SDL_DestroyTexture(image);
155
156 return true;
157 }
158
160 bool Clear()
161 {
162 if (handle == nullptr){ return false; }
163
164 SDL_SetRenderDrawColor
165 (
166 handle,
167 clearColor.GetRed(),
168 clearColor.GetGreen(),
169 clearColor.GetBlue(),
170 clearColor.GetAlpha()
171 );
172 SDL_RenderClear(handle);
173 return true;
174 }
175
179 bool SetTarget(Image *描画対象 = nullptr);
180
183 Image* GetTarget()
184 {
185 return target;
186 }
187
190 bool SetClip(const Rect &描画領域 = {0,0,0,0})
191 {
192 if (handle == nullptr){ return false; }
193
194 if (描画領域.GetW() == 0)
195 {
196 return !SDL_RenderSetClipRect(handle, 0);
197 }
198
199 SDL_Rect rect;
200 if (Camera::Get())
201 {
202 rect = Camera::Get()->TransRect(描画領域);
203 }
204 else
205 {
206 rect = 描画領域;
207 }
208
209 return !SDL_RenderSetClipRect(handle, &rect);
210 }
211
213 void SetBackColor(const Color &背景色)
214 {
215 clearColor.SetColor(背景色.GetRed(), 背景色.GetGreen(), 背景色.GetBlue() );
216 }
217
220 void SetBright(const Color &輝度)
221 {
222 rgba.SetColor(輝度.GetRed(), 輝度.GetGreen(), 輝度.GetBlue());
223 }
224
226 void SetBlendMode(BlendMode ブレンドモード, int α値)
227 {
228 blendMode = ブレンドモード;
229 if (α値 > 255 || ブレンドモード == BlendMode::NoBlend){ α値 = 255; }
230 else if (α値 < 0){ α値 = 0; }
231
232 rgba.SetColor(rgba.GetRed(),rgba.GetGreen(),rgba.GetBlue(),α値);
233 }
234
236 void SetDrawMode(const Color &輝度＋α値 = Color::White, BlendMode ブレンドモード = BlendMode::NoBlend)
237 {
238 SetBright(輝度＋α値);
239 SetBlendMode(ブレンドモード, 輝度＋α値.GetAlpha());
240 }
241
243 /*
244 bool SaveBMP(const char* ファイル名)
245 {
246 if ( handle == nullptr ){ return false; }
247 return !SDL_SaveBMP(surface, ファイル名);
248 }
249 */
250 };
251 }
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:52
SDX::Renderer::DrawExtend
bool DrawExtend(const Rect &元範囲, const Rect &コピー先範囲, Renderer &コピー先=Renderer::mainRenderer)
対象RenderHandleにコピー.
Definition: Renderer.h:143
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:51
SDX::Camera::TransRect
SDL_Rect TransRect(const SDL_Rect &変換前矩形) const
領域を画面上のどこに相当するか変換.
Definition: Camera.h:103
SDX::Renderer::Renderer
Renderer(int 幅, int 高さ)
コンストラクタ.
Definition: Renderer.h:62
SDX::Renderer::blendMode
BlendMode blendMode
描画モード
Definition: Renderer.h:50
SDX::Renderer::Create
bool Create(int 幅, int 高さ)
Windowと独立した、Rendererを生成.
Definition: Renderer.h:75
SDX::Renderer::Draw
bool Draw(const Rect &領域, Renderer &コピー先=Renderer::mainRenderer)
対象RenderHandleにコピー.
Definition: Renderer.h:125
SDX::Renderer::Destroy
bool Destroy()
Rendererを削除.
Definition: Renderer.h:89
SDX::Renderer::SetClip
bool SetClip(const Rect &描画領域={0, 0, 0, 0})
描画領域を設定する、範囲外には描画されない.
Definition: Renderer.h:190
SDX::Color::SetColor
void SetColor(int 赤, int 緑, int 青, int α値=255)
色の各要素を設定.
Definition: Color.h:24
SDX::Renderer::GetHandle
SDL_Renderer * GetHandle()
描画ハンドルを取得.
Definition: Renderer.h:68
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:160
SDX::Renderer::GetTarget
Image * GetTarget()
現在の描画先を取得する.
Definition: Renderer.h:183
SDX::Renderer::SetTarget
bool SetTarget(Image *描画対象=nullptr)
描画先を変更.
Definition: SDXafx.h:74
SDX::Renderer::ResetTransColor
bool ResetTransColor()
透過色を解除.
Definition: Renderer.h:116
SDX::SubWindow
ウィンドウを表すクラス.
Definition: SubWindow.h:13
SDX::Camera::Get
static Camera * Get()
現在アクティブなカメラを取得.
Definition: Camera.h:58
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:236
SDX::Renderer::SetBlendMode
void SetBlendMode(BlendMode ブレンドモード, int α値)
描画モードを設定.
Definition: Renderer.h:226
SDX::Color::GetAlpha
int GetAlpha() const
透明度を取得.
Definition: Color.h:48
SDX::Renderer::mainRenderer
static Renderer & mainRenderer
メインウィンドウのレンダラー
Definition: Renderer.h:48
SDX::Renderer::SetBackColor
void SetBackColor(const Color &背景色)
Screen::Clear後の色を設定.
Definition: Renderer.h:213
SDX::Renderer::SetBright
void SetBright(const Color &輝度)
描画輝度を設定.
Definition: Renderer.h:220
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:108