Implemented Texture.

This commit is contained in:
Relintai 2021-04-02 15:46:06 +02:00
parent af5e92d703
commit b5ddef635e
8 changed files with 266 additions and 11 deletions

View File

@ -14,7 +14,7 @@ fi
#g++ -Wall -g -c int_vector.cpp -o obj/int_vector.o
g++ -Wall -g -c math.cpp -o obj/math.o
g++ -Wall -g -c rect2.cpp -o obj/rect2.o
g++ -Wall -g $(sdl2-config --cflags) -c rect2.cpp -o obj/rect2.o
g++ -Wall -g -c color.cpp -o obj/color.o
g++ -Wall -g -c string.cpp -o obj/string.o

View File

@ -14,18 +14,24 @@ int main(int argv, char **args) {
i.set_pixel(i.get_height() - 1, i.get_width() - 1, Color(0, 0, 255, 255));
i.unlock();
/* old
SDL_Texture *t = SDL_CreateTextureFromSurface(r.get_renderer(), i.get_surface());
r.set_draw_color(0, 0, 0, 255);
r.clear();
SDL_Rect rs;
rs.x = 0;
rs.y = 0;
rs.w = i.get_width();
rs.h = i.get_height();
SDL_RenderCopy(r.get_renderer(), t, &rs, &rs);
*/
r.set_draw_color(0, 0, 0, 255);
r.clear();
Texture t(&i);
r.draw_texture(t, Rect2(0, 0, t.get_width(), t.get_height()));
r.present();
@ -55,8 +61,10 @@ int main(int argv, char **args) {
SDL_Delay(500);
}
SDL_DestroyTexture(t);
//old
//SDL_DestroyTexture(t);
t.free();
i.free();
r.destroy();

View File

@ -83,6 +83,28 @@ void Rect2::shrink(const float by) {
w -= by;
}
SDL_Rect Rect2::as_rect() const {
SDL_Rect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
return r;
}
SDL_FRect Rect2::as_frect() const {
SDL_FRect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
return r;
}
Rect2 &Rect2::operator+=(const Rect2 &b) {
x += b.x;
y += b.y;

View File

@ -1,6 +1,8 @@
#ifndef RECT2_H
#define RECT2_H
#include <SDL.h>
class Rect2 {
public:
float get_area() const;
@ -14,6 +16,9 @@ public:
void grow(const float by);
void shrink(const float by);
SDL_Rect as_rect() const;
SDL_FRect as_frect() const;
Rect2 &operator+=(const Rect2 &b);
Rect2 &operator-=(const Rect2 &b);

View File

@ -22,15 +22,41 @@ void Renderer::draw_rect(const SDL_Rect &rect) {
SDL_RenderFillRect(_renderer, &rect);
}
void Renderer::draw_rect(const Rect2 &rect) {
SDL_Rect r;
r.x = rect.x;
r.y = rect.y;
r.w = rect.w;
r.h = rect.h;
SDL_Rect r = rect.as_rect();
SDL_RenderFillRect(_renderer, &r);
}
void Renderer::draw_texture(const Texture &texture, const Rect2 &dst_rect) {
SDL_Rect sr;
sr.x = 0;
sr.y = 0;
sr.w = texture.get_width();
sr.h = texture.get_height();
SDL_Rect dr = dst_rect.as_rect();
SDL_RenderCopy(_renderer, texture.get_texture(), &sr, &dr);
}
void Renderer::draw_texture(const Texture &texture, const Rect2 &src_rect, const Rect2 &dst_rect) {
SDL_Rect sr = src_rect.as_rect();
SDL_Rect dr = dst_rect.as_rect();
SDL_RenderCopy(_renderer, texture.get_texture(), &sr, &dr);
}
void Renderer::draw_texture(const Texture &texture, const Rect2 &src_rect, const Rect2 &dst_rect, const double angle, const float cx, const float cy, const SDL_RendererFlip flip) {
SDL_Rect sr = src_rect.as_rect();
SDL_FRect dr = dst_rect.as_frect();
SDL_FPoint p;
p.x = cx;
p.y = cy;
SDL_RenderCopyExF(_renderer, texture.get_texture(), &sr, &dr, angle, &p, flip);
}
int Renderer::get_dpi() const {
float ddpi;
float hdpi;

View File

@ -3,6 +3,7 @@
#include "color.h"
#include "rect2.h"
#include "texture.h"
#include <SDL.h>
@ -16,6 +17,10 @@ public:
void draw_rect(const SDL_Rect &rect);
void draw_rect(const Rect2 &rect);
void draw_texture(const Texture &texture, const Rect2 &dst_rect);
void draw_texture(const Texture &texture, const Rect2 &src_rect, const Rect2 &dst_rect);
void draw_texture(const Texture &texture, const Rect2 &src_rect, const Rect2 &dst_rect, const double angle, const float cx = 0, const float cy = 0, const SDL_RendererFlip flip = SDL_FLIP_NONE);
int get_dpi() const;
int get_size_w() const;
int get_size_h() const;

View File

@ -1 +1,156 @@
#include "texture.h"
#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);
}
}
SDL_Texture *Texture::get_texture() {
return _texture;
}
SDL_Texture *Texture::get_texture() const {
return _texture;
}
Texture::Texture() {
_image = nullptr;
_texture = nullptr;
}
Texture::Texture(Image *image) {
_image = nullptr;
_texture = nullptr;
set_image(image);
}
Texture::~Texture() {
if (_texture) {
free();
}
}

View File

@ -1,8 +1,42 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#include "image.h"
#include <SDL.h>
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;
Texture();
Texture(Image *image);
virtual ~Texture();
private:
Image *_image;
SDL_Texture *_texture;
};
#endif