Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Pong sample

Added window_options
Modified glm to be available in xd namespace
  • Loading branch information...
commit adcd0532cddea59667dc2c463e5430803b950318 1 parent 02e4235
@rekotiira authored
Showing with 858 additions and 140 deletions.
  1. +6 −1 .gitignore
  2. +21 −0 include/xd/detail/iterate_entity_constructor.hpp
  3. +20 −2 include/xd/entity.hpp
  4. +12 −0 include/xd/glm.hpp
  5. +2 −2 include/xd/graphics.hpp
  6. +1 −1  include/xd/graphics/detail/vertex_traits.hpp
  7. +6 −6 include/xd/graphics/font.hpp
  8. +2 −1  include/xd/graphics/matrix_stack.hpp
  9. +6 −2 include/xd/graphics/shader_program.hpp
  10. +16 −17 include/xd/graphics/sprite_batch.hpp
  11. +1 −1  include/xd/graphics/stock_text_formatter.hpp
  12. +5 −5 include/xd/graphics/text_formatter.hpp
  13. +18 −3 include/xd/graphics/types.hpp
  14. +2 −0  include/xd/lua.hpp
  15. +0 −1  include/xd/lua/config.hpp
  16. +1 −0  include/xd/system.hpp
  17. +7 −3 include/xd/system/window.hpp
  18. +31 −2 include/xd/system/window_options.hpp
  19. BIN  samples/pong/font.otf
  20. +12 −0 samples/pong/main.cpp
  21. +354 −0 samples/pong/pong.cpp
  22. +65 −0 samples/pong/pong.hpp
  23. BIN  samples/pong/texture.png
  24. +1 −1  src/graphics/shader_program.cpp
  25. +2 −1  src/graphics/shaders.cpp
  26. +54 −44 src/graphics/sprite_batch.cpp
  27. +19 −11 src/system/window.cpp
  28. +37 −35 src/test/test.cpp
  29. +2 −0  win/vc10/common/common.vcxproj
  30. +6 −0 win/vc10/common/common.vcxproj.filters
  31. +1 −1  win/vc10/lua/lua.vcxproj
  32. +90 −0 win/vc10/samples/pong/pong.vcxproj
  33. +30 −0 win/vc10/samples/pong/pong.vcxproj.filters
  34. +8 −0 win/vc10/samples/pong/pong.vcxproj.user
  35. +20 −0 win/vc10/samples/samples.sln
