Тема: Exotic Engine
Показать сообщение отдельно
Старый 30.07.2010, 09:51   #225
Igor'
ПроЭктировщик
 
Аватар для Igor'
 
Регистрация: 15.10.2009
Сообщений: 190
Написано 47 полезных сообщений
(для 142 пользователей)
Ответ: Exotic Engine

Вот сеть наконецто с физикой доделал, сеть через UDP но с гарантированой доставкой пакетов, работает просто:

#include "..\sdk\inc\engine.h"
#include "..\sdk\inc\font\font.h"
#include "..\sdk\inc\console\console.h"
#include "..\sdk\inc\movie\clip.h"
#include "..\sdk\inc\texture\texture.h"
#include "..\sdk\inc\sound\sound.h"
#include "..\sdk\inc\object\object.h"
#include "..\sdk\inc\camera\camera.h"
#include "..\sdk\inc\model\model.h"
#include "..\sdk\inc\player\player.h"
#include "..\sdk\inc\light\light.h"
#include "..\sdk\inc\light\directionallight.h"
#include "..\sdk\inc\light\spotlight.h"
#include "..\sdk\inc\light\omnilight.h"
#include "..\sdk\inc\light\pointlight.h"
#include "..\sdk\inc\terrain\terrain.h"
#include "..\sdk\inc\water\water.h"
#include "..\sdk\inc\cl\compute.h"
#include "..\sdk\inc\material\material.h"
#include "..\sdk\inc\spatial\spatial.h"
#include "..\sdk\inc\game\game.h"
#include "..\sdk\inc\physic\physic.h"
#include "..\sdk\inc\collision\collision.h"
#include "..\sdk\inc\rigidbody\rigidbody.h"
#include "..\sdk\inc\log.h"
#include "..\sdk\inc\network\network.h"

using namespace ex;



void testCmd( const String& ){
}



static Cmd_t* test = NULL;
static PlayerSpectator* player = NULL;
static Engine* engine = NULL;
static Network* network = NULL;
static Physic* physic = NULL;
static SpotLight* sun = NULL;
static ObjectModel* sky = 0;
static Font* font = 0;


void ServerChat( int msg ) {
	if( msg == NMSG_PACKET ) {
		SocketPacket packet;
		if( ServerSocket::read(packet) ) 
			engine->print("message form client %i: %s\n",packet.peerId,(char*)packet.data);
		else
			return;

		for( int i = 0; i < ServerSocket::getPeerCount(); i++ ) {
			if( i == packet.peerId ) continue;
			SocketPacket opacket;
			opacket.length = packet.length;
			opacket.data = packet.data;
			ServerSocket::write(ServerSocket::getPeer(i),opacket);
		}
	}
}

void ClientChat( int msg ) {
	if( msg == NMSG_PACKET ) {
		SocketPacket packet;
		if( !ClientSocket::read(packet) ) return;
		engine->print("message: %s\n",(char*)packet.data);
	}
}

void Say( const String& text ) {
	SocketPacket packet;
	packet.length = text.size() + 1;
	packet.data = (byte*)text.c_str();
	ClientSocket::write(packet);
}

bool init(){
	engine = Engine::get();
	physic = Physic::get();
	network = Network::get();


	font = new Font("arial.ttf",16);
	
	sun = new SpotLight();
	sun->setIntensity(1.0f);
	sun->setColor(float3(1.0f,1.0f,1.0f));
	sun->setShadows(true);
	sun->setRadius(200.0f);
	engine->worldAddLight(sun);



	test = Console::get()->createCmd("test",testCmd,Cmd_t::CHEAT);
	Console::get()->createCmd("say",Say);


	engine->worldAmbient(vec3_t(0.6f,0.6f,0.6f));
	engine->worldFogRange(vec2_t(0.0f,120.0f));



	ObjectModel* box = ObjectModel::createBox();
	box->setOrigin( vec3_t(0,20,0) );
	engine->worldAddObject(box);
    RigidBody* body = new RigidBody(box,CollisionShape::createModelBox(box),20.0f);
	physic->addObject(body);

	float x_offset = 0,z_offset = -5.0f;
	for( int z = 0; z < 10; z++ ) {
		x_offset = -5.0f;
		for( int x = 0; x < 10; x++ ) {
			ObjectModel* box = ObjectModel::createBox();
			box->setOrigin(vec3_t(x_offset,-2,z_offset));
	        engine->worldAddObject(box);
            Collider* collider = new Collider(box,CollisionShape::createModelBox(box),0);
	        physic->addObject(collider);
			x_offset += 1.2f;
		}
		z_offset += 1.2f;
	}

	player = new PlayerSpectator();
	engine->worldAddPlayer(player);
	engine->worldSetPlayer(player);
	player->getView()->setFar(2048.0f);

	Collider* collider = new Collider(player->getView(),CollisionShape::createSphere(1.0f),1);
	collider->setObject( player->getView() );
	physic->addObject(collider);

	network->setServerCallback(ServerChat);
	network->setClientCallback(ClientChat);

	return true;
}

bool frame( float delta ){
	if( engine->getKey( KC_ESCAPE ) ) return false;	


	if( engine->getKey( KC_F3 ) ){
		engine->worldFog(true);
	}

	if( engine->getKey( KC_F4 ) ){
		engine->worldFog(false);
	}

	if( engine->getKey( KC_SPACE ) ){
		sun->setOrient( player->getView()->getOrient() );
		sun->setOrigin( player->getView()->getOrigin() );
	}

	return true;
}

bool guiFrame( float delta ) {
	font->enable(engine->getWindowWidth(),engine->getWindowHeight());
	font->printf(2,2,0,"Visible sectors: %i\n",engine->worldGetVisibleSectors());
	font->disable();
	return true;
}

#ifdef EX_WIN32
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {
	int argc = 1;
	char *argv[256];
	argv[0] = "none";
	while(*lpCmdLine && argc < 256) {
		while(*lpCmdLine && (*lpCmdLine <= ' ' || *lpCmdLine == '"')) lpCmdLine++;
		if(*lpCmdLine) {
			if(*lpCmdLine == '"') lpCmdLine++;
			argv[argc++] = lpCmdLine;
			while(*lpCmdLine && *lpCmdLine > ' ' && *lpCmdLine != '"') lpCmdLine++;
			if(*lpCmdLine) *(lpCmdLine++) = '\0';
		}
	}
#else
int main( int argc , char** argv ) {
#endif

	Engine* engine = Engine::create();
	engine->main(init,NULL,frame,guiFrame);
	Engine::destroy();

	return 0;
}
Cоздание сервера и приконекчивание к нему осуществляется в консоли c помощью комманд:

server_create <port>
server_destroy
client_connect <address:port>
client_disconnect
(Offline)