2D drawing slow as hell

Advanced game related topics
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: 2D drawing slow as hell

Post 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).
Last edited by Demivec on Mon Oct 10, 2011 6:44 pm, edited 1 time in total.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: 2D drawing slow as hell

Post by Shield »

Didn't take into account that Sin uses radians in PB...updated my code accordingly. :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: 2D drawing slow as hell

Post 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!) :)
Last edited by Swos2009 on Mon Oct 10, 2011 6:30 pm, edited 1 time in total.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: 2D drawing slow as hell

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

Re: 2D drawing slow as hell

Post 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) :|
pjay
Enthusiast
Enthusiast
Posts: 253
Joined: Thu Mar 30, 2006 11:14 am

Re: 2D drawing slow as hell

Post 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.
pjay
Enthusiast
Enthusiast
Posts: 253
Joined: Thu Mar 30, 2006 11:14 am

Re: 2D drawing slow as hell

Post by pjay »

Or rather, it *should* be fine :lol:
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: 2D drawing slow as hell

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

Re: 2D drawing slow as hell

Post by Swos2009 »

Thanks for Fixed pjay :wink:

I will Carry on Next Step of putting somethings in the Middle 8)
neumanix
User
User
Posts: 11
Joined: Thu Aug 13, 2009 3:13 pm
Location: Finland

Re: 2D drawing slow as hell

Post by neumanix »

Swos2009, with Kkrunchy I got the PB version down to 16k :)
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: 2D drawing slow as hell

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

Re: 2D drawing slow as hell

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

Re: 2D drawing slow as hell

Post 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 :?:
dige
Addict
Addict
Posts: 1410
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: 2D drawing slow as hell

Post 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
"Daddy, I'll run faster, then it is not so far..."
Post Reply