[ create a new paste ] login | about

Link: http://codepad.org/bL2zVmZY    [ raw code | fork ]

C++, pasted on Mar 22:
#include <SFML/Graphics.hpp>

/** Copyright (c) 2014-present Santiago Aboy Solanes
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice,
//      this list of conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright
//      notice, this list of conditions and the following disclaimer in the
//      documentation and/or other materials provided with the distribution.
//
//   3. The name of the author may be used to endorse or promote products
//      derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////////
*/

/**
EXPLANATION:
This is an example of a credit menu. I made it so you can customize it easily.
The credits re-appear after they show a first time.
If you add multiple texts and want all to be centered, I recommend you to use a std::list or std::vector to manage them.
*/

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

const std::string ttfName = "sans.ttf";

const float scale = 0.8; /// I used 0.8 so you have space for the title(top) and a "back" button (bottom)
const float invScale = 1 / scale; /// 1.25 in the example

int main()
{
    sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32), "SFML Credits");
    sf::Font myFont;
    myFont.loadFromFile(ttfName);

    sf::View myView;
    myView.setSize(SCREEN_WIDTH,SCREEN_HEIGHT);
    myView.setCenter(SCREEN_WIDTH / 2, SCREEN_HEIGHT/ 2);
    myView.setViewport(sf::FloatRect(0, (1 - scale) / 2, scale, scale));

    ///Title
    sf::Text titleCredit("Credits", myFont, SCREEN_HEIGHT / 12);
    titleCredit.setPosition( ( SCREEN_WIDTH - titleCredit.getGlobalBounds().width) / 2, 0);
    titleCredit.setColor(sf::Color::White);

    ///Credits themselves. You can read it from a file using fstreams. The credits are centered, and each text is left aligned.
    ///However, each one isn't centered.
    ///If you want each to be centered you have to create a sf::Text for each one.
    sf::Text creditText("Creator and Lead Developer\nSantiago Aboy Solanes\nLead Artist\nThat other guy\nThis\nis\nto\nshow\nhow\nit\nappears", myFont, SCREEN_WIDTH / 16);
    creditText.setPosition( ( SCREEN_WIDTH * invScale - creditText.getGlobalBounds().width) / 2, SCREEN_HEIGHT);
    creditText.setColor(sf::Color::White);

    ///Such as this one
    sf::Text creditText2("I am centered", myFont, SCREEN_WIDTH / 16);
    creditText2.setPosition( ( SCREEN_WIDTH * invScale - creditText2.getGlobalBounds().width) / 2, creditText.getGlobalBounds().top + creditText.getGlobalBounds().height);
    creditText2.setColor(sf::Color::Red);

    while (window.isOpen()){
        ///You can X out the window
        sf::Event myEvent;
        while (window.pollEvent(myEvent)){
            if (myEvent.type == sf::Event::Closed){
                window.close();
            }
        }
        ///Move the credits upward. You may want this to be tied to a sf::Clock/sf::Time and move it taking into account the delta time
        myView.move(0, SCREEN_HEIGHT * 0.0001);
        ///Reset the view if they go "too far"
        if(myView.getCenter().y > SCREEN_HEIGHT * 2 + creditText.getGlobalBounds().height + creditText2.getGlobalBounds().height){
            myView.setCenter(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
        }

        window.clear();
        ///set the view for the credits, and then show them
        window.setView(myView);
        window.draw(creditText);
        window.draw(creditText2);
        ///set the default view, so that the title doesn't get moved and then show the title
        window.setView(window.getDefaultView());
        window.draw(titleCredit);
        ///display all
        window.display();
    }
    return 0;
}


Create a new paste based on this one


Comments: