From 8023d607f128ef4b4e63b833fc2045f7b41a1e3f Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 31 Dec 2016 09:23:28 +0100 Subject: SFML sprites! --- TODO | 2 ++ library.cpp | 3 --- library.h | 1 + object_base.cpp | 10 ++++------ sprite.cpp | 5 ++++- sprite.h | 5 +++-- 6 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 TODO diff --git a/TODO b/TODO new file mode 100644 index 0000000..cb8c0ff --- /dev/null +++ b/TODO @@ -0,0 +1,2 @@ +- Group textures together: + http://www.sfml-dev.org/tutorials/2.4/graphics-sprite.php#the-importance-of-using-as-few-textures-as-possible diff --git a/library.cpp b/library.cpp index 12a9077..38bbf48 100644 --- a/library.cpp +++ b/library.cpp @@ -9,9 +9,6 @@ using namespace std; -extern sf::RenderWindow window; - - static const Font *currentFont=nullptr; static sf::Text sharedSfText; diff --git a/library.h b/library.h index af2eedc..e3e87c3 100644 --- a/library.h +++ b/library.h @@ -9,6 +9,7 @@ using namespace std; extern Global global; +extern sf::RenderWindow window; template diff --git a/object_base.cpp b/object_base.cpp index ce97f2a..d04d0f5 100644 --- a/object_base.cpp +++ b/object_base.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "object_base.h" #include "library.h" @@ -21,12 +22,9 @@ void Object::destroy(void){} void Object::step(void){} void Object::draw(void){ - /*if(!sprite_index){ + if(!sprite_index){ return; } - if(!sprite_index->fl_image){ - sprite_index->fl_image=new Fl_PNG_Image(nullptr,sprite_index->image,sprite_index->image_len); - assert(!sprite_index->fl_image->fail()); - } - sprite_index->fl_image->draw((int)x,(int)y);*/ + sprite_index->sf_sprite.setPosition(sf::Vector2f(x,y)); + window.draw(sprite_index->sf_sprite); } diff --git a/sprite.cpp b/sprite.cpp index e3503ea..260c54f 100644 --- a/sprite.cpp +++ b/sprite.cpp @@ -2,4 +2,7 @@ Sprite::Sprite(const unsigned char *image,unsigned int image_len) - :image(image),image_len(image_len)/*,fl_image(nullptr)*/{} + :image(image),image_len(image_len){ + sf_texture.loadFromMemory(image,image_len); + sf_sprite.setTexture(sf_texture); +} diff --git a/sprite.h b/sprite.h index b52026a..b5d9e7d 100644 --- a/sprite.h +++ b/sprite.h @@ -1,13 +1,14 @@ #pragma once -//#include +#include class Sprite{ public: const unsigned char *image; unsigned int image_len; - //Fl_PNG_Image *fl_image; + sf::Texture sf_texture; + sf::Sprite sf_sprite; Sprite(const unsigned char *image,unsigned int image_len); }; -- cgit v1.2.3-70-g09d2