naev 0.11.5
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 "render.h"
28#include "nluadef.h"
29#include "opengl.h"
30#include "array.h"
31
32/* GFX methods. */
33static int gfxL_dim( lua_State *L );
34static int gfxL_screencoords( lua_State *L );
35static int gfxL_renderTex( lua_State *L );
36static int gfxL_renderTexRaw( lua_State *L );
37static int gfxL_renderTexScale( lua_State *L );
38static int gfxL_renderTexH( lua_State *L );
39static int gfxL_renderRect( lua_State *L );
40static int gfxL_renderRectH( lua_State *L );
41static int gfxL_renderCircle( lua_State *L );
42static int gfxL_renderCircleH( lua_State *L );
43static int gfxL_renderLinesH( lua_State *L );
44static int gfxL_clearDepth( lua_State *L );
45static int gfxL_fontSize( lua_State *L );
46/* TODO get rid of printDim and print in favour of printfDim and printf */
47static int gfxL_printfDim( lua_State *L );
48static int gfxL_printfWrap( lua_State *L );
49static int gfxL_printRestoreClear( lua_State *L );
50static int gfxL_printRestoreLast( lua_State *L );
51static int gfxL_printf( lua_State *L );
52static int gfxL_printH( lua_State *L );
53static int gfxL_printDim( lua_State *L );
54static int gfxL_print( lua_State *L );
55static int gfxL_printText( lua_State *L );
56static int gfxL_setBlendMode( lua_State *L );
57static int gfxL_setScissor( lua_State *L );
58static int gfxL_screenshot( lua_State *L );
59static const luaL_Reg gfxL_methods[] = {
60 /* Information. */
61 { "dim", gfxL_dim },
62 { "screencoords", gfxL_screencoords },
63 /* Render stuff. */
64 { "renderTex", gfxL_renderTex },
65 { "renderTexRaw", gfxL_renderTexRaw },
66 { "renderTexScale", gfxL_renderTexScale },
67 { "renderTexH", gfxL_renderTexH },
68 { "renderRect", gfxL_renderRect },
69 { "renderRectH", gfxL_renderRectH },
70 { "renderCircle", gfxL_renderCircle },
71 { "renderCircleH", gfxL_renderCircleH },
72 { "renderLinesH", gfxL_renderLinesH },
73 /* Printing. */
74 { "clearDepth", gfxL_clearDepth },
75 { "fontSize", gfxL_fontSize },
76 { "printfDim", gfxL_printfDim },
77 { "printfWrap", gfxL_printfWrap },
78 { "printRestoreClear", gfxL_printRestoreClear },
79 { "printRestoreLast", gfxL_printRestoreLast },
80 { "printf", gfxL_printf },
81 { "printH", gfxL_printH },
82 { "printDim", gfxL_printDim },
83 { "print", gfxL_print },
84 { "printText", gfxL_printText },
85 /* Misc. */
86 { "setBlendMode", gfxL_setBlendMode },
87 { "setScissor", gfxL_setScissor },
88 { "screenshot", gfxL_screenshot },
89 {0,0}
90};
98int nlua_loadGFX( nlua_env env )
99{
100 /* Register the values */
101 nlua_register(env, "gfx", gfxL_methods, 0);
102
103 /* We also load the texture, colour, font, and transform modules as dependencies. */
104 nlua_loadCol( env );
105 nlua_loadTex( env );
106 nlua_loadFont( env );
107 nlua_loadTransform( env );
108 nlua_loadShader( env );
109 nlua_loadCanvas( env );
110
111 return 0;
112}
113
137static int gfxL_dim( lua_State *L )
138{
139 if (lua_isboolean(L,1)) {
140 lua_pushnumber( L, SCREEN_W );
141 lua_pushnumber( L, SCREEN_H );
142 }
143 else {
144 lua_pushnumber( L, gl_screen.nw );
145 lua_pushnumber( L, gl_screen.nh );
146 }
147 lua_pushnumber( L, gl_screen.scale );
148 return 3;
149}
150
159static int gfxL_screencoords( lua_State *L )
160{
161 vec2 screen;
162 double x, y;
163 const vec2 *game = luaL_checkvector( L, 1 );
164 int invert = lua_toboolean( L, 2 );
165 gl_gameToScreenCoords( &x, &y, game->x, game->y );
166 if (invert)
167 y = SCREEN_H-y;
168 vec2_cset( &screen, x, y );
169 lua_pushvector( L, screen );
170 return 1;
171}
172
191static int gfxL_renderTex( lua_State *L )
192{
193 const glTexture *tex;
194 const glColour *col;
195 double x, y;
196 int sx, sy;
197
198 /* Parameters. */
199 tex = luaL_checktex( L, 1 );
200 x = luaL_checknumber( L, 2 );
201 y = luaL_checknumber( L, 3 );
202 if (lua_isnumber( L, 4 )) {
203 sx = luaL_checkinteger( L, 4 )-1;
204 sy = luaL_checkinteger( L, 5 )-1;
205 col = luaL_optcolour( L, 6, &cWhite );
206 }
207 else {
208 sx = 0;
209 sy = 0;
210 col = luaL_optcolour( L, 4, &cWhite );
211 }
212
213 /* Some safety checking. */
214#if DEBUGGING
215 if (sx < 0 || sx >= tex->sx)
216 return NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (X position) sprite: %d > %d."),
217 tex->name, sx+1, tex->sx );
218 if (sy < 0 || sy >= tex->sy)
219 return NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (Y position) sprite: %d > %d."),
220 tex->name, sy+1, tex->sy );
221#endif /* DEBUGGING */
222
223 /* Render. */
224 gl_renderStaticSprite( tex, x, y, sx, sy, col );
225
226 return 0;
227}
228
242static int gfxL_renderTexScale( lua_State *L )
243{
244 glTexture *tex;
245 glColour *col;
246 double x, y, bw, bh;
247 int sx, sy;
248
249 /* Parameters. */
250 col = NULL;
251 tex = luaL_checktex( L, 1 );
252 x = luaL_checknumber( L, 2 );
253 y = luaL_checknumber( L, 3 );
254 bw = luaL_checknumber( L, 4 );
255 bh = luaL_checknumber( L, 5 );
256 if (lua_isnumber( L, 6 )) {
257 sx = luaL_checkinteger( L, 6 ) - 1;
258 sy = luaL_checkinteger( L, 7 ) - 1;
259 if (lua_iscolour(L, 8))
260 col = luaL_checkcolour( L, 8 );
261 }
262 else {
263 sx = 0;
264 sy = 0;
265 if (lua_iscolour(L, 6))
266 col = luaL_checkcolour(L,4);
267 }
268
269 /* Some safety checking. */
270#if DEBUGGING
271 if (sx >= tex->sx)
272 return NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (X position) sprite: %d > %d."),
273 tex->name, sx+1, tex->sx );
274 if (sx >= tex->sx)
275 return NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (Y position) sprite: %d > %d."),
276 tex->name, sy+1, tex->sy );
277#endif /* DEBUGGING */
278
279 /* Render. */
280 gl_renderScaleSprite( tex, x, y, sx, sy, bw, bh, col );
281
282 return 0;
283}
284
309static int gfxL_renderTexRaw( lua_State *L )
310{
311 glTexture *t;
312 const glColour *col;
313 double px,py, pw,ph, tx,ty, tw,th;
314 double angle;
315 int sx, sy;
316
317 /* Parameters. */
318 t = luaL_checktex( L, 1 );
319 px = luaL_checknumber( L, 2 );
320 py = luaL_checknumber( L, 3 );
321 pw = luaL_checknumber( L, 4 );
322 ph = luaL_checknumber( L, 5 );
323 sx = luaL_optinteger( L, 6, 1 ) - 1;
324 sy = luaL_optinteger( L, 7, 1 ) - 1;
325 tx = luaL_optnumber( L, 8, 0. );
326 ty = luaL_optnumber( L, 9, 0. );
327 tw = luaL_optnumber( L, 10, 1. );
328 th = luaL_optnumber( L, 11, 1. );
329 col = luaL_optcolour(L, 12, &cWhite );
330 angle = luaL_optnumber(L,13,0.);
331
332 /* Some safety checking. */
333#if DEBUGGING
334 if (sx >= t->sx)
335 return NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (X position) sprite: %d > %d."),
336 t->name, sx+1, t->sx );
337 if (sx >= t->sx)
338 return NLUA_ERROR( L, _("Texture '%s' trying to render out of bounds (Y position) sprite: %d > %d."),
339 t->name, sy+1, t->sy );
340#endif /* DEBUGGING */
341
342 /* Translate as needed. */
343 tx = (tx * t->sw + t->sw * (double)(sx)) / t->w;
344 tw = tw * t->srw;
345 if (tw < 0)
346 tx -= tw;
347 ty = (ty * t->sh + t->sh * (t->sy - (double)sy-1)) / t->h;
348 th = th * t->srh;
349 if (th < 0)
350 ty -= th;
351
352 gl_renderTexture( t, px, py, pw, ph, tx, ty, tw, th, col, angle );
353 return 0;
354}
355
365static int gfxL_renderTexH( lua_State *L )
366{
367 glTexture *t;
368 const glColour *col;
369 LuaShader_t *shader;
370 mat4 *H, *TH, ID;
371
372 ID = mat4_identity();
373
374 /* Parameters. */
375 t = luaL_checktex( L,1 );
376 shader = luaL_checkshader( L,2 );
377 H = luaL_checktransform( L,3 );
378 col = luaL_optcolour(L,4,&cWhite);
379 TH = luaL_opttransform( L,5,&ID );
380
381 glUseProgram( shader->program );
382
383 /* Set the vertex. */
384 glEnableVertexAttribArray( shader->VertexPosition );
385 gl_vboActivateAttribOffset( gl_squareVBO, shader->VertexPosition,
386 0, 2, GL_FLOAT, 0 );
387
388 /* Set up texture vertices if necessary. */
389 if (shader->VertexTexCoord >= 0) {
390 gl_uniformMat4( shader->ViewSpaceFromLocal, TH );
391 glEnableVertexAttribArray( shader->VertexTexCoord );
392 gl_vboActivateAttribOffset( gl_squareVBO, shader->VertexTexCoord,
393 0, 2, GL_FLOAT, 0 );
394 }
395
396 /* Set the texture(s). */
397 glBindTexture( GL_TEXTURE_2D, t->texture );
398 glUniform1i( shader->MainTex, 0 );
399 for (int i=0; i<array_size(shader->tex); i++) {
400 LuaTexture_t *lt = &shader->tex[i];
401 glActiveTexture( lt->active );
402 glBindTexture( GL_TEXTURE_2D, lt->texid );
403 glUniform1i( lt->uniform, lt->value );
404 }
405 glActiveTexture( GL_TEXTURE0 );
406
407 /* Set shader uniforms. */
408 gl_uniformColour( shader->ConstantColour, col );
409 gl_uniformMat4( shader->ClipSpaceFromLocal, H );
410
411 /* Draw. */
412 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
413
414 /* Clear state. */
415 glDisableVertexAttribArray( shader->VertexPosition );
416 if (shader->VertexTexCoord >= 0)
417 glDisableVertexAttribArray( shader->VertexTexCoord );
418
419 /* anything failed? */
420 gl_checkErr();
421
422 glUseProgram(0);
423
424 return 0;
425}
426
441static int gfxL_renderRect( lua_State *L )
442{
443 glColour *col;
444 double x,y, w,h;
445 int empty;
446
447 /* Parse parameters. */
448 x = luaL_checknumber( L, 1 );
449 y = luaL_checknumber( L, 2 );
450 w = luaL_checknumber( L, 3 );
451 h = luaL_checknumber( L, 4 );
452 col = luaL_checkcolour( L, 5 );
453 empty = lua_toboolean( L, 6 );
454
455 /* Render. */
456 if (empty)
457 gl_renderRectEmpty( x, y, w, h, col );
458 else
459 gl_renderRect( x, y, w, h, col );
460
461 return 0;
462}
463
472static int gfxL_renderRectH( lua_State *L )
473{
474 /* Parse parameters. */
475 const mat4 *H = luaL_checktransform(L,1);
476 const glColour *col = luaL_optcolour(L,2,&cWhite);
477 int empty = lua_toboolean(L,3);
478
479 /* Render. */
480 gl_renderRectH( H, col, !empty );
481
482 return 0;
483}
484
495static int gfxL_renderCircle( lua_State *L )
496{
497 glColour *col;
498 double x,y, r;
499 int empty;
500
501 /* Parse parameters. */
502 x = luaL_checknumber( L, 1 );
503 y = luaL_checknumber( L, 2 );
504 r = luaL_checknumber( L, 3 );
505 col = luaL_checkcolour( L, 4 );
506 empty = lua_toboolean( L, 5 );
507
508 /* Render. */
509 gl_renderCircle( x, y, r, col, !empty );
510
511 return 0;
512}
513
522static int gfxL_renderCircleH( lua_State *L )
523{
524
525 /* Parse parameters. */
526 const mat4 *H = luaL_checktransform(L,1);
527 const glColour *col = luaL_optcolour(L,2,&cWhite);
528 int empty = lua_toboolean(L,3);
529
530 /* Render. */
531 gl_renderCircleH( H, col, !empty );
532
533 return 0;
534}
535
536static gl_vbo *vbo_lines = NULL;
548static int gfxL_renderLinesH( lua_State *L )
549{
550 GLfloat buf[256*2];
551 int i = 3;
552 GLuint n = 0;
553 const mat4 *H = luaL_checktransform(L,1);
554 const glColour *c = luaL_optcolour(L,2,&cWhite);
555
556 if (vbo_lines==NULL)
557 vbo_lines = gl_vboCreateDynamic( 256*sizeof(GLfloat)*2, NULL );
558
559 while (!lua_isnoneornil(L,i)) {
560 if (n >= 256) {
561 WARN(_("Trying to draw too many lines in one call!"));
562 n = 256;
563 break;
564 }
565
566 if (lua_isnumber(L,i)) {
567 double x = luaL_checknumber(L,i);
568 double y = luaL_checknumber(L,i+1);
569 buf[2*n+0] = x;
570 buf[2*n+1] = y;
571 n++;
572 i+=2;
573 }
574 else {
575 const vec2 *v = luaL_checkvector(L,i);
576 buf[2*n+0] = v->x;
577 buf[2*n+1] = v->y;
578 n++;
579 i+=1;
580 }
581 }
582
583 glUseProgram(shaders.lines.program);
584
585 gl_vboData( vbo_lines, sizeof(GLfloat)*2*n, buf );
586 glEnableVertexAttribArray( shaders.lines.vertex );
587 gl_vboActivateAttribOffset( vbo_lines, shaders.lines.vertex, 0,
588 2, GL_FLOAT, 2*sizeof(GLfloat) );
589
590 gl_uniformColour(shaders.lines.colour, c);
591 gl_uniformMat4(shaders.lines.projection, H);
592
593 glDrawArrays( GL_LINE_STRIP, 0, n );
594 glUseProgram(0);
595
596 /* Check for errors. */
597 gl_checkErr();
598
599 return 0;
600}
601
605static int gfxL_clearDepth( lua_State *L )
606{
607 (void) L;
608 glClear( GL_DEPTH_BUFFER_BIT );
609 return 0;
610}
611
619static int gfxL_fontSize( lua_State *L )
620{
621 int small = lua_toboolean(L,1);
622 lua_pushnumber( L, small ? gl_smallFont.h : gl_defFont.h );
623 return 1;
624}
625
637static int gfxL_printDim( lua_State *L )
638{
639 const char *str;
640 int width;
641 glFont *font;
642
643 /* Parse parameters. */
644 font = lua_toboolean(L,1) ? &gl_smallFont : &gl_defFont;
645 str = luaL_checkstring(L,2);
646 width = luaL_optinteger(L,3,0);
647
648 /* Print length. */
649 if (width == 0)
650 lua_pushnumber( L, gl_printWidthRaw( font, str ) );
651 else
652 lua_pushnumber( L, gl_printHeightRaw( font, width, str ) );
653 return 1;
654}
655
664static int gfxL_printfDim( lua_State *L )
665{
666 const char *str;
667 int width;
668 glFont *font;
669
670 /* Parse parameters. */
671 font = luaL_checkfont(L,1);
672 str = luaL_checkstring(L,2);
673 width = luaL_optinteger(L,3,0);
674
675 /* Print length. */
676 if (width == 0)
677 lua_pushnumber( L, gl_printWidthRaw( font, str ) );
678 else
679 lua_pushnumber( L, gl_printHeightRaw( font, width, str ) );
680 return 1;
681}
682
693static int gfxL_printfWrap( lua_State *L )
694{
695 const char *s;
696 int width, maxw;
697 glFont *font;
699 int linenum;
700
701 /* Parse parameters. */
702 font = luaL_checkfont(L,1);
703 s = luaL_checkstring(L,2);
704 width = luaL_checkinteger(L,3);
705 if (width < 0)
706 return NLUA_ERROR(L,_("width has to be a positive value."));
707
708 /* Process output into table. */
709 lua_newtable(L);
710 gl_printLineIteratorInit(&iter, font, s, width);
711 linenum = 1;
712 maxw = 0;
713 while (gl_printLineIteratorNext(&iter)) {
714 maxw = MAX(maxw, iter.l_width);
715
716 /* Create entry of form { string, width } in the table. */
717 lua_newtable(L); /* t, t */
718 lua_pushlstring(L, &s[iter.l_begin], iter.l_end - iter.l_begin); /* t, t, s */
719 lua_rawseti(L,-2,1); /* t, t */
720 lua_pushinteger(L,iter.l_width); /* t, t, n */
721 lua_rawseti(L,-2,2); /* t, t */
722 lua_rawseti(L,-2,linenum++); /* t */
723 }
724
725 /* Push max width. */
726 lua_pushinteger(L, maxw);
727
728 return 2;
729}
730
735static int gfxL_printRestoreClear( lua_State *L )
736{
737 (void) L;
739 return 0;
740}
741
746static int gfxL_printRestoreLast( lua_State *L )
747{
748 (void) L;
750 return 0;
751}
752
767static int gfxL_printf( lua_State *L )
768{
769 glFont *font;
770 const char *str;
771 double x, y;
772 glColour *col;
773 int max, mid;
774
775 /* Parse parameters. */
776 font = luaL_checkfont(L,1);
777 str = luaL_checkstring(L,2);
778 x = luaL_checknumber(L,3);
779 y = luaL_checknumber(L,4);
780 col = luaL_checkcolour(L,5);
781 max = luaL_optinteger(L,6,0);
782 mid = lua_toboolean(L,7);
783
784 /* Render. */
785 if (mid)
786 gl_printMidRaw( font, max, x, y, col, 0., str );
787 else if (max > 0)
788 gl_printMaxRaw( font, max, x, y, col, 0., str );
789 else
790 gl_printRaw( font, x, y, col, 0., str );
791 return 0;
792}
793
804static int gfxL_printH( lua_State *L )
805{
806 const mat4 *H;
807 glFont *font;
808 const char *str;
809 const glColour *col;
810 double outline;
811
812 /* Parse parameters. */
813 H = luaL_checktransform(L,1);
814 font = luaL_checkfont(L,2);
815 str = luaL_checkstring(L,3);
816 col = luaL_optcolour(L,4,&cWhite);
817 outline = luaL_optnumber(L,5,0.);
818
819 /* Render. */
820 gl_printRawH( font, H, col, outline, str );
821 return 0;
822}
823
840static int gfxL_print( lua_State *L )
841{
842 glFont *font;
843 const char *str;
844 double x, y;
845 glColour *col;
846 int max, mid;
847
848 /* Parse parameters. */
849 font = lua_toboolean(L,1) ? &gl_smallFont : &gl_defFont;
850 str = luaL_checkstring(L,2);
851 x = luaL_checknumber(L,3);
852 y = luaL_checknumber(L,4);
853 col = luaL_checkcolour(L,5);
854 max = luaL_optinteger(L,6,0);
855 mid = lua_toboolean(L,7);
856
857 /* Render. */
858 if (mid)
859 gl_printMidRaw( font, max, x, y, col, -1., str );
860 else if (max > 0)
861 gl_printMaxRaw( font, max, x, y, col, -1., str );
862 else
863 gl_printRaw( font, x, y, col, -1., str );
864 return 0;
865}
866
882static int gfxL_printText( lua_State *L )
883{
884 glFont *font;
885 const char *str;
886 int w, h, lh;
887 double x, y;
888 glColour *col;
889
890 /* Parse parameters. */
891 font = lua_toboolean(L,1) ? &gl_smallFont : &gl_defFont;
892 str = luaL_checkstring(L,2);
893 x = luaL_checknumber(L,3);
894 y = luaL_checknumber(L,4);
895 w = luaL_checkinteger(L,5);
896 h = luaL_checkinteger(L,6);
897 col = luaL_checkcolour(L,7);
898 lh = luaL_optinteger(L,8,0);
899
900 /* Render. */
901 gl_printTextRaw( font, w, h, x, y, lh, col, -1., str );
902
903 return 0;
904}
905
915static int gfxL_setBlendMode( lua_State *L )
916{
917 const char *mode, *alphamode;
918 GLenum func, srcRGB, srcA, dstRGB, dstA;
919
920 /* Parse parameters. */
921 mode = luaL_checkstring(L,1);
922 alphamode= luaL_optstring(L,2,"alphamultiply");
923
924 /* Translate to OpenGL enums. */
925 func = GL_FUNC_ADD;
926 srcRGB = GL_ONE;
927 srcA = GL_ONE;
928 dstRGB = GL_ZERO;
929 dstA = GL_ZERO;
930
931 if (!strcmp( alphamode, "alphamultiply" ))
932 srcRGB = srcA = GL_SRC_ALPHA;
933 else if (!strcmp( alphamode, "premultiplied" )) {
934 if (!strcmp( mode, "lighten" ) || !strcmp( mode, "darken" ) || !strcmp( mode, "multiply" ))
935 NLUA_INVALID_PARAMETER(L,2);
936 }
937 else
938 NLUA_INVALID_PARAMETER(L,2);
939
940 if (!strcmp( mode, "alpha" ))
941 dstRGB = dstA = GL_ONE_MINUS_SRC_ALPHA;
942 else if (!strcmp( mode, "multiply" ))
943 srcRGB = srcA = GL_DST_COLOR;
944 else if (!strcmp( mode, "subtract" ))
945 func = GL_FUNC_REVERSE_SUBTRACT;
946 else if (!strcmp( mode, "add" ) ) {
947 func = GL_FUNC_REVERSE_SUBTRACT;
948 srcA = GL_ZERO;
949 dstRGB = dstA = GL_ONE;
950 }
951 else if (!strcmp( mode, "lighten" ))
952 func = GL_MAX;
953 else if (!strcmp( mode, "darken" ))
954 func = GL_MIN;
955 else if (!strcmp( mode, "screen" ))
956 dstRGB = dstA = GL_ONE_MINUS_SRC_COLOR;
957 else if (strcmp( mode, "replace" ))
958 NLUA_INVALID_PARAMETER(L,1);
959
960 glBlendEquation(func);
961 glBlendFuncSeparate(srcRGB, dstRGB, srcA, dstA);
962 gl_checkErr();
963
964 render_needsReset();
965 return 0;
966}
967
979static int gfxL_setScissor( lua_State *L )
980{
981 if (lua_gettop(L)>0) {
982 GLint x = luaL_optinteger(L,1,0);
983 GLint y = luaL_optinteger(L,2,0);
984 GLsizei w = luaL_optinteger(L,3,0);
985 GLsizei h = luaL_optinteger(L,4,0);
986 gl_clipRect( x, y, w, h );
987 render_needsReset();
988 }
989 else
991
992 return 0;
993}
994
1002static int gfxL_screenshot( lua_State * L )
1003{
1004 LuaCanvas_t *lc;
1005 int mustfree;
1006
1007 /* Set up canvas or try to reuse. */
1008 if (lua_iscanvas(L,1)) {
1009 lc = luaL_checkcanvas(L,1);
1010 mustfree = 0;
1011 }
1012 else {
1013 lc = calloc( 1, sizeof(LuaCanvas_t) );
1014 canvas_new( lc, gl_screen.rw, gl_screen.rh );
1015 mustfree = 1;
1016 }
1017
1018 /* Copy over. */
1019 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
1020 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, lc->fbo);
1021 /* We flip it over because that seems to be what love2d API wants. */
1022 glBlitFramebuffer(0, 0, gl_screen.rw, gl_screen.rh, 0, lc->tex->h, lc->tex->w, 0, GL_COLOR_BUFFER_BIT, GL_NEAREST);
1023 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
1024 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
1025
1026 /* Return new or old canvas. */
1027 lua_pushcanvas(L, *lc);
1028 if (mustfree)
1029 free( lc );
1030 return 1;
1031}
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:1027
void gl_printRestoreClear(void)
Clears the restoration.
Definition font.c:378
int gl_printLineIteratorNext(glPrintLineIterator *iter)
Updates iter with the next line's information.
Definition font.c:526
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:617
int gl_printWidthRaw(const glFont *ft_font, const char *text)
Gets the width that it would take to print some text.
Definition font.c:961
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:788
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:870
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:648
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:506
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:721
void gl_printRestoreLast(void)
Restores last colour.
Definition font.c:386
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.
int nlua_loadCol(nlua_env env)
Loads the colour library.
Definition nlua_colour.c:54
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:91
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:242
static int gfxL_renderRectH(lua_State *L)
Renders a rectangle given a transformation matrix.
Definition nlua_gfx.c:472
static int gfxL_screencoords(lua_State *L)
Gets the screen coordinates from game coordinates.
Definition nlua_gfx.c:159
static int gfxL_dim(lua_State *L)
Lua bindings to interact with rendering and the Naev graphical environment.
Definition nlua_gfx.c:137
static int gfxL_renderRect(lua_State *L)
Renders a rectangle.
Definition nlua_gfx.c:441
static const luaL_Reg gfxL_methods[]
Definition nlua_gfx.c:59
static int gfxL_printText(lua_State *L)
Prints a block of text on the screen.
Definition nlua_gfx.c:882
static int gfxL_printf(lua_State *L)
Prints text on the screen using a font.
Definition nlua_gfx.c:767
static int gfxL_screenshot(lua_State *L)
Takes the current rendered game screen and returns it as a canvas.
Definition nlua_gfx.c:1002
static int gfxL_renderTexH(lua_State *L)
Renders a texture using a transformation matrix.
Definition nlua_gfx.c:365
static int gfxL_renderTex(lua_State *L)
Renders a texture.
Definition nlua_gfx.c:191
static int gfxL_printfWrap(lua_State *L)
Gets the wrap for text.
Definition nlua_gfx.c:693
static int gfxL_printfDim(lua_State *L)
Gets the size of the text to print.
Definition nlua_gfx.c:664
static int gfxL_clearDepth(lua_State *L)
Clears the depth buffer.
Definition nlua_gfx.c:605
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:915
static int gfxL_renderLinesH(lua_State *L)
Renders a polyline or set of line segments.
Definition nlua_gfx.c:548
static int gfxL_renderCircle(lua_State *L)
Renders a circle.
Definition nlua_gfx.c:495
static int gfxL_printRestoreLast(lua_State *L)
Restores the last saved internal colour state.
Definition nlua_gfx.c:746
static int gfxL_printH(lua_State *L)
Prints text on the screen using a font with a transformation matirx.
Definition nlua_gfx.c:804
int nlua_loadGFX(nlua_env env)
Loads the graphics library.
Definition nlua_gfx.c:98
static int gfxL_renderCircleH(lua_State *L)
Renders a circle given a transformation matrix.
Definition nlua_gfx.c:522
static int gfxL_fontSize(lua_State *L)
Gets the size of the font.
Definition nlua_gfx.c:619
static int gfxL_renderTexRaw(lua_State *L)
Renders a texture using the core render function.
Definition nlua_gfx.c:309
static int gfxL_printDim(lua_State *L)
Gets the size of the text to print.
Definition nlua_gfx.c:637
static int gfxL_printRestoreClear(lua_State *L)
Clears the saved internal colour state.
Definition nlua_gfx.c:735
static int gfxL_print(lua_State *L)
Prints text on the screen.
Definition nlua_gfx.c:840
static int gfxL_setScissor(lua_State *L)
Sets the scissor clipping.
Definition nlua_gfx.c:979
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:130
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition nlua_vec2.c:145
glInfo gl_screen
Definition opengl.c:51
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.
Definition opengl_vbo.c:162
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:226
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:98
static const double c[]
Definition rng.c:264
Wrapper to canvass.
Definition nlua_canvas.h:19
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:52
int rh
Definition opengl.h:51
int rw
Definition opengl.h:50
int nw
Definition opengl.h:47
int nh
Definition opengl.h:48
The state of a line iteration. This matches the process of rendering text into an on-screen box: An e...
Definition font.h:40
Abstraction for rendering sprite sheets.
Definition opengl_tex.h:36
double sw
Definition opengl_tex.h:46
double sh
Definition opengl_tex.h:47
double w
Definition opengl_tex.h:40
double sx
Definition opengl_tex.h:44
double srh
Definition opengl_tex.h:49
char * name
Definition opengl_tex.h:37
GLuint texture
Definition opengl_tex.h:52
double sy
Definition opengl_tex.h:45
double h
Definition opengl_tex.h:41
double srw
Definition opengl_tex.h:48
Definition mat4.h:10
Represents a 2d vector.
Definition vec2.h:32
double y
Definition vec2.h:34
double x
Definition vec2.h:33