donderdag 7 oktober 2010

JGame modify background switching and progressbar during loads

JGame 3.3

Download .zip from http://13thmonkey.org/~boris/jgame/

Unpack JGame : jar xvf jgame*

Changing Background after initialize.

The 3.3 version didn´t allow changing the background during
the game. So I modified the source code±

Modified :

EngineLogic.java
Jgame\src-base\jgame\impl\EngineLogic.java

Modify this function:
public void repaintBG(JGEngineInterface eng) {


Comment out some check later in the function (scroll down approx. two pages to find bg_defined ..
// comment this since we want to change
// the background during play
//if (bg_defined[xi_mod][yi_mod]) continue;





Changing defineMedia behaviour 

Default action for defineMedia was not to show progress when chaning
levels for instance.
Another source alteration is necessary for the 3.3 version


JGame\src-jre\jgame\platform\JGEngine.java

Add below (setBGImage function)

public void setInitialised(){
canvas.setInitialised();
}
public void setUnItialised(){
canvas.setUninitialised();
}


And under JGCanvas change + add the following:

void setInitialised() {
is_initialised=true;
// Do not destroy painter function
// initpainter=null;
}
// New function
//
void setUninitialised() {
is_initialised=false;
progress_bar=0.0;
}


To recompile the jar file:

JGame\make-base.bat

Copy the JAR file to the development directory.


test.java

// MODIFIED jgame-jre.jar !!!
//
// Compile:
// javac -cp jgame-jre.jar test.java
//
// Run:
//
// test.html :
// <applet code="test" width="640" height="480" archive="jgame-jre.jar">
// </applet>

//
//
import jgame.*;
import jgame.platform.*;

//
// Demonstartion of loading level data
//
// And setting diferent backgrounds
//
public class test extends JGEngine {

String previousstate=""; // keep track of the game states

int level=1; // level number
int playernr=1000; // player number


Player player; // player object so that we can remove when
// we need to change to different player

double gamespeed=2.0; // game speed


public static void main(String [] args) { new test(0,0); }
public test() { initEngineApplet(); }
public test(int x,int y){initEngine(x,y);}
public void initCanvas() { setCanvasSettings(20,15,32,32,null,null,null); }
public void initGame() {

// backgrounds, font
defineMedia("test.tbl");

defineMedia("player"+playernr+".tbl");

setFrameRate(40,4);

setGameState("Title");

}


//----
// Title Stage
//-----
// start is called once ..
// we can ommit this for this time .. but it is left for demonstration
public void startTitle(){

}
//
// Each frame of the state this routine gets called
//
public void doFrameTitle() {
// SPACE BAR to start
if (getKey(' ')) {
//
// Create player
//
player=new Player(pfWidth()/2-25,pfHeight()-113);
gamespeed=1.0;
//
// Set state to our own state
//
setGameState("MyOwn");

}
}

//
// Each frame this PAINT routine gets called
// You put stuff on the screen in this method during the stage
//
public void paintFrameTitle() {
drawImageString("SPACE TO START",120,200,-1,"font_map",32,0);
}


// ---
// MyOwn  -- You define your own name .. it is picked up by the engine and
//           these methods get called once you do setGameState("MyOwn");
//
// ---
//
// Game State MyOwn
// start(STATE) is called once!
// Initialise objects to be used in the doFrame[state] or paintFrame[state] methods
//
public void startMyOwn(){
}
//
//
// Called each frame during the state 'MyOwn'
// -- DO NOT PAINT THINGS -- use doPaint
public void doFrameMyOwn(){

// if previous state was != myown
if(previousstate.indexOf("MyOwn")!=0){
// Set background
setBGImage("bgimg2");
}
previousstate="MyOwn";

moveObjects();
//
// manual set animation?
//defineAnimation(java.lang.String id, java.lang.String[] frames, double speed, boolean pingpong)
//


//  collision checking .. but we do not have other objects yet ..
// player = 1 , others are 2
// checkCollision(2,1);
//
// Calls the hit method for player (1)
// And gives the 2 as the object ...


}
// Paint routine for my own ..
public void paintFrameMyOwn(){

}


// ---
// GameState = load .. level load
// ---
// once called during state
public void startload(){

}

// Not used
public void doFrameload(){
previousstate="load";

// If we want a new  player ..
// if(player!=null) player.remove();

// Advance next level ..
if(level++ > 2)level=1;


//
// This sets the state to unintialised and makes sure
// that the progressbar function gets called!
//
setUnItialised();
// Maybe new frame rate?
setFrameRate(40,4);
// basic
defineMedia("levels/level"+level+".tbl");
// different objects -- we just reload the player object to use
// extra load time for demonstartion
defineMedia("player"+playernr+".tbl");

// Set the initialised state so that we remove the progress bar
setInitialised();

// new state
setGameState("MyOwn");

}
// Not used
public void paintFrameload(){
}


//------------------
// Special
//-----------------

//
// Called each state !! except for the setUninitialised state ..
//
public void paintFrame() {
drawImageString("SCORE 00 ",150,0,-1,"font_map",32,0);
}

// When state goes to game over
public void paintFrameGameOver() {
drawImageString("GAME OVER",165,200,-1,"font_map",32,0);
}

// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
// Other classes
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

// *******************************************
// Player class
// *******************************************
//
// The player takes care of movement and collision checks for
//            the player sprite
//
public class Player extends JGObject {
// Keep track of the old position ... used to determine if we moved
double oldx=0;
double oldy=0;
// What is the mininum and maximum area to move between
// (used when hitting objects, later ...)
double xmin=0;
double xmax=1024;
//
// Constructor
//
public Player(double x, double y) {
// player id          = 1
super("player",true,x,y,1,"");
}
//
// Called each frame
//
public void move() {
// Keys A and B change the background
if(getKey('A')){
clearKey('A');
setBGImage("bgimg2");
}
if(getKey('B')){
clearKey('B');
setBGImage("bgimg1");
}

if(getKey('L')){
clearKey('L');
setGameState("load");
}
//
// Player movement
//

if (getKey(KeyLeft)  && x > 14 && x > xmin) {             x -= 3*gamespeed; xdir=-1;}
if (getKey(KeyRight) && x < pfWidth()-51-14 && x < xmax){ x += 3*gamespeed; xdir=1;}
if(x!=oldx || y!=oldy){
// Walking aninatmion .. each direction has its animation
if (xdir<0) { setAnim("walking_w0000.bmp"); } else { setAnim("walking_e0000.bmp"); }
oldx=x;oldy=y;
startAnim();
}else{
// when you do not move stop animation ..
stopAnim();
}

}
//
// Object hit ..
// For now we do not have other objects ...
//
public void hit(JGObject obj) {
System.out.println("Object you hit : "+obj.toString());
}
}

// EOClass

// ****
// Other object
// ****

}
//
// RESOURCES
//
// levels\level1.tbl
// bgimg1 p 0 bgimg3.jpg - 0 0 0 0
// bgimg2 p 0 bgimg4.jpg - 0 0 0 0
//
// levels\level2.tbl
// bgimg1 p 0 bgimg3.jpg - 0 0 0 0
// bgimg2 p 0 bgimg4.jpg - 0 0 0 0


// Just use 4 jpg files as a placeholder


// test.tbl
// splash_image p 0 splash.jpg - 0 0 0 0
// font_map font293-tr.gif 0 0 32 20 0 0
// bgimg1 p 0 bgimg1.jpg - 0 0 0 0
// bgimg2 p 0 bgimg2.jpg - 0 0 0 0

// splash.jpg is just a jpg file
// font293-tr.gif is taken from the jgame examples (nebula)

// player1000.tbl

//luigismall_map luigismall.png 0 0 96 96 0 0
//walking_e0000.bmp_0 p 0 luigismall_map 0 -
//walking_e0000.bmp_1 p 0 luigismall_map 1 -
//walking_e0000.bmp_2 p 0 luigismall_map 2 -
//walking_e0000.bmp_3 p 0 luigismall_map 3 -
//walking_e0000.bmp_4 p 0 luigismall_map 4 -
//walking_e0000.bmp_5 p 0 luigismall_map 5 -
//walking_e0000.bmp_6 p 0 luigismall_map 6 -
//walking_e0000.bmp_7 p 0 luigismall_map 7 -
//walking_e0000.bmp walking_e0000.bmp_0;walking_e0000.bmp_1;walking_e0000.bmp_2;walking_e0000.bmp_3;walking_e0000.bmp_4;walking_e0000.bmp_5;walking_e0000.bmp_6;walking_e0000.bmp_7; 0.5
//walking_e0000.bmp_8 p 0 luigismall_map 8 -
//walking_e0000.bmp_9 p 0 luigismall_map 9 -
//walking_e0000.bmp_10 p 0 luigismall_map 10 -
//walking_e0000.bmp_11 p 0 luigismall_map 11 -
//walking_e0000.bmp_12 p 0 luigismall_map 12 -
//walking_e0000.bmp_13 p 0 luigismall_map 13 -
//walking_e0000.bmp_14 p 0 luigismall_map 14 -
//walking_e0000.bmp_15 p 0 luigismall_map 15 -
//walking_w0000.bmp walking_e0000.bmp_8;walking_e0000.bmp_9;walking_e0000.bmp_10;walking_e0000.bmp_11;walking_e0000.bmp_12;walking_e0000.bmp_13;walking_e0000.bmp_14;walking_e0000.bmp_15; 0.5

//
// See for jgame documentation how this animation sequence works...
//
// Basically the luigismall.png file has 16 frames in a stripe-format
// [0][1][2][3] ... [15]
// Each frame is 96 pixels high and 96 pixels wide
//
//



To compile your test program:

javac -cp jgame-jre.jar test.java

Geen opmerkingen:

Een reactie plaatsen