2D drawing slow as hell

Advanced game related topics
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: 2D drawing slow as hell

Post by Danilo »

Swos2009 wrote:thanks Danilo :)

I got the Demo Nearly done but I got one problem which is this

Image

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

Code: Select all

...
Could you tell me why the text is causing the problem :?:
There are several problems with your code.

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)
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: 2D drawing slow as hell

Post by Swos2009 »

Thanks dige and Danilo :mrgreen:
1.) dige already said the thing with ClipSprite.
I have forgot put that in but it was nice that Dige point out the error :)
2.) Do NOT use Sprite drawing within a StartDrawing/StopDrawing
It is because it would cause Slowdown?
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().
Ahhhh TextWidth() and I have notice the improvement on different between using Sprite Drawing on Text and DrawText that you have made Danilo :)

Now I need find Suitable MOD Music that goes with the Demo :wink: 8)

P.S. Should I add somethings interesting Background or should I leave it as it is?
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: 2D drawing slow as hell

Post by Danilo »

Swos2009 wrote:
2.) Do NOT use Sprite drawing within a StartDrawing/StopDrawing
It is because it would cause Slowdown?
StartDrawing() locks something (a device context) and
StopDrawing() unlocks it.
You should not draw Sprites while the device context is locked.
It works *sometimes*. Your last code just stops repainting here
after 1 or 2 seconds when drawing the sprites within the
StartDrawing()/StopDrawing().
Swos2009 wrote:P.S. Should I add somethings interesting Background or should I leave it as it is?
Up to you. ;)
Maybe add some more FX or parts to the demo.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: 2D drawing slow as hell

Post by Danilo »

Simple scroller (HERE) updated for PB4.51:

Code: Select all

;
; by Danilo, original version 16.04.2005
;
; updated for PB4.51 on 25.Oct.2011
;
If InitSprite()=0 Or InitKeyboard()=0
  MessageRequester("ERROR","Cant initialize screen !",#MB_ICONERROR):End
EndIf

ExamineDesktops()

w  = DesktopWidth(0)
h  = DesktopHeight(0)
#sn = "simpel"

;#PI      = 3.141592
#factor  = 0.015
x_offset = w + 100

Dim _sin.f(33)

If OpenScreen(w,h,32,#sn)=0
If OpenScreen(w,h,24,#sn)=0
  If OpenScreen(w,h,16,#sn)=0
   If OpenScreen(w,h,08,#sn)=0
     MessageRequester("ERROR","Cant open screen !",#MB_ICONERROR):End
EndIf:EndIf:EndIf:EndIf

  SetFrameRate(60)

  For a = 0 To 31
    If CreateSprite(a,w,8)=0
      CloseScreen():MessageRequester("ERROR","Cant init sprites !",#MB_ICONERROR):End
    EndIf
    StartDrawing(SpriteOutput(a))
      For i = 1 To 4
        Line(0,i-1,w,1,RGB(a*i*2,a*i*2,0))
        Line(0,8-i,w,1,RGB(a*i*2,a*i*2,0))
      Next i
    StopDrawing()
  Next a

  If CreateSprite(32,w,h/8)=0 Or CreateSprite(33,450,h/10)=0
    CloseScreen()
    MessageRequester("ERROR","Cant init sprites !",#MB_ICONERROR):End
  EndIf
  StartDrawing(SpriteOutput(32))
    Box(0,0,w,h/8,RGB($FF,$FF,$FF))
  StopDrawing()

  hFont = LoadFont(1,"Arial",h/20)
  StartDrawing(SpriteOutput(33))
    DrawingMode(1)
    DrawingFont(hFont)
    FrontColor(RGB($00,$00,$FF))
    DrawText(0,0,"PB forever!")
  StopDrawing()

  time = ElapsedMilliseconds()+3000
  While ElapsedMilliseconds()<time
    ExamineKeyboard()
    FlipBuffers()
    If IsScreenActive()
      ClearScreen(RGB(0,0,0))

      If StartDrawing(ScreenOutput())
        For a = 0 To h-1 Step 4
          x=0
          While x<w
            length = Random(w/8)
            r=Random($FF):g=Random($FF):b=Random($FF)
            Line(x,a  ,w/8,1,RGB(r,g,b))
            Line(x,a+1,w/8,1,RGB(r,g,b))
            x+length
          Wend
        Next a
        StopDrawing()
        Delay(Random(100))
      EndIf
     
    EndIf
  Wend

  oldtime = ElapsedMilliseconds()+10

  Repeat
    ExamineKeyboard()
    ;While oldtime > ElapsedMilliseconds():Delay(1):Wend
    ;oldtime = ElapsedMilliseconds()+10

    FlipBuffers()
    If IsScreenActive()
      ClearScreen(RGB(0,0,0))

      DisplaySprite(32,0,0)
      DisplaySprite(32,0,h-h/8)

      sin.f + 0.02
      For a = 0 To 31
        _sin(a) = Sin(sin + a*0.15)
        DisplaySprite(a,0,h/2+_sin(a)*h/4)
      Next a

      y.f + 0.05 : If y > 2*#PI : y = 0 : EndIf

      x_offset - 2 : If x_offset < -500 : x_offset = w+100 : EndIf

      x = x_offset
      For a = 0 To 449;599
        ClipSprite(33,a,0,1,SpriteHeight(33))
        DisplayTransparentSprite(33,x,h/2+Sin(y+#factor*a)*h/16)
        x+1
      Next a

    EndIf
  Until KeyboardPushed(#PB_Key_Escape)
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: 2D drawing slow as hell

Post by Swos2009 »

StartDrawing() locks something (a device context) and
StopDrawing() unlocks it.
You should not draw Sprites while the device context is locked.
It works *sometimes*. Your last code just stops repainting here
after 1 or 2 seconds when drawing the sprites within the
StartDrawing()/StopDrawing().
I see :)
Up to you.
Maybe add some more FX or parts to the demo.
I think I should stick to the Twister with Scrolling Sin wave Text as they look great :)

I have done some Demo in the past such as 3D Cube, Gadients, Plasma, RasterBar(which what you have done :wink: ) in BlitzBasic before and I think it is time for me Convect to Purebasic !!!

Your Demo code of RasterBar is worth learning from :)

Does anyone have Good MOD Music go with the Demo:?:
Post Reply