Changed Entity to 3d axis and added a fpstimer that calculates the fps

This commit is contained in:
Ziver Koc 2007-03-27 17:51:02 +00:00
parent ec551064c4
commit 28c3cd0529
11 changed files with 222 additions and 72 deletions

View file

@ -1 +1,6 @@
2007-03-26 16:59:28:234 # Loading texture: data/particle.bmp
2007-03-27 19:49:02:875 # Loading texture: data/units/tank.png
2007-03-27 19:49:03:796 # Loading texture: data/particle.bmp
2007-03-27 19:49:03:953 # Loading sound: data/sounds/test.wav
2007-03-27 19:49:03:984 # Sound sources: quantity=0
2007-03-27 19:49:03:984 # Stoping sound: source=0
2007-03-27 19:49:03:984 # Playing sound: source=0 buffer=262015032

View file

@ -8,20 +8,42 @@ import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import ei.engine.state.GameStateManager;
import ei.engine.util.FpsTimer;
import ei.engine.util.MultiPrintStream;
public class LWJGLGameWindow {
// Exit app
private static boolean exit = false;
// The size of the window
private static int width;
private static int height;
// The framerate
private int fps;
// If fullscreen
private boolean fullscreen;
// The title
private String title;
// The fps calculator
private FpsTimer timer;
/**
* Creates a default window whit 800x600
* no fullscreen
*
* @param title The title of the window
*/
public LWJGLGameWindow(String title){
this(800, 600, 60, false, title);
}
/**
* Creates a custom window
* @param width The width of the window
* @param height The height of the window
* @param fps The frame rate
* @param fullscreen If fullscreen
* @param title The title
*/
public LWJGLGameWindow(int width, int height, int fps, boolean fullscreen, String title){
LWJGLGameWindow.width = width;
LWJGLGameWindow.height = height;
@ -30,6 +52,7 @@ public class LWJGLGameWindow {
this.title = title;
MultiPrintStream.makeInstance(new MultiPrintStream("log.txt"));
timer = new FpsTimer();
try {
initDisplay();
@ -101,7 +124,9 @@ public class LWJGLGameWindow {
* Runs the game (the "main loop")
*/
private void run() {
int frame = 0;
while (!exit) {
timer.startTimer();
// Always call Window.update(), all the time - it does some behind the
// scenes work, and also displays the rendered output
Display.update();
@ -133,6 +158,13 @@ public class LWJGLGameWindow {
mainRender();
}
}
//calculate fps and print it on title
if(frame >= fps/2){
timer.stopTimer();
Display.setTitle(title+" ["+timer.getFps()+"]");
frame = 0;
}
frame++;
}
}

View file

@ -74,6 +74,39 @@ public class Particles extends Entity{
setDefault();
}
private void setDefault() {
MaxSpeedX = 500;
MaxSpeedY = 500;
MaxSpeedZ = 0;
pullForceX = 0.0f;
pullForceY = 0.0f;
pullForceZ = 0.0f;
life = 1.0f;
size = 20.0f;
regenerate = true;
init();
}
private void init() {
for (int i=0;i<maxParticles;i++){ // Initials All The Textures
particle[i].active = true; // Make All The Particles Active
particle[i].life = life; // Give All The Particles Full Life
particle[i].fade = ((float)(Math.random() * 100.0)) / 1000.0f + 0.003f; // Random Fade Speed
particle[i].r = colors[i * (colors.length / maxParticles)][0]; // Select Red Rainbow Color
particle[i].g = colors[i * (colors.length / maxParticles)][1]; // Select Red Rainbow Color
particle[i].b = colors[i * (colors.length / maxParticles)][2]; // Select Red Rainbow Color
particle[i].xi = ((float)((Math.random() * MaxSpeedX)) - (MaxSpeedX/2)) * 10.0f; // Random Speed On X Axis
particle[i].yi = ((float)((Math.random() * MaxSpeedY)) - (MaxSpeedY/2)) * 10.0f; // Random Speed On Y Axis
particle[i].zi = ((float)((Math.random() * MaxSpeedZ)) - (MaxSpeedZ/2)) * 10.0f; // Random Speed On Z Axis
particle[i].xg = pullForceX; // Set Horizontal Pull To Zero
particle[i].yg = pullForceY; // Set Vertical Pull Downward
particle[i].zg = pullForceZ; // Set Pull On Z Axis To Zero
}
}
/**
* Set the number of particles
* WARNING this will reset all the particles!!
@ -98,6 +131,9 @@ public class Particles extends Entity{
}
}
/**
* Updates the particles
*/
public void update() {
if(rainbow && (delay > 25)) {
delay = 0; // Reset The Rainbow Color Cycling Delay
@ -109,39 +145,9 @@ public class Particles extends Entity{
delay++; // Increase Rainbow Mode Color Cycling Delay Counter
}
private void init() {
for (int i=0;i<maxParticles;i++){ // Initials All The Textures
particle[i].active = true; // Make All The Particles Active
particle[i].life = life; // Give All The Particles Full Life
particle[i].fade = ((float)(Math.random() * 100.0)) / 1000.0f + 0.003f; // Random Fade Speed
particle[i].r = colors[i * (colors.length / maxParticles)][0]; // Select Red Rainbow Color
particle[i].g = colors[i * (colors.length / maxParticles)][1]; // Select Red Rainbow Color
particle[i].b = colors[i * (colors.length / maxParticles)][2]; // Select Red Rainbow Color
particle[i].xi = ((float)((Math.random() * MaxSpeedX)) - (MaxSpeedX/2)) * 10.0f; // Random Speed On X Axis
particle[i].yi = ((float)((Math.random() * MaxSpeedY)) - (MaxSpeedY/2)) * 10.0f; // Random Speed On Y Axis
particle[i].zi = ((float)((Math.random() * MaxSpeedZ)) - (MaxSpeedZ/2)) * 10.0f; // Random Speed On Z Axis
particle[i].xg = pullForceX; // Set Horizontal Pull To Zero
particle[i].yg = pullForceY; // Set Vertical Pull Downward
particle[i].zg = pullForceZ; // Set Pull On Z Axis To Zero
}
}
private void setDefault() {
MaxSpeedX = 500;
MaxSpeedY = 500;
MaxSpeedZ = 0;
pullForceX = 0.0f;
pullForceY = 0.0f;
pullForceZ = 0.0f;
life = 1.0f;
size = 20.0f;
//regenerate = false;
init();
}
/**
* Draw the particles
*/
public void render() {
if(enabled){
// store the current model matrix
@ -193,7 +199,7 @@ public class Particles extends Entity{
particle[i].x = getLocation().getX(); // Center On X Axis
particle[i].y = getLocation().getY(); // Center On Y Axis
particle[i].z = getZ(); // Center On Z Axis
particle[i].z = getLocation().getZ(); // Center On Z Axis
particle[i].xi = xspeed + ((float)((Math.random() * MaxSpeedX)) - (MaxSpeedX/2)) * 10.0f; // X Axis Speed And Direction
particle[i].yi = yspeed + ((float)((Math.random() * MaxSpeedY)) - (MaxSpeedY/2)) * 10.0f; // Y Axis Speed And Direction

View file

@ -78,6 +78,15 @@ public class Vector3f {
z += i.getZ();
}
/**
* Add to the vector
* @param i The amount to add
*/
public void add(Vector2f i){
x += i.getX();
y += i.getY();
}
/**
* Add to the vector
* @param x The value to add to the x value

View file

@ -124,7 +124,8 @@ public class AnimatedSprite extends Entity {
//and sets back the rotation so that the rotation pivot is the center of the sprite
super.setTranslationGL(
-textures.get(currentAnimation)[textureId].getImageWidth()/2,
-textures.get(currentAnimation)[textureId].getImageHeight()/2);
-textures.get(currentAnimation)[textureId].getImageHeight()/2,
0);
// bind to the appropriate texture for this sprite
textures.get(currentAnimation)[textureId].bindGL();

View file

@ -19,16 +19,13 @@ public abstract class Entity {
private String name;
/** The location of this entity in pixels*/
private Vector2f location;
private Vector3f location;
/** The rotation of this entity in pixels*/
private Vector3f rotation;
/** The size of this entity in pixels*/
private Vector2f size;
/** The z layer of the screen*/
private int z = 0;
private Vector3f size;
/**
* creates a new entity
@ -37,9 +34,9 @@ public abstract class Entity {
*/
public Entity(String name){
this.name = name;
location = new Vector2f();
location = new Vector3f();
rotation = new Vector3f();
size = new Vector2f(1,1);
size = new Vector3f(1,1,1);
}
/**
@ -49,30 +46,12 @@ public abstract class Entity {
return name;
}
/**
* Get the z value of the entity
*
* @return The z value
*/
public int getZ() {
return z;
}
/**
* set the z value of the entity
*
* @param l The z value to set
*/
public void setZ(int l) {
z = l;
}
/**
* Get the Location of the entity
*
* @return The Location of the entity
*/
public Vector2f getLocation() {
public Vector3f getLocation() {
return location;
}
@ -81,10 +60,19 @@ public abstract class Entity {
*
* @param l The location of the entity
*/
public void setLocation(Vector2f l) {
public void setLocation(Vector3f l) {
location = l;
}
/**
* set the location of this entity in pixels
*
* @param l The location of the entity
*/
public void setLocation(Vector2f l) {
location = new Vector3f(l.getX(),l.getY(),getLocation().getZ());
}
/**
* Get the rotation of the entity
*
@ -108,7 +96,7 @@ public abstract class Entity {
*
* @return The Location of the entity
*/
public Vector2f getScale() {
public Vector3f getScale() {
return size;
}
@ -117,7 +105,7 @@ public abstract class Entity {
*
* @param s The size of the entity
*/
public void setScale(Vector2f s) {
public void setScale(Vector3f s) {
size = s;
}
@ -135,15 +123,16 @@ public abstract class Entity {
*
* @param x The value to add x
* @param y The value to add y
* @param z The value to add z
*/
protected void setTranslationGL(float x, float y){
protected void setTranslationGL(float x, float y, float z){
// translate to the right location and prepare to draw
GL11.glTranslatef(x,y, 0);
GL11.glTranslatef(x,y, z);
}
protected void setScaleGL(){
// translate to the right location and prepare to draw
GL11.glScalef(size.getX(), size.getY(), 0.0f);
GL11.glScalef(size.getX(), size.getY(), size.getZ());
}
/**

View file

@ -94,7 +94,7 @@ public class Sprite extends Entity {
//the rotation
super.setRotationGL();
//and sets back the rotation so that the rotation pivot is the center of the sprite
super.setTranslationGL(-texture.getImageWidth()/2,-texture.getImageHeight()/2);
super.setTranslationGL(-texture.getImageWidth()/2,-texture.getImageHeight()/2,0);
// bind to the appropriate texture for this sprite
texture.bindGL();

View file

@ -1,5 +1,6 @@
package ei.engine.sound;
import ei.engine.math.Vector3f;
import ei.engine.scene.Entity;
/**
@ -13,6 +14,7 @@ public class Sound extends Entity{
private int buffer;
private int sourceId;
private int id;
private Vector3f velocity;
/**
* Create a new sound
@ -24,6 +26,7 @@ public class Sound extends Entity{
super(name);
id = soundId;
soundId++;
velocity = new Vector3f();
this.buffer = SoundLoader.getInstnace().loadSound(ref);
}
@ -75,10 +78,14 @@ public class Sound extends Entity{
public void update() {
//AL10.alSource(source.get(0), AL10.AL_POSITION, sourcePos);
SoundManager.getInstnace().setSoundLocation(
this, getLocation().getX(),getLocation().getY(),0);
this, getLocation().getX(),getLocation().getY(),getLocation().getZ());
}
public Vector3f getVelocity(){
return velocity;
}
/**
* unimplamented method
*

View file

@ -98,6 +98,16 @@ public class SoundManager {
AL10.alSourcef(sources.get(sourceId).source, AL10.AL_GAIN, 1.0f);
AL10.alSourcei(sources.get(sourceId).source, AL10.AL_LOOPING, (loop ? AL10.AL_TRUE : AL10.AL_FALSE) );
AL10.alSource3f(sources.get(sourceId).source, AL10.AL_POSITION,
sound.getLocation().getX(),
sound.getLocation().getY(),
sound.getLocation().getZ());
AL10.alSource3f(sources.get(sourceId).source, AL10.AL_VELOCITY,
sound.getVelocity().getX(),
sound.getVelocity().getY(),
sound.getVelocity().getZ());
AL10.alSourcePlay(sources.get(sourceId).source);
sources.get(sourceId).soundId = sound.getSoundId();
return sourceId;
@ -179,6 +189,39 @@ public class SoundManager {
}
}
/**
* Set the location of the listener
*
* @param x The x coordinate
* @param y The y coordinate
* @param z The z coordinate
*/
public void setListenerLocation(float x, float y, float z){
AL10.alListener3f(AL10.AL_POSITION, x, y, z);
}
/**
* Set the velocity of the listener
*
* @param x The x coordinate
* @param y The y coordinate
* @param z The z coordinate
*/
public void setListenerVelocity(float x, float y, float z){
AL10.alListener3f(AL10.AL_VELOCITY, x, y, z);
}
/**
* Set the orientation of the listener
*
* @param x The x coordinate
* @param y The y coordinate
* @param z The z coordinate
*/
public void setListenerOrientation(float x, float y, float z){
AL10.alListener3f(AL10.AL_ORIENTATION, x, y, z);
}
/**
* 1) Identify the error code.
* 2) Return the error as a string.

View file

@ -0,0 +1,58 @@
/**
*
*/
package ei.engine.util;
import org.lwjgl.Sys;
/**
* This class calculates the fps
* @author Ziver
*
*/
public class FpsTimer {
// temp feald
private long time;
// The fps
private float fps;
/**
* Starts the timer
*
*/
public void startTimer(){
if(Sys.getTime() > 0){
time = Sys.getTime();
}
else{
time = System.currentTimeMillis();
}
}
/**
* Stops the timer and calculates the fps
*
*/
public void stopTimer(){
if(Sys.getTime() > 0){
time = Sys.getTime() - time;
}
else{
time = System.currentTimeMillis() - time;
}
// calculate the fps
fps = (float)1000/time;
// round the fps to one decimal
fps = (float)((int)(fps * 10))/10;
}
/**
* Returns the fps
*
* @return The fps
*/
public float getFps(){
return fps;
}
}

View file

@ -39,7 +39,7 @@ public class InGameState extends GameState{
@Override
public void update() {
sprite1.getLocation().add(0.5f);
sprite1.getLocation().add(new Vector2f(0.5f,0.5f));
sprite1.getRotation().add(0, 0, 0.5f);
sound1.getLocation().add(new Vector2f(1,0));
rootNode.update();