Skip to content

Commit 829edaa

Browse files
authored
Fix Lua API polluting the game context (#6739)
* split rendering values into lua and fso contexts * address feedback
1 parent 3cf3aa9 commit 829edaa

File tree

8 files changed

+219
-68
lines changed

8 files changed

+219
-68
lines changed

code/graphics/2d.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ const char* Resolution_prefixes[GR_NUM_RESOLUTIONS] = {"", "2_"};
9090

9191
screen gr_screen;
9292

93+
lua_screen gr_lua_screen;
94+
9395
color_gun Gr_red, Gr_green, Gr_blue, Gr_alpha;
9496
color_gun Gr_t_red, Gr_t_green, Gr_t_blue, Gr_t_alpha;
9597
color_gun Gr_ta_red, Gr_ta_green, Gr_ta_blue, Gr_ta_alpha;
@@ -2115,7 +2117,11 @@ void gr_set_color( int r, int g, int b )
21152117

21162118
void gr_set_color_fast(const color *dst)
21172119
{
2118-
gr_screen.current_color = *dst;
2120+
if (gr_lua_context_active()) {
2121+
gr_lua_screen.current_color = *dst;
2122+
} else {
2123+
gr_screen.current_color = *dst;
2124+
}
21192125
}
21202126

21212127
//Compares the RGBA values of two colors. Returns true if the colors are identical
@@ -3287,3 +3293,11 @@ bool gr_is_viewport_window()
32873293

32883294
return false;
32893295
}
3296+
3297+
bool gr_lua_context_active() {
3298+
if (gr_lua_screen.force_fso_context) {
3299+
return false;
3300+
}
3301+
3302+
return gr_lua_screen.active;
3303+
}

code/graphics/2d.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,42 @@ typedef struct screen {
935935
std::function<bool()> gf_openxr_flip;
936936
} screen;
937937

938+
/**
939+
* @brief Scripting context render values
940+
*/
941+
typedef struct lua_screen {
942+
bool active = false;
943+
bool force_fso_context = false;
944+
color current_color = {
945+
0, // is_alphacolor
946+
0, // alphacolor
947+
0, // magic
948+
255, // red
949+
255, // green
950+
255, // blue
951+
255, // alpha
952+
0, // ac_type
953+
0 // raw8
954+
};
955+
float line_width = 1.0f;
956+
int current_font_index = 0;
957+
} lua_screen;
958+
959+
extern lua_screen gr_lua_screen;
960+
961+
bool gr_lua_context_active();
962+
963+
// Macros to easily choose been which context to use for color and line width
964+
// Note that font is handled slightly differently for the normal game context by using it's own global in the FontManager namespace
965+
// So FontManager::getCurrentFontIndex() is effectively its own macro
966+
967+
// Gets the current color between the game context and the lua context if active
968+
#define GR_CURRENT_COLOR (gr_lua_context_active() ? gr_lua_screen.current_color : gr_screen.current_color)
969+
970+
// Gets the current line width between the game context and the lua contet if active
971+
#define GR_CURRENT_LINE_WIDTH (gr_lua_context_active() ? gr_lua_screen.line_width : gr_screen.line_width)
972+
973+
938974
// handy macro
939975
#define GR_MAYBE_CLEAR_RES(bmap) do { int bmw = -1; int bmh = -1; if(bmap != -1){ bm_get_info( bmap, &bmw, &bmh, NULL, NULL, NULL); if((bmw != gr_screen.max_w) || (bmh != gr_screen.max_h)){gr_clear();} } else {gr_clear();} } while(false);
940976

