⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphicsengine.java

📁 M3DEA (Mobile 3D Engine API) 手机3D引擎API
💻 JAVA
字号:
/*
 *M3DEA - MOBILE 3D ENGINE API
 *Copyright (C) 2006 Alexandre Watanabe         alexandre_sw@yahoo.com.br     
 *		     Diego de Freitas           zenon_cc@yahoo.com.br
 *		     Paulo Rodrigo Priszculnik  paulorp@paulorp.trix.net
 *		     Rodrigo Arthur Lopes       raspl@terra.com.br
 *
 * This library is free software; you can redistribute it 
 * and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite
 * 330, Boston, MA 02111-1307 USA
 *
 */

package m3dea.graphics;

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.m3g.*;
import m3dea.audio.AudioEngine;
import m3dea.entities.EntityManager;
import m3dea.io.KeyManager;

/**
 * Classe principal do Engine, ela faz toda a parte de desenho, al閙 de coordenar a ordem de execu玢o dos outros engines
 */
public class GraphicsEngine extends GameCanvas implements Runnable
{
    // Controle Thread
    private boolean running = false;
    private boolean done = true;
    private boolean gravity = true;
   
    public static final int STRONG_RENDERING_HINTS = Graphics3D.ANTIALIAS | Graphics3D.TRUE_COLOR | Graphics3D.DITHER;
    public static final int WEAK_RENDERING_HINTS = 0;
    public static int RENDERING_HINTS = STRONG_RENDERING_HINTS;
        
    // objeto Graphics3D
    private Graphics3D g3d = null;
        
    private Graphics2D g2d = null;
    //private EntityManager entitymanager = EntityManager.getInstance();// bug??
    
    private static GraphicsEngine ge = null;
    
    /**
     * Recupera inst鈔cia do Engine
     * @return inst鈔cia do Engine
     */
    public static GraphicsEngine getInstance()
    {
        if(ge == null) ge = new GraphicsEngine();
        return ge;
    }
    
    /**
     * Seta Interface respons醰el por desenhar a parte 2D
     * @param g2d Objeto que implementa a interface Graphics2D
     */
    public void setGraphics2D(Graphics2D g2d){ this.g2d = g2d; }
        
    private GraphicsEngine()
    {
        super(true); 
                
         // roda em full screen
        setFullScreenMode(true);
                        
        g3d = Graphics3D.getInstance();
    }
   
    private void draw(Graphics g)
    {
        try
        {            
            g3d = Graphics3D.getInstance();            
            g3d.bindTarget(g, true, RENDERING_HINTS);	        

            // Renderiza todos os objetos usando EntitesManager  
            EntityManager.getInstance().render(g3d);	     
        }
        catch(Exception e) { e.printStackTrace(); }
        finally
        {
            g3d.releaseTarget();
        }
        
        // executa parte 2d definida pelo cliente
        if(g2d != null) g2d.draw(g);
    }
    
   
    /**
     * Cria uma Thread e come鏰 a rodar o engine
     */
    public void start() 
    {
        Thread t = new Thread(this);
        
        // seta status da thread
        running = true;
        done = false;
        
        t.start();        
    }
    
    /**
     * Para a execux鉶 do engine
     */
    public void stop() 
    {        
        running = false; 
        while(!done);
        EntityManager.getInstance().removeAll();
        AudioEngine.getInstance().stopAll();
    }

    /**
     * M閠odo respons醰el por coordenar a ordem de execu玢o dos eventos.
     * 1 - Processa teclas de entrada
     * 2 - Movimenta os Sprites
     * 3 - Aplica gravidade (opcional)
     * 4 - Checa Colis鉶 dos objetos
     * 5 - Desenha a cena
     */
    public void run()
    {
         while(running) 
         {
             //start = System.currentTimeMillis();
            try 
            {                
                // Processa teclas
                KeyManager.getInstance().update();
                
                // executa movimentos realizado por State (AI)
                EntityManager.getInstance().moveSprites();
                
                // aplica gravidade
                if( gravity )
                    EntityManager.getInstance().applyVisitor( EntityManager.getInstance().getGravity() );
                
                // checa colis鉶
                EntityManager.getInstance().applyVisitor( EntityManager.getInstance().getCollision() );
                                                
                // Faz toda a parte de desenho
                draw(getGraphics());
                flushGraphics();
                
                //elapsed = System.currentTimeMillis() - start;
                //if(elapsed < 20) Thread.sleep(20-elapsed);  
                Thread.sleep(20);
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        done = true;
    }

    /**
     * Verifica se gravidade est

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -