Skip to content

Commit

Permalink
Merge branch 'features/broker' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Henning Hasemann committed Mar 21, 2012
2 parents b2cae2e + 8e47c95 commit 87e5eb4
Show file tree
Hide file tree
Showing 21 changed files with 512 additions and 26 deletions.
44 changes: 38 additions & 6 deletions demo/init.lua
Expand Up @@ -23,13 +23,25 @@ function initChapter(n)
scarecrow:setPosition(VP(6000, 2700))
s:addActor(scarecrow)

local pumpkin = Actor("Pumpkin")
pumpkin:addAnimation("default", Image("/media/$res/pumpkin.png"))
pumpkin:setPosition(VP(5950, 2700))
pumpkin:setYOffset(-700)
s:addActor(pumpkin)
local pumpkin1 = Actor("Pumpkin One")
pumpkin1:addAnimation("default", Image("/media/$res/pumpkin.png"))
pumpkin1:setPosition(VP(5950, 2700))
pumpkin1:setYOffset(-700)
s:addActor(pumpkin1)

local pumpkin2 = Actor("Pumpkin Two")
pumpkin2:addAnimation("default", Image("/media/$res/pumpkin.png"))
pumpkin2:setPosition(VP(6200, 3500))
pumpkin2:setYOffset(-700)
s:addActor(pumpkin2)

local pumpkin3 = Actor("Pumpkin Three")
pumpkin3:addAnimation("default", Image("/media/$res/pumpkin.png"))
pumpkin3:setPosition(VP(6700, 3500))
pumpkin3:setYOffset(-700)
s:addActor(pumpkin3)

main_character.mc:setPosition(VP(250, 2700))
main_character.mc:setPosition(VP(5000, 2700))
s:addActor(mc)

local Audio = Audio()
Expand All @@ -46,6 +58,26 @@ function initChapter(n)
WaitTask(3000):start()
print("--- After nonblocking timer")
------------------------------

GAME:enableUserControl(false);

mc:say("Wooo, dark", 4000)
mc:say("I wonder if Deckard came this way?", 6000)

WaitTask(9000):block()

pumpkin1:say("GROG!", 3000 );
WaitTask(400):block()
pumpkin2:say("GROG!", 3000 );
WaitTask(400):block()
pumpkin3:say("GROG!", 3000 );
WaitTask(400):block()

WaitTask(3200):block()

mc:say("OMG! WTF?", 5000);

GAME:enableUserControl(true);

end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/CMakeLists.txt
Expand Up @@ -8,6 +8,9 @@ set(GRAIL_FILES
audio.cc
blit_cached.cc
debug.cc
dialog_line.cc
dialog_frontend.cc
dialog_frontend_subtitle.cc
event.cc
font.cc
game.cc
Expand All @@ -22,6 +25,7 @@ set(GRAIL_FILES
sound_task.cc
shortcuts.cc
sprite.cc
subtitle.cc
surface.cc
task.cc
text.cc
Expand Down
35 changes: 34 additions & 1 deletion lib/actor.cc
Expand Up @@ -12,7 +12,7 @@ using std::copy;
namespace grail {

Actor::Actor(std::string name) :
mode("default"), name(name), yOffset(0), alignmentX(0.5), alignmentY(1.0), speed(1000.0) {
mode("default"), name(name), yOffset(0), alignmentX(0.5), alignmentY(1.0), speed(1000.0), dialogGapTime(300) {
}

std::string Actor::getName() const {
Expand Down Expand Up @@ -93,6 +93,20 @@ void Actor::eachFrame(uint32_t ticks) {
if(animation) {
animation->eachFrame(ticks);
}

if (isSpeaking()) {
// start the next dialog in the queue
if (!getDialogLine()->isStarted()) {
getDialogLine()->start();
}

getDialogLine()->eachFrame();

// remove if finished
if (getDialogLine()->isComplete()) {
dialogLines.pop();
}
}

if(!walkPath.empty()) {
setMode("walk");
Expand Down Expand Up @@ -126,6 +140,25 @@ void Actor::eachFrame(uint32_t ticks) {
} // if walkPath not empty
}

void Actor::say(std::string statement, uint32_t displayTime) {
boost::shared_ptr<DialogLine> line(new DialogLine(shared_from_this(),statement,displayTime));
dialogLines.push(line);

// add a pause after the dialog
boost::shared_ptr<DialogLine> gap(new DialogLine(shared_from_this(),"", dialogGapTime));
dialogLines.push(gap);

Game::getInstance().getDialogFrontend()->say(line);
}

bool Actor::isSpeaking() {
return !(dialogLines.empty());
}

DialogLine::Ptr Actor::getDialogLine() {
return dialogLines.front();
}

std::ostream& operator<<(std::ostream& os, const Actor& actor) {
os << actor.getName();
return os;
Expand Down
14 changes: 13 additions & 1 deletion lib/actor.h
Expand Up @@ -7,23 +7,27 @@
#include <string>
#include <iostream>
#include <list>
#include <queue>

#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

#include "classes.h"
#include "vector2d.h"
#include "animation.h"
#include "area.h"
#include "debug.h"

class dialogLine;

namespace grail {

typedef std::list<VirtualPosition> Path;

/**
* Anything in the game that can walk around (e.g. people)
*/
class Actor : public Area {
class Actor : public Area, public boost::enable_shared_from_this<Actor> {
Animation::Ptr animation;
std::map<std::string, Animation::Ptr> animationModes;
std::string mode;
Expand All @@ -36,6 +40,10 @@ namespace grail {
double speed; ///< Unit is virtual pixels / second

Path walkPath;

// speaking
std::queue<boost::shared_ptr<DialogLine> > dialogLines;
uint32_t dialogGapTime; // the length of time between each line of dialog

protected:
VirtualPosition getUpperLeftCorner() const;
Expand Down Expand Up @@ -86,6 +94,10 @@ namespace grail {
void walkTo(Actor::Ptr actor);
void walkTo(VirtualPosition p);
void walkStraight(VirtualPosition p);

void say(std::string, uint32_t);
bool isSpeaking();
boost::shared_ptr<DialogLine> getDialogLine();
};

std::ostream& operator<<(std::ostream& os, const Actor& actor);
Expand Down
3 changes: 3 additions & 0 deletions lib/classes.h
Expand Up @@ -15,6 +15,9 @@ namespace grail {
class BlitCached;
class Box;
class Button;
class DialogLine;
class DialogFrontend;
class DialogFrontendText;
class DirectionAnimation;
class DirectoryIterator;
class DirectoryIteratorImpl;
Expand Down
10 changes: 10 additions & 0 deletions lib/dialog_frontend.cc
@@ -0,0 +1,10 @@
// vim: set noexpandtab:

#include "dialog_frontend.h"

namespace grail {

// void DialogFrontend::say(DialogLine::Ptr line) {
// }
}

38 changes: 38 additions & 0 deletions lib/dialog_frontend.h
@@ -0,0 +1,38 @@
// vim: set noexpandtab:

#ifndef DIALOG_FRONTEND_H
#define DIALOG_FRONTEND_H

#include <boost/shared_ptr.hpp>
#include <map>

#include "dialog_line.h"
#include "actor.h"
#include "text.h"

namespace grail {

// Virtual base class for in-game dialog renderers.
// Intended to be pluggable so a developer may customise how dialog
// is outputted to the screen.
// For example via subtitles or text floating above an actor's head.
class DialogFrontend {

public:
typedef boost::shared_ptr<DialogFrontend> Ptr;

virtual void say(DialogLine::Ptr) = 0;

virtual void eachFrame(uint32_t ticks) = 0;
virtual void renderAt(SDL_Surface* target, uint32_t ticks, VirtualPosition p) = 0;

protected:
// map of actors to the text objects used to render
// their dialog
std::map<Actor::Ptr, Text::Ptr> lines;
};

} // namespace grail

#endif // DIALOG_FRONTEND_H

105 changes: 105 additions & 0 deletions lib/dialog_frontend_subtitle.cc
@@ -0,0 +1,105 @@
// vim: set noexpandtab:

#include "dialog_frontend_subtitle.h"

namespace grail {

DialogFrontendSubtitle::DialogFrontendSubtitle() {

// sdefault subs position
int defaultX = 2000;
int defaultY = 2800;
subtitlePosition = VirtualPosition(defaultX, defaultY);


//center the subs by default
centered = true;

// show speaker's name by default
showSpeakersName = true;

// default font to be used
setFont("fonts/crkdwno1.ttf");
}

void DialogFrontendSubtitle::say(DialogLine::Ptr line) {

// set the default font here if not set
// ** for some reason doing this in the constructor causes a segfault
// and I can't figure out why -- feedelli **
if (!defaultFont) {
defaultFont = boost::shared_ptr<Font>(new Font(defaultFontPath, 30, 1));
}

std::string subtitleText = line->getText();

// prepend the speakers name and a colon if showSpeakersName is set
if (showSpeakersName) {
subtitleText = line->getSpeaker()->getName() + ": " + subtitleText;
line->setText(subtitleText);
}

// queue a new subtitle with the line to be said and using the default font for now
// ** to be implemented: allow characters to have their own fonts
boost::shared_ptr<Subtitle> s(new Subtitle(line, defaultFont));
subtitles.push_back(s);
}

void DialogFrontendSubtitle::eachFrame(uint32_t ticks) {

// remove completed subs
for (std::vector<boost::shared_ptr<Subtitle> >::iterator iter = subtitles.begin();
iter != subtitles.end(); ) {
if ((*iter)->isComplete()) {
iter = subtitles.erase(iter);
} else {
++iter;
}
}

// update the subtitles
for (std::vector<boost::shared_ptr<Subtitle> >::iterator iter = subtitles.begin();
iter != subtitles.end(); iter++) {
(*iter)->eachFrame(ticks);
}
}

void DialogFrontendSubtitle::renderAt(SDL_Surface* target, uint32_t ticks, VirtualPosition p) {

// count active subs
int activeSubtitles = 0;

// render the subtitles centered
// render backwards so earlier subtitles stack upwards
for (std::vector<boost::shared_ptr<Subtitle> >::reverse_iterator riter = subtitles.rbegin();
riter != subtitles.rend(); ++riter) {

if ((*riter)->isStarted()) {

activeSubtitles++;

VirtualPosition renderPosition = subtitlePosition;

// center the text around the rener position if set to centered
if (centered) {
renderPosition = renderPosition - ((*riter)->getSize()/2);
}

if (activeSubtitles > 1) {
renderPosition.setY(renderPosition.getY() - (200 * (activeSubtitles - 1)));
}

(*riter)->renderAt(target, ticks, (renderPosition));
}
}
}

void DialogFrontendSubtitle::setFont(std::string path) {
defaultFontPath = path;
}

void DialogFrontendSubtitle::setCentered(bool c) {
centered = c;
}
} // namespace grail

39 changes: 39 additions & 0 deletions lib/dialog_frontend_subtitle.h
@@ -0,0 +1,39 @@
// vim: set noexpandtab:

#ifndef DIALOG_FRONTEND_SUBTITLE_H
#define DIALOG_FRONTEND_SUBTITLE_H

#include "dialog_frontend.h"
#include "subtitle.h"
#include "font.h"
#include "boost/shared_ptr.hpp"

namespace grail {

// Displays actors' lines in subtitles on the screen
class DialogFrontendSubtitle : public DialogFrontend {

protected:
std::string defaultFontPath;
Font::Ptr defaultFont;
VirtualPosition subtitlePosition;
bool centered; //is the text centered around subtitleposition
bool showSpeakersName;

std::vector<boost::shared_ptr<Subtitle> > subtitles;

public:
DialogFrontendSubtitle();
void say(DialogLine::Ptr);

void eachFrame(uint32_t ticks);
virtual void renderAt(SDL_Surface* target, uint32_t ticks, VirtualPosition p);

void setFont(std::string);
void setCentered(bool); //set whether text should be centered or not
};

} // namespace grail

#endif // DIALOG_FRONTEND_SUBTITLE_H

0 comments on commit 87e5eb4

Please sign in to comment.