Author Topic: TTDblitzlib for tinyptc  (Read 3700 times)

0 Members and 1 Guest are viewing this topic.

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
TTDblitzlib for tinyptc
« on: March 20, 2009 »
Hi all, while looking over my old code I came across a lib that I started in 2006 for freebasic using tinyptc, later myself and zawran expanded it beyond what I started with but I thought it would be nice to share the first version with everyone here, it doesnt do a massive amount as such but provides some wrapper functions around tinyptc and then some basic drawing functions. I havent tested it on tinyptc_ext but am sure it should work. Was made in FB0.15 i think so might need some tweaks for the latest version.

The lib include itself, save as ttdblitzvib1.bi
Code: [Select]
'-------------------------------------------------------------------
' The Tin Dragons
' TinyPTC Based Lib to emulate a blitz2d like enviroment
'
' by Joncom2000
' Coded May 2006
'
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' Includes
'-------------------------------------------------------------------
#Include Once "tinyptc.bi"
'-------------------------------------------------------------------
' Setup Constants
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' Declare Globals
'-------------------------------------------------------------------
dim shared as integer ptr Backbuffer
dim shared as integer XRES, YRES
'-------------------------------------------------------------------
' Pre Declare Subroutines
'-------------------------------------------------------------------
declare sub TTDGraphics(ByVal x as integer, ByVal y as integer, Byval title as string)
declare sub TTDEndGraphics()
declare sub TTDFlip()
declare sub TTDCls(ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
declare sub TTDFastCls(ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
declare sub TTDPlot(ByVal x as integer, ByVal y as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
declare sub TTDLine(ByVal x1 as double, ByVal y1 as double,ByVal x2 as double, ByVal y2 as double,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer,ByVal Alpha as single)
declare sub TTDOval(ByVal xCenter as integer,ByVal yCenter as integer,ByVal Rx as integer,ByVal Ry as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)

'-------------------------------------------------------------------
' Actual Subroutines Follow
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' NAME : TTDGraphics()
' PURPOSE : This function opens a screen display
' INPUTS : x=width, y=height, title=app title.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDGraphics(ByVal x as integer, ByVal y as integer, ByVal title as string)
    if ptc_open(title, x, y) = 0 then
        print "Cannot open compatible graphics display :"+str(x)+", "+str(y)
        sleep
        end -1
    endif
    Backbuffer = callocate(x*y, Len(integer))
    XRES=x
    YRES=y
End Sub
'-------------------------------------------------------------------
' NAME : TTDFlip()
' PURPOSE : This function Flips the backbuffer into view
' INPUTS : None.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDFlip()
    ptc_update(Backbuffer)
End Sub
'-------------------------------------------------------------------
' NAME : TTDEndGraphics()
' PURPOSE : This function closes ptc window
' INPUTS : None.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDEndGraphics()
    ptc_close
End Sub
'-------------------------------------------------------------------
' NAME : TTDCls()
' PURPOSE : This function clears the buffer to RGB passed
' INPUTS : Red, Green,Blue values range 0-255.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDCls(ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
    Dim ClsColor as integer
    ClsColor= ( Red shl 16 ) + ( Green shl 8 ) + Blue
    For y=0 to YRES-1
        For x=0 to XRES-1
            Backbuffer[(y*XRES+x)] = ClsColor
        next
    next
End Sub
'-------------------------------------------------------------------
' NAME : TTDFastCls()
' PURPOSE : This function clears the buffer to RGB passed
' INPUTS : Red, Green,Blue values range 0-255.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDFastCls(ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
    Dim ClsColor as integer
    Dim num_ints as integer 'Size of buffer were clearing
    ClsColor= ( Red shl 16 ) + ( Green shl 8 ) + Blue
    num_ints=XRES*YRES
    asm
       mov eax, [ClsColor]
       mov ecx, [num_ints]
       mov edi, [BackBuffer]
       cld
       rep stosd
   end asm
End Sub
'-------------------------------------------------------------------
' NAME : TTDPlot()
' PURPOSE : This function plots a pixel in RGB color
' INPUTS : x,y position and Red, Green,Blue values range 0-255.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDPlot(ByVal x as integer, ByVal y as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
    Dim PlotColor as integer
    PlotColor = ( Red shl 16 ) + ( Green shl 8 ) + Blue
    if (x>-1) AND (x<XRES) AND (y>-1) AND (y<YRES) THEN
        Backbuffer[(y*XRES+x)] = PlotColor     
    end if
End Sub
'-------------------------------------------------------------------
' NAME : TTDLine()
' PURPOSE : This function draw a line of RGB and Has Alpha option
' INPUTS : x1,y1,x2,y2,r,g,b
' RETURNS : Nothing.
'-------------------------------------------------------------------
Sub TTDLine(ByVal x1 as double, ByVal y1 as double,ByVal x2 as double, ByVal y2 as double,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer,ByVal Alpha as single)
    Dim PlotColor,OldColor,TempColor as integer
    Dim r2,g2,b2 as integer
    Dim xI,Alpha2 as double
    PlotColor = ( Red shl 16 ) + ( Green shl 8 ) + Blue
    xI=0
    x2 = x2-x1
    y2 = y2-y1
    If Abs(x2)>Abs(y2) THEN
        steps = Abs(x2)
    Else
        steps = Abs(y2)
    Endif
    xI = x2 / steps
    y2 = Y2 / steps
    if Alpha>=1.0 then
        For x2 = 1 To steps
            dy=Int(y1)
            dx=Int(x1)
            If dy>-1 And dy<YRES then
                If dx>-1 And dx<XRES then
                    Backbuffer[(dy*XRES+dx)] = PlotColor 
                EndIf
            EndIf
            x1=x1+xI:y1=y1+y2
        Next
    else
        Alpha2=1.0 - Alpha
        For x2 = 1 To steps
            dy=Int(y1)
            dx=Int(x1)
            If dy>-1 And dy<YRES then
                If dx>-1 And dx<XRES then
                    OldColor=Backbuffer[(dy*XRES+dx)]
                    'do alphablend
                    r2 = ( ( OldColor Shr 16 ) And 255 ) * Alpha + ( ( PlotColor Shr 16 ) And 255 ) * Alpha2
g2 = ( ( OldColor Shr 8 ) And 255 ) * Alpha + ( ( PlotColor Shr 8 ) And 255 ) * Alpha2
b2 = ( OldColor And 255 ) * Alpha + ( PlotColor And 255 ) * Alpha2
If r2 > 255 Then r2 = 255
If g2 > 255 Then g2 = 255
If b2 > 255 Then b2 = 255
                    TempColor = ( r2 shl 16 ) + ( g2 shl 8 ) + b2
                    Backbuffer[(dy*XRES+dx)] = TempColor 
                EndIf
            EndIf
            x1=x1+xI:y1=y1+y2
        Next
    endif
end Sub
'-------------------------------------------------------------------
' NAME : TTDFastLine()
' PURPOSE : This function draw a line of RGB
' INPUTS : stx,sty,enx,endy,r,g,b
' RETURNS : Nothing.
'-------------------------------------------------------------------
Sub TTDFastLine(Byval stx as integer,Byval sty as integer,Byval enx as integer,Byval eny as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
Dim stpx,stpy as double
dim mvx,mvy,mv as integer
Dim PlotColor as integer
    PlotColor = ( Red shl 16 ) + ( Green shl 8 ) + Blue
mvx=Stx-enx
    mvy=sty-eny
If mvx<0 then
        mvx=-mvx
    endif
If mvy<0 then
        mvy=-mvy
    endif
If mvy>mvx then
        mv=mvy
    Else
        mv=mvx
    endif
stpx=(mvx/mv)
    If Stx>enx then
        stpx=-stpx
    endif
stpy=(mvy/mv)
    If Sty>eny then
        stpy=-stpy
    endif
For nc=0 To mv
        If sty>-1 And sty<YRES then
            If stx>-1 And stx<XRES then
                Backbuffer[(sty*XRES+stx)] = PlotColor 
            EndIf
        EndIf
stx=stx+stpx
        sty=sty+stpy
Next
End Sub
'-------------------------------------------------------------------
' NAME : TTDOval()
' PURPOSE : This function draw a Oval of RGB
' INPUTS : xcentre,ycentre=centre of oval Rx,Ry=size of oval r,g,b=color
' RETURNS : Nothing.
'-------------------------------------------------------------------
Sub TTDOval(ByVal xCenter as integer,ByVal yCenter as integer,ByVal Rx as integer,ByVal Ry as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
    Dim p,px,py,x,y as integer
    Dim Rx2,Ry2,twoRx2,twoRy2 as integer
    Dim pFloat as single
    ' The complex code I converted from a bmax example
    Rx2=Rx*Rx
    Ry2=Ry*Ry
    twoRx2=Rx2 Shl 1
    twoRy2=Ry2 Shl 1
    'Region 1
    x=0
    y=Ry
    TTDPlot(xCenter+x,yCenter+y,Red,Green,Blue)
    TTDPlot(xCenter-x,yCenter+y,Red,Green,Blue)
    TTDPlot(xCenter+x,yCenter-y,Red,Green,Blue)
    TTDPlot(xCenter-x,yCenter-y,Red,Green,Blue)
    pFloat=(Ry2-(Rx2*Ry))+(0.25*Rx2)
    p=Int(pFloat)
    If pFloat Mod 1.0>=0.5  Then
        p=p+1
    endif
    px=0
    py=twoRx2*y
    While px<py
        x=x+1
        px=px+twoRy2
        If p>=0 then
            y=y-1
            py=py-twoRx2
        EndIf
        If p<0 Then
            p=p+Ry2+px
        Else
            p=p+Ry2+px-py
        endif
        TTDPlot(xCenter+x,yCenter+y,Red,Green,Blue)
        TTDPlot(xCenter-x,yCenter+y,Red,Green,Blue)
        TTDPlot(xCenter+x,yCenter-y,Red,Green,Blue)
        TTDPlot(xCenter-x,yCenter-y,Red,Green,Blue)
    Wend
    'Region 2
    pFloat=(Ry2*(x+0.5)*(x+0.5))+(Rx2*(y-1.0)*(y-1.0))-(Rx2*(CSNG(Ry2)))
    p=Int(pFloat)
    If pFloat Mod 1.0>=0.5 Then
        p=p+1
    endif
    While y>0
        y=y-1
        py=py-twoRx2
        If p<=0 then
            x=x+1
            px=px+twoRy2
        EndIf
        If p>0 Then
            p=p+Rx2-py
        Else
            p=p+Rx2-py+px
        endif
        TTDPlot(xCenter+x,yCenter+y,Red,Green,Blue)
        TTDPlot(xCenter-x,yCenter+y,Red,Green,Blue)
        TTDPlot(xCenter+x,yCenter-y,Red,Green,Blue)
        TTDPlot(xCenter-x,yCenter-y,Red,Green,Blue)
    Wend
End sub

As you can see its nothing super fancy, but would be a good base to build up from for anyone new to using freebasic and tinyptc. with that heres an example using the line drawing function. (Note: #define PTC_WIN makes tinyptc open  window instead of fullscreen)

Code: [Select]
#define PTC_WIN
#include "ttdblitzlib1.bi"
'-------------------------------------------------------------------
' Test of Lib
'-------------------------------------------------------------------
TTDGraphics(640,480,"TTD Test App")
Dim count as integer
    do 
        TTDFastCls(0,0,0)
        for i=0 to 255
        TTDLine(0,10,500,10+i,0,i,0,0.5)
        TTDLine(0,YRES,500,YRES-i,0,0,i,0.5)
        next
        TTDFlip()
       
    loop until inkey$<>""
    TTDEndGraphics()
end
'-------------------------------------------------------------------
' End Test of Lib
'-------------------------------------------------------------------

Hopefully someone will find this useful, either as something to use or inspiration to make there own reuseable libs :)

Cheers
Jon


Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: TTDblitzlib for tinyptc
« Reply #1 on: March 20, 2009 »
Hi Jon, that's certainly very generous of you to offer your lib.
I did one myself, I haven't compared the two speed wise, my function does a few other things too so feel free to chop mine up to add to your own if you like;

http://www.dbfinteractive.com/downloadmenu.php

The one you want is the freebasic framework. (its in the libraries section)
I stopped working on my version so perhaps we can get our heads together to make a comprehensive version?

Oh and K+ to you too for sharing. Thanks.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: TTDblitzlib for tinyptc
« Reply #2 on: March 20, 2009 »
I fixed this up to work with version .21. There were a few errors since the variable declaration format has changed since .15.

ttdblitzvib1.bi
Code: [Select]
'-------------------------------------------------------------------
' The Tin Dragons
' TinyPTC Based Lib to emulate a blitz2d like enviroment
'
' by Joncom2000
' Coded May 2006
'
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' Includes
'-------------------------------------------------------------------
#Include Once "tinyptc.bi"
'-------------------------------------------------------------------
' Setup Constants
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' Declare Globals
'-------------------------------------------------------------------
dim shared as integer ptr Backbuffer
dim shared as integer XRES, YRES
'-------------------------------------------------------------------
' Pre Declare Subroutines
'-------------------------------------------------------------------
declare sub TTDGraphics(ByVal x as integer, ByVal y as integer, Byval title as string)
declare sub TTDEndGraphics()
declare sub TTDFlip()
declare sub TTDCls(ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
declare sub TTDFastCls(ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
declare sub TTDPlot(ByVal x as integer, ByVal y as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
declare sub TTDLine(ByVal x1 as double, ByVal y1 as double,ByVal x2 as double, ByVal y2 as double,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer,ByVal Alpha as single)
declare sub TTDOval(ByVal xCenter as integer,ByVal yCenter as integer,ByVal Rx as integer,ByVal Ry as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)

'-------------------------------------------------------------------
' Actual Subroutines Follow
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' NAME : TTDGraphics()
' PURPOSE : This function opens a screen display
' INPUTS : x=width, y=height, title=app title.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDGraphics(ByVal x as integer, ByVal y as integer, ByVal title as string)
    if ptc_open(title, x, y) = 0 then
        print "Cannot open compatible graphics display :"+str(x)+", "+str(y)
        sleep
        end -1
    endif
    Backbuffer = callocate(x*y, Len(integer))
    XRES=x
    YRES=y
End Sub
'-------------------------------------------------------------------
' NAME : TTDFlip()
' PURPOSE : This function Flips the backbuffer into view
' INPUTS : None.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDFlip()
    ptc_update(Backbuffer)
End Sub
'-------------------------------------------------------------------
' NAME : TTDEndGraphics()
' PURPOSE : This function closes ptc window
' INPUTS : None.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDEndGraphics()
    ptc_close
End Sub
'-------------------------------------------------------------------
' NAME : TTDCls()
' PURPOSE : This function clears the buffer to RGB passed
' INPUTS : Red, Green,Blue values range 0-255.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDCls(ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
    Dim ClsColor as integer
    ClsColor= ( Red shl 16 ) + ( Green shl 8 ) + Blue
    For y As Integer=0 to YRES-1
        For x As Integer=0 to XRES-1
            Backbuffer[(y*XRES+x)] = ClsColor
        next
    next
End Sub
'-------------------------------------------------------------------
' NAME : TTDFastCls()
' PURPOSE : This function clears the buffer to RGB passed
' INPUTS : Red, Green,Blue values range 0-255.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDFastCls(ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
    Dim ClsColor as integer
    Dim num_ints as integer 'Size of buffer were clearing
    ClsColor= ( Red shl 16 ) + ( Green shl 8 ) + Blue
    num_ints=XRES*YRES
    asm
       mov eax, [ClsColor]
       mov ecx, [num_ints]
       mov edi, [BackBuffer]
       cld
       rep stosd
   end asm
End Sub
'-------------------------------------------------------------------
' NAME : TTDPlot()
' PURPOSE : This function plots a pixel in RGB color
' INPUTS : x,y position and Red, Green,Blue values range 0-255.
' RETURNS : nothing.
'-------------------------------------------------------------------
Sub TTDPlot(ByVal x as integer, ByVal y as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
    Dim PlotColor as integer
    PlotColor = ( Red shl 16 ) + ( Green shl 8 ) + Blue
    if (x>-1) AND (x<XRES) AND (y>-1) AND (y<YRES) THEN
        Backbuffer[(y*XRES+x)] = PlotColor     
    end if
End Sub
'-------------------------------------------------------------------
' NAME : TTDLine()
' PURPOSE : This function draw a line of RGB and Has Alpha option
' INPUTS : x1,y1,x2,y2,r,g,b
' RETURNS : Nothing.
'-------------------------------------------------------------------
Sub TTDLine(ByVal x1 as double, ByVal y1 as double,ByVal x2 as double, ByVal y2 as double,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer,ByVal Alpha as single)
    Dim As Integer PlotColor,OldColor,TempColor, steps
    Dim As Integer r2,g2,b2, dy, dx
    Dim As Double xI,Alpha2
    PlotColor = ( Red shl 16 ) + ( Green shl 8 ) + Blue
    xI=0
    x2 = x2-x1
    y2 = y2-y1
    If Abs(x2)>Abs(y2) THEN
        steps = Abs(x2)
    Else
        steps = Abs(y2)
    Endif
    xI = x2 / steps
    y2 = Y2 / steps
    if Alpha>=1.0 then
        For x2 = 1 To steps
            dy=Int(y1)
            dx=Int(x1)
            If dy>-1 And dy<YRES then
                If dx>-1 And dx<XRES then
                    Backbuffer[(dy*XRES+dx)] = PlotColor 
                EndIf
            EndIf
            x1=x1+xI:y1=y1+y2
        Next
    else
        Alpha2=1.0 - Alpha
        For x2 = 1 To steps
            dy=Int(y1)
            dx=Int(x1)
            If dy>-1 And dy<YRES then
                If dx>-1 And dx<XRES then
                  OldColor=Backbuffer[(dy*XRES+dx)]
                  'do alphablend
                  r2 = ( ( OldColor Shr 16 ) And 255 ) * Alpha + ( ( PlotColor Shr 16 ) And 255 ) * Alpha2
g2 = ( ( OldColor Shr 8 ) And 255 ) * Alpha + ( ( PlotColor Shr 8 ) And 255 ) * Alpha2
b2 = ( OldColor And 255 ) * Alpha + ( PlotColor And 255 ) * Alpha2
If r2 > 255 Then r2 = 255
If g2 > 255 Then g2 = 255
If b2 > 255 Then b2 = 255
                   TempColor = ( r2 shl 16 ) + ( g2 shl 8 ) + b2
                  Backbuffer[(dy*XRES+dx)] = TempColor 
                EndIf
            EndIf
            x1=x1+xI:y1=y1+y2
        Next
    endif
end Sub
'-------------------------------------------------------------------
' NAME : TTDFastLine()
' PURPOSE : This function draw a line of RGB
' INPUTS : stx,sty,enx,endy,r,g,b
' RETURNS : Nothing.
'-------------------------------------------------------------------
Sub TTDFastLine(Byval stx as integer,Byval sty as integer,Byval enx as integer,Byval eny as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
Dim As Double stpx,stpy
dim As Integer mvx,mvy,mv
Dim PlotColor as integer
    PlotColor = ( Red shl 16 ) + ( Green shl 8 ) + Blue
mvx=Stx-enx
    mvy=sty-eny
If mvx<0 then
        mvx=-mvx
    endif
If mvy<0 then
        mvy=-mvy
    endif
If mvy>mvx then
        mv=mvy
    Else
        mv=mvx
    endif
stpx=(mvx/mv)
    If Stx>enx then
        stpx=-stpx
    endif
stpy=(mvy/mv)
    If Sty>eny then
        stpy=-stpy
    endif
For nc As Integer =0 To mv
        If sty>-1 And sty<YRES then
            If stx>-1 And stx<XRES then
                Backbuffer[(sty*XRES+stx)] = PlotColor 
            EndIf
        EndIf
stx=stx+stpx
        sty=sty+stpy
Next
End Sub
'-------------------------------------------------------------------
' NAME : TTDOval()
' PURPOSE : This function draw a Oval of RGB
' INPUTS : xcentre,ycentre=centre of oval Rx,Ry=size of oval r,g,b=color
' RETURNS : Nothing.
'-------------------------------------------------------------------
Sub TTDOval(ByVal xCenter as integer,ByVal yCenter as integer,ByVal Rx as integer,ByVal Ry as integer,ByVal Red as integer,ByVal Green as integer,ByVal Blue as integer)
    Dim As Integer p,px,py,x,y
    Dim As Integer Rx2,Ry2,twoRx2,twoRy2
    Dim pFloat as single
    ' The complex code I converted from a bmax example
    Rx2=Rx*Rx
    Ry2=Ry*Ry
    twoRx2=Rx2 Shl 1
    twoRy2=Ry2 Shl 1
    'Region 1
    x=0
    y=Ry
    TTDPlot(xCenter+x,yCenter+y,Red,Green,Blue)
    TTDPlot(xCenter-x,yCenter+y,Red,Green,Blue)
    TTDPlot(xCenter+x,yCenter-y,Red,Green,Blue)
    TTDPlot(xCenter-x,yCenter-y,Red,Green,Blue)
    pFloat=(Ry2-(Rx2*Ry))+(0.25*Rx2)
    p=Int(pFloat)
    If pFloat Mod 1.0>=0.5  Then
        p=p+1
    endif
    px=0
    py=twoRx2*y
    While px<py
        x=x+1
        px=px+twoRy2
        If p>=0 then
            y=y-1
            py=py-twoRx2
        EndIf
        If p<0 Then
            p=p+Ry2+px
        Else
            p=p+Ry2+px-py
        endif
        TTDPlot(xCenter+x,yCenter+y,Red,Green,Blue)
        TTDPlot(xCenter-x,yCenter+y,Red,Green,Blue)
        TTDPlot(xCenter+x,yCenter-y,Red,Green,Blue)
        TTDPlot(xCenter-x,yCenter-y,Red,Green,Blue)
    Wend
    'Region 2
    pFloat=(Ry2*(x+0.5)*(x+0.5))+(Rx2*(y-1.0)*(y-1.0))-(Rx2*(CSNG(Ry2)))
    p=Int(pFloat)
    If pFloat Mod 1.0>=0.5 Then
        p=p+1
    endif
    While y>0
        y=y-1
        py=py-twoRx2
        If p<=0 then
            x=x+1
            px=px+twoRy2
        EndIf
        If p>0 Then
            p=p+Rx2-py
        Else
            p=p+Rx2-py+px
        endif
        TTDPlot(xCenter+x,yCenter+y,Red,Green,Blue)
        TTDPlot(xCenter-x,yCenter+y,Red,Green,Blue)
        TTDPlot(xCenter+x,yCenter-y,Red,Green,Blue)
        TTDPlot(xCenter-x,yCenter-y,Red,Green,Blue)
    Wend
End Sub

test.bas:
Code: [Select]
#define PTC_WIN
#include "ttdblitzlib1.bi"
'-------------------------------------------------------------------
' Test of Lib
'-------------------------------------------------------------------
TTDGraphics(640,480,"TTD Test App")
Dim count as integer
    do 
        TTDFastCls(0,0,0)
        for i As Integer=0 to 255
        TTDLine(0,10,500,10+i,0,i,0,0.5)
        TTDLine(0,YRES,500,YRES-i,0,0,i,0.5)
        next
        TTDFlip()
       
    loop until inkey$<>""
    TTDEndGraphics()
end
'-------------------------------------------------------------------
' End Test of Lib
'-------------------------------------------------------------------


Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: TTDblitzlib for tinyptc
« Reply #3 on: March 21, 2009 »
@rdc.  Thanks for fixing it up for new version of FB.

@Shockwave,  Well this is just the basic lib I first made and shared with zawran back in 2006, the final version has alot more in it for handling triangles, images, fonts as well as filters to apply to buffers as well. The idea of releasing this version was to show a basic structure for a working lib and allow the people to add to it themselves. I dont really use FB so I am not looking to take this lib any further but if there's enough interest in it then I will help others take it further.

As a little example of what people could add themselves here's a triangle drawing function for it, the code is a conversion from a blitz2d example made by paranoid pete. Might need some changes for new fb like the 2 above.

stick this line with the other sub declares
Code: [Select]
declare sub TTDTriangle(ByVal x0 as integer,ByVal y0 as integer,ByVal x1 as integer,ByVal y1 as integer,ByVal x2 as integer,ByVal y2 as integer,ByVal r as integer,ByVal g as integer,ByVal b as integer)

then add this to the end of the lib.

Code: [Select]
'-------------------------------------------------------------------
' NAME : TTDTriangle()
' PURPOSE : This function draw a triangle filled of RGB
' INPUTS : x0,y0,x1,y1,x2,y2 position r,g,b=color
' RETURNS : Nothing.
'-------------------------------------------------------------------
Sub TTDTriangle(ByVal x0 as integer,ByVal y0 as integer,ByVal x1 as integer,ByVal y1 as integer,ByVal x2 as integer,ByVal y2 as integer,ByVal r as integer,ByVal g as integer,ByVal b as integer)
' Original code by Paranoid Pete
Dim temp,Leftdx,Leftdy,Rightdx,Rightdy,Leftx,Rightx,lx,rx,Leftm,Rightm as integer
    Dim PlotColor as integer
    PlotColor = ( g shl 16 ) + ( g shl 8 ) + b
    If y0>y1 then
temp=x1 : x1=x0 : x0=temp
temp=y1 : y1=y0 : y0=temp
EndIf
If y1>y2 then
temp=x2 : x2=x1 : x1=temp
temp=y2 : y2=y1 : y1=temp
EndIf
If y0>y1 then
temp=x1 : x1=x0 : x0=temp
temp=y1 : y1=y0 : y0=temp
EndIf
Leftdx=x1-x0  : Leftdy=y1-y0
Rightdx=x2-x0 : Rightdy=y2-y0
If Leftdy<>0.00 then Leftm=(Leftdx Shl 8)/Leftdy
If Rightdy<>0.00 then Rightm=(Rightdx Shl 8)/Rightdy
Leftx=x0 Shl 8 : Rightx=x0 Shl 8
For y=y0 To y1-1
If rightx<leftx then
lx=rightx Shr 8
rx=leftx  Shr 8
Else
lx=Leftx  Shr 8
rx=rightx Shr 8
EndIf
For x=lx To rx
'WritePixelFast x,y,rgb
            if (x>-1) AND (x<XRES) AND (y>-1) AND (y<YRES) THEN
                Backbuffer[(y*XRES+x)] = PlotColor     
            end if
Next
Leftx=Leftx+Leftm
Rightx=Rightx+Rightm
Next
Leftdx=x2-x1  : Leftdy=y2-y1
If leftdy<>0.00 then Leftm=(Leftdx Shl 8)/Leftdy
leftx=x1 Shl 8
For y=y1 To y2
If rightx<leftx then
lx=rightx Shr 8 : rx=leftx Shr 8
Else
lx=leftx Shr 8 : rx=rightx Shr 8
EndIf
For x=lx To rx
'WritePixelFast x,y,rgb
            if (x>-1) AND (x<XRES) AND (y>-1) AND (y<YRES) THEN
                Backbuffer[(y*XRES+x)] = PlotColor     
            end if
Next
leftx=leftx+leftm
rightx=rightx+rightm
Next
End Sub

Lots more of course can be done but that is part of the learning experience when coding :)

Cheers
Jon

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: TTDblitzlib for tinyptc
« Reply #4 on: March 21, 2009 »
You'll be better off chopping out the triangle routine in my own lib as all the filled draw routines are optimised with asm, my lib has plots, lines, beziers, filled triangles, filled circles, basic text output included, I released it in full a couple of years ago.

Like I said, if you want to chop mine up and make it more comprehensive (to include your ovals for example) then feel free, the only provisor is that it gets shared with the whole community so that it can help everyone.

I'd be interested in working with someone else to expand the library to include other stuff like bobs, collisions, gourad triangles and affine mapping too.
The lib would be truely useful then and give a great step into coding for new people.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: TTDblitzlib for tinyptc
« Reply #5 on: March 21, 2009 »
K++
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: