aboutsummaryrefslogtreecommitdiff
path: root/src/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics')
-rw-r--r--src/graphics/Shader.cpp4
-rw-r--r--src/graphics/Sprite.cpp19
-rw-r--r--src/graphics/Text.cpp12
-rw-r--r--src/graphics/Texture.cpp55
4 files changed, 74 insertions, 16 deletions
diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp
index 2bdc9b2..111f39d 100644
--- a/src/graphics/Shader.cpp
+++ b/src/graphics/Shader.cpp
@@ -34,6 +34,10 @@ namespace mgl {
return mgl_shader_program_set_uniform_vec2f(&shader_program, name, { value.x, value.y }) == 0;
}
+ bool Shader::set_uniform(const char *name, Color color) {
+ return mgl_shader_program_set_uniform_color(&shader_program, name, { color.r, color.g, color.b, color.a }) == 0;
+ }
+
bool Shader::is_valid() const {
return shader_program.id != 0;
}
diff --git a/src/graphics/Sprite.cpp b/src/graphics/Sprite.cpp
index 461b14c..4d5d181 100644
--- a/src/graphics/Sprite.cpp
+++ b/src/graphics/Sprite.cpp
@@ -35,10 +35,8 @@ namespace mgl {
}
vec2f Sprite::get_size() const {
- if(texture)
- return get_scale() * mgl::vec2f(texture->get_size().x, texture->get_size().y);
- else
- return { 0.0f, 0.0f };
+ const mgl_vec2f v = mgl_sprite_get_size(&sprite);
+ return { v.x, v.y };
}
void Sprite::set_scale(vec2f scale) {
@@ -50,10 +48,15 @@ namespace mgl {
}
void Sprite::set_size(vec2f size) {
- if(texture)
- set_scale({ size.x / (float)texture->get_size().x, size.y / (float)texture->get_size().y });
- else
- set_scale({ 0.0f, 0.0f });
+ mgl_sprite_set_size(&sprite, *(mgl_vec2f*)&size);
+ }
+
+ void Sprite::set_width(float width) {
+ mgl_sprite_set_width(&sprite, width);
+ }
+
+ void Sprite::set_height(float height) {
+ mgl_sprite_set_height(&sprite, height);
}
void Sprite::set_rotation(float degrees) {
diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp
index a30cdf3..e138a28 100644
--- a/src/graphics/Text.cpp
+++ b/src/graphics/Text.cpp
@@ -47,12 +47,20 @@ namespace mgl {
mgl_text_set_position(&text, {position.x, position.y});
}
+ vec2f Text::get_position() const {
+ return { text.position.x, text.position.y };
+ }
+
void Text::set_color(Color color) {
mgl_text_set_color(&text, {color.r, color.g, color.b, color.a});
}
- vec2f Text::get_position() const {
- return { text.position.x, text.position.y };
+ void Text::set_max_width(float max_width) {
+ mgl_text_set_max_width(&text, max_width);
+ }
+
+ void Text::set_max_rows(unsigned int max_rows) {
+ mgl_text_set_max_rows(&text, max_rows);
}
FloatRect Text::get_bounds() {
diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp
index 86c4ac0..3b1f410 100644
--- a/src/graphics/Texture.cpp
+++ b/src/graphics/Texture.cpp
@@ -7,6 +7,14 @@ namespace mgl {
memset(&texture, 0, sizeof(mgl_texture));
}
+ Texture::Texture(unsigned int gl_texture_id, mgl_texture_format format, const ReferenceOptions reference_options) {
+ memset(&texture, 0, sizeof(mgl_texture));
+ const mgl_texture_reference_options texture_reference_options = {
+ reference_options.pixel_coordinates
+ };
+ mgl_texture_init_reference_existing_gl_texture(&texture, gl_texture_id, format, &texture_reference_options);
+ }
+
Texture::Texture(Texture &&other) {
memcpy(&texture, &other.texture, sizeof(mgl_texture));
owned = other.owned;
@@ -47,11 +55,18 @@ namespace mgl {
if(mgl_texture_init(&texture) != 0)
return false;
- mgl_texture_load_options texture_load_options = {
+ const mgl_texture_load_options texture_load_options = {
load_options.compressed,
- load_options.pixel_coordinates
+ load_options.pixel_coordinates,
+ load_options.mipmap
};
- return mgl_texture_load_from_file(&texture, filepath, &texture_load_options) == 0;
+
+ if(mgl_texture_load_from_file(&texture, filepath, &texture_load_options) == 0) {
+ return true;
+ } else {
+ clear();
+ return false;
+ }
}
bool Texture::load_from_image(Image &image, const LoadOptions load_options) {
@@ -61,11 +76,39 @@ namespace mgl {
if(mgl_texture_init(&texture) != 0)
return false;
- mgl_texture_load_options texture_load_options = {
+ const mgl_texture_load_options texture_load_options = {
+ load_options.compressed,
+ load_options.pixel_coordinates,
+ load_options.mipmap
+ };
+
+ if(mgl_texture_load_from_image(&texture, image.internal_image(), &texture_load_options) == 0) {
+ return true;
+ } else {
+ clear();
+ return false;
+ }
+ }
+
+ bool Texture::load_from_memory(const unsigned char *data, int width, int height, mgl_image_format format, LoadOptions load_options) {
+ if(texture.id)
+ clear();
+
+ if(mgl_texture_init(&texture) != 0)
+ return false;
+
+ const mgl_texture_load_options texture_load_options = {
load_options.compressed,
- load_options.pixel_coordinates
+ load_options.pixel_coordinates,
+ load_options.mipmap
};
- return mgl_texture_load_from_image(&texture, image.internal_image(), &texture_load_options) == 0;
+
+ if(mgl_texture_load_from_memory(&texture, data, width, height, format, &texture_load_options) == 0) {
+ return true;
+ } else {
+ clear();
+ return false;
+ }
}
void Texture::clear() {