1. Post #1
    Conna Wiles
    Guest
    I'm currently doing the 'drawing a sprite' tutorial - this is my code (a mixture of everything I've learned so far), but for some reason it's just a black screen. I commented out the code that draws a spinning white cube (that worked).

    // kuromeku's messy c0de <3
    
    #include <SFML/Graphics.hpp>
    #include <SFML/System.hpp>
    #include <SFML/Window.hpp>
    #include <iostream>
    #include <string>
    
    template<typename T>
        void PrintMessage(T msg) {
            std::cout << msg << std::endl;
        };
    
    // this is where we draw shit lul
    void drawMain(sf::Clock &myClock) {
        // i'll look into these in more detail later
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.f, 0.f, -200.f);
        glRotatef(myClock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
        glRotatef(myClock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
        glRotatef(myClock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
    
        // draw a pretty cube :D (l2vertex bro)
        glBegin(GL_QUADS);
            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f(-50.f,  50.f, -50.f);
            glVertex3f( 50.f,  50.f, -50.f);
            glVertex3f( 50.f, -50.f, -50.f);
    
            glVertex3f(-50.f, -50.f, 50.f);
            glVertex3f(-50.f,  50.f, 50.f);
            glVertex3f( 50.f,  50.f, 50.f);
            glVertex3f( 50.f, -50.f, 50.f);
    
            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f(-50.f,  50.f, -50.f);
            glVertex3f(-50.f,  50.f,  50.f);
            glVertex3f(-50.f, -50.f,  50.f);
    
            glVertex3f(50.f, -50.f, -50.f);
            glVertex3f(50.f,  50.f, -50.f);
            glVertex3f(50.f,  50.f,  50.f);
            glVertex3f(50.f, -50.f,  50.f);
    
            glVertex3f(-50.f, -50.f,  50.f);
            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f( 50.f, -50.f, -50.f);
            glVertex3f( 50.f, -50.f,  50.f);
    
            glVertex3f(-50.f, 50.f,  50.f);
            glVertex3f(-50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f,  50.f);
        glEnd();
    };
    
    // the main func duurrrr
    int main()
    {
        sf::WindowSettings kuroSettings;
        sf::Image myScreen;
        sf::Image mySprite;
        sf::Clock myClock;
    
        // wrryyyyyyyyyyyyyyyyy
        if ( !mySprite.LoadFromFile("sprite.tga") ) {
            PrintMessage("FML");
        };
    
        // now we can make the real sprite.
        sf::Sprite myRealSprite;
    
        myRealSprite.SetColor( sf::Color(255, 255, 255, 255) );
        myRealSprite.SetX(200.f);
        myRealSprite.SetY(100.f);
        myRealSprite.SetImage(mySprite);
        myRealSprite.SetPosition(200.f, 100.f);
        myRealSprite.SetRotation(30.f);
        myRealSprite.SetCenter(0, 0);
        myRealSprite.SetScaleX(2.f);
        myRealSprite.SetScaleY(0.5f);
        myRealSprite.SetScale(2.f, 0.5f);
        myRealSprite.SetBlendMode(sf::Blend::Multiply);
    
        // set some settings for mah openGL
        kuroSettings.DepthBits = 24;
        kuroSettings.StencilBits = 8;
        kuroSettings.AntialiasingLevel = 2;
    
        // maek a motherfucking window homie
        sf::RenderWindow kuroApp(sf::VideoMode(800, 600, 32), "kuroApp", sf::Style::Close, kuroSettings);
    
        /*
        // Set color and depth clear value
        glClearDepth(1.f);
        glClearColor(0.f, 0.f, 0.f, 0.f);
    
        // Enable Z-buffer read and write
        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);
    
        // Setup a perspective projection
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(90.f, 1.f, 1.f, 500.f);
        */
    
        // i like vertical sync - it makes me smile :))))))
        kuroApp.UseVerticalSync(true);
    
        // Let's get out application input, ftwwwww
        const sf::Input& appInput = kuroApp.GetInput();
    
        while ( kuroApp.IsOpened() ) {
            sf::Event eventCapture;
    
            // this is the frame time and shit
            float theFrameTime = kuroApp.GetFrameTime();
    
            // some basic movement shit you no the score bruv
            if (appInput.IsKeyDown(sf::Key::Space)) {
                PrintMessage("STOP HOLDING DOWN SPACE WTF");
            };
    
            if (appInput.IsKeyDown(sf::Key::A) ) {
                myRealSprite.Move(-100 * theFrameTime, 0);
            };
    
            if (appInput.IsKeyDown(sf::Key::D) ) {
                 myRealSprite.Move(100 * theFrameTime, 0);
            };
    
            if (appInput.IsKeyDown(sf::Key::W) ) {
                 myRealSprite.Move(0, -100 * theFrameTime);
            };
    
            if (appInput.IsKeyDown(sf::Key::S) ) {
                myRealSprite.Move(0, 100 * theFrameTime);
            };
    
            if (appInput.IsKeyDown(sf::Key::Left) ) {
                myRealSprite.Rotate(-100 * theFrameTime);
            };
    
            if (appInput.IsKeyDown(sf::Key::Right) ) {
                myRealSprite.Rotate(100 * theFrameTime);
            };
    
            while (kuroApp.GetEvent(eventCapture)) {
                if (eventCapture.Type == sf::Event::Closed) {
                    kuroApp.Close();
                }else if (eventCapture.Type == sf::Event::Resized) {
                    glViewport(0, 0, eventCapture.Size.Width, eventCapture.Size.Height);
                }else if (eventCapture.Type == sf::Event::KeyPressed) {
                    switch(eventCapture.Key.Code) {
                        case sf::Key::Escape:
                            kuroApp.Close();
                            break;
                        case sf::Key::Return:
                            PrintMessage( myClock.GetElapsedTime() );
                            break;
                        case sf::Key::F1:
                            myScreen = kuroApp.Capture();
                            myScreen.SaveToFile("screenshot.jpg");
                            break;
                        default:
                            break;
                    };
                };
            };
    
            // clear the previous frame.
            //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
            /*
            // let's make some random colors so we can rave.
            int randomR = sf::Randomizer::Random(0, 255);
            int randomG = sf::Randomizer::Random(0, 255);
            int randomB = sf::Randomizer::Random(0, 255);
    
            // clear it and display it... with random colors.
            kuroApp.Clear( sf::Color(randomR, randomG, randomB) );
            */
    
            kuroApp.Clear();
    
            // call our main draw function.
            //drawMain(myClock);
    
            // display the window for epic justice
            kuroApp.Draw(myRealSprite);
            kuroApp.Display();
        };
    
        return EXIT_SUCCESS;
    }
    

    Edited:

    Nevermind, I fixed it - it was just drawing the sprite black... but for some reason I can't draw my sprite and my spinning cube at the same time. Anybody know a way around this?
    Reply With Quote Edit / Delete Reply Dumb Dumb x 1 (list)

  2. Post #2
    DarkSpirit05er's Avatar
    August 2009
    1,533 Posts
    Alot of includes.

    Edit:

    I like boxes.
    Reply With Quote Edit / Delete Reply Portugal Show Events Dumb Dumb x 3 (list)

  3. Post #3
    DeltaOps101's Avatar
    February 2006
    72 Posts
    Nevermind, I fixed it - it was just drawing the sprite black... but for some reason I can't draw my sprite and my spinning cube at the same time. Anybody know a way around this?
    For your code:
    kuroApp.PreserveOpenGLStates(true);
    ... when you create the sf::RenderWindow.

    (From here, in the "Mixing with OpenGL" section)


    Also, your comments are annoying.
    Reply With Quote Edit / Delete Reply United States Show Events Agree Agree x 3 (list)

  4. Post #4
    Gold Member
    efeX's Avatar
    April 2009
    2,323 Posts
    FYI SFML/graphics.hpp includes window.hpp which includes system.hpp so no need to do that, just include SFML/Graphics.hpp
    Reply With Quote Edit / Delete Reply United States Show Events Agree Agree x 1Disagree Disagree x 1 (list)

  5. Post #5
    Gold Member
    gparent's Avatar
    January 2005
    3,928 Posts
    FYI SFML/graphics.hpp includes window.hpp which includes system.hpp so no need to do that, just include SFML/Graphics.hpp
    Huh, no, don't do that. It's a great way to make your code not compile when they decide to change which header includes what. Also, if you do it with the standard library, your code could fail to compile under different compilers.

    If you use stuff from system.hpp, then include system.hpp.

    EDIT: However, a fair assumption would be that system.hpp will include these: http://www.sfml-dev.org/documentatio...hpp-source.htm, since "system" is an entire .lib.

    But that's intended.
    Reply With Quote Edit / Delete Reply Show Events Agree Agree x 2Useful Useful x 2 (list)

  6. Post #6
    Gold Member
    efeX's Avatar
    April 2009
    2,323 Posts
    I see. never really thought of that, thanks.

  7. Post #7
    DeltaOps101's Avatar
    February 2006
    72 Posts
    Huh, no, don't do that. It's a great way to make your code not compile when they decide to change which header includes what. Also, if you do it with the standard library, your code could fail to compile under different compilers.

    If you use stuff from system.hpp, then include system.hpp.

    EDIT: However, a fair assumption would be that system.hpp will include these: http://www.sfml-dev.org/documentatio...hpp-source.htm, since "system" is an entire .lib.

    But that's intended.
    Not to say that this isn't a good idea, but the behavior in question is specifically suggested (or at the very least acknowledged) in the SFML tutorials (here and here). Barring a major rewrite (which likely wouldn't be backwards compatible with your code to begin with), Graphics.hpp will continue to include Window.hpp, and Window.hpp will continue to include System.hpp.

    In general, though (and yes, in SFML too, despite it's insistence on oversimplifying everything) - both for clarity and the reasons you stated - it's still a good idea to explicitly include what you're using.

  8. Post #8
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,024 Posts
    Immediate mode :C
    lrn2OGL3 :3