Gage game menu

Hi,
I was wondering if any one has made a game menu to go with gage?
I’ve been trying for some time but i don’t think i have the code correct.
Also wondering if anyone has came upon a timer problem with gage.
I used the map example to start a demo but the timer it uses makes my game slow down after compiling a couple times.
It sometimes causes problems with DirectX 7, then again i’m using integrated video so that might be my problem.

What timer problem are you experiencing? Give us some specifics so we can help! I have no problems with the gage timer. Do you have the timer.dll in your startup directory or jar?

As far as a game menu with GAGE, I dont think GAGE has anything specific to use, but you should be able to build a menu that launches the game or whatever without using GAGEs code.

GageMenu,

great idea!

There u go jbanes; your next project ;D

[quote]GageMenu,

great idea!

There u go jbanes; your next project ;D
[/quote]
Wonderful. Now people are assigning me work. ::slight_smile:

I was working on an API called “GAGEGui”, but I haven’t yet been able to work out a design I like. Until I figure it out, do like the Romans do. Write your own. ;D

Long Post Here We Go:

/*
 * DefaultMenu.java
 *
 * Created on April 3, 2003, 8:10 AM
 */

package com.krypto.k2.graphics.menu;

import java.awt.*;
import java.awt.event.KeyEvent;
import com.krypto.k2.graphics.*;

/**
 *
 * @author  Krypto
 */
public class DefaultMenu implements GFXMenu, GFXConstants
{

    public String name = "";
    public boolean visible = false;
    public Color color = new Color(0.5f,1.0f,0.5f,0.5f);
    public String[] menuItems = null;
    public int selected = 0;
    
    /** Creates a new instance of DefaultMenu */   
    public DefaultMenu (final String _name, final String[] _menuItems)
    {
        name = _name;
        menuItems = _menuItems;
    }
    
   
    
    /** Returns Color of menu
     * @return
     *
     */
    public java.awt.Color getColor ()
    {
        return color; 
    }
    
    /** returns selected menu item
     * @return index of selected item
     *
     */
    public int getSelectedMenuItem ()
    {
        return selected;
    }
    
    /** Used to recive key input
     * @param kE
     *
     */
    public void triggerKeyEvent (KeyEvent kE)
    {
        switch (kE.getKeyCode ())
        {
            
            case KeyEvent.VK_UP:
                
                if(selected != 0)
                {
                    selected--;
                }
                break;
                
            case KeyEvent.VK_DOWN:
                
                if(selected < menuItems.length - 1)
                {
                    selected++;
                }
                break;                
        }
    } // implement specialties in subclasses
    
    public void draw (Graphics g)
    {
        if(visible)
        {
        Font myFont=new Font ("Arial",Font.BOLD,24);
        g.setFont (myFont);
        FontMetrics myMetrics = g.getFontMetrics ();
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
        
        int longest=0;
        int numItems = menuItems.length;
            
        String longestString = menuItems[0];
        {
            for(int i =0;i<numItems;i++)
            {
                int temp = menuItems[i].toCharArray ().length;
              
                if (temp>longest)
                {
                    longest = temp;
                    longestString = menuItems[i];
                }
            }            
        }
                 
        int mX,mY,mW,mH,center;
        mW = myMetrics.stringWidth (longestString) + 40;
        mH = numItems*myMetrics.getAscent () + 40 ;
        mX = (int)((GFXEngine.screenWidth - mW)/2);
        mY = (int)((GFXEngine.screenHeight - mH)/2);
        center =(int)(myMetrics.stringWidth (longestString)/2);
                
        g.setColor (color);
        //g.clearRect(mX,mY,mW,mH);
        g.fillRoundRect (mX,mY,mW,mH,20,20);             
        
        
        int offset;
        for(int i =0;i<menuItems.length;i++)
        {
            offset=center-(int)(myMetrics.stringWidth (menuItems[i])/2);
          
            if(i == selected)
            {
               g.setColor (Color.WHITE); 
            }
            else
            {
               g.setColor (Color.BLACK); 
            }
            
            g.drawString (menuItems[i], 
                          mX+20+offset, 
                          mY+myMetrics.getAscent() + i*myMetrics.getHeight() + 10);
        }
        
       }
    }
}

of Course I didn’t include the interface code, or listener code. But this should give you a good place to start. As for JBanes, you guys leave him alone, so he’ll have free time to help me some more, as he always has :wink:

Thanks for the menu code haven’t tried it but i see i have a lot of work to do.
This is the timer error i get after trying to run my demo
I just shorted the directories com.gage… instead of com.dsalinas… if thats where i went wrong please tell me.
Also my timer.dll is in my bin folder along with the jar
and my project file.
Note also that i am using the old gage files i haven’t downloaded the newest release.


Windows 98
java.lang.UnsatisfiedLinkError: getResolution
at com.gage.timer.windows.WindowsTimer.getResolution(Native Method)
at com.gage.timer.AdvancedTimer.getTicksPerSecond(AdvancedTimer.java:192)
at Shooter.run(Shooter.java:144)
at Shooter.main(Shooter.java:98)

Thats wrong. :slight_smile: That’ll be the problem I’d have thought.

Kev

ok what i did was make all the gage folders to shorter folders instead of the com.dsalinas…folders
i just made com.gage folders.
otherwise it wouldn’t compile and i’d get lots more errors.
i also changed the code in my import and package calls to match the folders i have them in.

If you change the package of the WindowsTimer, you’ll need to recreate the RMI stubs and recompile the C code. You probably don’t want to do that.

Jbanes meant to say JNI stubs. RMI if for calling methods from a different machine over a network.

The name of the C functions for native methods contain the entire fully qualified name of the class and method.

Thanks for the correction SW. :slight_smile: Guess I had my mind on something else. :-[ :slight_smile:

Ok so i solved my timer problem by getting the latest gage release and leaving the folders alone.
I then made a copy of the shooter example and started from there. I included the gage jar files in my classpath for my project and i have everything up and running.
I have two problems though:

  1. I try to compile it to a MS-DOS batch file->shooter.bat
    but i get an error when i try running it without using my IDE/Compiler.
    Error:“Exception in thread “main” java.lang.NoClassDefFoundError”

2.I try creating the jar file Shooter.jar but it dosn’t work
when i try running it.
Error:“could not find main class program will exit”

Remeber i’m using the shooter folders i have my project file in the bin folder but my shooter.java and class in the source folder.
I’ve tried adding the shooter.class and shooter.java files to the bin then creating the jar and batch file but still recieve the same errors.

Just one last thing, I created a very simple menu for my project nothing fancy just an image with simple options
like press enter to start, press escape to quit.If anyone is interested i will display the basic code for it. I don’t really know if it’s the correct way but it works for me.

Time to head back to school for classpaths. :slight_smile: You see, the JVM has to have a way of finding the classes it uses. The way it does this is via a mechanism called the “Classpath”. The classpath is simply a list of directories or JAR files that the JVM searches to find what it wants. Let’s take this example:

java Shooter

This will most likely fail with the error “Could not find class Sprite” or something along those lines. The solution is to pass the following classpath:

java -cp …\libs\gage2d.jar;. Shooter

Above, we passed …\libs\gage2d.jar and the currect directory as classpaths. When the JVM runs it will first look in gage2d.jar for Shooter.class. If it doesn’t find it, it will move on to the current directory and find it there. After that, it will start loading GAGE classes and again look first in gage2d.jar. This time it will find the classes in the jar file. If a class exists that it can’t find in gage2d.jar or the current directory, it will throw an error. BTW, you’ll note that we used a semicolon for a separator. This changes depending on the OS. Unix type OSes use semicolons instead.

So how come the JAR worked, you ask? Because the JAR contained a special file called a Manifest. The manifest has all the commands in it that you’d normally have to pass to the command line. For example, shooter comes with a file called “shooter.mf” in the source directory. It looks something like this:

Main-Class: Shooter
Class-Path: …/lib/sound.jar …/lib/gage2d.jar …/lib/timer.jar …/lib/Resources.jar

The “Main-Class” attribute tells the JVM the class to run. The “Class-Path” attribute does what it sounds like and passes a list of paths/JARs to look for files in. You’ll note that in this case the path separator is a space. To use this manifest, we simply add the “m” command to the JAR program. i.e.:

jar -cvmf shooter.mf …\bin\shooter.jar *.class

More info can be found here:

http://java.sun.com/docs/books/tutorial/jar/basics/run.html

Good luck!