Canvas.paint has an auto-refresh built in?

Error: Type “DesktopCanvas” has no member named “invalidate”.

use Refresh(False)

Where False means do not force the refresh (the same as invalidate did previously)

3 Likes

no. i didn’t mute you.

the problem here is not the refresh, not the timer, not (anymore) how to create a static picture to make it as background.

the problem is the canvas2 is refreshing when it’s over another canvas1 and i trigger canvas1.refresh.

i don’t wan’t canvas2 do the refresh.

:wink:

No, I’m not stubborn. Sometimes it’s hard to explain in detail, because it’s hard for me to translate into English.

Anyway, did you read my suggestion and go fishing?
(No offense intended)

yes. it’s simple as that. for you, maybe. for me, it’s very new, and very hard. it’s my first game i’m “writing”.

a game i played when i was 7, back in 80’s

this is a forced way to keep learning. a lot.

That’s a problem you cannot avoid. If a control is a child of another control and the parent gets a refresh, its child will also be invalidated. That’s the reason for the hints to use only one canvas and not to stack controls.

well, i’ll wait xojo check if it’s a bug or not. and if make sense to DO NOT refresh other canvas “in contact” with one that was triggered to refresh…

https://tracker.xojo.com/xojoinc/xojo/-/issues/79735

Anyway, I appreciate all the help and comments here. I’ve already changed some things, like @ChristopheDV suggestion about the frequency rate, and the idea from @MarkusR and @Tim_Hare of turning “the background” into an image. This certainly improves the game’s performance, even if it’s not necessary to do so, it’s nice to implement it as a good practice.

And of course, the time spent by @Emile_Schwarz , who stopped going fishing to suggest an excellent book for my learning.

:wink:

1 Like

I guess the graphics mockup you sent will be the full game display?
Might I ask where you see the necessity for a second canvas?

it’s not a child. it’s only acting like a layer.

the original game is only 31 columns x 23 rows on a 6 inch diagonal display. the display of HP-85.

about 500 pixels x 340 pixels

the version 2 of this game will have much more than 31 levels, and will be full screen, and a lot of other stuffs.

in fact i do not need anything about second canvas, buffer, refresh rate, etc. after read about the profile, and tested i thought about improve the game. and one thing is to make layers to keep static things static…

then i discovered this “bug”. that’s it!

There’s no reason to wait. It’s not a bug.

8 Likes

In this case, it does not matter if a control simply overlaps or is embedded in another control.
Basically a refresh is a message to the windows compositing system to redraw a rectangular area - which will in most cases be the area of the control.
If some other control overlaps, the compositing engine must also redraw this one, and it will do so by invalidating the overlapping area too.

As Thom said, it’s no bug. You cannot create a static layer this way. You will slow things down inevitably. You should reconsider your approach. What on that UI do you want to buffer? I see no reason for it, your code should be fast enough. Start with the basics first and if that should be too slow, ask for optimisation hints.

2 Likes

Having read your ticket: Use 2 pictures. #1 the background, #2 the cookies. Draw both over each other in the canvas’ paint event. And finally draw the respective sprite images from their pictures on the correct position. This will take as good as no time.

1 Like

you do a canvas1.refresh, and then canvas2 do the refresh too… just because it’s leaning against each other. i did not trigger the canvas2.refresh…

You cannot expect a control only to get the invalidations you invoke with a refresh. The system will also do. Like each time you resize a window, each control on it will get refreshes.

2 Likes

Basically you are right, it IS possible to use layers. But these are then, in macOS terms, subviews attached to a view (or on a lower level they are indeed called layer). Consider them invisible canvases you add to one canvas control and which can get static content. Which is system specific and would include individual code for each platform you address, plus these methods are not part of Xojo’s xplatform support so you would have to use declares into the systems or 3rd party developments.

The xplatform way to achieve what you want to do with Xojo is to use pictures as buffers. You can create a transparent picture, fill it with the cookie symbols and when the mouse overlaps a cookie, delete this one in the buffer picture by drawing with a different color (or Color.Clear) over its position. This will all work in one Canvas and on all systems, and surely fast enough.

1 Like

ahhh, ok

But these are then, in macOS terms, subviews attached to a view (or on a lower level they are indeed called layer).

thank you for your explanation!

1 Like

One canvas only, the “screen”.
One picture as the offscreen “canvas” used in the composition.
I would go creating classes of the objects in the game, I could group them into “planes” (layers) and paint them from the, lets call plane 0 the background, and painting all planes towards the front-most foreground, let’s call it plane 5, 6 “layers” to compose an instance of one full frame. Every object will carry all the properties I need to control any behavior, position, plane, where it is, current status, visible, type, picture, text, color, whatever. all you need to move a rat is to change its X,Y and wait another cycle of the 60 times per second (every 16ms) the composer present the new frame. The composer will create a blank picture, paint all layers there, all objects from plane 0 to 5, and finally paint the composed picture into the canvas to present it without flicking it (you won’t see ghosts and noise of every little piece being painted)
I hope you understand the principle.

That’s the key. Think object-oriented coding.
The cat, mice and dots each needs to objects with each its own behavior, properties, methodes, draw methode, … . Use a constructor to initiate each when you declare them.
Then draw those objects in ONE canvas and refresh that canvas every 13ms with a timer to get stable frame rates.
That should be your road to succes.

Try the example “objects in canvas” that comes with xojo