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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - hakeris1010

Pages: [1]
1
Window / Event handling in multiple threads.
« on: March 15, 2015, 08:44:55 pm »
I've seen that when handling events normally in one thread my game goes to several problems: for example, if mouse is moving, the continuous key pressing is not detected. This is because all the handling is in one window class, with one sf::Event object. But how to make it happen in multiple threads, with multiple event objects, for example one thread handles keyboard, another thread mouse?

2
General / Re: error: class sf::Window has no member named...
« on: March 02, 2015, 08:19:42 pm »
Thanks! so small difference that i have not even noticed  :)

3
General / error: class sf::Window has no member named...
« on: March 01, 2015, 10:55:40 am »
Hello.
I was trying to run this code from Laurent Gomilla's Github. However, for every method of every sfml class this error shows up (class sf::... has no member named...). I have all libraries linked statically, and everything was working before trying this code. The code:

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>


////////////////////////////////////////////////////////////
// Put here the exported GIMP image array!
////////////////////////////////////////////////////////////

int main()
{
    // Create the main window
    sf::Window window(sf::VideoMode(640, 480, 32), "SFML Window", sf::Style::Default, sf::ContextSettings(32));
    window.SetFramerateLimit(60);

    ////////////////////////////////////////////////////////////
    /// This line is added! Make sure the name of your image is the same
    ///
    //window.SetIcon( sfml_icon.width,  sfml_icon.height,  sfml_icon.pixel_data );
    ///
    ////////////////////////////////////////////////////////////

    // Create a clock for measuring the time elapsed
    sf::Clock clock;

    // Set the color and depth clear values
    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);

    // Start the game loop
    while (window.IsOpened())
    {
        // Process events
        sf::Event event;
        while (window.PollEvent(event))
        {
            // Close window : exit
            if (event.Type == sf::Event::Closed)
                window.Close();

            // Escape key : exit
            if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
                window.Close();

            // Resize event : adjust viewport
            if (event.Type == sf::Event::Resized)
                glViewport(0, 0, event.Size.Width, event.Size.Height);
       }

        // Activate the window before using OpenGL commands.
        // This is useless here because we have only one window which is
        // always the active one, but don't forget it if you use multiple windows
        window.SetActive();

        // Clear color and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Apply some transformations
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.f, 0.f, -200.f);
        glRotatef(clock.GetElapsedTime() * 0.05f, 1.f, 0.f, 0.f);
        glRotatef(clock.GetElapsedTime() * 0.03f, 0.f, 1.f, 0.f);
        glRotatef(clock.GetElapsedTime() * 0.09f, 0.f, 0.f, 1.f);

        // Draw a cube
        glBegin(GL_QUADS);

            glColor3f(1.f, 0.f, 0.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);

            glColor3f(1.f, 0.f, 0.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);

            glColor3f(0.f, 1.f, 0.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);

            glColor3f(0.f, 1.f, 0.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);

            glColor3f(0.f, 0.f, 1.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);

            glColor3f(0.f, 0.f, 1.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();

        // Finally, display the rendered frame on screen
        window.Display();
    }

    return EXIT_SUCCESS;
}

4
System / Re: High CPU usage when using Threads!
« on: February 25, 2015, 05:59:27 pm »
Now i added sf::sleep to the event checking thread.

But there is weird thing: when using  sf::sleep(sf::microseconds(1000)), or 1 millisecond, CPU usage goes down to zero, perfectly good.

But when sleeping for less than 1 millisecond, for example sf::sleep(sf::microseconds(900)), then CPU usage jumps as high as 25% !!!

How so small change in sleep time can make CPU usage so high? This might be a SFML bug.

5
System / High CPU usage when using Threads!
« on: February 25, 2015, 03:07:05 pm »
Hello.

I'm using SFML threads, and this makes very high CPU usage.  Windows task manager is used to measure the CPU usage.

When only main thread is running, CPU usage is only 1 %, but when 2 threads run, CPU gets as high as 25 %, 3 threads - 50% and so on!!

My CPU has 4 cores, so it seems that each additional thread consumes the whole core!

Why this happens, with 1 thread adding 25% CPU?

It's nothing related to the framerate or sf::sleep(), they make no effect to CPU.

There's a little experimental program which shows everything. When running this code CPU is 25%.
When using addidional threads (commented) then goes as high as 75 %.

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <SFML/Graphics.hpp>
    #include <SFML/System.hpp>

    using namespace std;

    sf::RenderWindow window;
    sf::Event ev;

    bool areEvents=false;
    bool Window_Closed=false;


    void drawZumthin()
    {
    for(int i=0; i<4; i++)
    {
        sf::CircleShape shape(50);
        shape.setFillColor(sf::Color::Green);
        shape.setPosition(i*100,0);

        window.draw(shape);
    }
    }

    void threadOne(int num)
    {
    long i=0;
    while(1)
    {
        if(i==0) cout<<"Thread no. "<<num<<endl;
        //sf::sleep(sf::milliseconds(20));
        i++;
    }
    }

    struct Chunk
    {
    int type;
    int tiles[64];
    };

    void loadNewChunks()
    {
    cout<<"Loading Chunks\n";

    Chunk chunks[25];

    for(int i=0; i<25; i++)
    {
        chunks[i].type=i;

        for(int j=0; j<64; j++)
        {
            chunks[i].tiles[j]=j-i;
        }
    }
    }

    void startWind()
    {
    window.create(sf::VideoMode(400,300),"test");
    window.setFramerateLimit(60);

    while(window.isOpen())
    {
        while(window.pollEvent(ev))
        {
            areEvents=true;

            if(ev.type==sf::Event::Closed)
            {
                window.close();
                Window_Closed=true;
            }
        }

        window.clear();

        drawZumthin();

        window.display();
    }
    }

    void getEvents()
    {
    cout<<"getEvents started\n";
    while(!Window_Closed)
    {
        if(areEvents)
        {
            bool newChunk=false;

            if(ev.type==sf::Event::TextEntered)
            {
                if(char(ev.text.unicode)=='w') cout<<"\nUp!\n\n";
                if(char(ev.text.unicode)=='s') cout<<"\nDown!\n\n";
                if(char(ev.text.unicode)=='a') cout<<"\nLeft!\n\n";
                if(char(ev.text.unicode)=='d') cout<<"\nRight!\n\n";

                newChunk=true;
            }

            if(newChunk) loadNewChunks();

            areEvents=false;
        }
    }
    cout<<"GetEvents Stopped.\n";
    }

    int main()
    {
    /*cout<<"Launching experimental threads\n";

    sf::Thread thr2(threadOne, 1);
    sf::Thread thr3(threadOne, 2);
    thr2.launch();
    thr3.launch();*/


    cout<<"Launching startWind()\n";

    sf::Thread thr1(startWind);

    thr1.launch();

    sf::sleep(sf::seconds(2));

    getEvents();

    return 0;
    }
   

Please help me with this issue. Thanks!

6
Window / How to make window fixed size (cant be resized)?
« on: October 22, 2014, 07:38:01 pm »
I cant find how to do it. My program window needs to be always 800x600, and i dont want it to allow resizing, because resizing causes bugs in my program.

7
Graphics / Set size of the sprtite
« on: September 08, 2014, 04:48:10 pm »
Hello! I need to set the the specific size of the sprite. SetScale() is not a solution there, because sprite may come in unknown dimensions, and program must resize it to lets say 160x100. What to do???

Pages: [1]