Page 4 of 5

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 5:31 pm
by Demivec
Here's my version :) :

Code: Select all

EnableExplicit

#xRes = 800
#yRes = 600

#Pos1 = 175
#Pos2 = 375

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

SetFrameRate(30)
Repeat
  Repeat:Until WindowEvent() = 0
  ExamineKeyboard()
  
  StartDrawing(ScreenOutput())
    DrawTwister(twister)  
  StopDrawing()
  
  FlipBuffers()
  ClearScreen(0)
Until KeyboardPushed(#PB_Key_Escape)
@Edit: Corrected code to use the correct step value of 2 and set the Autostretch of the windowed screen to 0 to prevent scaling (according to pjay's hint).

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 6:03 pm
by Shield
Didn't take into account that Sin uses radians in PB...updated my code accordingly. :)

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 6:16 pm
by Swos2009
Thanks Demivec as it`s interesting to learn from it and one things that I got Confuse :? over weather to used Degree or radians(I didnt know Purebasic used Radians instead!) :)

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 6:25 pm
by Demivec
Shield wrote:Didn't take into account that Sin uses radians in PB...updated my code accordingly. :)
@Shield: What is going on when only every other scanline is drawn? I couldn't remove the artifacts.

Here's a view of my results:
Image

Is there something I've overlooked or is this a bug?

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 6:29 pm
by Swos2009
Is there something I've overlooked or is this a bug?
It`s look like a Bug(I have check on Step 2 and 3) :|

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 6:30 pm
by pjay
It's not a bug as far as I can see, you have scaling on your windowed screen, change the scaling to '0' and it will be fine.

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 6:32 pm
by pjay
Or rather, it *should* be fine :lol:

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 6:42 pm
by Demivec
pjay wrote:Or rather, it *should* be fine :lol:
@pjay: Thanks, that did the trick. It looked to me like some kind of scaling but I didn't catch where it snuck in. Thanks for the hint. :wink:


Code corrected in former sample.

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 6:46 pm
by Swos2009
Thanks for Fixed pjay :wink:

I will Carry on Next Step of putting somethings in the Middle 8)

Re: 2D drawing slow as hell

Posted: Mon Oct 10, 2011 11:58 pm
by neumanix
Swos2009, with Kkrunchy I got the PB version down to 16k :)

Re: 2D drawing slow as hell

Posted: Tue Oct 11, 2011 2:33 am
by kenmo
Swos2009 wrote:It really interesting to see that as you both guys used StartDrawing and StopDrawing just Once.........Very Good
Ohhhh were you calling Start/StopDrawing for every drawing command? Well yes, that would totally suck! :shock:

Re: 2D drawing slow as hell

Posted: Tue Oct 11, 2011 10:09 pm
by Swos2009
Ohhhh were you calling Start/StopDrawing for every drawing command? Well yes, that would totally suck!
I couldnt agree more :D

How do You do Image Waving Check Flag in Cos and sin?

It would be interesting to know how to do it....

Re: 2D drawing slow as hell

Posted: Wed Oct 12, 2011 7:10 am
by Danilo
Swos2009 wrote:How do You do Image Waving Check Flag in Cos and sin?

It would be interesting to know how to do it....
Waving a flag can be done with ClipSprite. Cant find the code for it anymore,
so i modified another code from 2004:

Code: Select all

InitSprite():InitKeyboard()

ExamineDesktops()

OpenScreen(DesktopWidth(0),DesktopHeight(0),32,"")

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,"Hello World!")
  Box(0,0,600,100)
StopDrawing()

factor.f = 0.01

Repeat
  ExamineKeyboard()
  FlipBuffers()
  ClearScreen(RGB($FF,$88,$33))
  
  y.f + 0.05
  If y > 2*#PI : y = 0 : EndIf

  For a = 0 To 100
    ClipSprite(1,0,a,600,1)
    DisplayTransparentSprite(1,100+Sin(y+factor*a)*70,300+a)
  Next a

  For a = 0 To 599
    ClipSprite(1,a,0,1,100)
    DisplayTransparentSprite(1,100+a,100+Sin(y+factor*a)*70)
  Next a
Until KeyboardPushed(#PB_Key_Escape)
The lesson for you is to make it look nice... ;)


EDIT:
Found my codes for waving a flag in the german forum: LINK
All from 2005, so you have to change it to work with newer PureBasic -
but it should show you what you want to know if you read the sources.

Re: 2D drawing slow as hell

Posted: Mon Oct 24, 2011 11:44 pm
by Swos2009
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

; 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

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()

CreateSprite(2,600,100)
StartDrawing(SpriteOutput(2))
  DrawingMode(#PB_2DDrawing_Transparent|#PB_2DDrawing_Outlined)
  DrawingFont(hFont)
  FrontColor(RGB($00,$44,$44))
  DrawText(0,0,"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!!!")  
  Box(0,0,600,100)
StopDrawing()


  
factor.f = 0.01
X=800

If InitSound()
		LoadModule(#MODULE_FILE, "Music\thelastv.mod")
		PlayModule(#MODULE_FILE)
EndIf 
  
SetFrameRate(30)
Repeat
  Repeat:Until WindowEvent() = 0
  ExamineKeyboard() 
  
  StartDrawing(ScreenOutput())
       DrawTwister(twister)  
       y.f + 0.05
       If y > 2*#PI : y = 0 : EndIf

       For a = 0 To 599
           ClipSprite(1,a,0,1,100)
           DisplayTransparentSprite(1,100+a,100+Sin(y+factor*a)*70)
           
           DisplayTransparentSprite(2,100+a+x,400+Sin(y+factor*a)*70)           
       Next a
       
       X=X-3
      ; I am not sure on how to measure the Strings of Scrolling Text! as if reached end of the screen then repeat the scrolling :)
       
  StopDrawing()
  
  FlipBuffers()
  ClearScreen(0)
Until KeyboardPushed(#PB_Key_Escape)
Could you tell me why the text is causing the problem :?:

Re: 2D drawing slow as hell

Posted: Tue Oct 25, 2011 6:57 am
by dige

Code: Select all

    For a = 0 To 599
      ClipSprite(1,a,0,1,100)
      DisplayTransparentSprite(1,100+a,100+Sin(y+factor*a)*70)
      ClipSprite(2,a,0,1,100)
      DisplayTransparentSprite(2,100+a+x,400+Sin(y+factor*a)*70)
    Next a