Display issue when keeping key or mousebutton down

BlitzMax Forums/BlitzMax Beginners Area/Display issue when keeping key or mousebutton down

Pierrou(Posted 2009) [#1]
Hello,
I am relatively new to Blitzmax and I must say I love it. These days I am developing a program that displays text files at various sizes in a viewport on the main graphics object (it will be part of a reading game intended for speech therapists [I am a speech therapist myself). It seemed quite easy to let the text scroll inside that viewport by displaying the text a little bit higher or lower on the viewport (ex to scroll 4 pixels down I display the whole text 4 pixels higher).
It almost works, using KeyDown(KEY_DOWN) or Mousedown on a button drawn somewhere on the screen. But when the key or the mousebutton is down for too long, the text disappears instead of keeping on scrolling, and never appears again whatever I try to do. It doesn't seem to happen when keeping the key or the mouse button down more briefly.
I hope these explainations are clear enough despite my poor English and all.
Thanks in advance for your help,


TaskMaster(Posted 2009) [#2]
Without a code snippet, we can't really help you. but, you are probably just adding too many pixels too quickly and making it shoot way off the screen?!?!


Pierrou(Posted 2009) [#3]
This code adds or substracts 4 pixels to the starting point of the display
and then calls function "affiche" (which displays the text) when holding one button or the other.
All variables except sourisx and sourisy are global
If MouseDown(1)
			sourisx=MouseX()
			sourisy=MouseY()
			If sourisx>margex
				If sourisy>margeh-2*bordure And sourisy<margeh-bordure Then decal=decal+4 affiche
				If sourisy>graphicsy-3*bordure And sourisy<graphicsy-2*bordure Then decal=decal-4 affiche
			EndIf
		EndIf


And this is the main part of the affiche function that displays text :
Function affiche()

DrawRect margex,margeh,graphicsx-margex-bordure,graphicsy-margeh-4*bordure ' affichage texte
nombreligne=0
affichefic=ReadFile("temp.txt")
	If affichefic
		While Not Eof(affichefic)
			ligneaffichee$=ReadLine(affichefic)
			SetColor 255,255,255
			SetViewport margex,margeh,graphicsx-margex-bordure,graphicsy-margeh-4*bordure
			DrawText ligneaffichee$,(affichaged/2-300)+margex,margeh+hauteurligne+decal
			hauteurligne=hauteurligne+espacevert
		Wend
	EndIf
	Flip
SetViewport 0,0,graphicsx,graphicsy
End Function

It reads each line of the text file and displays it at the bottom of the previous one ("hauteurligne increases each time, espacevert being in fact the font height).
I hope it's quite clear...?
NB : margex, margeh , bordure, graphicsx, graphicsy are dimensions set at the beginning and don't vary.


TaskMaster(Posted 2009) [#4]
Hmmm, you are doing some things you probably shouldn't.

Like reading that file every time the function is called. You should just read it once at program startup. Unless that file is being changed somewhere else.

Also, you do not show where you are calling your if MouseDown code. If it is in a loop that is called on every frame, then it is possibly getting called tens if not hundreds of times per second.


Pierrou(Posted 2009) [#5]
Thank you for your help
1/ It might not be the best way to do it but the "temp.txt" file is a file generated each time the user changes the font size : the program reads the original text file (which might have very long lines), cuts each line depending on the font size to fit into the viewport and saves the lines into the "temp.txt". Then the temp.txt file is displayed. I probably should not open it if the font size remains the same, right.
2/ Indeed the mousedown is part of a small loop contaning that code and and the code relative to keyboard commands. That small loop is repeated forever, and is probably repeated hundreds of times per second. Seems as if you have something much better in mind...?
Thank you again,


Brucey(Posted 2009) [#6]
Couldn't you just use a String[] array or TList to hold your lines, rather than messing around in the file-system?

Memory is generally faster than accessing disks :-)

If you add some DebugLog messages, it may give you an idea of offsets and suchlike when everything disappears... like "oh, why is 'y' such a big number suddenly?"


Pierrou(Posted 2009) [#7]
I probably could indeed :)
In fact, I expected the file-system method to be much slower. But it "quite" worked....
I will try the string array and see what happens. Thank you!


Pierrou(Posted 2009) [#8]
Aaaah! It works much better that way... It obviously had something to do with the over-repeated file accesses. I mean, thanks to you it became obvious. Great language, great community.... !


_Skully(Posted 2009) [#9]
What I would recommend is to pull the whole text file into a string then take enough of the string each time to fill a line (by font size) and remember your "cursor" position for the next line.


Pierrou(Posted 2009) [#10]
I'll think of it. Thank you!