Programm sehr langsam, wieso ?



  • Hi,
    ich bin gerade dabei ein kleines "Spiel" zu programmieren.
    Jedoch liegen die fps bei mir im 6-7 er Bereich, da ich keine Ahnung habe, was mir die performance wegnimmt frag ich einfach mal hier, vlt. kann mir jmd helfen.

    //EDIT: nicht von dem npc1 verwirren lassen 🙂

    // Game loop
        while(true)
        {
            start = clock();
            //Spielereingabe
            if(kbhit())
            {
                char c = getch();
                switch(c)
                {
                    case 49:
                         if(npc1.GetXPosition() != 0)
                         npc1.SetPosition(npc1.GetXPosition()-1,npc1.GetYPosition());
                         break;
                    case 50:
                         if(npc1.GetYPosition() != 0)
                         npc1.SetPosition(npc1.GetXPosition(),npc1.GetYPosition()-1);
                         break;
                    case 51:
                         if(npc1.GetXPosition() != MAXX)
                         npc1.SetPosition(npc1.GetXPosition()+1,npc1.GetYPosition());
                         break;
                    case 52:
                         if(npc1.GetYPosition() != MAXY)
                         npc1.SetPosition(npc1.GetXPosition(),npc1.GetYPosition()+1);
                         break;
                    default:
                         break;
                }
            }
            //AI
            //Kollision
            //Spiel Zeichnen
            for(int x = 0; x != MAXX; x++)
            {
                for(int y = 0; y != MAXY; y++)
                {
                    if(npc1.GetXPosition() == x && npc1.GetYPosition() == y)
                    {
                        cout<<"1";
                    }
                    else if(npc2.GetXPosition() == x && npc2.GetYPosition() == y)
                    {
                        cout<<"2";
                    }
                    else cout<<" ";
                }
                cout<<"\n";
            }
            gotoxy(0,0);
            finish = clock();
            zeitdifferenz = finish - start;
            cout<<1000/zeitdifferenz<<" fps";  
        }
    

    MFG
    Whtthfck?



  • zeig mal den gesamten code inkl. alle header und alle sourcedateien



  • //main.cpp
    #include "Library.h"
    
    int main()
    {
        int MAXX = 78;
        int MAXY = 157;
        resize(1024,768);
        maximize();
        cursize(0);
    
        int feld[MAXX][MAXY];
        int start, finish, zeitdifferenz;
    
        Objekt npc1(0,0);
        Objekt npc2(MAXX,MAXY);
    
        while(true)
        {
            start = clock();
            //Spielereingabe
            if(kbhit())
            {
                char c = getch();
                switch(c)
                {
                    case 49:
                         if(npc1.GetXPosition() != 0)
                         npc1.SetPosition(npc1.GetXPosition()-1,npc1.GetYPosition());
                         break;
                    case 50:
                         if(npc1.GetYPosition() != 0)
                         npc1.SetPosition(npc1.GetXPosition(),npc1.GetYPosition()-1);
                         break;
                    case 51:
                         if(npc1.GetXPosition() != MAXX)
                         npc1.SetPosition(npc1.GetXPosition()+1,npc1.GetYPosition());
                         break;
                    case 52:
                         if(npc1.GetYPosition() != MAXY)
                         npc1.SetPosition(npc1.GetXPosition(),npc1.GetYPosition()+1);
                         break;
                    default:
                         break;
                }
            }
            //AI
            //Kollision
            //Spiel Zeichnen
            for(int x = 0; x != MAXX; x++)
            {
                for(int y = 0; y != MAXY; y++)
                {
                    if(npc1.GetXPosition() == x && npc1.GetYPosition() == y)
                    {
                        cout<<"1";
                    }
                    else if(npc2.GetXPosition() == x && npc2.GetYPosition() == y)
                    {
                        cout<<"2";
                    }
                    else cout<<" ";
                }
                cout<<"\n";
            }
            gotoxy(0,0);
            finish = clock();
            zeitdifferenz = finish - start;
            cout<<1000/zeitdifferenz<<" fps";  
        }
    
        system("PAUSE");
    }
    
    //Objekt.cpp
    #include "Library.h"
    
    Objekt::Objekt(int a, int b)
    {
        posx = a;
        posy = b;
    }
    
    void Objekt::SetPosition(int a, int b)
    {
        posx = a;
        posy = b;
    }
    
    int Objekt::GetXPosition()
    {
        return posx;
    }
    
    int Objekt::GetYPosition()
    {
        return posy;
    }
    
    //Objekt.h
    class Objekt 
    {
    
        private:
            int posx, posy;
    
        public:
            Objekt(int, int);
            void SetPosition(int, int);
            int GetXPosition();
            int GetYPosition();
    
    };
    
    //Library.h
    #include <iostream>
    #include <conio.h>
    #include <ic.hpp>
    
    using namespace std;
    using namespace ic;
    using namespace ic::shorties;
    
    #include "Objekt.h"
    

    Ja, schlechter stil, bin noch n noob 😛

    MFG
    Whtthfck?



  • Ich merke gerade, dass es wohl das Spielfeld ist.
    Gibt es da schnellere Möglichkeiten als ein int array ?

    MFG
    Whtthfck?



  • Whtthfck? schrieb:

    Ich merke gerade, dass es wohl das Spielfeld ist.
    Gibt es da schnellere Möglichkeiten als ein int array ?

    MFG
    Whtthfck?

    eher ists die Ausgabe auf der Konsole, die in der Regel sehr sehr langsam ist. Da kann man auch nicht sooo viel dran machen, spontan faellt mir z. B. ein:
    erst den ganzen Bildschirm mit ' ' "loeschen" (natuerlich immer mehr als nur 1 char ausgeben) und dann mit gotoxy() dahin springen wo die Spielerpostionen hingehoeren und zeichnen. k.A. ob das viel bringt, aber du sparst dir dann sehr viele cout-Aufrufe.

    Auch als kleinere Verbesserung: verwende keine Strings, wenn du nur chars ausgibst. Sprich statt:

    cout << "1";
    cout << "2";
    cout << " ";
    

    Nimmst du

    cout << '1';
    cout << '2':
    cout << ' ';
    

    Bringt zwar sicher kaum was, ist aber einfach schoener/richtiger.



  • Darauf haette ich auch getippt...
    zudem scheint die Ausgabe im Vollbild-Modus um einiges schneller zu laufen als im Fenster.



  • Hallo,

    Danke für die Antworten,
    ich habe jetzt:

    ...
            for(int x = 0; x != MAXX+1; x++)
            {
                for(int y = 0; y != MAXY+1; y++)
                {
                    if(npc1.GetXPosition() == x && npc1.GetYPosition() == y)
                    {
                        cout<<'1';
                    }
                    else if(npc2.GetXPosition() == x && npc2.GetYPosition() == y)
                    {
                        cout<<'2';
                    }
                    else cout<<' ';
                }
                cout<<"\n";
            }
    ...
    

    durch:

    gotoxy(npc1.GetXPosition(),npc1.GetYPosition());
    cout<<'1';
    gotoxy(npc2.GetXPosition(),npc2.GetYPosition());
    cout<<'2';
    

    ersetzt. Jedoch hängt sich das Programm dabei auf. Hat jmd ne Ahnung wieso?



  • step mit dem Debugger durch und schau mal, was fuer Argumente du an gotoxy uebergibst.

    EDIT: ich hab gotoxy nie benutzt, aber evlt. kanns ja sein dass das nur an Stellen funktioniert, die vorher "beschrieben" wurden (sprich: vielleicht musst du erstmal den ganzen Bildschirm mit ' ' vollschreiben).



  • 1. Keine Ausgabe durch cout, die ist sehr langsam. Verwende stattdessen WriteConsoleOutput() aus der WinAPI

    2. Nicht immer das gesamte Spielfeld ausgeben!

    1. Du überschreibst die bisherigen Positionen mit ' '
    2. Du schreibst an die neuen Positionen eine 1 bzw. eine 2

    Da gibst du nur 4 Zeichen statt 80*25 = 2000 aus, beschleunigt dein Spiel also um Faktor 500

    MfG SideWinder



  • Hey,

    Ich habs nun so gelöst:

    gotoxy(npc1.GetOldXPosition(),npc1.GetOldYPosition());
            cout<<' ';
            gotoxy(npc2.GetOldXPosition(),npc2.GetOldYPosition());
            cout<<' ';      
    
            gotoxy(npc1.GetXPosition(),npc1.GetYPosition());
            cout<<'1';
            gotoxy(npc2.GetXPosition(),npc2.GetYPosition());
            cout<<'2';
    
            npc1.SetOldPosition(npc1.GetXPosition(),npc1.GetYPosition());
            npc2.SetOldPosition(npc2.GetXPosition(),npc2.GetYPosition());
    

    danke für eure hilfe!
    greetz



  • Sollte imho auch mit cout schnell genug sein, ja das sieht gut aus.

    MfG SideWinder


Anmelden zum Antworten