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

Author Topic: [SFML 2] Set window focus  (Read 4700 times)

0 Members and 1 Guest are viewing this topic.

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
[SFML 2] Set window focus
« on: March 13, 2011, 05:24:22 am »
hi, is there any way to set the window focus? After creating my window, the window is always not in focus (e.g it doesn't capture key press event), and I have to click on it to set it as the focus.

regards

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SFML 2] Set window focus
« Reply #1 on: March 13, 2011, 09:45:09 am »
Nop, but this looks like a bug that needs to be fixed ;)
Laurent Gomila - SFML developer

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
[SFML 2] Set window focus
« Reply #2 on: March 13, 2011, 01:04:29 pm »
just in case if you need it, here is a short code that will reproduce the problem. I think it only occur if you are creating a 2nd window from another thread.

Code: [Select]


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
void ThreadFunction()
{
    // Create the main window
    sf::ContextSettings Settings;
    Settings.DepthBits         = 24; // Request a 24 bits depth buffer
    Settings.StencilBits       = 8;  // Request a 8 bits stencil buffer
    Settings.AntialiasingLevel = 0;  // Request 2 levels of antialiasing
    Settings.MajorVersion      = 3;
    Settings.MinorVersion      = 3;
    sf::Window App(sf::VideoMode(1024, 768, 32), "SFML OpenGL", sf::Style::Close, Settings);

    Settings = App.GetSettings();

    std::cout << "DepthBits " << Settings.DepthBits << "\n";
    std::cout << "StencilBits " << Settings.StencilBits << "\n";
    std::cout << "Antialiasing Level " << Settings.AntialiasingLevel << "\n";
    std::cout << "Opengl Version " << Settings.MajorVersion << "." << Settings.MinorVersion << "\n";

    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(0.176f, 0.196f, 0.667f, 0.0f);

    // 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 game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

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

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

        // Set the active window before using OpenGL commands
        // It's useless here because active window is always the same,
        // but don't forget it if you use multiple windows or controls
        //App.SetActive(true);

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

        // Finally, display rendered frame on screen
        App.Display();

        //App.SetActive(false);
    }

    std::cin.get();
}

int main()
{
    {
        sf::ContextSettings Settings;
        Settings.DepthBits         = 24; // Request a 24 bits depth buffer
        Settings.StencilBits       = 8;  // Request a 8 bits stencil buffer
        Settings.AntialiasingLevel = 0;  // Request 2 levels of antialiasing
        Settings.MajorVersion      = 3;
        Settings.MinorVersion      = 3;
        sf::Window dummy_win(sf::VideoMode(0, 0, 32), "dummy window", sf::Style::Close, Settings);
        dummy_win.Close();
    }

    sf::Thread thread(&ThreadFunction);

    thread.Launch();

    return EXIT_SUCCESS;
}