|
[SOM]Roberto
|
 |
« on: January 13, 2006, 03:44:03 pm » |
|
I got a problem. I try play music. When I run my program I hearing the music about half second. My program running perfectly in console.
//header file #ifndef __SIMPLE1_H__ #define __SIMPLE1_H__
#include <crystalspace.h>
///////////////////////////////////////////////////// #include <iostream> #include "audiere.h" #include <conio.h> /////////////////////////////////////////////////////
class Simple : public csApplicationFramework, public csBaseEventHandler { private: csRef<iEngine> engine; csRef<iLoader> loader; csRef<iGraphics3D> g3d; csRef<iKeyboardDriver> kbd; csRef<iVirtualClock> vc; csRef<iView> view;
iSector* room;
float rotX, rotY;
public: bool SetupModules (); bool OnKeyboard (iEvent&);
void ProcessFrame (); void CreateRoom (); void FinishFrame ();
///////////////////////////////////////////////////// void PlayMusic(); /////////////////////////////////////////////////////
Simple (); ~Simple ();
void OnExit ();
bool OnInitialize (int argc, char* argv[]); bool Application ();
CS_EVENTHANDLER_NAMES("application.simple1") CS_EVENTHANDLER_NIL_CONSTRAINTS };
#endif // __SIMPLE1_H__
//--------------------------------------------------------------------------------- //source file
#include "main.h"
CS_IMPLEMENT_APPLICATION
Simple::Simple () { SetApplicationName ("CrystalSpace.Simple1"); }
Simple::~Simple () { }
void Simple::ProcessFrame () { csTicks elapsed_time = vc->GetElapsedTicks (); float speed = (elapsed_time / 1000.0) * (0.06 * 20);
iCamera* c = view->GetCamera();
if (kbd->GetKeyState (CSKEY_SHIFT)) { if (kbd->GetKeyState (CSKEY_RIGHT)) c->Move (CS_VEC_RIGHT * 4 * speed); if (kbd->GetKeyState (CSKEY_LEFT)) c->Move (CS_VEC_LEFT * 4 * speed); if (kbd->GetKeyState (CSKEY_UP)) c->Move (CS_VEC_UP * 4 * speed); if (kbd->GetKeyState (CSKEY_DOWN)) c->Move (CS_VEC_DOWN * 4 * speed); } else { if (kbd->GetKeyState (CSKEY_RIGHT)) rotY += speed; if (kbd->GetKeyState (CSKEY_LEFT)) rotY -= speed; if (kbd->GetKeyState (CSKEY_PGUP)) rotX += speed; if (kbd->GetKeyState (CSKEY_PGDN)) rotX -= speed; if (kbd->GetKeyState (CSKEY_UP)) c->Move (CS_VEC_FORWARD * 4 * speed); if (kbd->GetKeyState (CSKEY_DOWN)) c->Move (CS_VEC_BACKWARD * 4 * speed); }
csMatrix3 rot = csXRotMatrix3 (rotX) * csYRotMatrix3 (rotY); csOrthoTransform ot (rot, c->GetTransform().GetOrigin ()); c->SetTransform (ot);
if (!g3d->BeginDraw (engine->GetBeginDrawFlags () | CSDRAW_3DGRAPHICS)) return;
view->Draw (); }
void Simple::FinishFrame () { g3d->FinishDraw (); g3d->Print (0); }
bool Simple::OnKeyboard(iEvent& ev) { csKeyEventType eventtype = csKeyEventHelper::GetEventType(&ev); if (eventtype == csKeyEventTypeDown) { utf32_char code = csKeyEventHelper::GetCookedCode(&ev); if (code == CSKEY_ESC) { csRef<iEventQueue> q = csQueryRegistry<iEventQueue> (GetObjectRegistry()); if (q.IsValid()) q->GetEventOutlet()->Broadcast( csevQuit(GetObjectRegistry())); } } return false; }
bool Simple::OnInitialize(int /*argc*/, char* /*argv*/ []) { if (!csInitializer::RequestPlugins(GetObjectRegistry(), CS_REQUEST_VFS, CS_REQUEST_OPENGL3D, CS_REQUEST_ENGINE, CS_REQUEST_FONTSERVER, CS_REQUEST_IMAGELOADER, CS_REQUEST_LEVELLOADER, CS_REQUEST_REPORTER, CS_REQUEST_REPORTERLISTENER, CS_REQUEST_END)) return ReportError("Failed to initialize plugins!");
csBaseEventHandler::Initialize(GetObjectRegistry());
if (!RegisterQueue(GetObjectRegistry(), csevAllEvents(GetObjectRegistry()))) return ReportError("Failed to set up event handler!");
///////////////////////////////////////////////////// PlayMusic(); /////////////////////////////////////////////////////
return true; }
void Simple::OnExit() { }
bool Simple::Application() { if (!OpenApplication(GetObjectRegistry())) return ReportError("Error opening system!");
if (SetupModules()) { Run(); }
return true; }
bool Simple::SetupModules () { g3d = csQueryRegistry<iGraphics3D> (GetObjectRegistry()); if (!g3d) return ReportError("Failed to locate 3D renderer!");
engine = csQueryRegistry<iEngine> (GetObjectRegistry()); if (!engine) return ReportError("Failed to locate 3D engine!");
vc = csQueryRegistry<iVirtualClock> (GetObjectRegistry()); if (!vc) return ReportError("Failed to locate Virtual Clock!");
kbd = csQueryRegistry<iKeyboardDriver> (GetObjectRegistry()); if (!kbd) return ReportError("Failed to locate Keyboard Driver!");
loader = csQueryRegistry<iLoader> (GetObjectRegistry()); if (!loader) return ReportError("Failed to locate Loader!");
view.AttachNew(new csView (engine, g3d)); iGraphics2D* g2d = g3d->GetDriver2D ();
view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());
engine->SetLightingCacheMode (0);
CreateRoom();
engine->Prepare ();
rotY = rotX = 0;
view->GetCamera ()->SetSector (room); view->GetCamera ()->GetTransform ().SetOrigin (csVector3 (0, 5, -3));
return true; }
void Simple::CreateRoom () { if (!loader->LoadTexture ("stone", "/lib/std/stone4.gif")) ReportError("Error loading 'stone4' texture!");
iMaterialWrapper* tm = engine->GetMaterialList ()->FindByName ("stone");
room = engine->CreateSector ("room");
csRef<iMeshWrapper> walls (engine->CreateSectorWallsMesh (room, "walls")); csRef<iMeshObject> walls_object = walls->GetMeshObject (); csRef<iMeshObjectFactory> walls_factory = walls_object->GetFactory(); csRef<iThingFactoryState> walls_state = scfQueryInterface<iThingFactoryState> (walls_factory); walls_state->AddInsideBox (csVector3 (-5, 0, -5), csVector3 (5, 20, 5)); walls_state->SetPolygonMaterial (CS_POLYRANGE_LAST, tm); walls_state->SetPolygonTextureMapping (CS_POLYRANGE_LAST, 3);
csRef<iLight> light; iLightList* ll = room->GetLights ();
light = engine->CreateLight(0, csVector3(-3, 5, 0), 10, csColor(1, 0, 0)); ll->Add (light);
light = engine->CreateLight(0, csVector3(3, 5, 0), 10, csColor(0, 0, 1)); ll->Add (light);
light = engine->CreateLight(0, csVector3(0, 5, -3), 10, csColor(0, 1, 0)); ll->Add (light); }
///////////////////////////////////////////////////// void Simple::PlayMusic() { using namespace audiere; AudioDevicePtr device(OpenDevice()); float volume = 1.0f;
OutputStreamPtr music(OpenSound(device, "data/music/music.ogg", false));
if (music == true) { music->setVolume(volume); music->play(); music->setRepeat(true); } } /////////////////////////////////////////////////////
int main (int argc, char* argv[]) { return csApplicationRunner<Simple>::Run (argc, argv); }
|