Crystal Space
Welcome, Guest. Please login or register.
September 23, 2014, 01:49:57 pm

Login with username, password and session length
Search:     Advanced search
9018 Posts in 2051 Topics by 8504 Members
Latest Member: Colleencoffey
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  render pixmap issue
« previous next »
Pages: [1] Print
Author Topic: render pixmap issue  (Read 4051 times)
Mcarsten
Newbie
*
Posts: 19


View Profile Email
« on: December 22, 2008, 04:00:35 pm »

Hi,
I have tried to display a pixmap based on the example in the user manual, but have had no luck.
Here is the code I have:
   iTextureManager* txtmgr = g3d->GetTextureManager ();
   vfs->ChDir ("/myapp/0.5/");
   csRef<iDataBuffer> buf = vfs->ReadFile ("mypic.jpg");
   csRef<iImage> ifile = imgldr->Load (buf, txtmgr->GetTextureFormat ());
   csRef<iTextureHandle> txt =
   txtmgr->RegisterTexture (ifile, CS_TEXTURE_2D);
   csSimplePixmap *pixmap = new csSimplePixmap( txt );
   pixmap->Draw( g3d, x, y );

I can step through (while debugging) and I cannot seem to find why the pixmap will not display on the map.

Any help is appreciated.  Thanks.
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #1 on: December 22, 2008, 04:02:19 pm »

When are you drawing this? CS is event-driven. You can't just draw something like that at any place that you want. You have to do it in the function that processes frame events and 3D drawing must be set up properly (i.e. BeginDraw()). Also don't combine loading of the pixmap with drawing the pixmap in the same routine. You only need to load it once but you have to draw it every frame.

Greetings,
Logged
Mcarsten
Newbie
*
Posts: 19


View Profile Email
« Reply #2 on: December 22, 2008, 04:11:45 pm »

right now i am drawing this based on a mouse event, when a click event is captured, I grab the x and y and try to draw the pixmap where the click occurred.
So are you saying that i need to call BeginDraw() after i call pixmap->Draw(..)?
Thanks.
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #3 on: December 22, 2008, 04:14:13 pm »

No. I'm saying that you shouldn't draw it while handling mouse events. You must draw it in the event that occurs every frame. i.e. the frame events (where you also call view->Draw()). If you draw it here while handling the mouse events it will only be drawn once when you get the mouse event but next frame it will go away again (CS clears screen every frame normally).

So in the mouse event handler you just set some flag or data indicating that you want to draw it while handling the fame events.

And you must additionally also draw the pixmap AFTER caling BeginDraw(). Not before.

Greetings,
Logged
Mcarsten
Newbie
*
Posts: 19


View Profile Email
« Reply #4 on: December 22, 2008, 04:18:54 pm »

ok, thank you, that makes a lot of sense.
thanks again.
Logged
Mcarsten
Newbie
*
Posts: 19


View Profile Email
« Reply #5 on: December 22, 2008, 07:26:08 pm »

thanks again for you help, I was able to be successful in rendering the pixmaps, although now i am having some other troubles.

I am using some CEGUIs and a mouse pointer.  The problem is that when i move the mouse around on the display (and the guis) the mouse and guis redisplay where i drag them although i get a trail of mouse pointers and guis where they have been previously.
How do i either clear off the old displays or place them behind the main dispaly?
thanks again.
Logged
Mcarsten
Newbie
*
Posts: 19


View Profile Email
« Reply #6 on: December 22, 2008, 07:41:51 pm »

i guess a simpler question would be....
how do i draw a pixmap in a specified sector?
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #7 on: December 22, 2008, 08:12:42 pm »

You don't. Pixmaps are 2D images that are drawn on top of the entire view. They have no relation to sectors.

Greetings,
Logged
Mcarsten
Newbie
*
Posts: 19


View Profile Email
« Reply #8 on: December 22, 2008, 08:33:05 pm »

ok, thank you.
the issue that i am seeing is that it seems that my pixmaps are being drawn behind my current sector.  Is there a way to specify that they be drawn on top of my display?
The reason I believe this is happening is that when i remove my sector and just draw the pixmaps they are displayed, although when i then add the room they are not displayed.
Thanks.
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #9 on: December 22, 2008, 08:34:14 pm »

You are probably calling Draw on the pixmap before you call view->Draw(). Check that the order is correctly.

Greetings,
Logged
Mcarsten
Newbie
*
Posts: 19


View Profile Email
« Reply #10 on: December 22, 2008, 08:37:06 pm »

yes, i am drawing the pixmap after i call view->Draw() and i am not seeing the pixmap on top of the view.
thanks.
« Last Edit: December 22, 2008, 08:45:04 pm by Mcarsten » Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #11 on: December 22, 2008, 08:53:26 pm »

Perhaps show me the full code.

Greetings,
Logged
Mcarsten
Newbie
*
Posts: 19


View Profile Email
« Reply #12 on: December 22, 2008, 08:58:40 pm »

i am basically (just for now) mucking with the CEGUITest code and have modified the cegui(s) to be customized for what i want.  and now i am trying to display a pixmap where the user clicks.  Sorry for pasting so much code but here is the contents of the cpp file.
THanks again!
/*
  Copyright (C) 2005 Dan Hardfeldt and Seth Yastrov

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program 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
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "ceguitest.h"

#include "csutil/custom_new_disable.h"
#include "csutil/win32/CEGUI.h"
#include "csutil/win32/CEGUIWindowManager.h"
#include "csutil/win32/CEGUILogger.h"
#include "csutil/custom_new_enable.h"
    

#include <cstool/cspixmap.h>
#include <igraphic/image.h>
#include <igraphic/imageio.h>
#include <ivideo/texture.h>
#include <ivideo/txtmgr.h>
#include <iutil/vfs.h>



CS_IMPLEMENT_APPLICATION

CEGUITest::CEGUITest()
{
  SetApplicationName ("CEGUITest");
}

CEGUITest::~CEGUITest()
{
}

void CEGUITest::ProcessFrame()
{
  // First get elapsed time from the virtual clock.
  csTicks elapsed_time = vc->GetElapsedTicks ();

  // Now rotate the camera according to keyboard state
  float speed = (elapsed_time / 1000.0) * (0.03 * 20);

/*
  iCamera* c = view->GetCamera();
  if (kbd->GetKeyState (CSKEY_SHIFT))
  {
    // If the user is holding down shift, the arrow keys will cause
    // the camera to strafe up, down, left or right from it's
    // current position.
    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
  {
    // left and right cause the camera to rotate on the global Y
    // axis; page up and page down cause the camera to rotate on the
    // _camera's_ X axis (more on this in a second) and up and down
    // arrows cause the camera to go forwards and backwards.
    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);
  }

  // We now assign a new rotation transformation to the camera.  You
  // can think of the rotation this way: starting from the zero
  // position, you first rotate "rotY" radians on your Y axis to get
  // the first rotation.  From there you rotate "rotX" radians on
  // your X axis to get the final rotation.  We multiply the
  // individual rotations on each axis together to get a single
  // rotation matrix.  The rotations are applied in right to left
  // order .

  csMatrix3 rot = csXRotMatrix3 (rotX) * csYRotMatrix3 (rotY);
  csOrthoTransform ot (rot, c->GetTransform().GetOrigin ());
  c->SetTransform (ot);
  */
  // Tell 3D driver we're going to display 3D things.
  if (!g3d->BeginDraw(
    engine->GetBeginDrawFlags() | CSDRAW_3DGRAPHICS))
    return;


   
  // Tell the camera to render into the frame buffer.
  view->Draw ();

      if ( drawPixmap ) {
      solarpixmap->Draw( g3d, clickedX, clickedY );
      //solarpixmap->DrawScaled( g3d, clickedX, clickedY, 4000, 4000 );
      //drawPixmap = false;
  }

  //solarpixmap->Draw( g3d, clickedX, clickedY );
 

  cegui->Render ();
}

void CEGUITest::FinishFrame()
{
  g3d->FinishDraw ();
  g3d->Print (0);
}

bool CEGUITest::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_PLUGIN ("crystalspace.cegui.wrapper", iCEGUI),
    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!");

  return true;
}

void CEGUITest::OnExit()
{
}

