Skip to content

Commit 511818c

Browse files
author
Maxime BACOUX
committed
Lums 2.8, new Batch API
1 parent 435bcda commit 511818c

8 files changed

Lines changed: 109 additions & 175 deletions

File tree

example/02_Nyan/Nyan.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Nyan : public lm::GameState
3131
glEnable(GL_TEXTURE_2D);
3232
_image.loadFile("Nyan.png");
3333
_sprite.setImage(_image);
34-
_sprite.setScale(2);
34+
_sprite.scale = {2, 2};
3535
}
3636

3737
void
@@ -58,7 +58,11 @@ class Nyan : public lm::GameState
5858
void
5959
render() const
6060
{
61-
_sprite.draw(0, 0);
61+
lm::SpriteBatch sb;
62+
63+
sb.begin();
64+
sb.draw(_sprite);
65+
sb.end();
6266
}
6367

6468
private:

example/03_Hello/Hello.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111
/* */
1212
/* ************************************************************************** */
1313

14-
#include <iostream>
1514
#include <Lums/Lums.hpp>
16-
#include <Lums/Script.hpp>
17-
#include <Lums/SoundManager.hpp>
18-
#include <Lums/Sound.hpp>
19-
#include <iostream>
2015

2116
class Hello : public lm::GameState
2217
{
@@ -48,12 +43,12 @@ class Hello : public lm::GameState
4843
_mario.linear(false);
4944
_sprite.setImage(_mario);
5045
_sprite.setAnimation(1, 3, 10);
51-
_sprite.setScale(3);
46+
_sprite.scale = {3, 3};
47+
_sprite.pos = {50, 50};
5248
_script.loadFile("hello.mrb");
5349
_script.run();
5450
_script.call("hi");
5551
_frame = 0;
56-
_arr.push(0, 0, 1, 0.5, 1);
5752
}
5853

5954
void
@@ -78,13 +73,12 @@ class Hello : public lm::GameState
7873
for (int i = 0; i < 20; i++)
7974
{
8075
for (int j = 0; j < 20; j++)
81-
sb.draw(_mario, i * 20 - 4, j * 20, 0, 2);
76+
sb.draw(_sprite);
8277
}
8378
sb.end();
8479
_font.printf(0, 50, "Hello, World! %d", _frame);
8580
_font.printf(0, 120, "x: %f", _x);
8681
_font.printf(0, 140, "y: %f", _y);
87-
_sprite.draw(92, 92);
8882
}
8983

9084
void
@@ -96,13 +90,13 @@ class Hello : public lm::GameState
9690
if (event.key == lm::Key::Escape)
9791
lm::Core::get().stop();
9892
else if (event.key == lm::Key::Left)
99-
_sprite.flipX(true);
93+
_sprite.flip.x = true;
10094
else if (event.key == lm::Key::Right)
101-
_sprite.flipX(false);
95+
_sprite.flip.x = false;
10296
else if (event.key == lm::Key::Up)
103-
_sprite.flipY(true);
97+
_sprite.flip.y = true;
10498
else if (event.key == lm::Key::Down)
105-
_sprite.flipY(false);
99+
_sprite.flip.y = false;
106100
}
107101
if (event.type == lm::Event::Type::LeftStick)
108102
{
@@ -112,13 +106,12 @@ class Hello : public lm::GameState
112106
}
113107

114108
private:
115-
lm::Font _font;
116-
lm::Image _mario;
117-
lm::Sprite _sprite;
118-
lm::Script _script;
119-
lm::Sound _music;
120-
lm::Sound _jump;
121-
lm::VertexArrayc<256> _arr;
109+
lm::Font _font;
110+
lm::Image _mario;
111+
lm::Sprite _sprite;
112+
lm::Script _script;
113+
lm::Sound _music;
114+
lm::Sound _jump;
122115
int _frame;
123116
float _x;
124117
float _y;
@@ -128,7 +121,6 @@ int
128121
main()
129122
{
130123
lm::Core core(200, 200, "Hello !");
131-
lm::SoundManager SoundManager;
132124

133125
core.push<Hello>();
134126
core.start();

include/Lums/Lums.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#define LUMS_STR2(str) #str
1818
#define LUMS_STR(str) LUMS_STR2(str)
1919
#define LUMS_VERSION_MAJOR 2
20-
#define LUMS_VERSION_MINOR 7
20+
#define LUMS_VERSION_MINOR 8
2121
#define LUMS_VERSION_TEENY 0
22-
#define LUMS_VERSION_PATCH 2
22+
#define LUMS_VERSION_PATCH 0
2323

2424
#define LUMS_VERSION_NUMBER LUMS_STR(LUMS_VERSION_MAJOR) "." \
2525
LUMS_STR(LUMS_VERSION_MINOR) "." \

include/Lums/Sprite.hpp

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,15 @@
1616

1717
#include <Lums/Image.hpp>
1818
#include <Lums/ExportDll.hpp>
19-
20-
21-
22-
#include <iostream>
19+
#include <Lums/Vector2.hpp>
2320

2421
namespace lm
2522
{
2623
class Sprite
2724
{
2825
public:
2926

30-
Sprite()
31-
: _image(nullptr)
32-
, _w(0)
33-
, _h(0)
34-
, _baseImage(0)
35-
, _currentImage(0)
36-
, _length(1)
37-
, _acc(0)
38-
, _speed(0)
39-
, _flipX(false)
40-
, _flipY(false)
41-
, _finished(false)
42-
{
43-
44-
}
45-
27+
Sprite();
4628
Sprite(Image& image, size_t state = 0);
4729

4830
Image&
@@ -63,6 +45,12 @@ namespace lm
6345
return _h;
6446
}
6547

48+
size_t
49+
atlas() const
50+
{
51+
return _currentImage;
52+
}
53+
6654
void
6755
setImage(Image& image)
6856
{
@@ -89,40 +77,11 @@ namespace lm
8977
_speed = speed;
9078
}
9179

92-
void
93-
setScale(double scale)
94-
{
95-
setScale(scale, scale);
96-
}
97-
98-
void
99-
setScale(double scaleX, double scaleY)
100-
{
101-
_scaleX = scaleX;
102-
_scaleY = scaleY;
103-
updateTexCoord();
104-
}
105-
10680
void
10781
setImageInAtlas(size_t i)
10882
{
10983
_baseImage = _currentImage = i;
11084
_acc = 0;
111-
updateTexCoord();
112-
}
113-
114-
void
115-
flipX(bool b)
116-
{
117-
_flipX = b;
118-
updateTexCoord();
119-
}
120-
121-
void
122-
flipY(bool b)
123-
{
124-
_flipY = b;
125-
updateTexCoord();
12685
}
12786

12887
bool
@@ -132,30 +91,20 @@ namespace lm
13291
}
13392

13493
LUMS_EXPORTED void update();
135-
LUMS_EXPORTED void draw(int x, int y) const;
13694

137-
void
138-
draw() const
139-
{
140-
draw(0, 0);
141-
}
95+
lm::Vector2f pos;
96+
lm::Vector2f scale;
97+
lm::Vector2b flip;
14298

14399
private:
144-
void updateTexCoord();
145-
146100
Image* _image;
147101
int _w;
148102
int _h;
149-
double _scaleX;
150-
double _scaleY;
151103
size_t _baseImage;
152104
size_t _currentImage;
153105
size_t _length;
154106
size_t _acc;
155107
size_t _speed;
156-
GLdouble _vertex[16];
157-
bool _flipX;
158-
bool _flipY;
159108
bool _loop;
160109
bool _finished;
161110

include/Lums/SpriteBatch.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,14 @@ namespace lm
2828
public:
2929
SpriteBatch();
3030
void begin();
31-
32-
void
33-
draw(const Image& image)
34-
{
35-
draw(image, 0, 0);
36-
}
31+
void draw(const Image& image, int atlas = 0, lm::Vector2f pos = {0, 0}, lm::Vector2f scale = {1, 1}, lm::Vector2b flip = {false, false});
3732

3833
void
39-
draw(const Image& image, float x, float y, int atlas = 0, float scale = 1)
34+
draw(const Sprite& sprite)
4035
{
41-
draw(image, x, y, atlas, scale, scale);
36+
draw(sprite.image(), sprite.atlas(), sprite.pos, sprite.scale, sprite.flip);
4237
}
4338

44-
void draw(const Image& image, float x, float y, int atlas, float scaleX, float scaleY);
4539
void end();
4640
~SpriteBatch();
4741

include/Lums/Vector2.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,37 @@ namespace lm
325325
*/
326326
T y;
327327
};
328+
329+
template <>
330+
struct Vector2<bool>
331+
{
332+
constexpr
333+
Vector2()
334+
: x(false)
335+
, y(false)
336+
{
337+
338+
}
339+
340+
constexpr
341+
Vector2(bool x, bool y)
342+
: x(x)
343+
, y(y)
344+
{
345+
346+
}
347+
348+
constexpr
349+
Vector2(const Vector2<bool>& rhs)
350+
: x(rhs.x)
351+
, y(rhs.y)
352+
{
353+
354+
}
355+
356+
bool x:1;
357+
bool y:1;
358+
};
328359

329360
/**
330361
* An alias for Vector2<int>
@@ -345,6 +376,11 @@ namespace lm
345376
* An alias for Vector2<Angle>
346377
*/
347378
typedef Vector2<Angle> Vector2a;
379+
380+
/**
381+
* An anias for Vector2<bool>
382+
*/
383+
typedef Vector2<bool> Vector2b;
348384
}
349385

350386
#endif

0 commit comments

Comments
 (0)