galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
LuaGraphics.cpp
Go to the documentation of this file.
1
7
8#include <sol/sol.hpp>
9
10#include "galaxy/core/ServiceLocator.hpp"
21
22#include "Lua.hpp"
23
24namespace galaxy
25{
26 namespace lua
27 {
29 {
30 auto& lua = core::ServiceLocator<sol::state>::ref();
31
32 auto animation_type = lua.new_usertype<graphics::Animation>("Animation", sol::constructors<graphics::Animation()>());
33 animation_type["current"] = &graphics::Animation::current;
34 animation_type["frames"] = &graphics::Animation::frames;
35 animation_type["index"] = &graphics::Animation::index;
36 animation_type["load"] = &graphics::Animation::load;
37 animation_type["name"] = &graphics::Animation::m_name;
38 animation_type["speed"] = &graphics::Animation::m_speed;
39 animation_type["next"] = &graphics::Animation::next;
40 animation_type["prev"] = &graphics::Animation::prev;
41 animation_type["restart"] = &graphics::Animation::restart;
42 // animation_type["set_frames"] = &graphics::Animation::set_frames;
43 animation_type["total"] = &graphics::Animation::total;
44
45 auto frame_type = lua.new_usertype<graphics::Frame>("Frame", sol::constructors<graphics::Frame()>());
46 frame_type["bounds"] = &graphics::Frame::m_bounds;
47 frame_type["duration"] = &graphics::Frame::m_duration;
48
49 auto cameradata_type = lua.new_usertype<graphics::Camera::Data>("CameraData", sol::no_constructor);
50 cameradata_type["model_view"] = &graphics::Camera::Data::m_model_view;
51 cameradata_type["projection"] = &graphics::Camera::Data::m_projection;
52
53 auto camera_type = lua.new_usertype<graphics::Camera>("Camera", sol::constructors<graphics::Camera()>());
54 camera_type["get_data"] = &graphics::Camera::get_data;
55 camera_type["get_model_view"] = &graphics::Camera::get_model_view;
56 camera_type["get_origin"] = &graphics::Camera::get_origin;
57 camera_type["get_pos"] = &graphics::Camera::get_pos;
58 camera_type["get_proj"] = &graphics::Camera::get_proj;
59 camera_type["get_rotation"] = &graphics::Camera::get_rotation;
60 camera_type["get_scale"] = &graphics::Camera::get_scale;
61 camera_type["get_transform"] = &graphics::Camera::get_transform;
62 camera_type["get_viewport"] = &graphics::Camera::get_viewport;
63 camera_type["rotation_speed"] = &graphics::Camera::m_rotation_speed;
64 camera_type["translation_speed"] = &graphics::Camera::m_translation_speed;
65 camera_type["allow_rotation"] = &graphics::Camera::m_allow_rotation;
66 camera_type["reset"] = &graphics::Camera::reset;
67 camera_type["rotate"] = &graphics::Camera::rotate;
68 camera_type["scale"] = &graphics::Camera::scale;
69 camera_type["set_viewport"] = &graphics::Camera::set_viewport;
70 camera_type["set_pos"] = &graphics::Camera::set_pos;
71 camera_type["set_rotation"] = &graphics::Camera::set_rotation;
72 camera_type["set_origin"] = &graphics::Camera::set_origin;
73 camera_type["set_scale_horizontal"] = &graphics::Camera::set_scale_horizontal;
74 camera_type["set_scale_vertical"] = &graphics::Camera::set_scale_vertical;
75 camera_type["translate"] = &graphics::Camera::translate;
76
77 lua.set("galaxy_colour_opaque", graphics::Colour::OPAQUE);
78 lua.set("galaxy_colour_transparent", graphics::Colour::TRANSPARENT);
79
80 auto colour_type = lua.new_usertype<graphics::Colour>("Colour", sol::constructors<graphics::Colour()>());
81 colour_type["get_red"] = &graphics::Colour::r<std::uint8_t>;
82 colour_type["get_green"] = &graphics::Colour::g<std::uint8_t>;
83 colour_type["get_blue"] = &graphics::Colour::b<std::uint8_t>;
84 colour_type["get_alpha"] = &graphics::Colour::a<std::uint8_t>;
85 colour_type["get_red_f"] = &graphics::Colour::r<float>;
86 colour_type["get_green_f"] = &graphics::Colour::g<float>;
87 colour_type["get_blue_f"] = &graphics::Colour::b<float>;
88 colour_type["get_alpha_f"] = &graphics::Colour::a<float>;
89 colour_type["set_r"] = &graphics::Colour::set_r;
90 colour_type["set_g"] = &graphics::Colour::set_g;
91 colour_type["set_b"] = &graphics::Colour::set_b;
92 colour_type["set_a"] = &graphics::Colour::set_a;
93 colour_type["set_rgba"] = &graphics::Colour::set_rgba;
94
95 auto rendercommand_type = lua.new_usertype<graphics::RenderCommand>("RenderCommand", sol::constructors<graphics::RenderCommand()>());
96 rendercommand_type["count"] = &graphics::RenderCommand::count;
97 rendercommand_type["instances"] = &graphics::RenderCommand::instances;
98 rendercommand_type["layer"] = &graphics::RenderCommand::layer;
99 rendercommand_type["mode"] = &graphics::RenderCommand::mode;
100 rendercommand_type["offset"] = &graphics::RenderCommand::offset;
101 rendercommand_type["uniforms"] = &graphics::RenderCommand::uniforms;
102 rendercommand_type["vao"] = &graphics::RenderCommand::vao;
103 rendercommand_type["texture"] = &graphics::RenderCommand::texture;
104
105 auto renderdata_type = lua.new_usertype<graphics::RenderData>("RenderData", sol::constructors<graphics::RenderData()>());
106 renderdata_type["colour"] = &graphics::RenderData::colour;
107 renderdata_type["entity"] = &graphics::RenderData::entity;
108 renderdata_type["point"] = &graphics::RenderData::point;
109 renderdata_type["textured"] = &graphics::RenderData::textured;
110 renderdata_type["transform"] = &graphics::RenderData::transform;
111
112 auto transform_type = lua.new_usertype<graphics::Transform>("Transform", sol::constructors<graphics::Transform()>());
113 transform_type["get_origin"] = &graphics::Transform::get_origin;
114 transform_type["get_pos"] = &graphics::Transform::get_pos;
115 transform_type["get_rotation"] = &graphics::Transform::get_rotation;
116 transform_type["get_scale"] = &graphics::Transform::get_scale;
117 transform_type["get_transform"] = &graphics::Transform::get_transform;
118 transform_type["reset"] = &graphics::Transform::reset;
119 transform_type["rotate"] = &graphics::Transform::rotate;
120 transform_type["scale"] = &graphics::Transform::scale;
121 transform_type["set_origin"] = &graphics::Transform::set_origin;
122 transform_type["set_pos"] = &graphics::Transform::set_pos;
123 transform_type["set_rotation"] = &graphics::Transform::set_rotation;
124 transform_type["set_scale_horizontal"] = &graphics::Transform::set_scale_horizontal;
125 transform_type["set_scale_vertical"] = &graphics::Transform::set_scale_vertical;
126 transform_type["translate"] = &graphics::Transform::translate;
127
128 // clang-format off
129 lua.new_enum<graphics::Text::Alignment>("TextAlignment",
130 {
134 });
135 // clang-format on
136
137 auto font_type = lua.new_usertype<graphics::Font>("Font", sol::constructors<graphics::Font()>());
138 font_type["build"] = &graphics::Font::build;
139 font_type["get_text_size"] = &graphics::Font::get_text_size;
140 font_type["load_file"] = sol::resolve<bool(const std::string&)>(&graphics::Font::load);
141 font_type["load_mem"] = sol::resolve<bool(unsigned char*, const unsigned int)>(&graphics::Font::load);
142 font_type["vertical_advance"] = &graphics::Font::vertical_advance;
143
144 auto fc_type = lua.new_usertype<graphics::FontContext>("Font", sol::no_constructor);
145 fc_type["set_dpi"] = &graphics::FontContext::set_dpi;
146
147 auto text_type = lua.new_usertype<graphics::Text>("Text", sol::constructors<graphics::Font()>());
148 text_type["create"] = &graphics::Text::create;
149 text_type["get_alignment"] = &graphics::Text::get_alignment;
150 text_type["get_font"] = &graphics::Text::get_font;
151 text_type["get_size"] = &graphics::Text::get_size;
152 text_type["get_text"] = &graphics::Text::get_text;
153 text_type["height"] = &graphics::Text::height;
154 text_type["colour"] = &graphics::Text::m_colour;
155 text_type["update"] = sol::resolve<void(std::string_view)>(&graphics::Text::update);
156 text_type["update_size"] = sol::resolve<void(const float)>(&graphics::Text::update);
157 text_type["update_alignment"] = sol::resolve<void(const graphics::Text::Alignment)>(&graphics::Text::update);
158 text_type["set_font"] = &graphics::Text::set_font;
159 text_type["width"] = &graphics::Text::width;
160
161 auto circle_type = lua.new_usertype<graphics::Circle>("Circle", sol::constructors<graphics::Circle()>());
162 circle_type["create"] = &graphics::Circle::create;
163 circle_type["fragments"] = &graphics::Circle::fragments;
164 circle_type["height"] = &graphics::Circle::height;
165 circle_type["mode"] = &graphics::Circle::mode;
166 circle_type["colour"] = &graphics::Circle::m_colour;
167 circle_type["radius"] = &graphics::Circle::radius;
168 circle_type["width"] = &graphics::Circle::width;
169
170 auto ellipse_type = lua.new_usertype<graphics::Ellipse>("Ellipse", sol::constructors<graphics::Ellipse()>());
171 ellipse_type["create"] = &graphics::Ellipse::create;
172 ellipse_type["fragments"] = &graphics::Ellipse::fragments;
173 ellipse_type["height"] = &graphics::Ellipse::height;
174 ellipse_type["mode"] = &graphics::Ellipse::mode;
175 ellipse_type["colour"] = &graphics::Ellipse::m_colour;
176 ellipse_type["radii"] = &graphics::Ellipse::radii;
177 ellipse_type["width"] = &graphics::Ellipse::width;
178
179 auto point_type = lua.new_usertype<graphics::Point>("Point", sol::constructors<graphics::Point()>());
180 point_type["create"] = &graphics::Point::create;
181 point_type["height"] = &graphics::Point::height;
182 point_type["mode"] = &graphics::Point::mode;
183 point_type["colour"] = &graphics::Point::m_colour;
184 point_type["pos"] = &graphics::Point::pos;
185 point_type["width"] = &graphics::Point::width;
186
187 auto polygon_type = lua.new_usertype<graphics::Polygon>("Polygon", sol::constructors<graphics::Polygon()>());
188 polygon_type["create"] = &graphics::Polygon::create;
189 polygon_type["height"] = &graphics::Polygon::height;
190 polygon_type["mode"] = &graphics::Polygon::mode;
191 polygon_type["colour"] = &graphics::Polygon::m_colour;
192 polygon_type["points"] = &graphics::Polygon::points;
193 polygon_type["width"] = &graphics::Polygon::width;
194
195 auto polyline_type = lua.new_usertype<graphics::Polyline>("Polyline", sol::constructors<graphics::Polyline()>());
196 polyline_type["create"] = &graphics::Polyline::create;
197 polyline_type["height"] = &graphics::Polyline::height;
198 polyline_type["mode"] = &graphics::Polyline::mode;
199 polyline_type["colour"] = &graphics::Polyline::m_colour;
200 polyline_type["points"] = &graphics::Polyline::points;
201 polyline_type["width"] = &graphics::Polyline::width;
202 }
203 } // namespace lua
204} // namespace galaxy
2D animation information.
Definition Animation.hpp:28
double m_speed
Speed of the animation. Multiplier, so 1.0f is regular speed.
void prev()
Go back to the previous frame.
bool load(const std::string &file)
Load a json file from VFS and set frames from that.
Definition Animation.cpp:71
std::size_t index() const
Get current frame index.
meta::vector< Frame > & frames()
Get the frames.
std::string m_name
Animation name.
std::size_t total() const
Get total frames.
void restart()
Sets animation back to beginning.
void next()
Advance to the next frame.
Frame & current()
Get currently active frame.
Orthographic 2D camera.
Definition Camera.hpp:28
const glm::mat4 & get_proj()
Get the Camera projection.
Definition Camera.cpp:124
void set_viewport(const float width, const float height)
Set Viewport.
Definition Camera.cpp:92
const glm::vec2 & get_viewport() const
Get camera viewport.
Definition Camera.cpp:113
float m_translation_speed
Movement speed.
Definition Camera.hpp:182
void translate(const float x, const float y) override
Translate (move) position.
Definition Camera.cpp:97
Data & get_data()
Get camera view and proj.
Definition Camera.cpp:130
void rotate(const float degrees) override
Rotate entity.
Definition Camera.cpp:105
bool m_allow_rotation
Allow camera to rotate.
Definition Camera.hpp:177
const glm::mat4 & get_model_view()
Retrieve internal transformation matrix.
Definition Camera.cpp:118
float m_rotation_speed
Rotational speed.
Definition Camera.hpp:187
float radius() const
Get radius.
Definition Circle.cpp:88
float fragments() const
Get fragments.
Definition Circle.cpp:83
void create(const float fragments, const float radius)
Create the circle.
Definition Circle.cpp:52
Represents an RGBA colour.
Definition Colour.hpp:27
void set_g(const std::uint8_t g)
Green.
Definition Colour.cpp:86
R r() const
Get red.
B b() const
Get blue.
static const constexpr std::uint8_t OPAQUE
Opaque colour.
Definition Colour.hpp:32
void set_r(const std::uint8_t r)
Red.
Definition Colour.cpp:68
void set_rgba(const glm::vec4 &rgba)
Set RGBA.
Definition Colour.cpp:140
G g() const
Get green.
void set_b(const std::uint8_t b)
Blue.
Definition Colour.cpp:104
void set_a(const std::uint8_t a)
Alpha.
Definition Colour.cpp:122
A a() const
Get alpha.
static const constexpr std::uint8_t TRANSPARENT
Transparent colour.
Definition Colour.hpp:37
An elliptical circle.
Definition Ellipse.hpp:21
float fragments() const
Get fragments.
Definition Ellipse.cpp:92
const glm::vec2 & radii() const
Get radii.
Definition Ellipse.cpp:97
void create(const float fragments, const glm::vec2 &radii)
Create the ellipse.
Definition Ellipse.cpp:52
MSDF OpenGL configuration.
void set_dpi(const float x, const float y) const
Set font DPI.
A font is a MSDF atlas of glyphs.
Definition Font.hpp:25
void build()
Build the font atlas.
Definition Font.cpp:107
bool load(const std::string &file)
Loads the font and sets up characters.
Definition Font.cpp:67
glm::vec2 get_text_size(const std::string &text, const float size)
Query text size with font.
Definition Font.cpp:130
float vertical_advance(const float size) const
Get vertical advance for drawing multiline text.
Definition Font.cpp:125
A single point.
Definition Point.hpp:21
void create(const glm::vec2 &pos)
Initial point positon.
Definition Point.cpp:27
const glm::vec2 & pos() const
Get pos.
Definition Point.cpp:40
A closed loop line collection of points. I.e. A square, triangle, etc.
Definition Polygon.hpp:22
void create(const meta::vector< glm::vec2 > &points)
Create shape.
Definition Polygon.cpp:44
const meta::vector< glm::vec2 > & points() const
Get list of points.
Definition Polygon.cpp:76
An open looped line of points.
Definition Polyline.hpp:22
void create(const meta::vector< glm::vec2 > &points)
List of points to create a line from.
Definition Polyline.cpp:44
const meta::vector< glm::vec2 > & points() const
Get list of points.
Definition Polyline.cpp:66
Colour m_colour
Used by all primitives.
Definition Shape.hpp:84
float height() const
Get texture height.
Definition Shape.cpp:60
unsigned int mode() const
Get OpenGL rendering mode.
Definition Shape.cpp:50
float width() const
Get texture width.
Definition Shape.cpp:55
String of glyphs rendered with a font.
Definition Text.hpp:23
const std::string & get_font() const
Get font ID.
Definition Text.cpp:205
const std::string & get_text() const
Get text.
Definition Text.cpp:190
Alignment get_alignment() const
Get the text alignment.
Definition Text.cpp:200
void create(std::string_view text, const float size, const std::string &font, const graphics::Colour &colour, Alignment alignment=Alignment::LEFT)
Creates the rendered text.
Definition Text.cpp:71
Alignment
Text alignment.
Definition Text.hpp:29
void set_font(std::string_view font)
Update the font used.
Definition Text.cpp:84
graphics::Colour m_colour
Text colour.
Definition Text.hpp:184
float height() const
Get text height.
Definition Text.cpp:185
void update()
Update the rendered text.
Definition Text.cpp:89
float get_size() const
Get text em size.
Definition Text.cpp:195
float width() const
Get text width.
Definition Text.cpp:180
Defines the 2D transformation of an entity.
Definition Transform.hpp:21
const glm::vec2 & get_origin() const
Get origin point.
void set_scale_vertical(const float y)
Set entity scale.
virtual void rotate(const float degrees)
Rotate entity.
Definition Transform.cpp:94
void set_pos(const float x, const float y)
Sets position without moving the entity.
void set_origin(const float x, const float y)
Set the origin point.
const glm::vec2 & get_pos() const
Get stored position.
virtual void translate(const float x, const float y)
Translate (move) position.
Definition Transform.cpp:86
const glm::vec2 & get_scale() const
Get stored scale.
float get_rotation() const
Get stored rotation.
void set_rotation(const float degrees)
Set the entity rotation.
void reset()
Reset transform.
void set_scale_horizontal(const float x)
Set entity scale.
void scale(const float scale)
Set entity scale.
glm::mat4 & get_transform()
Retrieve internal transformation matrix.
void inject_graphics()
Animated.cpp galaxy.
Definition Animated.cpp:16
glm::mat4 m_model_view
Combined transformation matrix.
Definition Camera.hpp:40
glm::mat4 m_projection
Camera projection matrix.
Definition Camera.hpp:45
Single frame of an animation.
Definition Frame.hpp:21
math::fRect m_bounds
Texture bounds.
Definition Frame.hpp:25
double m_duration
Time to spend on this frame in milliseconds.
Definition Frame.hpp:30
Data to be passed to the renderer.
unsigned int mode
Type to render i.e. GL_LINES, GL_TRIANGLES, etc.
unsigned int vao
OpenGL vertex array object.
unsigned int count
OpenGL index (element) buffer count.
void * offset
Offset in vertex buffer marking beginning of element data.
int instances
Number of instances to render.
int layer
Layer to render on.
RenderData uniforms
Shader uniform data.
Uniform data passed to shader from entity.
glm::mat4 transform
Orthographic transform.
bool textured
Are we rendering as a texture.
glm::vec4 colour
Colour / tint.
bool point
Is this being rendered with GL_POINTS.