Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (582)
games submitted by our members
Games in WIP (503)
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: ziddia (posted 2014-01-20 19:29:00, viewed 51 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   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220  
package com.ziddia.game;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.glu.GLU;

public class GameImpl extends Game
{
   
   /** A list of all the textured quads currently loaded. */
   GLTexture[][] textureList;
   
   /** Vertex, fragment, and program IDs. */
   int vsID, fsID, pID;
   
   @Override
    public void init()
    {
      // sets up shaders
       this.setupShaders();
       
       // initializes the camera
       camera.init(pID);
       
       // set up the texture list
       textureList = new GLTexture[100][100];
       
       // gen terrain
       this.genTerrain ();
       
       // grab the mouse fps-style
       Mouse.setGrabbed(true);
       
       // fix some colouring issues...
       GL11.glEnable(GL11.GL_DEPTH_TEST);
    }

   @Override
   public void update(long elapsedTime) 
   {
      // if slowmove is set, we will move extra slowly.
      boolean slowMove = false;
      // delta movement
      float deltaMoveCam = 0.05F;
      
      /*
       * PANNING THE CAMERA
       */
      
      if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
         slowMove = true;
      
      if (slowMove)
      {
         deltaMoveCam = 0.01F;
      }
      
      if (Keyboard.isKeyDown(Keyboard.KEY_W))
         camera.move(deltaMoveCam, 1F);
      
      if (Keyboard.isKeyDown(Keyboard.KEY_S))
         camera.move(-deltaMoveCam, 1F);
      
      if (Keyboard.isKeyDown(Keyboard.KEY_A))
         camera.move(deltaMoveCam, 0F);
      
      if (Keyboard.isKeyDown(Keyboard.KEY_D))
         camera.move(-deltaMoveCam, 0F);
      
      /*
       * ROTATING THE CAMERA
       */
      float deltaY = Mouse.getDX() / 2;
      float deltaX = Mouse.getDY() / 2;
      
      camera.addRotation(-deltaX, deltaY, 0F);
      
      /*
       * ZOOMING THE CAMERA
       */
      if (Keyboard.isKeyDown(Keyboard.KEY_Z))
         camera.changeFOV(camera.fov + 1);
      
      if (Keyboard.isKeyDown(Keyboard.KEY_X))
         camera.changeFOV(camera.fov - 1);
   }

   @Override
   public void render() 
   {
      // screen cleared
      GL11.glClear (GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
      
      long curr = System.currentTimeMillis();
      
      // render each piece of terrain
      for (int x = 0; x < textureList.length; x++)
      {
         for (int y = 0; y < textureList[0].length; y++)
         {
            textureList[x][y].translate((float) x / 10, (float) y / 10, 0F);
            textureList[x][y].render();
         }
      }
      
      System.out.println (System.currentTimeMillis() - curr);
   }

   @Override
   public void dispose() 
   {
      GL20.glUseProgram(0);
      GL20.glDetachShader(pID, vsID);
      GL20.glDetachShader(pID, fsID);
      
      GL20.glDeleteShader(vsID);
      GL20.glDeleteShader(fsID);
      GL20.glDeleteProgram(pID);
      
      // unbind just in case
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
      
      for (int x = 0; x < textureList.length; x++)
      {
         for (int y = 0; y < textureList[0].length; y++)
         {
            textureList[x][y].dispose();
         }
      }
      
      Display.destroy();
   }
   
   /**
    * Simple method for setting up all our shaders.
    * Just segregated for neatness.
    */
   private void setupShaders()
   {
      /* SHADERS AND STUFF */
       vsID = Game.loadShader("shaders/default/vertex_triangle.glsl", GL20.GL_VERTEX_SHADER);
       fsID = Game.loadShader("shaders/default/fragment_triangle.glsl", GL20.GL_FRAGMENT_SHADER);
       
       // create the program
       pID = GL20.glCreateProgram();
       
       // attach the shaders
       GL20.glAttachShader(pID, vsID);
       GL20.glAttachShader(pID, fsID);
       
       // provide input to the shaders
       // Position information will be attribute 0
       GL20.glBindAttribLocation(pID, 0, "in_Position");
       // Color information will be attribute 1
       GL20.glBindAttribLocation(pID, 1, "in_Color");
       // Texture information will be attribute 1
       GL20.glBindAttribLocation(pID, 2, "in_TextureCoord");
       
       // link and validate
       GL20.glLinkProgram(pID);
       GL20.glValidateProgram(pID);
   }
   
   /**
    * Utility method to quit when GL hits an error.
    * @param errorMessage
    */
   void exitOnGLError(String errorMessage) 
   {
      int errorValue = GL11.glGetError();
      
      if (errorValue != GL11.GL_NO_ERROR) {
         String errorString = GLU.gluErrorString(errorValue);
         System.err.println("ERROR - " + errorMessage + ": " + errorString);
         
         if (Display.isCreated()) Display.destroy();
         System.exit(-1);
      }
   }
   
   /**
    * Generates some basic terrain for the game.
    * Map size is 100x100 tiles.
    */
   private void genTerrain()
   {
      GLTexture land = new GLTexture();
      
      // plot basic locations
      land.plotPosition(0, 0, 0, 1).plotColour(1, 1, 1, 1, 0).plotST(0, 0, 0);
      land.plotPosition(0.1F, 0, 0, 1).plotColour(1, 1, 1, 1, 1).plotST(0, 1, 1);
      land.plotPosition(0.1F, 0.1F, 0, 1).plotColour(1, 1, 1, 1, 2).plotST(1, 1, 2);
      land.plotPosition(0, 0.1F, 0, 1).plotColour(1, 1, 1, 1, 3).plotST(1, 0, 3);
      
      land.setShader(pID);
      land.setTexture("res/uvgrid.png");
      
      for (int y = 0; y < 100; y++)
      {
         for (int x = 0; x < 100; x++)
         {
            // make it
            land.make();
            
            // add to list
            textureList[x][y] = land;
         }
      }
   }
   
   public static void main (String[] args)
   {
      new GameImpl();
   }

}





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.

xsi3rr4x (72 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (79 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (80 views)
2014-04-14 15:48:38

tom_mai78101 (104 views)
2014-04-10 04:04:31

BurntPizza (164 views)
2014-04-08 23:06:04

tom_mai78101 (260 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!