Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved]sf::Texture texture;  (Read 1876 times)

0 Members and 1 Guest are viewing this topic.

Yemeni Cpluspluser

  • Newbie
  • *
  • Posts: 25
  • C/C++, Soon Java and SQL ^_^
    • View Profile
    • Email
[Solved]sf::Texture texture;
« on: July 31, 2013, 10:20:11 pm »
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>

int  main()
{
    std::cout << "This is the brain of the game, don't think of closing it\nPlease wait the game is loading . . . " << std::endl;
    // declare mainWindow
    sf::RenderWindow mainWindow( sf::VideoMode( 800, 600, 32) , "Hello title" , sf::Style::Close ); // Declare of mainWindow
    std::cout << "Game resolution is : " << mainWindow.getSize().x  << " X " << mainWindow.getSize().y
              << " bits : " << mainWindow.getSettings().depthBits + mainWindow.getSettings().stencilBits << std::endl;
    // declared mainWidow

    // load images into ram
    sf::Image img_SpaceShip;
    if( !img_SpaceShip.loadFromFile( "Resource/img_ship.png" ) )
       std::cout << "Error LFF : Failed to load Resource/img_ship.png" << std::endl;
    else std::cout << "Resource/img_ship.png successfully loaded" << std::endl;

    sf::Texture img_button;
    if ( !img_button.loadFromFile( "Resource/img_button.png" ) )
        std::cout << "Error LFF : Failed to load  Resource/img_button.png" << std::endl;
    else std::cout << "Resource/img_button.png successfully loaded" << std::endl;

    sf::Texture img_button2;
    if ( !img_button2.loadFromFile( "Resource/img_button2.png" ) )
        std::cout << "Error LFF : Failed to load  Resource/img_button2.png" << std::endl;
    else std::cout << "Resource/img_button2.png successfully loaded" << std::endl;

    // load sound and music into ram
    sf::Music msnd_intro;
    if( !msnd_intro.openFromFile( "Resource/msnd_intro.ogg") )
        std::cout << "Error OFF : Failed to load Resource/msnd_intro.ogg" << std::endl;
    else std::cout << "Resource/msnd_intro.ogg successfully loaded" << std::endl;
    msnd_intro.setLoop( true );
    msnd_intro.play();

    // loads fonts
    sf::Font font_English;
    if (!font_English.loadFromFile( "Resource/font_English.ttf" ) )
        std::cout << "Error LFF : Failed to load Resource/font_English.ttf" << std::endl;
    else std::cout << "Resource/font_English.ttf successfully loaded" << std::endl;

    // every thing should be loaded

    // declarations

    sf::Text text_MainWindowText ( "SpaceShips 0.01 Alfa", font_English);
    text_MainWindowText.setCharacterSize( 40 );
    text_MainWindowText.setColor( sf::Color::Blue );
    text_MainWindowText.setPosition( 200 , 30 );

    sf::Text text_MainWindowNewGame ( "New Game", font_English);
    text_MainWindowNewGame.setCharacterSize( 30 );
    text_MainWindowNewGame.setColor( sf::Color::Green );
    text_MainWindowNewGame.setPosition( 60 , 150 );

    sf::Text text_MainWindowExit ( "Exit Game", font_English );
    text_MainWindowExit.setCharacterSize( 30 );
    text_MainWindowExit.setColor( sf::Color::Red );
    text_MainWindowExit.setPosition( 60, 200 );

    sf::Sprite spr_ButtonNewGame;
    spr_ButtonNewGame.setTexture( img_button );
    spr_ButtonNewGame.setPosition( 0 , 150 );
    spr_ButtonNewGame.setScale( 1.8 , 1 );

    sf::Sprite spr_buttonExitGame;
    spr_ButtonNewGame.setTexture( img_button2 );
    spr_ButtonNewGame.setPosition( 0 , 200 );
    spr_ButtonNewGame.setScale( 1.8 , 1 );



    // the main game loop
    while ( mainWindow.isOpen() ) // main game loop
    {
        sf::Event mainEvents; // declare of main events checker
        while ( mainWindow.pollEvent( mainEvents ) ); // checking
        {
            switch ( mainEvents.type ) // what event type
            {
            case sf::Event::EventType::Closed: // users clicks X or press Alt-F4
                mainWindow.close(); // close the window
                break;
            }
        }
    // main loop :
    mainWindow.clear( sf::Color( 11,11,11) );
    mainWindow.draw( spr_ButtonNewGame );
    mainWindow.draw( spr_buttonExitGame );
    mainWindow.draw( text_MainWindowText );
    mainWindow.draw( text_MainWindowNewGame );
    mainWindow.draw( text_MainWindowExit );
    mainWindow.display();
    }
    return EXIT_SUCCESS; // returns 0;
}
 

I tried making one texture for 2 sprites failed
then made 2 textures for 2 sprites failed

the problem is that it can only display one sprite at a time, I don't know why.


the difference from sf::Image and sf::Texture it says that that for memory and that for graphics card
so is it because I use sf::texture instead of sf::Image?



don't be hard on me, this is my first project  :P .
« Last Edit: August 01, 2013, 02:46:56 am by Yemeni Cpluspluser »
sf::signature mySignature;
mySignature.setPosition(forum.x,forum.y);
window.draw(mySignature);

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: sf::Texture texture;
« Reply #1 on: July 31, 2013, 10:52:51 pm »
    sf::Sprite spr_ButtonNewGame;
    spr_ButtonNewGame.setTexture( img_button );
    spr_ButtonNewGame.setPosition( 0 , 150 );
    spr_ButtonNewGame.setScale( 1.8 , 1 );

    sf::Sprite spr_buttonExitGame;
    spr_ButtonNewGame.setTexture( img_button2 );
    spr_ButtonNewGame.setPosition( 0 , 200 );
    spr_ButtonNewGame.setScale( 1.8 , 1 );

 

The problem is right here, you set the first sprite, create the second sprite, then set the second texture to the first sprite.
Don't Copy and paste ;)

Edit: Words - they got the better of me
« Last Edit: July 31, 2013, 10:54:29 pm by Gobbles »

Yemeni Cpluspluser

  • Newbie
  • *
  • Posts: 25
  • C/C++, Soon Java and SQL ^_^
    • View Profile
    • Email
Re: sf::Texture texture;
« Reply #2 on: July 31, 2013, 10:55:55 pm »
I thank your eagle eyes  :)
Lesson #1 from SFML users : Don't copy paste. OK!

sf::signature mySignature;
mySignature.setPosition(forum.x,forum.y);
window.draw(mySignature);

 

anything