Skip to content

Commit

Permalink
WAGE: Implement console drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
sev- committed Dec 31, 2015
1 parent 141bddb commit f256636
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
85 changes: 79 additions & 6 deletions engines/wage/gui.cpp
Expand Up @@ -46,6 +46,8 @@
*/

#include "common/system.h"
#include "graphics/fontman.h"
#include "graphics/font.h"
#include "wage/wage.h"
#include "wage/design.h"
#include "wage/entities.h"
Expand All @@ -71,6 +73,8 @@ Gui::Gui() {
p.push_back(checkers);
Common::Rect r(0, 0, _screen.w, _screen.h);

_scrollPos = 0;

Design::drawFilledRect(&_screen, r, kColorBlack, p, 1);
}

Expand All @@ -85,8 +89,26 @@ void Gui::setScene(Scene *scene) {
}

void Gui::appendText(String &str) {
_out += str;
_out += '\n';
if (!str.contains('\n')) {
_out.push_back(str);
return;
}

// Okay, we got new lines, need to split it
// and push substrings individually
Common::String tmp = "";

for (int i = 0; i < str.size(); i++) {
if (str[i] == '\n') {
_out.push_back(tmp);
tmp = "";
continue;
}

tmp += str[i];
}

_out.push_back(tmp);
}

void Gui::draw() {
Expand Down Expand Up @@ -205,21 +227,72 @@ void Gui::paintBorder(Graphics::Surface *g, int x, int y, int width, int height,
#endif
}

enum {
kConWOverlap = 20,
kConHOverlap = 20,
kConWPadding = 2,
kConHPadding = 10,
kConOverscan = 3,
kLineSpacing = 1
};

void Gui::renderConsole(Graphics::Surface *g, int x, int y, int width, int height) {
bool fullRedraw = false;
Common::Rect fullR(0, 0, width, height);
bool textReflow = false;
int surfW = width + kConWOverlap * 2;
int surfH = height + kConHOverlap * 2;

Common::Rect boundsR(kConWOverlap - kConOverscan, kConHOverlap - kConOverscan, width + kConWOverlap + kConOverscan, height + kConHOverlap + kConOverscan);
Common::Rect fullR(0, 0, surfW, surfH);

if (_console.w != surfW || _console.h != surfH) {
if (_console.w != surfW)
textReflow = true;

if (_console.w != width || _console.h != height) {
_console.free();

_console.create(width, height, Graphics::PixelFormat::createFormatCLUT8());
_console.create(surfW, surfH, Graphics::PixelFormat::createFormatCLUT8());
fullRedraw = true;
}

if (fullRedraw)
_console.fillRect(fullR, kColorWhite);

g->copyRectToSurface(_console, x, y, fullR);
const Graphics::Font *font = FontMan.getFontByUsage(Graphics::FontManager::kConsoleFont);
int lineHeight = font->getFontHeight() + kLineSpacing;
int textW = width - kConWPadding * 2;
int textH = height - kConHPadding * 2;

if (textReflow) {
_lines.clear();

for (int i = 0; i < _out.size(); i++) {
Common::StringArray wrappedLines;

font->wordWrapText(_out[i], textW, wrappedLines);

for (Common::StringArray::const_iterator j = wrappedLines.begin(); j != wrappedLines.end(); ++j)
_lines.push_back(*j);
}
}

const int firstLine = _scrollPos / lineHeight;
const int lastLine = MIN((_scrollPos + textH) / lineHeight + 1, _lines.size());
const int xOff = kConWOverlap;
const int yOff = kConHOverlap;
int x1 = xOff + kConWPadding;
int y1 = yOff - (_scrollPos % lineHeight) + kConHPadding;

for (int line = firstLine; line < lastLine; line++) {
const char *str = _lines[line].c_str();

if (*str)
font->drawString(&_console, _lines[line], x1, y1, textW, kColorBlack);

y1 += lineHeight;
}

g->copyRectToSurface(_console, x - kConOverscan, y - kConOverscan, boundsR);
}


Expand Down
5 changes: 4 additions & 1 deletion engines/wage/gui.h
Expand Up @@ -48,6 +48,7 @@
#ifndef WAGE_GUI_H
#define WAGE_GUI_H

#include "common/str-array.h"
#include "graphics/surface.h"
#include "common/rect.h"

Expand Down Expand Up @@ -76,7 +77,9 @@ class Gui {
Scene *_scene;
bool _sceneDirty;

Common::String _out;
Common::StringArray _out;
Common::StringArray _lines;
uint _scrollPos;
};

} // End of namespace Wage
Expand Down
2 changes: 1 addition & 1 deletion engines/wage/wage.cpp
Expand Up @@ -121,7 +121,7 @@ Common::Error WageEngine::run() {
Common::String input("look");

_world->_player->_currentScene = _world->_orderedScenes[1];
//_world->_globalScript->execute(_world, 1, &input, NULL, this);
_world->_globalScript->execute(_world, 1, &input, NULL, this);

_gui->setScene(_world->_orderedScenes[1]);
_gui->draw();
Expand Down

0 comments on commit f256636

Please sign in to comment.