Dragging or moving the window fast causes hang

BlitzMax Forums/BlitzMax Beginners Area/Dragging or moving the window fast causes hang

Takis76(Posted 2012) [#1]
Hello ,

I am creating my application with MAXGUI and I use some canvas gadget to draw my maps and some dungeon preview for my level editor.

When I am moving the window of my application or shaking fast or my window is outside of the screen , then my application hangs (Freezes for a long time) , if I wait and leave it , it will continue to work and unfreeze , but sometimes the application cause the not response error and windows forces to terminate.

I made a little research in the forums and you are saying like something about hooks which I didn't understand.

Also there is the EVENT_WINDOWMOVE , I put all RedrawGadget() for all my canvases , but the problem wasn't fixed.

Also put the RedrawGadget() for all canvases in my main event loop , but this made the program hangs again faster.

Is there some easy solution to make my program updated the window hook event?

Thank you very much.

Last edited 2012


jsp(Posted 2012) [#2]
When I am moving the window of my application or shaking fast or my window is outside of the screen , then my application hangs (Freezes for a long time)

This sounds like the events add up a lot in the event queue and when leaving it for a while they are processed and the application is responsive again.
Either there are too many events, or the time to process is too long.

something about hooks which I didn't understand.

I don't think hooks will help you here, your application would probably freeze even earlier. This happens because there is no queue when using hooks, but every event is processed immediately (although you could create your own queue then).

Also put the RedrawGadget() for all canvases in my main event loop , but this made the program hangs again faster.

As the cycle to redraw is probably taking too much time this makes things even worse when in the main loop. Try to use the redraw as less as possible (instead of more) and try to optimize that function if possible.

Is there some easy solution to make my program updated the window hook event?

Again, a hook is probably not the solution here. A hook helps if you need immediate action when an event occurs, for example you resize a window and the event goes into the queue but you can process that event only after releasing the mouse button which is may too late. Using a hook lets you redraw your window content already while resizing, which looks a lot better.
Don't know if you are using Logic Gui (only for design or also program structure), because it can create your whole form hooked in the events but you need to understand Blitzmax types quite well.
Old but still valid discussion about hooks:
http://www.blitzbasic.com/Community/posts.php?topic=79728


Takis76(Posted 2012) [#3]
If hooks not solves the problem , what do you think that will.

I have problem because my application hangs if I act violently with the window or if I put the window outside of the screen and my application is unusable.

I have only one RedrawGadget() function when I press the horizontal and vertical scrollers to update my map. My map is a canvas gadget with tiles.

I have put all my drawing canvas gadgets updates in the GADGETPAINT event.

I setGraphics on each of my canvases and I flip after drawing something in my canvases.
If I will put the flip command once at the end of the EventID only the last canvas will be drawn and the graphics will be false in that last canvas.

	If EventID() = EVENT_GADGETPAINT Then
	
		SetGraphics CanvasGraphics(background_canvas_panel)
		Draw_Background_panel_wallpaper()

		SetGraphics CanvasGraphics(map)
		Draw_Map()
		
		SetGraphics CanvasGraphics(dungeon_preview)
		Draw_Dungeon_Preview()
		
		SetGraphics CanvasGraphics(north_wall_preview)
		Draw_North_Walls_Preview()
		
		SetGraphics CanvasGraphics(south_wall_preview)
		Draw_South_Walls_Preview()
		
		SetGraphics CanvasGraphics(west_wall_preview)
		Draw_West_Walls_Preview()
		
		SetGraphics CanvasGraphics(east_wall_preview)
		Draw_East_Walls_Preview()
		
		SetGraphics CanvasGraphics(lower_panel_canvas)
		Draw_Lower_Panel()
		
		
		'Flip <--- If I put it here I have false results
	End If

Each function contains the flip command separately

I have seen many of posts about hooks and I don't have understand nothing.

What is the difference between flip and flip 0?


jsp(Posted 2012) [#4]
If hooks not solves the problem , what do you think that will.

Without seeing the code I can just guess and would say a different code design.

I have problem because my application hangs if I act violently wit

If you can't stop the events and can't minimize the time for the redraw you could may set a flag that a redraw is in progress and when a new event occurs it will be just ignored.
Another way could be to reduce the redraws to a certain FPS by calculating the MilliSecs between redraws.

if I put the window outside of the screen and my application is unusable

What happens then?

I have only one RedrawGadget() function when I press the horizontal and vertical scrollers to update my map. My map is a canvas gadget with tiles.

Do you draw everything or only what is visible? Maybe you loose some time drawing tiles not anymore visible.

I setGraphics on each of my canvases and I flip after drawing something in my canvases.

Sounds ok. I would also set the ViewPort before drawing on each of the canvasses, also to cut out all invisible area.
SetViewport 0,0,GadgetWidth( Canvas ),GadgetHeight( Canvas )

If I will put the flip command once at the end of the EventID only the last canvas will be drawn and the graphics will be false in that last canvas.

Every canvas needs it's own flip as you already discovered.

What is the difference between flip and flip 0?

Flip 0 does not wait for the vertical blank and thus occurs as soon as possible.


Takis76(Posted 2012) [#5]
I am drawing all tiles on my canvas , but I think graphics that outside of the canvas size , not presents or does?

I tried and setviewport function for all canvases , but I didn't have and improvement.

I upload the game file with source code to see how the program freezes.

https://www.box.com/s/697437255e9ce60f2342

Last edited 2012


*(Posted 2012) [#6]
Do you use Event = PollEvent() or WaitEvent?

PollEvent() can allow the system to continue when there are no events in the cue excellent for server apps, also adding a Delay(1) at the end of your main loop will free the CPU cycles for windows to use other things this also has a knock on effect of making your apps faster.


jsp(Posted 2012) [#7]
Load_Data.bmx is missing in the download.


Takis76(Posted 2012) [#8]
Neither PollEvent() Nor Delay(1) worked.

I have already use waitevent() to capture my events.

PollEvent() makes the application freezes more.


Takis76(Posted 2012) [#9]
I will upload in the morning


Takis76(Posted 2012) [#10]
The file was updated , Includes the Load_Data.bmx

https://www.box.com/s/697437255e9ce60f2342


matibee(Posted 2012) [#11]
It won't compile: Draw_Dungeon_Preview not found. Plus I need a zipstream module installed. So I couldn't prove what I'm about to recommend.

Your gadgetpaint code is flawed. You should just repaint the gadgets that need repainting, at the moment you are drawing all three views for every gadgetpaint message you receive, regardless of who asked for it. Your loop should be more like this:

Repeat

	WaitEvent()
	
	If EventID() = EVENT_WINDOWCLOSE And EVENT_APPTERMINATE Then End
	If EventID() = EVENT_GADGETPAINT Then
	
		Select EventSource()
		Case background_canvas_panel
			SetGraphics CanvasGraphics(background_canvas_panel)
			Draw_Background_panel_wallpaper()
			Flip
		Case map
			SetGraphics CanvasGraphics(map)
			Draw_Map()
			Flip
		Case dungeon_preview
			SetGraphics CanvasGraphics(dungeon_preview)
			Draw_Dungeon_Preview()
			Flip
		End Select 
		
	End If

Forever



Takis76(Posted 2012) [#12]
Ok ,
I uploaded again with koriolis zipstream mod included
With exe file included
With the folder and outside my BlidePlus solution.

If you don't have the BlidePlus the Eob4_Editor.bmx is the main file
If you don't have blide plus just load the maxide and load Eob4_Editor,bmx and everything will compile.


https://www.box.com/s/717e40bf56e8271d4973


jsp(Posted 2012) [#13]
There are also too many flip, sometimes in the draw function and then again in the main loop.

As matibee said there are still things missing to compile it.

The ZipStream module can be found here:
http://www.koriolis-fx.com/forum/index.php?action=dlattach;topic=15.0;attach=19

When I run the provided editor.exe and just doing nothing your program takes more or less 100% CPU. As you have a WaitEvent() in your main loop it looks like that you produced somewhere an endless loop or constantly generating events.


Takis76(Posted 2012) [#14]
Strange why my program not compiles to you and here compiles perfectly?

Where do I put my waitevent()?

I include ZipStream module in my editor.zip you have downloaded.

What things are still missing?

Here I have CPU Usage 1%


jsp(Posted 2012) [#15]
Sorry, didn't saw your new upload.
My last post was still related to the first code.

When I compile your new code it also is at 1%, will have a closer look later.


Zeke(Posted 2012) [#16]
mod/brl.mod/eventqueue.mod/eventqueue.bmx:
[bbcode]
Function Hook:Object( id,data:Object,context:Object )
Local ev:TEvent=TEvent( data )
If Not ev Return

Select ev.id
Case EVENT_WINDOWMOVE,EVENT_WINDOWSIZE,EVENT_TIMERTICK,EVENT_GADGETACTION,EVENT_GADGETPAINT 'added EVENT_GADGETPAINT
PostEvent ev,True
Default
PostEvent ev
End Select

Return data
End Function[/bbcode]
and rebuild modules.


Takis76(Posted 2012) [#17]
I change a lot of code and changed if(s) with Select(s) and Case(s)

And the program messed up. It became little faster , it freezes again but unfreezes little faster , might "select" and "case" commands are faster whan "if" commands.

The canvases was messed up , now canvaces draws other graphics than it should , for example graphics from upper canvas (The tools canvas) presents graphics for Map X and Y , Lower_panel_canvas.

If I will not use RedrawGadget() my gadgets not redraws to present the updated graphics in the canvases , but if I will use RedrawGadget() the program becomes slower and freezes longer.

Conclusion:

I will not use MaxGui for my Level Editor , I am working and with another one level editor (Backup)

The link of the second version of the level editor:
https://www.box.com/s/880c4a768974757502d0

Which works better , not freezes , but uses the ifsoGui module. Things that exist in MaxGui not exists in ifsoGui. IfsoGui not freezes my program and verything works nice.

But things are missing in ifsoGui that exists in MaxGui, like canvas gadgets and menus. (TaskMaster knows about this and fed up with my report).

A lots of difficulties with this language. Many of the modules not compiles , a lots of struggles. wxGui Module which is the best for this case not compiles at all.

I will create my level editor only and nothing else. (If I will manage to finish it).

The main game which works perfectly was created with PureBasic and PureGDK module. And there is no problem. (You will say why not you create your level editor with PureBasic and are you complaining here?) , The Gui system of PureBasic is not so easy as BlitzMax , (MaxGui and ifsoGui modules).

I spent a lots of money for the language for modules for expansions for programs , I am very unhappy.

The language alone without no modules, works. But a lots of things are missing.

The commercial modules which worked was , ifsoGui, but is incomplete.
The maxGui , is abandoned and is incomplete and not works well.
And from free modules the only module which compiled was koriolis zipstream which works perfectly.

My new ifsoGui skin is included take it as a gift.

For people who is curious to see the main game download this:
https://www.box.com/s/56b41340c30234d4fb0a

The old editor included which was created with MS-Visual Basic 6 , but is vary old and unstable language with newer versions of windows. XP & 7.


Takis76(Posted 2012) [#18]

mod/brl.mod/eventqueue.mod/eventqueue.bmx:


Function Hook:Object( id,data:Object,context:Object )
Local ev:TEvent=TEvent( data )
If Not ev Return

Select ev.id
Case EVENT_WINDOWMOVE,EVENT_WINDOWSIZE,EVENT_TIMERTICK,EVENT_GADGETACTION,EVENT_GADGETPAINT 'added EVENT_GADGETPAINT
PostEvent ev,True
Default
PostEvent ev
End Select

Return data
End Function

and rebuild modules




What do you mean with this?

To go to my mod folder to my brl module and in eventqueue.mod folder , I will find one source file with name eventqueue.bmx
and put the code you gave me above?

Where to put the code and how to call the new code from my application?
Or does it called automatically and fix whole my problems?
My new changes I made , I messed up my application , I will load my backups does this fix my problems?

I will make the changes and I will post what I found. If the problem will be fixed.

How did you learn all of these? Really one new programmer of one new language (NEW 7 years old) anyway , do any new programmer need to make all of these surgeries in the code to make everything work correctly , what happen for one newbie who don't know how to do all of these and there is not any documentation and all of these fixes not included when you purchase the language out of the box.


Takis76(Posted 2012) [#19]
Ok the last surgery was successful , it fixed my problem , I load the backups , before I change the code from if to selects and cases and now when I am shaking the window the program not freezes.

So why does this not was fixed in the official language?

Do you liked the second version of my editor?

The MaxGui version of the editor will become like the first editor the Visual Basic 6 one.

May I ask and one thing which must be fixed?

BUG WITH SCROLLBARS:

When you pressing the body of the horizontal scrollers and vertical scroll bars , not the up and down arrows , not the handler , but the body of the scrollbar , when you press the body of the scrollbar body highlights and not scrolls , only if you drag the handler and push the arrows , would you please help me to fix and this?

What kind of surgery doctors do I need to do in which file?
I will keep safe all of these updates.

Thank you very much , now at least I have some hope to continue to develop my level editor. :)

When I will finish whole my game , I will keep one gift for people who helped me.

Last edited 2012


Zeke(Posted 2012) [#20]
scrollbars fix:
when you create sliders and setslider range, change lower range from 0 to 1:
[bbcode]
map_horizontal_scroller = CreateSlider(0, 361, 601, 18, background_canvas_panel, SLIDER_HORIZONTAL)
SetSliderRange(map_horizontal_scroller, 0, 18)

map_vertical_scroller = CreateSlider(602, 0, 16, 361, background_canvas_panel, SLIDER_VERTICAL)
SetSliderRange(map_vertical_scroller, 0, 28)
[/bbcode]
to:
[bbcode]
map_horizontal_scroller = CreateSlider(0, 361, 601, 18, background_canvas_panel, SLIDER_HORIZONTAL)
SetSliderRange(map_horizontal_scroller, 1, 19) 'range 0 to 18

map_vertical_scroller = CreateSlider(602, 0, 16, 361, background_canvas_panel, SLIDER_VERTICAL)
SetSliderRange(map_vertical_scroller, 1, 29) 'range 0 to 28
[/bbcode]

also small fix to mainloop:
[bbcode]
If EventID() = EVENT_GADGETACTION Then
If EventSource() = map_horizontal_scroller Or map_vertical_scroller Then
RedrawGadget(map)
'Flip
EndIf
[/bbcode]
to:
[bbcode]
If EventID() = EVENT_GADGETACTION Then
If EventSource() = map_horizontal_scroller Or EventSource()=map_vertical_scroller Then 'fixed
RedrawGadget(map)
'Flip
EndIf
[/bbcode]

Last edited 2012


jsp(Posted 2012) [#21]
What Zeke suggest in his first post is to change the behavior of the MaxGUI internal Event Queue.
All events are collected in a queue and some events have the chance to flood the queue until overflow.
So what is done in the hook he describes is to ignore a certain event if there is still one of the same type in the queue.
Means when now a GADGETPAINT event comes up it will be ignored if there is still one in the queue.
The most common event who floods the queue is the TIMERTICK... when too fast.
This could help in your case to a certain extent.
I see some other problems when looking into your code, which I think should be solved first.

What I see is that you use a lot of different canvasses (7 or so), in my opinion you could just use one. That would already minimize some problems, as it is much easier to handle.
Another problem is that you put gadgets on top of a canvas, canvas on canvas, slider on canvas and so on. Please avoid it totally to put any other gadget on a canvas it is not really designed for it (nor supported).

When you only want to display a certain wallpaper without any animation, just use a simple panel and set the picture as pixmap to it.
When trying to draw your different stuff which is now visible in an extra canvas on the canvas, like the North,West... just use a simple offset value to draw on the big canvas.
Choose your gadgets in a way that the canvas is in the middle and other control gadgets like textfields, buttons, stepper, sliders are outside the canvas.

Types could simplify your workflow a lot.


Takis76(Posted 2012) [#22]
:) Perfect


Takis76(Posted 2012) [#23]
My program works well now why do I need put my slider outside of my canvases? I want my slider to be beside of my level number texts.

I had used lots of label gadgets , but I was need to refresh the gadgets and sometime all of these label gadgets was blinking.
I was need a lots of variables for creating all of these label gadgets so now I put a lots of things inside the canvas with the simple blitzmax core commands like drawtext and changing colors without need to mess with a lots of gadgets creations.

For example the tools panel , which I will put all of my tools buttons , I don't need to create buttons gadgets as image buttons not exist , I don't have yet used the smartbuttons , but I don't like the idea to have a panel gadget as a button , and again I will need to create a lots of panel gadgets.

The 4 canvases which you see the North South West and East wall side for each square and the dungeon preview, might be 5 useless canvaces , I will see if I will remove them and just use draw image in specific position.

Even if I use only one canvas for whole window area , again if I will draw the map tiles positioned on this only one canvas , I will need to put horizontal and vertical scrolls inside the canvas again , because the map needs horizontal and vertical scrollers.

But I will need and other canvases , when I choose other options from menus which I don't have ready yet. For example each of options of the game are on one panel , the panel have one canvas and on the canvas you see the graphics.

When the program starts you see the map edit panel , but there will be and other panels which will have canvases for adventure properties , for monster properties , for dungeon mechanics etc...


jsp(Posted 2012) [#24]
My program works well now why do I need put my slider outside of my canvases?
I had used lots of label gadgets , but I was need to refresh the gadgets and sometime all of these label gadgets was blinking.

As said, the way you use it, it is not supported. The blinking or flickering gadgets is one of the results. This flickering also means extra redraws which means extra time.

For example the tools panel , which I will put all of my tools buttons...

I don't know how your editor should look at the end, but there are always different possibilities.
You could just use the big canvas and reserve a part for the tools and do all things via graphics.
Or you could show all tools in a ListBox where you could choose the one needed. A ListBox can have also icons as big as your tiles.
If the tools are limited a normal toolbar may even enough. If needed you could put the toolbar in an extra floating window on top.
Even a bunch of panel (each with a tile) inside a ScrollPanel could be used, and so on...

Even if I use only one canvas for whole window area , again if I will draw the map tiles positioned on this only one canvas , I will need to put horizontal and vertical scrolls inside the canvas again , because the map needs horizontal and vertical scrollers.

The ScrollBars need to be outside the canvas, which means you need a little bit different design of the form to achieve the same as what you want.

But I will need and other canvases , when I choose other options from menus which I don't have ready yet. For example each of options of the game are on one panel , the panel have one canvas and on the canvas you see the graphics.
When the program starts you see the map edit panel , but there will be and other panels which will have canvases for adventure properties , for monster properties , for dungeon mechanics etc...

I don't understand clearly what you mean here. It is not so that you can't have another canvas, of course you can, but you don't need it if you just want to display different things. The same canvas can show your tiles, then show the map, then show the 3D view and so on. It depends only what you draw, in what mode you are like edit mode or trial mode in your program.


Takis76(Posted 2012) [#25]
I am making changes for using only one canvas.