Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: theagentd (posted 2013-01-10 10:25:50, viewed 237 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175  
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

import static org.lwjgl.opengl.GL11.*;

public class GameWindow
{
   //Player class
   class player
   {
      //vars
      double x;
      double y;
      double prevX;
      double prevY;
      double viewX;
      double viewY;
      double size;

      //constructor
      player()
      {
         x = 25;
         y = 150;
         prevX = x;
         prevY = y;
         viewX = x;
         viewY = y;
         size = 64;
      }
   }

   //Create a player 
   player p = new player();

   //Main
   public static void main(String[] argv)
   {
      GameWindow gWin = new GameWindow();
      gWin.run();
   }

   //Run function
   public void run()
   {
      //Attempt to create the window
      try
      {
         //Set the size and create it
         Display.setDisplayMode(new DisplayMode(800, 600));
         Display.setVSyncEnabled(true);
         Display.create();
      }
      catch(LWJGLException e)
      {
         int j = GL11.glGetError();
         System.out.println("ERROR : " + j);

         //e.printStackTrace();
         System.exit(0);
      }

      //Init openGL
      glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      //glDisable(GL_DEPTH_BUFFER_BIT);

      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();

      glOrtho(0, 800, 600, 0, -1, 1);

      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      int err = GL11.glGetError();
      if(err != GL_NO_ERROR)
      {
         System.out.println("err");
         System.exit(0);
      }

      //set up the need vars and timers
      long updateTime = 1000 * 1000 * 1000 / 25;
      int loops = 0;
      int MaxLoops = 10;
      int frames = 0;

      long gameClock = System.nanoTime();
      long lastFrame = gameClock;


      //The GAME LOOP! T.T
      while(!Display.isCloseRequested())
      {
         //For FPS count
         frames++;

         loops = 0;
         while((System.nanoTime() >= gameClock) && (loops < MaxLoops))
         {
            //Store the player's previous position
            p.prevX = p.x;

            //Move the player
            //If the go "out of bound" reset their position to the left side of the screen
            if(p.x > 800)
            {
               p.x = 0 - p.size;
               p.prevX = 0 - p.size;
            }
            else
               p.x += 7;

            //Update the loop count and game clock
            gameClock += updateTime;
            loops++;
         }

         //Calculate for the interpolation value
         double alpha = (double)(System.nanoTime() - gameClock + updateTime) / updateTime;
         System.out.println(alpha);

         //Account for the interpolation
         p.viewX = p.prevX + alpha * (p.x - p.prevX);
         p.viewY = p.prevY + alpha * (p.y - p.prevY);

         //Get the current FPS
         if(System.currentTimeMillis() - lastFrame > 1000)
         {
            Display.setTitle("FPS : "+ frames);
            frames = 0;
            lastFrame += 1000;
         }

         //Draw
         draw();

         //UPdate the display
         Display.update();
      }

      Display.destroy();
   }

   //Draw function
   void draw()
   {
      glClear(GL_COLOR_BUFFER_BIT);

      glBegin(GL_QUADS);

      glColor3f(1, 1, 1);
      glVertex2d(0, 0);
      glVertex2d(800, 0);
      glVertex2d(800, 600);
      glVertex2d(0, 600);

      glEnd();

      glBegin(GL_QUADS);

      glColor3f(0, 0, 0);
      glVertex2d(p.viewX, p.viewY);
      glVertex2d(p.viewX + p.size, p.viewY);
      glVertex2d(p.viewX + p.size, p.viewY + p.size);
      glVertex2d(p.viewX, p.viewY + p.size);

      glEnd();
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

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

The first screenshot will be displayed as a thumbnail.

pw (12 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!