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

Pages: 1 [2] 3 4 ... 7
16
SFML projects / Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« on: September 10, 2015, 12:28:05 am »
Okay, I fixed it

Highscore - Check :D
not really beautiful but hey, its something.
The accuracy and Score are also wrong :D

Main Menu:


Highscore:


here is the new release. Today in a .zip file :D
http://www.xup.in/dl,83314044/Release.zip/

PS: The Solution for my problem was really simple. Here is a snipped:
        //first open the file
        std::fstream file;
        file.open("highscore.txt", std::ios::in);
        if (file.fail()){
                perror("could not load highscore.txt file\n");
        }

        //create some stuff for temp save
        std::string tmp; ///< to display in game later
        int tmpNumPoints, tmpNumFired, tmpNumGot, tmpint;

        //now extract the integer we need (points, shots fired, shots got)
        while (file >> tmpNumPoints >> tmpint >> tmpint >> tmpint >> tmpNumFired >> tmpNumGot){
               
                //and store it into the integer
                //------------------------X--------------Y----------Z-------
                m_numInput.emplace_back(tmpNumPoints, tmpNumFired, tmpNumGot);
        }

        //won't need file anymore
        file.close();
 

Special Thanks to AlexAUT for his help ;-)

17
SFML projects / Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« on: September 09, 2015, 10:31:54 pm »
LoL



Think i need to figure out how to read in like the following:

int char int char int char int char int char int char(\n)
...

18
SFML projects / Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« on: September 09, 2015, 09:51:13 pm »
i changed the highscore txt file to the following:

Quote
370 18 28 1 129 120
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
370 18 28 1 129 78
0 0 0 0 0 0

Furthermore i changed the code to read from file to the following:
        //now extract the integer we need (points, shots fired, shots got)
        while (std::getline(file, tmp,'\n')){
                //IN-(1)STRING--POINTS--------(2)--------------(3)---------------(4)--------------(5)----Shots fired----(6)----Shots got
                file  >> tmpNumPoints >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpNumFired >> tmp >> tmpNumGot;

                std::cout << tmp << " <-das ist tmp " << tmpNumPoints << " <-das ist points \n" << tmpNumFired << " <-das ist fired" << tmpNumFired << " <- das ist got\n";
                //and store it into the integer
                //------------------------X--------------Y----------Z-------
                m_numInput.emplace_back(tmpNumPoints, tmpNumFired, tmpNumGot);
               
                std::cout << " Vector size at filling point: " << m_numInput.size() << std::endl;
        }

now i can read in 5 lines... with shitty output :D

19
SFML projects / Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« on: September 09, 2015, 09:09:42 pm »
Hello AlexAUT,

first of all thank you for your input.

Quote
1) iirc variablenames beginning with an underscore are reserverd for compilers/implementation
Ok, from now on i will declare member variables with this "m_VariableName"

Quote
2) Just use a forward slash in your include paths ( / ), they are the only way supported by all compilers ( \\ might not work with gcc)
I did not know that. I will change this ;-)

Quote
3) You can use while(std::getline(file, tmp)) {...} Instead of the two liner (eof and then getline)
Thanks. That acutally stopped the game.
Quote
4) You can check if file.bad() if one of the output operations failed
I will keep that in memory.

Quote
5) Are you sure this line is correct? Because tmpY will always be 100
No, absolutely not :D thanks. It should be:
tmpY = m_numInput[i].y * 100 / m_numInput[i].z; //get Accuracy

Quote
6)Your deconstructor isn't needed, you do not have to clear it yourself, the deconstructor of a container will delete it's elements (you have to delete the content of pointers if you have something like std::vector<sf::Vector3i*> and the pointers are the owner of the memory, which should be avoided anyways)
Yes, my bad.

Quote
7) This loop should cause the access violation if your highscore contains less than 10entries
... i am an idiot :'D

I changed it and came to some other issues that i have solved now (stuff like division by 0 - so terrible)

now the only problem is that i have only one row of data in my vector :'D

my changed c++ file:
        //set the background
        m_bg.setFilePath("graphics/core/menu.png");

        //show something
        m_back.setStringAndSize("BACK", 50);
        m_back.setPosition(325, 500);

        m_highscore.setStringAndSize("HIGHSCORE", 50);
        m_highscore.setPosition(275, 10);

        //init highscorelist+++++++++++++++++++++++++++++++++

        //first open the file
        std::fstream file;
        file.open("highscore.txt");
        if (file.fail()){
                perror("could not load highscore.txt file\n");
        }

        //create some stuff for temp save
        std::string tmp;       
        int tmpNumPoints, tmpNumFired, tmpNumGot, tmpint;

        //now extract the integer we need (points, shots fired, shots got)
        while (std::getline(file, tmp, '\n')){
                //IN-(1)STRING--POINTS--------(2)--------------(3)---------------(4)--------------(5)----Shots fired----(6)----Shots got
                file >> tmp >> tmpNumPoints >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpNumFired >> tmp >> tmpNumGot;
                //and store it into the integer
                //------------------------X--------------Y----------Z-------
                m_numInput.emplace_back(tmpNumPoints, tmpNumFired, tmpNumGot);
        }

        file.close();

        //convert the values of Vector3i to what we need (Points, Accuracy, Score)
        for (unsigned int i = 0; i < m_numInput.size(); i++){
                int tmpY, tmpZ;
                if (m_numInput[i].y != 0 && m_numInput[i].z != 0){
                        tmpY = m_numInput[i].y * 100 / m_numInput[i].z; //get Accuracy
                }
                else{
                        tmpY = 0;
                }
                if (m_numInput[i].x != 0 && tmpY != 0){
                        tmpZ = m_numInput[i].x / tmpY; //get Score (Points / Accuracy)
                }
                else{
                        tmpZ = 0;
                }
               
                //set new values
                m_numInput[i].y = tmpY;
                m_numInput[i].z = tmpZ;
        }

        //sort the Vec for Score (Z) descending
        std::stable_sort(m_numInput.begin(), m_numInput.end(), sortScoreDesc);

        //erase all entries that are not in the top ten
        //but only if there are more than ten!
        if (m_numInput.size() > 10){
                for (auto it = m_numInput.begin() + 10; it != m_numInput.end(); it++){
                        it = m_numInput.erase(it);
                }
        }

        //now we have a vector of integer, sorted by score which holds the TOP 10 plays
        //we need to print it out now
        for (unsigned int i = 0; i < m_numInput.size(); i++){
                std::cout << "Points: " << m_numInput[i].x << " Accuracy: " << m_numInput[i].y << " Score: " << m_numInput[i].z << std::endl;
                std::cout << " Vector size: " << m_numInput.size() << std::endl;
        }

20
SFML projects / Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« on: September 09, 2015, 08:08:27 pm »
Hey Guys,

i tried to implement a highscore :D #game crashes

Made a new gameState, added it to the main menu bla bla bla.

The constructor should load data from the highscore file. Which looks like this:
Quote
Points 0,Enemy Missed 0,Enemy Killed 0,Monkey Killed 0,Shots Fired 0,Shots Got 0
Points 0,Enemy Missed 0,Enemy Killed 0,Monkey Killed 0,Shots Fired 0,Shots Got 0
Points 370,Enemy Missed 18,Enemy Killed 28,Monkey Killed 1,Shots Fired 129,Shots Got 78

and my class looks like this:

public:
        HighscoreList();
        ~HighscoreList();

        void HandleEvents(Game &game);
        void Update(Game &game);
        void Render(Game &game);

private:
        static bool sortScoreDesc(const sf::Vector3<int>& a, const sf::Vector3<int>& b);
        //mystuff
        HighscoreManager _mng;
        IOHighscore _ioHighscore;
        Background _bg;
        Text _score, _back, _highscore;

        //streams
        std::vector<sf::Vector3i> _numInput; ///< trägt die 3 wichtigen zahlen
        std::vector<std::string> _highScoreDataV;

        std::stringstream _scoreStream;
};

#endif //__HIGHSCORELIST_H__

//HighscoreList.cpp

#include "HighscoreList.h"
#include <fstream>
#include <iostream>
#include <algorithm>

HighscoreList::HighscoreList(){

        //set the background
        _bg.setFilePath("graphics//core//menu.png");

        //show something
        _back.setStringAndSize("BACK", 50);
        _back.setPosition(325, 500);

        _highscore.setStringAndSize("HIGHSCORE", 50);
        _highscore.setPosition(275, 10);

        //init highscorelist+++++++++++++++++++++++++++++++++

        //first get a vector that has the exact size of lines as the file
        std::fstream file;
        file.open("highscore.txt");
        if (file.fail()){
                perror("could not load highscore.txt file\n");
        }

        //create some stuff for temp save
        std::string tmp;       
        int tmpNumPoints, tmpNumFired, tmpNumGot, tmpint;

        //now extract the integer we need (points, shots fired, shots got)
        while (!file.eof()){
                std::getline(file, tmp, '\n');
                //IN-(1)STRING--POINTS--------(2)--------------(3)---------------(4)--------------(5)----Shots fired----(6)----Shots got
                file >> tmp >> tmpNumPoints >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpNumFired >> tmp >> tmpNumGot;
                //and store it into the integer
                //------------------------X--------------Y----------Z-------
                _numInput.emplace_back(tmpNumPoints, tmpNumFired, tmpNumGot);
        }
        file.close();

        //convert the values of Vector3i to what we need (Points, Accuracy, Score)
        for (unsigned int i = 0; i < _numInput.size(); i++){
                int tmpY, tmpZ;
                tmpY = _numInput[i].z * 100 / _numInput[i].z; //get Accuracy
                tmpZ = _numInput[i].x / tmpY; //get Score (Points / Accuracy)

                //set new values
                _numInput[i].y = tmpY;
                _numInput[i].z = tmpZ;
        }

        //sort the Vec for Score (Z) descending
        std::stable_sort(_numInput.begin(), _numInput.end(), sortScoreDesc);
        //std::sort(_numInput.begin(),_numInput.end());

        //erase all entries that are not in the top ten
        for (auto it = _numInput.begin() + 10; it != _numInput.end();it++){
                it = _numInput.erase(it);
        }

        //now we have a vector of integer, sorted by score which holds the TOP 10 plays
        //we need to print it out now
        for (unsigned int i = 0; i < _numInput.size(); i++){
                std::cout << "Points: " << _numInput[i].x << " Accuracy: " << _numInput[i].y << " Score: " << _numInput[i].z << std::endl;
        }

        _score.setStringAndSize("", 20);
        _score.setPosition(150, 195);

}

HighscoreList::~HighscoreList(){
        _numInput.clear();
}

void HighscoreList::HandleEvents(Game &game){
        sf::Event pEvent;

        while (game.window.pollEvent(pEvent)){
                //close
                if (pEvent.type == sf::Event::Closed)
                        game.setRunning(false);
                //menu
                if (pEvent.type == sf::Event::KeyPressed){
                        if (pEvent.key.code == sf::Keyboard::Return)
                                game.ChangeState(Game::gameStates::MAINMENU);
                }
        }
}
void HighscoreList::Update(Game &game){
        _mng = game.highscore;

        //Update stuff (there is the convertion from int to stringstream)
        //_score.Update(_scoreStream, _mng.getPoints());
}
void HighscoreList::Render(Game &game){
        _bg.Render(game.window);

        //Render stuff
        _score.Render(game.window);
        _back.Render(game.window);
        _highscore.Render(game.window);
}

//if the first integer is greater than the second, this returns true
bool HighscoreList::sortScoreDesc(const sf::Vector3<int>& a, const sf::Vector3<int>& b){
        return (a.z > b.z);
}


If i enter the highscore overview in the game, it simply does nothing more.
only thing i could get was this:
Quote
Ausnahme (erste Chance) bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6
Ausnahmefehler bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6

Ausnahme (erste Chance) bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6
Ausnahmefehler bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6

Ausnahme (erste Chance) bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6
Ausnahmefehler bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6

Das Programm "[11728] Pew ReWork.exe" wurde mit Code -1073741510 (0xc000013a) beendet.

I will keep you updated guys :D

21
SFML projects / Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« on: September 09, 2015, 01:13:03 pm »
A quick update:

i fixed a bug where the select sound is played too often in the menu.
And the mouse now sticks to the window.
Furthermore the player-texture was upside down :'D

For some reason github has some issues uploading the changes. so here is just the fixed release:

http://www.xup.in/dl,68075662/Release.rar/

22
SFML projects / Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« on: September 09, 2015, 04:35:53 am »
Hello,

after a long time (i think nearly a year) i could finally work again on my little project.
Got really buisy  ;D

Found out that my code is more than just spaghetti :D it is horrible.
But i enhanced my understanding of Computers a bit and learned a little about how it should be.

