mirror of
https://github.com/Relintai/programming_tutorials.git
synced 2025-04-21 21:51:22 +02:00
297 lines
8.8 KiB
Plaintext
297 lines
8.8 KiB
Plaintext
|
|
Írjunk egy Textúra osztályt.
|
|
|
|
Ez fogja menedzselni az SDL_Texture osztályokat.
|
|
|
|
Ahogy a bevezetőben elhangzott, Ez egy kép adatait tartalmazza, viszont ez a
|
|
softwares rajzoláshoz optimalizált formában.
|
|
|
|
Kirajzolni a Renderer segédfüggvényeivel lehet, ezeket később menjetek vissza, és
|
|
implementáljátok be.
|
|
|
|
Fontos!
|
|
|
|
A renderer.h-t csak a texture.cpp fájlba include-oljátok be!
|
|
|
|
Ugyanis, a texture.h kelleni fog a renderer.h-nak is!
|
|
És nem lehet kör az inlcude gráfban.
|
|
|
|
Ebben az esetben a renderer csak a .cpp fájlban kell, így ez a legegyszerűbb megoldás.
|
|
|
|
|
|
------------
|
|
|
|
Megj.:
|
|
|
|
Ha a Renderer osztályt a .h fájlban is kellene használni valamiért, akkor előre kellene
|
|
definiálni, így:
|
|
|
|
---
|
|
|
|
texture.h:
|
|
|
|
class Renderer;
|
|
|
|
class Texture {
|
|
//...
|
|
};
|
|
|
|
texture.cpp:
|
|
|
|
#include "remderer.h"
|
|
|
|
---
|
|
|
|
És ugyanezt el lehetne játszani a Rendererbe is, sőt ha lenne több ilyen körkörös
|
|
függőség, azoknál is.
|
|
|
|
Nagy projektekben előfordul az ilyen, érdemes fejben tartani, hogy meg lehet
|
|
oldani, és hogy így.
|
|
|
|
Ez amúgy azért működik, mert a cpp fájlok külön fordítási egységekként vannak
|
|
lefordítva (alapból, akkor is, ha csak több cpp fájlt fodítunk egy paranccsal
|
|
programmá), és a végén egy utolsó lépésként vannak összelinkelve
|
|
(összeállítva) egy futtatható fájllá. És ez a hiba pont a fordítási
|
|
lépésben okoz gondot.
|
|
|
|
------------
|
|
|
|
És akkor a Texture osztály UML diagramja:
|
|
|
|
|---------------------------------------------------------------------------------------|
|
|
| class Texture |
|
|
|---------------------------------------------------------------------------------------|
|
|
| + Color get_color_mod() const |
|
|
| + void set_color_mod(const Color &color) |
|
|
| |
|
|
| + SDL_BlendMode get_blend_mode() const |
|
|
| + void set_blend_mode(const SDL_BlendMode blend_mode) |
|
|
| |
|
|
| + SDL_ScaleMode get_texture_scale_mode() const |
|
|
| + void set_texture_scale_mode(const SDL_ScaleMode scale_mode) |
|
|
| |
|
|
| + Image *get_image() |
|
|
| + void set_image(Image *image) |
|
|
| |
|
|
| + int get_width() const |
|
|
| + int get_height() const |
|
|
| + Uint32 get_format() const |
|
|
| + int get_access() const |
|
|
| |
|
|
| + void create(const int access, const int w, const int h) |
|
|
| + void refresh() |
|
|
| + void free() |
|
|
| |
|
|
| + SDL_Texture *get_texture() |
|
|
| + SDL_Texture *get_texture() const |
|
|
| |
|
|
| + bool is_render_target() |
|
|
| |
|
|
| + Texture() |
|
|
| + Texture(Image *image) |
|
|
| + virtual ~Texture() |
|
|
| |
|
|
| - Image *_image |
|
|
| - SDL_Texture *_texture |
|
|
|---------------------------------------------------------------------------------------|
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_color_mod:
|
|
Uint8 r
|
|
Uint8 g
|
|
Uint8 b
|
|
Uint8 a
|
|
|
|
SDL_GetTextureColorMod(_texture, &r, &g, &b)
|
|
SDL_GetTextureAlphaMod(_texture, &a)
|
|
|
|
return Color(r, g, b, a)
|
|
|
|
|
|
Az sdl külön kezeli a színezés paramétert, és az átlátszóságot, de ezt itt egybe vettem,
|
|
mert így jobban illik a program felépítéséhez.
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
set_color_mod:
|
|
SDL_SetTextureColorMod(_texture, color.r, color.g, color.b)
|
|
SDL_SetTextureAlphaMod(_texture, color.a)
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_blend_mode:
|
|
SDL_BlendMode blendMode
|
|
|
|
SDL_GetTextureBlendMode(_texture, &blendMode)
|
|
|
|
return blendMode
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
set_blend_mode:
|
|
SDL_SetTextureBlendMode(_texture, blend_mode)
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_texture_scale_mode:
|
|
SDL_ScaleMode scale_mode
|
|
|
|
SDL_GetTextureScaleMode(_texture, &scale_mode)
|
|
|
|
return scale_mode
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
set_texture_scale_mode:
|
|
SDL_SetTextureScaleMode(_texture, scale_mode)
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_image:
|
|
return _image
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
set_image:
|
|
if (_texture)
|
|
free()
|
|
|
|
|
|
_image = image
|
|
|
|
refresh()
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_width:
|
|
Uint32 format
|
|
int access
|
|
int w
|
|
int h
|
|
|
|
if (SDL_QueryTexture(_texture, &format, &access, &w, &h))
|
|
return 0
|
|
|
|
|
|
return w
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_height:
|
|
Uint32 format
|
|
int access
|
|
int w
|
|
int h
|
|
|
|
if (SDL_QueryTexture(_texture, &format, &access, &w, &h))
|
|
return 0
|
|
|
|
|
|
return h
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_format:
|
|
Uint32 format
|
|
int access
|
|
int w
|
|
int h
|
|
|
|
if (SDL_QueryTexture(_texture, &format, &access, &w, &h))
|
|
return 0
|
|
|
|
|
|
return format
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_access:
|
|
Uint32 format
|
|
int access
|
|
int w
|
|
int h
|
|
|
|
if (SDL_QueryTexture(_texture, &format, &access, &w, &h))
|
|
return 0
|
|
|
|
|
|
return access
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
create:
|
|
if (_texture)
|
|
free()
|
|
|
|
|
|
_image = nullptr
|
|
|
|
_texture = SDL_CreateTexture(Renderer::get_singleton()->get_renderer(), SDL_PIXELFORMAT_RGBA8888, access, w, h)
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
refresh:
|
|
if (_image == nullptr)
|
|
return
|
|
|
|
|
|
if (_image->get_surface() == nullptr)
|
|
return
|
|
|
|
|
|
if (_texture)
|
|
free()
|
|
|
|
|
|
_texture = SDL_CreateTextureFromSurface(Renderer::get_singleton()->get_renderer(), _image->get_surface())
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
free:
|
|
if (_texture)
|
|
SDL_DestroyTexture(_texture)
|
|
|
|
_texture = nullptr
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_texture():
|
|
return _texture
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
get_texture() const:
|
|
return _texture
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
is_render_target:
|
|
if (_texture == Renderer::get_singleton()->get_render_target())
|
|
return true
|
|
|
|
|
|
return false
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
Texture():
|
|
_image = nullptr
|
|
_texture = nullptr
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
Texture(Image *image):
|
|
_image = nullptr
|
|
_texture = nullptr
|
|
|
|
set_image(image)
|
|
|
|
------------------------------------------------------------------------------------------
|
|
|
|
~Texture:
|
|
if (_texture)
|
|
free()
|
|
|
|
------------------------------------------------------------------------------------------
|