View
7 .gitignore
@@ -13,4 +13,9 @@
/win/vc10/lua/Debug
/win/vc10/test/Debug
/win/vc10/common/Debug
-/win/vc10/system/Debug
+/win/vc10/system/Debug
+/win/vc10/samples/pong/Debug
+/win/vc10/samples/pong/Release
+/win/vc10/samples/Release
+/win/vc10/samples/Debug
+/win/vc10/samples/ipch
View
21 include/xd/detail/iterate_entity_constructor.hpp
@@ -0,0 +1,21 @@
+#if !BOOST_PP_IS_ITERATING
+# error "do not include this file directly"
+#endif
+
+#include <utility>
+#include <boost/preprocessor/cat.hpp>
+#include <boost/preprocessor/repetition/enum.hpp>
+#include <boost/preprocessor/repetition/enum_params.hpp>
+#include <boost/preprocessor/repetition/enum_binary_params.hpp>
+
+#define XD_N BOOST_PP_ITERATION()
+#define XD_FORWARD(Z, N, D) std::forward<BOOST_PP_CAT(T, N)>(BOOST_PP_CAT(p, N))
+
+template <BOOST_PP_ENUM_PARAMS(XD_N, typename T)>
+entity(BOOST_PP_ENUM_BINARY_PARAMS(XD_N, T, && p))
+ : Base(BOOST_PP_ENUM(XD_N, XD_FORWARD, _))
+{
+}
+
+#undef XD_FORWARD
+#undef XD_N
View
22 include/xd/entity.hpp
@@ -3,6 +3,9 @@
#include <xd/detail/entity.hpp>
+#include <xd/ref_counted.hpp>
+#include <xd/event_bus.hpp>
+#include <boost/config.hpp>
#include <boost/any.hpp>
#include <unordered_map>
#include <functional>
@@ -10,8 +13,10 @@
#include <list>
#include <map>
#include <type_traits>
-#include <xd/ref_counted.hpp>
-#include <xd/event_bus.hpp>
+
+#ifdef BOOST_NO_VARIADIC_TEMPLATES
+#include <boost/preprocessor/iteration/iterate.hpp>
+#endif
namespace xd
{
@@ -51,6 +56,19 @@ namespace xd
{
}
+#ifndef BOOST_NO_VARIADIC_TEMPLATES
+ // constructor that delegates parameters to the entity_base
+ template <typename... Args>
+ entity(Args&&... args)
+ : Base(std::forward<Args>(args)...)
+ {
+ }
+#else
+ // generate xd::entity::entity overloads with file iteration (up to XD_MAX_ARITY parameters)
+ #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, XD_MAX_ARITY, <xd/detail/iterate_entity_constructor.hpp>))
+ #include BOOST_PP_ITERATE()
+#endif
+
entity(const Base& base)
: Base(base)
{
View
12 include/xd/glm.hpp
@@ -0,0 +1,12 @@
+#ifndef H_XD_GLM
+#define H_XD_GLM
+
+#include <xd/vendor/glm/glm.hpp>
+#include <xd/vendor/glm/gtc/type_ptr.hpp>
+
+namespace xd
+{
+ using namespace glm;
+}
+
+#endif
View
4 include/xd/graphics.hpp
@@ -1,10 +1,10 @@
#ifndef H_XD_GRAPHICS
#define H_XD_GRAPHICS
+#include <xd/glm.hpp>
#include <xd/vendor/glew/glew.h>
-#include <xd/vendor/glm/glm.hpp>
-#include <xd/vendor/glm/gtc/type_ptr.hpp>
+#include <xd/factory.hpp>
#include <xd/graphics/exceptions.hpp>
#include <xd/graphics/font.hpp>
#include <xd/graphics/image.hpp>
View
2  include/xd/graphics/detail/vertex_traits.hpp
@@ -2,7 +2,7 @@
#define H_XD_GRAPHICS_DETAIL_VERTEX_TRAITS
#include <xd/vendor/glew/glew.h>
-#include <xd/vendor/glm/glm.hpp>
+#include <xd/glm.hpp>
#include <type_traits>
namespace xd
View
12 include/xd/graphics/font.hpp
@@ -4,18 +4,18 @@
#include <xd/graphics/detail/font.hpp>
#include <xd/vendor/glew/glew.h>
-#include <xd/vendor/glm/glm.hpp>
-#include <boost/noncopyable.hpp>
-#include <boost/optional.hpp>
-#include <boost/unordered_map.hpp>
-#include <memory>
-#include <unordered_map>
+#include <xd/glm.hpp>
+
#include <xd/ref_counted.hpp>
#include <xd/graphics/types.hpp>
#include <xd/graphics/vertex_batch.hpp>
#include <xd/graphics/shader_program.hpp>
#include <xd/graphics/transform_geometry.hpp>
#include <xd/vendor/utf8.h>
+#include <boost/noncopyable.hpp>
+#include <boost/optional.hpp>
+#include <memory>
+#include <unordered_map>
#ifndef XD_STATIC
// disable warning about boost::noncopyable not being dll-exportable
View
3  include/xd/graphics/matrix_stack.hpp
@@ -3,7 +3,7 @@
#include <stdexcept>
#include <stack>
-#include <xd/vendor/glm/glm.hpp>
+#include <xd/glm.hpp>
#include <xd/vendor/glm/gtc/matrix_transform.hpp>
namespace xd
@@ -81,6 +81,7 @@ namespace xd
{
scale(glm::vec3(s, s, s));
}
+
void rotate(float a, const glm::vec3& vec)
{
m_stack.top() = glm::rotate(m_stack.top(), a, vec);
View
8 include/xd/graphics/shader_program.hpp
@@ -2,18 +2,22 @@
#define H_XD_GRAPHICS_SHADER_PROGRAM
#include <xd/config.hpp>
+#include <xd/ref_counted.hpp>
#include <xd/vendor/glew/glew.h>
-#include <xd/vendor/glm/glm.hpp>
+#include <xd/glm.hpp>
#include <boost/noncopyable.hpp>
+#include <boost/intrusive_ptr.hpp>
#include <string>
#pragma warning(disable: 4275)
namespace xd
{
- class XD_API shader_program : boost::noncopyable
+ class XD_API shader_program : public ref_counted, public boost::noncopyable
{
public:
+ typedef boost::intrusive_ptr<shader_program> ptr;
+
// constructors & destructors
shader_program();
virtual ~shader_program();
View
33 include/xd/graphics/sprite_batch.hpp
@@ -6,7 +6,7 @@
#include <xd/graphics/transform_geometry.hpp>
#include <xd/graphics/texture.hpp>
#include <xd/graphics/types.hpp>
-#include <xd/vendor/glm/glm.hpp>
+#include <xd/glm.hpp>
#include <boost/noncopyable.hpp>
#pragma warning(disable: 4275)
@@ -14,36 +14,35 @@
namespace xd
{
// sprite batch
- class XD_API sprite_batch : public xd::ref_counted, public boost::noncopyable
+ class XD_API sprite_batch : public ref_counted, public boost::noncopyable
{
public:
typedef boost::intrusive_ptr<sprite_batch> ptr;
- sprite_batch(xd::transform_geometry& geometry_pipeline);
+ sprite_batch();
virtual ~sprite_batch();
void clear();
- void draw();
+ void draw(const mat4& matrix);
void set_scale(float scale);
float get_scale() const;
- void add(const xd::texture::ptr texture, float x, float y,
- const glm::vec4& color = glm::vec4(1, 1, 1, 1), const glm::vec2& anchor = glm::vec2(0, 0));
- void add(const xd::texture::ptr texture, float x, float y, float rotation, float scale,
- const glm::vec4& color = glm::vec4(1, 1, 1, 1), const glm::vec2& anchor = glm::vec2(0, 0));
- void add(const xd::texture::ptr texture, float x, float y, float rotation, const glm::vec2& scale,
- const glm::vec4& color = glm::vec4(1, 1, 1, 1), const glm::vec2& anchor = glm::vec2(0, 0));
+ void add(const texture::ptr texture, float x, float y,
+ const vec4& color = vec4(1, 1, 1, 1), const vec2& origin = vec2(0, 0));
+ void add(const texture::ptr texture, float x, float y, float rotation, float scale,
+ const vec4& color = vec4(1, 1, 1, 1), const vec2& origin = vec2(0, 0));
+ void add(const texture::ptr texture, float x, float y, float rotation, const vec2& scale,
+ const vec4& color = vec4(1, 1, 1, 1), const vec2& origin = vec2(0, 0));
- void add(const xd::texture::ptr texture, const xd::rect& src, float x, float y,
- const glm::vec4& color = glm::vec4(1, 1, 1, 1), const glm::vec2& anchor = glm::vec2(0, 0));
- void add(const xd::texture::ptr texture, const xd::rect& src, float x, float y, float rotation, float scale,
- const glm::vec4& color = glm::vec4(1, 1, 1, 1), const glm::vec2& anchor = glm::vec2(0, 0));
- void add(const xd::texture::ptr texture, const xd::rect& src, float x, float y, float rotation, const glm::vec2& scale,
- const glm::vec4& color = glm::vec4(1, 1, 1, 1), const glm::vec2& anchor = glm::vec2(0, 0));
+ void add(const texture::ptr texture, const rect& src, float x, float y,
+ const vec4& color = vec4(1, 1, 1, 1), const vec2& origin = vec2(0, 0));
+ void add(const texture::ptr texture, const rect& src, float x, float y, float rotation, float scale,
+ const vec4& color = vec4(1, 1, 1, 1), const vec2& origin = vec2(0, 0));
+ void add(const texture::ptr texture, const rect& src, float x, float y, float rotation, const vec2& scale,
+ const vec4& color = vec4(1, 1, 1, 1), const vec2& origin = vec2(0, 0));
private:
- transform_geometry& m_geometry_pipeline;
detail::sprite_batch_data *m_data;
float m_scale;
};
View
2  include/xd/graphics/stock_text_formatter.hpp
@@ -1,7 +1,7 @@
#ifndef H_XD_GRAPHICS_STOCK_TEXT_FORMATTER
#define H_XD_GRAPHICS_STOCK_TEXT_FORMATTER
-#include <xd/vendor/glm/glm.hpp>
+#include <xd/glm.hpp>
#include <string>
#include <ctime>
#include <unordered_map>
View
10 include/xd/graphics/text_formatter.hpp
@@ -3,6 +3,11 @@
#include <xd/graphics/detail/text_formatter.hpp>
+#include <xd/glm.hpp>
+#include <xd/graphics/exceptions.hpp>
+#include <xd/graphics/font.hpp>
+#include <xd/graphics/shader_program.hpp>
+#include <xd/vendor/utf8.h>
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <boost/noncopyable.hpp>
@@ -12,11 +17,6 @@
#include <string>
#include <functional>
#include <unordered_map>
-#include <xd/vendor/glm/glm.hpp>
-#include <xd/graphics/exceptions.hpp>
-#include <xd/graphics/font.hpp>
-#include <xd/graphics/shader_program.hpp>
-#include <xd/vendor/utf8.h>
#ifndef XD_STATIC
// disable warnings about nonstandard extension
View
21 include/xd/graphics/types.hpp
@@ -1,7 +1,7 @@
#ifndef H_XD_GRAPHICS_TYPES
#define H_XD_GRAPHICS_TYPES
-#include <xd/vendor/glm/glm.hpp>
+#include <xd/glm.hpp>
namespace xd
{
@@ -15,11 +15,15 @@ namespace xd
: x(x), y(y), w(w), h(h)
{}
- explicit rect(glm::vec2 xy, glm::vec2 wh)
+ explicit rect(const glm::vec2& xy, const glm::vec2& wh)
: x(xy.x), y(xy.y), w(wh.x), h(wh.y)
{}
- explicit rect(glm::vec4 rect)
+ explicit rect(const glm::vec2& xy, float w, float h)
+ : x(xy.x), y(xy.y), w(w), h(h)
+ {}
+
+ explicit rect(const glm::vec4& rect)
: x(rect[0]), y(rect[1]), w(rect[2]), h(rect[3])
{}
@@ -32,6 +36,17 @@ namespace xd
, h(static_cast<float>(h))
{}
+ // check if two rects intersect
+ bool intersects(const rect& other)
+ {
+ return (
+ x < (other.x+other.w) &&
+ (x+w) > other.x &&
+ y < (other.y+other.h) &&
+ (y+h) > other.y
+ );
+ }
+
float x, y, w, h;
};
}
View
2  include/xd/lua.hpp
@@ -1,6 +1,8 @@
#ifndef H_XD_LUA
#define H_XD_LUA
+#include <xd/config.hpp>
+#include <xd/factory.hpp>
#include <xd/lua/config.hpp>
#include <xd/lua/types.hpp>
#include <xd/lua/exceptions.hpp>
View
1  include/xd/lua/config.hpp
@@ -15,7 +15,6 @@
// classes/functions from the library
#if defined(_WIN32) || defined(__CYGWIN__)
#ifdef XD_LUA_EXPORT
- #define LUABIND_BUILDING
#ifdef XD_STATIC
#define XD_LUA_API extern
#else
View
1  include/xd/system.hpp
@@ -2,6 +2,7 @@
#define H_XD_SYSTEM
#include <xd/config.hpp>
+#include <xd/factory.hpp>
#include <xd/system/input.hpp>
#include <xd/system/exceptions.hpp>
#include <xd/system/window.hpp>
View
10 include/xd/system/window.hpp
@@ -1,15 +1,18 @@
#ifndef H_XD_SYSTEM_WINDOW
#define H_XD_SYSTEM_WINDOW
-#include <string>
+#include <xd/ref_counted.hpp>
+#include <xd/system/window_options.hpp>
#include <xd/system/input.hpp>
#include <xd/graphics/transform_geometry.hpp>
#include <xd/event_bus.hpp>
#include <boost/noncopyable.hpp>
+#include <boost/intrusive_ptr.hpp>
#include <boost/cstdint.hpp>
#include <unordered_map>
#include <unordered_set>
#include <functional>
+#include <string>
#if defined(_MSC_VER) && !defined(XD_STATIC)
// disable warning about boost::noncopyable not being dll-exportable
@@ -19,15 +22,16 @@
namespace xd
{
- class XD_API window : public boost::noncopyable
+ class XD_API window : public xd::ref_counted, public boost::noncopyable
{
public:
// typedefs
+ typedef boost::intrusive_ptr<window> ptr;
typedef event_bus<input_args>::callback_t input_event_callback_t;
typedef std::function<void ()> tick_callback_t;
// public interface
- window(const std::string& title, int width, int height);
+ window(const std::string& title, int width, int height, const window_options& options = window_options());
virtual ~window();
void update();
View
33 include/xd/system/window_options.hpp
@@ -1,12 +1,41 @@
#ifndef H_XD_SYSTEM_OPTIONS
#define H_XD_SYSTEM_OPTIONS
-#include <xd/window.h>
-
namespace xd
{
struct window_options
{
+ window_options()
+ : fullscreen(false)
+ , allow_resize(false)
+ , display_cursor(true)
+ , depth_bits(8)
+ , stencil_bits(0)
+ , antialiasing_level(0)
+ , major_version(2)
+ , minor_version(0)
+ {}
+
+ window_options(bool fullscreen, bool allow_resize, bool display_cursor, int depth_bits,
+ int antialiasing_level, int major_version, int minor_version)
+ : fullscreen(fullscreen)
+ , allow_resize(allow_resize)
+ , display_cursor(display_cursor)
+ , depth_bits(depth_bits)
+ , stencil_bits(stencil_bits)
+ , antialiasing_level(antialiasing_level)
+ , major_version(major_version)
+ , minor_version(minor_version)
+ {}
+
+ bool fullscreen;
+ bool allow_resize;
+ bool display_cursor;
+ int depth_bits;
+ int stencil_bits;
+ int antialiasing_level;
+ int major_version;
+ int minor_version;
};
}
View
BIN  samples/pong/font.otf
Binary file not shown
View
12 samples/pong/main.cpp
@@ -0,0 +1,12 @@
+#include "pong.hpp"
+#include <iostream>
+
+int main()
+{
+ try {
+ pong game;
+ game.run();
+ } catch (const std::exception& e) {
+ std::cerr << e.what() << std::endl;
+ }
+}
View
354 samples/pong/pong.cpp
@@ -0,0 +1,354 @@
+#include <boost/lexical_cast.hpp>
+#include <functional>
+#include <cstdlib>
+#include "pong.hpp"
+
+// the game area size, this is *not* the resolution of the game
+const float game_width = 320;
+const float game_height = 240;
+
+// game constants, you can tweak gameplay by changing these
+const float player_speed = 1.0f;
+const float computer_speed = 1.0f;
+const float ball_speed = 2.0f;
+const float game_margin = 10.0f;
+const float wall_thickness = 10.0f;
+const float paddle_thickness = 10.0f;
+const float paddle_height = 30.0f;
+const float ball_size = 7.0f;
+
+struct paddle_renderer : xd::render_component<pong_entity>
+{
+ void render(pong_entity& paddle)
+ {
+ xd::sprite_batch draw_paddle;
+ draw_paddle.add(paddle.game.m_texture, paddle.pos.x, paddle.pos.y, 0, xd::vec2(paddle_thickness, paddle_height));
+ draw_paddle.draw(paddle.game.m_geometry.mvp());
+ }
+};
+
+struct ball_renderer : xd::render_component<pong_entity>
+{
+ void render(pong_entity& ball)
+ {
+ xd::sprite_batch draw_ball;
+ draw_ball.add(ball.game.m_texture, ball.pos.x, ball.pos.y, 0, xd::vec2(ball_size, ball_size));
+ draw_ball.draw(ball.game.m_geometry.mvp());
+ }
+};
+
+struct input_controller : xd::logic_component<pong_entity>
+{
+ void update(pong_entity& paddle)
+ {
+ // check if paddle is being moved upwards or downwards
+ if (paddle.game.m_window->pressed("up"))
+ paddle.pos.y -= player_speed ;
+ if (paddle.game.m_window->pressed("down"))
+ paddle.pos.y += player_speed ;
+ }
+};
+
+// although the components in this particular case could access the ball via
+// paddle.game.m_ball, let's show an alternative way; here we inject the
+// dependancy via constructor, and store a reference to it in the component
+struct ai_controller : xd::logic_component<pong_entity>
+{
+ // the AI controller will act on ball's position
+ ai_controller(pong_entity::ptr ball)
+ : ball(ball)
+ {
+ }
+
+ // very dummy AI
+ void update(pong_entity& paddle)
+ {
+ // the amount of units to move, maximum is the computer_speed const
+ float diff = (paddle.pos.y+paddle_height/2)-(ball->pos.y+ball_size/2);
+ float units = xd::min(computer_speed, xd::abs(diff));
+
+ // move the paddle based on ball's position
+ if (diff < 0)
+ paddle.pos.y += units;
+ else
+ paddle.pos.y -= units;
+ }
+
+ pong_entity::ptr ball;
+};
+
+// you could also check if the paddle goes too up or down in input_controller
+// and ai_controller; but this way we decouple the collision check from the
+// controller method, giving us more flexibility (especially in more complex games)
+struct paddle_collision : xd::logic_component<pong_entity>
+{
+ void update(pong_entity& paddle)
+ {
+ // check if paddle collided with either of the walls
+ // restrict movement in that direction if it is colliding
+ if (paddle.pos.y < (game_margin+wall_thickness))
+ paddle.pos.y = (game_margin+wall_thickness);
+ else if ((paddle.pos.y+paddle_height) > (game_height-game_margin-wall_thickness))
+ paddle.pos.y = game_height-game_margin-wall_thickness-paddle_height;
+
+ // check if the paddle is colliding with the ball
+ auto ball = paddle.game.m_ball;
+ if (xd::rect(paddle.pos, paddle_thickness, paddle_height).intersects(xd::rect(ball->pos, ball_size, ball_size))) {
+ // get the velocity
+ auto& velocity = ball->get<glm::vec2>("velocity");
+ // we also need to check on which side of the paddle the collision happened on, so we can correctly
+ // invert either the x or y velocity of the ball, we simply check whether the collision happened on side
+ // or top/bottom of the paddle by checking the x position of ball in previous frame
+ if ((velocity.x < 0 && (paddle.pos.x+paddle_thickness) < (ball->pos.x-velocity.x*ball_speed)) ||
+ (velocity.x > 0 && paddle.pos.x > (ball->pos.x+ball_size-velocity.x*ball_speed)))
+ {
+ // the collision happened horizontally; position the ball so that it's not inside the paddle
+ ball->pos.x = paddle.pos.x + (velocity.x < 0 ? paddle_thickness : -ball_size);
+ // invert the x velocity to bounce the ball back
+ velocity.x = -velocity.x;
+ // finally to make a things bit interesting, instead of just bouncing the ball back, apply
+ // a little bit of variency on it depending on where on the paddle it hit
+ float hitting_point = (ball->pos.y+ball_size) - paddle.pos.y;
+ // because the ball doesn't have to entirely touch the paddle, it means the collision area
+ // is approximately paddle_height+ball_size, let's clamp the value between 0.0f - 1.0f
+ hitting_point = hitting_point / (paddle_height+ball_size);
+ // hitting_point 0 means that it hit the top of the paddle, and 1 that it hit the bottom,
+ // if the ball is moving downwards, the top part should dampen the angle and the bottom should
+ // should make it go at sharper angle; this works as intended, but if the ball is moving upwards
+ // we need to reverse the hitting_point to do the reverse
+ if (velocity.y < 0)
+ hitting_point = 1.0f - hitting_point;
+ // adjust the y-velocity based on the hitting point, this will effectively change the angle of the ball
+ velocity.y *= (hitting_point + 0.5f) * 1.2f;
+ // clamp the value to between min and max velocity
+ velocity.y = xd::clamp(xd::abs(velocity.y), 0.35f, 0.65f) * (velocity.y < 0 ? -1 : 1);
+ // normalize velocity
+ velocity = xd::normalize(velocity);
+ }
+ else
+ {
+ // the collision happened vertically, position the ball so that it's not inside the paddle
+ ball->pos.y = paddle.pos.y + (velocity.y < 0 ? paddle_height : -ball_size);
+ // invert the y velocity to bounce the ball back
+ velocity.y = -velocity.y;
+ }
+ }
+ }
+};
+
+// to show the alternative way, let's check for ball collision in the move component itself
+struct move_ball: xd::logic_component<pong_entity>
+{
+ // init function is ran when component is registered to entity
+ void init(pong_entity& ball)
+ {
+ // components can store additional data not in entity_base
+ // we normalize the velocity to make sure the length of the vector is 1
+ ball.get<glm::vec2>("velocity") = xd::normalize(xd::vec2(1, 1));
+ }
+
+ void update(pong_entity& ball)
+ {
+ // get the velocity from the entity
+ auto& velocity = ball.get<glm::vec2>("velocity");
+
+ // move the ball
+ ball.pos.x += ball_speed*velocity.x;
+ ball.pos.y += ball_speed*velocity.y;
+
+ // if ball is colliding with wall
+ if (ball.pos.y < (game_margin+wall_thickness)) {
+ ball.pos.y = game_margin+wall_thickness;
+ velocity.y = -velocity.y;
+ }
+ else if ((ball.pos.y+ball_size) > (game_height-game_margin-wall_thickness)) {
+ ball.pos.y = game_height-game_margin-wall_thickness-ball_size;
+ velocity.y = -velocity.y;
+ }
+ }
+};
+
+pong::pong()
+ : m_player_score(0)
+ , m_computer_score(0)
+{
+ // seed random
+ std::srand(static_cast<std::size_t>(std::time(0)));
+
+ // create window
+ m_window = xd::create<xd::window>("Pong", 640, 480);
+
+ // setup viewport to be the size of the window
+ glViewport(0, 0, m_window->width(), m_window->height());
+
+ // enable texturing and alpha testing
+ glEnable(GL_TEXTURE_2D);
+ glEnable(GL_ALPHA_TEST);
+ glAlphaFunc(GL_GREATER, 0);
+
+ // load the texture used for paddles, walls and ball
+ m_texture = xd::create<xd::texture>("texture.png");
+
+ // load the font and text shader
+ m_font = xd::create<xd::font>("font.otf", 16);
+ m_text_shader = xd::create<xd::text_shader>();
+
+ // create entities and give their initial positions
+ m_player = xd::create<pong_entity>(*this);
+ m_computer = xd::create<pong_entity>(*this);
+ m_ball = xd::create<pong_entity>(*this);
+
+ // make player controllible by keyboard and computer by AI, also deal with collisions
+ auto collision_detector = xd::create<paddle_collision>();
+ m_player->add_component(xd::create<input_controller>());
+ m_player->add_component(collision_detector);
+ m_computer->add_component(xd::create<ai_controller>(m_ball));
+ m_computer->add_component(collision_detector);
+
+ // finally the next component will move the ball
+ m_ball->add_component(xd::create<move_ball>());
+
+ // create renderers, multiple entities can share a single component
+ // which is why we only create one paddle renderer
+ auto render_paddle = xd::create<paddle_renderer>();
+ auto render_ball = xd::create<ball_renderer>();
+
+ // register renderers to entities
+ m_player->add_component(render_paddle);
+ m_computer->add_component(render_paddle);
+ m_ball->add_component(render_ball);
+
+ // reset game
+ reset();
+
+ // bind physical keys virtual keys
+ // this makes it easier to remap keys, and the code that checks for user input
+ // won't have to care about what physical keys the player has mapped his keys to
+ // we can also bind multiple physical keys to one virtual key, for example let's
+ // bind both Up/Down and W/A for moving the player paddle
+ m_window->bind_key(xd::KEY_UP, "up");
+ m_window->bind_key(xd::KEY_W, "up");
+ m_window->bind_key(xd::KEY_DOWN, "down");
+ m_window->bind_key(xd::KEY_S, "down");
+ m_window->bind_key(xd::KEY_ESC, "quit");
+
+ // register out update handler; this is where we'll update the game logic
+ // target for 60 FPS (1000 milliseconds / 60)
+ m_window->register_tick_handler(std::bind(&pong::update, this), 1000/60);
+}
+
+pong::~pong()
+{
+}
+
+void pong::run()
+{
+ // run the game loop
+ while (true)
+ {
+ // update events
+ m_window->update();
+
+ // check if window was closed
+ if (m_window->closed() || m_window->triggered("quit"))
+ break;
+
+ if (m_window->triggered(xd::KEY_N))
+ reset();
+
+ // render the game
+ render();
+ }
+}
+
+void pong::update()
+{
+ // update paddles and ball
+ m_player->update();
+ m_computer->update();
+ m_ball->update();
+
+ // check if we should give score
+ if ((m_ball->pos.x+ball_size) < 0) {
+ m_computer_score++;
+ reset();
+ }
+ if (m_ball->pos.x > game_width) {
+ m_player_score++;
+ reset();
+ }
+}
+
+void pong::render()
+{
+ // clear the window
+ m_window->clear();
+
+ // setup projection for the game
+ // note that even if the window resolution changes it'll properly scale
+ // to the whole visible area of the window, but allows us to use these values
+ // as game coordinates, making it easy to write resolution-independent code
+ m_geometry.projection().load(xd::ortho<float>(0, 320, 240, 0, -1, 1));
+
+ // load the identity matrix
+ m_geometry.model_view().identity();
+
+ // draw the walls on top and bottom with sprite batch
+ xd::sprite_batch walls;
+ // parameters: texture, x, y, rotation, scale
+ walls.add(m_texture, game_margin, game_margin, 0, xd::vec2(game_width-game_margin*2, wall_thickness));
+ // this demonstrates the origin feature, you can "anchor"
+ // the sprite to a different point by specifying the origin where to draw the sprite from
+ // the default is (0,0) which is top-left; here we specify (0,1) which means bottom-left
+ // if we didn't do this, we'd have to specify 240-10 as parameter to y instead of just 240
+ // parameters: texture, x, y, rotation, scale, color, origin
+ walls.add(m_texture, game_margin, game_height-game_margin, 0, xd::vec2(game_width-game_margin*2, wall_thickness), glm::vec4(1,1,1,1), glm::vec2(0,1));
+ walls.draw(m_geometry.mvp());
+
+ // now render the paddles and the ball, these will simply use the registered
+ // render components of the entities
+ m_player->render();
+ m_computer->render();
+ m_ball->render();
+
+ // draw the text in white color
+ xd::font_style style;
+ style.color = glm::vec4(1, 1, 1, 1);
+ // draw the player score
+ m_geometry.model_view().push();
+ m_geometry.model_view().translate(game_width/2-50-10*(m_player_score/10+1), game_margin+wall_thickness+game_height/10, 0);
+ m_geometry.model_view().scale(1, -1, 1);
+ m_font->render(boost::lexical_cast<std::string>(m_player_score), style, *m_text_shader, m_geometry.mvp());
+ m_geometry.model_view().pop();
+ // draw the computer score
+ m_geometry.model_view().push();
+ m_geometry.model_view().translate(game_width/2+50-10*(m_computer_score/10+1), game_margin+wall_thickness+game_height/10, 0);
+ m_geometry.model_view().scale(1, -1, 1);
+ m_font->render(boost::lexical_cast<std::string>(m_computer_score), style, *m_text_shader, m_geometry.mvp());
+ m_geometry.model_view().pop();
+
+ // show what we've drawn
+ m_window->swap();
+}
+
+void pong::reset()
+{
+ // set paddle positions
+ m_player->pos = xd::vec2(game_margin, game_height/2-paddle_height/2);
+ m_computer->pos = xd::vec2(game_width-paddle_thickness-game_margin, game_height/2-paddle_height/2);
+
+ // set ball starting position
+ m_ball->pos.x = game_margin + wall_thickness;
+ m_ball->pos.y = game_margin + wall_thickness;
+ m_ball->pos.y += static_cast<float>(std::rand())/RAND_MAX*(game_height-game_margin*2-wall_thickness*2);
+
+ // randomize y-velocity
+ auto& velocity = m_ball->get<glm::vec2>("velocity");
+ velocity.x = 1.0f;
+ velocity.y = static_cast<float>(std::rand())/RAND_MAX-0.5f;
+ if (velocity.y < 0)
+ velocity.y -= 0.5f;
+ else
+ velocity.y += 0.5f;
+ velocity = xd::normalize(velocity);
+}
View
65 samples/pong/pong.hpp
@@ -0,0 +1,65 @@
+#ifndef H_PONG
+#define H_PONG
+
+#include <xd/system.hpp>
+#include <xd/graphics.hpp>
+#include <xd/entity.hpp>
+
+// the entity data needs forward declaration of pong class
+class pong;
+
+// the data that each entity share
+// pong has only two entities: paddle and ball
+struct entity_data
+{
+ entity_data(pong& game)
+ : game(game)
+ {}
+
+ // reference to the game
+ pong& game;
+ // position
+ xd::vec2 pos;
+};
+
+// our entity class
+typedef xd::entity<entity_data> pong_entity;
+
+// the game class
+class pong
+{
+public:
+ pong();
+ ~pong();
+
+ void run();
+ void update();
+ void render();
+ void reset();
+
+private:
+ // some resources required by our game
+ xd::window::ptr m_window;
+ xd::texture::ptr m_texture;
+ xd::font::ptr m_font;
+ xd::text_shader::ptr m_text_shader;
+ xd::transform_geometry m_geometry;
+
+ // the game entities
+ pong_entity::ptr m_player;
+ pong_entity::ptr m_computer;
+ pong_entity::ptr m_ball;
+
+ // scores
+ int m_player_score;
+ int m_computer_score;
+
+ // give components access to the internals
+ friend struct paddle_renderer;
+ friend struct ball_renderer;
+ friend struct input_controller;
+ friend struct paddle_collision;
+ friend struct move_ball;
+};
+
+#endif
View
BIN  samples/pong/texture.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
2  src/graphics/shader_program.cpp
@@ -1,4 +1,4 @@
-#include <xd/vendor/glm/gtc/type_ptr.hpp>
+#include <xd/glm.hpp>
#include <xd/graphics/shader_program.hpp>
#include <xd/graphics/exceptions.hpp>
View
3  src/graphics/shaders.cpp
@@ -186,13 +186,14 @@ xd::sprite_shader::sprite_shader()
static const char *vertex_shader_src =
"#version 110\n"
"uniform mat4 mvpMatrix;"
+ "uniform vec4 vPosition;"
"attribute vec4 vVertex;"
"attribute vec2 vTexCoords;"
"varying vec2 vVaryingTexCoords;"
"void main(void)"
"{"
" vVaryingTexCoords = vTexCoords;"
- " gl_Position = mvpMatrix * vVertex;"
+ " gl_Position = mvpMatrix * (vPosition + vVertex);"
"}";
static const char *fragment_shader_src =
View
98 src/graphics/sprite_batch.cpp
@@ -11,12 +11,12 @@ namespace xd { namespace detail {
struct sprite
{
texture::ptr texture;
- xd::rect src;
+ rect src;
float x, y;
float rotation;
- glm::vec2 scale;
- glm::vec2 anchor;
- glm::vec4 color;
+ xd::vec2 scale;
+ xd::vec2 origin;
+ xd::vec4 color;
float depth;
};
@@ -28,9 +28,8 @@ namespace xd { namespace detail {
} }
-xd::sprite_batch::sprite_batch(xd::transform_geometry& geometry_pipeline)
- : m_geometry_pipeline(geometry_pipeline)
- , m_data(new detail::sprite_batch_data)
+xd::sprite_batch::sprite_batch()
+ : m_data(new detail::sprite_batch_data)
, m_scale(1)
{
}
@@ -45,53 +44,64 @@ void xd::sprite_batch::clear()
m_data->sprites.clear();
}
-void xd::sprite_batch::draw()
+void xd::sprite_batch::draw(const xd::mat4& mvp_matrix)
{
// create a vertex batch for sending vertex data
vertex_batch<detail::sprite_vertex_traits> batch(GL_QUADS);
// setup the shader
m_data->shader.use();
+ m_data->shader.bind_uniform("mvpMatrix", mvp_matrix);
- // get the modelview matrix for convenience
- auto& model_view = m_geometry_pipeline.model_view();
+ // create a quad for rendering sprites
+ detail::sprite_vertex quad[4];
// iterate through all sprites
for (auto i = m_data->sprites.begin(); i != m_data->sprites.end(); ++i) {
- // push matrix
- model_view.push();
- // create a quad for rendering sprites
- detail::sprite_vertex quad[4];
- // assign position
+ // so we need to type less ;)
auto tw = i->texture->width();
auto th = i->texture->height();
auto& src = i->src;
- auto& anchor = i->anchor;
- quad[0].pos = glm::vec2((0-anchor.x)*src.w, (1-anchor.y)*src.h);
- quad[1].pos = glm::vec2((1-anchor.x)*src.w, (1-anchor.y)*src.h);
- quad[2].pos = glm::vec2((1-anchor.x)*src.w, (0-anchor.y)*src.h);
- quad[3].pos = glm::vec2((0-anchor.x)*src.w, (0-anchor.y)*src.h);
+ auto& origin = i->origin;
+
+ // calculate scale
+ vec2 scale = m_scale * i->scale;
+
+ // assign position
+ quad[0].pos = vec2(scale.x*(0-origin.x)*src.w, scale.y*(1-origin.y)*src.h);
+ quad[1].pos = vec2(scale.x*(1-origin.x)*src.w, scale.y*(1-origin.y)*src.h);
+ quad[2].pos = vec2(scale.x*(1-origin.x)*src.w, scale.y*(0-origin.y)*src.h);
+ quad[3].pos = vec2(scale.x*(0-origin.x)*src.w, scale.y*(0-origin.y)*src.h);
+
+ // if there's rotation
+ if (i->rotation) {
+ // construct a rotation matrix
+ auto rotation_matrix = rotate(mat4(), i->rotation, vec3(0, 0, 1));
+ // rotate the 4 vertices
+ quad[0].pos = vec2(rotation_matrix * vec4(quad[0].pos, 0, 1));
+ quad[1].pos = vec2(rotation_matrix * vec4(quad[1].pos, 0, 1));
+ quad[2].pos = vec2(rotation_matrix * vec4(quad[2].pos, 0, 1));
+ quad[3].pos = vec2(rotation_matrix * vec4(quad[3].pos, 0, 1));
+ }
+
// assign texture pos
- quad[0].texpos = glm::vec2(src.x/tw , (src.y+src.h)/th);
- quad[1].texpos = glm::vec2((src.x+src.w)/tw, (src.y+src.h)/th);
- quad[2].texpos = glm::vec2((src.x+src.w)/tw, src.y/th);
- quad[3].texpos = glm::vec2(src.x/tw , src.y/th);
+ quad[0].texpos = vec2(src.x/tw , (src.y+src.h)/th);
+ quad[1].texpos = vec2((src.x+src.w)/tw, (src.y+src.h)/th);
+ quad[2].texpos = vec2((src.x+src.w)/tw, src.y/th);
+ quad[3].texpos = vec2(src.x/tw , src.y/th);
+
// load the quad
batch.load(&quad[0], 4);
- // translate to the correct position
- model_view.translate(i->x, i->y, i->depth);
- // setup rotation and scaling
- model_view.rotate(i->rotation, 0, 0, 1);
- model_view.scale(m_scale*i->scale.x, m_scale*i->scale.y, 1);
+
// give required params to shader
- m_data->shader.bind_uniform("mvpMatrix", m_geometry_pipeline.mvp());
+ m_data->shader.bind_uniform("vPosition", vec4(i->x, i->y, i->depth, 0));
m_data->shader.bind_uniform("vColor", i->color);
+
// bind the texture
i->texture->bind(GL_TEXTURE0);
+
// draw it
batch.render();
- // pop matrix
- model_view.pop();
}
}
@@ -105,32 +115,32 @@ float xd::sprite_batch::get_scale() const
return m_scale;
}
-void xd::sprite_batch::add(const xd::texture::ptr texture, float x, float y, const glm::vec4& color, const glm::vec2& anchor)
+void xd::sprite_batch::add(const xd::texture::ptr texture, float x, float y, const xd::vec4& color, const xd::vec2& origin)
{
- add(texture, xd::rect(0, 0, texture->width(), texture->height()), x, y, 0, glm::vec2(1, 1), color, anchor);
+ add(texture, rect(0, 0, texture->width(), texture->height()), x, y, 0, vec2(1, 1), color, origin);
}
-void xd::sprite_batch::add(const xd::texture::ptr texture, float x, float y, float rotation, float scale, const glm::vec4& color, const glm::vec2& anchor)
+void xd::sprite_batch::add(const xd::texture::ptr texture, float x, float y, float rotation, float scale, const xd::vec4& color, const xd::vec2& origin)
{
- add(texture, xd::rect(0, 0, texture->width(), texture->height()), x, y, rotation, glm::vec2(scale, scale), color, anchor);
+ add(texture, rect(0, 0, texture->width(), texture->height()), x, y, rotation, vec2(scale, scale), color, origin);
}
-void xd::sprite_batch::add(const xd::texture::ptr texture, float x, float y, float rotation, const glm::vec2& scale, const glm::vec4& color, const glm::vec2& anchor)
+void xd::sprite_batch::add(const xd::texture::ptr texture, float x, float y, float rotation, const xd::vec2& scale, const xd::vec4& color, const xd::vec2& origin)
{
- add(texture, xd::rect(0, 0, texture->width(), texture->height()), x, y, rotation, scale, color, anchor);
+ add(texture, rect(0, 0, texture->width(), texture->height()), x, y, rotation, scale, color, origin);
}
-void xd::sprite_batch::add(const xd::texture::ptr texture, const xd::rect& src, float x, float y, const glm::vec4& color, const glm::vec2& anchor)
+void xd::sprite_batch::add(const xd::texture::ptr texture, const xd::rect& src, float x, float y, const xd::vec4& color, const xd::vec2& origin)
{
- add(texture, src, x, y, 0, glm::vec2(1, 1), color, anchor);
+ add(texture, src, x, y, 0, vec2(1, 1), color, origin);
}
-void xd::sprite_batch::add(const xd::texture::ptr texture, const xd::rect& src, float x, float y, float rotation, float scale, const glm::vec4& color, const glm::vec2& anchor)
+void xd::sprite_batch::add(const xd::texture::ptr texture, const xd::rect& src, float x, float y, float rotation, float scale, const xd::vec4& color, const xd::vec2& origin)
{
- add(texture, src, x, y, rotation, glm::vec2(scale, scale), color, anchor);
+ add(texture, src, x, y, rotation, vec2(scale, scale), color, origin);
}
-void xd::sprite_batch::add(const xd::texture::ptr texture, const xd::rect& src, float x, float y, float rotation, const glm::vec2& scale, const glm::vec4& color, const glm::vec2& anchor)
+void xd::sprite_batch::add(const xd::texture::ptr texture, const xd::rect& src, float x, float y, float rotation, const xd::vec2& scale, const xd::vec4& color, const xd::vec2& origin)
{
detail::sprite sprite;
sprite.texture = texture;
@@ -139,7 +149,7 @@ void xd::sprite_batch::add(const xd::texture::ptr texture, const xd::rect& src,
sprite.y = y;
sprite.rotation = rotation;
sprite.scale = scale;
- sprite.anchor = anchor;
+ sprite.origin = origin;
sprite.color = color;
sprite.depth = 0.0f;
m_data->sprites.push_back(sprite);
View
30 src/system/window.cpp
@@ -19,7 +19,7 @@ namespace
}
};
-xd::window::window(const std::string& title, int width, int height)
+xd::window::window(const std::string& title, int width, int height, const window_options& options)
: m_width(width)
, m_height(height)
{
@@ -33,21 +33,25 @@ xd::window::window(const std::string& title, int width, int height)
throw xd::window_creation_failed();
}
- glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
- glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
- glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 2);
- glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 0);
-
- if (glfwOpenWindow(m_width, m_height, 8, 8, 8, 8, 24, 0, GLFW_WINDOW) == GL_FALSE) {
+ glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, !options.allow_resize);
+ glfwOpenWindowHint(GLFW_FSAA_SAMPLES, options.antialiasing_level);
+ glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, options.major_version);
+ glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, options.minor_version);
+
+ if (glfwOpenWindow(m_width, m_height, 8, 8, 8, 8, options.depth_bits, options.stencil_bits,
+ options.fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW) == GL_FALSE)
+ {
glfwTerminate();
throw xd::window_creation_failed();
}
glfwSetWindowTitle(title.c_str());
+ if (options.display_cursor)
+ glfwEnable(GLFW_MOUSE_CURSOR);
+ else
+ glfwDisable(GLFW_MOUSE_CURSOR);
glfwEnable(GLFW_SYSTEM_KEYS);
- glfwEnable(GLFW_MOUSE_CURSOR);
-
glfwDisable(GLFW_AUTO_POLL_EVENTS);
glfwDisable(GLFW_STICKY_KEYS);
glfwDisable(GLFW_STICKY_MOUSE_BUTTONS);
@@ -157,12 +161,16 @@ bool xd::window::closed() const
int xd::window::width() const
{
- return m_width;
+ int width;
+ glfwGetWindowSize(&width, nullptr);
+ return width;
}
int xd::window::height() const
{
- return m_height;
+ int height;
+ glfwGetWindowSize(nullptr, &height);
+ return height;
}
int xd::window::ticks() const
View
72 src/test/test.cpp
@@ -19,8 +19,8 @@ void wave_decorator(xd::text_decorator& decorator, const xd::formatted_text& tex
float start = (float)(clock()/1000 % 1000) / 1000.0f * 2*pi;
for (xd::formatted_text::const_iterator i = text.begin(); i != text.end(); ++i) {
float pos = sin(start)*height;
- decorator.push_position(glm::vec2(0, pos));
- //decorator.push_position(glm::vec2(0, int(pos > 0 ? pos + 0.5f : pos - 0.5f)));
+ decorator.push_position(xd::vec2(0, pos));
+ //decorator.push_position(xd::vec2(0, int(pos > 0 ? pos + 0.5f : pos - 0.5f)));
decorator.push_text(*i);
decorator.pop_position();
start += step;
@@ -77,10 +77,10 @@ test::test()
{
my_vertex data[4];
- data[0].pos = glm::vec3(-0.5f, 0.0f, 0.5f); data[0].tex = glm::vec2(0, 1);
- data[1].pos = glm::vec3( 0.5f, 0.0f, 0.5f); data[1].tex = glm::vec2(1, 1);
- data[2].pos = glm::vec3( 0.5f, 0.0f, -0.5f); data[2].tex = glm::vec2(1, 0);
- data[3].pos = glm::vec3(-0.5f, 0.0f, -0.5f); data[3].tex = glm::vec2(0, 0);
+ data[0].pos = xd::vec3(-0.5f, 0.0f, 0.5f); data[0].tex = xd::vec2(0, 1);
+ data[1].pos = xd::vec3( 0.5f, 0.0f, 0.5f); data[1].tex = xd::vec2(1, 1);
+ data[2].pos = xd::vec3( 0.5f, 0.0f, -0.5f); data[2].tex = xd::vec2(1, 0);
+ data[3].pos = xd::vec3(-0.5f, 0.0f, -0.5f); data[3].tex = xd::vec2(0, 0);
m_grass_batch.load(&data[0], 4);
}
@@ -89,25 +89,25 @@ test::test()
{
my_vertex data[16];
- data[0].pos = glm::vec3(-0.5f, 0.0f, 0.5f); data[0].tex = glm::vec2(0, 0);
- data[1].pos = glm::vec3(-0.5f, 0.0f, -0.5f); data[1].tex = glm::vec2(1, 0);
- data[2].pos = glm::vec3(-0.5f, -1.0f, -0.5f); data[2].tex = glm::vec2(1, 1);
- data[3].pos = glm::vec3(-0.5f, -1.0f, 0.5f); data[3].tex = glm::vec2(0, 1);
-
- data[4].pos = glm::vec3( 0.5f, 0.0f, -0.5f); data[4].tex = glm::vec2(0, 0);
- data[5].pos = glm::vec3( 0.5f, 0.0f, 0.5f); data[5].tex = glm::vec2(1, 0);
- data[6].pos = glm::vec3( 0.5f, -1.0f, 0.5f); data[6].tex = glm::vec2(1, 1);
- data[7].pos = glm::vec3( 0.5f, -1.0f, -0.5f); data[7].tex = glm::vec2(0, 1);
-
- data[8].pos = glm::vec3(-0.5f, 0.0f, -0.5f); data[8].tex = glm::vec2(0, 0);
- data[9].pos = glm::vec3( 0.5f, 0.0f, -0.5f); data[9].tex = glm::vec2(1, 0);
- data[10].pos = glm::vec3( 0.5f, -1.0f, -0.5f); data[10].tex = glm::vec2(1, 1);
- data[11].pos = glm::vec3(-0.5f, -1.0f, -0.5f); data[11].tex = glm::vec2(0, 1);
-
- data[12].pos = glm::vec3( 0.5f, 0.0f, 0.5f); data[12].tex = glm::vec2(0, 0);
- data[13].pos = glm::vec3(-0.5f, 0.0f, 0.5f); data[13].tex = glm::vec2(1, 0);
- data[14].pos = glm::vec3(-0.5f, -1.0f, 0.5f); data[14].tex = glm::vec2(1, 1);
- data[15].pos = glm::vec3( 0.5f, -1.0f, 0.5f); data[15].tex = glm::vec2(0, 1);
+ data[0].pos = xd::vec3(-0.5f, 0.0f, 0.5f); data[0].tex = xd::vec2(0, 0);
+ data[1].pos = xd::vec3(-0.5f, 0.0f, -0.5f); data[1].tex = xd::vec2(1, 0);
+ data[2].pos = xd::vec3(-0.5f, -1.0f, -0.5f); data[2].tex = xd::vec2(1, 1);
+ data[3].pos = xd::vec3(-0.5f, -1.0f, 0.5f); data[3].tex = xd::vec2(0, 1);
+
+ data[4].pos = xd::vec3( 0.5f, 0.0f, -0.5f); data[4].tex = xd::vec2(0, 0);
+ data[5].pos = xd::vec3( 0.5f, 0.0f, 0.5f); data[5].tex = xd::vec2(1, 0);
+ data[6].pos = xd::vec3( 0.5f, -1.0f, 0.5f); data[6].tex = xd::vec2(1, 1);
+ data[7].pos = xd::vec3( 0.5f, -1.0f, -0.5f); data[7].tex = xd::vec2(0, 1);
+
+ data[8].pos = xd::vec3(-0.5f, 0.0f, -0.5f); data[8].tex = xd::vec2(0, 0);
+ data[9].pos = xd::vec3( 0.5f, 0.0f, -0.5f); data[9].tex = xd::vec2(1, 0);
+ data[10].pos = xd::vec3( 0.5f, -1.0f, -0.5f); data[10].tex = xd::vec2(1, 1);
+ data[11].pos = xd::vec3(-0.5f, -1.0f, -0.5f); data[11].tex = xd::vec2(0, 1);
+
+ data[12].pos = xd::vec3( 0.5f, 0.0f, 0.5f); data[12].tex = xd::vec2(0, 0);
+ data[13].pos = xd::vec3(-0.5f, 0.0f, 0.5f); data[13].tex = xd::vec2(1, 0);
+ data[14].pos = xd::vec3(-0.5f, -1.0f, 0.5f); data[14].tex = xd::vec2(1, 1);
+ data[15].pos = xd::vec3( 0.5f, -1.0f, 0.5f); data[15].tex = xd::vec2(0, 1);
m_edge_batch.load(&data[0], 16);
}
@@ -130,8 +130,8 @@ test::~test()
void test::draw_text(float x, float y, const std::string& text)
{
xd::font_style style;
- style.color = glm::vec4(1, 1, 1, 1);
- style.shadow = xd::font_shadow(1, -1, glm::vec4(0, 0, 0, 1));
+ style.color = xd::vec4(1, 1, 1, 1);
+ style.shadow = xd::font_shadow(1, -1, xd::vec4(0, 0, 0, 1));
m_model_view->push();
m_model_view->translate(x, y, 0);
@@ -227,11 +227,13 @@ void test::run()
break;
}
+ glViewport(0, 0, width(), height());
+
// clear screen
clear();
// setup orthographic projection for isometric view
- /*m_geometry.projection().load(glm::ortho(-320.0f, 320.0f, -240.0f, 240.0f, -999.0f, 999.0f));
+ m_geometry.projection().load(xd::ortho(-320.0f, 320.0f, -240.0f, 240.0f, -999.0f, 999.0f));
// enable depth testing
glEnable(GL_DEPTH_TEST);
@@ -255,10 +257,10 @@ void test::run()
for (int z = 0; z < map_size; z++) {
draw_tile(x, z);
}
- }*/
+ }
// setup orthographic projection for text rendering
- m_geometry.projection().load(glm::ortho(0.0f, (float)width(), (float)height(), 0.0f, -100.0f, 100.0f));
+ m_geometry.projection().load(xd::ortho<float>(0, 640, 480, 0, -100, 100));
m_model_view->identity();
// disable depth testing and face culling
@@ -266,15 +268,15 @@ void test::run()
glDisable(GL_CULL_FACE);
// create a sprite batch
- xd::sprite_batch sprite_batch(m_geometry);
+ xd::sprite_batch sprite_batch;
// draw a sprite that rotates around its center (texture, x, y, rotation, scale, color, anchor)
- sprite_batch.add(m_grass_texture, 320, 240, ticks()/1000.0f*45, 1, glm::vec4(1,1,1,1), glm::vec2(0.5,0.5));
+ sprite_batch.add(m_grass_texture, 320, 240, ticks()/1000.0f*45, 1, xd::vec4(1,1,1,1), xd::vec2(0.5,0.5));
// draw a partly transparent sprite (texture, x, y, color)
- sprite_batch.add(m_grass_texture, 240, 240, glm::vec4(1,1,1,0.5));
+ sprite_batch.add(m_grass_texture, 240, 240, xd::vec4(1,1,1,0.5));
// draw a scaled sprite that is also red (texture, x, y, rotation, scale, color)
- sprite_batch.add(m_grass_texture, xd::rect(0, 0, 16, 16), 400, 240, 0, 1.5, glm::vec4(1,0,0,1));
+ sprite_batch.add(m_grass_texture, 400, 240, 0, 1.5, xd::vec4(1,0,0,1));
// draw all of them at same time
- sprite_batch.draw();
+ sprite_batch.draw(m_geometry.mvp());
// alpha testing
//glAlphaFunc(GL_GREATER, 0.5f);
View
2  win/vc10/common/common.vcxproj
@@ -83,11 +83,13 @@
<ClInclude Include="..\..\..\include\xd\config.hpp" />
<ClInclude Include="..\..\..\include\xd\detail\entity.hpp" />
<ClInclude Include="..\..\..\include\xd\detail\iterate_create.hpp" />
+ <ClInclude Include="..\..\..\include\xd\detail\iterate_entity_constructor.hpp" />
<ClInclude Include="..\..\..\include\xd\detail\iterate_factory_create.hpp" />
<ClInclude Include="..\..\..\include\xd\entity.hpp" />
<ClInclude Include="..\..\..\include\xd\event_bus.hpp" />
<ClInclude Include="..\..\..\include\xd\exception.hpp" />
<ClInclude Include="..\..\..\include\xd\factory.hpp" />
+ <ClInclude Include="..\..\..\include\xd\glm.hpp" />
<ClInclude Include="..\..\..\include\xd\graphics.hpp" />
<ClInclude Include="..\..\..\include\xd\lua.hpp" />
<ClInclude Include="..\..\..\include\xd\ref_counted.hpp" />
View
6 win/vc10/common/common.vcxproj.filters
@@ -57,5 +57,11 @@
<Filter>Header Files\detail</Filter>
</ClInclude>
<ClInclude Include="..\..\..\include\xd\types.hpp" />
+ <ClInclude Include="..\..\..\include\xd\glm.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ <ClInclude Include="..\..\..\include\xd\detail\iterate_entity_constructor.hpp">
+ <Filter>Header Files\detail</Filter>
+ </ClInclude>
</ItemGroup>
</Project>
View
2  win/vc10/lua/lua.vcxproj
@@ -70,7 +70,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
- <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;XD_LUA_EXPORT;LUA_LIB;LUA_BUILD_AS_DLL;LUABIND_DYNAMIC_LINK;_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
+ <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;XD_LUA_EXPORT;LUA_LIB;LUA_BUILD_AS_DLL;LUABIND_BUILDING;LUABIND_DYNAMIC_LINK;_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
<AdditionalOptions>/wd4251 /wd4275 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
View
90 win/vc10/samples/pong/pong.vcxproj
@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="..\..\..\..\samples\pong\pong.hpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="..\..\..\..\samples\pong\main.cpp" />
+ <ClCompile Include="..\..\..\..\samples\pong\pong.cpp" />
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <ProjectGuid>{6CFB0E08-FBA3-438B-812B-A73F49969EBD}</ProjectGuid>
+ <Keyword>Win32Proj</Keyword>
+ <RootNamespace>pong</RootNamespace>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <LinkIncremental>true</LinkIncremental>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <LinkIncremental>false</LinkIncremental>
+ <IncludePath>$(SolutionDir)..\..\..\include;$(IncludePath)</IncludePath>
+ <LibraryPath>$(SolutionDir)..\Release;$(LibraryPath)</LibraryPath>
+ <ExecutablePath>$(ExecutablePath)</ExecutablePath>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <ClCompile>
+ <PrecompiledHeader>
+ </PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <PrecompiledHeader>
+ </PrecompiledHeader>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ <AdditionalDependencies>opengl32.lib;glew.lib;xd_system.lib;xd_graphics.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ </Link>
+ </ItemDefinitionGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project>
View
30 win/vc10/samples/pong/pong.vcxproj.filters
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <Filter Include="Source Files">
+ <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+ <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+ </Filter>
+ <Filter Include="Header Files">
+ <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+ <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
+ </Filter>
+ <Filter Include="Resource Files">
+ <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
+ <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
+ </Filter>
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="..\..\..\..\samples\pong\pong.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="..\..\..\..\samples\pong\main.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ <ClCompile Include="..\..\..\..\samples\pong\pong.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ </ItemGroup>
+</Project>
View
8 win/vc10/samples/pong/pong.vcxproj.user
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <LocalDebuggerEnvironment>PATH=$(SolutionDir)..\Release;%PATH%</LocalDebuggerEnvironment>
+ <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
+ <LocalDebuggerWorkingDirectory>$(SolutionDir)..\..\..\samples\pong</LocalDebuggerWorkingDirectory>
+ </PropertyGroup>
+</Project>
View
20 win/vc10/samples/samples.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual C++ Express 2010
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pong", "pong\pong.vcxproj", "{6CFB0E08-FBA3-438B-812B-A73F49969EBD}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Win32 = Debug|Win32
+ Release|Win32 = Release|Win32
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {6CFB0E08-FBA3-438B-812B-A73F49969EBD}.Debug|Win32.ActiveCfg = Debug|Win32
+ {6CFB0E08-FBA3-438B-812B-A73F49969EBD}.Debug|Win32.Build.0 = Debug|Win32
+ {6CFB0E08-FBA3-438B-812B-A73F49969EBD}.Release|Win32.ActiveCfg = Release|Win32
+ {6CFB0E08-FBA3-438B-812B-A73F49969EBD}.Release|Win32.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
Please sign in to comment.
Something went wrong with that request. Please try again.