Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed scrolling text. Added animated talkie character, however it's a…

… rip from a game so I need to draw something original to add
  • Loading branch information...
commit 50d07072bc0c8c6e04cbcea26ade6d46fe2f1379 1 parent a6de649
@Rejcx authored
View
81 Application.cpp
@@ -7,6 +7,7 @@ Application::Application()
m_screenWidth = 800;
m_screenHeight = 600;
m_quit = false;
+ m_pause = false;
// Setup SFML stuff
m_app.Create( sf::VideoMode( m_screenWidth, m_screenHeight, 32 ), "Read to Me" );
@@ -15,7 +16,11 @@ Application::Application()
// Setup background
m_background = sf::Shape::Rectangle( 0, 0, m_screenWidth, m_screenHeight, sf::Color( 76, 169, 255 ) );
- m_textOutputWidth = m_screenWidth / 2;
+ m_textOutputWidth = m_screenWidth - 200;
+
+ m_talker.LoadFromFile( "character.png" );
+ m_talker.SetSmooth( false );
+ m_characterFrame = 0;
}
Application::~Application()
@@ -23,7 +28,7 @@ Application::~Application()
m_app.Close();
}
-void Application::CheckEvents()
+void Application::CheckEvents( Reader& reader )
{
sf::Event event;
while ( m_app.GetEvent( event ) )
@@ -32,6 +37,24 @@ void Application::CheckEvents()
{
m_quit = true;
}
+
+ if ( event.Type == sf::Event::KeyPressed )
+ {
+ if ( event.Key.Code == sf::Key::Equal )
+ {
+ reader.GoToNextLine();
+ }
+ if ( event.Key.Code == sf::Key::P )
+ {
+ m_pause = true;
+ cout << "\tPAUSED" << endl;
+ }
+ else if ( event.Key.Code == sf::Key::U )
+ {
+ m_pause = false;
+ cout << "\tUNPAUSED" << endl;
+ }
+ }
}
}
@@ -47,19 +70,42 @@ void Application::DrawStatus( const Reader& reader, const Book& book )
{
}
-void Application::DrawText( const Reader& reader, const Book& book )
+int Application::GetMaxLines()
+{
+ int maxLines = m_screenHeight / m_fontSize;
+ // Each line of text gets about 5 - 6 lines on the screen...
+ maxLines = maxLines / 6;
+ cout << "Approx max lines: " << maxLines << endl;
+ return maxLines;
+}
+
+void Application::DrawText( const Reader& reader, Book& book )
{
sf::Shape bg;
bg = sf::Shape::Rectangle( 5, 5, m_textOutputWidth, m_screenHeight-5, sf::Color( 182, 220, 255 ) );
m_app.Draw( bg );
- int yPos = m_fontSize;
+ int yPos = 0;
int xPos = 10;
- for ( int i = 0; i <= reader.GetCurrentLine(); i++ )
+
+ vector<string> lstLines = book.GetReadLines();
+
+ for ( int i = 0; i < lstLines.size(); i++ )
{
- string currentLine = book.GetLine( i );
+ yPos += m_fontSize;
+ cout << i << ", " << lstLines[i] << endl;
+ string currentLine = lstLines[i];
int beginIndex = 0;
int endIndex = 0;
+ int pulledLines = 1;
+
+ if ( i == reader.GetCurrentLine() )
+ {
+ cout << endl << endl;
+ cout << "-------------------------------------------" << endl;
+ cout << endl << "WRITE LINE" << endl << currentLine << endl;
+ cout << endl << "SPLIT UP" << endl;
+ }
// Account for word-wrapping
while ( currentLine.length() * (m_fontSize/2) > m_textOutputWidth )
@@ -76,9 +122,15 @@ void Application::DrawText( const Reader& reader, const Book& book )
textLine.SetPosition( xPos, yPos );
textLine.SetColor( sf::Color( 0, 0, 0 ) );
+ if ( i == reader.GetCurrentLine() )
+ {
+ cout << pulledLines << ": " << currentLine.substr( beginIndex, endIndex ) << endl;
+ }
+
m_app.Draw( textLine );
yPos += m_fontSize;
currentLine.erase( beginIndex, endIndex );
+ pulledLines++;
}
// Output remaining line
@@ -86,9 +138,26 @@ void Application::DrawText( const Reader& reader, const Book& book )
textLine.SetPosition( xPos, yPos );
textLine.SetColor( sf::Color( 0, 0, 0 ) );
+ if ( i == reader.GetCurrentLine() )
+ {
+ cout << pulledLines << ": " << currentLine << endl;
+ }
+
m_app.Draw( textLine );
yPos += m_fontSize;
}
+
+ sf::Sprite character;
+ character.SetImage( m_talker );
+ character.SetSubRect( sf::IntRect( m_characterFrame * 112, 0, m_characterFrame * 112 + 112, 192 ) );
+ character.SetPosition( 600, m_screenHeight - (m_talker.GetHeight()*2) );
+ character.SetScale( 2, 2 );
+ m_app.Draw( character );
+
+ if ( ++m_characterFrame > 2 )
+ {
+ m_characterFrame = 0;
+ }
}
void Application::EndDraw()
View
9 Application.h
@@ -12,11 +12,13 @@ class Application
Application();
~Application();
bool Running() { return !m_quit; }
- void CheckEvents();
+ bool Paused() { return m_pause; }
+ void CheckEvents( Reader& reader );
void BeginDraw();
void DrawStatus( const Reader& reader, const Book& book );
- void DrawText( const Reader& reader, const Book& book );
+ void DrawText( const Reader& reader, Book& book );
void EndDraw();
+ int GetMaxLines();
private:
sf::RenderWindow m_app;
@@ -24,9 +26,12 @@ class Application
int m_fontSize;
int m_screenWidth, m_screenHeight;
int m_textOutputWidth;
+ int m_characterFrame;
bool m_quit;
+ bool m_pause;
sf::Shape m_background;
+ sf::Image m_talker;
};
#endif
View
21 Book.cpp
@@ -40,3 +40,24 @@ void Book::LoadFile( const string& filename )
}
}
}
+
+string Book::GetLine( int line )
+{
+ return m_lstReadLines[ line ];
+}
+
+void Book::Update( int currentLine, int maxLines )
+{
+ string lineText = m_lstLines[currentLine];
+ m_lstReadLines.push_back( lineText );
+
+ cout << "Push line " << endl << lineText << endl << " to the read list" << endl;
+
+ cout << endl;
+ cout << "LINE COUNT IN LIST " << m_lstReadLines.size() << endl;
+
+ if ( m_lstReadLines.size() > maxLines )
+ {
+ m_lstReadLines.erase( m_lstReadLines.begin() );
+ }
+}
View
7 Book.h
@@ -9,10 +9,15 @@ class Book
{
public:
void LoadFile( const string& filename );
- string GetLine( int line ) const { return m_lstLines[line]; }
+ string GetLine( int line );
+ string GetMostRecentLine() { return m_lstReadLines[ m_lstReadLines.size() - 1]; }
+ int GetLineCount() { return m_lstLines.size(); }
+ void Update( int currentLine, int maxLines );
+ vector<string> GetReadLines() { return m_lstReadLines; }
private:
vector<string> m_lstLines;
+ vector<string> m_lstReadLines;
};
#endif
View
8 Reader.cpp
@@ -1,13 +1,17 @@
#include "Reader.h"
+#include <iostream>
+
Reader::Reader()
{
flite_init();
m_ptrVoice = register_cmu_us_rms( NULL );
}
-void Reader::ReadNextLine( const Book& book )
+void Reader::ReadNextLine( Book& book )
{
- flite_text_to_speech( book.GetLine( m_currentLine ).c_str(), m_ptrVoice, "play" );
+ string strValue = book.GetMostRecentLine();
+ cout << endl << "SAY LINE " << endl << strValue << endl;
+ flite_text_to_speech( strValue.c_str(), m_ptrVoice, "play" );
m_currentLine++;
}
View
3  Reader.h
@@ -14,8 +14,9 @@ class Reader
public:
Reader();
void SetNextLine( int value ) { m_currentLine = value; }
+ void GoToNextLine() { m_currentLine++; }
int GetCurrentLine() const { return m_currentLine; }
- void ReadNextLine( const Book& book );
+ void ReadNextLine( Book& book );
private:
int m_currentLine;
View
19 main.cpp
@@ -36,15 +36,22 @@ int main()
while ( app.Running() )
{
- app.CheckEvents();
+ app.CheckEvents( reader );
+ book.Update( reader.GetCurrentLine(), app.GetMaxLines() );
- app.BeginDraw();
+ if ( !app.Paused() )
+ {
+ app.BeginDraw();
- app.DrawStatus( reader, book );
- app.DrawText( reader, book );
+ app.DrawStatus( reader, book );
+ app.DrawText( reader, book );
- app.EndDraw();
+ app.EndDraw();
- reader.ReadNextLine( book );
+ if ( reader.GetCurrentLine() < book.GetLineCount() )
+ {
+ reader.ReadNextLine( book );
+ }
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.