SDXFrameWork 0.10 SDXFrameWork

- Main Page
- Related Pages
- Classes

All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Image.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/Screen.h>
7 #include <Multimedia/Loading.h>
8 #include <Multimedia/IDrawable.h>
9 #include <Framework/Shape.h>
10 #include <Framework/Camera.h>
11
12 namespace SDX
13 {
17 class Image : public IDrawable
18 {
19 friend class Drawing;
20 friend class ImagePack;//AdjustWidthで必要
21 private:
22 SDL_Rect part;
23 SDL_Texture* handle = nullptr;
24 Color color = { 255, 255, 255 ,255};
25
27 void RGBACulculate() const
28 {
29 if (Screen::GetRenderer()->blendMode == BlendMode::NoBlend)
30 {
31 //pngの透過部分があるのでNoBlendで描画はしない
32 SDL_SetTextureBlendMode(handle, (SDL_BlendMode)BlendMode::Alpha);
33 SDL_SetTextureAlphaMod(handle, color.GetAlpha());
34 }
35 else
36 {
37 SDL_SetTextureBlendMode(handle, (SDL_BlendMode)Screen::GetRenderer()->blendMode);
38 SDL_SetTextureAlphaMod(handle, Screen::GetRenderer()->rgba.GetAlpha() * color.GetAlpha() / 255);
39 }
40
41 SDL_SetTextureColorMod
42 (
43 handle,
44 Screen::GetRenderer()->rgba.GetRed() * color.GetRed() / 255,
45 Screen::GetRenderer()->rgba.GetGreen() * color.GetGreen() / 255,
46 Screen::GetRenderer()->rgba.GetBlue() * color.GetBlue() / 255
47 );
48 }
49 public:
50
51 Image(){};
52
53 ~Image(){};
54
56 Image(const char *ファイル名)
57 {
58 Load(ファイル名);
59 }
60
63 Image(const Image& コピー元, const Rect& コピー領域)
64 {
65 Copy(コピー元, コピー領域);
66 }
67
69 Image(SDL_Texture* 画像ハンドル,int 幅,int 高さ):
70 handle(画像ハンドル),
71 part({0,0,幅,高さ})
72 {
73 return;//何か書いておかないとDoxygenが不具合を起こす
74 }
75
77 Image(int 幅, int 高さ)
78 {
79 Make(幅, 高さ);
80 }
81
83 bool Load(const char *ファイル名)
84 {
85 if (Loading::isLoading)
86 {
87 Loading::AddLoading([=]{ Load(ファイル名);});
88 return true;
89 }
90
91 if (this->handle != nullptr){ return false; }
92
93 SDL_Surface* temp = IMG_Load(ファイル名);
94
95 if (temp == nullptr) return false;
96
97 handle = SDL_CreateTextureFromSurface(Screen::GetHandle(), temp);
98
99 part.x = 0;
100 part.y = 0;
101 part.w = temp->w;
102 part.h = temp->h;
103
104 SDL_FreeSurface(temp);
105 return (handle != nullptr);
106 }
107
109 bool Release()
110 {
111 if (this->handle != nullptr)
112 {
113 SDL_DestroyTexture(handle);
114 return true;
115 }
116 return false;
117 }
118
120 SDL_Texture* Make(int 幅, int 高さ)
121 {
122 Release();
123
124 handle = SDL_CreateTexture(Screen::GetHandle(), 0, SDL_TEXTUREACCESS_TARGET, 幅, 高さ);
125 part.x = 0;
126 part.y = 0;
127 part.w = 幅;
128 part.h = 高さ;
129
130 return handle;
131 }
132
135 SDL_Texture* Copy(const Image& 元イメージ, const Rect& コピー領域)
136 {
137 this->handle = 元イメージ.handle;
138 part.x = (int)コピー領域.x;
139 part.y = (int)コピー領域.y;
140 part.w = (int)コピー領域.GetW();
141 part.h = (int)コピー領域.GetH();
142
143 return handle;
144 }
145
148 Image Clone(bool is反転 = false) const
149 {
150 return Clone({part.x,part.y,part.w,part.h}, is反転);
151 }
152
155 Image Clone(const Rect& コピー領域 , bool is反転 = false) const
156 {
157 Image image((int)コピー領域.GetW(), (int)コピー領域.GetH());
158 auto prev = Screen::GetTarget();
159 auto mode = Screen::GetRenderer()->blendMode;
160 auto rgba = Screen::GetRenderer()->rgba;
161 auto cam = Camera::Get();
162
163 Screen::SetTarget(&image);
164 Screen::SetDrawMode();
165 Camera::Set();
166 DrawPart({ 0, 0 }, コピー領域, is反転);
167 Screen::SetTarget(prev);
168 Screen::SetDrawMode(rgba,mode);
169 Camera::Set(cam);
170
171 return image;
172 }
173
175 SDL_Texture* GetHandle() const
176 {
177 return handle;
178 }
179
181 bool Draw(const Point &座標, bool 反転フラグ = false) const override
182 {
183 SDL_Rect temp = { (int)座標.x, (int)座標.y, part.w, part.h };
184
185 if (Camera::Get())
186 {
187 temp = Camera::Get()->TransRect(temp);
188 }
189
190 RGBACulculate();
191 if (反転フラグ)
192 {
193 return !SDL_RenderCopyEx(Screen::GetHandle(), handle, &part, &temp, 0, nullptr, SDL_RendererFlip::SDL_FLIP_HORIZONTAL);
194 }
195 else{
196 return !SDL_RenderCopy(Screen::GetHandle(), handle, &part, &temp);
197 }
198 }
199
201 bool DrawExtend(const Rect &描画領域 , bool 反転フラグ = false) const override
202 {
203 SDL_Rect temp = { (int)描画領域.x, (int)描画領域.y, (int)描画領域.GetW(), (int)描画領域.GetH() };
204
205 if (Camera::Get())
206 {
207 temp = Camera::Get()->TransRect(temp);
208 }
209
210 //サイズがマイナスなら描画しない
211 if (temp.w < 0 || temp.h < 0)
212 {
213 return false;
214 }
215
216 RGBACulculate();
217 if (反転フラグ)
218 {
219 static SDL_Point point = { 0, 0 };
220 return !SDL_RenderCopyEx(Screen::GetHandle(), handle, &part, &temp, 0, &point, SDL_RendererFlip::SDL_FLIP_HORIZONTAL);
221 }
222 else
223 {
224 return !SDL_RenderCopy(Screen::GetHandle(), handle, &part, &temp);
225 }
226 }
227
229 bool DrawRotate(const Point &座標, double 拡大率, double 角度, bool 反転フラグ = false) const override
230 {
231 const int wbuf = int(part.w*拡大率);
232 const int hbuf = int(part.h*拡大率);
233 SDL_Rect temp = { (int)座標.x - wbuf / 2, (int)座標.y - hbuf / 2, wbuf, hbuf };
234
235 if (Camera::Get())
236 {
237 temp = Camera::Get()->TransRect(temp);
238 }
239
240 RGBACulculate();
241 return !SDL_RenderCopyEx(Screen::GetHandle(), handle, &part, &temp, 角度 * 180 / PAI, nullptr, SDL_RendererFlip(反転フラグ));
242 }
244 bool DrawRotateAxis(const Point &座標, const Point &回転軸座標, double 拡大率, double 角度, bool 反転フラグ = false) const override
245 {
246 return DrawRotateAxis(座標, 回転軸座標, 拡大率, 拡大率, 角度, 反転フラグ);
247 }
249 bool DrawRotateAxis(const Point &座標, const Point &回転軸座標, double 拡大率X, double 拡大率Y, double 角度, bool 反転フラグ = false) const override
250 {
251 const int wbuf = int(part.w*拡大率X);
252 const int hbuf = int(part.h*拡大率Y);
253 SDL_Rect temp = { (int)座標.x - wbuf / 2, (int)座標.y - hbuf / 2, wbuf, hbuf };
254 SDL_Point point = { int(回転軸座標.x*拡大率X), int(回転軸座標.y*拡大率Y) };
255
256 if (Camera::Get())
257 {
258 temp = Camera::Get()->TransRect(temp);
259 point.x = int(point.x * Camera::Get()->zoom);
260 point.y = int(point.y * Camera::Get()->zoom);
261 }
262
263 RGBACulculate();
264 return !SDL_RenderCopyEx(Screen::GetHandle(), handle, &part, &temp, 角度 * 180 / PAI, &point, SDL_RendererFlip(反転フラグ));
265 }
266
268 bool DrawPart(const Point &描画先座標, const Rect &描画元領域, bool 反転フラグ = false) const override
269 {
270 SDL_Rect temp = { (int)描画先座標.x, (int)描画先座標.y, (int)描画元領域.GetW(), (int)描画元領域.GetH() };
271 SDL_Rect part = { (int)描画元領域.x + this->part.x, (int)描画元領域.y + this->part.y, (int)描画元領域.GetW(), (int)描画元領域.GetH() };
272
273 if (Camera::Get())
274 {
275 temp = Camera::Get()->TransRect(temp);
276 }
277
278 RGBACulculate();
279 return !SDL_RenderCopyEx(Screen::GetHandle(), handle, &part, &temp, 0, nullptr, SDL_RendererFlip(反転フラグ));
280 }
281
283 bool DrawPartExtend(const Rect &描画先領域, const Rect &描画元領域, bool 反転フラグ = false) const
284 {
285 SDL_Rect temp = { (int)描画先領域.x, (int)描画先領域.y, (int)描画先領域.GetW(), (int)描画先領域.GetH() };
286 SDL_Rect part = { (int)描画元領域.x + this->part.x, (int)描画元領域.y + this->part.y, (int)描画元領域.GetW(), (int)描画元領域.GetH() };
287
288 if (Camera::Get())
289 {
290 temp = Camera::Get()->TransRect(temp);
291 }
292
293 //サイズがマイナスなら描画しない
294 if (temp.w < 0 || temp.h < 0)
295 {
296 return false;
297 }
298
299 RGBACulculate();
300 return !SDL_RenderCopyEx(Screen::GetHandle(), handle, &part, &temp, 0, nullptr, SDL_RendererFlip(反転フラグ));
301 }
302
304 int GetWidth() const
305 {
306 return part.w;
307 }
308
310 int GetHeight() const
311 {
312 return part.h;
313 }
314
316 void SetColor(const Color &描画色)
317 {
318 color.SetColor(描画色.GetRed(), 描画色.GetGreen(), 描画色.GetBlue(), 描画色.GetAlpha());
319 }
320 };
321 }
SDX::Image::Copy
SDL_Texture * Copy(const Image &元イメージ, const Rect &コピー領域)
別のImageの一部をコピーして、Imageを初期化.
Definition: Image.h:135
SDX::PAI
const double PAI
円周率
Definition: SDX.h:26
SDX::Rect::y
double y
起点座標
Definition: Rect.h:26
SDX::Image::Make
SDL_Texture * Make(int 幅, int 高さ)
空のイメージを作成.
Definition: Image.h:120
SDX::Color::GetGreen
int GetGreen() const
緑の要素を取得.
Definition: Color.h:36
SDX::Rect
矩形を表す図形クラス.
Definition: Rect.h:22
SDX::Screen::GetTarget
static Image * GetTarget()
描画先Imageを取得.
Definition: Screen.h:55
SDX::Point::y
double y
座標
Definition: Point.h:26
SDX::Image::DrawRotateAxis
bool DrawRotateAxis(const Point &座標, const Point &回転軸座標, double 拡大率, double 角度, bool 反転フラグ=false) const override
回転軸、角度、拡大率を指定して描画.
Definition: Image.h:244
SDX::Renderer::rgba
Color rgba
描画輝度とα値
Definition: Renderer.h:52
SDX::Image::DrawExtend
bool DrawExtend(const Rect &描画領域, bool 反転フラグ=false) const override
指定矩形内に描画.
Definition: Image.h:201
SDX::Image::DrawPart
bool DrawPart(const Point &描画先座標, const Rect &描画元領域, bool 反転フラグ=false) const override
指定した一部分を描画.
Definition: Image.h:268
SDX::BlendMode::NoBlend
ブレンドしない
SDX::Color::GetBlue
int GetBlue() const
青の要素を取得.
Definition: Color.h:42
SDX::Drawing
リソースを読み込まずに描画を行う関数群.
Definition: Drawing.h:17
SDX::Camera::TransRect
SDL_Rect TransRect(const SDL_Rect &変換前矩形) const
領域を画面上のどこに相当するか変換.
Definition: Camera.h:103
SDX::Point
点を表す図形クラス.
Definition: Point.h:22
SDX::Renderer::blendMode
BlendMode blendMode
描画モード
Definition: Renderer.h:50
SDX::Image::Image
Image(const char *ファイル名)
画像をメモリへ読み込む.
Definition: Image.h:56
SDX::Image::Image
Image(int 幅, int 高さ)
空のイメージを作成.
Definition: Image.h:77
SDX::Screen::GetRenderer
static Renderer * GetRenderer()
アクティブなRendererを取得.
Definition: Screen.h:33
SDX::Image::DrawRotate
bool DrawRotate(const Point &座標, double 拡大率, double 角度, bool 反転フラグ=false) const override
角度、拡大率を指定して描画.
Definition: Image.h:229
SDX::Color::SetColor
void SetColor(int 赤, int 緑, int 青, int α値=255)
色の各要素を設定.
Definition: Color.h:24
SDX::Screen::SetTarget
static bool SetTarget(Image *描画対象=nullptr)
描画先を変更.
Definition: Screen.h:48
SDX::Rect::x
double x
起点座標
Definition: Rect.h:25
SDX::Image
画像データを表すクラス.
Definition: Image.h:17
SDX::Color
色を表すクラス.
Definition: Color.h:11
SDX::Image::Clone
Image Clone(const Rect &コピー領域, bool is反転=false) const
Imageの一部から別Imageを作成.
Definition: Image.h:155
SDX::Loading::AddLoading
static void AddLoading(std::function< void(void)> &&読み込み関数)
非同期読み込み処理に追加.
Definition: Loading.h:96
SDX::Image::GetHeight
int GetHeight() const
高さを取得.
Definition: Image.h:310
SDX::Image::GetWidth
int GetWidth() const
幅を取得.
Definition: Image.h:304
SDX::Image::GetHandle
SDL_Texture * GetHandle() const
ハンドルを取得.
Definition: Image.h:175
SDX::IDrawable
描画可能なクラスのインターフェース.
Definition: IDrawable.h:14
SDX::Image::Release
bool Release()
イメージをメモリから開放.
Definition: Image.h:109
SDX::Screen::GetHandle
static SDL_Renderer * GetHandle()
スクリーンハンドルを取得.
Definition: Screen.h:26
SDX::Camera::Get
static Camera * Get()
現在アクティブなカメラを取得.
Definition: Camera.h:58
SDX::Screen::SetDrawMode
static void SetDrawMode(const Color &輝度＋α値=Color::White, BlendMode ブレンドモード=BlendMode::Alpha)
描画輝度と描画モードをまとめて設定
Definition: Screen.h:100
SDX::Color::GetRed
int GetRed() const
赤の要素を取得.
Definition: Color.h:30
SDX::Image::Image
Image(SDL_Texture *画像ハンドル, int 幅, int 高さ)
SDL_TextureからImageを作成.
Definition: Image.h:69
SDX::BlendMode::Alpha
αブレンド
SDX::Camera::zoom
double zoom
拡大率、マイナスになると描画が狂う
Definition: Camera.h:46
SDX::Point::x
double x
座標
Definition: Point.h:25
SDX::ImagePack
複数のImageをまとめるクラス.
Definition: ImagePack.h:14
SDX::Camera::Set
static void Set(Camera *アクティブにするCamera=nullptr)
現在アクティブなカメラを設定.
Definition: Camera.h:64
SDX::Image::SetColor
void SetColor(const Color &描画色)
描画色を指定.
Definition: Image.h:316
SDX::Color::GetAlpha
int GetAlpha() const
透明度を取得.
Definition: Color.h:48
SDX::Image::DrawPartExtend
bool DrawPartExtend(const Rect &描画先領域, const Rect &描画元領域, bool 反転フラグ=false) const
指定した一部分を描画.
Definition: Image.h:283
SDX::Image::Draw
bool Draw(const Point &座標, bool 反転フラグ=false) const override
指定座標に描画.
Definition: Image.h:181
SDX::Image::Load
bool Load(const char *ファイル名)
画像をメモリへ読み込む.
Definition: Image.h:83
SDX::Image::Image
Image(const Image &コピー元, const Rect &コピー領域)
別のImageの一部をコピーして、Imageを初期化.
Definition: Image.h:63
SDX::Rect::GetW
double GetW() const override
幅を取得.
Definition: Rect.h:107
SDX::Image::DrawRotateAxis
bool DrawRotateAxis(const Point &座標, const Point &回転軸座標, double 拡大率X, double 拡大率Y, double 角度, bool 反転フラグ=false) const override
回転軸、角度、拡大率を縦横別に指定して描画.
Definition: Image.h:249
SDX::Rect::GetH
double GetH() const override
高さを取得.
Definition: Rect.h:112
SDX::Image::Clone
Image Clone(bool is反転=false) const
同じImageを作成.
Definition: Image.h:148