Author Topic: Scroll Text with Index out of bounds error, Help needed.  (Read 4462 times)

0 Members and 1 Guest are viewing this topic.

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Hi Guys,
I am hopping someone can clear this up for me.
I am using the scroll text routine that I found on here a long time ago, But I had to change the screen size in my program 1024 x 768
now I get an out of bounds error with my font array as I changed the For/Next loop

Here is the code that would produce the error.

Code: [Select]
Repeat
StartDrawing (ScreenOutput())
  ;DrawImage (UseImage(font),0,0)
cco=0

For cc=0 To 32
letter=(Asc(Mid(t$,tptr+cc,1))-31)   ; Debug output: letter = -31 out of bounds!

DrawImage(UseImage(gfxfont(letter)),(sco+cco),300+50*Sin((cc+cco+sco+m)/120)) 
cco=cco+32
Next
m=m-5
  sco=sco-3
  If sco<-32
  tptr=tptr+1
  sco=sco+32
  EndIf
  If tptr>(Len(t$)-30 )
  tptr=1
  EndIf
  StopDrawing()
ExamineKeyboard()
FlipBuffers()
ClearScreen(0)

Until KeyboardPushed (#PB_Key_Escape)

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
what version of purebasic you using?

I have purebasic 5.11 at the moment :)

Offline combatking0

  • JavaScript lives!
  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4569
  • Karma: 235
  • Retroman!
    • View Profile
    • Combat King's Barcode Battler Home
What value does "tptr" have?
You are our 9001st visitor.
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
HotShot:  v5.21 LTS
Combatking0: tptr = 54 at the time of the error.

Thanks guys

Offline nikin

  • C= 64
  • **
  • Posts: 57
  • Karma: 15
    • View Profile
If letter is a unsigned int then it rashes here
Quote
letter=(Asc(Mid(t$,tptr+cc,1))-31)   ; Debug output: letter = -31 out of bounds!

Or else.. it crashes here.
Quote
gfxfont(letter)

As i guess, the gfxfont does not have a -31 element.

I do not know the language, but if Asc() returnes ASCII code for string (the -31 suggests it does as 32 is space, which is the first reasonable character), then the -31 character means that you got a 0 ASCII char in there.

Quote
If tptr>(Len(t$)-30 )
looks a bit suspicious. (i do not like hard coded numbers :) )

tptr=54 would mean that that t[54+32] is 0. so your 86. character turns out to be ASCII 0 .

I might be totally off here.. really  :whack: me :)

hmm one more thing comes in mind. UTF8 chars have a prefix of 00 (for  Latin1) .. so if you have those, and the string is nterpreted as ASCII then that can also cause this. see this : http://en.wikipedia.org/wiki/C1_Controls_and_Latin-1_Supplement
 
« Last Edit: December 22, 2013 by nikin »

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
I don't have PB here, but make sure drawimage won't crash with wrong letter value:

Code: [Select]
if letter<0
  letter = 0
endif
if letter>MAX_IMAGE_LETTERS     ;---> You need to know how much letters you have on your gfx font.
  letter = MAX_IMAGE_LETTERS
endif
DrawImage(UseImage(gfxfont(letter)),(sco+cco),300+50*Sin((cc+cco+sco+m)/120)) 
« Last Edit: December 22, 2013 by rbz »
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
I have been playing with it to try and sort it out.
It would seam that it has something to do with the number of literations in the for/next loop.
Reducing the number sorts out the problem, But the start position of the scrolled text is then wrong!  ???

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
If you post your gfx font + complete code of this scroller, I can download PB to test it.  :D
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
I am pleased to say I have sorted it out guys, It was just a few numbers out  :-[

Code: [Select]
Procedure ScrollText()

cco = 0

  For c = 0 To 30
letter = (Asc(Mid(t.s,tptr+c,1))-31)
DisplayTransparentSprite(Fonts(letter), sco+cco, 720, 255)
cco + 42
Next
 
sco - 2

If sco < -32 : tptr + 1 : sco + 42 :EndIf
  If tptr > (Len(t.s)-30) : tptr = 1 :EndIf

EndProcedure

Thanks Guys for your help.