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

Pages: [1]
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 / 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

3
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

4
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

5
General / Can't delete objects without memory dumping
« on: May 12, 2018, 12:51:37 am »
Hello everyone !

I'm currently learning SFML and C++.

I've created a little game, but the fact is I would like to delete the Player object and Obstacle object when the program is closed. I tried not to use pointers but the compiler says "obstacle.fall(10) : fall is not of type Obstacle()". And if I use pointers for dynamic allocation, it works great but when it comes to delete the objects, I have a memory error.

The delete instructions are written in Game::endGame().

Source code (just the main.cpp and Game.cpp files in order to keep things simple and clear):

main.cpp

#include <iostream>
#include <SFML/Graphics.hpp>
#include "Game.hpp"
#include "Player.hpp"
#include "Obstacle.hpp"

int main()
{
        sf::RenderWindow window(sf::VideoMode(800,600), "Test", sf::Style::Fullscreen);
                window.setVerticalSyncEnabled(true);
               
        Game *game = new Game();
       
        if(game->displayMenu(&window) != 0)
        {
                std::cerr << "Exiting the game..." << std::endl;
        }
       
        return 0;
}
 

Game.cpp

#include <iostream>
#include <SFML/Graphics.hpp>
#include "Game.hpp"
#include "Player.hpp"
#include "Obstacle.hpp"

Game::Game()
{
}

int Game::startGame(sf::RenderWindow *window)
{
        window->clear(sf::Color::Black);
       
        Player *player = new Player(360,520,40);
        Obstacle *obstacle = new Obstacle();
                                               
                        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::Left):
                                                                player->move(-20);
                                                                break;
                                                        case(sf::Keyboard::Right):
                                                                player->move(20);
                                                                break;
                                                       
                                                        default:
                                                                break;
                                                }
                                        }
                                }
                               
                                obstacle->fall(10);
                               
                                obstacle->isInWindow();
                                player->isInWindow();
                               
                                        if(isCollision(player,obstacle) == true)
                                        {
                                                if(lifes > 0)
                                                {
                                                        lifes -= 1;
                                                }
                                                else
                                                {
                                                        endGame(window, player, obstacle);
                                                }
                                        }
                                       
                               
                                obstacle->reset();
                               
                                window->clear(sf::Color::Black);
                                player->draw(window);
                                obstacle->draw(window);
                               
                                window->display();
                               
                                }
                        }
}

void Game::endGame(sf::RenderWindow *window, Player *player, Obstacle *obstacle)
{
        window->clear(sf::Color::Black);
       
        while(window->isOpen() == true)
        {
                sf::Event ev;
               
                while(window->pollEvent(ev) == true)
                {
                        if(ev.type == sf::Event::Closed)
                        {
                                window->close();
                                delete &player;
                                delete &obstacle;
                        }
                        if(ev.type == sf::Event::KeyPressed)
                        {
                                switch(ev.key.code)
                                {
                                        case(sf::Keyboard::Escape):
                                                window->close();
                                                delete &player;
                                                delete &obstacle;
                                                break;
                                        case(sf::Keyboard::Return):
                                                startGame(window);
                                                break;
                                       
                                        default:
                                                break;
                                }
                        }
                }
        }
       
        window->close();
}
 

Regards

Pages: [1]