Permalink
Browse files

STARTREK: Implement addSprite and delSprite

  • Loading branch information...
Drenn1 authored and sev- committed Feb 23, 2018
1 parent cd45502 commit 8cd5441959108b890d1433e70376c6cc1dd3bdb5
Showing with 118 additions and 65 deletions.
  1. +73 −62 engines/startrek/graphics.cpp
  2. +11 −2 engines/startrek/graphics.h
  3. +34 −1 engines/startrek/startrek.cpp
@@ -44,6 +44,8 @@ Graphics::Graphics(StarTrekEngine *vm) : _vm(vm), _egaMode(false) {

_backgroundImage = new Bitmap(_vm->openFile("DEMON0.BMP"));
_canvas = new Bitmap(SCREEN_WIDTH, SCREEN_HEIGHT);

_numSprites = 0;
}

Graphics::~Graphics() {
@@ -56,6 +58,48 @@ Graphics::~Graphics() {
delete _canvas;
}


void Graphics::loadEGAData(const char *filename) {
// Load EGA palette data
if (!_egaMode)
return;

if (!_egaData)
_egaData = new byte[256];

Common::SeekableReadStream *egaStream = _vm->openFile(filename);
egaStream->read(_egaData, 256);
delete egaStream;
}

void Graphics::drawBackgroundImage(const char *filename) {
// Draw an stjr BGD image (palette built-in)

Common::SeekableReadStream *imageStream = _vm->openFile(filename);
byte *palette = new byte[256 * 3];
imageStream->read(palette, 256 * 3);

// Expand color components
for (uint16 i = 0; i < 256 * 3; i++)
palette[i] <<= 2;

uint16 xoffset = imageStream->readUint16LE();
uint16 yoffset = imageStream->readUint16LE();
uint16 width = imageStream->readUint16LE();
uint16 height = imageStream->readUint16LE();

byte *pixels = new byte[width * height];
imageStream->read(pixels, width * height);

_vm->_system->getPaletteManager()->setPalette(palette, 0, 256);
_vm->_system->copyRectToScreen(pixels, width, xoffset, yoffset, width, height);
_vm->_system->updateScreen();

delete[] palette;
delete imageStream;
}


void Graphics::loadPalette(const Common::String &paletteName) {
// Set the palette from a PAL file
Common::String palFile = paletteName + ".PAL";
@@ -97,37 +141,7 @@ void Graphics::redrawScreen() {
// TODO: get rid of _canvas for efficiency
memcpy(_canvas->pixels, _backgroundImage->pixels, SCREEN_WIDTH*SCREEN_HEIGHT);

// drawSprite tests

// Draw mode 0
Sprite spr;
memset(&spr,0,sizeof(Sprite));
spr.bitmap = new Bitmap(_vm->openFile("MWALKE00.BMP"));
spr.drawPriority = 1;
spr.drawX = 150;
spr.drawY = 30;
spr.drawMode = 0;
drawSprite(spr);

// Draw mode 2 (translucent background)
memset(&spr,0,sizeof(Sprite));
spr.bitmap = new Bitmap(_vm->openFile("KWALKS00.BMP"));
spr.drawPriority = 1;
spr.drawX = 200;
spr.drawY = 40;
spr.drawMode = 2;
drawSprite(spr);

// Draw mode 3 (text)
memset(&spr,0,sizeof(Sprite));
spr.bitmap = new Bitmap(8*8,8*8);
for (int i=0;i<8*8;i++)
spr.bitmap->pixels[i] = 0x40+i;
spr.drawX = 8*10;
spr.drawY = 50;
spr.textColor = 0xb3;
spr.drawMode = 3;
drawSprite(spr);
drawAllSprites();

drawBitmapToScreen(_canvas);
}
@@ -272,45 +286,42 @@ void Graphics::drawSprite(const Sprite &sprite, const Common::Rect &rect) {
}
}


void Graphics::loadEGAData(const char *filename) {
// Load EGA palette data
if (!_egaMode)
return;

if (!_egaData)
_egaData = new byte[256];

Common::SeekableReadStream *egaStream = _vm->openFile(filename);
egaStream->read(_egaData, 256);
delete egaStream;
void Graphics::drawAllSprites() {
// TODO: implement properly
for (int i=0; i<_numSprites; i++) {
Sprite *spr = _sprites[i];
drawSprite(*spr);
}
}

void Graphics::drawBackgroundImage(const char *filename) {
// Draw an stjr BGD image (palette built-in)
void Graphics::addSprite(Sprite *sprite) {
if (_numSprites >= MAX_SPRITES)
error("addSprite: too many sprites");

Common::SeekableReadStream *imageStream = _vm->openFile(filename);
byte *palette = new byte[256 * 3];
imageStream->read(palette, 256 * 3);
// Initialize some fields
sprite->drawMode = 0;
sprite->field8 = 0;
sprite->field16 = 0;

// Expand color components
for (uint16 i = 0; i < 256 * 3; i++)
palette[i] <<= 2;
sprite->rectangle1.top = -1;
sprite->rectangle1.left = -1;
sprite->rectangle1.bottom = -2;
sprite->rectangle1.right = -2;

uint16 xoffset = imageStream->readUint16LE();
uint16 yoffset = imageStream->readUint16LE();
uint16 width = imageStream->readUint16LE();
uint16 height = imageStream->readUint16LE();
_sprites[_numSprites++] = sprite;
}

byte *pixels = new byte[width * height];
imageStream->read(pixels, width * height);
void Graphics::delSprite(Sprite *sprite) {
for (int i=0; i<_numSprites; i++) {
if (sprite != _sprites[i])
continue;

_vm->_system->getPaletteManager()->setPalette(palette, 0, 256);
_vm->_system->copyRectToScreen(pixels, width, xoffset, yoffset, width, height);
_vm->_system->updateScreen();
_numSprites--;
_sprites[i] = _sprites[_numSprites];
return;
}

delete[] palette;
delete imageStream;
error("delSprite: sprite not in list");
}


@@ -42,6 +42,8 @@ class StarTrekEngine;
const int SCREEN_WIDTH = 320;
const int SCREEN_HEIGHT = 200;

const int MAX_SPRITES = 32;


struct Sprite {
uint16 x,y;
@@ -67,15 +69,19 @@ class Graphics {
Graphics(StarTrekEngine *vm);
~Graphics();

void loadEGAData(const char *egaFile);
void drawBackgroundImage(const char *filename);

void loadPalette(const Common::String &paletteFile);
void loadPri(const char *priFile);

void redrawScreen();
void drawSprite(const Sprite &sprite);
void drawSprite(const Sprite &sprite, const Common::Rect &rect);
void drawAllSprites();

void loadEGAData(const char *egaFile);
void drawBackgroundImage(const char *filename);
void addSprite(Sprite *sprite);
void delSprite(Sprite *sprite);


private:
@@ -93,6 +99,9 @@ class Graphics {
Common::Rect _screenRect;
Bitmap *_backgroundImage;
Bitmap *_canvas;

Sprite *_sprites[MAX_SPRITES];
int _numSprites;
};

}
@@ -103,6 +103,39 @@ Common::Error StarTrekEngine::run() {
} else {
_gfx->drawBackgroundImage("BRIDGE.BGD");
}


// Sprite tests

// Draw mode 0
Sprite *spr = new Sprite;
_gfx->addSprite(spr);
spr->bitmap = new Bitmap(openFile("MWALKE00.BMP"));
spr->drawPriority = 1;
spr->drawX = 150;
spr->drawY = 30;
spr->drawMode = 0;

// Draw mode 2 (translucent background)
spr = new Sprite;
_gfx->addSprite(spr);
spr->bitmap = new Bitmap(openFile("KWALKS00.BMP"));
spr->drawPriority = 1;
spr->drawX = 200;
spr->drawY = 40;
spr->drawMode = 2;

// Draw mode 3 (text)
spr = new Sprite;
_gfx->addSprite(spr);
spr->bitmap = new TextBitmap(8*8,8*8);
for (int i=0;i<8*8;i++)
spr->bitmap->pixels[i] = 0x40+i;
spr->drawX = 8*10;
spr->drawY = 50;
spr->textColor = 0xb3;
spr->drawMode = 3;


Common::Event event;

@@ -116,7 +149,7 @@ Common::Error StarTrekEngine::run() {
break;
}
}
_system->updateScreen();
_gfx->redrawScreen();

_system->delayMillis(1000/60);
}

0 comments on commit 8cd5441

Please sign in to comment.