• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Dante12129

Framerate Regulator Not Working

4 posts in this topic

I have followed Lazyfoo's tutorial on regulating FPS to to make a timer for it. I have put the timer in my own library so that I can reuse it in a couple of programs. The problem is that using it doesn't actually cause any change in the framerate.

 

Appropriate part of Timer.hpp

class FPSRegulator
    {
        public:
            //The constructors and destructor
            FPSRegulator();
            FPSRegulator(int framerate);
            ~FPSRegulator();

            //Functions to operate the regulator
            void Start();
            void Delay();

        private:
            //The user-sprecified framerate
            unsigned int m_framerate;

            //Variables to store the time a tiemr has been going or been paused.
            unsigned int m_start_ticks;

            //The timer's status variables.
            bool m_started;

            //Get the ticks
            unsigned int GetTicks();
    };

 

Appropriate part of Timer.cpp

//FPSRegulator
//The constructors and destructor
grnbll::FPSRegulator::FPSRegulator()
{
    m_framerate = 30;
    m_start_ticks = 0;
    m_started = false;
}
grnbll::FPSRegulator::FPSRegulator(int framerate)
{
    m_framerate = framerate;
    m_start_ticks = 0;
    m_started = false;
}
grnbll::FPSRegulator::~FPSRegulator() = default;

//Functions to operate the regulator
void grnbll::FPSRegulator::Start()
{
    //Set the timer's status.
    m_started = true;

    //Start the timer.
    m_start_ticks = SDL_GetTicks();
}
void grnbll::FPSRegulator::Delay()
{
    if(GetTicks() < (1000/m_framerate))
    {
        SDL_Delay((1000/m_framerate)-GetTicks());
    }
}

//Private function to get the ticks
unsigned int grnbll::FPSRegulator::GetTicks()
{
    return SDL_GetTicks() - m_start_ticks;
}

 

And my main.cpp

#include "New_Engine.hpp"

int main ( int argc, char* argv[] )
{
    //Variables necessary to the screen and program
    grnbll::App app;
    grnbll::Screen screen(640, 480, "Game");
    grnbll::Event event;
    grnbll::FPSRegulator fps(30);
    fps.Start();
    bool running = true;

    SDL_Rect rect{0, 0, 50, 50};
    int x_amount = 1, y_amount = 1;

    Uint32 background_color = SDL_MapRGB(SDL_GetVideoSurface()->format, 0xFF, 0xA5, 0x00);
    Uint32 square_color = SDL_MapRGB(SDL_GetVideoSurface()->format, 0xFF, 0xFF, 0xFF);

    while(running)
    {
        //Event handling
        while(event.Poll())
        {
            if(event.GetOther().other == grnbll::OtherEvents::Quit)
            {
                running = false;
            }
        }
        //Logic
        if((rect.x + 50) > screen.GetWidth())
        {
            x_amount = -1;
        }
        if((rect.y + 50) > screen.GetHeight())
        {
            y_amount = -1;
        }
        if(rect.x <= 0)
        {
            x_amount = 1;
        }
        if(rect.y <= 0)
        {
            y_amount = 1;
        }

        rect.x += x_amount; rect.y += y_amount;

        //Rendering
        screen.SetBackgroundColor(background_color);
        SDL_FillRect(SDL_GetVideoSurface(), &rect, square_color);
        screen.Flip_Screen();
        std::cout << SDL_GetTicks() << std::endl;
        fps.Delay();
        //SDL_Delay(1000/30);
    }

    return 0;
}

 

If I use the SDL_Delay instead of the Delay method of my timer, it actually does slow it down and work. It's only with the regulator that it doesn't actually change the framerate.

0

Share this post


Link to post
Share on other sites

Unless I'm mistaken your Delay() if statement won't actually trigger.

 

You set m_start_ticks to a value when you start the counter, which means if it was at say: 100 m/s since the library started, then you're at 1500 in SDL_GetTicks, you'll end up with a value like 1400 < 33.3(rough estimate of 1000/m_framerate), which of course would only be true under very specific circumstances.

0

Share this post


Link to post
Share on other sites

Well I based it off of Lazyfoo's tutorials on calculating framerate. I made another program that uses timer class from his lesson 14 and the way he delays it, and it works. But when I do it using the Delay() function, it doesn't work even though they're pretty much the same, just adjusted for using member data and methods.

0

Share this post


Link to post
Share on other sites

You should probably reset the timer start ticks (m_start_ticks) after each delay, otherwise it will only delay once. Unless I misunderstood.

 

 

 

if(GetTicks() < (1000/m_framerate))
 

 

Will only be true when GetTicks() is between 0 and your frametime, and unless I misunderstood the GetTicks() will get arbitrarily large if you don't keep m_start_ticks equal to the start time of the last frame.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0