code/graphics/line_draw_list.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ namespace graphics {
1111
line_draw_list::line_draw_list() {
1212
}
1313
void line_draw_list::add_line(int x1, int y1, int x2, int y2, int resize_mode) {
14-
add_vertex(x1, y1, resize_mode, &gr_screen.current_color);
15-
add_vertex(x2, y2, resize_mode, &gr_screen.current_color);
14+
add_vertex(x1, y1, resize_mode, &GR_CURRENT_COLOR);
15+
add_vertex(x2, y2, resize_mode, &GR_CURRENT_COLOR);
1616
}
1717
void line_draw_list::add_gradient(int x1, int y1, int x2, int y2, int resize_mode) {
18-
add_vertex(x1, y1, resize_mode, &gr_screen.current_color);
18+
add_vertex(x1, y1, resize_mode, &GR_CURRENT_COLOR);
1919

20-
color endColor = gr_screen.current_color;
20+
color endColor = GR_CURRENT_COLOR;
2121
endColor.alpha = 0;
2222
add_vertex(x2, y2, resize_mode, &endColor);
2323
}

code/graphics/opengl/gropengl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,15 @@ void gr_opengl_zbias(int bias)
787787

788788
void gr_opengl_set_line_width(float width)
789789
{
790-
if (width <= 1.0f) {
791-
GL_state.SetLineWidth(width);
790+
if (gr_lua_context_active()) {
791+
gr_lua_screen.line_width = width;
792+
} else {
793+
if (width <= 1.0f) {
794+
GL_state.SetLineWidth(width);
795+
}
796+
797+
gr_screen.line_width = width;
792798
}
793-
gr_screen.line_width = width;
794799
}
795800

796801
int opengl_check_for_errors(const char *err_at)

code/graphics/render.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,10 @@ static void gr_string_old(float sx,
552552
render_mat.set_blend_mode(ALPHA_BLEND_ALPHA_BLEND_ALPHA);
553553
render_mat.set_depth_mode(ZBUFFER_TYPE_NONE);
554554
render_mat.set_texture_map(TM_BASE_TYPE, fontData->bitmap_id);
555-
render_mat.set_color(gr_screen.current_color.red,
556-
gr_screen.current_color.green,
557-
gr_screen.current_color.blue,
558-
gr_screen.current_color.alpha);
555+
render_mat.set_color(GR_CURRENT_COLOR.red,
556+
GR_CURRENT_COLOR.green,
557+
GR_CURRENT_COLOR.blue,
558+
GR_CURRENT_COLOR.alpha);
559559
render_mat.set_cull_mode(false);
560560
render_mat.set_texture_type(material::TEX_TYPE_AABITMAP);
561561

@@ -749,7 +749,7 @@ graphics::paths::PathRenderer* beginDrawing(int resize_mode) {
749749

750750
path->beginPath();
751751

752-
path->setStrokeWidth(gr_screen.line_width);
752+
path->setStrokeWidth(GR_CURRENT_LINE_WIDTH);
753753

754754
return path;
755755
}
@@ -829,7 +829,7 @@ void gr_string(float sx, float sy, const char* s, int resize_mode, float scaleMu
829829

830830
bool twoPassRequired = false;
831831

832-
path->setFillColor(&gr_screen.current_color);
832+
path->setFillColor(&GR_CURRENT_COLOR);
833833

834834
// Do a two pass algorithm, first render text using NanoVG, then render old characters
835835
for (int pass = 0; pass < 2; ++pass) {
@@ -940,13 +940,13 @@ static void gr_line(float x1, float y1, float x2, float y2, int resize_mode) {
940940
if ((x1 == x2) && (y1 == y2)) {
941941
path->circle(x1, y1, 1.5);
942942

943-
path->setFillColor(&gr_screen.current_color);
943+
path->setFillColor(&GR_CURRENT_COLOR);
944944
path->fill();
945945
} else {
946946
path->moveTo(x1, y1);
947947
path->lineTo(x2, y2);
948948

949-
path->setStrokeColor(&gr_screen.current_color);
949+
path->setStrokeColor(&GR_CURRENT_COLOR);
950950
path->stroke();
951951
}
952952

@@ -980,18 +980,18 @@ void gr_gradient(int x1, int y1, int x2, int y2, int resize_mode) {
980980
return;
981981
}
982982

983-
if (!gr_screen.current_color.is_alphacolor) {
983+
if (!GR_CURRENT_COLOR.is_alphacolor) {
984984
gr_line(x1, y1, x2, y2, resize_mode);
985985
return;
986986
}
987987

988988
auto path = beginDrawing(resize_mode);
989989

990-
color endColor = gr_screen.current_color;
990+
color endColor = GR_CURRENT_COLOR;
991991
endColor.alpha = 0;
992992

993993
auto gradientPaint =
994-
path->createLinearGradient(i2fl(x1), i2fl(y1), i2fl(x2), i2fl(y2), &gr_screen.current_color, &endColor);
994+
path->createLinearGradient(i2fl(x1), i2fl(y1), i2fl(x2), i2fl(y2), &GR_CURRENT_COLOR, &endColor);
995995

996996
path->moveTo(i2fl(x1), i2fl(y1));
997997
path->lineTo(i2fl(x2), i2fl(y2));
@@ -1017,7 +1017,7 @@ void gr_circle(int xc, int yc, int d, int resize_mode) {
10171017
auto path = beginDrawing(resize_mode);
10181018

10191019
path->circle(i2fl(xc), i2fl(yc), d / 2.0f);
1020-
path->setFillColor(&gr_screen.current_color);
1020+
path->setFillColor(&GR_CURRENT_COLOR);
10211021
path->fill();
10221022

10231023
endDrawing(path);
@@ -1030,7 +1030,7 @@ void gr_unfilled_circle(int xc, int yc, int d, int resize_mode) {
10301030
auto path = beginDrawing(resize_mode);
10311031

10321032
path->circle(i2fl(xc), i2fl(yc), d / 2.0f);
1033-
path->setStrokeColor(&gr_screen.current_color);
1033+
path->setStrokeColor(&GR_CURRENT_COLOR);
10341034
path->stroke();
10351035

10361036
endDrawing(path);
@@ -1055,11 +1055,11 @@ void gr_arc(int xc, int yc, float r, float angle_start, float angle_end, bool fi
10551055
path->arc(i2fl(xc), i2fl(yc), r, fl_radians(angle_start), fl_radians(angle_end), DIR_CW);
10561056
path->lineTo(i2fl(xc), i2fl(yc));
10571057

1058-
path->setFillColor(&gr_screen.current_color);
1058+
path->setFillColor(&GR_CURRENT_COLOR);
10591059
path->fill();
10601060
} else {
10611061
path->arc(i2fl(xc), i2fl(yc), r, fl_radians(angle_start), fl_radians(angle_end), DIR_CW);
1062-
path->setStrokeColor(&gr_screen.current_color);
1062+
path->setStrokeColor(&GR_CURRENT_COLOR);
10631063
path->stroke();
10641064
}
10651065

@@ -1110,7 +1110,7 @@ void gr_curve(int xc, int yc, int r, int direction, int resize_mode) {
11101110
}
11111111

11121112
path->arc(centerX, centerY, i2fl(r), beginAngle, endAngle, DIR_CW);
1113-
path->setStrokeColor(&gr_screen.current_color);
1113+
path->setStrokeColor(&GR_CURRENT_COLOR);
11141114
path->stroke();
11151115

11161116
endDrawing(path);
@@ -1131,7 +1131,7 @@ void gr_rect(int x, int y, int w, int h, int resize_mode, float angle) {
11311131
path->translate(-offsetX, -offsetY);
11321132
}
11331133
path->rectangle(i2fl(x), i2fl(y), i2fl(w), i2fl(h));
1134-
path->setFillColor(&gr_screen.current_color);
1134+
path->setFillColor(&GR_CURRENT_COLOR);
11351135
path->fill();
11361136

11371137
endDrawing(path);

code/graphics/software/FontManager.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ namespace font
5858

5959
int FontManager::getCurrentFontIndex()
6060
{
61-
if (!isFontNumberValid(currentFontIndex))
61+
int id = gr_lua_context_active() ? gr_lua_screen.current_font_index : currentFontIndex;
62+
if (!isFontNumberValid(id))
6263
return -1;
6364

64-
return currentFontIndex;
65+
return id;
6566
}
6667

6768
int FontManager::getFontIndex(const SCP_string& name)
@@ -118,7 +119,11 @@ namespace font
118119
void FontManager::setCurrentFontIndex(int id)
119120
{
120121
Assertion(isFontNumberValid(id), "New font index must be valid!");
121-
currentFontIndex = id;
122+
if (gr_lua_context_active()) {
123+
gr_lua_screen.current_font_index = id;
124+
} else {
125+
currentFontIndex = id;
126+
}
122127
}
123128

124129
font* FontManager::loadFontOld(const SCP_string& typeface)

code/hud/hudtarget.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,10 +2850,10 @@ void hud_tri(float x1,float y1,float x2,float y2,float x3,float y3, bool config)
28502850
verts[0].texture_position.v = 0.0f;
28512851
verts[0].flags = PF_PROJECTED;
28522852
verts[0].codes = 0;
2853-
verts[0].r = (ubyte)gr_screen.current_color.red;
2854-
verts[0].g = (ubyte)gr_screen.current_color.green;
2855-
verts[0].b = (ubyte)gr_screen.current_color.blue;
2856-
verts[0].a = (ubyte)gr_screen.current_color.alpha;
2853+
verts[0].r = (ubyte)GR_CURRENT_COLOR.red;
2854+
verts[0].g = (ubyte)GR_CURRENT_COLOR.green;
2855+
verts[0].b = (ubyte)GR_CURRENT_COLOR.blue;
2856+
verts[0].a = (ubyte)GR_CURRENT_COLOR.alpha;
28572857

28582858
verts[1].screen.xyw.x = x2;
28592859
verts[1].screen.xyw.y = y2;
@@ -2862,10 +2862,10 @@ void hud_tri(float x1,float y1,float x2,float y2,float x3,float y3, bool config)
28622862
verts[1].texture_position.v = 0.0f;
28632863
verts[1].flags = PF_PROJECTED;
28642864
verts[1].codes = 0;
2865-
verts[1].r = (ubyte)gr_screen.current_color.red;
2866-
verts[1].g = (ubyte)gr_screen.current_color.green;
2867-
verts[1].b = (ubyte)gr_screen.current_color.blue;
2868-
verts[1].a = (ubyte)gr_screen.current_color.alpha;
2865+
verts[1].r = (ubyte)GR_CURRENT_COLOR.red;
2866+
verts[1].g = (ubyte)GR_CURRENT_COLOR.green;
2867+
verts[1].b = (ubyte)GR_CURRENT_COLOR.blue;
2868+
verts[1].a = (ubyte)GR_CURRENT_COLOR.alpha;
28692869

28702870
verts[2].screen.xyw.x = x3;
28712871
verts[2].screen.xyw.y = y3;
@@ -2874,10 +2874,10 @@ void hud_tri(float x1,float y1,float x2,float y2,float x3,float y3, bool config)
28742874
verts[2].texture_position.v = 0.0f;
28752875
verts[2].flags = PF_PROJECTED;
28762876
verts[2].codes = 0;
2877-
verts[2].r = (ubyte)gr_screen.current_color.red;
2878-
verts[2].g = (ubyte)gr_screen.current_color.green;
2879-
verts[2].b = (ubyte)gr_screen.current_color.blue;
2880-
verts[2].a = (ubyte)gr_screen.current_color.alpha;
2877+
verts[2].r = (ubyte)GR_CURRENT_COLOR.red;
2878+
verts[2].g = (ubyte)GR_CURRENT_COLOR.green;
2879+
verts[2].b = (ubyte)GR_CURRENT_COLOR.blue;
2880+
verts[2].a = (ubyte)GR_CURRENT_COLOR.alpha;
28812881

28822882
for (auto& vert : verts) {
28832883
gr_resize_screen_posf(&vert.screen.xyw.x, &vert.screen.xyw.y, nullptr, nullptr, config ? HC_resize_mode : GR_RESIZE_FULL);

0 commit comments

Comments
 (0)