Creating the project

We go to develop a videogame 2d type mario bros for we to have a introduction to this magical world. We go to do this in JAVA and OpenGL, this JOGL:

pseudo mario bros type 2d videogame in JAVA OpenGL

The videogame is composed of five phases to be programmed: the elements graphics (the character, the scenary, the blocks or walls), the controls, collision detection, the logic of the videogame and textures.

From a sprite sheet of mario bros, we get two sprites of mario for our videogame: one, of mario in normal state, and two, of mario junping.

The coding.

First, we are going to create a empty JAVA project in eclise:

package-explorer nnew JAVA project

Second, we need some libraries, we are going to download the JOGL “library” that contains four libraries for linux: gluegen-rt.jar, jogl-all.jar, jogl-all-natives-linux-amd64.jar and gluegen-rt-natives-linux-amd64.jar and for windows also will be four: gluegen-rt.jar, jogl-all.jar, jogl-all-natives-windows-amd64.jar and gluegen-rt-natives-windows-amd64.jar. From here: https://jogamp.org/wiki/index.php/Downloading_and_installing_JOGL#Using_the_fat_jar , from this link: https://jogamp.org/deployment/jogamp-current/archive/jogamp-all-platforms.7z

Once downloaded the four libraries, we create in our project, the folder lib, where we copy dragging and dropping the four libraries:

the four .jar libraries in the lib folder

Now, leave the folder src empty, that is without the module-info.java created by default. And we create in src folder the JAVA Class: BasicFrame15.java.

