From bdb09d26fc6c489228cb2c28be75024f9a49eb75 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Thu, 28 Aug 2008 20:29:06 +0000 Subject: Accepted Patch by Scraggy that moves text in such a way that no text overlaps --- src/being.cpp | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) (limited to 'src/being.cpp') diff --git a/src/being.cpp b/src/being.cpp index 1880e7c0..dca87677 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -34,6 +34,7 @@ #include "particle.h" #include "sound.h" #include "localplayer.h" +#include "text.h" #include "resources/resourcemanager.h" #include "resources/imageset.h" @@ -51,6 +52,9 @@ int Being::instances = 0; ImageSet *Being::emotionSet = NULL; +static const int X_SPEECH_OFFSET = 18; +static const int Y_SPEECH_OFFSET = 60; + Being::Being(int id, int job, Map *map): mJob(job), mX(0), mY(0), @@ -83,6 +87,7 @@ Being::Being(int id, int job, Map *map): } instances++; + mSpeech = 0; } Being::~Being() @@ -106,6 +111,8 @@ Being::~Being() emotionSet->decRef(); emotionSet = NULL; } + + delete mSpeech; } void @@ -153,7 +160,12 @@ Being::setSprite(int slot, int id, std::string color) void Being::setSpeech(const std::string &text, Uint32 time) { - mSpeech = text; + // don't introduce a memory leak + delete mSpeech; + + mSpeech = new Text(text, mPx + X_SPEECH_OFFSET, mPy - Y_SPEECH_OFFSET, + gcn::Graphics::CENTER, speechFont, + gcn::Color(255, 255, 255)); mSpeechTime = 500; } @@ -361,13 +373,28 @@ void Being::logic() { // Reduce the time that speech is still displayed - if (mSpeechTime > 0) - mSpeechTime--; + if (mSpeechTime > 0 && mSpeech) + { + if (--mSpeechTime == 0) + { + delete mSpeech; + mSpeech = 0; + } + } + int oldPx = mPx; + int oldPy = mPy; // Update pixel coordinates mPx = mX * 32 + getXOffset(); mPy = mY * 32 + getYOffset(); - + if (mPx != oldPx || mPy != oldPy) + { + if (mSpeech) + { + mSpeech->adviseXY(mPx + X_SPEECH_OFFSET, mPy - Y_SPEECH_OFFSET); + } + updateCoords(); + } if (mEmotion != 0) { mEmotionTime--; @@ -432,21 +459,6 @@ Being::drawEmotion(Graphics *graphics, int offsetX, int offsetY) graphics->drawImage(emotionSet->get(emotionIndex), px, py); } -void -Being::drawSpeech(Graphics *graphics, int offsetX, int offsetY) -{ - int px = mPx + offsetX; - int py = mPy + offsetY; - - // Draw speech above this being - if (mSpeechTime > 0) - { - graphics->setFont(speechFont); - graphics->setColor(gcn::Color(255, 255, 255)); - graphics->drawText(mSpeech, px + 18, py - 60, gcn::Graphics::CENTER); - } -} - Being::Type Being::getType() const { -- cgit v1.2.3-70-g09d2