// osgcatch1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* 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
* OpenSceneGraph Public License for more details.
*/
////本程序总体上四个类,一个主函数
#define WIN32_LEAN_AND_MEAN
#define INITGUID // we use GUID's with DMusic
////// Includes
#include <windows.h> // standard Windows app include
#include <dmusicc.h> // DirectMusic includes
#include <dmusici.h>
#include <d3d8types.h> // for D3DVECTOR
#include <cguid.h> // for GUID_NULL
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gl/gl.h> // standard OpenGL include
#include <gl/glu.h>
////// DirectMusic variables
IDirectMusicLoader8 *dmusicLoader = NULL; // the loader
IDirectMusicPerformance8 *dmusicPerformance = NULL; // the performance
IDirectMusicSegment8 *dmusicSegment = NULL; // the segment
/*******************************************************
* DirectMusic Interfaces
*******************************************************/
// InitDirectXAudio()
// desc: initializes the DirectX Audio component for playback
bool InitDirectXAudio(HWND hwnd)
{
char pathStr[MAX_PATH]; // path for audio file
WCHAR wcharStr[MAX_PATH];
// create the loader object
if (FAILED(CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
IID_IDirectMusicLoader8, (void**)&dmusicLoader)))
{
MessageBox(hwnd, "Unable to create the IDirectMusicLoader8 object!\nPress OK to exit",
"ERROR!", MB_OK);
return false;
}
// create the performance object
if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
IID_IDirectMusicPerformance8, (void**)&dmusicPerformance)))
{
MessageBox(hwnd, "Unable to create the IDirectMusicPerformance8 object!\nPress OK to exit",
"ERROR!", MB_OK);
return false;
}
// initialize the performance with the standard audio path
dmusicPerformance->InitAudio(NULL, NULL, hwnd, DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,
DMUS_AUDIOF_ALL, NULL);
// retrieve the current directory
GetCurrentDirectory(MAX_PATH, pathStr);
// convert to unicode string
MultiByteToWideChar(CP_ACP, 0, pathStr, -1, wcharStr, MAX_PATH);
// set the search directory
dmusicLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wcharStr, FALSE);
return true;
}
// LoadSegment()
// desc: load a segment from a file
bool LoadSegment(HWND hwnd, char *filename)
{
WCHAR wcharStr[MAX_PATH];
// convert filename to unicode string
MultiByteToWideChar(CP_ACP, 0, filename, -1, wcharStr, MAX_PATH);
// load the segment from file
if (FAILED(dmusicLoader->LoadObjectFromFile(CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
wcharStr,
(void**)&dmusicSegment)))
{
MessageBox(hwnd, "Audio file not found! Press OK to exit",
"ERROR!", MB_OK);
return false;
}
// download the segment's instruments to the synthesizer
dmusicSegment->Download(dmusicPerformance);
return true;
}
// PlaySegment()
// desc: start playing a segment
void PlaySegment(IDirectMusicPerformance8* dmPerf, IDirectMusicSegment8* dmSeg)
{
// play the segment
dmPerf->PlaySegmentEx(dmSeg, NULL, NULL, 0, 0, NULL, NULL, NULL);
}
// StopSegment()
// desc: stop a segment from playing
void StopSegment(IDirectMusicPerformance8* dmPerf, IDirectMusicSegment8* dmSeg)
{
// stop the dmSeg from playing
dmPerf->StopEx(dmSeg, 0, 0);
}
// CloseDown()
// desc: shutdown music performance
void CloseDown(IDirectMusicPerformance8* dmPerf)
{
// stop the music
dmPerf->Stop(NULL, NULL, 0, 0);
// close down DirectMusic
dmPerf->CloseDown();
}
#include <osgProducer/Viewer>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osgUtil/GLObjectsVisitor>
#include <osgText/Text>
#include <osg/Geode>
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Switch>
#include <osg/TexMat>
#include <osg/Texture2D>
#include <osg/io_utils>
#include <osgParticle/ExplosionEffect>
#include <osgParticle/ExplosionDebrisEffect>
#include <osgParticle/SmokeEffect>
#include <osgParticle/FireEffect>
#include <sstream>
typedef std::vector<std::string> FileList;
typedef std::map<std::string, osg::ref_ptr<osg::Node> > ObjectMap;
static ObjectMap s_objectMap;
class Character : public osg::Referenced ////Character公共继承了osg::Referenced类
////主要是场景中成绩和人物的设置
{
public:
Character();
void setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& orgin, const osg::Vec3& width, const osg::Vec3& catchPos, float positionRatio);
void setLives(const std::string& filename, const osg::Vec3& orgin, const osg::Vec3& delta, unsigned int numLives);
void setCatches(const std::string& filename, const osg::Vec3& orgin, const osg::Vec3& delta, unsigned int numLives);
void moveLeft();
void moveRight();
void moveTo(float positionRatio);
void reset();
void resetCatches();
bool addCatch();
bool looseLife();
osg::Vec3 getCurrentCenterOfBasket() const { return _character->getPosition()+_centerBasket; }
float getCurrentRadiusOfBasket() const { return _radiusBasket; } ////_radiusBasket = width.length()*catchPos.z();
osg::Vec3 getLowerLeft() const { return _character->getPosition(); }
osg::Vec3 getUpperRight() const { return _character->getPosition(); }
osg::Vec3 _origin;
osg::Vec3 _width;
float _positionRatio;
osg::ref_ptr<osg::PositionAttitudeTransform> _character; ////_character是一个位置指针
unsigned int _numLives; ////剩余生命次数
osg::ref_ptr<osg::Switch> _livesSwitch;
unsigned int _numCatches; ////捕捉物体数量
osg::ref_ptr<osg::Switch> _catchSwitch;
osg::ref_ptr<osg::Group> _objectsGroup;
osg::Vec3 _centerBasket;
float _radiusBasket;
};
Character::Character():
_positionRatio(0.5f),
_numLives(3),
_numCatches(0)
{
} ////构造函数
void Character::setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& origin, const osg::Vec3& width, const osg::Vec3& catchPos, float positionRatio)
{
_origin = origin; ////前面带_的是类里面已经定义的变量,后面的是函数引进的变量
_width = width;
_positionRatio = positionRatio;
_numLives = 3;
_numCatches = 0;
float _characterSize = _width.length()*0.2f;
osg::Image* image = osgDB::readImageFile(filename);
if (image)
{
osg::Vec3 pos(-0.5f*_characterSize,0.0f,0.0f); ////贴图的位置及尺寸都以_characterSize为参考基准
osg::Vec3 width(_characterSize*((float)image->s())/(float)(image->t()),0.0f,0.0);
osg::Vec3 height(0.0f,0.0f,_characterSize);
osg::Geometry* geometry = osg::createTexturedQuadGeometry(pos,width,height); ////同时引用了上面三个位置
osg::StateSet* stateset = geometry->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0,new osg::Texture2D(image),osg::StateAttribute::ON);
stateset->setMo