Skip to content

Commit

Permalink
Support for adding captions to the time line.
Browse files Browse the repository at this point in the history
  • Loading branch information
acaudwell committed Oct 21, 2010
1 parent 7046c49 commit 9ca6049
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Expand Up @@ -7,6 +7,7 @@ gource_SOURCES = \
src/apache.cpp src/apache.h \
src/bzr.cpp src/bzr.h \
src/commitlog.cpp src/commitlog.h \
src/caption.cpp src/caption.h \
src/core/bounds.h \
src/core/camera.cpp src/core/camera.h \
src/core/conffile.cpp src/core/conffile.h \
Expand Down
50 changes: 50 additions & 0 deletions src/caption.cpp
@@ -0,0 +1,50 @@
/*
Copyright (C) 2010 Andrew Caudwell (acaudwell@gmail.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "caption.h"

RCaption::RCaption(const std::string& caption, time_t timestamp, const FXFont& font) {
this->caption = caption;
this->timestamp = timestamp;
this->font = font;

alpha = 1.0f;
colour = gGourceSettings.caption_colour;
decay = 1.0f / gGourceSettings.caption_duration;
}

const vec2f& RCaption::getPos() {
return pos;
}

void RCaption::setPos(const vec2f& pos) {
this->pos = pos;
}

bool RCaption::isFinished() {
return alpha <= 0.0f;
}

void RCaption::logic(float dt) {
alpha = std::max(0.0f, alpha - dt * decay);
}

void RCaption::draw() const {
glColor4f(colour.x, colour.y, colour.z, 1.0f - fabs(alpha - 0.5) * 2.0f);

font.draw(pos.x, pos.y, caption);
}
52 changes: 52 additions & 0 deletions src/caption.h
@@ -0,0 +1,52 @@
/*
Copyright (C) 2010 Andrew Caudwell (acaudwell@gmail.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef RCAPTION_H
#define RCAPTION_H

#include "core/display.h"
#include "core/fxfont.h"
#include "gource_settings.h"

class RCaption {
float alpha;
float decay;

vec2f pos;

vec3f colour;

FXFont font;
FXFont font2;

public:
std::string caption;
time_t timestamp;

RCaption(const std::string& caption, time_t timestamp, const FXFont& font);

void setPos(const vec2f& pos);
const vec2f& getPos();

bool isFinished();

void logic(float dt);

void draw() const;
};

#endif
2 changes: 1 addition & 1 deletion src/core
Submodule core updated 2 files
+14 −7 conffile.cpp
+2 −0 conffile.h
114 changes: 113 additions & 1 deletion src/gource.cpp
Expand Up @@ -41,6 +41,11 @@ Gource::Gource(FrameExporter* exporter) {
fontmedium.dropShadow(true);
fontmedium.roundCoordinates(false);

fontcaption = fontmanager.grab("FreeSans.ttf", gGourceSettings.caption_size);
fontcaption.dropShadow(true);
fontcaption.roundCoordinates(false);
fontcaption.alignTop(false);

font = fontmanager.grab("FreeSans.ttf", 14);
font.dropShadow(true);
font.roundCoordinates(true);
Expand Down Expand Up @@ -813,9 +818,19 @@ void Gource::reset() {
delete it->second;
}

last_percent = 0.0;
for(std::list<RCaption*>::iterator it = captions.begin(); it!=captions.end();it++) {
delete (*it);
}

for(std::list<RCaption*>::iterator it = active_captions.begin(); it!=active_captions.end();it++) {
delete (*it);
}

files.clear();
captions.clear();
active_captions.clear();

last_percent = 0.0;

idle_time=0;
currtime=0;
Expand Down Expand Up @@ -890,6 +905,45 @@ void Gource::seekTo(float percent) {
commitlog->seekTo(percent);
}

Regex caption_regex("^([0-9]+)\\|(.+)$");

void Gource::loadCaptions() {
if(!gGourceSettings.caption_file.size()) return;

std::ifstream cf(gGourceSettings.caption_file.c_str());

if(!cf.is_open()) return;

std::string line;
std::vector<std::string> matches;

time_t last_timestamp = 0;

while(std::getline(cf, line)) {

ConfFile::trim(line);

if(!caption_regex.match(line, &matches)) continue;

time_t timestamp = atol(matches[0].c_str());

//ignore older captions
if(timestamp<currtime) continue;

//ignore out of order captions
if(timestamp<last_timestamp) continue;

last_timestamp = timestamp;

//fprintf(stderr, "%d %s\n", timestamp, matches[1].c_str());

captions.push_back(new RCaption(matches[1], timestamp, fontcaption));
}

//fprintf(stderr, "loaded %d captions\n", captions.size());

}

void Gource::readLog() {
if(stop_position_reached) return;

Expand Down Expand Up @@ -1384,6 +1438,8 @@ void Gource::logic(float t, float dt) {
if(currtime==0 && commitqueue.size()) {
currtime = lasttime = commitqueue[0].timestamp;
subseconds = 0.0;

loadCaptions();
}

//set current time
Expand Down Expand Up @@ -1428,6 +1484,56 @@ void Gource::logic(float t, float dt) {
commitqueue.pop_front();
}

while(captions.size() > 0) {
RCaption* caption = captions.front();

if(caption->timestamp > currtime) break;

float font_height = fontcaption.getHeight();

//determine next free height
float y = display.height - 20.0f;
float cap_height = font_height * 1.3f;

while(1) {

bool found = false;

for(std::list<RCaption*>::iterator it = active_captions.begin(); it!=active_captions.end();it++) {
RCaption* actcap = *it;
vec2f cappos = actcap->getPos();

if(cappos.y == y) {
found = true;
break;
}
}

if(!found) break;

y -= cap_height;
}

caption->setPos(vec2f(20.0f, y));

captions.pop_front();
active_captions.push_back(caption);
}

for(std::list<RCaption*>::iterator it = active_captions.begin(); it!=active_captions.end();) {
RCaption* caption = *it;

caption->logic(dt);

if(caption->isFinished()) {
it = active_captions.erase(it);
delete caption;
continue;
}

it++;
}

//reset loop counters
gGourceUserInnerLoops = 0;
gGourceDirNodeInnerLoops = 0;
Expand Down Expand Up @@ -1952,6 +2058,12 @@ void Gource::draw(float t, float dt) {
fontmedium.alignTop(true);
}

for(std::list<RCaption*>::iterator it = active_captions.begin(); it!=active_captions.end(); it++) {
RCaption* caption = *it;

caption->draw();
}

if(message_timer>0.0f) {
fontmedium.draw(1, 3, message);
}
Expand Down
9 changes: 8 additions & 1 deletion src/gource.h
Expand Up @@ -23,6 +23,7 @@
#endif

#include <deque>
#include <list>
#include <fstream>

#include "core/display.h"
Expand All @@ -48,6 +49,7 @@
#include "slider.h"

#include "action.h"
#include "caption.h"
#include "file.h"
#include "user.h"
#include "dirnode.h"
Expand Down Expand Up @@ -107,7 +109,7 @@ class Gource : public SDLApp {
TextureResource* logotex;
TextureResource* backgroundtex;

FXFont font, fontlarge, fontmedium;
FXFont font, fontlarge, fontmedium, fontcaption;

bool first_read;
bool draw_loading;
Expand Down Expand Up @@ -145,6 +147,9 @@ class Gource : public SDLApp {
std::map<std::string, RFile*> files;
std::map<int, RFile*> tagfilemap;
std::map<int, RUser*> tagusermap;

std::list<RCaption*> captions;
std::list<RCaption*> active_captions;

QuadTree* dirNodeTree;
QuadTree* userTree;
Expand All @@ -164,6 +169,8 @@ class Gource : public SDLApp {
void selectFile(RFile* file);
void selectNextUser();

void loadCaptions();

void readLog();
void processCommit(RCommit& commit, float t);

Expand Down

0 comments on commit 9ca6049

Please sign in to comment.