wxGraphicsContext and matrix routines Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
aquawicket
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Aug 05, 2007 5:49 am

wxGraphicsContext and matrix routines

Post by aquawicket »

I've started playing with wxGraphicsContext to rotate and scale images.
This may be more of a "working with drawing matrix's" than a wx question.
If i want to have multiple images drawn in my wxGraphicsContext and give them seperate rotations, how can I achive that?

When i rotate the second image, image 1 is rotated again as well. How can we make their rotation attributes apply separately?

Code: Select all

wxAutoBufferedPaintDC dc(this);
wxGraphicsContext *gc = wxGraphicsContext::Create( dc );

gc.Translate(img1.posX + img1.GetWidth()/2.0, img1.posY + img1.GetHeight()/2.0);
gc.Rotate(img1.rotation);
gc.Scale(img1.xScale, img1.yScale);
gc.Translate(-img1.pos.x + img1.GetWidth()/2.0, -img1.pos.y + img1.GetHeight()/2.0);
gc.DrawBitmap(img1, img1.posX, img1.posY, img1.GetWidth(), img1.GetHeight());

gc.Translate(img2.posX + img2.GetWidth()/2.0, img2.posY + img2.GetHeight()/2.0);
gc.Rotate(img2.rotation);
gc.Scale(img2.xScale, img2.yScale);
gc.Translate(-img2.pos.x + img2.GetWidth()/2.0, -img2.pos.y + img2.GetHeight()/2.0);
gc.DrawBitmap(img2, img2.posX, img2.posY, img2.GetWidth(), img2.GetHeight());
I haven't found any samples or examples on wxGraphicsContext
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxGraphicsContext and matrix routines

Post by doublemax »

All Translate/Scale calls work on the current transformation matrix, so you have to reset it for each image.

There are two ways to do it:

Wrap the calls for each image in wxGraphicsMatrix::PushState() / PopState():

Code: Select all

gc.PushState(); 
gc.Translate(img1.posX + img1.GetWidth()/2.0, img1.posY + img1.GetHeight()/2.0);
// ...
gc.DrawBitmap(img1, img1.posX, img1.posY, img1.GetWidth(), img1.GetHeight());
gc.PopState();

gc.PushState();
// next image...
gc.PopState();

//and so on
Or set a default transformation matrix before each image:

Code: Select all

wxGraphicsMatrix gcm;

gc->SetTransform(gcm);
gc.Translate(img1.posX + img1.GetWidth()/2.0, img1.posY + img1.GetHeight()/2.0);
// ...
gc.DrawBitmap(img1, img1.posX, img1.posY, img1.GetWidth(), img1.GetHeight());

// next image...
gc->SetTransform(gcm);
Use the source, Luke!
aquawicket
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Aug 05, 2007 5:49 am

Re: wxGraphicsContext and matrix routines

Post by aquawicket »

Both work great.. thanks.
Post Reply