Re: 2D drawing slow as hell
Posted: Tue Oct 25, 2011 3:03 pm
There are several problems with your code.Swos2009 wrote:thanks Danilo
I got the Demo Nearly done but I got one problem which is this
I am trying do Scrolling Text that go cross the screen and if text reached end of the screen....do I measure the strings by using len commands?
here the code of it
Could you tell me why the text is causing the problemCode: Select all
...
1.) dige already said the thing with ClipSprite.
2.) Do NOT use Sprite drawing within a StartDrawing/StopDrawing
3.)
For a very long text you would need a long Sprite, like CreateSprite(2,5000,100).
But you want it only 600 in length. So display the Sprite at the same X position
and scroll the text by using different X offsets for DrawText().
You can measure string pixel length with TextWidth().
Something like this:
Code: Select all
; Specail thanks to Demivec and Danilo
#xRes = 800
#yRes = 600
#Pos1 = 175
#Pos2 = 375
#MODULE_FILE = 1
Structure Twister
ang.f
amp.f
x1.f
x2.f
x3.f
x4.f
EndStructure
Procedure DrawTwister(*twister.Twister)
Protected a.i
For a = 1 To 600 Step 2
*twister\x1 = ((Sin(Radian((a/*twister\amp) + *twister\ang))) * 100) + 300
*twister\x2 = ((Sin(Radian((a/*twister\amp) + *twister\ang + 90))) * 100) + 300
*twister\x3 = ((Sin(Radian((a/*twister\amp) + *twister\ang + 90 * 2))) * 100) + 300
*twister\x4 = ((Sin(Radian((a/*twister\amp) + *twister\ang + 90 * 3))) * 100) + 300
If *twister\x1 < *twister\x2
FrontColor(RGB(255, 0, 255))
LineXY(*twister\x1 - #Pos1, a, *twister\x2 - #Pos1, a)
LineXY(*twister\x1 + #Pos2, a, *twister\x2 + #Pos2, a)
EndIf
If *twister\x2 < *twister\x3
FrontColor(RGB(0, 0, 255))
LineXY(*twister\x2 - #Pos1, a, *twister\x3 - #Pos1, a)
LineXY(*twister\x2 + #Pos2, a, *twister\x3 + #Pos2, a)
EndIf
If *twister\x3 < *twister\x4
FrontColor(RGB(0, 255, 0))
LineXY(*twister\x3 - #Pos1, a, *twister\x4 - #Pos1, a)
LineXY(*twister\x3 + #Pos2, a, *twister\x4 + #Pos2, a)
EndIf
If *twister\x4 < *twister\x1
FrontColor(RGB(255, 255, 0))
LineXY(*twister\x4 - #Pos1, a, *twister\x1 - #Pos1, a)
LineXY(*twister\x4 + #Pos2, a, *twister\x1 + #Pos2, a)
EndIf
Next
*twister\ang + 2
If *twister\ang >= 360
*twister\ang = 0
EndIf
EndProcedure
InitSprite()
InitKeyboard()
OpenWindow(0, 0, 0, #xRes, #yRes, "Twister", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #xRes, #yRes, 0, 1, 1)
Define twister.Twister
twister\amp = 7
Global hFont = LoadFont(1,"Arial",72)
CreateSprite(1,600,100)
StartDrawing(SpriteOutput(1))
DrawingMode(#PB_2DDrawing_Transparent|#PB_2DDrawing_Outlined)
DrawingFont(hFont)
FrontColor(RGB($00,$44,$44))
DrawText(0,0,"PUREBASIC!")
Box(0,0,600,100)
StopDrawing()
#SCROLLTEXT = "Hiya Everyone, Welcome to PUREBASIC DEMO!, Picking the Music for this Demo is'nt easy for me hence why it is in the project Forum to see people feedback on what they think of it. I am really enjoy on what I do with it and hopefully build it from there over time :)........LET ROCK!!!"
CreateSprite(2,600,100)
Procedure DrawScroller()
Static X=800 ; startvalue for scroller
If StartDrawing(SpriteOutput(2))
Box(0,0,600,100,RGBA(0,0,0,0)) ; transparent clear
DrawingMode(#PB_2DDrawing_Transparent|#PB_2DDrawing_Outlined)
DrawingFont(hFont)
FrontColor(RGB($00,$44,$44))
; I am not sure on how to measure the Strings of Scrolling Text! as if reached end of the screen then repeat the scrolling :)
length = TextWidth(#SCROLLTEXT)
If X <= -length
X = 800
Else
X-3
EndIf
DrawText(X,0,#SCROLLTEXT)
;Box(0,0,600,100)
StopDrawing()
EndIf
EndProcedure
factor.f = 0.01
If InitSound()
If LoadModule(#MODULE_FILE, "Music\thelastv.mod")
PlayModule(#MODULE_FILE)
EndIf
EndIf
SetFrameRate(60)
Repeat
Repeat:Until WindowEvent() = 0
ExamineKeyboard()
If StartDrawing(ScreenOutput())
DrawTwister(twister)
StopDrawing()
EndIf
y.f + 0.05
If y > 2*#PI : y = 0 : EndIf
DrawScroller()
For a = 0 To 599
ClipSprite(1,a,0,1,100)
DisplayTransparentSprite(1,100+a,100+Sin(y+factor*a)*30)
ClipSprite(2,a,0,1,100)
DisplayTransparentSprite(2,100+a,400+Sin(y+factor*a)*70)
Next a
FlipBuffers()
ClearScreen(0)
Until KeyboardPushed(#PB_Key_Escape)