mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-14 04:57:21 +01:00
Added image from godot, and removed the sdl image/sprite/texture.
This commit is contained in:
parent
a834ef7e87
commit
88fafbe78e
3496
core/image/image.cpp
3496
core/image/image.cpp
File diff suppressed because it is too large
Load Diff
@ -1,69 +1,389 @@
|
||||
/*************************************************************************/
|
||||
/* image.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef IMAGE_H
|
||||
#define IMAGE_H
|
||||
|
||||
#include "core/color.h"
|
||||
#include "core/rect2.h"
|
||||
#include "core/string.h"
|
||||
#include "core/vector.h"
|
||||
#include <SDL.h>
|
||||
#include "core/math/rect2.h"
|
||||
#include "core/pool_vector.h"
|
||||
#include "core/resource.h"
|
||||
|
||||
//add an image loader registratin api -> so you can just pass a path in a load method and it will try to find a suitable loader.
|
||||
//remove sdl dependency
|
||||
//have everything implemented here
|
||||
//should have resizing and saving api -> thumbnail generation etc
|
||||
//Should be inherited from Reference
|
||||
/**
|
||||
* @author Juan Linietsky <reduzio@gmail.com>
|
||||
*
|
||||
* Image storage class. This is used to store an image in user memory, as well as
|
||||
* providing some basic methods for image manipulation.
|
||||
* Images can be loaded from a file, or registered into the Render object as textures.
|
||||
*/
|
||||
|
||||
class Image;
|
||||
|
||||
typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img);
|
||||
typedef PoolVector<uint8_t> (*SavePNGBufferFunc)(const Ref<Image> &p_img);
|
||||
typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size);
|
||||
|
||||
typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale);
|
||||
|
||||
class Image : public Resource {
|
||||
GDCLASS(Image, Resource);
|
||||
|
||||
class Image {
|
||||
public:
|
||||
void create(const Uint32 flags, const int width, const int height);
|
||||
static SavePNGFunc save_png_func;
|
||||
static SaveEXRFunc save_exr_func;
|
||||
static SavePNGBufferFunc save_png_buffer_func;
|
||||
|
||||
void enable_transparent_color(const Color &color);
|
||||
void disable_transparent_color();
|
||||
bool has_transparent_color();
|
||||
Color get_transparent_color();
|
||||
enum {
|
||||
MAX_WIDTH = 16384, // force a limit somehow
|
||||
MAX_HEIGHT = 16384 // force a limit somehow
|
||||
};
|
||||
|
||||
Color get_color_mod();
|
||||
void set_color_mod(const Color &color);
|
||||
enum Format {
|
||||
|
||||
Color get_alpha_mod();
|
||||
void set_alpha_mod(const Uint8 alpha);
|
||||
FORMAT_L8, //luminance
|
||||
FORMAT_LA8, //luminance-alpha
|
||||
FORMAT_R8,
|
||||
FORMAT_RG8,
|
||||
FORMAT_RGB8,
|
||||
FORMAT_RGBA8,
|
||||
FORMAT_RGBA4444,
|
||||
FORMAT_RGBA5551,
|
||||
FORMAT_RF, //float
|
||||
FORMAT_RGF,
|
||||
FORMAT_RGBF,
|
||||
FORMAT_RGBAF,
|
||||
FORMAT_RH, //half float
|
||||
FORMAT_RGH,
|
||||
FORMAT_RGBH,
|
||||
FORMAT_RGBAH,
|
||||
FORMAT_RGBE9995,
|
||||
FORMAT_DXT1, //s3tc bc1
|
||||
FORMAT_DXT3, //bc2
|
||||
FORMAT_DXT5, //bc3
|
||||
FORMAT_RGTC_R,
|
||||
FORMAT_RGTC_RG,
|
||||
FORMAT_BPTC_RGBA, //btpc bc7
|
||||
FORMAT_BPTC_RGBF, //float bc6h
|
||||
FORMAT_BPTC_RGBFU, //unsigned float bc6hu
|
||||
FORMAT_PVRTC2, //pvrtc
|
||||
FORMAT_PVRTC2A,
|
||||
FORMAT_PVRTC4,
|
||||
FORMAT_PVRTC4A,
|
||||
FORMAT_ETC, //etc1
|
||||
FORMAT_ETC2_R11, //etc2
|
||||
FORMAT_ETC2_R11S, //signed, NOT srgb.
|
||||
FORMAT_ETC2_RG11,
|
||||
FORMAT_ETC2_RG11S,
|
||||
FORMAT_ETC2_RGB8,
|
||||
FORMAT_ETC2_RGBA8,
|
||||
FORMAT_ETC2_RGB8A1,
|
||||
FORMAT_MAX
|
||||
};
|
||||
|
||||
SDL_BlendMode get_blend_mode();
|
||||
void set_blend_mode(const SDL_BlendMode mode);
|
||||
static const char *format_names[FORMAT_MAX];
|
||||
enum Interpolation {
|
||||
|
||||
Rect2 get_clip_rect();
|
||||
void set_clip_rect(const Rect2 &rect);
|
||||
INTERPOLATE_NEAREST,
|
||||
INTERPOLATE_BILINEAR,
|
||||
INTERPOLATE_CUBIC,
|
||||
INTERPOLATE_TRILINEAR,
|
||||
INTERPOLATE_LANCZOS,
|
||||
/* INTERPOLATE_TRICUBIC, */
|
||||
/* INTERPOLATE GAUSS */
|
||||
};
|
||||
|
||||
void duplicate(Image *into);
|
||||
enum CompressSource {
|
||||
COMPRESS_SOURCE_GENERIC,
|
||||
COMPRESS_SOURCE_SRGB,
|
||||
COMPRESS_SOURCE_NORMAL,
|
||||
COMPRESS_SOURCE_LAYERED,
|
||||
COMPRESS_SOURCE_MAX,
|
||||
};
|
||||
|
||||
void fill_rect(const Rect2 &rect, const Color &color);
|
||||
void fill_rects(const Vector<Rect2> &rects, const Color &color);
|
||||
void set_pixel(const int x, const int y, const Color &color);
|
||||
Color get_pixel(const int x, const int y);
|
||||
//some functions provided by something else
|
||||
|
||||
void blit_surface(const Image &source, const Rect2 &srcrect, const Rect2 &dstrect);
|
||||
static ImageMemLoadFunc _png_mem_loader_func;
|
||||
static ImageMemLoadFunc _jpg_mem_loader_func;
|
||||
static ImageMemLoadFunc _webp_mem_loader_func;
|
||||
static ImageMemLoadFunc _tga_mem_loader_func;
|
||||
static ImageMemLoadFunc _bmp_mem_loader_func;
|
||||
|
||||
static void (*_image_compress_bc_func)(Image *, float, CompressSource p_source);
|
||||
static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, CompressSource p_source);
|
||||
static void (*_image_compress_pvrtc2_func)(Image *);
|
||||
static void (*_image_compress_pvrtc4_func)(Image *);
|
||||
static void (*_image_compress_etc1_func)(Image *, float);
|
||||
static void (*_image_compress_etc2_func)(Image *, float, CompressSource p_source);
|
||||
|
||||
static void (*_image_decompress_pvrtc)(Image *);
|
||||
static void (*_image_decompress_bc)(Image *);
|
||||
static void (*_image_decompress_bptc)(Image *);
|
||||
static void (*_image_decompress_etc1)(Image *);
|
||||
static void (*_image_decompress_etc2)(Image *);
|
||||
|
||||
static PoolVector<uint8_t> (*webp_lossy_packer)(const Ref<Image> &p_image, float p_quality);
|
||||
static PoolVector<uint8_t> (*webp_lossless_packer)(const Ref<Image> &p_image);
|
||||
static Ref<Image> (*webp_unpacker)(const PoolVector<uint8_t> &p_buffer);
|
||||
static PoolVector<uint8_t> (*png_packer)(const Ref<Image> &p_image);
|
||||
static Ref<Image> (*png_unpacker)(const PoolVector<uint8_t> &p_buffer);
|
||||
|
||||
PoolVector<uint8_t>::Write write_lock;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
void _create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
|
||||
create(p_width, p_height, p_use_mipmaps, p_format);
|
||||
}
|
||||
|
||||
void _create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
|
||||
create(p_width, p_height, p_use_mipmaps, p_format, p_data);
|
||||
}
|
||||
|
||||
Format format;
|
||||
PoolVector<uint8_t> data;
|
||||
int width, height;
|
||||
bool mipmaps;
|
||||
|
||||
void _copy_internals_from(const Image &p_image) {
|
||||
format = p_image.format;
|
||||
width = p_image.width;
|
||||
height = p_image.height;
|
||||
mipmaps = p_image.mipmaps;
|
||||
data = p_image.data;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ void _get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const; //get where the mipmap begins in data
|
||||
|
||||
static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1);
|
||||
bool _can_modify(Format p_format) const;
|
||||
|
||||
_FORCE_INLINE_ void _put_pixelb(int p_x, int p_y, uint32_t p_pixel_size, uint8_t *p_data, const uint8_t *p_pixel);
|
||||
_FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixel_size, const uint8_t *p_data, uint8_t *p_pixel);
|
||||
|
||||
_FORCE_INLINE_ void _repeat_pixel_over_subsequent_memory(uint8_t *p_pixel, int p_pixel_size, int p_count);
|
||||
|
||||
void _set_data(const Dictionary &p_data);
|
||||
Dictionary _get_data() const;
|
||||
|
||||
Error _load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader);
|
||||
|
||||
static void average_4_uint8(uint8_t &p_out, const uint8_t &p_a, const uint8_t &p_b, const uint8_t &p_c, const uint8_t &p_d);
|
||||
static void average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d);
|
||||
static void average_4_half(uint16_t &p_out, const uint16_t &p_a, const uint16_t &p_b, const uint16_t &p_c, const uint16_t &p_d);
|
||||
static void average_4_rgbe9995(uint32_t &p_out, const uint32_t &p_a, const uint32_t &p_b, const uint32_t &p_c, const uint32_t &p_d);
|
||||
static void renormalize_uint8(uint8_t *p_rgb);
|
||||
static void renormalize_float(float *p_rgb);
|
||||
static void renormalize_half(uint16_t *p_rgb);
|
||||
static void renormalize_rgbe9995(uint32_t *p_rgb);
|
||||
|
||||
public:
|
||||
int get_width() const; ///< Get image width
|
||||
int get_height() const; ///< Get image height
|
||||
Vector2 get_size() const;
|
||||
bool has_mipmaps() const;
|
||||
int get_mipmap_count() const;
|
||||
|
||||
/**
|
||||
* Convert the image to another format, conversion only to raw byte format
|
||||
*/
|
||||
void convert(Format p_new_format);
|
||||
|
||||
/**
|
||||
* Get the current image format.
|
||||
*/
|
||||
Format get_format() const;
|
||||
|
||||
int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
|
||||
void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const; //get where the mipmap begins in data
|
||||
void get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const; //get where the mipmap begins in data
|
||||
|
||||
/**
|
||||
* Resize the image, using the preferred interpolation method.
|
||||
*/
|
||||
void resize_to_po2(bool p_square = false, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
|
||||
void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
|
||||
void shrink_x2();
|
||||
void expand_x2_hq2x();
|
||||
bool is_size_po2() const;
|
||||
/**
|
||||
* Crop the image to a specific size, if larger, then the image is filled by black
|
||||
*/
|
||||
void crop_from_point(int p_x, int p_y, int p_width, int p_height);
|
||||
void crop(int p_width, int p_height);
|
||||
|
||||
void flip_x();
|
||||
void flip_y();
|
||||
|
||||
/**
|
||||
* Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1)
|
||||
*/
|
||||
Error generate_mipmaps(bool p_renormalize = false);
|
||||
|
||||
void clear_mipmaps();
|
||||
void normalize(); //for normal maps
|
||||
|
||||
/**
|
||||
* Create a new image of a given size and format. Current image will be lost
|
||||
*/
|
||||
void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
|
||||
void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
|
||||
|
||||
void create(const char **p_xpm);
|
||||
/**
|
||||
* returns true when the image is empty (0,0) in size
|
||||
*/
|
||||
bool empty() const;
|
||||
|
||||
PoolVector<uint8_t> get_data() const;
|
||||
|
||||
Error load(const String &p_path);
|
||||
Error save_png(const String &p_path) const;
|
||||
PoolVector<uint8_t> save_png_to_buffer() const;
|
||||
Error save_exr(const String &p_path, bool p_grayscale) const;
|
||||
|
||||
/**
|
||||
* create an empty image
|
||||
*/
|
||||
Image();
|
||||
/**
|
||||
* create an empty image of a specific size and format
|
||||
*/
|
||||
Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
|
||||
/**
|
||||
* import an image of a specific size and format from a pointer
|
||||
*/
|
||||
Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
|
||||
|
||||
enum AlphaMode {
|
||||
ALPHA_NONE,
|
||||
ALPHA_BIT,
|
||||
ALPHA_BLEND
|
||||
};
|
||||
|
||||
AlphaMode detect_alpha() const;
|
||||
bool is_invisible() const;
|
||||
|
||||
static int get_format_pixel_size(Format p_format);
|
||||
static int get_format_pixel_rshift(Format p_format);
|
||||
static int get_format_block_size(Format p_format);
|
||||
static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h);
|
||||
|
||||
static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false);
|
||||
static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
|
||||
static int get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap);
|
||||
|
||||
enum CompressMode {
|
||||
COMPRESS_S3TC,
|
||||
COMPRESS_PVRTC2,
|
||||
COMPRESS_PVRTC4,
|
||||
COMPRESS_ETC,
|
||||
COMPRESS_ETC2,
|
||||
COMPRESS_BPTC,
|
||||
COMPRESS_MAX,
|
||||
};
|
||||
|
||||
Error compress(CompressMode p_mode = COMPRESS_S3TC, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7);
|
||||
Error decompress();
|
||||
bool is_compressed() const;
|
||||
|
||||
void fix_alpha_edges();
|
||||
void premultiply_alpha();
|
||||
void srgb_to_linear();
|
||||
void normalmap_to_xy();
|
||||
Ref<Image> rgbe_to_srgb();
|
||||
void bumpmap_to_normalmap(float bump_scale = 1.0);
|
||||
|
||||
void blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
|
||||
void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
|
||||
void blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
|
||||
void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
|
||||
void fill(const Color &p_color);
|
||||
void fill_rect(const Rect2 &p_rect, const Color &p_color);
|
||||
|
||||
Rect2 get_used_rect() const;
|
||||
Ref<Image> get_rect(const Rect2 &p_area) const;
|
||||
|
||||
static void set_compress_bc_func(void (*p_compress_func)(Image *, float, CompressSource));
|
||||
static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, CompressSource));
|
||||
static String get_format_name(Format p_format);
|
||||
|
||||
Error load_png_from_buffer(const PoolVector<uint8_t> &p_array);
|
||||
Error load_jpg_from_buffer(const PoolVector<uint8_t> &p_array);
|
||||
Error load_webp_from_buffer(const PoolVector<uint8_t> &p_array);
|
||||
Error load_tga_from_buffer(const PoolVector<uint8_t> &p_array);
|
||||
Error load_bmp_from_buffer(const PoolVector<uint8_t> &p_array);
|
||||
|
||||
Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
|
||||
Image(const char **p_xpm);
|
||||
|
||||
virtual Ref<Resource> duplicate(bool p_subresources = false) const;
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
|
||||
void free();
|
||||
//this is used for compression
|
||||
enum DetectChannels {
|
||||
DETECTED_L,
|
||||
DETECTED_LA,
|
||||
DETECTED_R,
|
||||
DETECTED_RG,
|
||||
DETECTED_RGB,
|
||||
DETECTED_RGBA,
|
||||
};
|
||||
|
||||
void load_bmp(const String &file_name);
|
||||
void save_bmp(const String &file_name);
|
||||
DetectChannels get_detected_channels();
|
||||
void optimize_channels();
|
||||
|
||||
Uint32 get_width() const;
|
||||
Uint32 get_height() const;
|
||||
Color get_pixelv(const Point2 &p_src) const;
|
||||
Color get_pixel(int p_x, int p_y) const;
|
||||
void set_pixelv(const Point2 &p_dst, const Color &p_color);
|
||||
void set_pixel(int p_x, int p_y, const Color &p_color);
|
||||
|
||||
SDL_Surface *get_surface();
|
||||
void set_surface(SDL_Surface *surface);
|
||||
void copy_internals_from(const Ref<Image> &p_image) {
|
||||
ERR_FAIL_COND_MSG(p_image.is_null(), "It's not a reference to a valid Image object.");
|
||||
format = p_image->format;
|
||||
width = p_image->width;
|
||||
height = p_image->height;
|
||||
mipmaps = p_image->mipmaps;
|
||||
data = p_image->data;
|
||||
}
|
||||
|
||||
Image();
|
||||
Image(const String &file_name);
|
||||
Image(SDL_Surface *surface);
|
||||
virtual ~Image();
|
||||
|
||||
private:
|
||||
SDL_Surface *_surface;
|
||||
~Image();
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST(Image::Format)
|
||||
VARIANT_ENUM_CAST(Image::Interpolation)
|
||||
VARIANT_ENUM_CAST(Image::CompressMode)
|
||||
VARIANT_ENUM_CAST(Image::CompressSource)
|
||||
VARIANT_ENUM_CAST(Image::AlphaMode)
|
||||
|
||||
#endif
|
@ -1,238 +0,0 @@
|
||||
#include "sprite.h"
|
||||
|
||||
#include "renderer.h"
|
||||
|
||||
Rect2 Sprite::get_texture_clip_rect() const {
|
||||
return _texture_clip_rect;
|
||||
}
|
||||
void Sprite::set_texture_clip_rect(const Rect2 &rect) {
|
||||
_texture_clip_rect = rect;
|
||||
}
|
||||
|
||||
Rect2 Sprite::get_transform() const {
|
||||
return _transform;
|
||||
}
|
||||
void Sprite::set_transform(const Rect2 &rect) {
|
||||
_transform = rect;
|
||||
}
|
||||
|
||||
float Sprite::get_x() const {
|
||||
return _transform.x;
|
||||
}
|
||||
void Sprite::set_x(const float val) {
|
||||
_transform.x = val;
|
||||
}
|
||||
|
||||
float Sprite::get_y() const {
|
||||
return _transform.y;
|
||||
}
|
||||
void Sprite::set_y(const float val) {
|
||||
_transform.y = val;
|
||||
}
|
||||
|
||||
float Sprite::get_w() const {
|
||||
return _transform.w;
|
||||
}
|
||||
void Sprite::set_w(const float val) {
|
||||
_transform.w = val;
|
||||
}
|
||||
|
||||
float Sprite::get_h() const {
|
||||
return _transform.h;
|
||||
}
|
||||
void Sprite::set_h(const float val) {
|
||||
_transform.h = val;
|
||||
}
|
||||
|
||||
double Sprite::get_angle() const {
|
||||
return _angle;
|
||||
}
|
||||
void Sprite::set_angle(const double val) {
|
||||
_angle = val;
|
||||
}
|
||||
|
||||
float Sprite::get_anchor_x() const {
|
||||
return _anchor_x;
|
||||
}
|
||||
void Sprite::set_anchor_x(const float val) {
|
||||
_anchor_x = val;
|
||||
}
|
||||
|
||||
float Sprite::get_anchor_y() const {
|
||||
return _anchor_y;
|
||||
}
|
||||
void Sprite::set_anchor_y(const float val) {
|
||||
_anchor_y = val;
|
||||
}
|
||||
|
||||
void Sprite::set_anchor(const float x, const float y) {
|
||||
_anchor_x = x;
|
||||
_anchor_y = y;
|
||||
}
|
||||
|
||||
SDL_RendererFlip Sprite::get_flip() const {
|
||||
return _flip;
|
||||
}
|
||||
void Sprite::set_flip(const SDL_RendererFlip val) {
|
||||
_flip = val;
|
||||
}
|
||||
|
||||
Color Sprite::get_color_mod() const {
|
||||
return _color_mod;
|
||||
}
|
||||
void Sprite::set_color_mod(const Color &color) {
|
||||
_color_mod = color;
|
||||
}
|
||||
|
||||
Texture *Sprite::get_texture() {
|
||||
return _texture;
|
||||
}
|
||||
Texture *Sprite::get_texture() const {
|
||||
return _texture;
|
||||
}
|
||||
void Sprite::set_texture(Texture *texture) {
|
||||
_texture = texture;
|
||||
}
|
||||
|
||||
void Sprite::draw() {
|
||||
Renderer::get_singleton()->draw_sprite(this);
|
||||
}
|
||||
|
||||
Sprite::Sprite() {
|
||||
_angle = 0;
|
||||
|
||||
_anchor_x = 0;
|
||||
_anchor_y = 0;
|
||||
|
||||
_flip = SDL_FLIP_NONE;
|
||||
|
||||
_texture = nullptr;
|
||||
|
||||
_color_mod = Color(255, 255, 255, 255);
|
||||
}
|
||||
Sprite::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::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::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::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::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::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);
|
||||
}
|
||||
Sprite::~Sprite() {
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
#ifndef SPRITE_H
|
||||
#define SPRITE_H
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
#include "core/rect2.h"
|
||||
#include "texture.h"
|
||||
#include "core/color.h"
|
||||
|
||||
//remove sdl dependency
|
||||
|
||||
class Sprite {
|
||||
public:
|
||||
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();
|
||||
|
||||
private:
|
||||
Rect2 _texture_clip_rect;
|
||||
Rect2 _transform;
|
||||
double _angle;
|
||||
|
||||
float _anchor_x;
|
||||
float _anchor_y;
|
||||
|
||||
SDL_RendererFlip _flip;
|
||||
|
||||
Color _color_mod;
|
||||
|
||||
Texture *_texture;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,166 +0,0 @@
|
||||
#include "texture.h"
|
||||
|
||||
#include "renderer.h"
|
||||
|
||||
Color Texture::get_color_mod() const {
|
||||
Uint8 r;
|
||||
Uint8 g;
|
||||
Uint8 b;
|
||||
Uint8 a;
|
||||
|
||||
SDL_GetTextureColorMod(_texture, &r, &g, &b);
|
||||
SDL_GetTextureAlphaMod(_texture, &a);
|
||||
|
||||
return Color(r, g, b, a);
|
||||
}
|
||||
void Texture::set_color_mod(const Color &color) {
|
||||
SDL_SetTextureColorMod(_texture, color.r, color.g, color.b);
|
||||
SDL_SetTextureAlphaMod(_texture, color.a);
|
||||
}
|
||||
|
||||
SDL_BlendMode Texture::get_blend_mode() const {
|
||||
SDL_BlendMode blendMode;
|
||||
|
||||
SDL_GetTextureBlendMode(_texture, &blendMode);
|
||||
|
||||
return blendMode;
|
||||
}
|
||||
void Texture::set_blend_mode(const SDL_BlendMode blend_mode) {
|
||||
SDL_SetTextureBlendMode(_texture, blend_mode);
|
||||
}
|
||||
|
||||
SDL_ScaleMode Texture::get_texture_scale_mode() const {
|
||||
SDL_ScaleMode scale_mode;
|
||||
|
||||
SDL_GetTextureScaleMode(_texture, &scale_mode);
|
||||
|
||||
return scale_mode;
|
||||
}
|
||||
void Texture::set_texture_scale_mode(const SDL_ScaleMode scale_mode) {
|
||||
SDL_SetTextureScaleMode(_texture, scale_mode);
|
||||
}
|
||||
|
||||
Image *Texture::get_image() {
|
||||
return _image;
|
||||
}
|
||||
void Texture::set_image(Image *image) {
|
||||
if (_texture) {
|
||||
free();
|
||||
}
|
||||
|
||||
_image = image;
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
int Texture::get_width() const {
|
||||
Uint32 format;
|
||||
int access;
|
||||
int w;
|
||||
int h;
|
||||
|
||||
if (SDL_QueryTexture(_texture, &format, &access, &w, &h)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return w;
|
||||
}
|
||||
int Texture::get_height() const {
|
||||
Uint32 format;
|
||||
int access;
|
||||
int w;
|
||||
int h;
|
||||
|
||||
if (SDL_QueryTexture(_texture, &format, &access, &w, &h)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
Uint32 Texture::get_format() const {
|
||||
Uint32 format;
|
||||
int access;
|
||||
int w;
|
||||
int h;
|
||||
|
||||
if (SDL_QueryTexture(_texture, &format, &access, &w, &h)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return format;
|
||||
}
|
||||
int Texture::get_access() const {
|
||||
Uint32 format;
|
||||
int access;
|
||||
int w;
|
||||
int h;
|
||||
|
||||
if (SDL_QueryTexture(_texture, &format, &access, &w, &h)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return access;
|
||||
}
|
||||
|
||||
void Texture::create(int access, int w, int h) {
|
||||
if (_texture) {
|
||||
free();
|
||||
}
|
||||
|
||||
_image = nullptr;
|
||||
|
||||
_texture = SDL_CreateTexture(Renderer::get_singleton()->get_renderer(), SDL_PIXELFORMAT_RGBA8888, access, w, h);
|
||||
}
|
||||
void Texture::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());
|
||||
}
|
||||
void Texture::free() {
|
||||
if (_texture) {
|
||||
SDL_DestroyTexture(_texture);
|
||||
|
||||
_texture = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Texture *Texture::get_texture() {
|
||||
return _texture;
|
||||
}
|
||||
SDL_Texture *Texture::get_texture() const {
|
||||
return _texture;
|
||||
}
|
||||
|
||||
bool Texture::is_render_target() {
|
||||
if (_texture == Renderer::get_singleton()->get_render_target()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Texture::Texture() {
|
||||
_image = nullptr;
|
||||
_texture = nullptr;
|
||||
}
|
||||
Texture::Texture(Image *image) {
|
||||
_image = nullptr;
|
||||
_texture = nullptr;
|
||||
|
||||
set_image(image);
|
||||
}
|
||||
Texture::~Texture() {
|
||||
if (_texture) {
|
||||
free();
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
#ifndef TEXTURE_H
|
||||
#define TEXTURE_H
|
||||
|
||||
#include "image.h"
|
||||
#include <SDL.h>
|
||||
|
||||
//remove sdl dependency
|
||||
//should be allocated with a selected renderer
|
||||
//could have an RID in them, and the renderer could look them up
|
||||
//should have a similar inheritance tree like godot's
|
||||
//Should be inherited from Reference
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
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();
|
||||
|
||||
private:
|
||||
Image *_image;
|
||||
SDL_Texture *_texture;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user