Recent

Author Topic: Painting on a control - 3 questions: OnPaint, ImageList, program logic  (Read 3384 times)

lainz

  • Hero Member
  • *****
  • Posts: 4685
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #15 on: January 22, 2025, 09:51:28 pm »
If your game doesn't flicker do as you wish, but there's a flicker in some cases because you're painting several times on the screen. Noticeable only in some old hardware (that I had in time and noticed it with Lazarus and Windows 7).

TRon

  • Hero Member
  • *****
  • Posts: 3928
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #16 on: January 22, 2025, 09:54:23 pm »
If your game doesn't flicker do as you wish, but there's a flicker in some cases because you're painting several times on the screen. Noticeable only in some old hardware (that I had in time and noticed it with Lazarus and Windows 7).
This was exactly what I wanted to write as I remember minesweeper on win95 and when a lot of cells were being opened you could see the flickering of the cells when they were drawn. Ofc that has been a long time ago with different hardware but still it did not have to that and could be solved even back then.
 
@majolika: PS: how is the weather over there ?  (let's up that postcount of yours  :D )

edit: nevermind. I just noticed you are  not 'green' anymore. You should be able to edit your messages now.
« Last Edit: January 22, 2025, 09:58:18 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

majolika

  • New Member
  • *
  • Posts: 42
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #17 on: January 22, 2025, 10:12:31 pm »
PS: how is the weather over there ?  (let's up that postcount of yours  :D )
Just got back from the outside. :)
During the day it was +7°C but now there's a bliizzard out there and about –15°C.

lainz

  • Hero Member
  • *****
  • Posts: 4685
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #18 on: January 22, 2025, 10:15:53 pm »
Here is 35° C.

And yes I can also remember the flickering from the original minesweeper from Windows 95.  :)

majolika

  • New Member
  • *
  • Posts: 42
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #19 on: January 22, 2025, 10:22:23 pm »
Here is 35° C.
Lucky you! :)


And yes I can also remember the flickering from the original minesweeper from Windows 95.  :)
I remember flickering in MS DOS on my monochrome display. :))

TRon

  • Hero Member
  • *****
  • Posts: 3928
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #20 on: January 22, 2025, 10:29:29 pm »
Just got back from the outside. :)
During the day it was +7°C but now there's a bliizzard out there and about –15°C.
Minus 15 ? darn that is not weather to show up in shorts. It's currently around 4°C here.

Still not able to edit a message ?
I do not have to remember anything anymore thanks to total-recall.

majolika

  • New Member
  • *
  • Posts: 42
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #21 on: January 22, 2025, 10:41:48 pm »
Still not able to edit a message ?
No. But that's not the most wanted feature for me. :)

You better tell me why my Button1 works only once. :)

Logic is pefectly clear:
• Button clicked? → ok, set global bool
• in OnPaint check global bool, if true do staff  and unset global bool
Nothing special, it seems. But then Button stops reacting.
Why? WHY?!

TRon

  • Hero Member
  • *****
  • Posts: 3928
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #22 on: January 22, 2025, 11:25:16 pm »
You better tell me why my Button1 works only once. :)
Oh, I'm sorry. I missed that you still have issues.

Quote
Logic is pefectly clear:
• Button clicked? → ok, set global bool
• in OnPaint check global bool, if true do staff  and unset global bool
Nothing special, it seems. But then Button stops reacting.
Why? WHY?!
I can't answer the why as your logic seems correct. However I suspect that the paintbox does not get a refresh on secondary press of your button. Why that does work for the first press and not (as you stated) consecutive ones I have no idea. Try adding a call to paintbox1.refresh at your buttonclick event after having set the necessary values and see if that helps.

edit: on second thought. The first time around the dimensions of the paintbox are being set which probably triggers the repaint. On consecutive runs the dimensions are the same, so nothing has to be changed/refreshed.
« Last Edit: January 22, 2025, 11:34:02 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

