Crystal Space
Welcome, Guest. Please login or register.
April 17, 2014, 11:08:03 pm

Login with username, password and session length
Search:     Advanced search
8990 Posts in 2037 Topics by 7594 Members
Latest Member: Wandagarcia
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  Create Sprite2D from Code
« previous next »
Pages: [1] Print
Author Topic: Create Sprite2D from Code  (Read 1459 times)
Quintus
Newbie
*
Posts: 1


View Profile Email
« on: June 01, 2009, 07:45:03 pm »

Hi , I want to create a simple 2D-sprite as a rectangle(4 vertices) with some static texture on it  from the source code.
Is it possible with CS? I use CS 1.2

with "from the code" i mean without having some factory-file or world file
What would be the few lines to create the mentioned 2D-sprite?

I got some code from net that should do this:

Code:

csRef<iSprite2DState> Billboard::createSprite2DState(iMeshObject* sprite, iMaterialWrapper* matwrap)
{
  csRef<iSprite2DState> state;
  state = SCF_QUERY_INTERFACE(sprite, iSprite2DState);
  assert(state.IsValid());

// create a 1x1 square polygon. this creates a diamond. the vertices need to be moved a bit.
  state->CreateRegularVertices (4, false);

// move the vertices.
  csColoredVertices& v = state->GetVertices();
// csColoredVertices is defined in sprite2d.h as an array of csSprite2DVertex's. csSprite2dVertex is
// defined in sprite2d.h as a struct containing a csVector2 named pos, and two floats named u and v.
// the polygon vertices are moved to create a square (-.5,-.5), (-.5,.5), (.5,.5), (.5,-.5) with the texxture mapped apropriately. (and right-side up).
  v[0].pos.x = -0.5; v[0].u = 0;
  v[0].pos.y = -0.5; v[0].v = 1;
  v[1].pos.x = -0.5; v[1].u = 0;
  v[1].pos.y = 0.5; v[1].v = 0;
  v[2].pos.x = 0.5; v[2].u = 1;
  v[2].pos.y = 0.5; v[2].v = 0;
  v[3].pos.x = 0.5; v[3].u = 1;
  v[3].pos.y = -0.5; v[3].v = 1;

  state->SetMaterialWrapper(matwrap);
  return state;
}

csRef<iMeshObject> Billboard::createSprite(iObjectRegistry* object_reg)
{
  assert(object_reg);
  csRef<iMeshObjectType> type;
  type = CS_QUERY_REGISTRY(object_reg, iMeshObjectType);
  assert(type.IsValid());
  csRef<iMeshObjectFactory> factory = type->NewFactory();
  assert(factory.IsValid());
  return factory->NewInstance();
}

csRef<iMeshWrapper> Billboard::createMeshWrapper(iMeshObject* sprite, iEngine* engine, const char* name, iSector* sector)
 {
  csRef<iMeshWrapper> meshwrapper = engine->CreateMeshWrapper(sprite, name, sector);
  assert(meshwrapper.IsValid());
  LOG("terangreal:Billboard", 4, "setting billboard render priority to " << engine->GetRenderPriority("alpha") << " (for alpha)");
  meshwrapper->SetRenderPriority (engine->GetRenderPriority ("alpha"));
  meshwrapper->SetZBufMode (CS_ZBUF_TEST);
  return meshwrapper;
}

void Billboard::resizeSprite(iSprite2DState* state, double x, double y)
{
  csColoredVertices& v = state->GetVertices();
// the polygon vertices are moved to create a square (-.5,-.5), (-.5,.5), (.5,.5), (.5,-.5)
// with the texxture mapped apropriately. (and right-side up).
  v[0].pos.x = -(x/2); v[0].u = 0;
  v[0].pos.y = -(y/2); v[0].v = 1;
  v[1].pos.x = -(x/2); v[1].u = 0;
  v[1].pos.y = (y/2); v[1].v = 0;
  v[2].pos.x = (x/2); v[2].u = 1;
  v[2].pos.y = (y/2); v[2].v = 0;
  v[3].pos.x = (x/2); v[3].u = 1;
  v[3].pos.y = -(y/2); v[3].v = 1;
}

void Billboard::loadObject()
{
  if(alreadyLoaded) return;

  try
  {
  spriteMesh = createSprite(object_reg);
  assert(spriteMesh.IsValid());
  cs_sprite2d_state = createSprite2DState(spriteMesh, material);
  assert(cs_sprite2d_state.IsValid());
  meshwrapper = createMeshWrapper(spriteMesh, engine, getURL().getString().c_str(), sector);
  assert(meshwrapper.IsValid());
  cs_sprite2d_state->SetMixMode(CS_FX_ALPHA);
  transformgroup->GetChildren()->Add(meshwrapper);
  Object3D::loadObject();
  }
  catch(Vobject::NoSuchObjectError& e)
  {
  LOG("S3VR::Billboard", 1, "Warning: recieved NoSuchObjectError exception in loadObject(). (" << e.what() << ")");
  }
  catch(bad_cast)
  {
  LOG("S3VR::Billboard", 1, "Warning: recieved bad_cast exception in loadObject().");
  }
  catch(exception)
  {
  LOG("S3VR::Billboard", 1, "Warning: received unknown exception in loadObject().");
  }

}




Maybe this woks for older versions of CS
But it doesnt work for mine.

- In the CS1.2 the methode  state->GetVertices()  defined in sprite2d.h has changed in its return value ( in former versions it returned a reference to array for vertices , now its a pointer 
-csRef<iSprite2DState> state->SetMaterialWrapper(matwrap)  there is no SetMaterialWrapper()- methode anymore for csRef<iSprite2DState> object

Sorry for these newbe questions but can someone help me to create a simple 2D-sprite out of the code with 4 vertices and some tuxture?

Thanx a lot
 
Logged
Vincent
Full Member
***
Posts: 191


View Profile WWW
« Reply #1 on: June 04, 2009, 05:09:57 pm »

Hello,

  I can't provide a really detailed answer, since I never used spr2d, but give some pointers.
Everything which is done from XML can be done from code, or how else would CS itself do ? wink
The trick is to check the loader code for this object type, in this case I guess
csSprite2DLoader::Parse() in plugins/mesh/spr2d/persist/standard/spr2dldr.cpp
A quick look there reveals that the material wrapper is to be set using iMeshObject interface
(with 1.4; iirc there was some refactoring a while ago to make spr2d behave more like other meshes).
Good luck, and keep us posted about your progress...
Logged
kickvb
Global Moderator
Jr. Member
*****
Posts: 79


View Profile
« Reply #2 on: July 31, 2009, 04:05:11 pm »

Here is some sample code which should do the trick:

Code:
  // create the factory:
  csRef<iMeshFactoryWrapper> fact = engine->CreateMeshFactory("crystalspace.mesh.object.sprite.2d", "lineFact");
  csRef<iGeneralFactoryState> factState = scfQueryInterface<iGeneralFactoryState> (fact->GetMeshObjectFactory ());
  fact->GetMeshObjectFactory()->SetMaterialWrapper(myMaterial);

  // create the mesh
  csRef<iMeshWrapper> mesh = engine->CreateMeshWrapper (fact, "line");
  csRef<iSprite2DState> state = scfQueryInterface<iSprite2DState> (mesh->GetMeshObject());

  // create vertices.
  iColoredVertices* v = state->GetVertices();
  v->SetSize(4);
  v->Get(0).pos.x = -1;
  v->Get(0).pos.y = 1;
  v->Get(0).u = 0;
  v->Get(0).v = 0;
  v->Get(1).pos.x = 1;
  v->Get(1).pos.y = 1;
  v->Get(1).u = 1;
  v->Get(1).v = 0;
  v->Get(2).pos.x = 1;
  v->Get(2).pos.y = -1;
  v->Get(2).u = 1;
  v->Get(2).v = 1;
  v->Get(3).pos.x = -1;
  v->Get(3).pos.y = -1;
  v->Get(3).u = 0;
  v->Get(3).v = 1;

  v->Get(0).color_init.Set(1, 1, 1);
  v->Get(1).color_init.Set(1, 1, 1);
  v->Get(2).color_init.Set(1, 1, 1);
  v->Get(3).color_init.Set(1, 1, 1);

  // position the mesh
  iMovable* movable = mesh->GetMovable();
  movable->SetSector(room);
  movable->SetPosition(csVector3 (1, 1, 1));
  movable->UpdateMove();
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 6.261 seconds with 16 queries.