And we do on the .jars the next: Mouse right buttom->Build Path->Add to Build Path (On the four .jar libraries). Finally we have the four libraries expand in another four (in this case, it is for linux, (for windows, it would be analogous):

The four .jar expanded

Now, we are in position of beginning to code our videogame:

On the name of the Java Class we add “implements GLEventListener”, adding more up also the reference to the corresponding library: “import com.jogamp.opengl.GLEventListener”; Then, it will be said that it must be implemented the abstract method display, dispose and reshape:

reshape, dispose, and display methods

This three methods are necessary to implement for run an OpenGL Java (JOGL) application.

Also we need to implement the main method, that it will have the assignment of five keys with the five variables corresponding to miraarriba, miraabajo, miraarriba, muevederecha, mueveizquierda y saltar, it five variables are created as global variables; In addition, we size the canvas, that it also is created here and associated to a instance of this class BasicFrame:


import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.Animator;
public class BasicFrame16 implements GLEventListener{
	static boolean miraarriba, miraabajo, mueveizquierda, muevederecha, saltar;
	public static void main(String[] args) {
		   
		   
	      //getting the capabilities object of GL2 profile
	      final GLProfile profile = GLProfile.get(GLProfile.GL2);
	      GLCapabilities capabilities = new GLCapabilities(profile);
	        
	      // The canvas
	      final GLCanvas glcanvas = new GLCanvas(capabilities);
	      BasicFrame16 b = new BasicFrame16();
	      glcanvas.addGLEventListener(b);
	      //glcanvas.addKeyListener( new BasicFrame4());
	      
	      glcanvas.setSize(400, 400);
	        
	      //creating frame
	      final Frame frame = new Frame (" Basic Frame");
	      
	      frame.addKeyListener(new KeyListener(){
	    	  public void keyPressed(KeyEvent ke)
	    		{
	    			switch(ke.getKeyCode())
	    			{
	    			case KeyEvent.VK_CONTROL:saltar = true;
	    			System.out.println(muevederecha);
	    			break;
	    			case KeyEvent.VK_DOWN:miraabajo = true;
	    			break;
	    			case KeyEvent.VK_UP:miraarriba = true;
	    			break;
	    			case KeyEvent.VK_LEFT:mueveizquierda = true;
	    			System.out.println(mueveizquierda);
	    			break;
	    			case KeyEvent.VK_RIGHT:muevederecha = true;
	    			System.out.println("muevederecha: "+muevederecha);
	    			break;
	    			}
	    		}
	    		
	    		public void keyReleased(KeyEvent ke)
	    		{
	    		}
	    		
	    		public void keyTyped(KeyEvent ke)
	    		{
	    			
	    		}
	      }); 
	      final Animator animator = new Animator(glcanvas);
	      frame.addWindowListener(new WindowAdapter() {
	    	  
	    	  @Override
	          public void windowClosing(WindowEvent e) {
	             // Use a dedicate thread to run the stop() to ensure that the
	             // animator stops before program exits.
	             new Thread() {
	                @Override
	                public void run() {
	                   if (animator.isStarted()) animator.stop();
	                   System.exit(0);
	                }
	             }.start();
	          }
	      });
	      animator.start();
	      
	      //adding canvas to frame
	      frame.add(glcanvas);
	      //frame.setExtendedState(Frame.MAXIMIZED_BOTH);
	      frame.setSize( 640, 480 );
	      frame.setVisible(true);
	      
	      
	}
}

Now, we are going to create the display method( this method is used for print the graphics in screen). For what also we do use of a method called init() that it will serve for initialize global variables that we go creating in the BasicFrame context.

The first that we go to do in this display method is to change the position of the main character depending of that variable (muevederecha or mueveizquierda) boolean is activated for the main method.

Asignación de comportamientos a teclas pulsadas del teclado

Furthermore, we declare two methods relatives with the display method (even more with the GLEventListener or the interface of JOGL application), the dispose method, for free memory after his use, and the reshape method, for redimensionating the working space in real time.

So is how our videogame goes for now:

import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;

public class BasicFrame16 implements GLEventListener{
	GL2 gl;
	GLU glu;
	float incx, incy, incxEnemy;
	static boolean miraarriba, miraabajo, mueveizquierda, muevederecha, saltar;
	@Override
    public void display(GLAutoDrawable glad){
    	final GL2 gl = glad.getGL().getGL2();//Se genera un objeto tipo GL2
    	gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
    	gl.glLoadIdentity();
    	gl.glPointSize(3.0f);
    	gl.glColor3d(24/255.0,128/255.0, 21/255.0);
    	if(muevederecha)
    	{
    		incx=incx + 1;
    		incxEnemy = incxEnemy + 1;
    		muevederecha = false;
    		//reiniciamos muevederecha, para otro keypress de teclado
    	}
    	
    	if(mueveizquierda)
    	{
    		incx=incx- 1;
    		mueveizquierda = false;
    	}
	}
	
	 @Override
	   public void dispose(GLAutoDrawable arg0) {
	      //method body
	   }
	 
	 @Override
	   public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
	    
	   }
	
	@Override
	   public void init(GLAutoDrawable glad){
	       final GL2 gl = glad.getGL().getGL2(); //Se genera un objeto de tipo GL2
	       final GLU glu = new GLU(); //Se genera un objeto de tipo GLU
	       incx=incy=0;
	       muevederecha=false;
	       mueveizquierda=false;
	       saltar=false;
	       
	   }
	
	public static void main(String[] args) {
		   
		   
	      //getting the capabilities object of GL2 profile
	      final GLProfile profile = GLProfile.get(GLProfile.GL2);
	      GLCapabilities capabilities = new GLCapabilities(profile);
	        
	      // The canvas
	      final GLCanvas glcanvas = new GLCanvas(capabilities);
	      BasicFrame16 b = new BasicFrame16();
	      glcanvas.addGLEventListener(b);
	      //glcanvas.addKeyListener( new BasicFrame4());
	      
	      glcanvas.setSize(400, 400);
	        
	      //creating frame
	      final Frame frame = new Frame (" Basic Frame");
	      
	      frame.addKeyListener(new KeyListener(){
	    	  public void keyPressed(KeyEvent ke)
	    		{
	    			switch(ke.getKeyCode())
	    			{
	    			case KeyEvent.VK_CONTROL:saltar = true;
	    			System.out.println(muevederecha);
	    			break;
	    			case KeyEvent.VK_DOWN:miraabajo = true;
	    			break;
	    			case KeyEvent.VK_UP:miraarriba = true;
	    			break;
	    			case KeyEvent.VK_LEFT:mueveizquierda = true;
	    			System.out.println(mueveizquierda);
	    			break;
	    			case KeyEvent.VK_RIGHT:muevederecha = true;
	    			System.out.println("muevederecha: "+muevederecha);
	    			break;
	    			}
	    		}
	    		
	    		public void keyReleased(KeyEvent ke)
	    		{
	    		}
	    		
	    		public void keyTyped(KeyEvent ke)
	    		{
	    			
	    		}
	      }); 
	      final Animator animator = new Animator(glcanvas);
	      frame.addWindowListener(new WindowAdapter() {
	    	  
	    	  @Override
	          public void windowClosing(WindowEvent e) {
	             // Use a dedicate thread to run the stop() to ensure that the
	             // animator stops before program exits.
	             new Thread() {
	                @Override
	                public void run() {
	                   if (animator.isStarted()) animator.stop();
	                   System.exit(0);
	                }
	             }.start();
	          }
	      });
	      animator.start();
	      
	      //adding canvas to frame
	      frame.add(glcanvas);
	      //frame.setExtendedState(Frame.MAXIMIZED_BOTH);
	      frame.setSize( 640, 480 );
	      frame.setVisible(true);
	      
	      
	}
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *