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

Pages: [1]
1
General / access violation with unique pointer (c++)
« on: June 14, 2014, 11:40:54 pm »
hi,
i am working on a 2D shooter. As i wanted to rework the code because its just spaghetti, i caused an error i cant fix myself and hope that someone of you guys can help me ;)

i will post the code that i think will be relevant for solving the issue. if you need something more, please tell me.

//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);
}
 



as the picture is german, let me say that right to _Myptr and running it says <could not read memory>
the right table is the call stack.

the debug output says this:
Quote
Ausnahme (erste Chance) bei 0x50024EBE (sfml-window-2.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Schreiben an Position 0xFEEEFEEE
Ausnahmefehler bei 0x50024EBE (sfml-window-2.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Schreiben an Position 0xFEEEFEEE

Das Programm "[2032] Pew ReWork.exe" wurde mit Code 0 (0x0) beendet.

means something like: exception error at 0x50024EBE (sfml-window-2.dll) in Pew ReWork.exe: 0xC0000005: access violation when writing at position  0xFEEEFEEE
(freely translated, my english is a bit rusty)

thank you for your help

2
Audio / SFML 2.1 some sounds do not play
« on: March 22, 2014, 07:57:36 pm »
so, the problem is, some sounds are played and some are not...

it is regarding to my project which is also here in this forum:
http://en.sfml-dev.org/forums/index.php?topic=13453.0
if you get the release and play it, you will notice that:
if a big cow is spawning there is no sound
if you get the third weapon there is no sound
if the third boss dies there is no sound
every other sound works

here my code for the sound:
//IngameSfx.h

#ifndef INGAMESFX_H
#define INGAMESFX_H

#include <SFML/Audio.hpp>
#include <string>

class IngameSound
{
public:

        void LoadSoundBuffer();
        void setBuffer(int &volume);
        void PlaySound(std::string sound);

private:

        sf::SoundBuffer bossDeathBuffer;
        sf::SoundBuffer bulletShotBuffer;
        sf::SoundBuffer enemyCollisionBuffer;
        sf::SoundBuffer healthDropBuffer;
        sf::SoundBuffer playerCollisionBuffer;
        sf::SoundBuffer playerDeathBuffer;
        sf::SoundBuffer monkeyFartBuffer;
        sf::SoundBuffer pewBuffer;
        sf::SoundBuffer boss1HitBuffer;
        sf::SoundBuffer cowBuffer;
        sf::SoundBuffer boss3spawn;
        sf::SoundBuffer boss3death;

        sf::Sound bossDeathSound;
        sf::Sound bulletShotSound;
        sf::Sound enemyCollisionSound;
        sf::Sound healthDropSound;
        sf::Sound playerCollisionSound;
        sf::Sound playerDeathSound;
        sf::Sound monkeyFartSound;
        sf::Sound pewSound;
        sf::Sound boss1HitSound;
        sf::Sound cowSound;
        sf::Sound boss3spawnSound;
        sf::Sound boss3deathSound;
};

#endif

//IngameSfx.cpp

#include "IngameSfx.h"

//Sound
void IngameSound::LoadSoundBuffer()
{

        bossDeathBuffer.loadFromFile("audio//bossDeath.ogg");
        bulletShotBuffer.loadFromFile("audio//bulletShot.ogg");
        enemyCollisionBuffer.loadFromFile("audio//enemyCollision.ogg");
        healthDropBuffer.loadFromFile("audio//healthDrop.ogg");
        playerCollisionBuffer.loadFromFile("audio//playerCollision.ogg");
        playerDeathBuffer.loadFromFile("audio//playerDeath.ogg");
        monkeyFartBuffer.loadFromFile("audio//fart.ogg");
        pewBuffer.loadFromFile("audio//pew.ogg");
        boss1HitBuffer.loadFromFile("audio//boss1hit.ogg");
        cowBuffer.loadFromFile("audio//cow.ogg");
        boss3spawn.loadFromFile("audio//boss3spawn.ogg");
        boss3death.loadFromFile("audio//boss3death.ogg");
}

void IngameSound::setBuffer(int &volume)
{
        bossDeathSound.setBuffer(bossDeathBuffer);
                bossDeathSound.setVolume(volume);
        bulletShotSound.setBuffer(bulletShotBuffer);
                bulletShotSound.setVolume(volume);
        enemyCollisionSound.setBuffer(enemyCollisionBuffer);
                enemyCollisionSound.setVolume(volume);
        healthDropSound.setBuffer(healthDropBuffer);
                healthDropSound.setVolume(volume);
        playerCollisionSound.setBuffer(playerCollisionBuffer);
                playerCollisionSound.setVolume(volume);
        playerDeathSound.setBuffer(playerDeathBuffer);
                playerDeathSound.setVolume(volume);
        monkeyFartSound.setBuffer(monkeyFartBuffer);
                monkeyFartSound.setVolume(volume);
        pewSound.setBuffer(pewBuffer);
                pewSound.setVolume(volume);
        boss1HitSound.setBuffer(boss1HitBuffer);
                boss1HitSound.setVolume(volume);
        cowSound.setBuffer(cowBuffer);
                cowSound.setVolume(volume);
        boss3spawnSound.setBuffer(boss3spawn);
                boss3spawnSound.setVolume(volume);
        boss3deathSound.setBuffer(boss3death);
                boss3deathSound.setVolume(volume);
}

void IngameSound::PlaySound(std::string sound)
{
        if (sound == "bulletShot")
        {
                bulletShotSound.play();
        }
        if (sound == "fart")
        {
                monkeyFartSound.play();
        }
        if (sound == "enemyCollision")
        {
                enemyCollisionSound.play();
        }
        if (sound == "playerCollision")
        {
                playerCollisionSound.play();
        }
        if (sound == "bossDeath")
        {
                bossDeathSound.play();
        }
        if (sound == "healthDrop")
        {
                healthDropSound.play();
        }
        if (sound == "playerDeath")
        {
                playerDeathSound.play();
        }
        if (sound == "pew")
        {
                pewSound.play();
        }
        if (sound == "boss1Hit")
        {
                boss1HitSound.play();
        }
        if (sound == "cow")
        {
                cowSound.play();
        }
        if (sound == "boss3spawn")
        {
                boss3spawnSound.play();
        }
        if (sound == "boss3death")
        {
                boss3deathSound.play();
        }
}

the sound is used in several classes. here some example:

works
void UpdateManager::BulletSpawn(int &counter, std::vector<Bullet> &vector, HighscoreManager &highscore, WeaponManager &weapon, Player &player, IngameSound &sound)
{
        if (counter >= 175 && weapon.bulletA)
        {
                Bullet bulletx;
                highscore.setShotsFired(1);
                bulletx.SetPosition(player.getPosition().x - 4, player.getPosition().y);
                vector.push_back(bulletx);
                sound.PlaySound("bulletShot");
                counter = 0;
        }
}

does not work
void UpdateManager::PewSpawn(std::vector<Pew> &vector, HighscoreManager &highscore, WeaponManager &weapon, Player &player, IngameSound &sound, bool &pewOnCooldown)
{
        if (weapon.pewA && !pewOnCooldown)
        {
                Pew pewx;
                highscore.setShotsFired(1);
                pewx.SetPosition(player.getPosition().x - 4, player.getPosition().y);
                vector.push_back(pewx);
                sound.PlaySound("pew");
                pewOnCooldown = true;
        }
}

maybe it has something to deal with the files?
if you get the release the files are all included.

thanks for any help ;)

3
Graphics / [C++ and SFML 2.1] Scrolling backgrounds ( y+) [SOLVED]
« on: November 09, 2013, 01:40:32 pm »
hey, i wanted to add a scrolling background to my game and now i have an issue with the background, as the title says :D (the jpg files are 800x600 just like the window)

i show you my code and screenshots so you can see whats going wrong

#ifndef GUIMOVABLEBACKGROUNDS_H
#define GUIMOVABLEBACKGROUNDS_H

#include <SFML\Graphics.hpp>
# include <iostream>

class MovableBackground
{
public:
        MovableBackground();
        void Update(sf::RenderWindow &window, float elapsedTime);
        void Render(sf::RenderWindow &window);
       
private:
        sf::Texture bg1Tex;
        sf::Texture bg2Tex;

        sf::Sprite      bg1Sprite;
        sf::Sprite      bg2Sprite;

        float bgSpeed;
        float bg1Y;
        float bg2Y;
};
#endif
 

GUImovableBackground.cpp
#include "GUImovableBackground.h"


MovableBackground::MovableBackground()
{
        bgSpeed = 0.1;

        bg1Tex.loadFromFile("graphics//background.jpg");
        bg1Tex.setSmooth(false);
        bg1Sprite.setTexture(bg1Tex);
        bg1Y = bg1Sprite.getPosition().y;

        bg2Tex.loadFromFile("graphics//background.jpg");
        bg2Tex.setSmooth(false);
        bg2Sprite.setTexture(bg2Tex);
        bg2Sprite.setPosition(0, -799);
        bg2Y = bg2Sprite.getPosition().y;
}


void MovableBackground::Update(sf::RenderWindow &window, float elapsedTime)
{
        if (bg1Y >= window.getSize().y)
        {
                bg1Y = - 799;
        }else
        {
                bg1Y += bgSpeed * elapsedTime;
        }

        if (bg2Y >= window.getSize().y)
        {
                bg2Y = - 799;
        }else
        {
                bg2Y += bgSpeed * elapsedTime;
        }

        if (bg1Y == -799)
        {
                std::cout << bg1Y << "\t bg1" << std::endl;
        }
        if (bg2Y == -799)
        {
                std::cout << bg2Y << "\t bg2" << std::endl;
        }

        bg1Sprite.setPosition(0, bg1Y);
        bg2Sprite.setPosition(0, bg2Y);
}
void MovableBackground::Render(sf::RenderWindow &window)
{
        window.draw(bg1Sprite);
        window.draw(bg2Sprite);
 

the first screenshot:


the second screenshot:


As you can see, one time it nearly works, the other time the distance is just too high :/
and this goes on all the time... when background1 is set to - 799 the little gap is there. When background2 is set to - 799 the huge gap is there.

Maybe the mistake i made is simple, but I just dont see it :S
(if you need more code please tell me)

4
SFML projects / [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« on: November 03, 2013, 03:53:40 am »

Okay, let me introduce "Pew",
my first Game - a simple 2D Space Shooter.


I start with some pictures.

menugamesettingsgame overhighscore

A video of the Game
http://youtu.be/Gwmj3inet-g


Update
http://youtu.be/Lz6h8L5a3ls

(click to show/hide)

At the moment the game has no goal but to survive and I think that this will not change until I have the final boss.
Feel free to tell me what you think of it.


here you can get the release:
http://www.xup.in/dl,13847981/Pew.zip/

(if you can not play it, just inform me. I will try to help  ;))

the code is at github (i will try to keep that up to date, so changes will be seen first there):
https://github.com/nebula2/Pew


Control:
Q and E to change weapons (you get the second weapon at 1000 points, the third after the first boss)
WASD to move
Left Mouse Button to shoot



Pages: [1]