Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Di Jul 15, 2025 02:07

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 8 Beiträge ] 
Autor Nachricht
 Betreff des Beitrags: efficient main loop with opengl
BeitragVerfasst: So Mai 10, 2009 16:08 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
What is the best way to write a main loop for an api based opengl application?

In my current construction i get yerky results. (like it is frozen a bit every x seconds) Even though cpu usage stays below 10%.

Here is my main loop:

Code:
  1.  
  2. while not finished do
  3.   begin
  4.  
  5.     if (PeekMessage(msg, 0, 0, 0, PM_REMOVE)) then // Check if there is a message for this window
  6.     begin
  7.       if (msg.message = WM_QUIT) then     // If WM_QUIT message received then we are done
  8.         finished := True
  9.       else
  10.       begin                               // Else translate and dispatch the message to this window
  11.          TranslateMessage(msg);
  12.           DispatchMessage(msg);
  13.         Sleep(1);
  14.       end;
  15.     end else
  16.     begin
  17.       Inc(FPSCount);                      // Increment FPS Counter
  18.  
  19.       LastTime :=ElapsedTime;
  20.       ElapsedTime :=GetTickCount() - DemoStart;     // Calculate Elapsed Time
  21.       ElapsedTime :=(LastTime + ElapsedTime) DIV 2;
  22.  
  23.       if getfeedback = false then drawglscene; //here my scene is rendered
  24.  
  25.       sleep(1);
  26.  
  27.       if (keys[VK_ESCAPE]) then           // If user pressed ESC then set finised TRUE
  28.         finished := True
  29.       else
  30.       begin
  31.           fwdVel:=0;
  32.           yaw:=0;
  33.           pitch:=0;
  34.           if keys[VK_UP] then fwdVel:=0.2;
  35.           if keys[VK_DOWN] then fwdVel:=-0.2;
  36.           if keys[VK_LEFT] then yaw:=0.8;
  37.           if keys[VK_RIGHT] then yaw:=-0.8;
  38.           if keys[VK_PRIOR] then pitch:=0.8;  // page up
  39.           if keys[VK_NEXT]then pitch:=-0.8; // page down
  40.           if keys[VK_SPACE] then resetcamera:=true;
  41.       end;
  42.     end;
  43.   end;
  44.  


Should i ditch my main loop and do a total rewrite? And if so what would be the best way? Rendering from a thread? Rendering on idle (if that is detecteable from api only, i know it works with the forms template).

Thanks for your anwers in advance.

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: So Mai 10, 2009 17:05 
Offline
DGL Member
Benutzeravatar

Registriert: Fr Mär 30, 2007 18:35
Beiträge: 331
What's the purpose of Sleep(1)? The call may take longer than 1ms, sometimes up to 16ms. That could be the cause of your lags.


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: So Mai 10, 2009 21:06 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
Removing the both sleep(1) does not make an improvement.

E.g. i get about 30 to 50 fps with waitforvblanc on. With waitforvblanc off i get about 70 fps.
Now with a very old build of my engine (pre version control, so where have i backed up those source files) i get
about 58 fps with waitforvblanc (that is quit nice since my lcd has a refresh rate of 60 (m)hz . And with waitforvblanc of i get about 170 fps. The realy strange thing is that the old one should have been slower as it does xml parsing each frame :twisted: .

Anyway i must have done something stupid. :oops: I just have to find what the cause is.

But tips on building a stable opengl environment are welcome.
E.g. how do i use the idle time?

_________________
http://3das.noeska.com - create adventure games without programming


Zuletzt geändert von noeska am So Mai 10, 2009 21:56, insgesamt 1-mal geändert.

Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: So Mai 10, 2009 21:47 
Offline
DGL Member
Benutzeravatar

Registriert: Di Mai 18, 2004 16:45
Beiträge: 2623
Wohnort: Berlin
Programmiersprache: Go, C/C++
Ich würde definitiv das PeekMessage in ein while packen, da sonnst nur eine Message pro frame abgearbeitet wird und weitere dann auflaufen.
Du berechnest die durchschnittliche Zeit pro Frame aber nutzt diese bei den Tastenevents weiter unten nicht und die berechnung der Zeit gefällt mir nicht.
Ich würde wie schon öfter im Forum gezeigt wurde das Delta auf minimum und Maximum Werte beschränken.
Das Problem könnte auftreten, wenn du ein FPS drop oder peek hast und somit die Variable ElapsedTime einen sinnfreien Mittelwert erhält(der noch auf die nächsten Frames Auswirkung hat).

_________________
"Wer die Freiheit aufgibt um Sicherheit zu gewinnen, der wird am Ende beides verlieren"
Benjamin Franklin

Projekte: https://github.com/tak2004


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: So Mai 10, 2009 22:18 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
Placing the PeekMessage in a while did some good things. The engines seems to be more responsive. Makes sense given your explanation. Maybe i should loose the finished variable and make the PeekMessage the main while.
So next i should redesign the fps calculation.
What do you mean by i should use the average time on the key events?

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mo Mai 11, 2009 03:36 
Offline
DGL Member
Benutzeravatar

Registriert: Di Mai 18, 2004 16:45
Beiträge: 2623
Wohnort: Berlin
Programmiersprache: Go, C/C++
Code:
  1. if keys[VK_UP] then fwdVel:=0.2*ElapsedTime;
  2. if keys[VK_DOWN] then fwdVel:=-0.2*ElapsedTime;
  3. if keys[VK_LEFT] then yaw:=0.8*ElapsedTime;
  4. if keys[VK_RIGHT] then yaw:=-0.8*ElapsedTime;
  5. if keys[VK_PRIOR] then pitch:=0.8*ElapsedTime; // page up
  6. if keys[VK_NEXT]then pitch:=-0.8*ElapsedTime; // page down

_________________
"Wer die Freiheit aufgibt um Sicherheit zu gewinnen, der wird am Ende beides verlieren"
Benjamin Franklin

Projekte: https://github.com/tak2004


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mo Mai 11, 2009 22:22 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 29, 2003 00:11
Beiträge: 436
Maybe you find this usefull:
http://dewitters.koonsolo.com/gameloop.html


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Mai 21, 2009 18:48 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
:oops: never do xml parsing from a file streamed from within a vfs within the renderloop.
As when a collision occured i checked if an event was tied to the object the player collided with. That part is over 5 years old, what was i thinking back then. :shock:

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 8 Beiträge ] 
Foren-Übersicht » English » English Programming Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.009s | 16 Queries | GZIP : On ]