📄 demo.cpp
字号:
/******************************************************************************
* This source code is part of Bad Camel Gaming
* Copyright (C) 2002 Zephie Greyvenstein
* See Readme.html for acknowledgements
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*****************************************************************************/
/******************************************************************************
* FILENAME : demo.cpp
* DESCRIPTION : Main demo controlling class
* AUTHOR : Zephie Greyvenstein
*****************************************************************************/
#include "demo.h"
#include "oasisGraphicsSystem.h"
#include "oasisObjectSystem.h"
#include "oasisObject.h"
#include "oasisFrameListenerSystem.h"
#include "oasisCameraSystem.h"
#include "oasisSoundSystem.h"
#include "oasisInputSystem.h"
#include "oasisPhysicsSystem.h"
#include "oasisConsoleSystem.h"
#ifdef HAS_LUA
#include "lua.h"
#include "oasisScriptLuaInterpreter.h"
extern int tolua_demo_open( lua_State * );
#endif
#ifdef HAS_RUBY
#include "oasisScriptCommon.h"
extern "C" {
extern void Init_Sample( void );
}
#endif
template< > demo *singleton< demo >::singletonPtr = 0;
demo::demo( ) {
// Create collision listener
reactor = new collision( );
}
demo::~demo( ) {
if ( reactor ) {
delete reactor;
}
}
demo &demo::get( void ) {
return singleton< demo >::get( );
}
demo *demo::getPtr( void ) {
return singleton< demo >::getPtr( );
}
bool demo::startDemo( Oasis::scriptSystem::Language language ) {
/* This demo shows a simple example of how to use the components of the
framework. Functionality has been mixed between C++ and scripts.
*/
// Set up systems
uint32 flags = root::SS_OBJECT |
root::SS_SCRIPT |
root::SS_INPUT |
root::SS_SOUND |
root::SS_PHYSICS |
root::SS_CAMERA |
root::SS_CONSOLE;
// Start the systems
root::get( ).startSystems( flags );
// We need to extend the specific languages in order to initialize
// subsystems.
scriptSystem::get( ).extend( this );
// Choose the language.
scriptSystem::get( ).initLanguage( language );
// Register mouse listener
inputSystem::get( ).addMouseListener( this );
// Register frame listener
frameListenerSystem::get( ).addFrameListener( this );
cameraSystem::get( ).setSoundListener( true );
// Run script
scriptSystem::get( ).executeScript( "null" );
consoleSystem::get( ).open( );
consoleSystem::get( ).write( "\nWelcome to the demo system." );
if( scriptSystem::get().getLanguage( ) == Oasis::scriptSystem::LRUBY )
consoleSystem::get( ).write( "Use \"$demo.help()\" for instructions\n" );
if( scriptSystem::get().getLanguage( ) == Oasis::scriptSystem::LLUA )
consoleSystem::get( ).write( "Use \"demo:help()\" for instructions\n" );
reactDelay = 0;
// We want auto updates
root::get( ).doAutoLoop( );
return true;
}
void demo::setupLua( ) {
#ifdef HAS_LUA
dynamic_cast < scriptLuaInterpreter *> ( scriptSystem::get( ).
getInterpreter( ) )->loadLibrary( tolua_demo_open );
uint8 rettype;
scriptSystem::get( ).executeString( rettype, "demo = demo:get( )" );
scriptSystem::get( ).executeString( rettype, "camera = cameraSystem:get( )" );
scriptSystem::get( ).executeString( rettype, "phys = physicsSystem:get( )" );
scriptSystem::get( ).executeString( rettype, "objects = objectSystem:get( )" );
scriptSystem::get( ).executeString( rettype, "graph = graphicsSystem:get( )" );
#endif
}
void demo::setupRuby( ) {
uint8 rettype;
Init_Sample( );
scriptSystem::get( ).executeString( rettype, "$camera = Oasis::CameraSystem::get( )" );
scriptSystem::get( ).executeString( rettype, "$phys = Oasis::PhysicsSystem::get( )" );
scriptSystem::get( ).executeString( rettype, "$objects = Oasis::ObjectSystem::get( )" );
scriptSystem::get( ).executeString( rettype, "$graph = Oasis::GraphicsSystem::get( )" );
scriptSystem::get( ).executeString( rettype, "$demo = Sample::Demo::get( )" );
}
void demo::mouseMoved( const vector3 &moved ) {
cameraSystem::get( ).yaw( -moved.x * 40 );
cameraSystem::get( ).pitch( -moved.y * 40 );
}
void demo::mousePressed( uint8 button, uint8 count ) {
uint8 rettype;
if ( button == 0x10 ) {
if ( reactDelay == 0 ) {
scriptSystem::get( ).executeString( rettype, "triggerScene( )" );
reactDelay = 0.5;
}
} else if ( button == 0x20 ) {
scriptSystem::get( ).executeString( rettype, "resetScene( )" );
}
}
void demo::addReactor( object *subject ) {
subject->addCollisionListener( reactor );
}
void demo::help( void ) {
string helpString = "INSTRUCTIONS\n============\n";
helpString += "KEYS:\n";
helpString += "UP Move subject forward\n";
helpString += "DOWN Move subject backward\n";
helpString += "LEFT Move subject left\n";
helpString += "RIGHT Move subject right\n";
helpString += "W Move camera forward\n";
helpString += "S Move camera backward\n";
helpString += "LMB Trigger scene event\n";
helpString += "RMB Reset scene\n";
helpString += "F8 Iterates to the next language.\n";
helpString += "~ Toggle this console\n";
helpString += "\nCOMMANDS:\n";
if( scriptSystem::get().getLanguage( ) == Oasis::scriptSystem::LRUBY ) {
helpString += "$demo.help() Get this help\n";
helpString += "$demo.list() Get the list of available demos\n";
helpString += "$demo.load( demoName ) Load a demo\n";
helpString += "\nNOTES: \n";
helpString += "$demo = Sample::Demo::get( )";
}
else if( scriptSystem::get().getLanguage( ) == Oasis::scriptSystem::LLUA ) {
helpString += "demo:help() Get this help\n";
helpString += "demo:list() Get the list of available demos\n";
helpString += "demo:load( demoName ) Load a demo\n";
}
consoleSystem::get( ).write( helpString );
}
void demo::swapLanguage( void ) {
consoleSystem::get( ).write( "\nWelcome to the demo system." );
if( scriptSystem::get().getLanguage() == scriptSystem::LRUBY ) {
#ifdef HAS_LUA
scriptSystem::get().initLanguage( scriptSystem::LLUA );
consoleSystem::get( ).write( "Language Swapped to LUA" );
consoleSystem::get( ).write( "Use \"demo:help()\" for instructions\n" );
#endif
}
else if( scriptSystem::get().getLanguage() == scriptSystem::LLUA ) {
#ifdef HAS_RUBY
scriptSystem::get().initLanguage( scriptSystem::LRUBY );
consoleSystem::get( ).write( "Language Swapped to RUBY" );
consoleSystem::get( ).write( "Use \"$demo.help()\" for instructions\n" );
#endif
}
scriptSystem::get( ).executeScript( "null" );
}
void demo::list( void ) {
string helpString = "AVAILABLE DEMOS\n===============\n";
helpString += "boxstack The classic boxstack demo \n";
helpString += "simprag A simple ragdoll demo \n";
consoleSystem::get( ).write( helpString );
}
void demo::load( const string &demoName ) {
uint8 rettype;
// Run script
///TODO: This current system doesn't allow for zipped scripts, and doesn't
/// use SDDataChunks...
scriptSystem::get( ).executeScript( demoName );
scriptSystem::get( ).executeString( rettype, "resetScene( )" );
}
bool demo::frameStarted( float time ) {
uint8 rettype;
if ( inputSystem::get( ).isKeyDown( keyListener::KC_ESCAPE ) ) {
return false;
}
if ( inputSystem::get( ).isKeyDown( keyListener::KC_F12 ) ) {
graphicsSystem::get( ).takeShot( );
}
if ( inputSystem::get( ).isKeyDown( keyListener::KC_F8 ) ) {
swapLanguage( );
}
vector3 moveVector( 0, 0, 0 );
if ( inputSystem::get( ).isKeyDown( keyListener::KC_W ) ) {
moveVector.z = time * -50;
}
if ( inputSystem::get( ).isKeyDown( keyListener::KC_S ) ) {
moveVector.z = time * 50;
}
if ( inputSystem::get( ).isKeyDown( keyListener::KC_UP ) ) {
scriptSystem::get( ).executeString( rettype, "doForward( )" );
}
if ( inputSystem::get( ).isKeyDown( keyListener::KC_DOWN ) ) {
scriptSystem::get( ).executeString( rettype, "doBackward( )" );
}
if ( inputSystem::get( ).isKeyDown( keyListener::KC_LEFT ) ) {
scriptSystem::get( ).executeString( rettype, "doLeft( )" );
}
if ( inputSystem::get( ).isKeyDown( keyListener::KC_RIGHT ) ) {
scriptSystem::get( ).executeString( rettype, "doRight( )" );
}
cameraSystem::get( ).moveRelative( moveVector );
if ( reactDelay > 0 ) {
reactDelay -= time;
if ( reactDelay < 0 ) {
reactDelay = 0;
}
}
scriptSystem::get( ).executeString( rettype, "updateScene( %f )", time );
return true;
}
void demo::_registerClass( void ) {
scriptSystem::get( ).scriptLog( "Demo api started..." );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -