Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A little help with open layer

Credits go to Fladimir da Gorf for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
A little help with open layer
FMC
Member #4,431
March 2004
avatar

I'm making a Qix clone, basically i have an image covered by another image, everytime you complete a shape that shape gets erased from the upper image, showing the one underneath.

What i do is simply:

Canvas::SetTo(gdata.mask); //i set the drawing surface to the upper image
Canvas::SetPixelWriteMode( ALPHA_ONLY ); 
a.Draw( Rgba(255,255,255));  //a is the shape polygon, drawing with alpha should make a "hole"
// Set the screen as the active canvas // 
Canvas::SetTo( SCREEN_BACKBUF );

problem is that as soon as i call Canvas::SetTo(gdata.mask); the upper image disappears!???
What am i doing wrong?

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

To render textured polygons, you could use the new TexturedPoly. You shouldn't call Canvas::SetTo after you've drawn to the screen, or otherwise it won't work with some hardware (I think this should be in the manual, but maybe not...)

To clip to arbitary regions, I recommend drawing to the alpha channel of the screen and using Blenders::Set( GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA );

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

FMC
Member #4,431
March 2004
avatar

Well, before using Canvas::SetTo i call GfxRend::RefreshScreen();,shouldn't this fix it?

Am i wrong or TexturedPoly isn't in the 2.0 release?

Anyway i'm attaching source and binary (allegro 4.2) so that you can have a better idea of what my problem is :)

P.S. I've found a bug in LineStrip: if two points are too close it gets rendered weirdly, is this a known issue?

[edit]too big (600k) to attach... linky

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

CursedTyrant
Member #7,080
April 2006
avatar

Quote:

Well, before using Canvas::SetTo i call GfxRend::RefreshScreen();,shouldn't this fix it?

RTFM? In fact, it can only make matters worse.

Quoted from OL Manual:

Quote:

GfxRend::RefreshScreen can still be used for backwards compability but it's highly deprecated as it may mess up your code if you use Canvas as well. So if you use Canvas, it's best to use only Canvas, not RefreshScreen.

Call Canvas::Refresh() instead. And no, I did not find TexturedPoly in the newest release of OL either (I had to comment out the #include in OpenLayer.hpp).

---------
Signature.
----
[My Website] | [My YouTube Channel]

FMC
Member #4,431
March 2004
avatar

Well, i usually check the fucking demos, and there GfxRend::RefreshScreen() is still used ;)

Anyway Canvas::Refresh doesn't help.

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

TexturedPoly can be found in the latest SVN version. Could you post the whole code so that I can check it out?

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

FMC
Member #4,431
March 2004
avatar

Attached with second post :)

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

OK, I'm blind :)

Quote:

P.S. I've found a bug in LineStrip: if two points are too close it gets rendered weirdly, is this a known issue?

That's already fixed in SVN, too.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

CursedTyrant
Member #7,080
April 2006
avatar

---------
Signature.
----
[My Website] | [My YouTube Channel]

FMC
Member #4,431
March 2004
avatar

To clear things up i have:

underneath image:{"name":"15ph.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/6\/e64f2ade9c31d490b4b88a2deb21f3c0.jpg","w":320,"h":240,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/6\/e64f2ade9c31d490b4b88a2deb21f3c0"}15ph.jpg

upper image (gdata.mask):{"name":"22bk1.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/6\/b6302bef5c6df54675aad140dc584212.jpg","w":320,"h":240,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/6\/b6302bef5c6df54675aad140dc584212"}22bk1.jpg

i should get: {"name":"30vt.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/1\/21cc7298c130ba11413a7c9f2a8c406a.jpg","w":640,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/1\/21cc7298c130ba11413a7c9f2a8c406a"}30vt.jpg

instead i get:{"name":"45hz.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/62b990cf1de789827f45ea4ca91206fa.jpg","w":640,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/62b990cf1de789827f45ea4ca91206fa"}45hz.jpg

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

Canvas::SetPixelWriteMode( ALPHA_ONLY );
a.Draw( Rgba(255,255,255)); //a is the shape polygon, drawing with alpha should make a "hole"

The problem is that you're rendering "visibility" instead of "invisibility". The following code works for me:

Canvas::SetTo(gdata.mask);
Canvas::SetPixelWriteMode( ALPHA_ONLY );

Blenders::Push();
Blenders::Set( COPY_BLENDER );
a.Draw( Rgba::INVISIBLE );
Blenders::Pop();
// Set the screen as the active canvas // 
Canvas::SetTo( SCREEN_BACKBUF );

Note that the code requires the latest SVN version (COPY_BLENDER and other advanced Blenders stuff aren't in 2.0).

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

FMC
Member #4,431
March 2004
avatar

There is something fishy going on... even if i comment out a.Dwaw as soon as i call Canvas::SetTo(gdata.mask); the upper image disappears...

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

Have you downloaded the latest version from the SVN repository yet? Your program works fine for me with the code I posted.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

FMC
Member #4,431
March 2004
avatar

Updated with SVN and used your code, everything is fine, thank you :)

P.S. Examples still use the deprecated GfxRend::RefreshScreen
P.P.S. it would be nice if you had the berlios link in your sig

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

P.P.S. it would be nice if you had the berlios link in your sig

Damn right, I wonder why I haven't thought of that... Now the link points there instead of SF.net.

Quote:

P.S. Examples still use the deprecated GfxRend::RefreshScreen

OK, I should take all those out.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Neil Walker
Member #210
April 2000
avatar

Quote:

OK, I should take all those out.

I already have (and fixed some more examples), though I haven't updated the SVN yet :)

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

FMC
Member #4,431
March 2004
avatar

A couple things i noted:

1)void CopyFromScreen(int x, int y ) prints 3 numbers in the console (no idea of why it does)

2)the manual should specify that SetPixelWriteMode( COLOR_ONLY ); is NOT the default when canvas is set to a bitmap; right now it says it is the default.

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

The numbers are probably some debugging stuff I've forgot there. About the PixelWriteMode - in the manual I can see it says:

Quote:

COLOR_AND_ALPHA - Both the color value and alpha channel are affected (Default for Bitmaps)

Maybe you're looking at an older one?

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

FMC
Member #4,431
March 2004
avatar

I'm using the docs in your sig :)

I'm getting some unexpected behaviour, i'm not sure if it's my fault:

1void draw(){
2 Bitmap tmp((*gdata.buff), mpos.x-30, mpos.y-30, gdata.bcursors[1].Width(), gdata.bcursors[1].Height());
3
4 Canvas::SetTo( *gdata.buff );
5
6 gdata.himage.Blit(0,0);
7 gdata.mask.Blit(0,0);
8 sEnemy.draw();
9 gdata.bcursors[0].Blit(mpos.x-40, mpos.y-40);
10 
11 draw_trail();
12 draw_info();
13
14 Canvas::Refresh();
15
16 //blit the buffer copy to the magnifying lens
17 Canvas::SetTo(gdata.bcursors[1]);
18 Canvas::SetPixelWriteMode( COLOR_ONLY );
19 tmp.Blit(0.0, 0.0);
20 Canvas::SetPixelWriteMode(COLOR_AND_ALPHA);
21 Canvas::Refresh();
22
23 //blit all to screen
24 Canvas::SetTo( SCREEN_BACKBUF );
25 gdata.buff->Blit(0,0);
26 gdata.bcursors[1].BlitStretched(mpos.x-39.0, mpos.y-40.0, gdata.bcursors[1].Width()*1.5, gdata.bcursors[1].Height()*1.5); //blit the magnified image
27 Canvas::Refresh();
28
29 tmp.Destroy();
30 }

draw_info code:

1void draw_info(){
2 Rect a(10.0, 10.0, 200.0, 55.0);
3 a.SetRoundness(10.0);
4 a.Draw(Rgba(110,110,110, 140));
5 Rect b(10.0, 415.0, 90.0, 60.0);
6 b.SetRoundness(10.0);
7 b.Draw(Rgba(110,110,110, 140));
8 char buff[128];
9 sprintf(buff, "Vite: %d", mpos.vite);
10 std::string text = buff;
11 gdata.neuro.Print( text, 20, 30 );
12 sprintf(buff, "Punteggio: %d", mpos.score);
13 text = buff;
14 gdata.neuro.Print( text, 20, 50 );
15 sprintf(buff, "%02d%%", mpos.manca);
16 text = buff;
17 gdata.neuro.Print( text, 20, 460 );
18 sprintf(buff, "%02d:%02d", mpos.tempo/6000, (mpos.tempo/100)%60);
19 text = buff;
20 gdata.neuro.Print( text, 20, 440 );
21 }

I'm trying to get a magnifying lens effect

Mouse cursor gdata.bcursors[0]: ball7yi.png

Mask used: gdata.bcursors[1]: ball2eh.png

what i get: {"name":"image18hm.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/7\/77d75a1abf5ad005ed008395e57eea8d.jpg","w":644,"h":508,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/7\/77d75a1abf5ad005ed008395e57eea8d"}image18hm.jpg

Various problems:
1)Some people display it differently from me (in a "broken" way)
2) As you can see, in the upper left (the score rect) you get a copy of what is in the lens... ???

Thanks for all the help and for the great lib you provided :)

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

There's a better an much more efficient way to do this (it's a "fake" way, but anyways).

- Make a circular textured polygon (during the startup) which has a texture the size of the lense
- Get a copy of the contents of the screen to the texture every frame
- When you need to draw the lense, just draw the textured polygon stretched to the size of the lense.

Did I explain it well enough?

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

FMC
Member #4,431
March 2004
avatar

Perfectly :)

Where can i find TexturedPoly?
I have downloaded the recect SVN docs but i can't find it

[edit]
Pseudo-code to see if i got it right:

Poly lens; //declared somewhere
Bitmap texture_lens; //attached to lens 
...
blit_all_but_mouse_cursor();
texture_lens.CopyFromScreen( where_mouse_is );
...
and here i am at a loss, how do i stretch a polygon?

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

Where can i find TexturedPoly?
I have downloaded the recect SVN docs but i can't find it

Unfortunately it's not in the manual yet, but it's just:

TexturedPoly myPoly( textureImage );
// Then use myPoly just like if it was a Poly

To pre-cache the texture nodes in loading-time, you can call myPoly.Construct() when you've finished building the polygon.

Quote:

and here i am at a loss, how do i stretch a polygon?

Using a Placement, for example, you can do this after you've constructed the new myPoly:

myPoly.TransformBy( Placement( Vec2D( 0.0, 0.0 ), 0.0, stretchFactor ));

where stretchFactor is 1.5 in your case.

EDIT: I implemented Ellipse::ToPolygon just for you ;) (In SVN now, as well as documentation for TexturedPoly)

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

FMC
Member #4,431
March 2004
avatar

Thanks again for all the help :)

so in init:

    gdata.myPoly = new TexturedPoly(gdata.bcursors[1]);//gdata.bcursors[1] is just a buffer
    for(int x=0; x<360; x+=20){//(i didn't see your edit Ellipse::ToPolygon)
        gdata.myPoly->Add(Vec2D(30.0*cos(x*3.14/180.0), 30.0*sin(x*3.14/180.0)));
        }
    gdata.myPoly->Construct();

then, the drawing code:

    draw_all_that_has_to_be_drawn();
    gdata.bcursors[1].CopyFromScreen(mpos.x-30, mpos.y-30);
    gdata.myPoly->TransformBy( Placement( Vec2D(0.0, 0.0), 0.0, 1.0));    
    gdata.myPoly->Draw(1.0);
    Canvas::Refresh();

Problem is that with any value different from 1.0 it doesn't draw ???
Also, do you suggest to recreate it everycicle with new coordinates or simply move it arount with TransformBy( Placement( Vec2D())?

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

Problem is that with any value different from 1.0 it doesn't draw

Well, that's odd. Maybe I've messed up the placement somehow.

Quote:

Also, do you suggest to recreate it everycicle with new coordinates or simply move it arount with TransformBy( Placement( Vec2D())?

It's best to move it with Placement, that's much more efficient.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

FMC
Member #4,431
March 2004
avatar

Fixed!
The problem was that when i called

gdata.myPoly->TransformBy( Placement( Vec2D(mpos.x+161, mpos.y+121), 0.0, 1.5));

i forgot to also call

    gdata.myPoly->TransformBy( Placement( Vec2D(-mpos.x-161, -mpos.y-121), 0.0, 1.0/1.5));

which resetted things for the next cycle :)

Only thing that remains to fix is some sort of weird corruption when "magnifying" text and translucent rectangles. Here you can see what i mean

The drawing code is quite slim now...:

1void draw(){
2 gdata.himage.Blit(0,0);
3 gdata.mask.Blit(0,0);
4 sEnemy.draw();
5 gdata.bcursors[0].Blit(mpos.x-40, mpos.y-40);
6
7 draw_trail();
8 draw_info();
9
10 gdata.bcursors[1].CopyFromScreen(mpos.x-30, mpos.y-30);
11 gdata.myPoly->TransformBy( Placement( Vec2D(mpos.x+161, mpos.y+121), 0.0, 1.5));
12 gdata.myPoly->Draw(1.0);
13 gdata.myPoly->TransformBy( Placement( Vec2D(-mpos.x-161, -mpos.y-121), 0.0, 1.0/1.5));
14
15 Canvas::Refresh();
16 }

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

 1   2 


Go to: