Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2013
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package nl.jorikoolstra.jLevel.Game;
  2.  
  3. import nl.jorikoolstra.jLevel.Graphics.ScreenManager;
  4.  
  5. import java.awt.*;
  6. import javax.swing.JFrame;
  7.  
  8.  
  9. public final class GameMain extends JFrame
  10. {
  11.     public static final int FPS = 75;
  12.     public static final int GENERATE_MAX_FPS = 0;
  13.  
  14.     public static final Font DEFAULT_FONT = new Font("Courier", Font.PLAIN, 24);
  15.  
  16.     private static ScreenManager screenManager;
  17.  
  18.  
  19.  
  20.     public static void main(String[] args)
  21.     {
  22.         if (args.length != 3)
  23.         {
  24.             System.exit(1);
  25.         }
  26.  
  27.         new GameMain(args);
  28.     }
  29.  
  30.  
  31.     private GameMain(String[] args)
  32.     {
  33.         try
  34.         {
  35.             if (initializeGameEnvironment(args) == false)
  36.             {
  37.                 this.dispose();
  38.                 System.exit(0);
  39.             }
  40.  
  41.             gameLoop();
  42.         }
  43.         finally
  44.         {
  45.             screenManager.restoreScreen();
  46.         }
  47.  
  48.         this.dispose();
  49.         System.exit(0);
  50.     }
  51.  
  52.  
  53.     private void gameLoop()
  54.     {
  55.         while (true)
  56.         {
  57.             Graphics2D screenGraphics = screenManager.getGraphics();
  58.  
  59.             /* Update screen */
  60.             draw(screenGraphics);
  61.             screenGraphics.dispose();
  62.             screenManager.updateGraphicsDisplay();
  63.  
  64.  
  65.             if (FPS != GENERATE_MAX_FPS)
  66.             {
  67.                 try
  68.                 {
  69.                     /* Render no more than FPS frames per second */
  70.                     Thread.sleep(1000 / FPS);
  71.                 }
  72.                 catch (InterruptedException ex) {}
  73.             }
  74.         }
  75.     }
  76.  
  77.  
  78.     private void draw(Graphics g)
  79.     {
  80.         /* Set default font */
  81.         setFont(DEFAULT_FONT);
  82.  
  83.         /* Clear the screen */
  84.         g.clearRect(0, 0, screenManager.getWidth(), screenManager.getHeight());
  85.  
  86.         g.drawString("This is just a test string too see the flickering...", 100, 100);
  87.     }
  88.  
  89.  
  90.     private boolean initializeGameEnvironment(String[] args)
  91.     {
  92.         /* ------ Initialize graphics environment ------ */
  93.         DisplayMode displayMode = new DisplayMode(Integer.parseInt(args[0]) /* width */,
  94.                               Integer.parseInt(args[1]) /* heigth */,
  95.                               Integer.parseInt(args[2]) /* bit depth */,
  96.                               DisplayMode.REFRESH_RATE_UNKNOWN /* refresh rate */);
  97.  
  98.         screenManager = new ScreenManager();
  99.  
  100.         screenManager.setFullScreen(displayMode, this);
  101.  
  102.         return true;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement