Android Open Source - android-gear Java Game Loop






From Project

Back to project page android-gear.

License

The source code is released under:

MIT License

If you think the Android project android-gear listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.androidgear.core;
//from  w ww  .  j  a va  2s . co m
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

import com.androidgear.core.asset.AssetLoader;
import com.androidgear.core.asset.AssetManager;
import com.androidgear.core.asset.JavaAssetLoader;
import com.androidgear.core.config.JavaConfig;
import com.androidgear.core.dialog.DialogFactory;
import com.androidgear.core.dialog.JavaDialogFactory;
import com.androidgear.core.graphics.JavaGraphics;
import com.androidgear.core.window.JavaGameWindow;

public class JavaGameLoop implements WindowListener {
    protected JFrame mainWindow;
    private boolean active;
    private BufferStrategy bufferStrategy;
    private long expectedMilis;

    private final Game game;
    private JavaGraphics graphics;

    private long lastUpdate;
    private boolean isPointerDown;

    private JavaGameWindow gameWindow;
    private JavaConfig config;

    public JavaGameLoop(final Game game) {
        this.game = game;

        GearCommands.prepare(JavaGearCommands.class);

        DialogFactory.prepare(JavaDialogFactory.class);

        mainWindow = new JFrame(game.getClass().getSimpleName());
        mainWindow.setIgnoreRepaint(true);
        mainWindow.setLocation(100, 100);

        config = new JavaConfig();
        config.init(mainWindow, game);

        mainWindow.setVisible(true);
        mainWindow.createBufferStrategy(2);
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setResizable(false);

        gameWindow = config.getWindow();

        ((JavaDialogFactory)DialogFactory.instance()).setJFrame(mainWindow);

        mainWindow.addWindowListener(this);
        mainWindow.addMouseListener(new MouseListener() {
            @Override
            public void mouseReleased(MouseEvent event) {
                int x = event.getX() - gameWindow.getLeft();
                int y = event.getY() - gameWindow.getTop();
                if (!verifyCoords(x, y)) {
                    gameWindow.delegateClick(event.getX(), event.getY());
                    return;
                }
                isPointerDown = false;
                game.onEndPointerEvent(x, y);
            }

            @Override
            public void mousePressed(MouseEvent event) {
                int x = event.getX() - gameWindow.getLeft();
                int y = event.getY() - gameWindow.getTop();
                if (!verifyCoords(x, y)) {
                    return;
                }
                isPointerDown = true;
                game.onStartPointerEvent(x, y);
            }

            public void mouseExited(MouseEvent event) {
            }

            public void mouseEntered(MouseEvent event) {
            }

            public void mouseClicked(MouseEvent event) {
            }
        });
        mainWindow.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseMoved(MouseEvent event) {
            }

            @Override
            public void mouseDragged(MouseEvent event) {
                int x = event.getX() - gameWindow.getLeft();
                int y = event.getY() - gameWindow.getTop();
                if (!verifyCoords(x, y)) {
                    return;
                }
                if (isPointerDown) {
                    game.onPointerDragEvent(x, y);
                }
            }
        });

        graphics = new JavaGraphics();
        graphics.setWidth(gameWindow.getWindowWidth());
        graphics.setHeight(gameWindow.getWindowHeight());

        active = false;
    }

    public void terminate() {
        active = false;
    }

    public void run() {
        active = true;

        // Asset Manager
        AssetLoader loader = new JavaAssetLoader();
        AssetManager.setLoader(loader);

        // Game Init
        bufferStrategy = mainWindow.getBufferStrategy();

        expectedMilis = 1000 / game.getFrameRate();
        game.init(graphics);
        lastUpdate = System.currentTimeMillis();

        long lastRender = 0;

        // Game loop
        do {
            long update = System.currentTimeMillis();
            long delta = update - lastUpdate;

            if (lastUpdate - lastRender >= expectedMilis) {
                // Game Render
                Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();

                graphics.setG2d(g);
                gameWindow.draw(g, graphics);

                g.dispose();
                bufferStrategy.show();

                lastRender = System.currentTimeMillis();
            } else {
                // Game update
                gameWindow.update(delta);
                game.update(delta);
                Thread.yield();
            }
            lastUpdate += delta;
        } while (active);

        // Game destroy
        game.destroy();
        bufferStrategy.dispose();
        mainWindow.dispose();
    }

    private boolean verifyCoords(int x, int y) {
        return x >= 0 && x <= gameWindow.getWindowWidth() && y >= 0
                && y <= gameWindow.getWindowHeight();
    }

    public void windowClosing(WindowEvent e) {
        terminate();
    }

    public void windowOpened(WindowEvent e) {
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }
}




Java Source Code List

com.androidgear.core.AbstractAndroidGearActivity.java
com.androidgear.core.AndroidGameLoop.java
com.androidgear.core.AndroidGearCommands.java
com.androidgear.core.Game.java
com.androidgear.core.GearCommands.java
com.androidgear.core.JavaGameLoop.java
com.androidgear.core.JavaGearCommands.java
com.androidgear.core.asset.AndroidAssetLoader.java
com.androidgear.core.asset.AssetLoader.java
com.androidgear.core.asset.AssetManager.java
com.androidgear.core.asset.AssetNotFoundException.java
com.androidgear.core.asset.JavaAssetLoader.java
com.androidgear.core.config.JavaConfig.java
com.androidgear.core.dialog.AndroidDialogFactory.java
com.androidgear.core.dialog.AndroidDialog.java
com.androidgear.core.dialog.DialogButtonListener.java
com.androidgear.core.dialog.DialogFactory.java
com.androidgear.core.dialog.Dialog.java
com.androidgear.core.dialog.JavaDialogFactory.java
com.androidgear.core.dialog.JavaDialog.java
com.androidgear.core.graphics.AndroidGraphics.java
com.androidgear.core.graphics.AndroidImage.java
com.androidgear.core.graphics.AndroidSpriteSheet.java
com.androidgear.core.graphics.Font.java
com.androidgear.core.graphics.Graphics.java
com.androidgear.core.graphics.Image.java
com.androidgear.core.graphics.JavaGraphics.java
com.androidgear.core.graphics.JavaImage.java
com.androidgear.core.graphics.JavaSpriteSheet.java
com.androidgear.core.graphics.SpriteSheet.java
com.androidgear.core.sound.AndroidSound.java
com.androidgear.core.sound.JavaSound.java
com.androidgear.core.sound.Sound.java
com.androidgear.core.window.JavaGameWindow.java
com.androidgear.core.window.JavaSimpleWindow.java
com.androidgear.core.window.JavaSimulatorWindow.java
com.androidgear.samples.JavaSampleGame.java
com.androidgear.samples.SampleGameActivity.java
com.androidgear.samples.core.SampleGame.java
${package}.__GameClass__Activity.java
${package}.Java__GameClass__.java
${package}.core.__GameClass__.java