diff options
author | Vas Crabb <vas@vastheman.com> | 2019-11-22 17:53:31 +1100 |
---|---|---|
committer | Vas Crabb <vas@vastheman.com> | 2019-11-22 17:53:31 +1100 |
commit | 09746dfedde2d2731de309a5729cb939cb1c93ba (patch) | |
tree | 1c377fbbbf9a335cfbe2e003bc399c6f02e6e742 /src/frontend/mame/ui/viewgfx.cpp | |
parent | aefe95ecd99b5be78a2c3cb3e1dca035315d8386 (diff) |
UI updates:
* Re-wrote localisation loader: sanitise input, check for buffer overruns, fix endianness handling, keep data in a single allocated block, do a single hash lookup when fetching a string, print diagnostic output when things go wrong
* Sort UI language menu so it's not in whatever random order the filesystem yields
* Fixed most menu code to adjust L/R border for UI aspect ratio and pass container to render manager when getting UI aspect ratio
* Converted a couple more things to use smart pointers
Diffstat (limited to 'src/frontend/mame/ui/viewgfx.cpp')
-rw-r--r-- | src/frontend/mame/ui/viewgfx.cpp | 117 |
1 files changed, 57 insertions, 60 deletions
diff --git a/src/frontend/mame/ui/viewgfx.cpp b/src/frontend/mame/ui/viewgfx.cpp index 2b6102f6e99..5b4d187bc95 100644 --- a/src/frontend/mame/ui/viewgfx.cpp +++ b/src/frontend/mame/ui/viewgfx.cpp @@ -391,16 +391,16 @@ static void palette_handler(mame_ui_manager &mui, render_container &container, u int total = state.palette.which ? palette->indirect_entries() : palette->entries(); const rgb_t *raw_color = palette->palette()->entry_list_raw(); render_font *ui_font = mui.get_font(); - float chwidth, chheight; float titlewidth; float x0, y0; render_bounds cellboxbounds; render_bounds boxbounds; - int x, y, skip; + int skip; // add a half character padding for the box - chheight = mui.get_line_height(); - chwidth = ui_font->char_width(chheight, mui.machine().render().ui_aspect(), '0'); + const float aspect = mui.machine().render().ui_aspect(&container); + const float chheight = mui.get_line_height(); + const float chwidth = ui_font->char_width(chheight, aspect, '0'); boxbounds.x0 = 0.0f + 0.5f * chwidth; boxbounds.x1 = 1.0f - 0.5f * chwidth; boxbounds.y0 = 0.0f + 0.5f * chheight; @@ -420,8 +420,8 @@ static void palette_handler(mame_ui_manager &mui, render_container &container, u cellboxbounds.y0 += 3.0f * chheight; // compute the cell size - float cellwidth = (cellboxbounds.x1 - cellboxbounds.x0) / (float)state.palette.columns; - float cellheight = (cellboxbounds.y1 - cellboxbounds.y0) / (float)state.palette.columns; + const float cellwidth = (cellboxbounds.x1 - cellboxbounds.x0) / float(state.palette.columns); + const float cellheight = (cellboxbounds.y1 - cellboxbounds.y0) / float(state.palette.columns); // figure out the title std::ostringstream title_buf; @@ -433,7 +433,7 @@ static void palette_handler(mame_ui_manager &mui, render_container &container, u int32_t mouse_target_x, mouse_target_y; float mouse_x, mouse_y; bool mouse_button; - render_target *mouse_target = mui.machine().ui_input().find_mouse(&mouse_target_x, &mouse_target_y, &mouse_button); + render_target *const mouse_target = mui.machine().ui_input().find_mouse(&mouse_target_x, &mouse_target_y, &mouse_button); if (mouse_target != nullptr && mouse_target->map_point_container(mouse_target_x, mouse_target_y, container, mouse_x, mouse_y) && cellboxbounds.x0 <= mouse_x && cellboxbounds.x1 > mouse_x && cellboxbounds.y0 <= mouse_y && cellboxbounds.y1 > mouse_y) @@ -454,7 +454,7 @@ static void palette_handler(mame_ui_manager &mui, render_container &container, u // expand the outer box to fit the title const std::string title = title_buf.str(); - titlewidth = ui_font->string_width(chheight, mui.machine().render().ui_aspect(), title.c_str()); + titlewidth = ui_font->string_width(chheight, aspect, title.c_str()); x0 = 0.0f; if (boxbounds.x1 - boxbounds.x0 < titlewidth + chwidth) x0 = boxbounds.x0 - (0.5f - 0.5f * (titlewidth + chwidth)); @@ -467,17 +467,17 @@ static void palette_handler(mame_ui_manager &mui, render_container &container, u y0 = boxbounds.y0 + 0.5f * chheight; for (auto ch : title) { - container.add_char(x0, y0, chheight, mui.machine().render().ui_aspect(), rgb_t::white(), *ui_font, ch); - x0 += ui_font->char_width(chheight, mui.machine().render().ui_aspect(), ch); + container.add_char(x0, y0, chheight, aspect, rgb_t::white(), *ui_font, ch); + x0 += ui_font->char_width(chheight, aspect, ch); } // draw the top column headers - skip = (int)(chwidth / cellwidth); - for (x = 0; x < state.palette.columns; x += 1 + skip) + skip = int(chwidth / cellwidth); + for (int x = 0; x < state.palette.columns; x += 1 + skip) { x0 = boxbounds.x0 + 6.0f * chwidth + (float)x * cellwidth; y0 = boxbounds.y0 + 2.0f * chheight; - container.add_char(x0 + 0.5f * (cellwidth - chwidth), y0, chheight, mui.machine().render().ui_aspect(), rgb_t::white(), *ui_font, "0123456789ABCDEF"[x & 0xf]); + container.add_char(x0 + 0.5f * (cellwidth - chwidth), y0, chheight, aspect, rgb_t::white(), *ui_font, "0123456789ABCDEF"[x & 0xf]); // if we're skipping, draw a point between the character and the box to indicate which // one it's referring to @@ -486,8 +486,8 @@ static void palette_handler(mame_ui_manager &mui, render_container &container, u } // draw the side column headers - skip = (int)(chheight / cellheight); - for (y = 0; y < state.palette.columns; y += 1 + skip) + skip = int(chheight / cellheight); + for (int y = 0; y < state.palette.columns; y += 1 + skip) // only display if there is data to show if (state.palette.offset + y * state.palette.columns < total) @@ -503,21 +503,21 @@ static void palette_handler(mame_ui_manager &mui, render_container &container, u // draw the row header sprintf(buffer, "%5X", state.palette.offset + y * state.palette.columns); - for (x = 4; x >= 0; x--) + for (int x = 4; x >= 0; x--) { - x0 -= ui_font->char_width(chheight, mui.machine().render().ui_aspect(), buffer[x]); - container.add_char(x0, y0 + 0.5f * (cellheight - chheight), chheight, mui.machine().render().ui_aspect(), rgb_t::white(), *ui_font, buffer[x]); + x0 -= ui_font->char_width(chheight, aspect, buffer[x]); + container.add_char(x0, y0 + 0.5f * (cellheight - chheight), chheight, aspect, rgb_t::white(), *ui_font, buffer[x]); } } // now add the rectangles for the colors - for (y = 0; y < state.palette.columns; y++) - for (x = 0; x < state.palette.columns; x++) + for (int y = 0; y < state.palette.columns; y++) + for (int x = 0; x < state.palette.columns; x++) { int index = state.palette.offset + y * state.palette.columns + x; if (index < total) { - pen_t pen = state.palette.which ? palette->indirect_color(index) : raw_color[index]; + const pen_t pen = state.palette.which ? palette->indirect_color(index) : raw_color[index]; container.add_rect(cellboxbounds.x0 + x * cellwidth, cellboxbounds.y0 + y * cellheight, cellboxbounds.x0 + (x + 1) * cellwidth, cellboxbounds.y0 + (y + 1) * cellheight, 0xff000000 | pen, PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA)); @@ -625,10 +625,6 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui ui_gfx_info &info = state.gfxdev[dev]; device_gfx_interface &interface = *info.interface; gfx_element &gfx = *interface.gfx(set); - float fullwidth, fullheight; - float cellwidth, cellheight; - float chwidth, chheight; - float titlewidth; float x0, y0; render_bounds cellboxbounds; render_bounds boxbounds; @@ -636,13 +632,14 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui int targwidth = mui.machine().render().ui_target().width(); int targheight = mui.machine().render().ui_target().height(); int cellxpix, cellypix; - int xcells, ycells; + int xcells; int pixelscale = 0; - int x, y, skip; + int skip; // add a half character padding for the box - chheight = mui.get_line_height(); - chwidth = ui_font->char_width(chheight, mui.machine().render().ui_aspect(), '0'); + const float aspect = mui.machine().render().ui_aspect(&container); + const float chheight = mui.get_line_height(); + const float chwidth = ui_font->char_width(chheight, aspect, '0'); boxbounds.x0 = 0.0f + 0.5f * chwidth; boxbounds.x1 = 1.0f - 0.5f * chwidth; boxbounds.y0 = 0.0f + 0.5f * chheight; @@ -662,8 +659,8 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui cellboxbounds.y0 += 3.0f * chheight; // convert back to pixels - cellboxwidth = (cellboxbounds.x1 - cellboxbounds.x0) * (float)targwidth; - cellboxheight = (cellboxbounds.y1 - cellboxbounds.y0) * (float)targheight; + cellboxwidth = (cellboxbounds.x1 - cellboxbounds.x0) * float(targwidth); + cellboxheight = (cellboxbounds.y1 - cellboxbounds.y0) * float(targheight); // compute the number of source pixels in a cell cellxpix = 1 + ((info.rotate[set] & ORIENTATION_SWAP_XY) ? gfx.height() : gfx.width()); @@ -684,20 +681,20 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui pixelscale = std::max(1, pixelscale); // in the Y direction, we just display as many as we can - ycells = cellboxheight / (pixelscale * cellypix); + const int ycells = cellboxheight / (pixelscale * cellypix); // now determine the actual cellbox size cellboxwidth = std::min(cellboxwidth, xcells * pixelscale * cellxpix); cellboxheight = std::min(cellboxheight, ycells * pixelscale * cellypix); // compute the size of a single cell at this pixel scale factor, as well as the aspect ratio - cellwidth = (cellboxwidth / (float)xcells) / (float)targwidth; - cellheight = (cellboxheight / (float)ycells) / (float)targheight; + const float cellwidth = (cellboxwidth / float(xcells)) / float(targwidth); + const float cellheight = (cellboxheight / float(ycells)) / float(targheight); //cellaspect = cellwidth / cellheight; // working from the new width/height, recompute the boxbounds - fullwidth = (float)cellboxwidth / (float)targwidth + 6.5f * chwidth; - fullheight = (float)cellboxheight / (float)targheight + 4.0f * chheight; + const float fullwidth = float(cellboxwidth) / float(targwidth) + 6.5f * chwidth; + const float fullheight = float(cellboxheight) / float(targheight) + 4.0f * chheight; // recompute boxbounds from this boxbounds.x0 = (1.0f - fullwidth) * 0.5f; @@ -707,9 +704,9 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui // recompute cellboxbounds cellboxbounds.x0 = boxbounds.x0 + 6.0f * chwidth; - cellboxbounds.x1 = cellboxbounds.x0 + (float)cellboxwidth / (float)targwidth; + cellboxbounds.x1 = cellboxbounds.x0 + float(cellboxwidth) / float(targwidth); cellboxbounds.y0 = boxbounds.y0 + 3.5f * chheight; - cellboxbounds.y1 = cellboxbounds.y0 + (float)cellboxheight / (float)targheight; + cellboxbounds.y1 = cellboxbounds.y0 + float(cellboxheight) / float(targheight); // figure out the title std::ostringstream title_buf; @@ -720,7 +717,7 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui int32_t mouse_target_x, mouse_target_y; float mouse_x, mouse_y; bool mouse_button; - render_target *mouse_target = mui.machine().ui_input().find_mouse(&mouse_target_x, &mouse_target_y, &mouse_button); + render_target *const mouse_target = mui.machine().ui_input().find_mouse(&mouse_target_x, &mouse_target_y, &mouse_button); if (mouse_target != nullptr && mouse_target->map_point_container(mouse_target_x, mouse_target_y, container, mouse_x, mouse_y) && cellboxbounds.x0 <= mouse_x && cellboxbounds.x1 > mouse_x && cellboxbounds.y0 <= mouse_y && cellboxbounds.y1 > mouse_y) @@ -748,7 +745,7 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui // expand the outer box to fit the title const std::string title = title_buf.str(); - titlewidth = ui_font->string_width(chheight, mui.machine().render().ui_aspect(), title.c_str()); + const float titlewidth = ui_font->string_width(chheight, aspect, title.c_str()); x0 = 0.0f; if (boxbounds.x1 - boxbounds.x0 < titlewidth + chwidth) x0 = boxbounds.x0 - (0.5f - 0.5f * (titlewidth + chwidth)); @@ -761,17 +758,17 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui y0 = boxbounds.y0 + 0.5f * chheight; for (auto ch : title) { - container.add_char(x0, y0, chheight, mui.machine().render().ui_aspect(), rgb_t::white(), *ui_font, ch); - x0 += ui_font->char_width(chheight, mui.machine().render().ui_aspect(), ch); + container.add_char(x0, y0, chheight, aspect, rgb_t::white(), *ui_font, ch); + x0 += ui_font->char_width(chheight, aspect, ch); } // draw the top column headers - skip = (int)(chwidth / cellwidth); - for (x = 0; x < xcells; x += 1 + skip) + skip = int(chwidth / cellwidth); + for (int x = 0; x < xcells; x += 1 + skip) { x0 = boxbounds.x0 + 6.0f * chwidth + (float)x * cellwidth; y0 = boxbounds.y0 + 2.0f * chheight; - container.add_char(x0 + 0.5f * (cellwidth - chwidth), y0, chheight, mui.machine().render().ui_aspect(), rgb_t::white(), *ui_font, "0123456789ABCDEF"[x & 0xf]); + container.add_char(x0 + 0.5f * (cellwidth - chwidth), y0, chheight, aspect, rgb_t::white(), *ui_font, "0123456789ABCDEF"[x & 0xf]); // if we're skipping, draw a point between the character and the box to indicate which // one it's referring to @@ -780,8 +777,8 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui } // draw the side column headers - skip = (int)(chheight / cellheight); - for (y = 0; y < ycells; y += 1 + skip) + skip = int(chheight / cellheight); + for (int y = 0; y < ycells; y += 1 + skip) // only display if there is data to show if (info.offset[set] + y * xcells < gfx.elements()) @@ -797,10 +794,10 @@ static void gfxset_handler(mame_ui_manager &mui, render_container &container, ui // draw the row header sprintf(buffer, "%5X", info.offset[set] + y * xcells); - for (x = 4; x >= 0; x--) + for (int x = 4; x >= 0; x--) { - x0 -= ui_font->char_width(chheight, mui.machine().render().ui_aspect(), buffer[x]); - container.add_char(x0, y0 + 0.5f * (cellheight - chheight), chheight, mui.machine().render().ui_aspect(), rgb_t::white(), *ui_font, buffer[x]); + x0 -= ui_font->char_width(chheight, aspect, buffer[x]); + container.add_char(x0, y0 + 0.5f * (cellheight - chheight), chheight, aspect, rgb_t::white(), *ui_font, buffer[x]); } } @@ -1053,11 +1050,10 @@ static void gfxset_draw_item(running_machine &machine, gfx_element &gfx, int ind static void tilemap_handler(mame_ui_manager &mui, render_container &container, ui_gfx_state &state) { render_font *ui_font = mui.get_font(); - float chwidth, chheight; render_bounds mapboxbounds; render_bounds boxbounds; - int targwidth = mui.machine().render().ui_target().width(); - int targheight = mui.machine().render().ui_target().height(); + const int targwidth = mui.machine().render().ui_target().width(); + const int targheight = mui.machine().render().ui_target().height(); float titlewidth; float x0, y0; int mapboxwidth, mapboxheight; @@ -1070,8 +1066,9 @@ static void tilemap_handler(mame_ui_manager &mui, render_container &container, u std::swap(mapwidth, mapheight); // add a half character padding for the box - chheight = mui.get_line_height(); - chwidth = ui_font->char_width(chheight, mui.machine().render().ui_aspect(), '0'); + const float aspect = mui.machine().render().ui_aspect(&container); + const float chheight = mui.get_line_height(); + const float chwidth = ui_font->char_width(chheight, aspect, '0'); boxbounds.x0 = 0.0f + 0.5f * chwidth; boxbounds.x1 = 1.0f - 0.5f * chwidth; boxbounds.y0 = 0.0f + 0.5f * chheight; @@ -1125,7 +1122,7 @@ static void tilemap_handler(mame_ui_manager &mui, render_container &container, u int32_t mouse_target_x, mouse_target_y; float mouse_x, mouse_y; bool mouse_button; - render_target *mouse_target = mui.machine().ui_input().find_mouse(&mouse_target_x, &mouse_target_y, &mouse_button); + render_target *const mouse_target = mui.machine().ui_input().find_mouse(&mouse_target_x, &mouse_target_y, &mouse_button); if (mouse_target != nullptr && mouse_target->map_point_container(mouse_target_x, mouse_target_y, container, mouse_x, mouse_y) && mapboxbounds.x0 <= mouse_x && mapboxbounds.x1 > mouse_x && mapboxbounds.y0 <= mouse_y && mapboxbounds.y1 > mouse_y) @@ -1155,7 +1152,7 @@ static void tilemap_handler(mame_ui_manager &mui, render_container &container, u // expand the outer box to fit the title const std::string title = title_buf.str(); - titlewidth = ui_font->string_width(chheight, mui.machine().render().ui_aspect(), title.c_str()); + titlewidth = ui_font->string_width(chheight, aspect, title.c_str()); if (boxbounds.x1 - boxbounds.x0 < titlewidth + chwidth) { boxbounds.x0 = 0.5f - 0.5f * (titlewidth + chwidth); @@ -1170,8 +1167,8 @@ static void tilemap_handler(mame_ui_manager &mui, render_container &container, u y0 = boxbounds.y0 + 0.5f * chheight; for (auto ch : title) { - container.add_char(x0, y0, chheight, mui.machine().render().ui_aspect(), rgb_t::white(), *ui_font, ch); - x0 += ui_font->char_width(chheight, mui.machine().render().ui_aspect(), ch); + container.add_char(x0, y0, chheight, aspect, rgb_t::white(), *ui_font, ch); + x0 += ui_font->char_width(chheight, aspect, ch); } // update the bitmap @@ -1231,7 +1228,7 @@ static void tilemap_handle_keys(running_machine &machine, ui_gfx_state &state, i } // return to (0,0) (HOME) - if( machine.ui_input().pressed(IPT_UI_HOME)) + if (machine.ui_input().pressed(IPT_UI_HOME)) { state.tilemap.xoffs = 0; state.tilemap.yoffs = 0; |