Skip to content

Commit

Permalink
BLADERUNNER: Abstraction from 555 pixel format
Browse files Browse the repository at this point in the history
Removing hardcoded 555 pixel format to support Android
  • Loading branch information
peterkohaut committed Apr 17, 2019
1 parent 955bf0c commit e86ee33
Show file tree
Hide file tree
Showing 33 changed files with 402 additions and 341 deletions.
13 changes: 7 additions & 6 deletions engines/bladerunner/bladerunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void BladeRunnerEngine::pauseEngineIntern(bool pause) {
}

Common::Error BladeRunnerEngine::run() {
Graphics::PixelFormat format = createRGB555();
Graphics::PixelFormat format = screenPixelForrmat();
initGraphics(640, 480, &format);

_system->showMouse(true);
Expand Down Expand Up @@ -375,8 +375,8 @@ bool BladeRunnerEngine::startup(bool hasSavegames) {

// This is the original startup in the game

_surfaceFront.create(640, 480, createRGB555());
_surfaceBack.create(640, 480, createRGB555());
_surfaceFront.create(640, 480, screenPixelForrmat());
_surfaceBack.create(640, 480, screenPixelForrmat());

_time = new Time(this);

Expand Down Expand Up @@ -535,7 +535,7 @@ bool BladeRunnerEngine::startup(bool hasSavegames) {
_scores = new Scores(this);

_mainFont = new Font(this);
_mainFont->open("KIA6PT.FON", 640, 480, -1, 0, 0x252D);
_mainFont->open("KIA6PT.FON", 640, 480, -1, 0, _surfaceFront.format.RGBToColor(72, 72, 104));
_mainFont->setSpacing(1, 0);

for (int i = 0; i != 43; ++i) {
Expand Down Expand Up @@ -1874,7 +1874,7 @@ void BladeRunnerEngine::playerDied() {
_kia->open(kKIASectionLoad);
}

bool BladeRunnerEngine::saveGame(Common::WriteStream &stream, const Graphics::Surface &thumbnail) {
bool BladeRunnerEngine::saveGame(Common::WriteStream &stream, Graphics::Surface &thumbnail) {
if ( !_gameIsAutoSaving
&& ( !playerHasControl() || _sceneScript->isInsideScript() || _aiScripts->isInsideScript())
){
Expand All @@ -1884,6 +1884,7 @@ bool BladeRunnerEngine::saveGame(Common::WriteStream &stream, const Graphics::Su
Common::MemoryWriteStreamDynamic memoryStream(DisposeAfterUse::YES);
SaveFileWriteStream s(memoryStream);

thumbnail.convertToInPlace(gameDataPixelFormat());
s.write(thumbnail.getPixels(), SaveFileManager::kThumbnailSize);
s.writeFloat(1.0f);
_settings->save(s);
Expand Down Expand Up @@ -2080,7 +2081,7 @@ void BladeRunnerEngine::blitToScreen(const Graphics::Surface &src) const {

Graphics::Surface BladeRunnerEngine::generateThumbnail() const {
Graphics::Surface thumbnail;
thumbnail.create(640 / 8, 480 / 8, createRGB555());
thumbnail.create(640 / 8, 480 / 8, _surfaceFront.format);

for (int y = 0; y < thumbnail.h; ++y) {
for (int x = 0; x < thumbnail.w; ++x) {
Expand Down
11 changes: 8 additions & 3 deletions engines/bladerunner/bladerunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class BladeRunnerEngine : public Engine {
void playerGainsControl();
void playerDied();

bool saveGame(Common::WriteStream &stream, const Graphics::Surface &thumbnail);
bool saveGame(Common::WriteStream &stream, Graphics::Surface &thumbnail);
bool loadGame(Common::SeekableReadStream &stream);
void newGame(int difficulty);
void autoSaveGame(int textId, bool endgame);
Expand All @@ -308,8 +308,13 @@ class BladeRunnerEngine : public Engine {
Common::String getTargetName() const;
};

static inline const Graphics::PixelFormat createRGB555() {
return Graphics::PixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0);
static inline const Graphics::PixelFormat gameDataPixelFormat() {
return Graphics::PixelFormat(2, 5, 5, 5, 1, 10, 5, 0, 15);
}

static inline const Graphics::PixelFormat screenPixelForrmat() {
// Should be a format supported by Android port
return Graphics::PixelFormat(2, 5, 5, 5, 1, 11, 6, 1, 0);
}

void blit(const Graphics::Surface &src, Graphics::Surface &dst);
Expand Down
51 changes: 20 additions & 31 deletions engines/bladerunner/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,25 +1129,23 @@ void Debugger::drawSceneObjects() {
case kSceneObjectTypeUnknown:
break;
case kSceneObjectTypeActor:
color = 0x7C00; // 11111 00000 00000;
color = _vm->_surfaceFront.format.RGBToColor(255, 0, 0);
drawBBox(a, b, _vm->_view, &_vm->_surfaceFront, color);
_vm->_surfaceFront.frameRect(sceneObject->screenRectangle, color);
_vm->_mainFont->drawColor(_vm->_textActorNames->getText(sceneObject->id - kSceneObjectOffsetActors), _vm->_surfaceFront, pos.x, pos.y, color);
break;
case kSceneObjectTypeItem:
color = 0x03E0; // 00000 11111 00000
color = _vm->_surfaceFront.format.RGBToColor(0, 255, 0);
char itemText[40];
drawBBox(a, b, _vm->_view, &_vm->_surfaceFront, color);
sprintf(itemText, "item %i", sceneObject->id - kSceneObjectOffsetItems);
_vm->_surfaceFront.frameRect(sceneObject->screenRectangle, color);
_vm->_mainFont->drawColor(itemText, _vm->_surfaceFront, pos.x, pos.y, color);
break;
case kSceneObjectTypeObject:
color = 0x3DEF; //01111 01111 01111;
//if (sceneObject->_isObstacle)
// color += 0b100000000000000;
color = _vm->_surfaceFront.format.RGBToColor(127, 127, 127);
if (sceneObject->isClickable) {
color = 0x03E0; // 00000 11111 00000;
color = _vm->_surfaceFront.format.RGBToColor(0, 255, 0);
}
drawBBox(a, b, _vm->_view, &_vm->_surfaceFront, color);
_vm->_surfaceFront.frameRect(sceneObject->screenRectangle, color);
Expand Down Expand Up @@ -1175,10 +1173,7 @@ void Debugger::drawLights() {
posTarget.z = -t;

Vector3 size = Vector3(5.0f, 5.0f, 5.0f);
int colorR = (light->_color.r * 31.0f);
int colorG = (light->_color.g * 31.0f);
int colorB = (light->_color.b * 31.0f);
int color = (colorR << 10) + (colorG << 5) + colorB;
int color = _vm->_surfaceFront.format.RGBToColor(light->_color.r * 255.0f, light->_color.g * 255.0f, light->_color.b * 255.0f);

drawBBox(posOrigin - size, posOrigin + size, _vm->_view, &_vm->_surfaceFront, color);

Expand Down Expand Up @@ -1209,10 +1204,7 @@ void Debugger::drawFogs() {
posTarget.z = -t;

Vector3 size = Vector3(5.0f, 5.0f, 5.0f);
int colorR = (fog->_fogColor.r * 31.0f);
int colorG = (fog->_fogColor.g * 31.0f);
int colorB = (fog->_fogColor.b * 31.0f);
int color = (colorR << 10) + (colorG << 5) + colorB;
int color = _vm->_surfaceFront.format.RGBToColor(fog->_fogColor.r * 255.0f, fog->_fogColor.g * 255.0f, fog->_fogColor.b * 255.0f);

drawBBox(posOrigin - size, posOrigin + size, _vm->_view, &_vm->_surfaceFront, color);

Expand All @@ -1231,14 +1223,14 @@ void Debugger::drawRegions() {
for (int i = 0; i < 10; i++) {
Regions::Region *region = &_vm->_scene->_regions->_regions[i];
if (!region->present) continue;
_vm->_surfaceFront.frameRect(region->rectangle, 0x001F); // 00000 00000 11111
_vm->_surfaceFront.frameRect(region->rectangle, _vm->_surfaceFront.format.RGBToColor(0, 0, 255));
}

//draw exits
for (int i = 0; i < 10; i++) {
Regions::Region *region = &_vm->_scene->_exits->_regions[i];
if (!region->present) continue;
_vm->_surfaceFront.frameRect(region->rectangle, 0x7FFF); // 11111 11111 11111
_vm->_surfaceFront.frameRect(region->rectangle, _vm->_surfaceFront.format.RGBToColor(255, 255, 255));
}
}

Expand All @@ -1251,7 +1243,7 @@ void Debugger::drawWaypoints() {
}
Vector3 pos = waypoint->position;
Vector3 size = Vector3(3.0f, 3.0f, 3.0f);
int color = 0x7FFF; // 11111 11111 11111
int color = _vm->_surfaceFront.format.RGBToColor(255, 255, 255);
drawBBox(pos - size, pos + size, _vm->_view, &_vm->_surfaceFront, color);
Vector3 spos = _vm->_view->calculateScreenPosition(pos);
char waypointText[40];
Expand All @@ -1267,7 +1259,7 @@ void Debugger::drawWaypoints() {
}
Vector3 pos = cover->position;
Vector3 size = Vector3(3.0f, 3.0f, 3.0f);
int color = 0x7C1F; // 11111 00000 11111
int color = _vm->_surfaceFront.format.RGBToColor(255, 0, 255);
drawBBox(pos - size, pos + size, _vm->_view, &_vm->_surfaceFront, color);
Vector3 spos = _vm->_view->calculateScreenPosition(pos);
char coverText[40];
Expand All @@ -1283,7 +1275,7 @@ void Debugger::drawWaypoints() {
}
Vector3 pos = flee->position;
Vector3 size = Vector3(3.0f, 3.0f, 3.0f);
int color = 0x03FF; // 00000 11111 11111
int color = _vm->_surfaceFront.format.RGBToColor(0, 255, 255);
drawBBox(pos - size, pos + size, _vm->_view, &_vm->_surfaceFront, color);
Vector3 spos = _vm->_view->calculateScreenPosition(pos);
char fleeText[40];
Expand All @@ -1300,9 +1292,9 @@ void Debugger::drawWalkboxes() {
for (int j = 0; j < walkbox->vertexCount; j++) {
Vector3 start = _vm->_view->calculateScreenPosition(walkbox->vertices[j]);
Vector3 end = _vm->_view->calculateScreenPosition(walkbox->vertices[(j + 1) % walkbox->vertexCount]);
_vm->_surfaceFront.drawLine(start.x, start.y, end.x, end.y, 0x7FE0); // 11111 11111 00000
_vm->_surfaceFront.drawLine(start.x, start.y, end.x, end.y, _vm->_surfaceFront.format.RGBToColor(255, 255, 0));
Vector3 pos = _vm->_view->calculateScreenPosition(0.5 * (start + end));
_vm->_mainFont->drawColor(walkbox->name, _vm->_surfaceFront, pos.x, pos.y, 0x7FE0); // 11111 11111 00000
_vm->_mainFont->drawColor(walkbox->name, _vm->_surfaceFront, pos.x, pos.y, _vm->_surfaceFront.format.RGBToColor(255, 255, 0));
}
}
}
Expand All @@ -1317,19 +1309,16 @@ void Debugger::drawScreenEffects() {
Common::Rect r((entry.x + x) * 2, (entry.y + y) * 2, (entry.x + x) * 2 + 2, (entry.y + y) * 2 + 2);

int ec = entry.data[j++];
Color256 color = entry.palette[ec];
int bladeToScummVmConstant = 256 / 16;

Graphics::PixelFormat _pixelFormat = createRGB555();
int color555 = _pixelFormat.RGBToColor(
CLIP(color.r * bladeToScummVmConstant, 0, 255),
CLIP(color.g * bladeToScummVmConstant, 0, 255),
CLIP(color.b * bladeToScummVmConstant, 0, 255));
_vm->_surfaceFront.fillRect(r, color555);
const int bladeToScummVmConstant = 256 / 16;

int color = _vm->_surfaceFront.format.RGBToColor(
CLIP(entry.palette[ec].r * bladeToScummVmConstant, 0, 255),
CLIP(entry.palette[ec].g * bladeToScummVmConstant, 0, 255),
CLIP(entry.palette[ec].b * bladeToScummVmConstant, 0, 255));
_vm->_surfaceFront.fillRect(r, color);
}
}
}
}


} // End of namespace BladeRunner
13 changes: 9 additions & 4 deletions engines/bladerunner/dialogue_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ void DialogueMenu::draw(Graphics::Surface &s) {

Common::Point mouse = _vm->getMousePos();
if (mouse.x >= x && mouse.x < x2) {
s.vLine(mouse.x, y1 + 8, y2 + 2, 0x2108);
s.vLine(mouse.x, y1 + 8, y2 + 2, s.format.RGBToColor(64, 64, 64));
}
if (mouse.y >= y && mouse.y < y2) {
s.hLine(x1 + 8, mouse.y, x2 + 2, 0x2108);
s.hLine(x1 + 8, mouse.y, x2 + 2, s.format.RGBToColor(64, 64, 64));
}

_shapes[0].draw(s, x1, y1);
Expand All @@ -346,7 +346,7 @@ void DialogueMenu::draw(Graphics::Surface &s) {
for (int i = 0; i != _listSize; ++i) {
_shapes[1].draw(s, x1, y);
_shapes[4].draw(s, x2, y);
uint16 color = ((_items[i].colorIntensity >> 1) << 10) | ((_items[i].colorIntensity >> 1) << 5) | _items[i].colorIntensity;
uint16 color = s.format.RGBToColor((_items[i].colorIntensity / 2) * (256 / 32), (_items[i].colorIntensity / 2) * (256 / 32), _items[i].colorIntensity * (256 / 32));
_vm->_mainFont->drawColor(_items[i].text, s, x, y, color);
y += kLineHeight;
}
Expand Down Expand Up @@ -519,7 +519,12 @@ void DialogueMenu::darkenRect(Graphics::Surface &s, int x1, int y1, int x2, int
for (int y = y1; y != y2; ++y) {
for (int x = x1; x != x2; ++x) {
uint16 *p = (uint16 *)s.getBasePtr(x, y);
*p = (*p & 0x739C) >> 2; // 0 11100 11100 11100
uint8 r, g, b;
s.format.colorToRGB(*p, r, g, b);
r /= 4;
g /= 4;
b /= 4;
*p = s.format.RGBToColor(r, g, b);
}
}
}
Expand Down
35 changes: 14 additions & 21 deletions engines/bladerunner/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ bool Font::open(const Common::String &fileName, int screenWidth, int screenHeigh
_screenHeight = screenHeight;
_spacing1 = spacing1;
_spacing2 = spacing2;
_defaultColor = color;
_color = color;

Common::ScopedPtr<Common::SeekableReadStream> stream(_vm->getResourceStream(fileName));
Expand All @@ -69,9 +70,11 @@ bool Font::open(const Common::String &fileName, int screenWidth, int screenHeigh
_characters[i].height = stream->readUint32LE();
_characters[i].dataOffset = stream->readUint32LE();
}

for (int i = 0; i < _dataSize; i++) {
_data[i] = stream->readUint16LE();
}

return true;
}

Expand All @@ -90,10 +93,7 @@ void Font::setSpacing(int spacing1, int spacing2) {
}

void Font::setColor(uint16 color) {
if (_data && _color != color) {
replaceColor(_color, color);
_color = color;
}
_color = color;
}

void Font::draw(const Common::String &text, Graphics::Surface &surface, int x, int y) const {
Expand All @@ -114,9 +114,7 @@ void Font::draw(const Common::String &text, Graphics::Surface &surface, int x, i
}

void Font::drawColor(const Common::String &text, Graphics::Surface &surface, int x, int y, uint16 color) {
if (_color != color) {
setColor(color);
}
setColor(color);
draw(text, surface, x, y);
}

Expand Down Expand Up @@ -159,23 +157,12 @@ void Font::reset() {
_screenHeight = 0;
_spacing1 = 0;
_spacing2 = 0;
_color = 0x7FFF;
_color = screenPixelForrmat().RGBToColor(255, 255, 255);
_intersperse = 0;

memset(_characters, 0, 256 * sizeof(Character));
}

void Font::replaceColor(uint16 oldColor, uint16 newColor) {
if (!_data || !_dataSize) {
return;
}
for (int i = 0; i < _dataSize; i++) {
if (_data[i] == oldColor) {
_data[i] = newColor;
}
}
}

void Font::drawCharacter(const uint8 character, Graphics::Surface &surface, int x, int y) const {
uint8 characterIndex = character + 1;
if (x < 0 || x >= _screenWidth || y < 0 || y >= _screenHeight || !_data || characterIndex >= _characterCount) {
Expand Down Expand Up @@ -208,8 +195,14 @@ void Font::drawCharacter(const uint8 character, Graphics::Surface &surface, int
int currentX = x;
int endX = width + x - 1;
while (currentX <= endX && currentX < _screenWidth) {
if ((*srcPtr & 0x8000) == 0) {
*dstPtr = *srcPtr;
uint8 a, r, g, b;
gameDataPixelFormat().colorToARGB(*srcPtr, a, r, g, b);
if (!a) {
if (_color == _defaultColor) {
*dstPtr = surface.format.RGBToColor(r, g, b);
} else {
*dstPtr = _color;
}
}
dstPtr++;
srcPtr++;
Expand Down
2 changes: 1 addition & 1 deletion engines/bladerunner/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Font {
int _screenHeight;
int _spacing1;
int _spacing2;
uint16 _defaultColor;
uint16 _color;
int _intersperse;

Expand All @@ -76,7 +77,6 @@ class Font {

private:
void reset();
void replaceColor(uint16 oldColor, uint16 newColor);

void drawCharacter(const uint8 character, Graphics::Surface &surface, int x, int y) const;
};
Expand Down
4 changes: 2 additions & 2 deletions engines/bladerunner/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ bool Image::open(const Common::String &name) {
#endif
}

const Graphics::PixelFormat pixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0);
_surface.init(width, height, 2*width, data, pixelFormat);
_surface.init(width, height, 2*width, data, gameDataPixelFormat());
_surface.convertToInPlace(screenPixelForrmat());

delete[] buf;
delete stream;
Expand Down
12 changes: 6 additions & 6 deletions engines/bladerunner/obstacles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ void Obstacles::draw() {
_polygons[i].vertices[j].y
));

_vm->_surfaceFront.drawLine(p0.x, p0.y, p1.x, p1.y, 0x7FFF);
_vm->_surfaceFront.drawLine(p0.x, p0.y, p1.x, p1.y, _vm->_surfaceFront.format.RGBToColor(255, 255, 255));

p0 = p1;
}
Expand All @@ -964,17 +964,17 @@ void Obstacles::draw() {
Vector3 p2 = _vm->_view->calculateScreenPosition(playerPos + Vector3( 12.0f, 0.0f, 12.0f));
Vector3 p3 = _vm->_view->calculateScreenPosition(playerPos + Vector3(-12.0f, 0.0f, 12.0f));

_vm->_surfaceFront.drawLine(p0.x, p0.y, p1.x, p1.y, 0x7C00);
_vm->_surfaceFront.drawLine(p1.x, p1.y, p2.x, p2.y, 0x7C00);
_vm->_surfaceFront.drawLine(p2.x, p2.y, p3.x, p3.y, 0x7C00);
_vm->_surfaceFront.drawLine(p3.x, p3.y, p0.x, p0.y, 0x7C00);
_vm->_surfaceFront.drawLine(p0.x, p0.y, p1.x, p1.y, _vm->_surfaceFront.format.RGBToColor(255, 0, 0));
_vm->_surfaceFront.drawLine(p1.x, p1.y, p2.x, p2.y, _vm->_surfaceFront.format.RGBToColor(255, 0, 0));
_vm->_surfaceFront.drawLine(p2.x, p2.y, p3.x, p3.y, _vm->_surfaceFront.format.RGBToColor(255, 0, 0));
_vm->_surfaceFront.drawLine(p3.x, p3.y, p0.x, p0.y, _vm->_surfaceFront.format.RGBToColor(255, 0, 0));
}

// draw path along polygons
for (int i = 1; i < _pathSize; ++i) {
Vector3 p0 = _vm->_view->calculateScreenPosition(Vector3(_path[i - 1].x, y, _path[i - 1].y));
Vector3 p1 = _vm->_view->calculateScreenPosition(Vector3(_path[i].x, y, _path[i].y));
_vm->_surfaceFront.drawLine(p0.x, p0.y, p1.x, p1.y, 0x7C00);
_vm->_surfaceFront.drawLine(p0.x, p0.y, p1.x, p1.y, _vm->_surfaceFront.format.RGBToColor(255, 0, 0));
}

// draw "next" vertex
Expand Down
Loading

0 comments on commit e86ee33

Please sign in to comment.