Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Making A 2D Game Engine But It Getting 0 FPS Help!

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Location
    GB
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Making A 2D Game Engine But It Getting 0 FPS Help!

    I'm Making A 2D Game Engine But I Think Something Is Wrong. FPS is 0.

    Code:

    package com.chara.engine;

    public class GameContainer implements Runnable
    {
    private Thread thread;

    private boolean running = false;
    private final double UPDATE_CAP = 1.0/60.0;

    public GameContainer()
    {

    }

    public void start()
    {
    thread = new Thread(this);
    thread.run();
    }

    public void stop()
    {

    }

    public void run()
    {
    running = true;

    boolean render = false;
    float firstTime = 0;
    float lastTime = System.nanoTime() / 1000000000f;
    double passedTime = 0;
    double unprocessedTime = 0;

    double frameTime = 0;
    int frames = 0;
    int fps = 0;

    while(running)
    {
    firstTime = System.nanoTime() / 1000000000;
    passedTime = firstTime - lastTime;
    lastTime = firstTime;

    unprocessedTime += passedTime;
    frameTime += passedTime;

    while(unprocessedTime >= UPDATE_CAP)
    {
    unprocessedTime -= UPDATE_CAP;

    render = true;

    if(frameTime >= 1.0)
    {
    frameTime = 0;
    frames = 0;
    fps = frames;
    System.out.println("FPS:"+ fps);
    }
    //TODO: Update game
    }

    if(render)
    {
    frames++;
    //TODO: Render game
    }else
    {
    try
    {
    Thread.sleep(1);
    } catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    }
    }
    dispose();

    }

    private void dispose()
    {

    }

    public static void main(String[] args)
    {
    GameContainer gc = new GameContainer();
    gc.start();
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making A 2D Game Engine But It Getting 0 FPS Help!

    Is this the code you are asking about?
       frames = 0;     // set frames to 0
       fps = frames;   // copy that 0 to fps
       System.out.println("FPS:"+ fps);   // print the 0



    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Location
    GB
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making A 2D Game Engine But It Getting 0 FPS Help!

    Quote Originally Posted by Norm View Post
    Is this the code you are asking about?
       frames = 0;     // set frames to 0
       fps = frames;   // copy that 0 to fps
       System.out.println("FPS:"+ fps);   // print the 0



    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Thanks It Worked

Similar Threads

  1. How To Add Communication From My GUI To My Game Engine
    By LarrySellers in forum AWT / Java Swing
    Replies: 1
    Last Post: September 26th, 2014, 02:58 AM
  2. Programming a game engine?
    By Tea.EarlGrey.Hot. in forum Java Theory & Questions
    Replies: 4
    Last Post: May 21st, 2014, 08:41 AM
  3. lwjgl, gamedevelopment and game engine
    By atique in forum Java Theory & Questions
    Replies: 7
    Last Post: June 28th, 2013, 12:59 PM
  4. what are game engine
    By game06 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 17th, 2013, 05:05 AM
  5. Making custom search engine
    By bochra in forum Web Frameworks
    Replies: 4
    Last Post: June 20th, 2011, 07:37 AM