Announcement

Collapse
No announcement yet.

High Speed Graphics Pt. 2

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • High Speed Graphics Pt. 2

    Ok Guys.. Check this out.. I wrote this code using PB1.1 cause its all
    I had here but I think my results were pretty amazing..

    I first created an optimized line (ray trace) routine without
    using distance formulas or Single Point Variables.

    Hence this routine..

    Sub FastRay (ByVal x1 as Long, ByVal y1 as Long, ByVal x2 as Long, ByVal y2 as Long)
    Dim xchange as Long, ychange as Long
    Dim xp as Long, yp as Long, ray as Long, ray_seg as Long
    Dim x as Long, y as Long
    Dim ray_xchange as Long, ray_ychange as Long
    Dim count as long, lowest_change as Long
    Dim x_Step as Long, y_Step as Long

    ray_xchange = x2 - x1
    x_step = Sgn(ray_xchange)
    ray_ychange = y2 - y1
    y_step = Sgn(ray_ychange)

    xp = x1
    yp = y1


    If ray_xchange < ray_ychange Then
    lowest_change = Abs(ray_xchange)
    Select Case lowest_change
    Case 0
    ray_seg = Abs(ray_ychange)
    Case Else
    ray_seg = Abs(ray_ychange / lowest_change)
    End SELECT

    For ray = 1 to lowest_change
    For count = 1 to ray_Seg
    Screen(xp,yp) = 0
    yp = yp + y_step
    Next count
    xp = xp + x_step
    Next ray

    Else
    lowest_change = Abs(ray_ychange)
    Select Case lowest_change
    Case 0
    ray_seg = Abs(ray_xchange)
    Case Else
    ray_seg = Abs(ray_xchange / lowest_change)
    End SELECT

    For ray = 1 to lowest_change
    For count = 1 to ray_Seg
    Screen(xp,yp) = 0
    xp = xp + x_step
    Next count
    yp = yp + y_step
    Next ray
    End if
    End Sub


    I think took this code an placed it in a loop to fill an area 1024 x 768.
    I am running this routine on a Pentium III 1Ghz so my frame rates
    are high. I wanted to know what people with lower machine speeds
    recieve from this little routine.


    Sub CompleteTest () EXPORT
    Dim x1 as Long, y1 as Long, x2 as Long, y2 as Long
    Dim yl as Long, xloop as Long


    'Actual Ray Variables
    Dim xchange as Long, ychange as Long
    Dim xp as Long, yp as Long, ray as Long, ray_seg as Long, ray_height as Long
    Dim x as Long, y as Long, h as Long
    Dim ray_xchange as Long, ray_ychange as Long
    Dim count as long, lowest_change as Long
    Dim x_Step as Long, y_Step as Long


    For xloop = 1 to 80
    For yl = 0 to 1024
    x1 = 0 : y1 = y
    x2 = 768: y2 = y


    'Actual Ray Cast Routine
    ray_xchange = x2 - x1
    x_step = Sgn(ray_xchange)
    ray_ychange = y2 - y1
    y_step = Sgn(ray_ychange)

    xp = x1
    yp = y1


    If ray_xchange < ray_ychange Then
    lowest_change = Abs(ray_xchange)
    Select Case lowest_change
    Case 0
    ray_seg = Abs(ray_ychange)
    Case Else
    ray_seg = Abs(ray_ychange / lowest_change)
    End SELECT

    For ray = 0 to lowest_change
    For count = 1 to ray_Seg
    Screen(xp,yp) = 0
    'Screen(xp,yp+1) = 0
    'Screen(xp+1,yp) = 0
    'Screen(xp+1,yp+1) = 0
    yp = yp + y_step
    Next count
    xp = xp + x_step
    Next ray

    Else
    lowest_change = Abs(ray_ychange)
    Select Case lowest_change
    Case 0
    ray_seg = Abs(ray_xchange)
    Case Else
    ray_seg = Abs(ray_xchange / lowest_change)
    End SELECT

    For ray = 0 to lowest_change
    For count = 1 to ray_Seg
    Screen(xp,yp) = 0
    'Screen(xp,yp+1) = 0
    'Screen(xp+1,yp) = 0
    'Screen(xp+1,yp+1) = 0
    xp = xp + x_step
    Next count
    yp = yp + y_step
    Next ray
    End if
    'End of Ray Cast Routine

    Next yl
    Next xloop
    End Sub

    With this Pentium III 1Ghz machine I got 80 frames in under a second
    1024x768. You will see some commented lines where I tried to double
    the frame rate by simply deviding my itterations 1/2 and doubling
    the Screen variable set routine. In most cases this would speed
    up routines, but I found that this slowed my code down. (This is 16bit
    compiled code but I used Longs throughout.)

    Tell me if there are some areas that need optimization or should
    I keep this routine.. I would like to get 160fps on a 1Ghz machine
    though.. Because I'm sure the frame rate will drop considerably
    once I add color affects and stuff..

    Thanks a bunch to all who helped in this design..

    ------------------
    Explorations v3.0 RPG Development System
    http://www.explore-rpg.com
    Explorations v9.10 RPG Development System
    http://www.explore-rpg.com

  • #2
    Sorry for the double post but I'm soo excited right now I had to share this..

    I am home now, and working on my AMD380Mhz machine. I took the
    above code and replaced the reference to Screen(x,y) with
    ptrScreen WORD PTR.

    All references to arrays are @ptrScreen[xp * 768 + xp] = value
    And was able to crank out 1024x768x80 in WELL less than a second.
    Pointers are awesome!!

    And thats 16bit PowerBasic 2.0!

    I just changed the outer most loop to 200 (yea 200 fps) And I'm still
    under 1 second! - I wonder if this thing is really working.. ha!

    I will double check the logic and get back to you..


    The obvious bottleneck for the code is storing into variables. Its
    MUCH faster to store a value via a pointer than to store a value
    via a VARIABLE of any type - Long, WORD, DWORD etc..



    ------------------
    Explorations v3.0 RPG Development System
    http://www.explore-rpg.com
    Explorations v9.10 RPG Development System
    http://www.explore-rpg.com

    Comment

    Working...
    X