Esempio n. 1
0
bool Button::IsPixelTransparent(unsigned short x, unsigned short y)
{
	// some buttons have hollow Image frame filled w/ Picture
	// some buttons in BG2 are text only (if BAM == 'GUICTRL')
	Sprite2D* Unpressed = buttonImages[BUTTON_IMAGE_UNPRESSED];
	if (Picture || PictureList.size() || ! Unpressed) return false;
	int xOffs = ( Width / 2 ) - ( Unpressed->Width / 2 );
	int yOffs = ( Height / 2 ) - ( Unpressed->Height / 2 );
	return Unpressed->IsPixelTransparent(x - xOffs, y - yOffs);
}
Esempio n. 2
0
void SpriteSheet2D::UpdateSprite(const String& name, const IntRect& rectangle, const Vector2& hotSpot)
{
    if (!texture_)
        return;

    Sprite2D* sprite = GetSprite(name);
    if (sprite)
    {
        sprite->SetRectangle(rectangle);
        sprite->SetHotSpot(hotSpot);
    }
}
Esempio n. 3
0
void Animation::MirrorAnimation()
{
	Video *video = core->GetVideoDriver();

	for (size_t i = 0; i < indicesCount; i++) {
		Sprite2D * tmp = frames[i];
		frames[i] = video->MirrorSpriteHorizontal( tmp, true );
		tmp->release();
	}

	// flip animArea horizontally as well
	animArea.x = -animArea.w - animArea.x;
}
Esempio n. 4
0
void Animation::MirrorAnimationVert()
{
	Video *video = core->GetVideoDriver();

	for (size_t i = 0; i < indicesCount; i++) {
		Sprite2D * tmp = frames[i];
		frames[i] = video->MirrorSpriteVertical( tmp, true );
		tmp->release();
	}

	// flip animArea vertically as well
//	animArea.y = -animArea.h - animArea.y;
}
Esempio n. 5
0
void Projectile::GetPaletteCopy(Animation *anim[], Palette *&pal)
{
	if (pal)
		return;
	for (unsigned int i=0;i<MAX_ORIENT;i++) {
		if (anim[i]) {
			Sprite2D* spr = anim[i]->GetFrame(0);
			if (spr) {
				pal = spr->GetPalette()->Copy();
				break;
			}
		}
	}
}
void ScriptedAnimation::GetPaletteCopy()
{
	if (palette)
		return;
	//it is not sure that the first position will have a resource in it
	//therefore the cycle
	for (unsigned int i=0;i<3*MAX_ORIENT;i++) {
		if (anims[i]) {
			Sprite2D* spr = anims[i]->GetFrame(0);
			if (spr) {
				palette = spr->GetPalette()->Copy();
				//we need only one palette, so break here
				break;
			}
		}
	}
}
Esempio n. 7
0
TTFFont::TTFFont(Palette* pal, FT_Face face, int lineheight, int baseline)
	: Font(pal, lineheight, baseline), face(face)
{
// on FT < 2.4.2 the manager will defer ownership to this object
#if FREETYPE_VERSION_ATLEAST(2,4,2)
	FT_Reference_Face(face); // retain the face or the font manager will destroy it
#endif
	// ttf fonts dont produce glyphs for whitespace
	Sprite2D* blank = core->GetVideoDriver()->CreateSprite8(0, 0, NULL, NULL);
	// blank for returning when there is an error
	// TODO: ttf fonts have a "box" glyph they use for this
	CreateGlyphForCharSprite(0, blank);
	blank->Width = core->TLKEncoding.zerospace ? 1 : (LineHeight * 0.25);;
	CreateGlyphForCharSprite(' ', blank);
	blank->Width *= 4;
	CreateGlyphForCharSprite('\t', blank);
	blank->release();
}
Esempio n. 8
0
Image* ImageMgr::GetImage()
{
	unsigned int height = GetHeight();
	unsigned int width = GetWidth();
	Image *data = new Image(width, height);

	Sprite2D *spr = GetSprite2D();

	for (unsigned int y = 0; y < height; y++) {
		for (unsigned int x = 0; x < width; x++) {
			data->SetPixel(x,y, spr->GetPixel(x,y));
		}
	}

	core->GetVideoDriver()->FreeSprite(spr);

	return data;
}
Esempio n. 9
0
bool SpriteSheet2D::Save(Serializer& dest) const
{
    if (!texture_)
        return false;

    SharedPtr<XMLFile> xmlFile(new XMLFile(context_));    
    XMLElement rootElem = xmlFile->CreateRoot("spritesheet");
    rootElem.SetAttribute("texture", texture_->GetName());

    for (HashMap<String, SharedPtr<Sprite2D> >::ConstIterator i = spriteMapping_.Begin(); i != spriteMapping_.End(); ++i)
    {
        XMLElement spriteElem = rootElem.CreateChild("sprite");
        spriteElem.SetAttribute("name", i->first_);
        Sprite2D* sprite = i->second_;
        spriteElem.SetIntRect("rectangle", sprite->GetRectangle());
        spriteElem.SetVector2("hotspot", sprite->GetHotSpot());
    }

    return xmlFile->Save(dest);
}
Esempio n. 10
0
Bitmap* ImageMgr::GetBitmap()
{
	unsigned int height = GetHeight();
	unsigned int width = GetWidth();
	Bitmap *data = new Bitmap(width, height);

	Log(ERROR, "ImageMgr", "Don't know how to handle 24bit bitmap from %s...",
		str->filename );

	Sprite2D *spr = GetSprite2D();

	for (unsigned int y = 0; y < height; y++) {
		for (unsigned int x = 0; x < width; x++) {
			data->SetAt(x,y, spr->GetPixel(x,y).r);
		}
	}

	core->GetVideoDriver()->FreeSprite(spr);

	return data;
}
Esempio n. 11
0
Sprite2D* BAMImporter::GetFrameInternal(unsigned short findex, unsigned char mode,
			bool BAMsprite, const unsigned char* data,
			AnimationFactory* datasrc)
{
	Sprite2D* spr = 0;

	if (BAMsprite) {
		bool RLECompressed = (frames[findex].FrameData & 0x80000000) == 0;

		assert(data);
		const unsigned char* framedata = data;
		framedata += (frames[findex].FrameData & 0x7FFFFFFF) - DataStart;
		if (RLECompressed) {
			spr = core->GetVideoDriver()->CreateSpriteBAM8(
				frames[findex].Width, frames[findex].Height,
				true, framedata, datasrc, palette, CompressedColorIndex);
		} else {
			spr = core->GetVideoDriver()->CreateSpriteBAM8(
				frames[findex].Width, frames[findex].Height, false,
				framedata, datasrc, palette, CompressedColorIndex );
		}
	} else {
		void* pixels = GetFramePixels(findex);
		spr = core->GetVideoDriver()->CreateSprite8(
			frames[findex].Width, frames[findex].Height, 8,
			pixels, palette->col, true, 0 );
	}

	spr->XPos = (ieWordSigned)frames[findex].XPos;
	spr->YPos = (ieWordSigned)frames[findex].YPos;
	if (mode == IE_SHADED) {
		// CHECKME: is this ever used? Should we modify the sprite's palette
		// without creating a local copy for this sprite?
		Palette* pal = spr->GetPalette();
		pal->CreateShadedAlphaChannel();
		pal->Release();
	}
	return spr;
}
Esempio n. 12
0
bool Animation2D::Save(Serializer& dest) const
{
    XMLFile xmlFile(context_);
    XMLElement rootElem = xmlFile.CreateRoot("Animation");
    
    float endTime = 0.0f;
    for (unsigned i = 0; i < frameSprites_.Size(); ++i)
    {
        XMLElement frameElem = rootElem.CreateChild("Frame");
        frameElem.SetFloat("duration", frameEndTimes_[i] - endTime);
        endTime = frameEndTimes_[i];

        Sprite2D* sprite = frameSprites_[i];
        SpriteSheet2D* spriteSheet = sprite->GetSpriteSheet();
        if (!spriteSheet)
            frameElem.SetString("sprite", sprite->GetName());
        else
            frameElem.SetString("sprite", spriteSheet->GetName() + "@" + sprite->GetName());
    }

    return xmlFile.Save(dest);
}
Esempio n. 13
0
Font* BAMFontManager::GetFont(unsigned short /*ptSize*/,
							  FontStyle /*style*/, Palette* pal)
{
	AnimationFactory* af = bamImp->GetAnimationFactory(resRef); // released by BAMFont

	Font* fnt = NULL;
	if (isStateFont) {
		// Hack to work around original data where some status icons have inverted x and y positions (ie level up icon)
		// isStateFont is set in Open() and simply compares the first 6 characters of the file with "STATES"

		// since state icons should all be the same size/position we can just take the position of the first one
		Sprite2D* first = af->GetFrame(0, 0);
		int pos = first->YPos; // baseline
		first->release();
		fnt = new BAMFont(af, &pos);
	} else {
		fnt = new BAMFont(af, NULL);
	}

	if (pal) {
		fnt->SetPalette(pal);
	}
	return fnt;
}
Esempio n. 14
0
void AnimatedSprite2D::UpdateSourceBatchesSpriter()
{
    const Matrix3x4& nodeWorldTransform = GetNode()->GetWorldTransform();

    Vector<Vertex2D>& vertices = sourceBatches_[0].vertices_;
    vertices.Clear();

    Rect drawRect;
    Rect textureRect;
    unsigned color = color_.ToUInt();

    Vertex2D vertex0;
    Vertex2D vertex1;
    Vertex2D vertex2;
    Vertex2D vertex3;

    const std::vector<Spriter::SpatialTimelineKey*>& timelineKeys = spriterInstance_->GetTimelineKeys();
    for (size_t i = 0; i < timelineKeys.size(); ++i)
    {
        if (timelineKeys[i]->GetObjectType() != Spriter::SPRITE)
            continue;

        Spriter::SpriteTimelineKey* timelineKey = (Spriter::SpriteTimelineKey*)timelineKeys[i];

        Spriter::SpatialInfo& info = timelineKey->info_;        
        Vector3 position(info.x_, info.y_, 0.0f);
        if (flipX_)
            position.x_ = -position.x_;
        if (flipY_)
            position.y_ = -position.y_;

        float angle = info.angle_;
        if (flipX_ != flipY_)
            angle = -angle;

        Matrix3x4 localTransform(position * PIXEL_SIZE, 
            Quaternion(angle), 
            Vector3(info.scaleX_, info.scaleY_, 1.0f));

        Matrix3x4 worldTransform = nodeWorldTransform * localTransform;
        Sprite2D* sprite = animationSet_->GetSpriterFileSprite(timelineKey->folderId_, timelineKey->fileId_);
        if (!sprite)
            return;

        if (timelineKey->useDefaultPivot_)
            sprite->GetDrawRectangle(drawRect, flipX_, flipY_);
        else
            sprite->GetDrawRectangle(drawRect, Vector2(timelineKey->pivotX_, timelineKey->pivotY_), flipX_, flipY_);

        if (!sprite->GetTextureRectangle(textureRect, flipX_, flipY_))
            return;

        vertex0.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.min_.y_, 0.0f);
        vertex1.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.max_.y_, 0.0f);
        vertex2.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.max_.y_, 0.0f);
        vertex3.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.min_.y_, 0.0f);

        vertex0.uv_ = textureRect.min_;
        vertex1.uv_ = Vector2(textureRect.min_.x_, textureRect.max_.y_);
        vertex2.uv_ = textureRect.max_;
        vertex3.uv_ = Vector2(textureRect.max_.x_, textureRect.min_.y_);

        vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color;

        vertices.Push(vertex0);
        vertices.Push(vertex1);
        vertices.Push(vertex2);
        vertices.Push(vertex3);
    }

    worldBoundingBoxDirty_ = true;
}
Esempio n. 15
0
const Glyph& TTFFont::GetGlyph(ieWord chr) const
{
#if HAVE_ICONV
	if (!core->TLKEncoding.multibyte) {
		char* oldchar = (char*)&chr;
		ieWord unicodeChr = 0;
		char* newchar = (char*)&unicodeChr;
		size_t in = (core->TLKEncoding.widechar) ? 2 : 1, out = 2;

		// TODO: make this work on BE systems
		// TODO: maybe we want to work with non-unicode fonts?
		iconv_t cd = iconv_open("UTF-16LE", core->TLKEncoding.encoding.c_str());
	#if __FreeBSD__
		size_t ret = iconv(cd, (const char **)&oldchar, &in, &newchar, &out);
	#else
		size_t ret = iconv(cd, &oldchar, &in, &newchar, &out);
	#endif
		if (ret != GEM_OK) {
			Log(ERROR, "FONT", "iconv error: %d", errno);
		}
		iconv_close(cd);
		chr = unicodeChr;
	}
#endif
	// first check if the glyph already exists
	const Glyph& g = Font::GetGlyph(chr);
	if (g.pixels) {
		return g;
	}

	// attempt to generate glyph

	// TODO: fix the font styles!
	/*
	// currently gemrb has exclusive styles...
	// TODO: make styles ORable
	style = NORMAL;
	if ( face->style_flags & FT_STYLE_FLAG_ITALIC ) {
		style = ITALIC;
	}
	if ( face->style_flags & FT_STYLE_FLAG_BOLD ) {
		// bold overrides italic
		// TODO: allow bold and italic together
		style = BOLD;
	}

	glyph_overhang = face->size->metrics.y_ppem / 10;
	// x offset = cos(((90.0-12)/360)*2*M_PI), or 12 degree angle
	glyph_italics = 0.207f;
	glyph_italics *= height;
	*/
	FT_Error error = 0;
	FT_UInt index = FT_Get_Char_Index(face, chr);
	if (!index) {
		return AliasBlank(chr);
	}

	error = FT_Load_Glyph( face, index, FT_LOAD_DEFAULT | FT_LOAD_TARGET_MONO);
	if( error ) {
		LogFTError(error);
		return AliasBlank(chr);
	}

	FT_GlyphSlot glyph = face->glyph;
	FT_Glyph_Metrics* metrics = &glyph->metrics;
	/*
	//int maxx, yoffset;
	if ( FT_IS_SCALABLE( face ) ) {
		// Get the bounding box
		maxx = FT_FLOOR(metrics->horiBearingX) + FT_CEIL(metrics->width);
		yoffset = ascent - FT_FLOOR(metrics->horiBearingY);
	} else {
		// Get the bounding box for non-scalable format.
		// Again, freetype2 fills in many of the font metrics
		// with the value of 0, so some of the values we
		// need must be calculated differently with certain
		// assumptions about non-scalable formats.

		maxx = FT_FLOOR(metrics->horiBearingX) + FT_CEIL(metrics->horiAdvance);
		yoffset = 0;
	}

	// TODO: handle styles for fonts that dont do it themselves

	 FIXME: maxx is currently unused.
	 glyph spacing is non existant right now
	 font styles are non functional too
	 */

	FT_Bitmap* bitmap;
	uint8_t* pixels = NULL;

	/* Render the glyph */
	error = FT_Render_Glyph( glyph, ft_render_mode_normal );
	if( error ) {
		LogFTError(error);
		return AliasBlank(chr);
	}

	bitmap = &glyph->bitmap;

	Size sprSize(bitmap->width, bitmap->rows);

	/* Ensure the width of the pixmap is correct. On some cases,
	 * freetype may report a larger pixmap than possible.*/
	/*
	if (sprSize.w > maxx) {
		sprSize.w = maxx;
	}*/

	if (sprSize.IsEmpty()) {
		return AliasBlank(chr);
	}

	// we need 1px empty space on each side
	sprSize.w += 2;

	pixels = (uint8_t*)malloc(sprSize.w * sprSize.h);
	uint8_t* dest = pixels;
	uint8_t* src = bitmap->buffer;

	for( int row = 0; row < sprSize.h; row++ ) {
		// TODO: handle italics. we will need to offset the row by font->glyph_italics * row i think.

		// add 1px left padding
		memset(dest++, 0, 1);
		// -2 to account for padding
		memcpy(dest, src, sprSize.w - 2);
		dest += sprSize.w - 2;
		src += bitmap->pitch;
		// add 1px right padding
		memset(dest++, 0, 1);
	}
	// assert that we fill the buffer exactly
	assert((dest - pixels) == (sprSize.w * sprSize.h));

	// TODO: do an underline if requested

	Sprite2D* spr = core->GetVideoDriver()->CreateSprite8(sprSize.w, sprSize.h, pixels, NULL, true, 0);
	spr->YPos = FT_FLOOR(metrics->horiBearingY);
	// FIXME: casting away const
	const Glyph& ret = ((TTFFont*)this)->CreateGlyphForCharSprite(chr, spr);
	spr->release();
	return ret;
}
Esempio n. 16
0
/** Draws the Control on the Output Display */
void WorldMapControl::Draw(unsigned short XWin, unsigned short YWin)
{
	WorldMap* worldmap = core->GetWorldMap();
	if (!Width || !Height) {
		return;
	}
	if(!Changed)
		return;
	Changed = false;
	Video* video = core->GetVideoDriver();
	Region r( XWin+XPos, YWin+YPos, Width, Height );
	Region clipbackup;
	video->GetClipRect(clipbackup);
	video->SetClipRect(&r);
	video->BlitSprite( worldmap->GetMapMOS(), MAP_TO_SCREENX(0), MAP_TO_SCREENY(0), true, &r );

	unsigned int i;
	unsigned int ec = worldmap->GetEntryCount();
	for(i=0;i<ec;i++) {
		WMPAreaEntry *m = worldmap->GetEntry(i);
		if (! (m->GetAreaStatus() & WMP_ENTRY_VISIBLE)) continue;

		int xOffs = MAP_TO_SCREENX(m->X);
		int yOffs = MAP_TO_SCREENY(m->Y);
		Sprite2D* icon = m->GetMapIcon(worldmap->bam);
		if( icon ) {
			if (m == Area) {
				Palette *pal = icon->GetPalette();
				icon->SetPalette(pal_selected);
				video->BlitSprite( icon, xOffs, yOffs, true, &r );
				icon->SetPalette(pal);
				pal->Release();
			} else {
				video->BlitSprite( icon, xOffs, yOffs, true, &r );
			}
			video->FreeSprite( icon );
		}

		if (AnimPicture && !strnicmp(m->AreaResRef, currentArea, 8) ) {
			video->BlitSprite( AnimPicture, xOffs, yOffs, true, &r );
		}
	}

	// Draw WMP entry labels
	if (ftext==NULL) {
		video->SetClipRect(&clipbackup);
		return;
	}
	for(i=0;i<ec;i++) {
		WMPAreaEntry *m = worldmap->GetEntry(i);
		if (! (m->GetAreaStatus() & WMP_ENTRY_VISIBLE)) continue;
		Sprite2D *icon=m->GetMapIcon(worldmap->bam);
		int h=0,w=0,xpos=0,ypos=0;
		if (icon) {
			h=icon->Height;
			w=icon->Width;
			xpos=icon->XPos;
			ypos=icon->YPos;
			video->FreeSprite( icon );
		}

		Region r2 = Region( MAP_TO_SCREENX(m->X-xpos), MAP_TO_SCREENY(m->Y-ypos), w, h );
		if (!m->GetCaption())
			continue;

		int tw = ftext->CalcStringWidth( (unsigned char*)m->GetCaption() ) + 5;
		int th = ftext->maxHeight;
		
		Palette* text_pal = pal_normal;
		
		if (Area == m) {
			text_pal = pal_selected;
		} else {
			if (! (m->GetAreaStatus() & WMP_ENTRY_VISITED)) {
				text_pal = pal_notvisited;
			}
		}

		ftext->Print( Region( r2.x + (r2.w - tw)/2, r2.y + r2.h, tw, th ),
				( const unsigned char * ) m->GetCaption(), text_pal, 0, true );
	}
	video->SetClipRect(&clipbackup);
}