Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Stuttering in very simple Java2D fullscreen exclusive mode game on MS Windows  (Read 1760 times)
0 Members and 1 Guest are viewing this topic.
Offline surban

Junior Newbie





« Posted 2007-06-10 01:55:04 »

Hi!

I'm trying to write a simple game using Java2D and fullscreen exclusive mode api. I have followed the tutorials and examples, but I am not able to get

this little demo working smoothly on Vista. Mac OS X runs without problems! I have attached the source. A prebuilt JAR is available from http://gambitchess.org/moin.py/StutterDemo

Using the vsync hack as described in http://www.java-gaming.org/forums/index.php?topic=14696.0 does help a bit (framerate is locked to 60 Hz then), but

it stutters anyways. For running with vsync hack, please make sure, that pxsync.dll is in your working directory.

Thank you!

Specs: JDK 1.6, Intel Core 2 Duo, Windows Vista 32-bit, ATI Radeon X1600, DirectX Games work fine..

Source of Main.java:



/** send comments and ideas to sebastian.urban@gmx.de */

package stutterdemo;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

// uncomment this and other lines below to enable vsync hack
//import de.pxlab.pxl.VideoSync;

/** This demo shows stuttering of simple graphics in fullscreen-mode using Java2D
 * on MS Windows.
 *
 * NOTE: This demo runs fine on Mac OS X!
 */
public class Main extends Frame implements Runnable {
   
    /** set to true to enable logging of frame delays */
    final boolean writeLog = false;
   
    /** path to logfile (make sure you have write permissions, especially on Vista!) */
    final String logPath = "C:\\users\\public\\StutterDemoLog.csv";
   
    /** set to true to change display mode at startup */
    final boolean changeDisplayMode = false;
   
    /** list of preferred display modes */
    private static DisplayMode[] BEST_DISPLAY_MODES = new DisplayMode[] {
        //new DisplayMode(1680, 1050, 32, 0),
        new DisplayMode(1024, 768, 32, 0),
        new DisplayMode(1024, 768, -1, 0), // for Linux
    };
   
    /** graphics device for fullscreen mode */
    GraphicsDevice device;
   
    /** used to stop the game thread on key press */
    boolean gameShouldRun = true;
   
    /** x coordinate of the box in the simulation */
    double boxX = 10;
   
    /** Creates a new instance of Main */
    public Main(GraphicsConfiguration gc, GraphicsDevice device) {
        super(gc);
       
        this.device = device;
       
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                gameShouldRun = false;
            }
        });
       
        setResizable(false);
        setUndecorated(true);
        setIgnoreRepaint(true);
        device.setFullScreenWindow(this);
        setVisible(true);
       
        if (!device.isFullScreenSupported()) {
            JOptionPane.showMessageDialog(null, "Fullscreen mode not supported!");
            System.exit(1);
        }
        if (changeDisplayMode) {
            if (!device.isDisplayChangeSupported()) {
                JOptionPane.showMessageDialog(null, "Display mode change not supported!");
                System.exit(2);
            }
            chooseBestDisplayMode(device);
        }
       
        createBufferStrategy(2);
        new Thread(this).start();
    }
   
   
    /** gets best display mode */
    private static DisplayMode getBestDisplayMode(GraphicsDevice device) {
        for (int x = 0; x < BEST_DISPLAY_MODES.length; x++) {
            DisplayMode[] modes = device.getDisplayModes();
            for (int i = 0; i < modes.length; i++) {
                if (modes.getWidth() == BEST_DISPLAY_MODES
  • .getWidth()
                        && modes.getHeight() == BEST_DISPLAY_MODES
  • .getHeight()
                        && modes.getBitDepth() == BEST_DISPLAY_MODES
  • .getBitDepth()
                        ) {
                    return modes;
                }
            }
        }
        return null;
    }
   
    /** sets the display mode to the preferred mode */
    protected static void chooseBestDisplayMode(GraphicsDevice device) {
        DisplayMode best = getBestDisplayMode(device);
        if (best != null) {
            device.setDisplayMode(best);
        }
    }
   
   
    /** game loop thread */
    public void run() {
        BufferStrategy bufferStrategy = getBufferStrategy();
       
        BufferedWriter logWriter = null;
       
        long startTime = System.nanoTime();
        long lastTickStartTime = System.nanoTime();
        long frameCount = 0;
        long lastFrameCountStartTime = lastTickStartTime;
        long frameRateCalcInterval = 40000 * 1000 * 1000;
        double frameRate = 0;
        long totalFrames = 0;
       
        if (writeLog) {
            try {
                logWriter = new BufferedWriter(new FileWriter(logPath));
                logWriter.write("Frame; lastTickDuration (mSec); Time since start (mSec); boxX\n");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
       
        // GameLoop
        while (gameShouldRun) {
            long lastTickDuration = System.nanoTime() - lastTickStartTime;
            lastTickStartTime = System.nanoTime();
           
            if (logWriter != null) {
                try {
                    logWriter.write(String.format("%d; %d; %f; %f\n", totalFrames, lastTickDuration / (1000 * 1000),
                            (float) (lastTickStartTime - startTime) / (1000f * 1000f),
                            boxX));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                totalFrames++;
            }
           
            // calc framerate
            frameCount++;
            if (lastFrameCountStartTime + frameRateCalcInterval < lastTickStartTime) {
                frameRate = (double) frameCount / (double) (lastTickStartTime - lastFrameCountStartTime) * 1000 * 1000 * 1000;
                frameCount = 0;
                lastFrameCountStartTime = lastTickStartTime;
            }
           
            // run simulation step
            simulation(lastTickDuration);
           
            // redraw
            Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
            if (!bufferStrategy.contentsLost()) {
                drawScene(g);
                g.setColor(Color.WHITE);
                g.drawString(String.format("%.0f", frameRate), getWidth() - 50, getHeight());
                g.dispose();
               
                // UNCOMMENT the following to enable vsync hack
                // does help, but with it, it stutters every 3-4 seconds
                //if (VideoSync.isEmulated()) // make sure it's not emulated
                //    System.exit(3);
                //VideoSync.waitForBeginOfVerticalBlank();
               
                bufferStrategy.show();
            }
           
            // run event thread, otherwise key events get stuck
            Thread.yield();
        }
       
        // clean up
        try {
            if (logWriter != null)
                logWriter.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                device.setFullScreenWindow(null);
                dispose();
            }
        });
    }
   
    /** game animation */
    protected void simulation(double t) {
        boxX += t / (1000 * 1000) * 11/15;
       
        if (boxX > getWidth())
            boxX = 0;
    }
   
    /** scene drawer */
    protected void drawScene(Graphics2D g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
       
        g.setColor(Color.WHITE);
        g.fillRect((int)boxX, getHeight()/2, 20, 20);
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        SwingUtilities.invokeLater(new Thread() {
            public void run() {
                GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice device = env.getDefaultScreenDevice();
                GraphicsConfiguration gc = device.getDefaultConfiguration();
               
                new Main(gc, device);
            }
        });
    }
}



Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #1 - Posted 2007-06-12 23:39:44 »

This could be because on Vista the default accelerated Java2D pipeline is
not available (because it's incompatible with Vista's DWM), so all rendering
goes through software.

Another thing: do not make your frame non-resizeable, it needs to be
resized to the display mode's dimensions when it goes
full-screen..

Thanks,
  Dmitri
Offline surban

Junior Newbie





« Reply #2 - Posted 2007-06-16 20:10:33 »

Thanks,

when will the accelerated pipeline be available for Vista?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline taifunbrowser

Senior Newbie





« Reply #3 - Posted 2007-06-17 01:51:05 »

's scary as it looks, you really should try opengl Sad Seriously, just spend the time and stress out over getting it installed: You'll have alot of trouble getting it to work, but when you do, you will be happy.

Just do it.

http://beam.to/jtouhou - the JTouhou project! Mixed 3d and 2d, a multiplatform port for the classic dojin-soft series by Shanghai alice!
Offline woogley
« Reply #4 - Posted 2007-06-17 18:06:52 »

runs smoothly on my vista @ 160fps

vista 32bit // 3Ghz Core Duo // 1GB ram //  ATI VisionTek Radion X1550 // 256MB vram
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #5 - Posted 2007-06-20 22:08:10 »

Thanks,

when will the accelerated pipeline be available for Vista?


The current plan is the "Consumer JRE" release (search for more info),
due to be out sometime next year.

Alternatively you can use the OpenGL pipeline (if you install the drivers).

Dmitri
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (40 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!