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

Author Topic: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)  (Read 68662 times)

0 Members and 1 Guest are viewing this topic.

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #45 on: February 21, 2014, 07:13:04 pm »
A little update (and a question for help :D)

i managed to get the input/output stuff into 2 files (one .h and one .cpp) but now i have problems with the music

this is how my main.cpp looks like in this moment:
//main.cpp

#include <vector>
#include <iostream>
#include "StateManager.h"
#include "WindowStates.h"
#include "IOstuff.h"
#include "Music.h"

int main()
{

        //StateManager IDs
        std::vector<StateManager*> states;
        int screenState = 0;

        //create window
        IOscreen ioscreen;
       
        sf::RenderWindow window;
        window.setFramerateLimit(60);
       
        //set window style
        if (ioscreen.getScreenSettings())
        {
                window.create(sf::VideoMode(800, 600, 32), "Pew", sf::Style::Fullscreen);
                window.setMouseCursorVisible(false);
        }
        else
        {
                window.create(sf::VideoMode(800, 600, 32), "Pew", sf::Style::Default);
        }

        //Music

        int volume;
        int volumeCheck = NULL; //will later be interesting when i change the volume i want to keep that updated
        IOsound iosound;
        iosound.ReadSoundSettings(volume);
        IngameMusic ingMusic;
        ingMusic.LoadMusic(volume);
        MenuMusic       menMusic;
        menMusic.LoadMusic(volume);
        menMusic.PlayMusic("menusong");

        //states
        Menu state0;
        states.push_back(&state0);
        Intro state1;
        states.push_back(&state1);
        Settings state2;
        states.push_back(&state2);
        Game state3;
        states.push_back(&state3);
        GraphicSet state4;
        states.push_back(&state4);
        Highscore state5;
        states.push_back(&state5);
        SoundSet state6;
        states.push_back(&state6);
        DiffSet state7;
        states.push_back(&state7);
        CoopSet state8;
        states.push_back(&state8);

        //runs states & switches between them
        while (screenState >= 0)
        {
                //main loop
                screenState = states[screenState]->Run(window);

                std::cout << screenState << std::endl;

                //when screen changes
                if (screenState == 5)//to give the highscore
                {
                        state5.setHighscoreManager(state3.getHighScore());
                }

                else if (screenState != 3)//to clear the game
                {
                        state3.ClearStuff();
                }

                //Control Music
                if (screenState == 1 || screenState == 3)
                {
                        menMusic.Pause("menu");
                }
                if (screenState == 1)
                {
                        menMusic.Pause("menu");
                        menMusic.PlayMusic("introsong");
                }
                if (screenState != 3)
                {
                        ingMusic.Pause();
                }
                if (screenState == 3)
                {
                        menMusic.Pause("intro");
                        ingMusic.PlayMusic();
                }
                if (screenState != 3 || screenState != 1)
                {
                        menMusic.Pause("intro");
                        menMusic.UnpauseMenu();
                }
        }
        return 0;
}

so here is the problem:
when the screenState changes, the music does not stop like i planned it. any suggestions? :D if i find a solution i will keep you updated ;)
___________________________
I checked it again and came to the result, that only the menu music does not stop
« Last Edit: February 21, 2014, 07:18:01 pm by nebula »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #46 on: February 22, 2014, 01:49:46 am »
i could fix it by deleting my resume function.. seems to cause issues somehow...
but nevermind. now i got the changes i wanted.. well almost likely.

the only problem is that as soon as you hit the main menu again the menu theme starts playing again.
but before that was normal every time someone changed the screenstate, so there is progress :D

tomorrow i will make a new video for the main page... the one here is not very up to date^^

i uploaded the changes as always ;)

______________________________________________________

cl15:
- codechanges:
  ->input/output summed up to IOstuff
  ->music is now controlled in the main.cpp
- music will now play correctly in menu,
  as long as you are not again in main menu
______________________________________________________

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #47 on: February 28, 2014, 10:58:17 pm »


time to do the third boss :D



as you can see my painting skills did not really improve :'D but hey, this enemy will have 3 states.

1.)
  • some nonesence flying
  • shooting heads of cows
  • only attackable at left or right "spawner"
2.)
  • still shitty movement :D
  • mid "spawner" shows up to shoot with some crazy cows
  • only mid "spawner" is attackable (the first ones are now away)
3.)
  • some really nonsence movement
  • mid spawner is broken
  • mashine is attackable
  • mashine tries to hit player with collision

well, that is how i imagined it... kinda :D you'll see
__________________________________________
after that enemy, the following will happen
  • ending for game will be made (for a feeling of success :P)
  • highscore
  • pool of buffs (one you get after you finished the game successfully)
  • credits
  • More enemies
  • and more

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #48 on: March 22, 2014, 07:06:57 pm »
Finally the third boss is at a state where i can release it :D

here a video:


changes...
cl16:
- third boss added
  -> includes 3 states
  -> 1 - cowheads; 2 - cows; 3 - mashine
- pew sound (third weapon) changed


as always, the release is at the main post ;)

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #49 on: March 22, 2014, 07:48:39 pm »
there seems to be a problem with playing the .ogg files.
pew is not played anymore (changed the file)
when the third boss dies, sound is not played
when cows spawn (the normal ones) no sound is played...

i will put this into the forum maybe someone has the solution :D

therocode

  • Full Member
  • ***
  • Posts: 125
    • View Profile
    • Development blog
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #50 on: March 23, 2014, 02:42:40 pm »
Haha that is awesome!

I like the cow boss graphics, and the part when it starts spinning around. :D

The sound effects also give a nice retro feel. Keep it up!

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #51 on: March 23, 2014, 03:25:56 pm »
thanks :D

i will make more :P

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #52 on: March 23, 2014, 04:01:46 pm »
Sound should work now. Updated everything ;)

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #53 on: June 14, 2014, 10:32:54 pm »
hi,

it's been a while :D i had just no time left to keep my project going :/

today, as i saw my code i wanted to structure it right up from the beginning, because even i had problems with finding back into it.

This got me to a problem and i can't find the mistake :( maybe someone can help me :D

first i will post some code and then tell you what exactly happens.

//main.cpp

#include "Game.h"

int main()
{
        Game pew;
        pew.ChangeState(Game::gameStates::MAINMENU);

        pew.Run();

        return 0;
}
 

//Game.h

#ifndef GAME_H
#define GAME_H

#include <iostream>
#include <memory>

#include "GameState.h"
#include "MainMenuState.h"
#include "PlayState.h"
#include "Intro.h"

class Game
{
public:
         Game();
        ~Game();

        enum class gameStates{ MAINMENU, INTRO, SETTINGS, PLAY, GRAPHICSET, SOUNDSET, DIFFSET, COOPSET, HIGHSCORE };

        void Run();
        void ChangeState(gameStates newState);
        bool isRunning(){ return running; };
        void setRunning(bool mRunning);
       
        sf::RenderWindow window;

private:

        void Quit();
        void Update();
        void HandleEvents();
        void Render();

        std::unique_ptr<GameState> CurrentState;
       
        bool running;
};

#endif
 

//Game.cpp

#include "Game.h"

Game::Game()
{
        running = true;

        window.create(sf::VideoMode(800, 600), "Pew");
        window.setFramerateLimit(60);
        window.setVerticalSyncEnabled(true);
}

Game::~Game()
{
        CurrentState = nullptr;
}

void Game::Run()
{
        while (running)
        {
                Update();
                HandleEvents();
                Render();
                Quit();        
        }
}

void Game::ChangeState(gameStates newState)
{
        switch (newState)
        {
        case gameStates::MAINMENU:
                CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState));
                break;
        case gameStates::PLAY:
                CurrentState = std::move(std::unique_ptr<PlayState>(new PlayState));
                break;
        case gameStates::INTRO:
                CurrentState = std::move(std::unique_ptr<Intro>(new Intro));
                break;
        }
}

void Game::setRunning(bool mRunning)
{
        running = mRunning;
}

void Game::Quit()
{
        if (!running)
                window.close();
}
void Game::Update()
{
        CurrentState->Update(*this);
}
void Game::HandleEvents()
{
        CurrentState->HandleEvents(*this);
}
void Game::Render()
{
        window.clear();
        CurrentState->Render(*this);
        window.display();
}
 

//MainMenuState.h

#ifndef MAINMENUSTATE_H
#define MAINMENUSTATE_H

#include "Game.h"
#include "MenuSfx.h"
#include "IOstuff.h"

class MainMenuState : public GameState
{
public:
        MainMenuState();
        ~MainMenuState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Render(Game &game);
private:
        IOsound iosound;
        MenuSound sound;

        bool  playing;
        float speed;
        float elapsedTime;
        float x_movement;
        float y_movement;
        float x, y;
        short int   debauch;
        short int   selection;
        int   volume;

        sf::Event   *pEvent;
        sf::Clock   *pClock;
        sf::Texture *texture;
        sf::Sprite  *sprite;

        Background bg;
        Text play, again, settings, close;
};

#endif
 

//MainMenuState.cpp

#define PI 3.14159
#include "MainMenuState.h"
#include <cmath>

MainMenuState::MainMenuState()
{
        bg.setFilePath("graphics//core//menu.png");
       
        //sound & music
        sound.LoadSoundBuffer();
        sound.setBuffer(volume);

        playing = false;
        speed = 0.5;
        selection = 0;
        iosound.ReadSoundSettings(volume);

        pEvent = new sf::Event;
        pClock = new sf::Clock;
        texture = new sf::Texture;
        sprite = new sf::Sprite;

        //Enemy
        elapsedTime = 0;
        x_movement = 0;
        y_movement = 0;
        debauch = 200;
        texture->loadFromFile("graphics//enemies//enemy.png");
        texture->setSmooth(false);
        sprite->setTexture(*texture);
        sprite->setOrigin(36.5, 35);

        //buttons

        play.setStringAndSize("play", 70);
        again.setStringAndSize("again", 70);
        settings.setStringAndSize("settings", 70);
        close.setStringAndSize("close", 70);

        play.setPosition(270, 150);
        again.setPosition(270, 150);
        settings.setPosition(270, 250);
        close.setPosition(270, 350);
}

MainMenuState::~MainMenuState()
{
        delete pEvent;
        delete pClock;
        delete sprite;
        delete texture;

        pEvent =  nullptr;
        pClock =  nullptr;
        sprite = nullptr;
        texture = nullptr;

}

void MainMenuState::HandleEvents(Game &game)
{
        while (game.window.pollEvent(*pEvent))
        {
                if (pEvent->type == sf::Event::Closed)
                        game.setRunning(false);

                //Keyboard selection
                if (pEvent->type == sf::Event::KeyPressed)
                {
                        switch (pEvent->key.code)
                        {
                        case sf::Keyboard::Up:
                                if (selection > 0)
                                {
                                        selection -= 1;
                                        sound.PlaySound("select");
                                }
                                else
                                        selection = 0;
                                break;

                        case sf::Keyboard::Down:
                                if (selection < 2)
                                {
                                        selection += 1;
                                        sound.PlaySound("select");
                                }
                                else
                                        selection = 2;
                                break;

                        case sf::Keyboard::Return:
                                if (selection == 0)
                                {
                                        if (playing)
                                                game.ChangeState(Game::gameStates::PLAY);
                                        else
                                                playing = true;
                                                game.ChangeState(Game::gameStates::INTRO);
                                }
                                else if (selection == 1)
                                        game.ChangeState(Game::gameStates::SETTINGS);
                                else
                                        game.setRunning(false);
                                break;
                        default:
                                break;
                        }
                }
        }
}

