Skip to content

Commit

Permalink
Lums 2.8, new Batch API
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime BACOUX committed Apr 8, 2015
1 parent 435bcda commit 511818c
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 175 deletions.
8 changes: 6 additions & 2 deletions example/02_Nyan/Nyan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
34 changes: 13 additions & 21 deletions example/03_Hello/Hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
/* */
/* ************************************************************************** */

#include <iostream>
#include <Lums/Lums.hpp>
#include <Lums/Script.hpp>
#include <Lums/SoundManager.hpp>
#include <Lums/Sound.hpp>
#include <iostream>

class Hello : public lm::GameState
{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
{
Expand All @@ -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;
Expand All @@ -128,7 +121,6 @@ int
main()
{
lm::Core core(200, 200, "Hello !");
lm::SoundManager SoundManager;

core.push<Hello>();
core.start();
Expand Down
4 changes: 2 additions & 2 deletions include/Lums/Lums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) "." \
Expand Down
73 changes: 11 additions & 62 deletions include/Lums/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,15 @@

#include <Lums/Image.hpp>
#include <Lums/ExportDll.hpp>



#include <iostream>
#include <Lums/Vector2.hpp>

namespace lm
{
class Sprite
{
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&
Expand All @@ -63,6 +45,12 @@ namespace lm
return _h;
}

size_t
atlas() const
{
return _currentImage;
}

void
setImage(Image& image)
{
Expand All @@ -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
Expand All @@ -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;

Expand Down
12 changes: 3 additions & 9 deletions include/Lums/SpriteBatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
36 changes: 36 additions & 0 deletions include/Lums/Vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,37 @@ namespace lm
*/
T y;
};

template <>
struct Vector2<bool>
{
constexpr
Vector2()
: x(false)
, y(false)
{

}

constexpr
Vector2(bool x, bool y)
: x(x)
, y(y)
{

}

constexpr
Vector2(const Vector2<bool>& rhs)
: x(rhs.x)
, y(rhs.y)
{

}

bool x:1;
bool y:1;
};

/**
* An alias for Vector2<int>
Expand All @@ -345,6 +376,11 @@ namespace lm
* An alias for Vector2<Angle>
*/
typedef Vector2<Angle> Vector2a;

/**
* An anias for Vector2<bool>
*/
typedef Vector2<bool> Vector2b;
}

#endif
Loading

0 comments on commit 511818c

Please sign in to comment.