/*
* Copyright 2009 Markus Koller
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.blackspirit.graphics.demo;
import java.io.IOException;
import java.util.Random;
import java.util.ServiceLoader;
import javax.vecmath.Color4f;
import net.java.games.input.Component.Identifier.Key;
import net.java.games.input.Controller;
import net.java.games.input.Controller.Type;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Keyboard;
import ch.blackspirit.graphics.CanvasFactory;
import ch.blackspirit.graphics.DisplayMode;
import ch.blackspirit.graphics.DrawingMode;
import ch.blackspirit.graphics.Flip;
import ch.blackspirit.graphics.Graphics;
import ch.blackspirit.graphics.GraphicsContext;
import ch.blackspirit.graphics.GraphicsListener;
import ch.blackspirit.graphics.Image;
import ch.blackspirit.graphics.RealtimeCanvas;
import ch.blackspirit.graphics.ResourceManager;
import ch.blackspirit.graphics.View;
import ch.blackspirit.graphics.WindowListener;
import ch.blackspirit.graphics.anim.Animation;
import ch.blackspirit.graphics.anim.AnimationImpl;
import ch.blackspirit.graphics.anim.Frame;
import ch.blackspirit.graphics.anim.FrameImpl;
import ch.blackspirit.graphics.util.ColorGradientFactory;
/**
* @author Markus Koller
*/
public class SceneDemo {
private final static int WIDTH = 800;
private final static int HEIGHT = 600;
private boolean up = false;
private boolean down = false;
private boolean left = false;
private boolean right = false;
private float posX = 380;
private float posY = 250;
// Variables for drawing animation
private Animation<Frame> walk;
private int xOffset = 0;
private int lightShineRandom1 = 0;
private int lightShineRandom2 = 0;
private int lightShineRandom3 = 0;
private int lightShineRandom4 = 0;
private RealtimeCanvas canvas;
public static void main(String []args) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
SceneDemo demo = new SceneDemo();
demo.start(ServiceLoader.load(ch.blackspirit.graphics.CanvasFactory.class).iterator().next());
}
public void start(ch.blackspirit.graphics.CanvasFactory factory) throws IOException {
ControllerEnvironment controllerEnv = ControllerEnvironment.getDefaultEnvironment();
Keyboard keyboard = null;
for(Controller controller: controllerEnv.getControllers()) {
if(controller.getType() == Type.KEYBOARD) {
keyboard = (Keyboard)controller;
break;
}
}
// Create a fullscreen realtime canvas using the current display mode.
DisplayMode mode = factory.getDisplayMode(WIDTH, HEIGHT);
if(mode != null) {
canvas = factory.createRealtimeCanvasFullscreen(mode);
} else {
canvas = factory.createRealtimeCanvasFullscreen();
}
canvas.setVSync(true);
canvas.addWindowListener(WindowListener.EXIT_ON_CLOSE);
canvas.setWindowTitle("Image Demo");
final Image campfire = canvas.getImageFactory().createImage(
SceneDemo.class.getResource("/sprites/campfire.png"), false);
final Image animLeftImage = canvas.getImageFactory().createImage(
SceneDemo.class.getResource("/sprites/Crono - Walk (Left) 44x68.png"), false);
final Image animFrontImage = canvas.getImageFactory().createImage(
SceneDemo.class.getResource("/sprites/Crono - Walk (Front) 40x70.png"), false);
final Image animBackImage = canvas.getImageFactory().createImage(
SceneDemo.class.getResource("/sprites/Crono - Walk (Back) 36x70.png"), false);
final Image leftImage = canvas.getImageFactory().createImage(
SceneDemo.class.getResource("/sprites/Crono (Left).png"), false);
final Image frontImage = canvas.getImageFactory().createImage(
SceneDemo.class.getResource("/sprites/Crono (Front).png"), false);
final Image backImage = canvas.getImageFactory().createImage(
SceneDemo.class.getResource("/sprites/Crono (Back).png"), false);
final Image grass = canvas.getImageFactory().createImage(
SceneDemo.class.getResource("/sprites/grass.png"), false);
final Image dark = canvas.getImageFactory().createImage(128, 128, true);
final Image light = canvas.getImageFactory().createBufferedImage(128, 128,
BufferTypeUtil.getBest(canvas.getImageFactory(), true));
ResourceManager rm = canvas.getResourceManager();
rm.cacheImage(dark);
rm.cacheImage(campfire);
rm.cacheImage(animLeftImage);
rm.cacheImage(animFrontImage);
rm.cacheImage(animBackImage);
rm.cacheImage(leftImage);
rm.cacheImage(frontImage);
rm.cacheImage(backImage);
rm.cacheImage(grass);
final Animation<Frame> animLeft = new AnimationImpl<Frame>();
animLeft.addFrame(new FrameImpl(animLeftImage, 160000000, 0, 0, 44, 68));
animLeft.addFrame(new FrameImpl(animLeftImage, 160000000, 45, 0, 44, 68));
animLeft.addFrame(new FrameImpl(animLeftImage, 160000000, 90, 0, 44, 68));
animLeft.addFrame(new FrameImpl(animLeftImage, 160000000,135, 0, 44, 68));
animLeft.addFrame(new FrameImpl(animLeftImage, 160000000,180, 0, 44, 68));
animLeft.addFrame(new FrameImpl(animLeftImage, 160000000,225, 0, 44, 68));
animLeft.setRepeated(true);
final Animation<Frame> animFront = new AnimationImpl<Frame>();
animFront.addFrame(new FrameImpl(animFrontImage, 160000000, 0, 0, 40, 70));
animFront.addFrame(new FrameImpl(animFrontImage, 160000000, 41, 0, 40, 70));
animFront.addFrame(new FrameImpl(animFrontImage, 160000000, 82, 0, 40, 70));
animFront.addFrame(new FrameImpl(animFrontImage, 160000000,123, 0, 40, 70));
animFront.addFrame(new FrameImpl(animFrontImage, 160000000,164, 0, 40, 70));
animFront.addFrame(new FrameImpl(animFrontImage, 160000000,205, 0, 40, 70));
animFront.setRepeated(true);
final Animation<Frame> animLeftStill = new AnimationImpl<Frame>();
animLeftStill.addFrame(new FrameImpl(leftImage, 160000000, 0, 0, 28, 68));
animLeftStill.setRepeated(true);
final Animation<Frame> animFrontStill = new AnimationImpl<Frame>();
animFrontStill.addFrame(new FrameImpl(frontImage, 160000000, 0, 0, 32, 70));
animFrontStill.setRepeated(true);
final Animation<Frame> animBackStill = new AnimationImpl<Frame>();
animBackStill.addFrame(new FrameImpl(backImage, 160000000, 0, 0, 32, 66));
animBackStill.setRepeated(true);
final Animation<Frame> animBack = new AnimationImpl<Frame>();
animBack.addFrame(new FrameImpl(animBackImage, 160000000, 0, 0, 36, 70));
animBack.addFrame(new FrameImpl(animBackImage, 160000000, 37, 0, 36, 70));
animBack.addFrame(new FrameImpl(animBackImage, 160000000, 74, 0, 36, 70));
animBack.addFrame(new FrameImpl(animBackImage, 160000000,111, 0, 36, 70));
animBack.addFrame(new FrameImpl(animBackImage, 160000000,148, 0, 36, 70));
animBack.addFrame(new FrameImpl(animBackImage, 160000000,185, 0, 36, 70));
animBack.setRepeated(true);
final Animation<Frame> animFire = new AnimationImpl<Frame>();
animFire.addFrame(new FrameImpl(campfire, 120000000,1, 1, 64, 80));
animFire.addFrame(new FrameImpl(campfire, 120000000, 66, 1, 64, 80));
animFire.addFrame(new FrameImpl(campfire, 120000000, 131, 1, 64, 80));
animFire.addFrame(new FrameImpl(campfire, 120000000,196, 1, 64, 80));
animFire.setRepeated(true);
final Color4f white = new Color4f(1,1,1,1);
final Color4f red = new Color4f(1,0,0,1);
ColorGradientFactory gradientFactory = new ColorGradientFactory();
gradientFactory.addSourcePoint(light.getWidth() / 2, light.getHeight() / 2, light.getWidth() / 2f * .9f, new Color4f(0,0,0,0));
gradientFactory.setBaseColor(new Color4f(1,1,1,1));
gradientFactory.drawGradient(light);
GraphicsContext imageContext = canvas.createImageGraphicsContext(dark);
walk = animFrontStill;
// Draw the light shine
imageContext.setGraphicsListener(new GraphicsListener() {
public void draw(View view, Graphics graphics) {
view.setCamera(-400+posX, -300+posY, 0);
graphics.setClearColor(new Color4f(.0f,.0f,.0f, .98f));
graphics.clear();
drawLight(graphics, walk, light, posX + xOffset, posY, lightShineRandom3);
drawLight(graphics, animFire, light, 150 - animFire.getWidth()/2, 100 - animFire.getHeight()/2, lightShineRandom1);
drawLight(graphics, animFire, light, 650 - animFire.getWidth()/2, 100 - animFire.getHeight()/2, lightShineRandom2);
drawLight(graphics, animFire, light, 150 - animFire.getWidth()/2, 450 - animFire.getHeight()/2, lightShineRandom3);
drawLight(graphics, animFire, light, 650 - animFire.getWidth()/2, 450 - animFire.getHeight()/2, lightShineRandom4);
}
public void init(View view, Graphics renderer) {
view.setCamera(0, 0, 0);
view.setSize(1024, 1024);
}
public void sizeChanged(GraphicsContext graphicsContext, View view) {}
});
canvas.setGraphicsListener(new GraphicsListener() {
// Variables for fire simulation
Random random = new Random();
long lightShineTime = 0;
long start = System.nanoTime();
long currTime = start;
long count = 0;
long fps = 0;
Flip flip = Flip.NONE;
float angle = 0;
public void draw(View view, Graphics renderer) {
long elapsedTime = System.nanoTime() - currTime;
currTime += elapsedTime;
lightShineTime += elapsedTime;
angle += elapsedTime / 20000000f;
// Walking
if(up && !down) {
posY -= (float)elapsedTime / 11000000;
walk = animBack;
flip = Flip.NONE;
xOffset = 0;
} else if(down && !up) {
posY += (float)elapsedTime / 11000000;
walk = animFront;
flip = Flip.NONE;
xOffset = 0;
} else if(left && !right) {
posX -= (float)elapsedTime / 10000000;
walk = animLeft;
flip = Flip.NONE;
xOffset = 0;
} else if(right && !left) {
posX += (float)elapsedTime / 10000000;
walk = animLeft;
flip = Flip.VERTICAL;
xOffset = 0;
} else {
if(walk == animLeft) {
walk = animLeftStill;
if(flip == Flip.NONE) xOffset = 4;
else xOffset = 8;
}
if(walk == animFront) {
walk = animFrontStill;
xOffset = 3;
}
if(walk == animBack) {
walk = animBackStill;
xOffset = 1;
}
}
view.setCamera(posX, posY, 0);
// update animation
animLeft.update(elapsedTime);
animFront.update(elapsedTime);
animBack.update(elapsedTime);
animFire.update(elapsedTime);
renderer.setColor(white);
long gleft = (long)posX -400 - grass.getWidth()*2;
gleft = gleft - (gleft % (grass.getWidth()*2));
long gtop = (long)posY -300 - grass.getHeight()*2;
gtop = gtop - (gtop % (grass.getHeight()*2));
for(long x = gleft; x <= gleft + 800 + grass.getWidth()*2; x+=grass.getWidth() * 2) {
for(long y = gtop; y <= gtop + 600 + grass.getHeight()*2; y+=grass.getHeight() * 2) {
renderer.translate(x, y);
renderer.drawImage(grass, grass.getWidth() * 2, grass.getHeight() * 2);
renderer.clearTransform();
}
}
renderer.translate(posX + xOffset, posY);
walk.draw(renderer, walk.getWidth(), walk.getHeight(), flip);
renderer.clearTransform();
// Fires
drawFire(renderer, animFire, light, 150, 100, lightShineRandom1);
drawFire(renderer, animFire, light, 650, 100, lightShineRandom2);
drawFire(renderer, animFire, light, 150, 450, lightShineRandom3);
drawFire(renderer, animFire, light, 650, 450, lightShineRandom4);
// Draw darkness
renderer.clearTransform();
renderer.translate(posX-400, posY-300);
renderer.drawImage(dark, 1024, 1024);
renderer.setColor(white);
renderer.clearTransform();
renderer.translate(posX-400+50, posY-300+50);
renderer.drawText("Scene Demo");
// draw frames per second
renderer.setColor(red);
renderer.clearTransform();
renderer.translate(posX-400+650, posY-300+580);
renderer.drawText("FPS:" + fps);
// calculate frames per second every second
count++;
if(currTime - start > 1000000000) {
start = currTime;
fps = count;
count = 0;
System.out.println(fps);
}
// Simulate flickering of light
if(lightShineTime > 120000000) {
lightShineRandom1 = random.nextInt(10);
lightShineRandom2 = random.nextInt(10);
lightShineRandom3 = random.nextInt(10);
lightShineRandom4 = random.nextInt(10);
lightShineTime = 0;
}
}
public void init(View view, Graphics renderer) {
view.setCamera(400, 300, 0);
view.setSize(800, 600);
}
public void sizeChanged(GraphicsContext graphicsContext, View view) { }
});
// Cleaning up
System.gc();
long lastVSyncChange = 0;
while(true) {
if(keyboard != null) {
keyboard.poll();
// End demo
if(keyboard.isKeyDown(Key.Q) || keyboard.isKeyDown(Key.ESCAPE)) {
canvas.dispose();
System.exit(0);
}
// VSync
if(keyboard.isKeyDown(Key.S)) {
long time = System.currentTimeMillis();
if(time - lastVSyncChange > 1000) {
canvas.setVSync(!canvas.getVSync());
lastVSyncChange = time;
}
}
// Character movement
down = keyboard.isKeyDown(Key.DOWN);
left = keyboard.isKeyDown(Key.LEFT);
right = keyboard.isKeyDown(Key.RIGHT);
up = keyboard.isKeyDown(Key.UP);
}
imageContext.draw();
canvas.draw();
}
}
public static void drawLight(Graphics graphics, final Animation<Frame> anim, final Image light, final float x, final float y, int lightShine) {
final int shineRadX = 128 + lightShine;
final int shineRadY = 96 + lightShine;
final float lightPosX = x + anim.getWidth()/2 - shineRadX;
final float lightPosY = y + anim.getHeight()/4*3 - shineRadY;
graphics.setColor(new Color4f(.0f,.0f,.0f,1f));
graphics.setDrawingMode(DrawingMode.MULTIPLY);
graphics.translate(lightPosX, lightPosY);
graphics.drawImage(light,shineRadX*2, shineRadY*2);
graphics.clearTransform();
graphics.setDrawingMode(DrawingMode.ALPHA_BLEND);
}
public static void drawFire(Graphics renderer, final Animation<Frame> animFire, final Image light, final float fireX, final float fireY, final int lightShine) {
// Draw the fire
renderer.setColor(new Color4f(1f,1f,1f,1f));
renderer.translate(fireX - animFire.getWidth()/2, fireY - animFire.getHeight()/2);
animFire.draw(renderer, animFire.getWidth(), animFire.getHeight());
renderer.clearTransform();
}
}