The App thing is something that probably will never be done because i just dont have the energy for that :(

So i did not do much, but here is it:

Coop will be shut down. That is because i changed the controls of the game.
You can now use the mouse in most of the menu and you will need it to play the game.

Projectiles are now shot in the direction the mouse is in relation to the player.
The player now moves with WASD and is always heading towards the mouse cursor.

I think that this makes the controls more dynamic.

As always, the code is uploaded to github. https://github.com/nebula2/Pew/tree/master/src%20Visual%20Studio%202013/Pew-Rework

Here is the Release:
http://www.xup.in/dl,32149673/Release.rar/

Maybe i will make an extra video for the new controls for you to see how it behaves.

23
SFML projects / Re:creation - a top down action rpg about undeads
« on: August 29, 2015, 04:02:49 pm »
Dude this is awesome work.
Really like what you did there!

Keep it up ;-)

24
SFML projects / Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« 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 ;)

25
General / Re: access violation with unique pointer (c++)
« on: June 19, 2014, 04:51:48 pm »
okay, i got it running :P

the mistake was in the intro

changed it to this:
//Intro.h

#ifndef INTRO_H
#define INTRO_H

#include "Game.h"


class Intro : public GameState
{
public:
        Intro();
        ~Intro();

        void HandleEvents(Game &game);
        void Update(Game &game);
        void Render(Game &game);

private:
        sf::Clock   pClock;
        sf::Texture intro;
        sf::Sprite  introSprite;
        float elapsedTime;
        float bgSpeed, y;
};

#endif
 

//Intro.cpp

#include "Intro.h"

Intro::Intro()
{
        elapsedTime = 0;

        intro.loadFromFile("graphics//core//intro.jpg");
        intro.setSmooth(false);
        introSprite.setTexture(intro);
        introSprite.setPosition(0, 600);
        bgSpeed = 0.03;
        y = introSprite.getPosition().y;
}

Intro::~Intro()
{
}

void Intro::HandleEvents(Game &game)
{
        sf::Event pEvent;

        while (game.window.pollEvent(pEvent))
        {
                if (pEvent.type == sf::Event::Closed)
                        game.setRunning(false);

                if (pEvent.type == sf::Event::KeyPressed)
                {
                        if (pEvent.key.code == sf::Keyboard::Escape)
                                game.setRunning(false);

                        if (pEvent.key.code == sf::Keyboard::Return)
                                game.ChangeState(Game::gameStates::PLAY);
                }
        }
}
void Intro::Update(Game &game)
{
        elapsedTime = pClock.restart().asMilliseconds();
        y -= bgSpeed * elapsedTime;
        introSprite.setPosition(0, y);

        if (y <= -1200)
                game.ChangeState(Game::gameStates::PLAY);
}
void Intro::Render(Game &game)
{
                game.window.draw(introSprite);
}
 

thanks for the help ;) this topic can now be closed

26
General / Re: access violation with unique pointer (c++)
« on: June 15, 2014, 01:15:39 am »
thank you ;) i tested some things, it must be as you say. i got something wrong in my mainmenu class. i will try to fix this tomorrow :D

Edit:

Sorry, but i just can't find whats wrong...

27
General / Re: access violation with unique pointer (c++)
« on: June 15, 2014, 01:07:50 am »
as this happens in the intro state, i think it would be good to see that code too... too much? :D

//Intro.h

#ifndef INTRO_H
#define INTRO_H

#include "Game.h"


class Intro : public GameState
{
public:
        Intro();
        ~Intro();

        void HandleEvents(Game &game);
        void Update(Game &game);
        void Render(Game &game);

private:
        Text startText;

        sf::Event   pEvent;
        sf::Clock   pClock;
        sf::Texture intro;
        sf::Sprite  introSprite;
        float elapsedTime;
        float bgSpeed, y;
        bool startintro;
        int returnCounter;
};

#endif
 

//Intro.cpp

#include "Intro.h"

Intro::Intro()
{
        //basic
        elapsedTime = 0;
        startintro = false;
        returnCounter = 0;

        //background
        startText.setStringAndSize("Return to start", 60);

        intro.loadFromFile("graphics//core//intro.jpg");
        intro.setSmooth(false);
        introSprite.setTexture(intro);
        introSprite.setPosition(0, 600);
        bgSpeed = 0.03;
        y = introSprite.getPosition().y;
}

Intro::~Intro()
{
}

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

                if (pEvent.type == sf::Event::KeyPressed)
                {
                        if (pEvent.key.code == sf::Keyboard::Return)
                                returnCounter += 1;

                        if (pEvent.key.code == sf::Keyboard::Escape)
                                game.setRunning(false);
                }
        }
}
void Intro::Update(Game &game)
{
        if (!startintro)
        {
                startText.setOrigin(startText.getGlobalBounds().width / 2, startText.getGlobalBounds().height / 2);
                startText.setPosition(game.window.getSize().x / 2, game.window.getSize().y / 2);
                introSprite.setPosition(0, 600);
        }

        if (returnCounter == 1)
                startintro = true;

        if (returnCounter >= 2)
                game.ChangeState(Game::gameStates::PLAY);

        //move background
        elapsedTime = pClock.restart().asMilliseconds();
        y -= bgSpeed * elapsedTime;
        introSprite.setPosition(0, y);

        //end intro
        if (y <= -1200)
        {
                game.ChangeState(Game::gameStates::PLAY);
                startintro = false;
        }
}
void Intro::Render(Game &game)
{
        if (!startintro)
                startText.Render(game.window);

        if (startintro)
                game.window.draw(introSprite);
}
 

28
General / Re: access violation with unique pointer (c++)
« on: June 15, 2014, 01:02:14 am »
In these sf::Event is on stack as a local in function so it lasts as long as the scope it's in does not caring how long the class this function is called on lives, only member variables and this care about whether or not class lives. In MainMenuState you (for some reason..., that's really bad practice) allocate it with new in the class itself and that gets deleted in destructor when class gets freed by unique ptr.

okay so i changed the mainMenuState.
it looks now like this:
//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);

        //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()
{
        std::cout << "zerstoert aldaa";
}

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);
}
 
i can now switch the state.

but when the state is changed i really quick get the next error:

Quote
HEAP[Pew ReWork.exe]: HEAP: Free Heap block 55f87d8 modified at 55f8854 after it was freed
Pew ReWork.exe hat einen Haltepunkt ausgelöst.

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

http://www.suckmypic.net/C38mbus8.png



well :D what da f*** :'D

29
General / Re: access violation with unique pointer (c++)
« on: June 15, 2014, 12:48:59 am »
You should make it so the unique ptr that was there originally is stored somewhere for 'delayed' deletion or return from HandleEvents if you called ChangeState.

mhh. okay i will think about how to handle that. but i find it strange, that this whole stuff works in this case:

//main.cpp
#include "Game.h"

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

        while (cyberPong.isRunning())
        {
                cyberPong.Run();
        }

        return 0;
}
 

//GameState.h

#ifndef GAMESTATE_H
#define GAMESTATE_H

class Game;

class GameState
{
public:
        virtual void HandleEvents(Game &game) = 0;
        virtual void Update(Game &game) = 0;
        virtual void Draw(Game &game) = 0;
        virtual          ~GameState(){};
};

#endif
 

//Game.h

#ifndef GAME_H
#define GAME_H

#include <iostream>
#include <memory>
#include <SFML\Graphics.hpp>
#include "GameState.h"
#include "MainMenuState.h"
#include "PlayState.h"

class Game
{
public:
        Game();

        enum class gameStates{ MAINMENU, PLAY};

        void Run();
        void ChangeState(gameStates newState);
        bool isRunning();
        bool running;
        sf::RenderWindow window;
private:
        std::unique_ptr<GameState> CurrentState;
};

#endif
 

//Game.cpp

#include "Game.h"

Game::Game()
{
        window.create(sf::VideoMode(1280, 720), "CyberPong");
        window.setFramerateLimit(60);
        running = true;
}

void Game::Run()
{
        while (window.isOpen())
        {
                CurrentState->HandleEvents(*this);

                window.clear(sf::Color(0,134,194));
               
                CurrentState->Update(*this);
                CurrentState->Draw(*this);

                window.display();
        }
}

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

bool Game::isRunning()
{
        return running;
}
 

//MainMenuState.h

#ifndef MAINMENUSTATE_H
#define MAINMENUSTATE_H

#include "Game.h"

class MainMenuState : public GameState
{
public:
        MainMenuState();
        ~MainMenuState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Draw(Game &game);
private:
        bool bStartGameSelected;
        bool bQuitGameSelected;
        sf::Font font;
        sf::Text txtStartGame;
        sf::Text txtQuitGame;
};

#endif
 

//MainMenuState.cpp

#include "MainMenuState.h"

