Archive for October, 2009

box

Hola everyone.

I am currently working on my own two game projects: one project is of mobile phone game and other is 2.5 or side-scrolling 3d game, not much sure. So for second project i am making an small engine that will be used to make 2.5D game or side scrolling 3d game, Just simple and fun game. I have chosen LWJGL, which is a java binding for Opengl to make my game. So first thing i need to work on loading images.texture mapping in lwjgl. I found some pretty much useful tutorials and great help from lwjgl forums. But atlast i got something that i was looking for from the forum.

I came to know about slick-util which is free and open source lib to load images and play sounds. Its very easy to pick up and implement it. Its just worked flawlessly and smoothly for me. You can download the slick-util from here. PNG, GIF, JPG, TGA are supported and WAV, OGG, XM sounds are supported by slick-util.

So here i’l show u how easily i loaded images and mapped to a 3D BOX. First u have to setup lwjgl and slick-util with netbeans. Yeah i am using netbeans to write my program. You can see my post to setup lwjgl with netbeans. Now we will set up slick-util, its a continuation to the steps that we did to setup lwjgl.

Continued…

Step 1: We just have to configure the project properties so that project can find the lib to compile and run.

1

2

Ok that was it. Now slick-util lib has been set up.Now first i’l show u how to load image and bind it , full source will be at the end.

Texture texture;

Texture is an interface having description of texture which will be loaded.

texture = TextureLoader.getTexture(“PNG”, new FileInputStream(“Data/Crate.png”));

now we are loading the texture using TextureLoader utility class. Just smooth and simple.

texture.bind();

Now we are binding the texture.

So thats what u need to load the image and map them to objects. Download this image to use it in this program.

Crate

Full Code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gaanza.engine.test2;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

/**
 *
 * @author Java Guy
 */

public class EngineTest {

    private static boolean gameRunning=true;
    private static int targetWidth = 800;
    private static int targetHeight = 600;

    private float xrot=0.1f;
    private float yrot=0.1f;
    private float zrot=0.1f;

    /** The texture that’s been loaded */
        private Texture texture;

    public static void main(String[] args){
        EngineTest app=new EngineTest();
        initDisplay(false);
        initGL();
        app.init();
        app.run();
    }

    private static void initDisplay(boolean fullscreen){

        DisplayMode chosenMode = null;

        try {
                DisplayMode[] modes = Display.getAvailableDisplayModes();

                for (int i=0;i<modes.length;i++) {
                    if ((modes[i].getWidth() == targetWidth) && (modes[i].getHeight() == targetHeight)) {
                        chosenMode = modes[i];
                        break;
                    }
                }
            } catch (LWJGLException e) {
        Sys.alert(“Error”, “Unable to determine display modes.”);
        System.exit(0);
    }

        // at this point if we have no mode there was no appropriate, let the user know
    // and give up
        if (chosenMode == null) {
            Sys.alert(“Error”, “Unable to find appropriate display mode.”);
            System.exit(0);
        }

        try {
            Display.setDisplayMode(chosenMode);
            Display.setFullscreen(fullscreen);
            Display.setTitle(“LWJGL window”);
            Display.create();
        }
        catch (LWJGLException e) {
            Sys.alert(“Error”,“Unable to create display.”);
            System.exit(0);
        }

}

