Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move letters off the grid to make text more readable #656

Open
wants to merge 6 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/brogue/Buttons.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ void drawButton(brogueButton *button, enum buttonDrawStates highlight, screenDis

short symbolNumber = 0;

CellTextInfo buttonTextInfo = (CellTextInfo) {
.mode = 2,
.firstColumn = button->x,
.lastColumn = button->x + width - 1,
};

for (int i = 0, textLoc = 0; i < width && i + button->x < COLS; i++, textLoc++) {
while (button->text[textLoc] == COLOR_ESCAPE) {
textLoc = decodeMessageColor(button->text, textLoc, &fColorBase);
Expand Down Expand Up @@ -115,6 +121,9 @@ void drawButton(brogueButton *button, enum buttonDrawStates highlight, screenDis
if (dbuf) {
// Only buffers can have opacity set.
dbuf->cells[button->x + i][button->y].opacity = opacity;
dbuf->cells[button->x + i][button->y].textInfo = buttonTextInfo;
} else {
displayBuffer.cells[button->x + i][button->y].textInfo = buttonTextInfo;
}
}
}
Expand Down
175 changes: 134 additions & 41 deletions src/brogue/IO.c

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion src/brogue/MainMenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,14 +890,23 @@ void mainBrogueJunction() {
initializeLaunchArguments(&rogue.nextGame, rogue.nextGamePath, &rogue.nextGameSeed);

do {
// In between menus and games, zero out the textinfo.
for (int x = 0; x < COLS; x++) {
for (int y = 0; y < ROWS; y++) {
displayBuffer.cells[x][y].textInfo = (CellTextInfo) { .mode = 0 };
}
}
rogue.gameHasEnded = false;
rogue.playbackFastForward = false;
rogue.playbackMode = false;
switch (rogue.nextGame) {
case NG_NOTHING:
case NG_NOTHING: {
// Run the main menu to get a decision out of the player.
const SavedDisplayBuffer rbuf = saveDisplayBuffer();
titleMenu();
restoreDisplayBuffer(&rbuf);
break;
}
case NG_GAME_VARIANT:
rogue.nextGame = NG_NOTHING;
initializeGameVariant();
Expand Down
21 changes: 19 additions & 2 deletions src/brogue/Rogue.h
Original file line number Diff line number Diff line change
Expand Up @@ -1283,12 +1283,22 @@ enum dungeonLayers {
NUMBER_TERRAIN_LAYERS
};

typedef struct CellTextInfo {
/// 0 : Normal terminal output
/// 1 : Left-aligned text [startColumn must be set]
/// 2 : Middle-aligned text [startColumn and endColumn must be set]
Comment on lines +1288 to +1289
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 1 : Left-aligned text [startColumn must be set]
/// 2 : Middle-aligned text [startColumn and endColumn must be set]
/// 1 : Left-aligned text [firstColumn must be set]
/// 2 : Middle-aligned text [firstColumn and lastColumn must be set]

int8_t mode;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: what about an enum?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that would be a good idea, I was being lazy.

I'll fix this after I rebase the PR to fix the conflicts.

int8_t firstColumn; // first column (x coordinate) of text (inclusive)
int8_t lastColumn; // last column (x coordinate) of text (inclusive)
} CellTextInfo;

// keeps track of graphics so we only redraw if the cell has changed:
typedef struct cellDisplayBuffer {
enum displayGlyph character;
char foreColorComponents[3];
char backColorComponents[3];
char opacity;
CellTextInfo textInfo;
} cellDisplayBuffer;

typedef struct screenDisplayBuffer {
Expand Down Expand Up @@ -2884,7 +2894,8 @@ extern "C" {
void plotChar(enum displayGlyph inputChar,
short xLoc, short yLoc,
short backRed, short backGreen, short backBlue,
short foreRed, short foreGreen, short foreBlue);
short foreRed, short foreGreen, short foreBlue,
CellTextInfo textInfo);

typedef struct PauseBehavior {
/// If `interuptForMouseMove` is true, then the pause function will return `true`
Expand Down Expand Up @@ -2931,7 +2942,7 @@ extern "C" {
short x, short y, short width,
boolean includeButtons);
void funkyFade(screenDisplayBuffer *displayBuf, const color *colorStart, const color *colorEnd, short stepCount, short x, short y, boolean invert);
void displayCenteredAlert(char *message);
void displayCenteredAlert(const char *message);
void flashMessage(char *message, short x, short y, int time, const color *fColor, const color *bColor);
void flashTemporaryAlert(char *message, int time);
void highlightScreenCell(short x, short y, const color *highlightColor, short strength);
Expand All @@ -2956,7 +2967,9 @@ extern "C" {
void hiliteCell(short x, short y, const color *hiliteColor, short hiliteStrength, boolean distinctColors);
void colorMultiplierFromDungeonLight(short x, short y, color *editColor);
void plotCharWithColor(enum displayGlyph inputChar, windowpos loc, const color *cellForeColor, const color *cellBackColor);
void plotCharWithColorAndTextInfo(enum displayGlyph inputChar, windowpos loc, const color *cellForeColor, const color *cellBackColor, CellTextInfo textInfo);
void plotCharToBuffer(enum displayGlyph inputChar, windowpos loc, const color *foreColor, const color *backColor, screenDisplayBuffer *dbuf);
void plotCharToBufferWithTextInfo(enum displayGlyph inputChar, windowpos loc, const color *foreColor, const color *backColor, CellTextInfo textInfo, screenDisplayBuffer *dbuf);
void plotForegroundChar(enum displayGlyph inputChar, short x, short y, const color *foreColor, boolean affectedByLighting);
void commitDraws(void);
void dumpLevelToScreen(void);
Expand All @@ -2978,7 +2991,11 @@ extern "C" {
void flashForeground(short *x, short *y, const color **flashColor, short *flashStrength, short count, short frames);
void flashCell(const color *theColor, short frames, short x, short y);
void colorFlash(const color *theColor, unsigned long reqTerrainFlags, unsigned long reqTileFlags, short frames, short maxRadius, short x, short y);
// Prints the specified formatted string to the screen, starting at the coordinates (x, y).
void printString(const char *theString, short x, short y, const color *foreColor, const color* backColor, screenDisplayBuffer *dbuf);
void printStringWithTextMode(const char *theString, short x, short y, const color *foreColor, const color* backColor, int textMode, screenDisplayBuffer *dbuf);
// The same as `printString`, but centers the text when performing letter spacing adjustments.
void printStringCentered(const char *theString, short x, short y, const color *foreColor, const color* backColor, screenDisplayBuffer *dbuf);
short wrapText(char *to, const char *sourceText, short width);
short printStringWithWrapping(const char *theString, short x, short y, short width, const color *foreColor,
const color *backColor, screenDisplayBuffer *dbuf);
Expand Down
3 changes: 2 additions & 1 deletion src/platform/null-platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ static void null_nextKeyOrMouseEvent(rogueEvent *returnEvent, boolean textInput,
static void null_plotChar(enum displayGlyph ch,
short xLoc, short yLoc,
short foreRed, short foreGreen, short foreBlue,
short backRed, short backGreen, short backBlue) {
short backRed, short backGreen, short backBlue,
CellTextInfo textInfo) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ struct brogueConsole {
enum displayGlyph inputChar,
short x, short y,
short foreRed, short foreGreen, short foreBlue,
short backRed, short backGreen, short backBlue
short backRed, short backGreen, short backBlue,
CellTextInfo textInfo
);

void (*remap)(const char *, const char *);
Expand Down
5 changes: 3 additions & 2 deletions src/platform/platformdependent.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ boolean isEnvironmentGlyph(enum displayGlyph glyph) {
void plotChar(enum displayGlyph inputChar,
short xLoc, short yLoc,
short foreRed, short foreGreen, short foreBlue,
short backRed, short backGreen, short backBlue) {
currentConsole.plotChar(inputChar, xLoc, yLoc, foreRed, foreGreen, foreBlue, backRed, backGreen, backBlue);
short backRed, short backGreen, short backBlue,
CellTextInfo textInfo) {
currentConsole.plotChar(inputChar, xLoc, yLoc, foreRed, foreGreen, foreBlue, backRed, backGreen, backBlue, textInfo);
}

boolean shiftKeyIsDown() {
Expand Down
5 changes: 3 additions & 2 deletions src/platform/sdl2-platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,12 @@ static void _plotChar(
enum displayGlyph inputChar,
short x, short y,
short foreRed, short foreGreen, short foreBlue,
short backRed, short backGreen, short backBlue
short backRed, short backGreen, short backBlue,
CellTextInfo textInfo
) {
updateTile(y, x, fontIndex(inputChar),
foreRed, foreGreen, foreBlue,
backRed, backGreen, backBlue);
backRed, backGreen, backBlue, textInfo);
}


Expand Down
38 changes: 35 additions & 3 deletions src/platform/tiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef struct ScreenTile {
short foreRed, foreGreen, foreBlue; // foreground color (0..100)
short backRed, backGreen, backBlue; // background color (0..100)
short charIndex; // index of the glyph to draw
CellTextInfo textInfo;
short needsRefresh; // true if the tile has changed since the last screen refresh, else false
} ScreenTile;

Expand Down Expand Up @@ -589,7 +590,8 @@ static void createTextures(SDL_Renderer *renderer, int outputWidth, int outputHe
///
void updateTile(int row, int column, short charIndex,
short foreRed, short foreGreen, short foreBlue,
short backRed, short backGreen, short backBlue)
short backRed, short backGreen, short backBlue,
CellTextInfo textInfo)
{
screenTiles[row][column] = (ScreenTile){
.foreRed = foreRed,
Expand All @@ -599,6 +601,7 @@ void updateTile(int row, int column, short charIndex,
.backGreen = backGreen,
.backBlue = backBlue,
.charIndex = charIndex,
.textInfo = textInfo,
.needsRefresh = 1
};
}
Expand Down Expand Up @@ -692,8 +695,11 @@ void updateScreen() {
continue; // this tile uses another texture and gets painted at another step
}

int tileRow = tile->charIndex / 16;
int tileColumn = tile->charIndex % 16;
int textMode = tile->textInfo.mode;
int charIndex = tile->charIndex;

int tileRow = charIndex / 16;
int tileColumn = charIndex % 16;

if (tileEmpty[tileRow][tileColumn]
&& !(tileRow == 21 && tileColumn == 1)) { // wall top (procedural)
Expand All @@ -712,6 +718,32 @@ void updateScreen() {
dest.x = x * outputWidth / COLS;
dest.y = y * outputHeight / ROWS;

if (textMode == 1) {
// If it's text, then we want to compress the letters
// so the spacing between consecutive letters is more
// natural and readable.
dest.x -= outputWidth * (x - tile->textInfo.firstColumn) / 5 / COLS;
}
if (textMode == 2) {
int offsetForLetter = outputWidth * (x - tile->textInfo.firstColumn) / 5 / COLS;
int offsetForEnd = outputWidth * (tile->textInfo.lastColumn - tile->textInfo.firstColumn) / 5 / COLS;
// `offsetForLetter` is zero for the first letter, and increases for each
// subsequent letter in the line.
dest.x -= offsetForLetter;
// `offsetForEnd` is a constant for all text in the same contiguous span.
// By adding half its value back, we shift the text over so that the first
// letter and the last letter in the span are shifted equally for kerning,
// so that spacing on both sides is uniform.
dest.x += offsetForEnd / 2;
}

// int diff = abs(tile->foreRed - tile->backRed) + abs(tile->foreGreen - tile->backGreen) + abs(tile->foreBlue - tile->backBlue);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This is some debugging logic, I'll remove it

// if (diff < 100) {
// tile->foreRed = 100;
// tile->foreGreen = 100;
// tile->foreBlue = 100;
// }

// blend the foreground
if (SDL_SetTextureColorMod(Textures[step],
round(2.55 * tile->foreRed),
Expand Down
4 changes: 3 additions & 1 deletion src/platform/tiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#define __TILES_H__

#include <SDL.h>
#include "Rogue.h"

void initTiles(void);
void resizeWindow(int width, int height);
void updateTile(int row, int column, short charIndex,
short foreRed, short foreGreen, short foreBlue,
short backRed, short backGreen, short backBlue);
short backRed, short backGreen, short backBlue,
CellTextInfo textInfo);
void updateScreen(void);
SDL_Surface *captureScreen(void);

Expand Down