First Game

Started by hunter2379, January 04, 2013, 19:08:34

Previous topic - Next topic

hunter2379

Hello, I'm new to the forum. Been using java for years now but never for a game. I main a main class and then a display class. The main class calls the display class as a thread. Within the display class I have
                while (!Display.isCloseRequested()) {
			//Insert Display In Here
			if (main.displayMenu==true){
				menu.start();
			}
			Display.update();
			Display.sync(60);
		}


In the menu class, in start I'm trying to load a background image. What I have so far is

       static ImageIcon mainmenu_background;

public static void start() {      
       ...
       mainmenu_background = new Image("res/mainMenu_background.jpg");
}


The error I get is Cannot instantiate the type Image.

Thanks to any one who helps, I look forward to working with you all on the forum. :)

quew8

Welcome.
Image is an abstract class. The subclass you probably want is java.awt.image.BufferedImage as this has an accessible buffer of data that can be loaded in opengl provided the BufferedImage has a compatible ColorModel. The easiest way to load an image into a BuffedImage is to use java's ImageIO's static method read(). It takes a File, InputStream or URL as an argument and returns a BufferedImage.
Forgive me if I'm jumping at shadows here but it seems like you don't quite understand the game loop else your code has confused me. You seem to be starting a thread that will load an image every frame. Games don't have much use for threads other than loading resources. All rendering and input processing actions should be done in the main thread, and image loading threads should only be started at the beginning of your program (or when they become necessary) but definitely only once. Again sorry if I've completely misunderstood the code, I'm just trying to save you some problems.

hunter2379

Hmmm, I understand and you're right. I would have run into that issue. Mind explaining to me the game loop you mentioned?

quew8

Sure thing. The game loop is everything the game does - the beating heart as it were. You have the basis there, it's just what you're putting in the loop that's odd. An lwjgl example is the best explanation I think.

Display.create();  //Creates the window. Initializes the opengl context

loadTextures(); //Loads everything the game will need, but will be unresponsive until it completes. To be fancy could do this in a separate
loadSounds();        // thread and display a splash screen until then.
createSceneObjects();

while(!Display.isCloseRequested()) { //isCloseRequestes() tells you whether the close button has been pressed yet. 
    int delta = getChangeInTime();   //Work out the time since the last frame started. Hence how far things should move etc.
                                                     //Will be necessary for any game I can think of.
    processInputs();  //Using Keyboard and Mouse class.
    if(main.displayMenu) {
         drawMenu();  //Draw the menu.
    } else {
         updateGame(delta);  //Decide whats moving where, whats hit what, who's dead and who killed them etc.
         drawGameScene();  //Draw all the monsters or whatever.
    }
    
    Display.update(); //Flips the buffers (puts what you have drawn on the window) and polls the input device ready for the next frame.
    Display.sync(60);  //Limits the number of frames (loop iterations) can happen every second so your cpu doesn't burn.
                               //The reason you need to work out delta above is that the game may still run slower than this on slower hardware.
} //Each iteration of this loop is one frame of your game.

//Release any other resources you might have.
Display.destroy();


I hope this is helpful to you. One link I would suggest is the tutorial on timing on the lwjgl wiki. http://www.lwjgl.org/wiki/index.php?title=LWJGL_Basics_4_(Timing). It is very good, in fact they all are, I would advise giving all the basics a read if you haven't already seeing as it's your first game. There's also an example space invaders game there whose code you can browse to get a feel for what your doing.

hunter2379

Holy crap! I understand it and this has inspired me! I get it! I f*cking get it! Thanks, now I understand where I'm going with everything.

Thanks!

hunter2379

I think im going to start fresh seeing how i seem to have messed up somewhere but dont have a lot written down. New error is

Exception in thread "main" java.lang.RuntimeException: No OpenGL context found in the current thread.
   at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
   at org.lwjgl.opengl.GL11.glEnable(GL11.java:1000)
   at main.initGL(main.java:84)
   at main.start(main.java:51)
   at main.main(main.java:122)


Edit: Never Mind! Fixed it.