diff --git a/example/02_Nyan/Nyan.cpp b/example/02_Nyan/Nyan.cpp index 0e5aa98..410538c 100644 --- a/example/02_Nyan/Nyan.cpp +++ b/example/02_Nyan/Nyan.cpp @@ -31,7 +31,7 @@ class Nyan : public lm::GameState glEnable(GL_TEXTURE_2D); _image.loadFile("Nyan.png"); _sprite.setImage(_image); - _sprite.setScale(2); + _sprite.scale = {2, 2}; } void @@ -58,7 +58,11 @@ class Nyan : public lm::GameState void render() const { - _sprite.draw(0, 0); + lm::SpriteBatch sb; + + sb.begin(); + sb.draw(_sprite); + sb.end(); } private: diff --git a/example/03_Hello/Hello.cpp b/example/03_Hello/Hello.cpp index 425deb1..17e4772 100644 --- a/example/03_Hello/Hello.cpp +++ b/example/03_Hello/Hello.cpp @@ -11,12 +11,7 @@ /* */ /* ************************************************************************** */ -#include #include -#include -#include -#include -#include class Hello : public lm::GameState { @@ -48,12 +43,12 @@ class Hello : public lm::GameState _mario.linear(false); _sprite.setImage(_mario); _sprite.setAnimation(1, 3, 10); - _sprite.setScale(3); + _sprite.scale = {3, 3}; + _sprite.pos = {50, 50}; _script.loadFile("hello.mrb"); _script.run(); _script.call("hi"); _frame = 0; - _arr.push(0, 0, 1, 0.5, 1); } void @@ -78,13 +73,12 @@ class Hello : public lm::GameState for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) - sb.draw(_mario, i * 20 - 4, j * 20, 0, 2); + sb.draw(_sprite); } sb.end(); _font.printf(0, 50, "Hello, World! %d", _frame); _font.printf(0, 120, "x: %f", _x); _font.printf(0, 140, "y: %f", _y); - _sprite.draw(92, 92); } void @@ -96,13 +90,13 @@ class Hello : public lm::GameState if (event.key == lm::Key::Escape) lm::Core::get().stop(); else if (event.key == lm::Key::Left) - _sprite.flipX(true); + _sprite.flip.x = true; else if (event.key == lm::Key::Right) - _sprite.flipX(false); + _sprite.flip.x = false; else if (event.key == lm::Key::Up) - _sprite.flipY(true); + _sprite.flip.y = true; else if (event.key == lm::Key::Down) - _sprite.flipY(false); + _sprite.flip.y = false; } if (event.type == lm::Event::Type::LeftStick) { @@ -112,13 +106,12 @@ class Hello : public lm::GameState } private: - lm::Font _font; - lm::Image _mario; - lm::Sprite _sprite; - lm::Script _script; - lm::Sound _music; - lm::Sound _jump; - lm::VertexArrayc<256> _arr; + lm::Font _font; + lm::Image _mario; + lm::Sprite _sprite; + lm::Script _script; + lm::Sound _music; + lm::Sound _jump; int _frame; float _x; float _y; @@ -128,7 +121,6 @@ int main() { lm::Core core(200, 200, "Hello !"); - lm::SoundManager SoundManager; core.push(); core.start(); diff --git a/include/Lums/Lums.hpp b/include/Lums/Lums.hpp index 95674e0..8bf71ed 100644 --- a/include/Lums/Lums.hpp +++ b/include/Lums/Lums.hpp @@ -17,9 +17,9 @@ #define LUMS_STR2(str) #str #define LUMS_STR(str) LUMS_STR2(str) #define LUMS_VERSION_MAJOR 2 -#define LUMS_VERSION_MINOR 7 +#define LUMS_VERSION_MINOR 8 #define LUMS_VERSION_TEENY 0 -#define LUMS_VERSION_PATCH 2 +#define LUMS_VERSION_PATCH 0 #define LUMS_VERSION_NUMBER LUMS_STR(LUMS_VERSION_MAJOR) "." \ LUMS_STR(LUMS_VERSION_MINOR) "." \ diff --git a/include/Lums/Sprite.hpp b/include/Lums/Sprite.hpp index 4a0ffa2..ec6e9ce 100644 --- a/include/Lums/Sprite.hpp +++ b/include/Lums/Sprite.hpp @@ -16,10 +16,7 @@ #include #include - - - -#include +#include namespace lm { @@ -27,22 +24,7 @@ namespace lm { public: - Sprite() - : _image(nullptr) - , _w(0) - , _h(0) - , _baseImage(0) - , _currentImage(0) - , _length(1) - , _acc(0) - , _speed(0) - , _flipX(false) - , _flipY(false) - , _finished(false) - { - - } - + Sprite(); Sprite(Image& image, size_t state = 0); Image& @@ -63,6 +45,12 @@ namespace lm return _h; } + size_t + atlas() const + { + return _currentImage; + } + void setImage(Image& image) { @@ -89,40 +77,11 @@ namespace lm _speed = speed; } - void - setScale(double scale) - { - setScale(scale, scale); - } - - void - setScale(double scaleX, double scaleY) - { - _scaleX = scaleX; - _scaleY = scaleY; - updateTexCoord(); - } - void setImageInAtlas(size_t i) { _baseImage = _currentImage = i; _acc = 0; - updateTexCoord(); - } - - void - flipX(bool b) - { - _flipX = b; - updateTexCoord(); - } - - void - flipY(bool b) - { - _flipY = b; - updateTexCoord(); } bool @@ -132,30 +91,20 @@ namespace lm } LUMS_EXPORTED void update(); - LUMS_EXPORTED void draw(int x, int y) const; - void - draw() const - { - draw(0, 0); - } + lm::Vector2f pos; + lm::Vector2f scale; + lm::Vector2b flip; private: - void updateTexCoord(); - Image* _image; int _w; int _h; - double _scaleX; - double _scaleY; size_t _baseImage; size_t _currentImage; size_t _length; size_t _acc; size_t _speed; - GLdouble _vertex[16]; - bool _flipX; - bool _flipY; bool _loop; bool _finished; diff --git a/include/Lums/SpriteBatch.hpp b/include/Lums/SpriteBatch.hpp index fc50843..a5302c7 100644 --- a/include/Lums/SpriteBatch.hpp +++ b/include/Lums/SpriteBatch.hpp @@ -28,20 +28,14 @@ namespace lm public: SpriteBatch(); void begin(); - - void - draw(const Image& image) - { - draw(image, 0, 0); - } + void draw(const Image& image, int atlas = 0, lm::Vector2f pos = {0, 0}, lm::Vector2f scale = {1, 1}, lm::Vector2b flip = {false, false}); void - draw(const Image& image, float x, float y, int atlas = 0, float scale = 1) + draw(const Sprite& sprite) { - draw(image, x, y, atlas, scale, scale); + draw(sprite.image(), sprite.atlas(), sprite.pos, sprite.scale, sprite.flip); } - void draw(const Image& image, float x, float y, int atlas, float scaleX, float scaleY); void end(); ~SpriteBatch(); diff --git a/include/Lums/Vector2.hpp b/include/Lums/Vector2.hpp index 89c23dc..8ab60ca 100644 --- a/include/Lums/Vector2.hpp +++ b/include/Lums/Vector2.hpp @@ -325,6 +325,37 @@ namespace lm */ T y; }; + + template <> + struct Vector2 + { + constexpr + Vector2() + : x(false) + , y(false) + { + + } + + constexpr + Vector2(bool x, bool y) + : x(x) + , y(y) + { + + } + + constexpr + Vector2(const Vector2& rhs) + : x(rhs.x) + , y(rhs.y) + { + + } + + bool x:1; + bool y:1; + }; /** * An alias for Vector2 @@ -345,6 +376,11 @@ namespace lm * An alias for Vector2 */ typedef Vector2 Vector2a; + + /** + * An anias for Vector2 + */ + typedef Vector2 Vector2b; } #endif \ No newline at end of file diff --git a/src/Sprite.cpp b/src/Sprite.cpp index 16f3319..4ae39ec 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -17,6 +17,20 @@ using namespace lm; +Sprite::Sprite() +: _image(nullptr) +, _w(0) +, _h(0) +, _baseImage(0) +, _currentImage(0) +, _length(1) +, _acc(0) +, _speed(0) +, _finished(false) +{ + +} + Sprite::Sprite(Image& image, size_t state) : _image(&image) , _w(image.iwidth()) @@ -26,10 +40,8 @@ Sprite::Sprite(Image& image, size_t state) , _length(1) , _acc(0) , _speed(0) -, _flipX(false) -, _flipY(false) { - updateTexCoord(); + } void @@ -50,69 +62,5 @@ Sprite::update() } else _currentImage++; - updateTexCoord(); - } -} - -void -Sprite::draw(int x, int y) const -{ - glPushMatrix(); - glEnable(GL_TEXTURE_2D); - glTranslated(x, y, 0); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glVertexPointer(2, GL_DOUBLE, sizeof(GLdouble) * 4, _vertex); - glTexCoordPointer(2, GL_DOUBLE, sizeof(GLdouble) * 4, _vertex + 2); - _image->bind(); - glDrawArrays(GL_QUADS, 0, 4); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - glDisable(GL_TEXTURE_2D); - glPopMatrix(); -} - -/* PRIVATE */ - -void -Sprite::updateTexCoord() -{ - FrameDescriptorf tex = _image->atlasAt(_currentImage); - _w = tex.w * _image->width() * _scaleX; - _h = tex.h * _image->height() * _scaleY; - - double dw = _w; - double dh = _h; - double x = _flipX ? 0 : tex.offX; - double y = _flipY ? 0 : tex.offY; - - if (_flipX) - { - tex.x += tex.w; - tex.w = -tex.w; - } - - if (_flipY) - { - tex.y += tex.h; - tex.h = -tex.h; } - - _vertex[0] = x; - _vertex[1] = y; - _vertex[2] = tex.x; - _vertex[3] = tex.y; - _vertex[4] = x + dw; - _vertex[5] = y; - _vertex[6] = tex.x + tex.w; - _vertex[7] = tex.y; - _vertex[8] = x + dw; - _vertex[9] = y + dh; - _vertex[10] = tex.x + tex.w; - _vertex[11] = tex.y + tex.h; - _vertex[12] = x; - _vertex[13] = y + dh; - _vertex[14] = tex.x; - _vertex[15] = tex.y + tex.h; } - diff --git a/src/SpriteBatch.cpp b/src/SpriteBatch.cpp index 0f013d0..1f444e0 100644 --- a/src/SpriteBatch.cpp +++ b/src/SpriteBatch.cpp @@ -12,7 +12,6 @@ /* ************************************************************************** */ #include -#include using namespace lm; @@ -30,7 +29,7 @@ SpriteBatch::begin() } void -SpriteBatch::draw(const Image& image, float x, float y, int atlas, float scaleX, float scaleY) +SpriteBatch::draw(const Image& image, int atlas, lm::Vector2f pos, lm::Vector2f scale, lm::Vector2b flip) { if (!_texture) { @@ -47,15 +46,29 @@ SpriteBatch::draw(const Image& image, float x, float y, int atlas, float scaleX, } FrameDescriptorf frame = image.atlasAt(atlas); - float w = frame.w * image.width() * scaleX; - float h = frame.h * image.height() * scaleY; + float w = frame.w * image.width() * scale.x; + float h = frame.h * image.height() * scale.y; - x += frame.offX; - y += frame.offY; - _va.push(x, y, 1.0f, 1.0f, 1.0f, frame.x, frame.y); - _va.push(x + w, y, 1.0f, 1.0f, 1.0f, frame.x + frame.w, frame.y); - _va.push(x + w, y + h, 1.0f, 1.0f, 1.0f, frame.x + frame.w, frame.y + frame.h); - _va.push(x, y + h, 1.0f, 1.0f, 1.0f, frame.x, frame.y + frame.h); + if (flip.x) + { + pos.x += w; + w = -w; + } + else + pos.x += frame.offX; + + if (flip.y) + { + pos.y += h; + h = -h; + } + else + pos.y += frame.offY; + + _va.push(pos.x, pos.y, 1.0f, 1.0f, 1.0f, frame.x, frame.y); + _va.push(pos.x + w, pos.y, 1.0f, 1.0f, 1.0f, frame.x + frame.w, frame.y); + _va.push(pos.x + w, pos.y + h, 1.0f, 1.0f, 1.0f, frame.x + frame.w, frame.y + frame.h); + _va.push(pos.x, pos.y + h, 1.0f, 1.0f, 1.0f, frame.x, frame.y + frame.h); _count++; } @@ -77,8 +90,6 @@ SpriteBatch::~SpriteBatch() void SpriteBatch::flush() { - //std::cout << "Hi" << std::endl; - //glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, _texture); _va.draw(GL_QUADS); _va.clear();