MainMenuState::MainMenuState()
{
        font.loadFromFile("Font//Saddlebag.ttf");

        txtStartGame.setFont(font);
        txtStartGame.setString("Spiel Starten");
        txtStartGame.setCharacterSize(30);
        txtStartGame.setPosition(400, 250);

        txtQuitGame.setFont(font);
        txtQuitGame.setString("Spiel Beenden");
        txtQuitGame.setCharacterSize(30);
        txtQuitGame.setPosition(400, 400);
}

void MainMenuState::HandleEvents(Game &game)
{
        sf::Event event;

        while (game.window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                {
                        game.window.close();
                        game.running = false;
                }
                if (event.type == sf::Event::MouseButtonReleased)
                {
                        if (event.mouseButton.button == 0)
                        {
                                if (bStartGameSelected)
                                        game.ChangeState(Game::gameStates::PLAY);
                                else if (bQuitGameSelected)
                                {
                                        game.window.close();
                                        game.running = false;
                                }
                        }
                }
        }
}

void MainMenuState::Update(Game &game)
{
        if (txtStartGame.getGlobalBounds().contains(
                sf::Mouse::getPosition(game.window).x,
                sf::Mouse::getPosition(game.window).y) &&
                txtStartGame.getColor() != sf::Color::Green)
        {
                txtStartGame.setColor(sf::Color::Green);
                bStartGameSelected = true;
        }
        else if (!txtStartGame.getGlobalBounds().contains(
                sf::Mouse::getPosition(game.window).x,
                sf::Mouse::getPosition(game.window).y) &&
                txtStartGame.getColor() == sf::Color::Green)
        {
                txtStartGame.setColor(sf::Color::White);
                bStartGameSelected = false;
        }

        if (txtQuitGame.getGlobalBounds().contains(
                sf::Mouse::getPosition(game.window).x,
                sf::Mouse::getPosition(game.window).y) &&
                txtQuitGame.getColor() != sf::Color::Green)
        {
                txtQuitGame.setColor(sf::Color::Green);
                bQuitGameSelected = true;
        }
        else if (!txtQuitGame.getGlobalBounds().contains(
                sf::Mouse::getPosition(game.window).x,
                sf::Mouse::getPosition(game.window).y) &&
                txtQuitGame.getColor() == sf::Color::Green)
        {
                txtQuitGame.setColor(sf::Color::White);
                bQuitGameSelected = false;
        }
}

void MainMenuState::Draw(Game &game)
{
        game.window.draw(txtStartGame);
        game.window.draw(txtQuitGame);
}

MainMenuState::~MainMenuState()
{
        std::cout << "Menudestruktor wurde aufgerufen" << std::endl;
}
 

//PlayState.h

#ifndef PLAYSTATE_H
#define PLAYSTATE_H

#include "Game.h"
#include "GameState.h"

class PlayState : public GameState
{
public:
        PlayState();
        ~PlayState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Draw(Game &game);
private:
        std::unique_ptr<sf::CircleShape> upBall;
        std::shared_ptr<sf::RectangleShape> spPad1;
        std::shared_ptr<sf::RectangleShape> spPad2;
};

#endif
 

//PlayState.cpp

#include "PlayState.h"

PlayState::PlayState()
{
        spPad1 = std::make_shared<sf::RectangleShape>(sf::Vector2f(53, 192));
        spPad2 = std::make_shared<sf::RectangleShape>(sf::Vector2f(53, 192));
        upBall = std::unique_ptr<sf::CircleShape>(new sf::CircleShape(20));

        spPad1->setPosition(0, 300);
        spPad2->setPosition(1227, 300);
}

PlayState::~PlayState()
{
        std::cout << "Playstate wurde zerstört" << std::endl;
}


void PlayState::HandleEvents(Game &game)
{
        sf::Event event;

        while (game.window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                {
                        game.window.close();
                        game.running = false;
                }
        }
}

void PlayState::Update(Game &game)
{

}

void PlayState::Draw(Game &game)
{
        game.window.draw(*upBall);
        game.window.draw(*spPad1);
        game.window.draw(*spPad2);
}
 

30
General / Re: access violation with unique pointer (c++)
« on: June 15, 2014, 12:36:38 am »
When your MainMenuState calls ChangeState it deletes itself because of unique_ptr being set to something different and because of that things such as sf::Event, sf::Clock etc. that are its members are deleted already so in next pollEvent loop it'll crash, you should return immediately from HandleEvents if you called ChangeState.

i understand what you mean. but how can i realize this?

Pages: 1 [2] 3 4 ... 7