20#include "nlua_canvas.h"
21#include "nlua_colour.h"
23#include "nlua_shader.h"
25#include "nlua_transform.h"
124 nlua_loadCanvas( env );
156 if ( lua_isboolean( L, 1 ) ) {
157 lua_pushnumber( L, SCREEN_W );
158 lua_pushnumber( L, SCREEN_H );
180 int invert = lua_toboolean( L, 2 );
184 vec2_cset( &screen, x, y );
219 x = luaL_checknumber( L, 2 );
220 y = luaL_checknumber( L, 3 );
221 if ( lua_isnumber( L, 4 ) ) {
222 sx = luaL_checkinteger( L, 4 ) - 1;
223 sy = luaL_checkinteger( L, 5 ) - 1;
224 col = luaL_optcolour( L, 6, &cWhite );
228 col = luaL_optcolour( L, 4, &cWhite );
233 if ( sx < 0 || sx >= tex->
sx )
234 return NLUA_ERROR( L,
235 _(
"Texture '%s' trying to render out of bounds (X "
236 "position) sprite: %d > %d." ),
237 tex->
name, sx + 1, tex->
sx );
238 if ( sy < 0 || sy >= tex->
sy )
239 return NLUA_ERROR( L,
240 _(
"Texture '%s' trying to render out of bounds (Y "
241 "position) sprite: %d > %d." ),
242 tex->
name, sy + 1, tex->
sy );
275 x = luaL_checknumber( L, 2 );
276 y = luaL_checknumber( L, 3 );
277 bw = luaL_checknumber( L, 4 );
278 bh = luaL_checknumber( L, 5 );
279 if ( lua_isnumber( L, 6 ) ) {
280 sx = luaL_checkinteger( L, 6 ) - 1;
281 sy = luaL_checkinteger( L, 7 ) - 1;
294 return NLUA_ERROR( L,
295 _(
"Texture '%s' trying to render out of bounds (X "
296 "position) sprite: %d > %d." ),
297 tex->
name, sx + 1, tex->
sx );
299 return NLUA_ERROR( L,
300 _(
"Texture '%s' trying to render out of bounds (Y "
301 "position) sprite: %d > %d." ),
302 tex->
name, sy + 1, tex->
sy );
342 double px, py, pw, ph, tx, ty, tw, th;
348 px = luaL_checknumber( L, 2 );
349 py = luaL_checknumber( L, 3 );
350 pw = luaL_checknumber( L, 4 );
351 ph = luaL_checknumber( L, 5 );
352 sx = luaL_optinteger( L, 6, 1 ) - 1;
353 sy = luaL_optinteger( L, 7, 1 ) - 1;
354 tx = luaL_optnumber( L, 8, 0. );
355 ty = luaL_optnumber( L, 9, 0. );
356 tw = luaL_optnumber( L, 10, 1. );
357 th = luaL_optnumber( L, 11, 1. );
358 col = luaL_optcolour( L, 12, &cWhite );
359 angle = luaL_optnumber( L, 13, 0. );
364 return NLUA_ERROR( L,
365 _(
"Texture '%s' trying to render out of bounds (X "
366 "position) sprite: %d > %d." ),
369 return NLUA_ERROR( L,
370 _(
"Texture '%s' trying to render out of bounds (Y "
371 "position) sprite: %d > %d." ),
376 tx = ( tx * t->
sw + t->
sw * (double)( sx ) ) / t->
w;
380 ty = ( ty * t->
sh + t->
sh * ( t->
sy - (double)sy - 1 ) ) / t->
h;
385 gl_renderTexture( t, px, py, pw, ph, tx, ty, tw, th, col, angle );
411 col = luaL_optcolour( L, 4, &cWhite );
412 TH = luaL_opttransform( L, 5, &ID );
414 glUseProgram( shader->program );
417 glEnableVertexAttribArray( shader->VertexPosition );
422 if ( shader->VertexTexCoord >= 0 ) {
423 gl_uniformMat4( shader->ViewSpaceFromLocal, TH );
424 glEnableVertexAttribArray( shader->VertexTexCoord );
430 glBindTexture( GL_TEXTURE_2D, t->
texture );
431 glUniform1i( shader->MainTex, 0 );
432 for (
int i = 0; i <
array_size( shader->tex ); i++ ) {
434 glActiveTexture( lt->active );
435 glBindTexture( GL_TEXTURE_2D, lt->texid );
436 glUniform1i( lt->uniform, lt->value );
438 glActiveTexture( GL_TEXTURE0 );
441 gl_uniformColour( shader->ConstantColour, col );
442 gl_uniformMat4( shader->ClipSpaceFromLocal, H );
445 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
448 glDisableVertexAttribArray( shader->VertexPosition );
449 if ( shader->VertexTexCoord >= 0 )
450 glDisableVertexAttribArray( shader->VertexTexCoord );
454 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
484 x = luaL_checknumber( L, 1 );
485 y = luaL_checknumber( L, 2 );
486 w = luaL_checknumber( L, 3 );
487 h = luaL_checknumber( L, 4 );
489 empty = lua_toboolean( L, 6 );
512 const glColour *col = luaL_optcolour( L, 2, &cWhite );
513 int empty = lua_toboolean( L, 3 );
538 x = luaL_checknumber( L, 1 );
539 y = luaL_checknumber( L, 2 );
540 r = luaL_checknumber( L, 3 );
542 empty = lua_toboolean( L, 5 );
561 const glColour *col = luaL_optcolour( L, 2, &cWhite );
562 int empty = lua_toboolean( L, 3 );
569static gl_vbo *vbo_lines = NULL;
583 GLfloat buf[256 * 2];
587 const glColour *
c = luaL_optcolour( L, 2, &cWhite );
589 if ( vbo_lines == NULL )
592 while ( !lua_isnoneornil( L, i ) ) {
594 NLUA_WARN( L, _(
"Trying to draw too many lines in one call!" ) );
599 if ( lua_isnumber( L, i ) ) {
600 double x = luaL_checknumber( L, i );
601 double y = luaL_checknumber( L, i + 1 );
608 buf[2 * n + 0] = v->x;
609 buf[2 * n + 1] = v->y;
615 glUseProgram( shaders.lines.program );
617 gl_vboData( vbo_lines,
sizeof( GLfloat ) * 2 * n, buf );
618 glEnableVertexAttribArray( shaders.lines.vertex );
620 2 *
sizeof( GLfloat ) );
622 gl_uniformColour( shaders.lines.colour,
c );
623 gl_uniformMat4( shaders.lines.projection, H );
625 glDrawArrays( GL_LINE_STRIP, 0, n );
630 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
641 glClear( GL_DEPTH_BUFFER_BIT );
643 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
656 int small = lua_toboolean( L, 1 );
683 str = luaL_checkstring( L, 2 );
684 width = luaL_optinteger( L, 3, 0 );
712 str = luaL_checkstring( L, 2 );
713 width = luaL_optinteger( L, 3, 0 );
743 str = luaL_checkstring( L, 2 );
744 width = luaL_checkinteger( L, 3 );
748 lua_pushinteger( L, x );
749 lua_pushinteger( L, y );
773 s = luaL_checkstring( L, 2 );
774 width = luaL_checkinteger( L, 3 );
776 return NLUA_ERROR( L, _(
"width has to be a positive value." ) );
788 lua_pushlstring( L, &s[iter.l_begin],
789 iter.
l_end - iter.l_begin );
790 lua_rawseti( L, -2, 1 );
791 lua_pushinteger( L, iter.
l_width );
792 lua_rawseti( L, -2, 2 );
793 lua_rawseti( L, -2, linenum++ );
797 lua_pushinteger( L, maxw );
849 str = luaL_checkstring( L, 2 );
850 x = luaL_checknumber( L, 3 );
851 y = luaL_checknumber( L, 4 );
853 max = luaL_optinteger( L, 6, 0 );
854 mid = lua_toboolean( L, 7 );
887 str = luaL_checkstring( L, 3 );
888 col = luaL_optcolour( L, 4, &cWhite );
889 outline = luaL_optnumber( L, 5, 0. );
925 str = luaL_checkstring( L, 2 );
926 x = luaL_checknumber( L, 3 );
927 y = luaL_checknumber( L, 4 );
929 max = luaL_optinteger( L, 6, 0 );
930 mid = lua_toboolean( L, 7 );
968 str = luaL_checkstring( L, 2 );
969 x = luaL_checknumber( L, 3 );
970 y = luaL_checknumber( L, 4 );
971 w = luaL_checkinteger( L, 5 );
972 h = luaL_checkinteger( L, 6 );
974 lh = luaL_optinteger( L, 8, 0 );
997 const char *mode, *alphamode;
998 GLenum func, srcRGB, srcA, dstRGB, dstA;
1001 mode = luaL_checkstring( L, 1 );
1002 alphamode = luaL_optstring( L, 2,
"alphamultiply" );
1011 if ( !strcmp( alphamode,
"alphamultiply" ) )
1012 srcRGB = srcA = GL_SRC_ALPHA;
1013 else if ( !strcmp( alphamode,
"premultiplied" ) ) {
1014 if ( !strcmp( mode,
"lighten" ) || !strcmp( mode,
"darken" ) ||
1015 !strcmp( mode,
"multiply" ) )
1016 NLUA_INVALID_PARAMETER( L, 2 );
1018 NLUA_INVALID_PARAMETER( L, 2 );
1020 if ( !strcmp( mode,
"alpha" ) )
1021 dstRGB = dstA = GL_ONE_MINUS_SRC_ALPHA;
1022 else if ( !strcmp( mode,
"multiply" ) )
1023 srcRGB = srcA = GL_DST_COLOR;
1024 else if ( !strcmp( mode,
"subtract" ) )
1025 func = GL_FUNC_REVERSE_SUBTRACT;
1026 else if ( !strcmp( mode,
"add" ) ) {
1027 func = GL_FUNC_REVERSE_SUBTRACT;
1029 dstRGB = dstA = GL_ONE;
1030 }
else if ( !strcmp( mode,
"lighten" ) )
1032 else if ( !strcmp( mode,
"darken" ) )
1034 else if ( !strcmp( mode,
"screen" ) )
1035 dstRGB = dstA = GL_ONE_MINUS_SRC_COLOR;
1036 else if ( strcmp( mode,
"replace" ) )
1037 NLUA_INVALID_PARAMETER( L, 1 );
1039 glBlendEquation( func );
1040 glBlendFuncSeparate( srcRGB, dstRGB, srcA, dstA );
1041 if ( gl_checkErr() )
1042 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
1044 render_needsReset();
1058 GLenum funcRGB, srcRGB, dstRGB;
1061 if ( lua_gettop( L ) <= 3 ) {
1065 glBlendEquation( funcRGB );
1066 glBlendFunc( srcRGB, dstRGB );
1068 GLenum funcA, srcA, dstA;
1075 glBlendEquationSeparate( funcRGB, funcA );
1076 glBlendFuncSeparate( srcRGB, dstRGB, srcA, dstA );
1079 if ( gl_checkErr() )
1080 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
1082 render_needsReset();
1099 if ( lua_gettop( L ) > 0 ) {
1100 GLint x = luaL_optinteger( L, 1, 0 );
1101 GLint y = luaL_optinteger( L, 2, 0 );
1102 GLsizei w = luaL_optinteger( L, 3, 0 );
1103 GLsizei h = luaL_optinteger( L, 4, 0 );
1105 render_needsReset();
1109 if ( gl_checkErr() )
1110 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
1129 if ( lua_iscanvas( L, 1 ) ) {
1130 lc = luaL_checkcanvas( L, 1 );
1139 glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
1140 glBindFramebuffer( GL_DRAW_FRAMEBUFFER, lc->
fbo );
1143 lc->
tex->
w, 0, GL_COLOR_BUFFER_BIT, GL_NEAREST );
1144 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
1147 lua_pushcanvas( L, *lc );
1151 if ( gl_checkErr() )
1152 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
1180 r = luaL_checknumber( L, 1 );
1181 g = luaL_optnumber( L, 2, r );
1182 b = luaL_optnumber( L, 3, r );
1183 if ( !lua_isnoneornil( L, 4 ) ) {
1184 double n = luaL_checknumber( L, 4 ) /
1191 gltf_lightAmbient( r, g, b );
1206 gltf_lightAmbientGet( &r, &g, &b );
1207 lua_pushnumber( L, r );
1208 lua_pushnumber( L, g );
1209 lua_pushnumber( L, b );
1222 gltf_lightIntensity( luaL_checknumber( L, 1 ) );
1234 lua_pushnumber( L, gltf_lightIntensityGet() );
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
int gl_printHeightRaw(const glFont *ft_font, const int width, const char *text)
Gets the height of a non-formatted string.
void gl_printRestoreClear(void)
Clears the restoration.
int gl_printLineIteratorNext(glPrintLineIterator *iter)
Updates iter with the next line's information.
void gl_printRaw(const glFont *ft_font, double x, double y, const glColour *c, double outlineR, const char *text)
Prints text on screen.
int gl_printWidthRaw(const glFont *ft_font, const char *text)
Gets the width that it would take to print some text.
int gl_printMidRaw(const glFont *ft_font, int width, double x, double y, const glColour *c, double outlineR, const char *text)
Displays text centered in position and width.
int gl_printTextRaw(const glFont *ft_font, const int width, const int height, double bx, double by, int line_height, const glColour *c, double outlineR, const char *text)
Prints a block of text that fits in the dimensions given.
void gl_printRawH(const glFont *ft_font, const mat4 *H, const glColour *c, const double outlineR, const char *text)
Prints text on screen using a transformation matrix.
void gl_printLineIteratorInit(glPrintLineIterator *iter, const glFont *ft_font, const char *text, int width)
Initialize an iterator object for breaking text into lines.
int gl_printMaxRaw(const glFont *ft_font, const int max, double x, double y, const glColour *c, double outlineR, const char *text)
Behaves like gl_printRaw but stops displaying text after a certain distance.
void gl_printRestoreLast(void)
Restores last colour.
int gl_printEndRaw(int *xo, int *yo, const glFont *ft_font, int width, const char *text)
Gets the position at which text would end writing.
mat4 mat4_identity(void)
Creates an identity matrix.
Header file with generic functions and naev-specifics.
int lua_iscolour(lua_State *L, int ind)
Checks to see if ind is a colour.
int nlua_loadCol(nlua_env env)
Loads the colour library.
glColour * lua_tocolour(lua_State *L, int ind)
Lua bindings to interact with colours.
glColour * luaL_checkcolour(lua_State *L, int ind)
Gets colour at index or raises error if there is no colour at index.
int nlua_loadFont(nlua_env env)
Loads the font library.
glFont * luaL_checkfont(lua_State *L, int ind)
Gets font at index or raises error if there is no font at index.
static int gfxL_renderTexScale(lua_State *L)
DEPRECATED: Renders a texture, scaled. Ultimately, love.graphics should handle this.
static int gfxL_renderRectH(lua_State *L)
Renders a rectangle given a transformation matrix.
static int gfxL_screencoords(lua_State *L)
Gets the screen coordinates from game coordinates.
static int gfxL_lightAmbient(lua_State *L)
Sets the ambient lighting.
static int gfxL_dim(lua_State *L)
Lua bindings to interact with rendering and the Naev graphical environment.
static int gfxL_renderRect(lua_State *L)
Renders a rectangle.
static const luaL_Reg gfxL_methods[]
static int gfxL_printText(lua_State *L)
Prints a block of text on the screen.
static int gfxL_printf(lua_State *L)
Prints text on the screen using a font.
static int gfxL_screenshot(lua_State *L)
Takes the current rendered game screen and returns it as a canvas.
static int gfxL_renderTexH(lua_State *L)
Renders a texture using a transformation matrix.
static int gfxL_lightAmbientGet(lua_State *L)
Gets the ambient lighting values.
static int gfxL_renderTex(lua_State *L)
Renders a texture.
static int gfxL_printfWrap(lua_State *L)
Gets the wrap for text.
static int gfxL_printfDim(lua_State *L)
Gets the size of the text to print.
static int gfxL_clearDepth(lua_State *L)
Clears the depth buffer.
static int gfxL_setBlendMode(lua_State *L)
Sets the OpenGL blending mode. See https://love2d.org/wiki/love.graphics.setBlendMode as of version 0...
static int gfxL_renderLinesH(lua_State *L)
Renders a polyline or set of line segments.
static int gfxL_setBlendState(lua_State *L)
Sets the OpenGL blending state. See https://love2d.org/wiki/love.graphics.setBlendState as of version...
static int gfxL_lightIntensityGet(lua_State *L)
Gets the light intensity.
static int gfxL_renderCircle(lua_State *L)
Renders a circle.
static int gfxL_glVersion(lua_State *L)
Gets the OpenGL version.
static int gfxL_printRestoreLast(lua_State *L)
Restores the last saved internal colour state.
static int gfxL_printH(lua_State *L)
Prints text on the screen using a font with a transformation matirx.
int nlua_loadGFX(nlua_env env)
Loads the graphics library.
static int gfxL_renderCircleH(lua_State *L)
Renders a circle given a transformation matrix.
static int gfxL_lightIntensity(lua_State *L)
Sets the intensity of the main lights excluding ambient lighting. Multiplies the default radiosity va...
static int gfxL_fontSize(lua_State *L)
Gets the size of the font.
static int gfxL_printfEnd(lua_State *L)
Gets the position at which text would end printing. Can be the middle of a line.
static int gfxL_renderTexRaw(lua_State *L)
Renders a texture using the core render function.
static int gfxL_printDim(lua_State *L)
Gets the size of the text to print.
static int gfxL_printRestoreClear(lua_State *L)
Clears the saved internal colour state.
static int gfxL_print(lua_State *L)
Prints text on the screen.
static int gfxL_setScissor(lua_State *L)
Sets the scissor clipping.
int nlua_loadShader(nlua_env env)
Loads the shader library.
LuaShader_t * luaL_checkshader(lua_State *L, int ind)
Gets shader at index or raises error if there is no shader at index.
glTexture * luaL_checktex(lua_State *L, int ind)
Gets texture at index or raises error if there is no texture at index.
int nlua_loadTex(nlua_env env)
Loads the texture library.
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
GLenum gl_stringToBlendFactor(const char *s)
Gets a blend factor from a string.
GLenum gl_stringToBlendFunc(const char *s)
Gets a blend function from a string.
void gl_renderRect(double x, double y, double w, double h, const glColour *c)
Renders a rectangle.
void gl_gameToScreenCoords(double *nx, double *ny, double bx, double by)
Converts in-game coordinates to screen coordinates.
void gl_renderCircleH(const mat4 *H, const glColour *c, int filled)
Draws a circle.
void gl_renderTexture(const glTexture *texture, double x, double y, double w, double h, double tx, double ty, double tw, double th, const glColour *c, double angle)
Texture blitting backend.
void gl_unclipRect(void)
Clears the 2d clipping planes.
void gl_renderScaleSprite(const glTexture *sprite, double bx, double by, int sx, int sy, double bw, double bh, const glColour *c)
Blits a scaled sprite, position is in absolute screen coordinates.
void gl_renderStaticSprite(const glTexture *sprite, double bx, double by, int sx, int sy, const glColour *c)
Blits a sprite, position is in absolute screen coordinates.
void gl_renderRectEmpty(double x, double y, double w, double h, const glColour *c)
Renders a rectangle.
void gl_renderRectH(const mat4 *H, const glColour *c, int filled)
Renders a rectangle.
void gl_clipRect(int x, int y, int w, int h)
Sets up 2d clipping planes around a rectangle.
void gl_renderCircle(double cx, double cy, double r, const glColour *c, int filled)
Draws a circle.
gl_vbo * gl_vboCreateDynamic(GLsizei size, const void *data)
Creates a dynamic vbo.
void gl_vboActivateAttribOffset(gl_vbo *vbo, GLuint index, GLuint offset, GLint size, GLenum type, GLsizei stride)
Activates a VBO's offset.
void gl_vboData(gl_vbo *vbo, GLsizei size, const void *data)
Reloads new data or grows the size of the vbo.
Represents a font in memory.
The state of a line iteration. This matches the process of rendering text into an on-screen box: An e...
Abstraction for rendering sprite sheets.