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 68942 times)

0 Members and 2 Guests are viewing this topic.

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #60 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.
« Last Edit: September 09, 2015, 04:49:05 am by nebula »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #61 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/

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #62 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

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #63 on: September 09, 2015, 08:35:19 pm »
You should learn how to debug, most of the time you find the root of an access violation in under 10seconds if you know how to use your debug tools.

Some suggestions:

1) iirc variablenames beginning with an underscore are reserverd for compilers/implementation
2) Just use a forward slash in your include paths ( / ), they are the only way supported by all compilers ( \\ might not work with gcc)
3) You can use while(std::getline(file, tmp)) {...} Instead of the two liner (eof and then getline)
4) You can check if file.bad() if one of the output operations failed
5) Are you sure this line is correct? Because tmpY will always be 100
tmpY = _numInput[i].z * 100 / _numInput[i].z; //get Accuracy
 
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)
7) This loop should cause the access violation if your highscore contains less than 10entries
    //erase all entries that are not in the top ten
    for (auto it = _numInput.begin() + 10; it != _numInput.end();it++){
        it = _numInput.erase(it);
    }
(but i'm not quite sure, try a indices loop (for(int i = 10; i < _numInput.size(); i++)) and look if it solves your problem or even better just call _numInput.resize(10);

AlexAUT

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #64 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;
        }
« Last Edit: September 09, 2015, 09:22:18 pm by nebula »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #65 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

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #66 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)
...

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #67 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 ;-)
« Last Edit: September 10, 2015, 12:32:43 am by nebula »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #68 on: September 10, 2015, 08:41:32 am »
Looks nice!

You could still simply replace this
if (m_numInput.size() > 10){
        for (auto it = m_numInput.begin() + 10; it != m_numInput.end(); it++){
            it = m_numInput.erase(it);
        }
    }
 

with this

 m_numInput.resize(10);

looks cleaner  :P



You could also use 3 different sf::Text for each highscore value (Points, Accuracy, Score) so you can align them in a vertical line



AlexAUT


nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #69 on: September 10, 2015, 03:36:58 pm »
Thank you AlexAUT,

now I have this:



As you can see - now the stuff should be correct :D

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #70 on: September 10, 2015, 06:34:44 pm »
Reworked the Mainpage.
  • Now with a video which shows the game with the new controls
    (could not find out how to show it like the old one).
  • Pictures updated
  • Textual updates
  • Links updated
« Last Edit: September 10, 2015, 10:53:09 pm by nebula »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #71 on: September 11, 2015, 09:05:30 pm »
1) iirc variablenames beginning with an underscore are reserverd for compilers/implementation
Not quite.
Symbols starting with underscore are reserved for the implementation in global scope. Names that start with underscore+uppercase letter or names that contain double underscore are reserved in any scope.
See section 2.10 of the standard.

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #72 on: September 11, 2015, 11:44:34 pm »
Hey Guys,

added healthbar for the normal enemy.



Will update this for the others later. Just wanted to show something :D

And here is what i plan to do in the future:

  • Sprites of Weapons should have the rotation according to their direction
  • An Intro before the main menu is entered - to give credits to SFML and some guys that helped me
  • Some kind of transition between the Gamestates, because as you may have noticed the present solution causes some issues
  • Countdown before the game starts
  • Some animated Sprites / better Sprites in general
  • Maybe a Reward System (for example speed buff, faster shooting, a shield)

If you have suggestions or something, let me know  :P
« Last Edit: September 11, 2015, 11:54:41 pm by nebula »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #73 on: September 12, 2015, 07:41:08 pm »
Hi Guys,

All Enemies (except for the Formation - temp) got healthbars now.

Latest Release on Mainpage - Code on Github

UPDATE:
Quote
All Enemies (except for the Formation - temp) got healthbars now.
Fixed that

Also fixed other things quickly

New: Player can now only move in window - I think he got enough freedom
« Last Edit: September 12, 2015, 08:07:47 pm by nebula »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #74 on: September 12, 2015, 08:35:50 pm »
INFO:

There is currently a bug in the Highscore:

HighscoreList.cpp
        //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 = m_numInput.erase(it);
                        it++;
                }
        }

I will fix this soon

 

anything