Skip to content

Commit

Permalink
Engine: make display message funcs use arguments instead of global vars
Browse files Browse the repository at this point in the history
This turns TopBarSettings, DisplayVars and display_message_aschar into function args.
  • Loading branch information
ivan-mogilko committed Feb 29, 2024
1 parent 12b07c2 commit 096e986
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 112 deletions.
1 change: 0 additions & 1 deletion Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ target_sources(engine
ac/textbox.h
ac/timer.cpp
ac/timer.h
ac/topbarsettings.h
ac/translation.cpp
ac/translation.h
ac/viewframe.cpp
Expand Down
2 changes: 1 addition & 1 deletion Engine/ac/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2886,7 +2886,7 @@ void _displayspeech(const char*texx, int aschar, int xx, int yy, int widd, int i
char_thinking = aschar;

set_our_eip(155);
display_main(tdxp, tdyp, bwidth, texx, DISPLAYTEXT_SPEECH, FONT_SPEECH, textcol, isThought, allowShrink, overlayPositionFixed);
display_main(tdxp, tdyp, bwidth, texx, nullptr, DISPLAYTEXT_SPEECH, FONT_SPEECH, textcol, isThought, allowShrink, overlayPositionFixed);
set_our_eip(156);
if ((play.in_conversation > 0) && (game.options[OPT_SPEECHTYPE] == 3))
closeupface = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion Engine/ac/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ void DialogOptions::Draw()

// needs to draw the right text window, not the default
Bitmap *text_window_ds = nullptr;
draw_text_window(&text_window_ds, false, &txoffs,&tyoffs,&xspos,&yspos,&areawid,nullptr,needheight, game.options[OPT_DIALOGIFACE]);
draw_text_window(&text_window_ds, false, &txoffs,&tyoffs,&xspos,&yspos,&areawid,nullptr,needheight, game.options[OPT_DIALOGIFACE], DisplayVars());
options_surface_has_alpha = guis[game.options[OPT_DIALOGIFACE]].HasAlphaChannel();
// since draw_text_window incrases the width, restore it
areawid = savedwid;
Expand Down
77 changes: 34 additions & 43 deletions Engine/ac/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "ac/speech.h"
#include "ac/string.h"
#include "ac/system.h"
#include "ac/topbarsettings.h"
#include "debug/debug_log.h"
#include "gfx/blender.h"
#include "gui/guibutton.h"
Expand All @@ -57,20 +56,11 @@ extern AGSPlatformDriver *platform;
extern int loops_per_character;
extern SpriteCache spriteset;

int display_message_aschar=0;

TopBarSettings topBar;
struct DisplayVars
{
int linespacing = 0; // font's line spacing
int fulltxtheight = 0; // total height of all the text
} disp;


// Generates a textual image and returns a disposable bitmap
Bitmap *create_textual_image(const char *text, int asspch, int isThought,
int &xx, int &yy, int &adjustedXX, int &adjustedYY, int wii, int usingfont, int allowShrink,
bool &alphaChannel)
bool &alphaChannel, const TopBarSettings *topbar)
{
//
// Configure the textual image
Expand Down Expand Up @@ -98,12 +88,13 @@ Bitmap *create_textual_image(const char *text, int asspch, int isThought,
snprintf(todis, STD_BUFFER_SIZE - 1, "%s", text);
ensure_text_valid_for_font(todis, usingfont);
break_up_text_into_lines(todis, Lines, wii - 2 * padding, usingfont);
disp.linespacing = get_font_linespacing(usingfont);
disp.fulltxtheight = get_text_lines_surf_height(usingfont, Lines.Count());
DisplayVars disp(
get_font_linespacing(usingfont),
get_text_lines_surf_height(usingfont, Lines.Count()));

if (topBar.wantIt) {
if (topbar) {
// ensure that the window is wide enough to display any top bar text
int topBarWid = get_text_width_outlined(topBar.text, topBar.font);
int topBarWid = get_text_width_outlined(topbar->Text.GetCStr(), topbar->Font);
topBarWid += data_to_game_coord(play.top_bar_borderwidth + 2) * 2;
if (longestline < topBarWid)
longestline = topBarWid;
Expand All @@ -112,10 +103,10 @@ Bitmap *create_textual_image(const char *text, int asspch, int isThought,
const Rect &ui_view = play.GetUIViewport();
if (xx == OVR_AUTOPLACE);
// centre text in middle of screen
else if (yy<0) yy = ui_view.GetHeight() / 2 - disp.fulltxtheight / 2 - padding;
else if (yy<0) yy = ui_view.GetHeight() / 2 - disp.FullTextHeight / 2 - padding;
// speech, so it wants to be above the character's head
else if (asspch > 0) {
yy -= disp.fulltxtheight;
yy -= disp.FullTextHeight;
if (yy < 5) yy = 5;
yy = adjust_y_for_guis(yy);
}
Expand Down Expand Up @@ -148,7 +139,7 @@ Bitmap *create_textual_image(const char *text, int asspch, int isThought,
const int extraHeight = paddingDoubledScaled;
color_t text_color = MakeColor(15);
const int bmp_width = std::max(2, wii);
const int bmp_height = std::max(2, disp.fulltxtheight + extraHeight);
const int bmp_height = std::max(2, disp.FullTextHeight + extraHeight);
Bitmap *text_window_ds = BitmapHelper::CreateTransparentBitmap(bmp_width, bmp_height, game.GetColorDepth());

// inform draw_text_window to free the old bitmap
Expand Down Expand Up @@ -180,7 +171,8 @@ Bitmap *create_textual_image(const char *text, int asspch, int isThought,

if (drawBackground)
{
draw_text_window_and_bar(&text_window_ds, wantFreeScreenop, &ttxleft, &ttxtop, &adjustedXX, &adjustedYY, &wii, &text_color, 0, usingGui);
draw_text_window_and_bar(&text_window_ds, wantFreeScreenop, topbar, disp,
&ttxleft, &ttxtop, &adjustedXX, &adjustedYY, &wii, &text_color, 0, usingGui);
if (usingGui > 0)
{
alphaChannel = guis[usingGui].HasAlphaChannel();
Expand All @@ -191,7 +183,7 @@ Bitmap *create_textual_image(const char *text, int asspch, int isThought,

for (size_t ee = 0; ee<Lines.Count(); ee++) {
//int ttxp=wii/2 - get_text_width_outlined(lines[ee], usingfont)/2;
int ttyp = ttxtop + ee*disp.linespacing;
int ttyp = ttxtop + ee * disp.Linespacing;
// asspch < 0 means that it's inside a text box so don't
// centre the text
if (asspch < 0) {
Expand All @@ -211,7 +203,7 @@ Bitmap *create_textual_image(const char *text, int asspch, int isThought,
}
else {
int xoffs, yoffs, oriwid = wii - padding * 2;
draw_text_window_and_bar(&text_window_ds, wantFreeScreenop, &xoffs, &yoffs, &adjustedXX, &adjustedYY, &wii, &text_color);
draw_text_window_and_bar(&text_window_ds, wantFreeScreenop, topbar, disp, &xoffs, &yoffs, &adjustedXX, &adjustedYY, &wii, &text_color);

if (game.options[OPT_TWCUSTOM] > 0)
{
Expand All @@ -221,7 +213,7 @@ Bitmap *create_textual_image(const char *text, int asspch, int isThought,
adjust_y_coordinate_for_text(&yoffs, usingfont);

for (size_t ee = 0; ee<Lines.Count(); ee++)
wouttext_aligned(text_window_ds, xoffs, yoffs + ee * disp.linespacing, oriwid, usingfont, text_color, Lines[ee].GetCStr(), play.text_align);
wouttext_aligned(text_window_ds, xoffs, yoffs + ee * disp.Linespacing, oriwid, usingfont, text_color, Lines[ee].GetCStr(), play.text_align);
}

return text_window_ds;
Expand Down Expand Up @@ -266,7 +258,8 @@ bool display_check_user_input(int skip)
// Pass yy = -1 to find Y co-ord automatically
// allowShrink = 0 for none, 1 for leftwards, 2 for rightwards
// pass blocking=2 to create permanent overlay
ScreenOverlay *display_main(int xx, int yy, int wii, const char *text, int disp_type, int usingfont,
ScreenOverlay *display_main(int xx, int yy, int wii, const char *text,
const TopBarSettings *topbar, int disp_type, int usingfont,
int asspch, int isThought, int allowShrink, bool overlayPositionFixed, bool roomlayer)
{
//
Expand All @@ -288,9 +281,9 @@ ScreenOverlay *display_main(int xx, int yy, int wii, const char *text, int disp_
// _display_main may be called even for custom textual overlays
EndSkippingUntilCharStops();

if (topBar.wantIt)
if (topbar)
{
// the top bar should behave like DisplaySpeech wrt blocking
// the top bar should behave like DisplaySpeech wrt blocking (???)
disp_type = DISPLAYTEXT_SPEECH;
}

Expand Down Expand Up @@ -330,7 +323,7 @@ ScreenOverlay *display_main(int xx, int yy, int wii, const char *text, int disp_
int adjustedXX, adjustedYY;
bool alphaChannel;
Bitmap *text_window_ds = create_textual_image(text, asspch, isThought,
xx, yy, adjustedXX, adjustedYY, wii, usingfont, allowShrink, alphaChannel);
xx, yy, adjustedXX, adjustedYY, wii, usingfont, allowShrink, alphaChannel, topbar);

size_t nse = add_screen_overlay(roomlayer, xx, yy, ovrtype, text_window_ds, adjustedXX - xx, adjustedYY - yy, alphaChannel);
auto *over = get_overlay(nse); // FIXME: optimize return value
Expand Down Expand Up @@ -414,13 +407,13 @@ ScreenOverlay *display_main(int xx, int yy, int wii, const char *text, int disp_
return nullptr;
}

void display_at(int xx, int yy, int wii, const char *text)
void display_at(int xx, int yy, int wii, const char *text, const TopBarSettings *topbar)
{
EndSkippingUntilCharStops();
// Start voice-over, if requested by the tokens in speech text
try_auto_play_speech(text, text, play.narrator_speech);

display_main(xx, yy, wii, text, DISPLAYTEXT_MESSAGEBOX, FONT_NORMAL, 0, 0, 0, false);
display_main(xx, yy, wii, text, topbar, DISPLAYTEXT_MESSAGEBOX, FONT_NORMAL, 0, 0, 0, false);

// Stop any blocking voice-over, if was started by this function
if (play.IsBlockingVoiceSpeech())
Expand Down Expand Up @@ -746,7 +739,8 @@ int get_textwindow_padding(int ifnum) {
}

void draw_text_window(Bitmap **text_window_ds, bool should_free_ds,
int*xins,int*yins,int*xx,int*yy,int*wii, color_t *set_text_color, int ovrheight, int ifnum) {
int*xins,int*yins,int*xx,int*yy,int*wii, color_t *set_text_color,
int ovrheight, int ifnum, const DisplayVars &disp) {
assert(text_window_ds);
Bitmap *ds = *text_window_ds;
if (ifnum < 0)
Expand All @@ -773,7 +767,7 @@ void draw_text_window(Bitmap **text_window_ds, bool should_free_ds,
xx[0]-=game.SpriteInfos[tbnum].Width;
yy[0]-=game.SpriteInfos[tbnum].Height;
if (ovrheight == 0)
ovrheight = disp.fulltxtheight;
ovrheight = disp.FullTextHeight;

if (should_free_ds)
delete *text_window_ds;
Expand All @@ -790,41 +784,38 @@ void draw_text_window(Bitmap **text_window_ds, bool should_free_ds,
}

void draw_text_window_and_bar(Bitmap **text_window_ds, bool should_free_ds,
const TopBarSettings *topbar, const DisplayVars &disp,
int*xins,int*yins,int*xx,int*yy,int*wii,color_t *set_text_color,int ovrheight, int ifnum) {

assert(text_window_ds);
draw_text_window(text_window_ds, should_free_ds, xins, yins, xx, yy, wii, set_text_color, ovrheight, ifnum);
draw_text_window(text_window_ds, should_free_ds, xins, yins, xx, yy, wii, set_text_color, ovrheight, ifnum, disp);

if ((topBar.wantIt) && (text_window_ds && *text_window_ds)) {
if ((topbar) && (text_window_ds && *text_window_ds)) {
// top bar on the dialog window with character's name
// create an enlarged window, then free the old one
Bitmap *ds = *text_window_ds;
Bitmap *newScreenop = BitmapHelper::CreateBitmap(ds->GetWidth(), ds->GetHeight() + topBar.height, game.GetColorDepth());
newScreenop->Blit(ds, 0, 0, 0, topBar.height, ds->GetWidth(), ds->GetHeight());
Bitmap *newScreenop = BitmapHelper::CreateBitmap(ds->GetWidth(), ds->GetHeight() + topbar->Height, game.GetColorDepth());
newScreenop->Blit(ds, 0, 0, 0, topbar->Height, ds->GetWidth(), ds->GetHeight());
delete *text_window_ds;
*text_window_ds = newScreenop;
ds = *text_window_ds;

// draw the top bar
color_t draw_color = ds->GetCompatibleColor(play.top_bar_backcolor);
ds->FillRect(Rect(0, 0, ds->GetWidth() - 1, topBar.height - 1), draw_color);
ds->FillRect(Rect(0, 0, ds->GetWidth() - 1, topbar->Height - 1), draw_color);
if (play.top_bar_backcolor != play.top_bar_bordercolor) {
// draw the border
draw_color = ds->GetCompatibleColor(play.top_bar_bordercolor);
for (int j = 0; j < data_to_game_coord(play.top_bar_borderwidth); j++)
ds->DrawRect(Rect(j, j, ds->GetWidth() - (j + 1), topBar.height - (j + 1)), draw_color);
ds->DrawRect(Rect(j, j, ds->GetWidth() - (j + 1), topbar->Height - (j + 1)), draw_color);
}

// draw the text
int textx = (ds->GetWidth() / 2) - get_text_width_outlined(topBar.text, topBar.font) / 2;
int textx = (ds->GetWidth() / 2) - get_text_width_outlined(topbar->Text.GetCStr(), topbar->Font) / 2;
color_t text_color = ds->GetCompatibleColor(play.top_bar_textcolor);
wouttext_outline(ds, textx, play.top_bar_borderwidth + get_fixed_pixel_size(1), topBar.font, text_color, topBar.text);
wouttext_outline(ds, textx, play.top_bar_borderwidth + get_fixed_pixel_size(1), topbar->Font, text_color, topbar->Text.GetCStr());

// don't draw it next time
topBar.wantIt = 0;
// adjust the text Y position
yins[0] += topBar.height;
yins[0] += topbar->Height;
}
else if (topBar.wantIt)
topBar.wantIt = 0;
}
40 changes: 35 additions & 5 deletions Engine/ac/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
//
//=============================================================================
//
//
// Functions for displaying various standard textual overlays.
//
//=============================================================================
#ifndef __AGS_EE_AC__DISPLAY_H
#define __AGS_EE_AC__DISPLAY_H

#include "gui/guimain.h"
#include "gfx/bitmap.h"
#include "util/string.h"

using AGS::Common::GUIMain;

Expand All @@ -32,12 +34,36 @@ using AGS::Common::GUIMain;
// also accepts explicit overlay ID >= OVER_CUSTOM

struct ScreenOverlay;

struct TopBarSettings
{
AGS::Common::String Text;
int Font = 0;
int Height = 0;

TopBarSettings() = default;
TopBarSettings(const AGS::Common::String &text, int font, int height)
: Text(text), Font(font), Height(height) {}
};

// TODO: this struct seems bit redundant now, but may be merged with other display params
struct DisplayVars
{
int Linespacing = 0; // font's line spacing
int FullTextHeight = 0; // total height of all the text

DisplayVars() = default;
DisplayVars(int linespacing, int fulltxheight)
: Linespacing(linespacing), FullTextHeight(fulltxheight) {}
};

// Generates a textual image from the given text and parameters;
// see _display_main's comment below for parameters description.
// NOTE: this function treats text as-is, not doing any processing over it.
// TODO: refactor this collection of args into 1-2 structs with params.
Common::Bitmap *create_textual_image(const char *text, int asspch, int isThought,
int &xx, int &yy, int &adjustedXX, int &adjustedYY, int wii, int usingfont, int allowShrink,
bool &alphaChannel);
bool &alphaChannel, const TopBarSettings *topbar);
// Creates a textual overlay using the given parameters;
// Pass yy = -1 to find Y co-ord automatically
// allowShrink = 0 for none, 1 for leftwards, 2 for rightwards
Expand All @@ -49,10 +75,12 @@ Common::Bitmap *create_textual_image(const char *text, int asspch, int isThought
// > 0 - suppose it's a classic LA-style speech above character's head
// NOTE: this function treats the text as-is; it assumes that any processing
// (translation, parsing voice token) was done prior to its call.
ScreenOverlay *display_main(int xx, int yy, int wii, const char *text, int disp_type, int usingfont,
// TODO: refactor this collection of args into few args + 1-2 structs with extended params.
ScreenOverlay *display_main(int xx, int yy, int wii, const char *text,
const TopBarSettings *topbar, int disp_type, int usingfont,
int asspch, int isThought, int allowShrink, bool overlayPositionFixed, bool roomlayer = false);
// Displays a standard blocking message box at a given position
void display_at(int xx, int yy, int wii, const char *text);
void display_at(int xx, int yy, int wii, const char *text, const TopBarSettings *topbar);
// Cleans up display message state
void post_display_cleanup();
// Tests the given string for the voice-over tags and plays cue clip for the given character;
Expand Down Expand Up @@ -81,8 +109,10 @@ int get_textwindow_top_border_height (int twgui);
// point text_window_ds to it
// returns text start x & y pos in parameters
// Warning!: draw_text_window() and draw_text_window_and_bar() can create new text_window_ds
void draw_text_window(Common::Bitmap **text_window_ds, bool should_free_ds, int*xins,int*yins,int*xx,int*yy,int*wii,color_t *set_text_color,int ovrheight, int ifnum);
void draw_text_window(Common::Bitmap **text_window_ds, bool should_free_ds, int*xins,int*yins,int*xx,int*yy,int*wii,
color_t *set_text_color,int ovrheight, int ifnum, const DisplayVars &disp);
void draw_text_window_and_bar(Common::Bitmap **text_window_ds, bool should_free_ds,
const TopBarSettings *topbar, const DisplayVars &disp,
int*xins,int*yins,int*xx,int*yy,int*wii,color_t *set_text_color,int ovrheight=0, int ifnum=-1);
int get_textwindow_padding(int ifnum);

Expand Down
Loading

0 comments on commit 096e986

Please sign in to comment.