naev 0.10.4
nlua_gfx.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11
12#include "naev.h"
15#include "nlua_gfx.h"
16
17#include "font.h"
18#include "log.h"
19#include "ndata.h"
20#include "nlua_colour.h"
21#include "nlua_font.h"
22#include "nlua_shader.h"
23#include "nlua_tex.h"
24#include "nlua_transform.h"
25#include "nlua_canvas.h"
26#include "nlua_vec2.h"
27#include "nluadef.h"
28#include "opengl.h"
29#include "array.h"
30
31/* GFX methods. */
32static int gfxL_dim( lua_State *L );
33static int gfxL_screencoords( lua_State *L );
34static int gfxL_renderTex( lua_State *L );
35static int gfxL_renderTexRaw( lua_State *L );
36static int gfxL_renderTexScale( lua_State *L );
37static int gfxL_renderTexH( lua_State *L );
38static int gfxL_renderRect( lua_State *L );
39static int gfxL_renderRectH( lua_State *L );
40static int gfxL_renderCircle( lua_State *L );
41static int gfxL_renderCircleH( lua_State *L );
42static int gfxL_renderLinesH( lua_State *L );
43static int gfxL_clearDepth( lua_State *L );
44static int gfxL_fontSize( lua_State *L );
45/* TODO get rid of printDim and print in favour of printfDim and printf */
46static int gfxL_printfDim( lua_State *L );
47static int gfxL_printfWrap( lua_State *L );
48static int gfxL_printRestoreClear( lua_State *L );
49static int gfxL_printRestoreLast( lua_State *L );
50static int gfxL_printf( lua_State *L );
51static int gfxL_printH( lua_State *L );
52static int gfxL_printDim( lua_State *L );
53static int gfxL_print( lua_State *L );
54static int gfxL_printText( lua_State *L );
55static int gfxL_setBlendMode( lua_State *L );
56static int gfxL_setScissor( lua_State *L );
57static int gfxL_screenshot( lua_State *L );
58static const luaL_Reg gfxL_methods[] = {
59 /* Information. */
60 { "dim", gfxL_dim },
61 { "screencoords", gfxL_screencoords },
62 /* Render stuff. */
63 { "renderTex", gfxL_renderTex },
64 { "renderTexRaw", gfxL_renderTexRaw },
65 { "renderTexScale", gfxL_renderTexScale },
66 { "renderTexH", gfxL_renderTexH },
67 { "renderRect", gfxL_renderRect },
68 { "renderRectH", gfxL_renderRectH },
69 { "renderCircle", gfxL_renderCircle },
70 { "renderCircleH", gfxL_renderCircleH },
71 { "renderLinesH", gfxL_renderLinesH },
72 /* Printing. */
73 { "clearDepth", gfxL_clearDepth },
74 { "fontSize", gfxL_fontSize },
75 { "printfDim", gfxL_printfDim },
76 { "printfWrap", gfxL_printfWrap },
77 { "printRestoreClear", gfxL_printRestoreClear },
78 { "printRestoreLast", gfxL_printRestoreLast },
79 { "printf", gfxL_printf },
80 { "printH", gfxL_printH },
81 { "printDim", gfxL_printDim },
82 { "print", gfxL_print },
83 { "printText", gfxL_printText },
84 /* Misc. */
85 { "setBlendMode", gfxL_setBlendMode },
86 { "setScissor", gfxL_setScissor },
87 { "screenshot", gfxL_screenshot },
88 {0,0}
89};
97int nlua_loadGFX( nlua_env env )
98{
99 /* Register the values */
100 nlua_register(env, "gfx", gfxL_methods, 0);
101
102 /* We also load the texture, colour, font, and transform modules as dependencies. */
103 nlua_loadCol( env );
104 nlua_loadTex( env );
105 nlua_loadFont( env );
106 nlua_loadTransform( env );
107 nlua_loadShader( env );
108 nlua_loadCanvas( env );
109
110 return 0;
111}
112
136static int gfxL_dim( lua_State *L )
137{
138 if (lua_isboolean(L,1)) {
139 lua_pushnumber( L, SCREEN_W );
140 lua_pushnumber( L, SCREEN_H );
141 }
142 else {
143 lua_pushnumber( L, gl_screen.nw );
144 lua_pushnumber( L, gl_screen.nh );
145 }
146 lua_pushnumber( L, gl_screen.scale );
147 return 3;
148}
149
158static int gfxL_screencoords( lua_State *L )
159{
160 vec2 screen;
161 vec2 *game = luaL_checkvector( L, 1 );
162 int invert = lua_toboolean( L, 2 );
163 gl_gameToScreenCoords( &screen.x, &screen.y, game->x, game->y );
164 if (invert)
165 screen.y = SCREEN_H-screen.y;
166 lua_pushvector( L, screen );
167 return 1;
168}
169
188static int gfxL_renderTex( lua_State *L )
189{
190 glTexture *tex;
191 glColour *col;
192 double x, y;
193 int sx, sy;
194
195
196 /* Parameters. */
197 col = NULL;
198 tex = luaL_checktex( L, 1 );
199 x = luaL_checknumber( L, 2 );
200 y = luaL_checknumber( L, 3 );
201 if (lua_isnumber( L, 4 )) {
202 sx = luaL_checkinteger( L, 4 ) - 1;
203 sy = luaL_checkinteger( L, 5 ) - 1;
204 if (lua_iscolour(L, 6))
205 col = luaL_checkcolour(L,6);
206 }
207 else {
208 sx = 0;
209 sy = 0;
210 if (lua_iscolour(L, 4))
211 col = luaL_checkcolour(L,4);
212 }
213
214 /* Some safety checking. */
215#if DEBUGGING
216 if (sx >= tex->sx)
217 NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (X position) sprite: %d > %d."),
218 tex->name, sx+1, tex->sx );
219 if (sx >= tex->sx)
220 NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (Y position) sprite: %d > %d."),
221 tex->name, sy+1, tex->sy );
222#endif /* DEBUGGING */
223
224 /* Render. */
225 gl_renderStaticSprite( tex, x, y, sx, sy, col );
226
227 return 0;
228}
229
243static int gfxL_renderTexScale( lua_State *L )
244{
245 glTexture *tex;
246 glColour *col;
247 double x, y, bw, bh;
248 int sx, sy;
249
250
251 /* Parameters. */
252 col = NULL;
253 tex = luaL_checktex( L, 1 );
254 x = luaL_checknumber( L, 2 );
255 y = luaL_checknumber( L, 3 );
256 bw = luaL_checknumber( L, 4 );
257 bh = luaL_checknumber( L, 5 );
258 if (lua_isnumber( L, 6 )) {
259 sx = luaL_checkinteger( L, 6 ) - 1;
260 sy = luaL_checkinteger( L, 7 ) - 1;
261 if (lua_iscolour(L, 8))
262 col = luaL_checkcolour( L, 8 );
263 }
264 else {
265 sx = 0;
266 sy = 0;
267 if (lua_iscolour(L, 6))
268 col = luaL_checkcolour(L,4);
269 }
270
271 /* Some safety checking. */
272#if DEBUGGING
273 if (sx >= tex->sx)
274 NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (X position) sprite: %d > %d."),
275 tex->name, sx+1, tex->sx );
276 if (sx >= tex->sx)
277 NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (Y position) sprite: %d > %d."),
278 tex->name, sy+1, tex->sy );
279#endif /* DEBUGGING */
280
281 /* Render. */
282 gl_renderScaleSprite( tex, x, y, sx, sy, bw, bh, col );
283
284 return 0;
285}
286
311static int gfxL_renderTexRaw( lua_State *L )
312{
313 glTexture *t;
314 const glColour *col;
315 double px,py, pw,ph, tx,ty, tw,th;
316 double angle;
317 int sx, sy;
318
319
320 /* Parameters. */
321 t = luaL_checktex( L, 1 );
322 px = luaL_checknumber( L, 2 );
323 py = luaL_checknumber( L, 3 );
324 pw = luaL_checknumber( L, 4 );
325 ph = luaL_checknumber( L, 5 );
326 sx = luaL_optinteger( L, 6, 1 ) - 1;
327 sy = luaL_optinteger( L, 7, 1 ) - 1;
328 tx = luaL_optnumber( L, 8, 0. );
329 ty = luaL_optnumber( L, 9, 0. );
330 tw = luaL_optnumber( L, 10, 1. );
331 th = luaL_optnumber( L, 11, 1. );
332 col = luaL_optcolour(L, 12, &cWhite );
333 angle = luaL_optnumber(L,13,0.);
334
335 /* Some safety checking. */
336#if DEBUGGING
337 if (sx >= t->sx)
338 NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (X position) sprite: %d > %d."),
339 t->name, sx+1, t->sx );
340 if (sx >= t->sx)
341 NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (Y position) sprite: %d > %d."),
342 t->name, sy+1, t->sy );
343#endif /* DEBUGGING */
344
345 /* Translate as needed. */
346 tx = (tx * t->sw + t->sw * (double)(sx)) / t->w;
347 tw = tw * t->srw;
348 if (tw < 0)
349 tx -= tw;
350 ty = (ty * t->sh + t->sh * (t->sy - (double)sy-1)) / t->h;
351 th = th * t->srh;
352 if (th < 0)
353 ty -= th;
354
355 gl_renderTexture( t, px, py, pw, ph, tx, ty, tw, th, col, angle );
356 return 0;
357}
358
368static int gfxL_renderTexH( lua_State *L )
369{
370 glTexture *t;
371 const glColour *col;
372 LuaShader_t *shader;
373 mat4 *H, *TH, ID;
374
375 ID = mat4_identity();
376
377
378 /* Parameters. */
379 t = luaL_checktex( L,1 );
380 shader = luaL_checkshader( L,2 );
381 H = luaL_checktransform( L,3 );
382 col = luaL_optcolour(L,4,&cWhite);
383 TH = luaL_opttransform( L,5,&ID );
384
385 glUseProgram( shader->program );
386
387 /* Set the vertex. */
388 glEnableVertexAttribArray( shader->VertexPosition );
389 gl_vboActivateAttribOffset( gl_squareVBO, shader->VertexPosition,
390 0, 2, GL_FLOAT, 0 );
391
392 /* Set up texture vertices if necessary. */
393 if (shader->VertexTexCoord >= 0) {
394 gl_uniformMat4( shader->ViewSpaceFromLocal, TH );
395 glEnableVertexAttribArray( shader->VertexTexCoord );
396 gl_vboActivateAttribOffset( gl_squareVBO, shader->VertexTexCoord,
397 0, 2, GL_FLOAT, 0 );
398 }
399
400 /* Set the texture(s). */
401 glBindTexture( GL_TEXTURE_2D, t->texture );
402 glUniform1i( shader->MainTex, 0 );
403 for (int i=0; i<array_size(shader->tex); i++) {
404 LuaTexture_t *lt = &shader->tex[i];
405 glActiveTexture( lt->active );
406 glBindTexture( GL_TEXTURE_2D, lt->texid );
407 glUniform1i( lt->uniform, lt->value );
408 }
409 glActiveTexture( GL_TEXTURE0 );
410
411 /* Set shader uniforms. */
412 gl_uniformColor( shader->ConstantColor, col );
413 gl_uniformMat4( shader->ClipSpaceFromLocal, H );
414
415 /* Draw. */
416 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
417
418 /* Clear state. */
419 glDisableVertexAttribArray( shader->VertexPosition );
420 if (shader->VertexTexCoord >= 0)
421 glDisableVertexAttribArray( shader->VertexTexCoord );
422
423 /* anything failed? */
424 gl_checkErr();
425
426 glUseProgram(0);
427
428 return 0;
429}
430
445static int gfxL_renderRect( lua_State *L )
446{
447 glColour *col;
448 double x,y, w,h;
449 int empty;
450
451
452 /* Parse parameters. */
453 x = luaL_checknumber( L, 1 );
454 y = luaL_checknumber( L, 2 );
455 w = luaL_checknumber( L, 3 );
456 h = luaL_checknumber( L, 4 );
457 col = luaL_checkcolour( L, 5 );
458 empty = lua_toboolean( L, 6 );
459
460 /* Render. */
461 if (empty)
462 gl_renderRectEmpty( x, y, w, h, col );
463 else
464 gl_renderRect( x, y, w, h, col );
465
466 return 0;
467}
468
477static int gfxL_renderRectH( lua_State *L )
478{
479 /* Parse parameters. */
480 const mat4 *H = luaL_checktransform(L,1);
481 const glColour *col = luaL_optcolour(L,2,&cWhite);
482 int empty = lua_toboolean(L,3);
483
484 /* Render. */
485 gl_renderRectH( H, col, !empty );
486
487 return 0;
488}
489
500static int gfxL_renderCircle( lua_State *L )
501{
502 glColour *col;
503 double x,y, r;
504 int empty;
505
506
507 /* Parse parameters. */
508 x = luaL_checknumber( L, 1 );
509 y = luaL_checknumber( L, 2 );
510 r = luaL_checknumber( L, 3 );
511 col = luaL_checkcolour( L, 4 );
512 empty = lua_toboolean( L, 5 );
513
514 /* Render. */
515 gl_renderCircle( x, y, r, col, !empty );
516
517 return 0;
518}
519
528static int gfxL_renderCircleH( lua_State *L )
529{
530
531 /* Parse parameters. */
532 const mat4 *H = luaL_checktransform(L,1);
533 const glColour *col = luaL_optcolour(L,2,&cWhite);
534 int empty = lua_toboolean(L,3);
535
536 /* Render. */
537 gl_renderCircleH( H, col, !empty );
538
539 return 0;
540}
541
542static gl_vbo *vbo_lines = NULL;
554static int gfxL_renderLinesH( lua_State *L )
555{
556 GLfloat buf[256*2];
557 int i = 3;
558 GLuint n = 0;
559 const mat4 *H = luaL_checktransform(L,1);
560 const glColour *c = luaL_optcolour(L,2,&cWhite);
561
562 if (vbo_lines==NULL)
563 vbo_lines = gl_vboCreateDynamic( 256*sizeof(GLfloat)*2, NULL );
564
565 while (!lua_isnoneornil(L,i)) {
566 if (n >= 256) {
567 WARN(_("Trying to draw too many lines in one call!"));
568 n = 256;
569 break;
570 }
571
572 if (lua_isnumber(L,i)) {
573 double x = luaL_checknumber(L,i);
574 double y = luaL_checknumber(L,i+1);
575 buf[2*n+0] = x;
576 buf[2*n+1] = y;
577 n++;
578 i+=2;
579 }
580 else {
581 vec2 *v = luaL_checkvector(L,i);
582 buf[2*n+0] = v->x;
583 buf[2*n+1] = v->y;
584 n++;
585 i+=1;
586 }
587 }
588
589 glUseProgram(shaders.lines.program);
590
591 gl_vboData( vbo_lines, sizeof(GLfloat)*2*n, buf );
592 glEnableVertexAttribArray( shaders.lines.vertex );
593 gl_vboActivateAttribOffset( vbo_lines, shaders.lines.vertex, 0,
594 2, GL_FLOAT, 2*sizeof(GLfloat) );
595
596 gl_uniformColor(shaders.lines.colour, c);
597 gl_uniformMat4(shaders.lines.projection, H);
598
599 glDrawArrays( GL_LINE_STRIP, 0, n );
600 glUseProgram(0);
601
602 /* Check for errors. */
603 gl_checkErr();
604
605 return 0;
606}
607
611static int gfxL_clearDepth( lua_State *L )
612{
613 (void) L;
614 glClear( GL_DEPTH_BUFFER_BIT );
615 return 0;
616}
617
625static int gfxL_fontSize( lua_State *L )
626{
627 int small = lua_toboolean(L,1);
628 lua_pushnumber( L, small ? gl_smallFont.h : gl_defFont.h );
629 return 1;
630}
631
643static int gfxL_printDim( lua_State *L )
644{
645 const char *str;
646 int width;
647 glFont *font;
648
649 /* Parse parameters. */
650 font = lua_toboolean(L,1) ? &gl_smallFont : &gl_defFont;
651 str = luaL_checkstring(L,2);
652 width = luaL_optinteger(L,3,0);
653
654 /* Print length. */
655 if (width == 0)
656 lua_pushnumber( L, gl_printWidthRaw( font, str ) );
657 else
658 lua_pushnumber( L, gl_printHeightRaw( font, width, str ) );
659 return 1;
660}
661
670static int gfxL_printfDim( lua_State *L )
671{
672 const char *str;
673 int width;
674 glFont *font;
675
676 /* Parse parameters. */
677 font = luaL_checkfont(L,1);
678 str = luaL_checkstring(L,2);
679 width = luaL_optinteger(L,3,0);
680
681 /* Print length. */
682 if (width == 0)
683 lua_pushnumber( L, gl_printWidthRaw( font, str ) );
684 else
685 lua_pushnumber( L, gl_printHeightRaw( font, width, str ) );
686 return 1;
687}
688
699static int gfxL_printfWrap( lua_State *L )
700{
701 const char *s;
702 int width, maxw;
703 glFont *font;
705 int linenum;
706
707 /* Parse parameters. */
708 font = luaL_checkfont(L,1);
709 s = luaL_checkstring(L,2);
710 width = luaL_checkinteger(L,3);
711 if (width < 0)
712 NLUA_ERROR(L,_("width has to be a positive value."));
713
714 /* Process output into table. */
715 lua_newtable(L);
716 gl_printLineIteratorInit(&iter, font, s, width);
717 linenum = 1;
718 maxw = 0;
719 while (gl_printLineIteratorNext(&iter)) {
720 maxw = MAX(maxw, iter.l_width);
721
722 /* Create entry of form { string, width } in the table. */
723 lua_newtable(L); /* t, t */
724 lua_pushlstring(L, &s[iter.l_begin], iter.l_end - iter.l_begin); /* t, t, s */
725 lua_rawseti(L,-2,1); /* t, t */
726 lua_pushinteger(L,iter.l_width); /* t, t, n */
727 lua_rawseti(L,-2,2); /* t, t */
728 lua_rawseti(L,-2,linenum++); /* t */
729 }
730
731 /* Push max width. */
732 lua_pushinteger(L, maxw);
733
734 return 2;
735}
736
741static int gfxL_printRestoreClear( lua_State *L )
742{
743 (void) L;
745 return 0;
746}
747
752static int gfxL_printRestoreLast( lua_State *L )
753{
754 (void) L;
756 return 0;
757}
758
773static int gfxL_printf( lua_State *L )
774{
775 glFont *font;
776 const char *str;
777 double x, y;
778 glColour *col;
779 int max, mid;
780
781
782 /* Parse parameters. */
783 font = luaL_checkfont(L,1);
784 str = luaL_checkstring(L,2);
785 x = luaL_checknumber(L,3);
786 y = luaL_checknumber(L,4);
787 col = luaL_checkcolour(L,5);
788 max = luaL_optinteger(L,6,0);
789 mid = lua_toboolean(L,7);
790
791 /* Render. */
792 if (mid)
793 gl_printMidRaw( font, max, x, y, col, 0., str );
794 else if (max > 0)
795 gl_printMaxRaw( font, max, x, y, col, 0., str );
796 else
797 gl_printRaw( font, x, y, col, 0., str );
798 return 0;
799}
800
811static int gfxL_printH( lua_State *L )
812{
813 const mat4 *H;
814 glFont *font;
815 const char *str;
816 const glColour *col;
817 double outline;
818
819
820 /* Parse parameters. */
821 H = luaL_checktransform(L,1);
822 font = luaL_checkfont(L,2);
823 str = luaL_checkstring(L,3);
824 col = luaL_optcolour(L,4,&cWhite);
825 outline = luaL_optnumber(L,5,0.);
826
827 /* Render. */
828 gl_printRawH( font, H, col, outline, str );
829 return 0;
830}
831
848static int gfxL_print( lua_State *L )
849{
850 glFont *font;
851 const char *str;
852 double x, y;
853 glColour *col;
854 int max, mid;
855
856 /* Parse parameters. */
857 font = lua_toboolean(L,1) ? &gl_smallFont : &gl_defFont;
858 str = luaL_checkstring(L,2);
859 x = luaL_checknumber(L,3);
860 y = luaL_checknumber(L,4);
861 col = luaL_checkcolour(L,5);
862 max = luaL_optinteger(L,6,0);
863 mid = lua_toboolean(L,7);
864
865 /* Render. */
866 if (mid)
867 gl_printMidRaw( font, max, x, y, col, -1., str );
868 else if (max > 0)
869 gl_printMaxRaw( font, max, x, y, col, -1., str );
870 else
871 gl_printRaw( font, x, y, col, -1., str );
872 return 0;
873}
874
890static int gfxL_printText( lua_State *L )
891{
892 glFont *font;
893 const char *str;
894 int w, h, lh;
895 double x, y;
896 glColour *col;
897
898
899 /* Parse parameters. */
900 font = lua_toboolean(L,1) ? &gl_smallFont : &gl_defFont;
901 str = luaL_checkstring(L,2);
902 x = luaL_checknumber(L,3);
903 y = luaL_checknumber(L,4);
904 w = luaL_checkinteger(L,5);
905 h = luaL_checkinteger(L,6);
906 col = luaL_checkcolour(L,7);
907 lh = luaL_optinteger(L,8,0);
908
909 /* Render. */
910 gl_printTextRaw( font, w, h, x, y, lh, col, -1., str );
911
912 return 0;
913}
914
924static int gfxL_setBlendMode( lua_State *L )
925{
926 const char *mode, *alphamode;
927 GLenum func, srcRGB, srcA, dstRGB, dstA;
928
929 /* Parse parameters. */
930 mode = luaL_checkstring(L,1);
931 alphamode= luaL_optstring(L,2,"alphamultiply");
932
933 /* Translate to OpenGL enums. */
934 func = GL_FUNC_ADD;
935 srcRGB = GL_ONE;
936 srcA = GL_ONE;
937 dstRGB = GL_ZERO;
938 dstA = GL_ZERO;
939
940 if (!strcmp( alphamode, "alphamultiply" ))
941 srcRGB = srcA = GL_SRC_ALPHA;
942 else if (!strcmp( alphamode, "premultiplied" )) {
943 if (!strcmp( mode, "lighten" ) || !strcmp( mode, "darken" ) || !strcmp( mode, "multiply" ))
944 NLUA_INVALID_PARAMETER(L);
945 }
946 else
947 NLUA_INVALID_PARAMETER(L);
948
949 if (!strcmp( mode, "alpha" ))
950 dstRGB = dstA = GL_ONE_MINUS_SRC_ALPHA;
951 else if (!strcmp( mode, "multiply" ))
952 srcRGB = srcA = GL_DST_COLOR;
953 else if (!strcmp( mode, "subtract" ))
954 func = GL_FUNC_REVERSE_SUBTRACT;
955 else if (!strcmp( mode, "add" ) ) {
956 func = GL_FUNC_REVERSE_SUBTRACT;
957 srcA = GL_ZERO;
958 dstRGB = dstA = GL_ONE;
959 }
960 else if (!strcmp( mode, "lighten" ))
961 func = GL_MAX;
962 else if (!strcmp( mode, "darken" ))
963 func = GL_MIN;
964 else if (!strcmp( mode, "screen" ))
965 dstRGB = dstA = GL_ONE_MINUS_SRC_COLOR;
966 else if (strcmp( mode, "replace" ))
967 NLUA_INVALID_PARAMETER(L);
968
969 glBlendEquation(func);
970 glBlendFuncSeparate(srcRGB, dstRGB, srcA, dstA);
971 gl_checkErr();
972
973 return 0;
974}
975
987static int gfxL_setScissor( lua_State *L )
988{
989 if (lua_gettop(L)>0) {
990 GLint x = luaL_optinteger(L,1,0);
991 GLint y = luaL_optinteger(L,2,0);
992 GLsizei w = luaL_optinteger(L,3,0);
993 GLsizei h = luaL_optinteger(L,4,0);
994 gl_clipRect( x, y, w, h );
995 }
996 else
998
999 return 0;
1000}
1001
1009static int gfxL_screenshot( lua_State * L )
1010{
1011 LuaCanvas_t *lc;
1012 int mustfree;
1013
1014 /* Set up canvas or try to reuse. */
1015 if (lua_iscanvas(L,1)) {
1016 lc = luaL_checkcanvas(L,1);
1017 mustfree = 0;
1018 }
1019 else {
1020 lc = calloc( 1, sizeof(LuaCanvas_t) );
1021 canvas_new( lc, gl_screen.rw, gl_screen.rh );
1022 mustfree = 1;
1023 }
1024
1025 /* Copy over. */
1026 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
1027 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, lc->fbo);
1028 /* We flip it over because that seems to be what love2d API wants. */
1029 glBlitFramebuffer(0, 0, gl_screen.rw, gl_screen.rh, 0, lc->tex->h, lc->tex->w, 0, GL_COLOR_BUFFER_BIT, GL_NEAREST);
1030 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
1031 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
1032
1033 /* Return new or old canvas. */
1034 lua_pushcanvas(L, *lc);
1035 if (mustfree)
1036 free( lc );
1037 return 1;
1038}
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition: array.h:168
int gl_printHeightRaw(const glFont *ft_font, const int width, const char *text)
Gets the height of a non-formatted string.
Definition: font.c:1026
void gl_printRestoreClear(void)
Clears the restoration.
Definition: font.c:377
int gl_printLineIteratorNext(glPrintLineIterator *iter)
Updates iter with the next line's information.
Definition: font.c:525
glFont gl_smallFont
Definition: font.c:154
void gl_printRaw(const glFont *ft_font, double x, double y, const glColour *c, double outlineR, const char *text)
Prints text on screen.
Definition: font.c:616
int gl_printWidthRaw(const glFont *ft_font, const char *text)
Gets the width that it would take to print some text.
Definition: font.c:960
glFont gl_defFont
Definition: font.c:153
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.
Definition: font.c:787
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.
Definition: font.c:869
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.
Definition: font.c:647
void gl_printLineIteratorInit(glPrintLineIterator *iter, const glFont *ft_font, const char *text, int width)
Initialize an iterator object for breaking text into lines.
Definition: font.c:505
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.
Definition: font.c:720
void gl_printRestoreLast(void)
Restores last colour.
Definition: font.c:385
mat4 mat4_identity(void)
Creates an identity matrix.
Definition: mat4.c:195
Header file with generic functions and naev-specifics.
#define MAX(x, y)
Definition: naev.h:39
int lua_iscolour(lua_State *L, int ind)
Checks to see if ind is a colour.
Definition: nlua_colour.c:118
int nlua_loadCol(nlua_env env)
Loads the colour library.
Definition: nlua_colour.c:52
glColour * luaL_checkcolour(lua_State *L, int ind)
Gets colour at index or raises error if there is no colour at index.
Definition: nlua_colour.c:89
int nlua_loadFont(nlua_env env)
Loads the font library.
Definition: nlua_font.c:46
glFont * luaL_checkfont(lua_State *L, int ind)
Gets font at index or raises error if there is no font at index.
Definition: nlua_font.c:75
static int gfxL_renderTexScale(lua_State *L)
DEPRECATED: Renders a texture, scaled. Ultimately, love.graphics should handle this.
Definition: nlua_gfx.c:243
static int gfxL_renderRectH(lua_State *L)
Renders a rectangle given a transformation matrix.
Definition: nlua_gfx.c:477
static int gfxL_screencoords(lua_State *L)
Gets the screen coordinates from game coordinates.
Definition: nlua_gfx.c:158
static int gfxL_dim(lua_State *L)
Lua bindings to interact with rendering and the Naev graphical environment.
Definition: nlua_gfx.c:136
static int gfxL_renderRect(lua_State *L)
Renders a rectangle.
Definition: nlua_gfx.c:445
static const luaL_Reg gfxL_methods[]
Definition: nlua_gfx.c:58
static int gfxL_printText(lua_State *L)
Prints a block of text on the screen.
Definition: nlua_gfx.c:890
static int gfxL_printf(lua_State *L)
Prints text on the screen using a font.
Definition: nlua_gfx.c:773
static int gfxL_screenshot(lua_State *L)
Takes the current rendered game screen and returns it as a canvas.
Definition: nlua_gfx.c:1009
static int gfxL_renderTexH(lua_State *L)
Renders a texture using a transformation matrix.
Definition: nlua_gfx.c:368
static int gfxL_renderTex(lua_State *L)
Renders a texture.
Definition: nlua_gfx.c:188
static int gfxL_printfWrap(lua_State *L)
Gets the wrap for text.
Definition: nlua_gfx.c:699
static int gfxL_printfDim(lua_State *L)
Gets the size of the text to print.
Definition: nlua_gfx.c:670
static int gfxL_clearDepth(lua_State *L)
Clears the depth buffer.
Definition: nlua_gfx.c:611
static int gfxL_setBlendMode(lua_State *L)
Sets the OpenGL blending mode. See https://love2d.org/wiki/love.graphics.setBlendMode as of version 0...
Definition: nlua_gfx.c:924
static int gfxL_renderLinesH(lua_State *L)
Renders a polyline or set of line segments.
Definition: nlua_gfx.c:554
static int gfxL_renderCircle(lua_State *L)
Renders a circle.
Definition: nlua_gfx.c:500
static int gfxL_printRestoreLast(lua_State *L)
Restores the last saved internal colour state.
Definition: nlua_gfx.c:752
static int gfxL_printH(lua_State *L)
Prints text on the screen using a font with a transformation matirx.
Definition: nlua_gfx.c:811
int nlua_loadGFX(nlua_env env)
Loads the graphics library.
Definition: nlua_gfx.c:97
static int gfxL_renderCircleH(lua_State *L)
Renders a circle given a transformation matrix.
Definition: nlua_gfx.c:528
static int gfxL_fontSize(lua_State *L)
Gets the size of the font.
Definition: nlua_gfx.c:625
static int gfxL_renderTexRaw(lua_State *L)
Renders a texture using the core render function.
Definition: nlua_gfx.c:311
static int gfxL_printDim(lua_State *L)
Gets the size of the text to print.
Definition: nlua_gfx.c:643
static int gfxL_printRestoreClear(lua_State *L)
Clears the saved internal colour state.
Definition: nlua_gfx.c:741
static int gfxL_print(lua_State *L)
Prints text on the screen.
Definition: nlua_gfx.c:848
static int gfxL_setScissor(lua_State *L)
Sets the scissor clipping.
Definition: nlua_gfx.c:987
int nlua_loadShader(nlua_env env)
Loads the shader library.
Definition: nlua_shader.c:57
LuaShader_t * luaL_checkshader(lua_State *L, int ind)
Gets shader at index or raises error if there is no shader at index.
Definition: nlua_shader.c:86
glTexture * luaL_checktex(lua_State *L, int ind)
Gets texture at index or raises error if there is no texture at index.
Definition: nlua_tex.c:100
int nlua_loadTex(nlua_env env)
Loads the texture library.
Definition: nlua_tex.c:62
mat4 * luaL_checktransform(lua_State *L, int ind)
Gets transform at index or raises error if there is no transform at index.
int nlua_loadTransform(nlua_env env)
Loads the transform library.
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
Definition: nlua_vec2.c:124
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition: nlua_vec2.c:139
glInfo gl_screen
Definition: opengl.c:51
void gl_renderRect(double x, double y, double w, double h, const glColour *c)
Renders a rectangle.
Definition: opengl_render.c:90
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.
Definition: opengl_vbo.c:164
void gl_vboActivateAttribOffset(gl_vbo *vbo, GLuint index, GLuint offset, GLint size, GLenum type, GLsizei stride)
Activates a VBO's offset.
Definition: opengl_vbo.c:228
void gl_vboData(gl_vbo *vbo, GLsizei size, const void *data)
Reloads new data or grows the size of the vbo.
Definition: opengl_vbo.c:100
static const double c[]
Definition: rng.c:264
Wrapper to canvass.
Definition: nlua_canvas.h:19
GLuint fbo
Definition: nlua_canvas.h:20
glTexture * tex
Definition: nlua_canvas.h:21
Represents a font in memory.
Definition: font.h:16
int h
Definition: font.h:18
double scale
Definition: opengl.h:48
int rh
Definition: opengl.h:47
int rw
Definition: opengl.h:46
int nw
Definition: opengl.h:43
int nh
Definition: opengl.h:44
The state of a line iteration. This matches the process of rendering text into an on-screen box: An e...
Definition: font.h:40
size_t l_end
Definition: font.h:45
Abstraction for rendering sprite sheets.
Definition: opengl_tex.h:34
double sw
Definition: opengl_tex.h:44
double sh
Definition: opengl_tex.h:45
double w
Definition: opengl_tex.h:38
double sx
Definition: opengl_tex.h:42
double srh
Definition: opengl_tex.h:47
char * name
Definition: opengl_tex.h:35
GLuint texture
Definition: opengl_tex.h:50
double sy
Definition: opengl_tex.h:43
double h
Definition: opengl_tex.h:39
double srw
Definition: opengl_tex.h:46
Definition: mat4.h:10
Represents a 2d vector.
Definition: vec2.h:32
double y
Definition: vec2.h:34
double x
Definition: vec2.h:33