bool CEGUITest::Application()
{
  if (!OpenApplication(GetObjectRegistry()))
    return ReportError("Error opening system!");

  vfs = csQueryRegistry<iVFS> (GetObjectRegistry());
  if (!vfs) return ReportError("Failed to locate VFS!");

  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!");

  cegui = csQueryRegistry<iCEGUI> (GetObjectRegistry());
  if (!cegui) return ReportError("Failed to locate CEGUI plugin");

  imgldr = csQueryRegistry<iImageIO>(GetObjectRegistry());

  // Initialize CEGUI wrapper
  cegui->Initialize ();

  // Set the logging level
  cegui->GetLoggerPtr ()->setLoggingLevel(CEGUI::Informative);

//#if (CEGUI_VERSION_MAJOR == 0) && (CEGUI_VERSION_MINOR >= 5)
  // Use the 0.5 version of the skin
  vfs->ChDir ("/ceguitest/0.5/");
//#else
  // Other versions here, if supported
//#endif

  // Load the ice skin (which uses Falagard skinning system)
  cegui->GetSchemeManagerPtr ()->loadScheme("ice.scheme");

  cegui->GetSystemPtr ()->setDefaultMouseCursor("ice", "MouseArrow");

  CEGUI::Font* font = cegui->GetFontManagerPtr ()->createFont("FreeType",
    "Vera", "/fonts/ttf/Vera.ttf");
  font->setProperty("PointSize", "10");
  font->load();

  CEGUI::WindowManager* winMgr = cegui->GetWindowManagerPtr ();

  // Load layout and set as root
  cegui->GetSystemPtr ()->setGUISheet(winMgr->loadWindowLayout("ice.layout"));

  //setup pixmaps
     iTextureManager* txtmgr = g3d->GetTextureManager ();
   csRef<iDataBuffer> buf = vfs->ReadFile ("solarpanel.jpg");
   solarfile = imgldr->Load (buf, txtmgr->GetTextureFormat ());
   solartexture =   txtmgr->RegisterTexture (solarfile, CS_TEXTURE_2D);
   solarpixmap = new csSimplePixmap( solartexture );

   clickedX = clickedY = 0;
   drawPixmap = false;

  // Subscribe to the clicked event for the exit button
  CEGUI::Window* btn = winMgr->getWindow("SOLAR");
  btn->subscribeEvent(CEGUI::PushButton::EventClicked,
    CEGUI::Event::Subscriber(&CEGUITest::OnSolarButtonClicked, this));

  // First disable the lighting cache. Our app is simple enough
  // not to need this.
  engine->SetLightingCacheMode (0);

  // These are used store the current orientation of the camera.
  rotY = rotX = 0;

  view.AttachNew(new csView (engine, g3d));
  iGraphics2D* g2d = g3d->GetDriver2D ();
  view->SetRectangle(0, 0, g2d->GetWidth(), g2d->GetHeight ());

  CreateRoom();
  view->GetCamera()->SetSector (room);
  view->GetCamera()->GetTransform().SetOrigin(csVector3 (0, 5, 0));

  Run();

  return true;
}

bool CEGUITest::OnSolarButtonClicked (const CEGUI::EventArgs&)
{
  csRef<iEventQueue> q =
    csQueryRegistry<iEventQueue> (GetObjectRegistry());
  if (q.IsValid()) q->GetEventOutlet()->Broadcast(csevQuit(GetObjectRegistry()));
  return true;
}

bool CEGUITest::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;
}

void CEGUITest::drawOnClick( int x, int y )
{
   /*
   //move to initialize stuff
   iTextureManager* txtmgr = g3d->GetTextureManager ();
   vfs->ChDir ("/ceguitest/0.5/");
   csRef<iDataBuffer> buf = vfs->ReadFile ("solarpanel.jpg");
   csRef<iImage> ifile = imgldr->Load (buf, txtmgr->GetTextureFormat ());
   csRef<iTextureHandle> txt =
   txtmgr->RegisterTexture (ifile, CS_TEXTURE_2D);
   solarpixmap = new csSimplePixmap( txt );
   //move to frame func
   solarpixmap->Draw( g3d, x, y );
   */
}

bool CEGUITest::OnMouseDown (iEvent& ev)
{
   clickedX = (float)csMouseEventHelper::GetX(&ev);
   clickedY = (float)csMouseEventHelper::GetY(&ev);
   //drawOnClick( x, y );
   drawPixmap = true;
   
   //ReportError("mouseDown!!");
   return false;
}

bool CEGUITest::OnMouseUp (iEvent&)
{
   //ReportError("mouseUp!!");

   return false;
}

bool CEGUITest::OnMouseMove (iEvent&)
{
   //ReportError("mouseMove!!");
   return false;
}

void CEGUITest::CreateRoom ()
{
  // Load the texture from the standard library.  This is located in
  // CS/data/standard.zip and mounted as /lib/std using the Virtual
  // File System (VFS) plugin.
  if (!loader->LoadTexture ("stone", "/lib/std/misty.jpg"))
    ReportError("Error loading 'misty' texture!");

  iMaterialWrapper* tm =
    engine->GetMaterialList ()->FindByName ("stone");

  room = engine->CreateSector ("room");
  //here
  csRef<iMeshWrapper> walls (
    engine->CreateSectorWallsMesh (room, "walls"));
  csRef<iThingFactoryState> walls_state =
    scfQueryInterface<iThingFactoryState> (walls->GetMeshObject ()->GetFactory());
  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);
//here
 
  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);

  engine->Prepare ();
}


/*---------------*
 * Main function
 *---------------*/
int main (int argc, char* argv[])
{
  return csApplicationRunner<CEGUITest>::Run (argc, argv);
}
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #13 on: December 22, 2008, 09:03:09 pm »

Ah, I forgot to tell you that you need to switch to 2D mode first before drawing the pixmap:

g3d->BeginDraw(CSDRAW_2DGRAPHICS);

Greetings,
Logged
Mcarsten
Newbie
*
Posts: 19


View Profile Email
« Reply #14 on: December 22, 2008, 09:07:57 pm »

awesome, that is exactly what i needed.
thanks again!
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 4.332 seconds with 15 queries.