void MainMenuState::Update(Game &game)
{
        if (selection == 0)//Spielen
        {
                play.setColor(sf::Color(255, 128, 0));
                again.setColor(sf::Color(255, 128, 0));
                settings.setColor(sf::Color(255, 255, 255));
                close.setColor(sf::Color(255, 255, 255));
        }
        else if (selection == 1)//Settings
        {
                play.setColor(sf::Color(255, 255, 255));
                again.setColor(sf::Color(255, 255, 255));
                settings.setColor(sf::Color(255, 128, 0));
                close.setColor(sf::Color(255, 255, 255));
        }
        else//Beenden
        {
                play.setColor(sf::Color(255, 255, 255));
                again.setColor(sf::Color(255, 255, 255));
                settings.setColor(sf::Color(255, 255, 255));
                close.setColor(sf::Color(255, 128, 0));
        }

        //moving enemy
        elapsedTime = pClock->restart().asMilliseconds();
        x_movement += elapsedTime;
        y_movement += elapsedTime;
        y = sprite->getPosition().y;
        x = sprite->getPosition().x;
        y = 300 + std::sin((y_movement * PI) / 180) * debauch;
        x = 400 + std::cos((x_movement * PI) / 180) * debauch;

        if (x_movement > 360)
                x_movement = 0;

        if (y_movement > 360)
                y_movement = 0;

        sprite->setPosition(x, y);
}

void MainMenuState::Render(Game &game)
{
        bg.Render(game.window);
        game.window.draw(*sprite);

        if (playing)
                again.Render(game.window);
        else
                play.Render(game.window);

        settings.Render(game.window);
        close.Render(game.window);
}
 



Soooooo, this is a lot code, but i wanted to make sure you got everything you need :D
the error accures, if i want to change the gamestate. here is a screenshot of the debug output:




thank you for your help

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #54 on: June 14, 2014, 11:07:28 pm »
Just quickly overlooked the code and saw in the game.cpp in changeState one error

this line:
CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState));

should be

CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState()));

you forgot the ()


AlexAUT

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #55 on: June 14, 2014, 11:15:07 pm »
Just quickly overlooked the code and saw in the game.cpp in changeState one error

this line:
CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState));

should be

CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState()));

you forgot the ()


AlexAUT

thanks for your reply ;)
i changed it, but sadly this does not solve the problem :'(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #56 on: June 14, 2014, 11:19:41 pm »
new X and new X() are perfectly equivalent. This was not an error.

To the OP: you should have opened a new thread. Now you're requesting help, you're not adding to your project's presentation. And you should show the call stack, so that we know exactly where it crashes.
Laurent Gomila - SFML developer

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #57 on: June 14, 2014, 11:40:07 pm »
new X and new X() are perfectly equivalent. This was not an error.

To the OP: you should have opened a new thread. Now you're requesting help, you're not adding to your project's presentation. And you should show the call stack, so that we know exactly where it crashes.

sorry for that, i opened a new thread ;)

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #58 on: June 15, 2014, 01:09:43 am »
new X and new X() are perfectly equivalent. This was not an error.

To the OP: you should have opened a new thread. Now you're requesting help, you're not adding to your project's presentation. And you should show the call stack, so that we know exactly where it crashes.

Just nitpicking here but they are not necessarily the same depending on whether X is a POD or not.
"Lasciate ogni speranza, voi ch'entrate"

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #59 on: July 19, 2014, 05:13:36 pm »
Soooo,

i managed to get a better state handling running, for the end user this mainly just brings 3 improvements:
1. intro starts when it should,
2. if you play, go to the menu and go back to play, nothing from the game before is there
3. sound is now changed correctly when you change it in the settings, so you can hear how loud it is without having to go back to control it.

further the pause function is now on escape, and not on "P" as it was.

You can get the release at the mainpage as always.
By the way. i have holiday soon and will bring some improvements to the game like more content and functionalities.

See ya ;)