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

📄 ogre4jroot.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*  Ogre4J
    Copyright (C) 2002 macross

    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 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
*/
#include "Ogre4JRoot.h"

#include <jni.h>
#include <OgreConfigFile.h>
#include <OgreKeyEvent.h>
#include <OgreMouseEvent.h>


//==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==

Ogre4JRoot& Ogre4JRoot::instance() {
    static Ogre4JRoot singleton;
    return singleton;
}

//==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==

Ogre4JRoot::Ogre4JRoot() {
    
    root         = 0;
    renderWindow = 0;


    frameEventRef = 0;
    keyEventRef   = 0;
    mouseEventRef = 0;


    quitRenderLoop = false;

}

//==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==

Ogre4JRoot::~Ogre4JRoot() {

}

//==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==

bool Ogre4JRoot::start(
    const char* pluginFile,
    const char* ressourceFile,
    bool showConfigDialog
) {

    root = new Root(pluginFile);

    {
        ConfigFile cf;
        cf.load(ressourceFile);
    
        ConfigFile::SettingsIterator i = cf.getSettingsIterator();

        String typeName, archName;
        while (i.hasMoreElements()) {
            typeName = i.peekNextKey();
            archName = i.getNext();
            ResourceManager::addCommonArchiveEx( archName, typeName );
        }
    }

    // init window
    //if(root->restoreConfig()) {
    if(root->showConfigDialog()) {
        renderWindow = root->initialise(true);        
    }
    else {
        return false;
    }


    eventProcessor = new EventProcessor();
    eventProcessor->initialise(renderWindow);    
    eventProcessor->addKeyListener(this);
    eventProcessor->addMouseListener(this);
    eventProcessor->addMouseMotionListener(this);


    eventProcessor->startProcessingEvents(/*false*/);


    // add frame listener
    root->addFrameListener(this);    


    TextureManager::getSingleton().setDefaultNumMipMaps(10);
    
    return true;
}

//==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==

void Ogre4JRoot::exit() {
    quitRenderLoop = true;
}

//==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==

jobject Ogre4JRoot::addViewport(JNIEnv *env, jobject camera) {

    Camera* cam = (Camera*)getCppRef(camera);

    Viewport* vp = renderWindow->addViewport(cam);

    return newJRef(viewportClass, vp);
}

//==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==~==

void Ogre4JRoot::startRendering(JNIEnv *env, jobject eventHub) {
    
    this->jniEnv = env;

    this->eventHub = eventHub;
    // create listener method ids
    jclass cls = env->GetObjectClass(eventHub);

    // render loop

    char* renderLoopEventSignature = "()V";

    preRenderLoopMethodId =
        env->GetMethodID(
            cls,
            "appStarted",
            renderLoopEventSignature
        );

    postRenderLoopMethodId =
        env->GetMethodID(
            cls,
            "appEnded",
            renderLoopEventSignature
        );

    
    // frame

    char* frameEventSignature = "(Lorg/ogre4j/event/FrameEvent;)V";

    frameStartedMethodId =
        env->GetMethodID(
            cls,
            "frameStarted",
            frameEventSignature
        );

    frameEndedMethodId =
        env->GetMethodID(
            cls,
            "frameEnded",
            frameEventSignature
        );

    
    // keys

    char* keyEventSignature = "(Lorg/ogre4j/event/KeyEvent;)V";

    keyClickedMethodId =
        env->GetMethodID(
            cls,
            "keyClicked",
            keyEventSignature
        );

    keyPressedMethodId =
        env->GetMethodID(
            cls,
            "keyPressed",
            keyEventSignature
        );


    keyReleasedMethodId =
        env->GetMethodID(
            cls,
            "keyReleased",
            keyEventSignature
        );

    

    // mouse motion

    char* mouseEventSignature = "(Lorg/ogre4j/event/MouseEvent;)V";

    mouseMovedMethodId =
        env->GetMethodID(
            cls,
            "mouseMoved",
            mouseEventSignature
        );

    mouseDraggedMethodId =
        env->GetMethodID(
            cls,
            "mouseDragged",
            mouseEventSignature
        );



    // mouse     

    mouseClickedMethodId =
        env->GetMethodID(
            cls,
            "mouseClicked",
            mouseEventSignature
        );

    mouseEnteredMethodId =
        env->GetMethodID(
            cls,
            "mouseEntered",
            mouseEventSignature
        );

    mouseExitedMethodId =
        env->GetMethodID(
            cls,
            "mouseExited",
            mouseEventSignature
        );

    mousePressedMethodId =
        env->GetMethodID(
            cls,
            "mousePressed",
            mouseEventSignature
        );

    mouseReleasedMethodId =
        env->GetMethodID(
            cls,
            "mouseReleased",
            mouseEventSignature
        );



    // get classes   
    nativeObjectClass = jniEnv->FindClass("org/ogre4j/NativeObject");

    viewportClass     = jniEnv->FindClass("org/ogre4j/Viewport");
    sceneManagerClass = jniEnv->FindClass("org/ogre4j/SceneManager");
    lightClass        = jniEnv->FindClass("org/ogre4j/Light");
    cameraClass       = jniEnv->FindClass("org/ogre4j/Camera"); 
    boneClass         = jniEnv->FindClass("org/ogre4j/Bone");
    sceneNodeClass    = jniEnv->FindClass("org/ogre4j/SceneNode");
    entityClass       = jniEnv->FindClass("org/ogre4j/Entity");

    overlayClass      = jniEnv->FindClass("org/ogre4j/Overlay");
    billboardClass    = jniEnv->FindClass("org/ogre4j/Billboard");
    billboardSetClass = jniEnv->FindClass("org/ogre4j/BillboardSet");

    animationClass    = jniEnv->FindClass("org/ogre4j/Animation");
    meshClass         = jniEnv->FindClass("org/ogre4j/Mesh");
    skeletonClass     = jniEnv->FindClass("org/ogre4j/Skeleton");

    animationStateClass = jniEnv->FindClass("org/ogre4j/AnimationState");
    materialClass       = jniEnv->FindClass("org/ogre4j/Material");

    vectorClass       = jniEnv->FindClass("org/ogre4j/math/Vector3");
    quaternionClass   = jniEnv->FindClass("org/ogre4j/math/Quaternion");

    colourValueClass  = jniEnv->FindClass("org/ogre4j/ColourValue");

    logClass          = jniEnv->FindClass("org/ogre4j/Log");

	planeClass		  = jniEnv->FindClass("org/ogre4j/Plane");
	subEntityClass	  = jniEnv->FindClass("org/ogre4j/SubEntity");

    // get setter mids
    midVectorSet      = jniEnv->GetMethodID(vectorClass,      "set", "(FFF)V" );
    midQuaternionSet  = jniEnv->GetMethodID(quaternionClass,  "set", "(FFFF)V");
    midColourValueSet = jniEnv->GetMethodID(colourValueClass, "set", "(FFFF)V");
	midPlaneSet		  = jniEnv->GetMethodID(planeClass,		  "set", "(Lorg/ogre4j/math/Vector3;F)V");

    // get fids    
    fidNativeObject_self          = jniEnv->GetFieldID(nativeObjectClass, "_self", "I");    
    fidNativeObject_movableObject = jniEnv->GetFieldID(nativeObjectClass, "_movableObject", "I");    

    fidVector3X = jniEnv->GetFieldID(vectorClass, "x", "F");
    fidVector3Y = jniEnv->GetFieldID(vectorClass, "y", "F");
    fidVector3Z = jniEnv->GetFieldID(vectorClass, "z", "F");
        
    fidColourValueR = jniEnv->GetFieldID(colourValueClass, "r", "F");
    fidColourValueG = jniEnv->GetFieldID(colourValueClass, "g", "F");
    fidColourValueB = jniEnv->GetFieldID(colourValueClass, "b", "F");
    fidColourValueA = jniEnv->GetFieldID(colourValueClass, "a", "F");
        
    fidQuaternionX = jniEnv->GetFieldID(quaternionClass, "x", "F");
    fidQuaternionY = jniEnv->GetFieldID(quaternionClass, "y", "F");
    fidQuaternionZ = jniEnv->GetFieldID(quaternionClass, "z", "F");
    fidQuaternionW = jniEnv->GetFieldID(quaternionClass, "w", "F");

	fidPlaneNormal = jniEnv->GetFieldID(planeClass, "normal", "Lorg/ogre4j/math/Vector3;");
	fidPlaneD	   = jniEnv->GetFieldID(planeClass, "d", "F");

    // go
    try {        
        jniEnv->CallVoidMethod(
            eventHub,
            preRenderLoopMethodId
        );
    

        root->startRendering();

    
        jniEnv->CallVoidMethod(
            eventHub,
            postRenderLoopMethodId
        );
    }
    catch(Ogre::Exception& e) {
        printf("******* An exception has occured!\n%s\n*******\n", e.getFullDescription().c_str());
    }

⌨️ 快捷键说明

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