Skip to content

Commit f25d98c

Browse files
committed
Add AA bitmap support for standard gr.drawImage
Since that function already has full support for UV scaling it makes sense to unify the behavior in one function instead of having a separate monochrome function that does weird things. Also deprecates that function since all its use cases are now covered by drawImage.
1 parent 4b7013f commit f25d98c

File tree

5 files changed

+160
-136
lines changed

5 files changed

+160
-136
lines changed

code/graphics/2d.cpp

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,122 +1919,6 @@ void gr_bitmap_uv(int _x, int _y, int _w, int _h, float _u0, float _v0, float _u
19191919
g3_render_primitives_textured(&material_params, verts, 4, PRIM_TYPE_TRIFAN, true);
19201920
}
19211921

1922-
// NEW new bitmap functions -Bobboau
1923-
// void gr_bitmap_list(bitmap_2d_list* list, int n_bm, int resize_mode)
1924-
// {
1925-
// for (int i = 0; i < n_bm; i++) {
1926-
// bitmap_2d_list *l = &list[i];
1927-
//
1928-
// bm_get_info(gr_screen.current_bitmap, &l->w, &l->h, NULL, NULL, NULL);
1929-
//
1930-
// if ( resize_mode != GR_RESIZE_NONE && (gr_screen.custom_size || (gr_screen.rendering_to_texture != -1)) ) {
1931-
// gr_resize_screen_pos(&l->x, &l->y, &l->w, &l->h, resize_mode);
1932-
// }
1933-
// }
1934-
//
1935-
// g3_draw_2d_poly_bitmap_list(list, n_bm, TMAP_FLAG_INTERFACE);
1936-
// }
1937-
1938-
// _->NEW<-_ NEW new bitmap functions -Bobboau
1939-
//takes a list of rectangles that have assosiated rectangles in a texture
1940-
void gr_bitmap_list(bitmap_rect_list* list, int n_bm, int resize_mode)
1941-
{
1942-
GR_DEBUG_SCOPE("2D Bitmap list");
1943-
1944-
// adapted from g3_draw_2d_poly_bitmap_list
1945-
1946-
for ( int i = 0; i < n_bm; i++ ) {
1947-
bitmap_2d_list *l = &list[i].screen_rect;
1948-
1949-
// if no valid hight or width values were given get some from the bitmap
1950-
if ( (l->w <= 0) || (l->h <= 0) ) {
1951-
bm_get_info(gr_screen.current_bitmap, &l->w, &l->h, NULL, NULL, NULL);
1952-
}
1953-
1954-
if ( resize_mode != GR_RESIZE_NONE && (gr_screen.custom_size || (gr_screen.rendering_to_texture != -1)) ) {
1955-
gr_resize_screen_pos(&l->x, &l->y, &l->w, &l->h, resize_mode);
1956-
}
1957-
}
1958-
1959-
vertex* vert_list = new vertex[6 * n_bm];
1960-
float sw = 0.1f;
1961-
1962-
for ( int i = 0; i < n_bm; i++ ) {
1963-
// stuff coords
1964-
1965-
bitmap_2d_list* b = &list[i].screen_rect;
1966-
texture_rect_list* t = &list[i].texture_rect;
1967-
//tri one
1968-
vertex *V = &vert_list[i * 6];
1969-
V->screen.xyw.x = (float)b->x;
1970-
V->screen.xyw.y = (float)b->y;
1971-
V->screen.xyw.w = sw;
1972-
V->texture_position.u = (float)t->u0;
1973-
V->texture_position.v = (float)t->v0;
1974-
V->flags = PF_PROJECTED;
1975-
V->codes = 0;
1976-
1977-
V++;
1978-
V->screen.xyw.x = (float)(b->x + b->w);
1979-
V->screen.xyw.y = (float)b->y;
1980-
V->screen.xyw.w = sw;
1981-
V->texture_position.u = (float)t->u1;
1982-
V->texture_position.v = (float)t->v0;
1983-
V->flags = PF_PROJECTED;
1984-
V->codes = 0;
1985-
1986-
V++;
1987-
V->screen.xyw.x = (float)(b->x + b->w);
1988-
V->screen.xyw.y = (float)(b->y + b->h);
1989-
V->screen.xyw.w = sw;
1990-
V->texture_position.u = (float)t->u1;
1991-
V->texture_position.v = (float)t->v1;
1992-
V->flags = PF_PROJECTED;
1993-
V->codes = 0;
1994-
1995-
//tri two
1996-
V++;
1997-
V->screen.xyw.x = (float)b->x;
1998-
V->screen.xyw.y = (float)b->y;
1999-
V->screen.xyw.w = sw;
2000-
V->texture_position.u = (float)t->u0;
2001-
V->texture_position.v = (float)t->v0;
2002-
V->flags = PF_PROJECTED;
2003-
V->codes = 0;
2004-
2005-
V++;
2006-
V->screen.xyw.x = (float)(b->x + b->w);
2007-
V->screen.xyw.y = (float)(b->y + b->h);
2008-
V->screen.xyw.w = sw;
2009-
V->texture_position.u = (float)t->u1;
2010-
V->texture_position.v = (float)t->v1;
2011-
V->flags = PF_PROJECTED;
2012-
V->codes = 0;
2013-
2014-
V++;
2015-
V->screen.xyw.x = (float)b->x;
2016-
V->screen.xyw.y = (float)(b->y + b->h);
2017-
V->screen.xyw.w = sw;
2018-
V->texture_position.u = (float)t->u0;
2019-
V->texture_position.v = (float)t->v1;
2020-
V->flags = PF_PROJECTED;
2021-
V->codes = 0;
2022-
}
2023-
2024-
material mat_params;
2025-
material_set_interface(
2026-
&mat_params,
2027-
gr_screen.current_bitmap,
2028-
gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER ? true : false,
2029-
gr_screen.current_alpha
2030-
);
2031-
g3_render_primitives_textured(&mat_params, vert_list, 6 * n_bm, PRIM_TYPE_TRIS, true);
2032-
2033-
delete[] vert_list;
2034-
}
2035-
2036-
2037-
20381922
/**
20391923
* Given endpoints, and thickness, calculate coords of the endpoint
20401924
* Adapted from gr_pline_helper()

code/graphics/2d.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,6 @@ void gr_set_shader(shader *shade);
12681268
// new bitmap functions
12691269
void gr_bitmap(int x, int y, int resize_mode = GR_RESIZE_FULL);
12701270
void gr_bitmap_uv(int _x, int _y, int _w, int _h, float _u0, float _v0, float _u1, float _v1, int resize_mode = GR_RESIZE_FULL);
1271-
void gr_bitmap_list(bitmap_rect_list* list, int n_bm, int resize_mode);
12721271

12731272
// special function for drawing polylines. this function is specifically intended for
12741273
// polylines where each section is no more than 90 degrees away from a previous section.

code/graphics/render.cpp

Lines changed: 128 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11

22
#include "graphics/render.h"
3+
34
#include "graphics/material.h"
45
#include "graphics/matrix.h"
5-
#include "graphics/software/font_internal.h"
6+
#include "graphics/paths/PathRenderer.h"
67
#include "graphics/software/FSFont.h"
78
#include "graphics/software/NVGFont.h"
89
#include "graphics/software/VFNTFont.h"
9-
#include "graphics/paths/PathRenderer.h"
10-
11-
#include "mod_table/mod_table.h"
10+
#include "graphics/software/font_internal.h"
1211
#include "localization/localize.h"
13-
#include "matrix.h"
12+
#include "mod_table/mod_table.h"
13+
#include "render/3d.h"
1414

15-
static void gr_flash_internal(int r, int g, int b, int a, bool alpha_flash) {
15+
static void gr_flash_internal(int r, int g, int b, int a, bool alpha_flash)
16+
{
1617
CLAMP(r, 0, 255);
1718
CLAMP(g, 0, 255);
1819
CLAMP(b, 0, 255);
@@ -1148,14 +1149,130 @@ void gr_render_primitives_immediate(material* material_info,
11481149
}
11491150

11501151
void gr_render_primitives_2d_immediate(material* material_info,
1151-
primitive_type prim_type,
1152-
vertex_layout* layout,
1153-
int n_verts,
1154-
void* data,
1155-
size_t size) {
1152+
primitive_type prim_type,
1153+
vertex_layout* layout,
1154+
int n_verts,
1155+
void* data,
1156+
size_t size)
1157+
{
11561158
gr_set_2d_matrix();
11571159

11581160
gr_render_primitives_immediate(material_info, prim_type, layout, n_verts, data, size);
11591161

11601162
gr_end_2d_matrix();
11611163
}
1164+
1165+
// _->NEW<-_ NEW new bitmap functions -Bobboau
1166+
// takes a list of rectangles that have assosiated rectangles in a texture
1167+
static void draw_bitmap_list(bitmap_rect_list* list, int n_bm, int resize_mode, material* render_mat)
1168+
{
1169+
GR_DEBUG_SCOPE("2D Bitmap list");
1170+
1171+
// adapted from g3_draw_2d_poly_bitmap_list
1172+
1173+
for (int i = 0; i < n_bm; i++) {
1174+
bitmap_2d_list* l = &list[i].screen_rect;
1175+
1176+
// if no valid hight or width values were given get some from the bitmap
1177+
if ((l->w <= 0) || (l->h <= 0)) {
1178+
bm_get_info(gr_screen.current_bitmap, &l->w, &l->h, nullptr, nullptr, nullptr);
1179+
}
1180+
1181+
if (resize_mode != GR_RESIZE_NONE && (gr_screen.custom_size || (gr_screen.rendering_to_texture != -1))) {
1182+
gr_resize_screen_pos(&l->x, &l->y, &l->w, &l->h, resize_mode);
1183+
}
1184+
}
1185+
1186+
auto vert_list = new vertex[6 * n_bm];
1187+
float sw = 0.1f;
1188+
1189+
for (int i = 0; i < n_bm; i++) {
1190+
// stuff coords
1191+
1192+
bitmap_2d_list* b = &list[i].screen_rect;
1193+
texture_rect_list* t = &list[i].texture_rect;
1194+
// tri one
1195+
vertex* V = &vert_list[i * 6];
1196+
V->screen.xyw.x = (float)b->x;
1197+
V->screen.xyw.y = (float)b->y;
1198+
V->screen.xyw.w = sw;
1199+
V->texture_position.u = (float)t->u0;
1200+
V->texture_position.v = (float)t->v0;
1201+
V->flags = PF_PROJECTED;
1202+
V->codes = 0;
1203+
1204+
V++;
1205+
V->screen.xyw.x = (float)(b->x + b->w);
1206+
V->screen.xyw.y = (float)b->y;
1207+
V->screen.xyw.w = sw;
1208+
V->texture_position.u = (float)t->u1;
1209+
V->texture_position.v = (float)t->v0;
1210+
V->flags = PF_PROJECTED;
1211+
V->codes = 0;
1212+
1213+
V++;
1214+
V->screen.xyw.x = (float)(b->x + b->w);
1215+
V->screen.xyw.y = (float)(b->y + b->h);
1216+
V->screen.xyw.w = sw;
1217+
V->texture_position.u = (float)t->u1;
1218+
V->texture_position.v = (float)t->v1;
1219+
V->flags = PF_PROJECTED;
1220+
V->codes = 0;
1221+
1222+
// tri two
1223+
V++;
1224+
V->screen.xyw.x = (float)b->x;
1225+
V->screen.xyw.y = (float)b->y;
1226+
V->screen.xyw.w = sw;
1227+
V->texture_position.u = (float)t->u0;
1228+
V->texture_position.v = (float)t->v0;
1229+
V->flags = PF_PROJECTED;
1230+
V->codes = 0;
1231+
1232+
V++;
1233+
V->screen.xyw.x = (float)(b->x + b->w);
1234+
V->screen.xyw.y = (float)(b->y + b->h);
1235+
V->screen.xyw.w = sw;
1236+
V->texture_position.u = (float)t->u1;
1237+
V->texture_position.v = (float)t->v1;
1238+
V->flags = PF_PROJECTED;
1239+
V->codes = 0;
1240+
1241+
V++;
1242+
V->screen.xyw.x = (float)b->x;
1243+
V->screen.xyw.y = (float)(b->y + b->h);
1244+
V->screen.xyw.w = sw;
1245+
V->texture_position.u = (float)t->u0;
1246+
V->texture_position.v = (float)t->v1;
1247+
V->flags = PF_PROJECTED;
1248+
V->codes = 0;
1249+
}
1250+
1251+
g3_render_primitives_textured(render_mat, vert_list, 6 * n_bm, PRIM_TYPE_TRIS, true);
1252+
1253+
delete[] vert_list;
1254+
}
1255+
1256+
void gr_bitmap_list(bitmap_rect_list* list, int n_bm, int resize_mode)
1257+
{
1258+
material mat_params;
1259+
material_set_interface(&mat_params,
1260+
gr_screen.current_bitmap,
1261+
gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER,
1262+
gr_screen.current_alpha);
1263+
1264+
draw_bitmap_list(list, n_bm, resize_mode, &mat_params);
1265+
}
1266+
1267+
void gr_aabitmap_list(bitmap_rect_list* list, int n_bm, int resize_mode)
1268+
{
1269+
material render_mat;
1270+
render_mat.set_blend_mode(ALPHA_BLEND_ALPHA_BLEND_ALPHA);
1271+
render_mat.set_depth_mode(ZBUFFER_TYPE_NONE);
1272+
render_mat.set_texture_map(TM_BASE_TYPE, gr_screen.current_bitmap);
1273+
render_mat.set_color(gr_screen.current_color);
1274+
render_mat.set_cull_mode(false);
1275+
render_mat.set_texture_type(material::TEX_TYPE_AABITMAP);
1276+
1277+
draw_bitmap_list(list, n_bm, resize_mode, &render_mat);
1278+
}

code/graphics/render.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,7 @@ void gr_render_primitives_immediate(material* material_info, primitive_type prim
226226
* @param size The size of the data
227227
*/
228228
void gr_render_primitives_2d_immediate(material* material_info, primitive_type prim_type, vertex_layout* layout, int n_verts, void* data, size_t size);
229+
230+
void gr_bitmap_list(bitmap_rect_list* list, int n_bm, int resize_mode);
231+
232+
void gr_aabitmap_list(bitmap_rect_list* list, int n_bm, int resize_mode);

code/scripting/api/libs/graphics.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,10 +1304,11 @@ ADE_FUNC(loadTexture, l_Graphics, "string Filename, [boolean LoadIfAnimation, bo
13041304
ADE_FUNC(drawImage,
13051305
l_Graphics,
13061306
"string|texture fileNameOrTexture, [number X1=0, number Y1=0, number X2, number Y2, number UVX1 = 0.0, number UVY1 "
1307-
"= 0.0, number UVX2=1.0, number UVY2=1.0, number alpha=1.0]",
1307+
"= 0.0, number UVX2=1.0, number UVY2=1.0, number alpha=1.0, boolean aaImage = false]",
13081308
"Draws an image file or texture. Any image extension passed will be ignored."
13091309
"The UV variables specify the UV value for each corner of the image. "
1310-
"In UV coordinates, (0,0) is the top left of the image; (1,1) is the lower right.",
1310+
"In UV coordinates, (0,0) is the top left of the image; (1,1) is the lower right. If aaImage is true, image needs "
1311+
"to be monochrome and will be drawn tinted with the currently active color.",
13111312
"boolean",
13121313
"Whether image was drawn")
13131314
{
@@ -1324,11 +1325,12 @@ ADE_FUNC(drawImage,
13241325
float uv_x2=1.0f;
13251326
float uv_y2=1.0f;
13261327
float alpha=1.0f;
1328+
bool aabitmap = false;
13271329

13281330
if(lua_isstring(L, 1))
13291331
{
13301332
const char* s = nullptr;
1331-
if(!ade_get_args(L, "s|iiiifffff", &s,&x1,&y1,&x2,&y2,&uv_x1,&uv_y1,&uv_x2,&uv_y2,&alpha))
1333+
if (!ade_get_args(L, "s|iiiifffffb", &s, &x1, &y1, &x2, &y2, &uv_x1, &uv_y1, &uv_x2, &uv_y2, &alpha, &aabitmap))
13321334
return ade_set_error(L, "b", false);
13331335

13341336
idx = Script_system.LoadBm(s);
@@ -1339,8 +1341,19 @@ ADE_FUNC(drawImage,
13391341
else
13401342
{
13411343
texture_h* th = nullptr;
1342-
if (!ade_get_args(L, "o|iiiifffff", l_Texture.GetPtr(&th), &x1, &y1, &x2, &y2, &uv_x1, &uv_y1, &uv_x2, &uv_y2,
1343-
&alpha))
1344+
if (!ade_get_args(L,
1345+
"o|iiiifffffb",
1346+
l_Texture.GetPtr(&th),
1347+
&x1,
1348+
&y1,
1349+
&x2,
1350+
&y2,
1351+
&uv_x1,
1352+
&uv_y1,
1353+
&uv_x2,
1354+
&uv_y2,
1355+
&alpha,
1356+
&aabitmap))
13441357
return ade_set_error(L, "b", false);
13451358

13461359
if (!th->isValid()) {
@@ -1365,17 +1378,24 @@ ADE_FUNC(drawImage,
13651378

13661379
gr_set_bitmap(idx, lua_Opacity_type, GR_BITBLT_MODE_NORMAL, alpha);
13671380
bitmap_rect_list brl = bitmap_rect_list(x1, y1, w, h, uv_x1, uv_y1, uv_x2, uv_y2);
1368-
gr_bitmap_list(&brl, 1, GR_RESIZE_NONE);
1381+
1382+
if (aabitmap) {
1383+
gr_aabitmap_list(&brl, 1, GR_RESIZE_NONE);
1384+
} else {
1385+
gr_bitmap_list(&brl, 1, GR_RESIZE_NONE);
1386+
}
13691387

13701388
return ADE_RETURN_TRUE;
13711389
}
13721390

1373-
ADE_FUNC(drawMonochromeImage,
1391+
ADE_FUNC_DEPRECATED(drawMonochromeImage,
13741392
l_Graphics,
13751393
"string|texture fileNameOrTexture, number X1, number Y1, [number X2, number Y2, number alpha=1.0]",
13761394
"Draws a monochrome image from a texture or file using the current color",
13771395
"boolean",
1378-
"Whether image was drawn")
1396+
"Whether image was drawn",
1397+
gameversion::version(20, 1),
1398+
"gr.drawImage now has support for drawing monochrome images with full UV scaling support")
13791399
{
13801400
if(!Gr_inited)
13811401
return ade_set_error(L, "b", false);

0 commit comments

Comments
 (0)