    private static boolean initGL(){
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();

//        Calculate the aspect ratio of the window
        GLU.gluPerspective(45.0f,((float)targetWidth)/((float)targetHeight),0.1f,100.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();

        GL11.glEnable(GL11.GL_TEXTURE_2D);                                    // Enable Texture Mapping ( NEW )
        GL11.glShadeModel(GL11.GL_SMOOTH);
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GL11.glClearDepth(1.0f);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthFunc(GL11.GL_LEQUAL);
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
        return true;
    }

    private void init(){
        try {
            texture = TextureLoader.getTexture(“PNG”, new FileInputStream(“Data/grass.png”));
        } catch (IOException ex) {
            Logger.getLogger(EngineTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void run(){
        while(gameRunning){
            update();
            render();
            Display.update();

            // finally check if the user has requested that the display be
            // shutdown
            if (Display.isCloseRequested()) {
                    gameRunning = false;
                    Display.destroy();
                    System.exit(0);
                }
            }
    }

    private void update(){
        xrot+=0.1f;
        yrot+=0.1f;
        zrot+=0.1f;
    }

    private void render(){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();

        GL11.glTranslatef(0.0f,0.0f,-5.0f);                              // Move Into The Screen 5 Units
        GL11.glRotatef(xrot,1.0f,0.0f,0.0f);                        // Rotate On The X Axis
                GL11.glRotatef(yrot,0.0f,1.0f,0.0f);                        // Rotate On The Y Axis
                GL11.glRotatef(zrot,0.0f,0.0f,1.0f);                        // Rotate On The Z Axis

        texture.bind(); // or GL11.glBind(texture.getTextureID());

        GL11.glBegin(GL11.GL_QUADS);

                // Front Face
                GL11.glTexCoord2f(0.0f, 0.0f);
                GL11.glVertex3f(-1.0f, -1.0f,  1.0f);   // Bottom Left Of The Texture and Quad
                GL11.glTexCoord2f(1.0f, 0.0f);
                GL11.glVertex3f( 1.0f, -1.0f,  1.0f);   // Bottom Right Of The Texture and Quad
                GL11.glTexCoord2f(1.0f, 1.0f);
                GL11.glVertex3f( 1.0f,  1.0f,  1.0f);   // Top Right Of The Texture and Quad
                GL11.glTexCoord2f(0.0f, 1.0f);
                GL11.glVertex3f(-1.0f,  1.0f,  1.0f);   // Top Left Of The Texture and Quad

                // Back Face
                GL11.glTexCoord2f(1.0f, 0.0f);
                GL11.glVertex3f(-1.0f, -1.0f, -1.0f);   // Bottom Right Of The Texture and Quad
                GL11.glTexCoord2f(1.0f, 1.0f);
                GL11.glVertex3f(-1.0f,  1.0f, -1.0f);   // Top Right Of The Texture and Quad
                GL11.glTexCoord2f(0.0f, 1.0f);
                GL11.glVertex3f( 1.0f,  1.0f, -1.0f);   // Top Left Of The Texture and Quad
                GL11.glTexCoord2f(0.0f, 0.0f);
                GL11.glVertex3f( 1.0f, -1.0f, -1.0f);   // Bottom Left Of The Texture and Quad

                // Top Face
                GL11.glTexCoord2f(0.0f, 1.0f);
                GL11.glVertex3f(-1.0f,  1.0f, -1.0f);   // Top Left Of The Texture and Quad
                GL11.glTexCoord2f(0.0f, 0.0f);
                GL11.glVertex3f(-1.0f,  1.0f,  1.0f);   // Bottom Left Of The Texture and Quad
                GL11.glTexCoord2f(1.0f, 0.0f);
                GL11.glVertex3f( 1.0f,  1.0f,  1.0f);   // Bottom Right Of The Texture and Quad
                GL11.glTexCoord2f(1.0f, 1.0f);
                GL11.glVertex3f( 1.0f,  1.0f, -1.0f);   // Top Right Of The Texture and Quad

                // Bottom Face
                GL11.glTexCoord2f(1.0f, 1.0f);
                GL11.glVertex3f(-1.0f, -1.0f, -1.0f);   // Top Right Of The Texture and Quad
                GL11.glTexCoord2f(0.0f, 1.0f);
                GL11.glVertex3f( 1.0f, -1.0f, -1.0f);   // Top Left Of The Texture and Quad
                GL11.glTexCoord2f(0.0f, 0.0f);
                GL11.glVertex3f( 1.0f, -1.0f,  1.0f);   // Bottom Left Of The Texture and Quad
                GL11.glTexCoord2f(1.0f, 0.0f);
                GL11.glVertex3f(-1.0f, -1.0f,  1.0f);   // Bottom Right Of The Texture and Quad

                // Right face
                GL11.glTexCoord2f(1.0f, 0.0f);
                GL11.glVertex3f( 1.0f, -1.0f, -1.0f);   // Bottom Right Of The Texture and Quad
                GL11.glTexCoord2f(1.0f, 1.0f);
                GL11.glVertex3f( 1.0f,  1.0f, -1.0f);   // Top Right Of The Texture and Quad
                GL11.glTexCoord2f(0.0f, 1.0f);
                GL11.glVertex3f( 1.0f,  1.0f,  1.0f);   // Top Left Of The Texture and Quad
                GL11.glTexCoord2f(0.0f, 0.0f);
                GL11.glVertex3f( 1.0f, -1.0f,  1.0f);   // Bottom Left Of The Texture and Quad

                // Left Face
                GL11.glTexCoord2f(0.0f, 0.0f);
                GL11.glVertex3f(-1.0f, -1.0f, -1.0f);   // Bottom Left Of The Texture and Quad
                GL11.glTexCoord2f(1.0f, 0.0f);
                GL11.glVertex3f(-1.0f, -1.0f,  1.0f);   // Bottom Right Of The Texture and Quad
                GL11.glTexCoord2f(1.0f, 1.0f);
                GL11.glVertex3f(-1.0f,  1.0f,  1.0f);   // Top Right Of The Texture and Quad
                GL11.glTexCoord2f(0.0f, 1.0f);
                GL11.glVertex3f(-1.0f,  1.0f, -1.0f);   // Top Left Of The Texture and Quad
        GL11.glEnd();
    }
}

Gracias.

Diving into Mobile Gaming !!

Recently my friends suggested me to make games for mobile phones. I haven’t done any formal course for mobile programming so i did a some search on mobile game development on google and gone through some articles like The Clash of Mobile Platforms: J2ME, ExEn, Mophun and WGE

I have a Nokia N70ME handset and most of my friends have same kinda(specifications) handset. All of them are java enabled. I did a search on their specifications and found that almost all were midp 2.0 enabled. After doing some research on mobile gaming i chose J2ME for developing mobile games because majority of the phones of my target audience are java-enabled so i can reach to masses. Secondly, SUN provides you all the development tools for free. And thirdly i am more comfortable in java programming language so it would be easier for me to pick up things more fast and easily. So i am using netbeans IDE 6.7 with the mobility pack to write my app and its pretty productive and reliable. My next step was to find some good online tutorial to just get started, all basic things. I searched the google and found many useful stuff there:

http://developers.sun.com/mobility/midp/articles/gameapi/

http://developers.sun.com/mobility/midp/articles/game/

http://today.java.net/pub/a/today/2005/07/07/j2me3.html

http://www.devx.com/wireless/Article/27167/1954

So i got many stuffs to get started making midp 2.0 games. It didn’t take too much time to pick up things. I got the concepts easily and started implementing them. I just started with testing codes like drawing images, moving them, rotation etc. After testing all those codes i decided to make very simple 2d side scrolling game. Then i searched for some free sprites on google and got some stuffs on world war 2 theme. Then i asked my friend to make some background images for me and he made some pretty good backgrounds. So then i was off to fly and started making the game.

This is my current progress on game and i am pretty sure that soon i’ll complete the game and upload here for the download.

jungleMobile

This is my mobile game project that i am working. Soon i’l finish it. And important thing is that i am enjoying very much making game for mobile phones. I tested it on my Nokia N70 handset and it worked flawlessly. I am very excited regarding this project.

Gracias.