Author Topic: predefined arrays  (Read 3511 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
predefined arrays
« on: January 14, 2012 »
Hi guys I was wondering if someone could help as I am using Blitzmax and I am reading data values in to an array
to create my sine wave,  but the values just don't work right, plus there seams to be a speed issue as well, here is
my code Thanks.

 Framework brl.GLMax2D
Import BRL.BMPLoader
Import BRL.Retro

SetGraphicsDriver GLMax2DDriver()

Const Xres:Int = 800
Const Yres:Int = 600

Global Image:TImage = LoadAnimImage("sfont16.bmp", 16, 16, 0, 59)
Global Fonts:TPixmap[] = Image.pixmaps

Global Scrlpos:Int
Global Tpos:Int = 1
Global SineAdd:Int

Global Y1:Float[80]   ' Arrays for Sine values
Global Y2:Float[84]

Global Angle:Int   ' Array counter

Graphics(Xres, Yres, 32, GRAPHICS_BACKBUFFER)

glViewport 0, 0, Xres, Yres
glMatrixMode GL_PROJECTION
glLoadIdentity()
gluOrtho2D(0.0, Xres, 0.0, Yres)
glMatrixMode GL_MODELVIEW
glLoadIdentity

Global Scrolltext:String = "                                                     "
Scrolltext:+"this is my opengl sine wave scroll text routine this was part "
Scrolltext:+"written for me on the dbf forum by raizor and shockwave..  wrap! "
Scrolltext:+"                                                                    "
Scrolltext = Upper(Scrolltext)
                                                            
Type FPS
   Global Counter, Time, TFPS
   Function Calc%()
      Counter:+1
      If Time < MilliSecs() Then
         TFPS = Counter
         Time = MilliSecs() + 1000
         Counter = 0
      EndIf
      Return TFPS
   EndFunction
EndType

RestoreData Data1
For Local a:Int = 0 To 79
   ReadData Y1[a]   '      Read values in to array for sine wave 1
Next

RestoreData Data2
For Local b:Int = 0 To 83
   ReadData Y2   '      Read values in to array for sine wave 2
Next

Repeat

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
   glEnable(GL_TEXTURE_2D)
   glPushMatrix()
   glLoadIdentity()
   
   GLDrawText("FPS: " + FPS.Calc(), 20, 20)

   SineAdd:-3
   Scroller()
   
   glPopMatrix()
   Flip(-1)
   
Until KeyDown(KEY_ESCAPE)

EndGraphics
End

Function Scroller()
   
   Local Drawpos:Int
   Local Textadd:Int
   Local Letter:Int
   Local Font:Int
   
   Scrlpos:+2
   
   glColor4f(1, 1, 1, 1)
   
   If Scrlpos > 16 Then
      Scrlpos = Scrlpos - 16
      Tpos = Tpos + 1
      If Tpos > Len(Scrolltext) - 52 Then Tpos = 1
   End If
            
   Drawpos = (-16) - Scrlpos

   Repeat
   
   If Scrlpos > 15 Then
      Angle:+1
      If Angle = 84 Then Angle = 0
   End If

   
      Letter = Asc(Mid(Scrolltext, (Tpos + Textadd), 1)) - 32
      Font = GLTexFromPixmap(Fonts[Letter])
      glBindTexture(GL_TEXTURE_2D, Font)
      
      ' Old Methods   
      'YCord1 = (Yres / 2) + (40 * Sin(SineAdd + Drawpos))
      'YCord2 = (Yres / 2) + (40 * Sin(SineAdd + Drawpos) + 16)
      
      glLoadIdentity()
   
      glBegin(GL_QUADS)
         glColor4f(0.2, 0.2, 1, 1)                              ' Blue Colour
         glTexCoord2f 0, 1; glVertex3f 0 + Drawpos, 0 + Y1[Angle], 0       ' Bottom Left
         glTexCoord2f 1, 1; glVertex3f 16 + Drawpos, 0 + Y2[Angle], 0     ' Bottom Right
         glColor4f(1, 1, 1, 1)                                 ' White Colour
         glTexCoord2f 1, 0; glVertex3f 16 + Drawpos, 16 + Y2[Angle], 0   ' Top Right
         glTexCoord2f 0, 0; glVertex3f 0 + Drawpos, 16 + Y1[Angle], 0     ' Top Left
      glEnd
         
       Textadd:+1
      Drawpos:+16

   Until Drawpos >= Xres

End Function

#Data1
DefData 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319
DefData 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339
DefData 338, 337, 336, 335, 334, 333, 332, 331, 330, 329, 328, 327, 326, 325, 324, 323, 322, 321, 320, 319
DefData 318, 317, 316, 315, 314, 313, 312, 311, 310, 309, 308, 307, 306, 305, 304, 303, 302, 301, 300, 299

#Data2
DefData 375, 378, 381, 384, 387, 390, 393, 396, 399, 402, 405, 408, 411, 414, 417, 420, 423, 426, 429, 432, 435, 438, 441, 444, 447, 450, 453, 456, 459, 462, 465, 468, 471, 474, 477, 480, 483, 486, 489, 492, 495, 498
DefData 498, 495, 492, 489, 486, 483, 480, 477, 474, 471, 468, 465, 462, 459, 456, 453, 450, 447, 444, 441, 438, 435, 432, 429, 426, 423, 420, 417, 414, 411, 408, 405, 402, 399, 396, 393, 390, 387, 384, 381, 378, 375
« Last Edit: January 14, 2012 by Pot Noodle »

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: predefined arrays
« Reply #1 on: January 14, 2012 »
Can you attach the project to please Pot Noodle?
raizor

Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: predefined arrays
« Reply #2 on: January 14, 2012 »
Sure thing Raizor.

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: predefined arrays
« Reply #3 on: January 14, 2012 »
Thanks. Will take a look, hopefully tonight :)
raizor

Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: predefined arrays
« Reply #4 on: January 16, 2012 »
Give this a shot Pot Noodle. It's a bit quick and dirty, but you should be able to follow what's going on. I had some headaches with modulus due to not be overly familiar with BlitzMax.

It works as follows:

I generate a sine table loopup that's 512 values in length (sin(0) up to sin (255) down to sin(0)). This gives us a sine table where the first and last entries match and we can cycle though it.

I then have a global sine offset, which acts as an index into the sine table. This global offset is updated each frame. I use modulus to make sure the incremented value always falls in the range 0 - 511. Modulus essentially provides a remainder when a number is divided by another number.

5 mod 2 equals 1   (2 goes into 5 twice and gives us a remainder of 1).

So we can happily increment the global sine offset and modulus will always ensure the value is in the 0 - 511 range.

The scroller looks a bit plain due to the single sine table lookup and offset. You probably want to play around with using two sine tables and mixing the values together to offset the Y position.

The other option is to have two offsets in the sine array. Each offset would get incremented by a different number each frame (i.e. sine offset #1 gets 1 added to it each frame, sine offset #2 gets 3 added to it each frame). You then get the values from the sine table corresponding to the offsets, add them together and then divide the result by the number of offsets. This allows you to use a single sine table and still get a nice organic wave effect. You can also weight the values, for instance, take 40% of value #1 and 60% of value #2 (or whatever mix you like). You could also vary these weightings over time too, to make it even more organic.

You probably want to add some delta timing to the scroller too. Instead of updating the position by 1 (or 16 or whatever) each frame, get the system time each frame and use the time elapsed since last frame to determine the scroller scroll position.
« Last Edit: January 16, 2012 by Raizor »
raizor

Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: predefined arrays
« Reply #5 on: January 17, 2012 »
First I would like to say thanks Raizor for all your work and time, I can see that it was not easy for you to sort this one out and that I now you are not familiar with Blitzmax, but forgive me if I am out of order here but I would like to say that if it wasn't for you I would have not had any help with this at all, Please do not take this the wrong way, I am just happy that you took time out from you busy schedule to help me.
Thanks again.
 

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: predefined arrays
« Reply #6 on: January 17, 2012 »
Glad to help Pot Noodle, you're welcome :)
raizor

Challenge Trophies Won: