Permalink
Browse files

Cleaned up Read To Me code - added Application, Book, and Reader cals…

…ses. Word wrap works properly now.
  • Loading branch information...
1 parent a868994 commit a6de6492c4fdb640604819e2a66afdb936d695e6 @rejcx committed Mar 7, 2013
Showing with 251 additions and 152 deletions.
  1. +97 −0 Application.cpp
  2. +32 −0 Application.h
  3. +42 −0 Book.cpp
  4. +18 −0 Book.h
  5. +1 −1 ReadToMe.cbp
  6. +13 −0 Reader.cpp
  7. +25 −0 Reader.h
  8. +23 −151 main.cpp
View
@@ -0,0 +1,97 @@
+#include "Application.h"
+
+#include <iostream>
+
+Application::Application()
+{
+ m_screenWidth = 800;
+ m_screenHeight = 600;
+ m_quit = false;
+
+ // Setup SFML stuff
+ m_app.Create( sf::VideoMode( m_screenWidth, m_screenHeight, 32 ), "Read to Me" );
+ m_font.LoadFromFile( "Averia-Bold.ttf" );
+ m_fontSize = 16;
+
+ // Setup background
+ m_background = sf::Shape::Rectangle( 0, 0, m_screenWidth, m_screenHeight, sf::Color( 76, 169, 255 ) );
+ m_textOutputWidth = m_screenWidth / 2;
+}
+
+Application::~Application()
+{
+ m_app.Close();
+}
+
+void Application::CheckEvents()
+{
+ sf::Event event;
+ while ( m_app.GetEvent( event ) )
+ {
+ if ( event.Type == sf::Event::Closed )
+ {
+ m_quit = true;
+ }
+ }
+}
+
+void Application::BeginDraw()
+{
+ m_app.Clear();
+
+ // Draw the background
+ m_app.Draw( m_background );
+}
+
+void Application::DrawStatus( const Reader& reader, const Book& book )
+{
+}
+
+void Application::DrawText( const Reader& reader, const 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 xPos = 10;
+ for ( int i = 0; i <= reader.GetCurrentLine(); i++ )
+ {
+ string currentLine = book.GetLine( i );
+ int beginIndex = 0;
+ int endIndex = 0;
+
+ // Account for word-wrapping
+ while ( currentLine.length() * (m_fontSize/2) > m_textOutputWidth )
+ {
+ endIndex = ( 2 * m_textOutputWidth / m_fontSize ) - 10;
+ // Adjust endIndex to be where a ' ' is.
+ while ( currentLine[ endIndex ] != ' ' )
+ {
+ endIndex++;
+ }
+
+ sf::String textLine( currentLine.substr( beginIndex, endIndex ),
+ m_font, m_fontSize );
+ textLine.SetPosition( xPos, yPos );
+ textLine.SetColor( sf::Color( 0, 0, 0 ) );
+
+ m_app.Draw( textLine );
+ yPos += m_fontSize;
+ currentLine.erase( beginIndex, endIndex );
+ }
+
+ // Output remaining line
+ sf::String textLine( currentLine, m_font, m_fontSize );
+ textLine.SetPosition( xPos, yPos );
+ textLine.SetColor( sf::Color( 0, 0, 0 ) );
+
+ m_app.Draw( textLine );
+ yPos += m_fontSize;
+ }
+}
+
+void Application::EndDraw()
+{
+ m_app.Display();
+}
View
@@ -0,0 +1,32 @@
+#ifndef _APPLICATION
+#define _APPLICATION
+
+#include <SFML/Graphics.hpp>
+
+#include "Reader.h"
+#include "Book.h"
+
+class Application
+{
+ public:
+ Application();
+ ~Application();
+ bool Running() { return !m_quit; }
+ void CheckEvents();
+ void BeginDraw();
+ void DrawStatus( const Reader& reader, const Book& book );
+ void DrawText( const Reader& reader, const Book& book );
+ void EndDraw();
+
+ private:
+ sf::RenderWindow m_app;
+ sf::Font m_font;
+ int m_fontSize;
+ int m_screenWidth, m_screenHeight;
+ int m_textOutputWidth;
+ bool m_quit;
+
+ sf::Shape m_background;
+};
+
+#endif
View
@@ -0,0 +1,42 @@
+#include "Book.h"
+
+#include <fstream>
+#include <iostream>
+using namespace std;
+
+void Book::LoadFile( const string& filename )
+{
+ ifstream infile( filename.c_str() );
+
+ string inputBuffer;
+ string lineBuffer = "";
+
+ while ( infile >> inputBuffer )
+ {
+ if ( inputBuffer.find( "." ) != std::string::npos ||
+ inputBuffer.find( "?" ) != std::string::npos ||
+ inputBuffer.find( "!" ) != std::string::npos )
+ {
+ // Cut off sentence at the punctuation
+ int puncIndex = inputBuffer.find( "." );
+ if ( puncIndex == std::string::npos )
+ {
+ puncIndex = inputBuffer.find( "?" );
+ if ( puncIndex == std::string::npos )
+ {
+ puncIndex = inputBuffer.find( "!" );
+ }
+ }
+
+ lineBuffer += " " + inputBuffer.substr( 0, puncIndex+1 );
+ m_lstLines.push_back( lineBuffer );
+
+ // Begin next line
+ lineBuffer = inputBuffer.substr( puncIndex+1 );
+ }
+ else
+ {
+ lineBuffer += " " + inputBuffer;
+ }
+ }
+}
View
18 Book.h
@@ -0,0 +1,18 @@
+#ifndef _BOOK
+#define _BOOK
+
+#include <vector>
+#include <string>
+using namespace std;
+
+class Book
+{
+ public:
+ void LoadFile( const string& filename );
+ string GetLine( int line ) const { return m_lstLines[line]; }
+
+ private:
+ vector<string> m_lstLines;
+};
+
+#endif
View
@@ -7,7 +7,7 @@
<Option compiler="gcc" />
<Build>
<Target title="Release">
- <Option output="bin/Release/Flite-TTS-Sample" prefix_auto="1" extension_auto="1" />
+ <Option output="ReadToMe" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
View
@@ -0,0 +1,13 @@
+#include "Reader.h"
+
+Reader::Reader()
+{
+ flite_init();
+ m_ptrVoice = register_cmu_us_rms( NULL );
+}
+
+void Reader::ReadNextLine( const Book& book )
+{
+ flite_text_to_speech( book.GetLine( m_currentLine ).c_str(), m_ptrVoice, "play" );
+ m_currentLine++;
+}
View
@@ -0,0 +1,25 @@
+#ifndef _READER
+#define _READER
+
+#include "Book.h"
+
+extern "C" {
+ #include "flite.h"
+}
+
+extern "C" cst_voice* register_cmu_us_rms(const char* voxdir);
+
+class Reader
+{
+ public:
+ Reader();
+ void SetNextLine( int value ) { m_currentLine = value; }
+ int GetCurrentLine() const { return m_currentLine; }
+ void ReadNextLine( const Book& book );
+
+ private:
+ int m_currentLine;
+ cst_voice* m_ptrVoice;
+};
+
+#endif
Oops, something went wrong.

0 comments on commit a6de649

Please sign in to comment.