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

Pages: [1] 2
1
General / Does SFML check errors ?
« on: September 08, 2018, 12:14:45 pm »
Greetings everyone,

I would like to know if SFML2 automatically checks if the window and renderer successfully initialized like you could do with SDL2 by checking if a pointer is null and then send a message via the stderr flux ?

Is there a built-in "check-error" function that does it in SFML2 ?

Thanks

2
General / Re: Good way to create a menu ?
« on: June 26, 2018, 09:27:14 pm »
Thanks for your answer,

Where can I learn more about this state machine design concept then ? It would be really interesting

3
General / Re: Good way to create a menu ?
« on: June 25, 2018, 09:59:02 pm »
Thanks for your answers,

Yes I mean a game menu with text and a cursor like I did in my project that you can download.
Nothing relating to an OS menu, just a game menu.

So, how would you handle the events for both the menu and then the game ?

4
General / Good way to create a menu ?
« on: June 24, 2018, 03:39:34 pm »
Hello everyone !

Here is a small project I have written :

https://ufile.io/pjlv0

You will find all the source code above.

I'm trying to create a main menu but I'm not sure the logic is good, what is the right way to create a main menu (and submenus) with SFML ? Because I don't see how to write "clean" code where there would be event handling for the menu and then event handling for the main game.

My current logic is to create a Game object where the window is opened and destroyed when the user exits the window.

Regards

5
Audio / Re: Play a sound when moving a ship
« on: May 17, 2018, 03:32:58 pm »
Up !

6
Audio / Re: Play a sound when moving a ship
« on: May 15, 2018, 12:31:15 am »
Thanks for your answer eXpl0it3r,

I'd just like one sound to be played when the ship moves (every positions).

Here is a method I wrote for the Ship class :

void Ship::move_sound(sf::Sound *sound)
{
        if(isSoundPlaying == false)
        {
                sound->play();
                isSoundPlaying = true;
        }
        else
        {
                sound->stop();
                isSoundPlaying = false;
        }
}

isSoundPlaying is a boolean that is initialized to false when the Ship object is created. Then I put :

player->move_sound();

inside each if statements but it doesn't give me the loop effect I'd like to have.

I'd like that the ship sound (1 second) would repeat each time it reaches the end of its loop and that either Up, Down, Left or Right is pressed (at least).

7
Audio / Play a sound when moving a ship
« on: May 14, 2018, 11:28:49 pm »
Hello,

I'd like to play a sound when my ship is moving. Where can I call "ship_sound.play()" ? I tried inside the sf::Keyboard::IsKeyPressed() but it plays the sound and restart it at the same time (which is logic)

void Game::run()
{
        window.create(sf::VideoMode(800,600), "Test", sf::Style::Default);
                window.setVerticalSyncEnabled(true);
               
        sf::SoundBuffer buffer;
       
        if(!buffer.loadFromFile("ship.ogg"))
        {
                std::cerr << "Failed to load ship sound" << std::endl;
        }
        else
        {
                std::unique_ptr<Ship> player = std::make_unique<Ship>(10,10,40);
               
                sf::Sound ship_sound;
                        ship_sound.setBuffer(buffer);
       
                while(window.isOpen() == true)
                {
                        sf::Event ev;
                       
                        while(window.pollEvent(ev) == true)
                        {
                                if(ev.type == sf::Event::Closed)
                                {
                                        window.close();
                                }
                                if(ev.type == sf::Event::KeyPressed)
                                {
                                        if(ev.key.code == sf::Keyboard::Escape)
                                        {
                                                window.close();
                                        }
                                }
                        }
                       
                        //Left : If only Left Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                //Left rotation: If Left + Either Down or Up are pressed
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
                                {
                                        player->rotate(10);
                                }
                                else
                                {
                                        player->move_x(-10);
                                }
                        }
                       
                        //Right : If only Right Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                //Right rotation : If Right + Either Down or Up are pressed
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
                                {
                                        player->rotate(-10);
                                }
                                else
                                {
                                        player->move_x(10);
                                }
                        }

                        //Up : If only Up Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == false && sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == false))
                        {
                                player->move_y(-10);
                        }
                       
                        //Down : If only Down Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == false && sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == false))
                        {
                                player->move_y(10);
                        }
                       
                        window.clear(sf::Color::Black);
                        player->drawOn(&window);
                        window.display();
                }
        }
}

Regards

8
General / Re: Handle multiple pressed Keys in a switch statement
« on: May 14, 2018, 02:17:35 pm »
"Outside the event loop" -> Got it !  ;)

