Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trying to make separate timer and rendering loops  (Read 3893 times)

0 Members and 1 Guest are viewing this topic.

MrHello

  • Newbie
  • *
  • Posts: 4
    • View Profile
Trying to make separate timer and rendering loops
« on: March 08, 2012, 02:32:45 am »
Well, as explained in the title I'm trying to do this abiding by the rules of the clock and sf::sleep, atm I have the following code.

Code: [Select]
/*
 * main.cpp
 *
 *  Created on: 5 Mar 2012
 *      Author: Lee
 */

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <vector>
#include "InGameHandle/inGame.hpp"
#include "InGameHandle/gameStateBase.hpp"


class EngineMain{
private:
  sf::VideoMode myVideoMode;
  sf::RenderWindow myWindow;
  sf::Clock gameClock;

  sf::Clock fpsClock;
  sf::Clock lpsClock;

  GameStateBase* applicationIsInPointer;


  float constantFPS;
  float constantLPS;

  float lastTime;

  bool draw;
public:

void init(float _fps, float _lps)
{
  constantFPS = _fps;
  constantLPS = _lps;

  draw = false;

  myVideoMode.Width = 1024;
  myVideoMode.Height = 768;
  myVideoMode.BitsPerPixel = 16;
  myWindow.Create(myVideoMode, "hello");
  applicationIsInPointer = new InGameHandler;

  appRunning();
}

void appRunning()
{
  //Game init
  applicationIsInPointer->init();

  while (myWindow.IsOpen())
    {
      sf::Event event;
        while(myWindow.PollEvent(event))
          {
            switch(event.Type)
            {
            case sf::Event::Closed:
              myWindow.Close();
              break;
            default:
              break;
            }
          }

        if(lpsClock.GetElapsedTime().AsMilliseconds() > constantLPS)
          {
            applicationIsInPointer->logic();
            //Update the logic of the game
            lastTime = lpsClock.GetElapsedTime().AsMilliseconds();
            lpsClock.Restart();
          }

        if(fpsClock.GetElapsedTime().AsMilliseconds() > constantFPS)
          {
            myWindow.Clear();
           //Return EVERYTHING that needs to be drawn from the game here
            applicationIsInPointer->draw();
            //Then for loop through it and draw it
            myWindow.Display();
            fpsClock.Restart();
          }
        sf::Sleep(sf::Milliseconds(lastTime));

    }

}


};

int main()
{
  EngineMain engine;
  engine.init(60,60);
}






I know I'm doing a lot wrong here, but can someone help me out with this and explain the logical steps so I can learn?

You may be wondering why I'm not using "Set frame limit", this is because it doesn't appear to set the frame limit correctly or reliably on my PC.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Trying to make separate timer and rendering loops
« Reply #1 on: March 08, 2012, 04:07:36 pm »
How can your applicationIsInPointer object draw stuff if it doesn't reach your window? You've commented that it returned things, but you are doing nothing with the return.

Apart from that, I think you are sleeping the wrong ammount of time. You are storing the time passed on the lastTime variable, and then sleeping another of that same ammount. If it passed 1 millisecond since the last frame, you sleep 1 more millisecond. The correct ammount, I think, would be constantLPS - lastTime, or something like that.

You can also use the same clock for both operations, if both will work at the same rate.

MrHello

  • Newbie
  • *
  • Posts: 4
    • View Profile
Trying to make separate timer and rendering loops
« Reply #2 on: March 08, 2012, 07:25:17 pm »
I want to have a system where there are two separate loops, of which can be set to any number. I want logic to be priorotized and frames to be skipped if logic takes a long time.

I managed this in my Allegro engine, but I have no idea what I'm doing in SFML.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Trying to make separate timer and rendering loops
« Reply #3 on: March 08, 2012, 08:40:24 pm »
Mrhello, have you looked at the Basic Game Engine tutorial on the SFML Wiki? In the section on Game Loop it has some information you may be interested in, including an external link which goes into more detail.

I haven't read all of the linked site in detail, but it comes across as well thought out.

Here it is for quick ref:

http://entropyinteractive.com/2011/02/game-engine-design-the-game-loop/
{much better code}

frosty

  • Newbie
  • *
  • Posts: 13
    • View Profile
Trying to make separate timer and rendering loops
« Reply #4 on: March 11, 2012, 04:37:15 am »
Quote from: "Jove"
Mrhello, have you looked at the Basic Game Engine tutorial on the SFML Wiki? In the section on Game Loop it has some information you may be interested in


Unfortunately the wiki is out of date (as are all existing tutorials) and does not seem to reflect the new time/clock API changes.

It would be great to get some updated & complete working examples of how the new system works for us newbies and how we can implement clocks & delta timing using the new API. Thus far I have been unable to find anything current on this topic.

coolhome

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Trying to make separate timer and rendering loops
« Reply #5 on: March 11, 2012, 10:00:54 pm »
Quote from: "frosty"
It would be great to get some updated & complete working examples of how the new system works for us newbies and how we can implement clocks & delta timing using the new API. Thus far I have been unable to find anything current on this topic.


The new SFML Clock / Time system is simple. Here is a topic I found on it with delta.
http://www.sfml-dev.org/forum/viewtopic.php?t=7068&sid=4ca8b34ceb82a49c0ee67ba417382da7

Also just look up the documentation! It's the best way to learn SFML 2.0 at the moment.

http://sfml-dev.org/documentation/2.0/classsf_1_1Clock.php
http://sfml-dev.org/documentation/2.0/classsf_1_1Time.php

Best of luck!
CoderZilla - Everything Programming

frosty

  • Newbie
  • *
  • Posts: 13
    • View Profile
Trying to make separate timer and rendering loops
« Reply #6 on: March 11, 2012, 10:46:25 pm »
Quote from: "coolhome"
Quote from: "frosty"
It would be great to get some updated & complete working examples of how the new system works for us newbies and how we can implement clocks & delta timing using the new API. Thus far I have been unable to find anything current on this topic.


The new SFML Clock / Time system is simple. Here is a topic I found on it with delta.
http://www.sfml-dev.org/forum/viewtopic.php?t=7068&sid=4ca8b34ceb82a49c0ee67ba417382da7


Thanks, I did see that thread and the docs, and I think I got a simple test semi-working. I guess I'm still just trying to wrap my head around the overall concepts. Wikis and tutorials come in handy here, helping to illustrate the big picture, and most of the SFML-specific ones are broken at the moment. Maybe now that the new naming convention and API have been pushed...  :wink: