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

Author Topic: can somebody post simple small code for loading 1 image  (Read 2443 times)

0 Members and 2 Guests are viewing this topic.

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
can somebody post simple small code for loading 1 image
« on: July 08, 2013, 04:12:22 am »
Title says it all. Graphic tutorial is confusing (atleast for me). I added all the code, from tutorial, i added Textures and sprites code and i get tons of errors from copiler. Am not a c++ newb. [ in case someboy thinks i added "...." from tutorials into my code. So is somebody willing to write a simple program that loads single file and draws it on surface? Nothing more from that.

Thank you in advance

with respect
wmbuRn


G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: can somebody post simple small code for loading 1 image
« Reply #1 on: July 08, 2013, 05:56:48 am »
Sure, but it won't be helpful.
This code is for version 2.0. (which is the one you should use, maybe your linux repo is still in 1.6)

http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php
#include <SFML/Graphics.hpp>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...
        // window.draw(...);

        // end the current frame
        window.display();
    }

    return 0;
}

+

http://www.sfml-dev.org/documentation/2.0/classsf_1_1Texture.php
// This example shows the most common use of sf::Texture:
// drawing a sprite
// Load a texture from a file
sf::Texture texture;
if (!texture.loadFromFile("texture.png"))
    return -1;
// Assign it to a sprite
sf::Sprite sprite;
sprite.setTexture(texture);
// Draw the textured sprite
window.draw(sprite);

=

#include <SFML/Graphics.hpp>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

        // load a texture from a file
        sf::Texture texture;
        if (!texture.loadFromFile("texture.png"))
                return -1;
        // assign it to a sprite
        sf::Sprite sprite;
        sprite.setTexture(texture);


    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...
                // draw the textured sprite
                window.draw(sprite);

        // end the current frame
        window.display();
    }

    return 0;
}

Or you could post your real code and errors.
« Last Edit: July 08, 2013, 06:10:20 am by G. »

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: can somebody post simple small code for loading 1 image
« Reply #2 on: July 08, 2013, 03:11:11 pm »
Thank you G. Code worked and which i will analyze and continue throught tutorial. I am using sfml 2.0. Otherwise i wont be able to learn window tutorial :)

Thanks again, you were a great help. Now since i know where code goes i can do other stuff.
« Last Edit: July 08, 2013, 03:39:34 pm by wmbuRn »

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: can somebody post simple small code for loading 1 image
« Reply #3 on: July 08, 2013, 03:34:03 pm »
So G. i saw your code, and i raise it to:

-------------------------------------------------------------
#include <SFML/Graphics.hpp>

int main()
{

    sf::RenderWindow screen(sf::VideoMode(1024, 768), "Mah title!");
    screen.setFramerateLimit(30);


    sf::Texture texture;
    if (!texture.loadFromFile("cb.bmp"))
    {
        return -1;
    }


    sf::Sprite sprite;
    sprite.setTexture(texture);



    while (screen.isOpen())  // main loop
    {

        sf::Event evt;
        while (screen.pollEvent(evt))
        {
           
            if (evt.type == sf::Event::Closed)
                screen.close();

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
            sprite.move(-5, 0);
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            {
            sprite.move(5,0);
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
                 sprite.move(0,-5);
            }

             if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
            sprite.move(0,5);
            }

        } // end of while(screen.pollEvent(evt)

screen.clear(sf::Color::Black); // Black as night
screen.draw(sprite); // sprite drawed
screen.display();

    } // end of while(screen.isOpen())

return 0;
}  // end int main()
 
------------------------------------------------------------------------

Thank you again G. :)
« Last Edit: July 09, 2013, 05:34:04 am by wmbuRn »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: can somebody post simple small code for loading 1 image
« Reply #4 on: July 08, 2013, 09:32:05 pm »
Ok, but it was only a matter of 2 copy / paste.  ??? ???
But don't use sf::Keyboard::isKeyPressed inside your event loop, they are not events.

And please, use the [code = cpp] tag when posting your code on the forum.  ;)

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: can somebody post simple small code for loading 1 image
« Reply #5 on: July 08, 2013, 10:34:55 pm »
Will learn sfml. every hr am better by 20% :)

Will use code next time. And yes i needed copy so that i know where things go. Now its easy :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: can somebody post simple small code for loading 1 image
« Reply #6 on: July 08, 2013, 10:35:21 pm »
Quote
Will use code next time.
Don't hesitate to edit your previous posts too.
Laurent Gomila - SFML developer

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: can somebody post simple small code for loading 1 image
« Reply #7 on: July 09, 2013, 05:34:35 am »
Done :)