void Game::run()
{
        window.create(sf::VideoMode(800,600), "Test", sf::Style::Default);
                window.setVerticalSyncEnabled(true);
               
        sf::SoundBuffer buffer;
       
        if(!buffer.loadFromFile("ship.ogg"))
        {
                std::cerr << "Failed to load ship sound" << std::endl;
        }
        else
        {
                std::unique_ptr<Ship> player = std::make_unique<Ship>(10,10,40);
               
                sf::Sound ship_sound;
                        ship_sound.setBuffer(buffer);
       
                while(window.isOpen() == true)
                {
                        sf::Event ev;
                       
                        while(window.pollEvent(ev) == true)
                        {
                                if(ev.type == sf::Event::Closed)
                                {
                                        window.close();
                                }
                                if(ev.type == sf::Event::KeyPressed)
                                {
                                        if(ev.key.code == sf::Keyboard::Escape)
                                        {
                                                window.close();
                                        }
                                }
                        }
                       
                        //Left : If only Left Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                //Left rotation: If Left + Either Down or Up are pressed
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
                                {
                                        player->rotate(10);
                                }
                                else
                                {
                                        player->move_x(-10);
                                }
                        }
                       
                        //Right : If only Right Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                //Right rotation : If Right + Either Down or Up are pressed
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
                                {
                                        player->rotate(-10);
                                }
                                else
                                {
                                        player->move_x(10);
                                }
                        }

                        //Up : If only Up Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == false && sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == false))
                        {
                                player->move_y(-10);
                        }
                       
                        //Down : If only Down Key is pressed
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == false && sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == false))
                        {
                                player->move_y(10);
                        }
                       
                        window.clear(sf::Color::Black);
                        player->drawOn(&window);
                        window.display();
                }
        }
}

It's smooth now !

9
General / Re: Handle multiple pressed Keys in a switch statement
« on: May 14, 2018, 01:21:17 am »
Great this time it works for good !

//Left : If Left Key is pressed
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
        //Left rotation: If Left + Either Down or Up are pressed
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
        {
                player->rotate(10);
        }
        else
        {
                player->move_x(-10);
        }
}

//Right : If Right Key is pressed
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
        //Right rotation : If Right + Either Down or Up are pressed
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
        {
                player->rotate(-10);
        }
        else
        {
                player->move_x(10);
        }
}

//Up : If only Up Key is pressed
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == false && sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == false))
{
        player->move_y(-10);
}

//Down : If only Down Key is pressed
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == false && sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == false))
{
        player->move_y(10);
}

How can I make the game smoother ? Because it doesn't feel that smooth when I play with the ship : I'd like to move the ship with just the key pressed one time without that repeated effect (kind of delay between movements)

10
General / Re: Handle multiple pressed Keys in a switch statement
« on: May 13, 2018, 03:52:10 pm »
Ahah yeah you are right !

I reviewed my code, here are the changes :

#include <iostream>
#include <SFML/Graphics.hpp>
#include <memory>
#include "Game.hpp"
#include "Ship.hpp"

Game::Game()
{
        std::cout << "Object created : Game" << std::endl;
}

Game::~Game()
{
        std::cout << "Object destroyed : Game" << std::endl;
}

void Game::run()
{
        window.create(sf::VideoMode(800,600), "Test", sf::Style::Default);
                window.setVerticalSyncEnabled(true);
       
        std::unique_ptr<Ship> player = std::make_unique<Ship>(10,10,40);
       
        while(window.isOpen() == true)
        {
                sf::Event ev;
               
                while(window.pollEvent(ev) == true)
                {
                        if(ev.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                        if(ev.type == sf::Event::KeyPressed)
                        {
                                //Escape
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                                {
                                        window.close();
                                }
                                //Left + !(Up && Down)
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) == false)
                                {
                                                player->move_x(-10);
                                }
                                //Left + (Up && Down)
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) == true)
                                {
                                                player->rotate(10);
                                }
                                //Right + !(Up && Down)
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) == false)
                                {
                                                player->move_x(10);
                                }
                                //Right + (Up && Down)
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) == true )
                                {
                                                player->rotate(10);
                                }
                                //Down + !(Left && Right)
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) == false)
                                {
                                                player->move_y(10);
                                }
                                //Up + !(Left && Right)
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) == false )
                                {
                                                player->move_y(-10);
                                }
                               
                        }
                }
               
                window.clear(sf::Color::Black);
                player->drawOn(&window);
                window.display();
        }
}
 

Now it works, but the idea would be to to rotations and not to move on the left or the right. How can I do it ?

11
General / Re: Handle multiple pressed Keys in a switch statement
« on: May 12, 2018, 10:46:00 pm »
Thanks for your answer Laurent,

I changed my code but there is no rotations when I simultaneously press Up and Left for example.

sf::Event ev;
               
                while(window.pollEvent(ev) == true)
                {
                        if(ev.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                        if(ev.type == sf::Event::KeyPressed)
                        {
                               
                                switch(ev.key.code)
                                {
                                        case(sf::Keyboard::Escape):
                                                window.close();
                                                break;
                                        case(sf::Keyboard::Up):
                                                player->move_y(-10);
                                                break;
                                        case(sf::Keyboard::Down):
                                                player->move_y(10);
                                                break;
                                        case(sf::Keyboard::Left):
                                                player->rotate(10);
                                                break;
                                        case(sf::Keyboard::Right):
                                                player->move_x(10);
                                                break;
                                }
                                if((ev.key.code == sf::Keyboard::Up) && (ev.key.code == sf::Keyboard::Left))
                                {
                                                player->rotate(10);
                                }
                                if((ev.key.code == sf::Keyboard::Down) && (ev.key.code == sf::Keyboard::Right))
                                {
                                                player->rotate(10);
                                }
                        }
                }

12
General / Handle multiple pressed Keys in a switch statement
« on: May 12, 2018, 05:28:39 pm »
Hello !

I'd like to design a little game in which, a little ship (a Triangle) would move on the screen.
The problem is that I'd like to make the ship rotate when multiple keys are pressed (see below).

But the compiler refuses :

make all (in directory: /home//Desktop/C++)
g++ -c main.cpp Game.cpp Ship.cpp
Game.cpp: In member function ‘void Game::run()’:
Game.cpp:56:6: error: duplicate case value
      case((sf::Keyboard::Down) && (sf::Keyboard::Right)):
      ^~~~
Game.cpp:53:6: note: previously used here
      case((sf::Keyboard::Up) && (sf::Keyboard::Left)):
      ^~~~
Makefile:8: recipe for target 'all' failed
make: *** [all] Error 1
Compilation failed.
 

void Game::run()
{
        window.create(sf::VideoMode(800,600), "Test", sf::Style::Default);
                window.setVerticalSyncEnabled(true);
       
        std::unique_ptr<Ship> player = std::make_unique<Ship>(10,10,40);
       
        while(window.isOpen() == true)
        {
                sf::Event ev;
               
                while(window.pollEvent(ev) == true)
                {
                        if(ev.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                        if(ev.type == sf::Event::KeyPressed)
                        {
                                switch(ev.key.code)
                                {
                                        case(sf::Keyboard::Escape):
                                                window.close();
                                                break;
                                        case(sf::Keyboard::Up):
                                                player->move_y(-10);
                                                break;
                                        case(sf::Keyboard::Down):
                                                player->move_y(10);
                                                break;
                                        case(sf::Keyboard::Left):
                                                player->move_x(-10);
                                                break;
                                        case(sf::Keyboard::Right):
                                                player->move_x(10);
                                                break;
                                        case((sf::Keyboard::Up) && (sf::Keyboard::Left)):
                                                player->rotate(10);
                                                break;
                                        case((sf::Keyboard::Down) && (sf::Keyboard::Right)):
                                                player->rotate(-10);
                                                break;
                                        default:
                                                break;
                                }
                        }
                }
               
                window.clear(sf::Color::Black);
                player->drawOn(&window);
                window.display();
        }
}

Regards

13
General / Re: Can't delete objects without memory dumping
« on: May 12, 2018, 04:26:54 pm »
Thank you guys ! It works now !

The syntax I used is :

std::unique_ptr<Player> player = std::make_unique<Player>(360,520,40);
std::unique_ptr<Obstacle> obstacle = std::make_unique<Obstacle>();

And I changed the functions prototypes :

Before :

bool Game::isCollision(Player *player, Obstacle *obstacle)

After :

bool Game::isCollision(std::unique_ptr<Player> &player, std::unique_ptr<Obstacle> &obstacle)

I did try with destructors in the .hpp files to print a message when the objects are destroyed and indeed it works perfectly !

14
General / Re: Can't delete objects without memory dumping
« on: May 12, 2018, 12:14:21 pm »
Yes indeed ! I'm just very interesed in computer programing, never studied it at school but find it so cool. So that's great news, because I was learning some old tutorials (written when I was a teenager ahah) that's why it's a bit outdated.

Anyway, the point is that since the C++11 standard, we can use :

Foo *foo = new Foo("Foo",4,4);

And don't need to call  :

Foo *foo = new Foo("Foo",4,4);
delete foo();

?

Regards

15
General / Re: Can't delete objects without memory dumping
« on: May 12, 2018, 01:21:56 am »
Thanks for your answer pw90 !

Really ? Ok then it's great ! Because I thought that in C++ everytime you create an object with a pointer you have to delete it manually.

So what you mean is that when my program exits the SFML automatically handles the deletions ?

Pages: [1] 2
anything