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 - sleeter

Pages: [1]
1
The style you prefer, Nexus, is the one I've used above, but I missed the necessary braces which fixed my code nicely. :-)

I'm going to try to force myself to use braces going forward. There seems no real downside but quite a good reason to do so.

2
I probably should, yes. But it looks neater to me to not use them when the situation doesn't need braces. Would have saved me a red face here though.

if(foo == bar) FooBar();

Just looks neater and nicer than:

if(foo == bar) {
    FooBar();
}

Or as I see a lot:

if(foo == bar)
{
    FooBar();
}

Is there a code-related reason to use the third format over the second? From what I've read and seen so far it seems like it's just style preference.

3
Thanks a lot Eigenbom! I completely failed to spot that the return was outside braces so triggered for every action on the window.  :-[

Thanks also for the advice Omega, I've moved my window event handling to one place and created a switch to handle what specific thing to do in each state.

Thanks for the help. I knew it would be a bone-head mistake on my part. :)

4
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]