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.


Topics - sleeter

Pages: [1]
1
Hello everyone,

Please let me start by explicitly stating, I am not an expert or a great programmer. I know a little (normally just enough to get myself into bother, but not out of it) and have only written a couple of simple VB programs for my work.

I tend to learn by trying bits of a program out before I put those bits together into a final project. At the moment I am working on splash screens for a game I have a mind to program but I cannot work out how to have the splash screen visible until the player presses a key (any key, rather than a specific key). I didn't want to do this by polling for every single key on the keyboard, so I'm trying to trigger on the event 'KeyReleased' (see code).

My problem is that 'KeyReleased' is triggered when I mouse-over the SFML window, which seems to be completely against what I would intuit and can see from the documentation I've read.

 :-[

main.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "loop.h"

int main() {
        Loop::StartLoop();
}

loop.h
#pragma once

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

class Loop {

public:
        Loop() { _loopState = Dead; };
        ~Loop() { };

        static void StartLoop();
       
        static sf::RenderWindow _loopWindow;

private:
        static void ContinuousLoop();
        static void Pause();

        static enum PossibleLoopStates { Dead, Opening, Running, Closing };
        static int _loopState;
};

loop.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include "loop.h"
#include "splash.h"

void Loop::StartLoop() {
        if(_loopState == Dead) {
                _loopWindow.create(sf::VideoMode(800, 400), "Looper Window");
                Splash::ShowSplash(_loopWindow);
                _loopState = Opening;
                ContinuousLoop();
        } else {
                return;
        }
}

void Loop::ContinuousLoop() {

        while(_loopWindow.isOpen()) {
                sf::Event happened;

                _loopWindow.clear();

                while(_loopWindow.pollEvent(happened)) {
                        if(happened.type == sf::Event::Closed)
                                _loopWindow.close();
                        if(happened.type == sf::Event::KeyPressed && happened.key.code == sf::Keyboard::Escape)
                                _loopWindow.close();
                        if(happened.type == sf::Event::LostFocus) {
                                Pause();
                                std::cout << "\nUnpaused!";
                        }
                }

                _loopWindow.display();
        }
}

void Loop::Pause() {

        while(_loopWindow.isOpen()) {
                sf::Event happened;

                std::cout << "Paused!";

                sf::sleep(sf::milliseconds(500));

                while(_loopWindow.pollEvent(happened)) {
                        if(happened.type == sf::Event::Closed)
                                _loopWindow.close();
                        if(happened.type == sf::Event::KeyPressed && happened.key.code == sf::Keyboard::Escape)
                                _loopWindow.close();
                        if(happened.type == sf::Event::GainedFocus)
                                return;
                }

                _loopWindow.display();
        }
}

sf::RenderWindow Loop::_loopWindow;
int Loop::_loopState;

splash.h
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

class Splash {

public:
        Splash() { };
        ~Splash() { };

        static void SetTexture();
        static void ShowSplash(sf::RenderWindow&);

private:
        static sf::Texture _splashTexture;
        static sf::Sprite _splashSprite;
};

splash.cpp
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include "splash.h"
#include "loop.h"

void Splash::SetTexture() {
        if(!_splashTexture.loadFromFile("splash.png")) {
                std::cout << "Splash Texture failed to load!";
                return;
        } else {
                _splashSprite.setTexture(_splashTexture);
        }
}

void Splash::ShowSplash(sf::RenderWindow& window) {
       
        SetTexture();
       
        while(window.isOpen()) {
                sf::Event happened;

                window.clear();

                while(window.pollEvent(happened)) {
                        if(happened.type == sf::Event::Closed)
                                window.close();
                        if(happened.type == sf::Event::KeyPressed && happened.key.code == sf::Keyboard::Escape)
                                window.close();
                        if(happened.type == happened.KeyReleased && happened.key.code >= 0)
                                std::cout << happened.key.code << ": EXIT SPLASH";
                                return;
                }

                std::cout << "Splash. ";
                window.draw(_splashSprite);
                window.display();
        }
}

sf::Texture Splash::_splashTexture;
sf::Sprite Splash::_splashSprite;

As I said, this is just my attempt to get the splash screen working and at the moment it's probably wrong in a number of places and in a number of ways (which I am happy to hear too).

Until I mouse over the SFML window the command window prints "Splash!" over and over. As soon as I mouse over it stops and returns to loop.cpp. If the window loads under the mouse pointer it never starts to "Splash!" The cout to get the key code doesn't print anything when the program switches to the loop either.

Can someone help my by explaining why? I accept this is almost certainly not the way to do what I want, but it also feels like, even if it's wrong, it shouldn't be wrong in this way. If that makes sense.

Thanks for any help.

Pages: [1]
anything