majolika

  • New Member
  • *
  • Posts: 42
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #23 on: January 22, 2025, 11:35:44 pm »
Try adding a call to paintbox1.refresh at your buttonclick event after having set the necessary values and see if that helps.
It works! Great! Thanks a lot!
But why it works?! What's the matter?

TRon

  • Hero Member
  • *****
  • Posts: 3928
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #24 on: January 22, 2025, 11:37:30 pm »
But why it works?! What's the matter?
See edit in my previous post. You can verify that by setting the paintbox dimensions on form create event and then try the buttonclick. If my clarification is correct then it should not paint on the first click either.

To be fair in that case you should assign the onpaint event in the formcreate event as well.
I do not have to remember anything anymore thanks to total-recall.

majolika

  • New Member
  • *
  • Posts: 42
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #25 on: January 22, 2025, 11:55:05 pm »
If my clarification is correct then it should not paint on the first click either.
It works fine: paint from the first click, even wothout assigning onpaint in the fomcreate.
Your guess is interesting but does not explain how paintbox.onpaint corupt button.onclick?
How one control can so unclearly ingluence another thougth I must send signals with the global vars, bot directly.
It blows my mind. I need to get some sleep. :)

majolika

  • New Member
  • *
  • Posts: 42
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #26 on: January 22, 2025, 11:56:46 pm »
Sorry for typos I'm dying to sleep.

TRon

  • Hero Member
  • *****
  • Posts: 3928
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #27 on: January 23, 2025, 12:23:25 am »
Sorry for typos I'm dying to sleep.
By all means, then get some sleep please. There is always tomorrow. I see you are still logged in. I'll have a look at it later because I have other things on my plate right now as well. We'll figure it out  :)
I do not have to remember anything anymore thanks to total-recall.

TRon

  • Hero Member
  • *****
  • Posts: 3928
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #28 on: January 23, 2025, 01:04:09 am »
It works fine: paint from the first click, even wothout assigning onpaint in the fomcreate.
Your guess is interesting but does not explain how paintbox.onpaint corupt button.onclick?
How one control can so unclearly ingluence another thougth I must send signals with the global vars, bot directly.
It blows my mind. I need to get some sleep. :)
Well, in that case there must be something off with your testing method or a difference in widgetset/platform.

If the onpaint method is removed from the object inspector and set the dimensons of the paintbox at form create event and after that assign the onpaint event...
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   PaintBox1.Width := FieldCol * BoxSize;
  4.   PaintBox1.Height := FieldRow * BoxSize;
  5.   PaintBox1.OnPaint := @Self.PaintBox1Paint;
  6. end;
  7.  

.. and have the buttonclick event read:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Button1Clicked := True;
  4. //  PaintBox1.Width := FieldCol * BoxSize;
  5. //  PaintBox1.Height := FieldRow * BoxSize;
  6. //  PaintBox1.Refresh;
  7. end;
  8.  

Then the paintbox onpaint event will not be called at that click and as a result the grid will not be painted and will stay empty.

This is to be expected as changing the width or height (e.g. the value you set differs from what the value of that property already was set to) triggers for the paintbox control to be resized, in the process triggering an onpaint event.

Because code in this post does not have an onpaint event assigned while resizing the paintbox, the paintbox will not draw anything besides what it draws in its default paint implementation (which apparently is an empty box).

Hence we have to trigger the event ourselves by either using .refresh, .update, .invalidate or invoke the paint method (whatever is applicable for the situation).

See also attached project.
I do not have to remember anything anymore thanks to total-recall.

lainz

  • Hero Member
  • *****
  • Posts: 4685
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Painting on a control - 3 questions: OnPaint, ImageList, program logic
« Reply #29 on: January 23, 2025, 03:04:18 am »
Use a Timer and refresh the paintbox every 33ms (1000/30) to get something like 30fps.

Is like real games works.

 

TinyPortal © 2005-2018