programming_tutorials/02_sdl_basics/7_sdl_sprite.txt

411 lines
14 KiB
Plaintext

Írjunk egy Sprite osztályt.
Egy Sprite az egybe fog egy textúrát (Texture) és az ennek a kirajzolásához
szükséges adatokat. (Pozíció, méret, stb).
A lényege egy ilyne osztálynak, hogy ne kelljen sok külön textúrát létrehozni,
és eltárolni a memóriában, ha csak egy textúrát többször több helyre akarunk kirajzolni.
A Renderer-be van függvény, amik ezt használják, azokat ne felejtsátek el implementálni!
Fontos, az include-olásos probléma ugyanúgy jelen van mint a textúra osztálynál!
Figylejetek rá!
|---------------------------------------------------------------------------------------|
| class Sprite |
|---------------------------------------------------------------------------------------|
| + Rect2 get_texture_clip_rect() const |
| + void set_texture_clip_rect(const Rect2 &rect) |
| |
| + Rect2 get_transform() const |
| + void set_transform(const Rect2 &rect) |
| |
| + float get_x() const |
| + void set_x(const float val) |
| |
| + float get_y() const |
| + void set_y(const float val) |
| |
| + float get_w() const |
| + void set_w(const float val) |
| |
| + float get_h() const |
| + void set_h(const float val) |
| |
| + double get_angle() const |
| + void set_angle(const double val) |
| |
| + float get_anchor_x() const |
| + void set_anchor_x(const float val) |
| |
| + float get_anchor_y() const |
| + void set_anchor_y(const float val) |
| |
| + void set_anchor(const float x, const float y) |
| |
| + SDL_RendererFlip get_flip() const |
| + void set_flip(const SDL_RendererFlip val) |
| |
| + Color get_color_mod() const |
| + void set_color_mod(const Color &color) |
| |
| + Texture *get_texture() |
| + Texture *get_texture() const |
| + void set_texture(Texture *texture) |
| |
| + void draw() |
| |
| + Sprite() |
| + Sprite(Texture *texture) |
| + Sprite(Texture *texture, const Color &color_mod) |
| + Sprite(Texture *texture, const float x, const float y, const double angle = 0) |
| |
| + Sprite(Texture *texture, const float x, const float y, |
| const Rect2 &texture_clip_rect, const double angle = 0) |
| |
| + Sprite(Texture *texture, const Rect2 &transform, |
| const Rect2 &texture_clip_rect, const double angle = 0) |
| |
| + Sprite(Texture *texture, const float x, const float y, |
| const float w, const float h, const double angle = 0) |
| + virtual ~Sprite() | -> Üres
| |
| - Rect2 _texture_clip_rect |
| - Rect2 _transform |
| - double _angle |
| |
| - float _anchor_x |
| - float _anchor_y |
| |
| - SDL_RendererFlip _flip |
| |
| - Color _color_mod |
| |
| - Texture *_texture |
|---------------------------------------------------------------------------------------|
------------------------------------------------------------------------------------------
get_texture_clip_rect:
return _texture_clip_rect
Ezzel be lehet állítani, hogy a textúrának csak egy kis részletét rajzolja ki a sprite.
Pl így lehet sprite sheet-eket, tile map-okat, de akár bitmap font-okat is csinálni.
------------------------------------------------------------------------------------------
set_texture_clip_rect:
_texture_clip_rect = rect
------------------------------------------------------------------------------------------
get_transform:
return _transform
Transform-nak neveztem, de jó ha tudjátok, hogy játékmotorokban a transzformációk
általában mátrixokat jelentenek. Ezek annyival jobbak, hogy a forgatások, és skálázások
is beléjük fér.
------------------------------------------------------------------------------------------
set_transform:
_transform = rect
------------------------------------------------------------------------------------------
get_x:
return _transform.x
------------------------------------------------------------------------------------------
set_x:
_transform.x = val
------------------------------------------------------------------------------------------
get_y:
return _transform.y
------------------------------------------------------------------------------------------
set_y:
_transform.y = val
------------------------------------------------------------------------------------------
get_w:
return _transform.w
------------------------------------------------------------------------------------------
set_w:
_transform.w = val
------------------------------------------------------------------------------------------
get_h:
return _transform.h
------------------------------------------------------------------------------------------
set_h:
_transform.h = val
------------------------------------------------------------------------------------------
get_angle:
return _angle
------------------------------------------------------------------------------------------
set_angle:
_angle = val
------------------------------------------------------------------------------------------
get_anchor_x:
return _anchor_x
------------------------------------------------------------------------------------------
set_anchor_x:
_anchor_x = val
------------------------------------------------------------------------------------------
get_anchor_y:
return _anchor_y
------------------------------------------------------------------------------------------
set_anchor_y:
_anchor_y = val
------------------------------------------------------------------------------------------
set_anchor:
_anchor_x = x
_anchor_y = y
------------------------------------------------------------------------------------------
get_flip:
return _flip
------------------------------------------------------------------------------------------
set_flip:
_flip = val
------------------------------------------------------------------------------------------
get_color_mod:
return _color_mod
------------------------------------------------------------------------------------------
set_color_mod:
_color_mod = color
------------------------------------------------------------------------------------------
get_texture():
return _texture
------------------------------------------------------------------------------------------
get_texture() const:
return _texture
------------------------------------------------------------------------------------------
set_texture:
_texture = texture
------------------------------------------------------------------------------------------
draw:
Renderer::get_singleton()->draw_sprite(this)
Kényelmi függvény, hogy ne kelljen mindenhol include-olni a renderert.
Végül nem virtuálisnak csináltam, de nyugodtan át lehet rakni annak,
jó dolgokat lehet vele csinálni, ha az.
------------------------------------------------------------------------------------------
Sprite():
_angle = 0
_anchor_x = 0
_anchor_y = 0
_flip = SDL_FLIP_NONE
_texture = nullptr
_color_mod = Color(255, 255, 255, 255)
------------------------------------------------------------------------------------------
Sprite(Texture *texture):
_angle = 0
_anchor_x = 0
_anchor_y = 0
_flip = SDL_FLIP_NONE
_texture = texture
if (_texture != nullptr):
_texture_clip_rect.w = texture->get_width()
_texture_clip_rect.h = texture->get_height()
_transform.w = texture->get_width()
_transform.h = texture->get_height()
_color_mod = Color(255, 255, 255, 255)
------------------------------------------------------------------------------------------
Sprite(Texture *texture, const Color &color_mod):
_angle = 0
_anchor_x = 0
_anchor_y = 0
_flip = SDL_FLIP_NONE
_texture = texture
if (_texture != nullptr):
_texture_clip_rect.w = texture->get_width()
_texture_clip_rect.h = texture->get_height()
_transform.w = texture->get_width()
_transform.h = texture->get_height()
_color_mod = color_mod
------------------------------------------------------------------------------------------
Sprite(Texture *texture, const float x, const float y, const double angle):
_angle = angle
_anchor_x = 0
_anchor_y = 0
_flip = SDL_FLIP_NONE
_texture = texture
_transform.x = x
_transform.y = y
if (_texture != nullptr):
_texture_clip_rect.w = texture->get_width()
_texture_clip_rect.h = texture->get_height()
_transform.w = texture->get_width()
_transform.h = texture->get_height()
_color_mod = Color(255, 255, 255, 255)
------------------------------------------------------------------------------------------
Sprite(Texture *texture, const float x, const float y, const Rect2 &texture_clip_rect, const double angle):
_angle = angle
_anchor_x = 0
_anchor_y = 0
_flip = SDL_FLIP_NONE
_texture = texture
_transform.x = x
_transform.y = y
if (_texture != nullptr):
_transform.w = texture->get_width()
_transform.h = texture->get_height()
_texture_clip_rect = texture_clip_rect
_color_mod = Color(255, 255, 255, 255)
------------------------------------------------------------------------------------------
Sprite(Texture *texture, const Rect2 &transform, const Rect2 &texture_clip_rect, const double angle):
_angle = angle
_anchor_x = 0
_anchor_y = 0
_flip = SDL_FLIP_NONE
_texture = texture
_transform = transform
_texture_clip_rect = texture_clip_rect
_color_mod = Color(255, 255, 255, 255)
------------------------------------------------------------------------------------------
Sprite(Texture *texture, const float x, const float y, const float w, const float h, const double angle):
_angle = angle
_anchor_x = 0
_anchor_y = 0
_flip = SDL_FLIP_NONE
_texture = texture
_transform.x = x
_transform.y = y
_transform.w = w
_transform.h = h
if (_texture != nullptr):
_texture_clip_rect.w = texture->get_width()
_texture_clip_rect.h = texture->get_height()
_color_mod = Color(255, 255, 255, 255)
------------------------------------------------------------------------------------------