Crystal Space
Welcome, Guest. Please login or register.
November 24, 2014, 09:35:01 am

Login with username, password and session length
Search:     Advanced search
9018 Posts in 2046 Topics by 9101 Members
Latest Member: Adamgit
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  Playing music in Crystal Space
« previous next »
Pages: [1] Print
Author Topic: Playing music in Crystal Space  (Read 2203 times)
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« 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);
}
Logged
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« Reply #1 on: January 14, 2006, 07:53:20 am »

No answer? It problem important me...
Logged
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« Reply #2 on: January 14, 2006, 07:58:19 pm »

No answer? It problem important me...

Help, help more!
Logged
dingobloo
Idle Extrordinare.
Global Moderator
Full Member
*****
Posts: 125


View Profile WWW
« Reply #3 on: January 15, 2006, 02:31:06 am »

I am confused about which sound system you are using, it doen't appear to be the new sound system available in CS. For an example of the useage of the sound system just mentioned you may look in /CS/apps/tests/sndtest/ or alternativley
here:
http://cvs.sourceforge.net/viewcvs.py/crystal/CS/apps/tests/sndtest/
« Last Edit: January 15, 2006, 02:35:09 am by dingobloo » Logged
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« Reply #4 on: January 16, 2006, 04:47:13 pm »

I am confused about which sound system you are using, it doen't appear to be the new sound system available in CS. For an example of the useage of the sound system just mentioned you may look in /CS/apps/tests/sndtest/ or alternativley
here:
http://cvs.sourceforge.net/viewcvs.py/crystal/CS/apps/tests/sndtest/

Yes, I know this software render Open AL system in CS, but is slowly. My prefered sound system CPU usage is 1-3%...
Some alternatives are uncompressed wav and etc files, but I want to .ogg vorbis playing my game.

Thanks!
« Last Edit: January 16, 2006, 04:54:34 pm by [SOM]Roberto » Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 5.141 seconds with 15 queries.