2D drawing slow as hell

Advanced game related topics
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: 2D drawing slow as hell

Post by AndyMK »

Write to the "windowoutput" rather than screenoutput. The first example gives me 305fps on a 2.4ghz Core2Duo using the onboard intel G31 vga. The code below gives me 850fps but the text has a slight flicker.

Code: Select all

#screenwidth = 640
#screenheight = 480

InitSprite()
OpenWindow(0, 0, 0, #screenwidth, #screenheight, "PB Drawing", #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, #screenwidth, #screenheight, 0, 0, 0, #PB_Screen_NoSynchronization)

fpscounter.i = 0
timer.i = 0
fps.i = 0
frames.i = 0
totalTime.l = 0
curTime.l = ElapsedMilliseconds()

Repeat
  
  lastTime = curTime
  curTime = ElapsedMilliseconds()
  totalTime + (curTime-lastTime)
  If(totalTime>1000)
    totalTime-1000
    fps = frames
    frames = 0
  EndIf
  frames + 1
    
  StartDrawing(WindowOutput(0))
    For i = 0 To 20
      Box(Random(320), Random(240), Random(320), Random(240), Random($FFFFFF) )
    Next i
    DrawText(20, 430, Str(fps))
  StopDrawing()
    
  FlipBuffers()
    
Until WindowEvent() = #PB_Event_CloseWindow
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: 2D drawing slow as hell

Post by AndyMK »

adding

Code: Select all

timeBeginPeriod_(1)
at the beginning of the code gives me an extra 10 fps but on faster systems this will be higher.
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: 2D drawing slow as hell

Post by AndyMK »

Using both cpu cores i get 980fps but not sure if it is done right.

Code: Select all

#screenwidth = 640
#screenheight = 480

Global sem1 = CreateSemaphore(), sem2 = CreateSemaphore(), sem3 = CreateSemaphore(), sem4 = CreateSemaphore(), fps

Procedure Draw1(val)
  Repeat
    WaitSemaphore(sem1)
    StartDrawing(WindowOutput(0))
      For i = 0 To 20
        Box(Random(320), Random(240), Random(320), Random(240), Random($FFFFFF) )
      Next i
      DrawText(20, 430, Str(fps))
    StopDrawing()
    
    SignalSemaphore(sem2)
  ForEver
EndProcedure

Procedure Draw2(val)
  Repeat
    WaitSemaphore(sem3)
    StartDrawing(WindowOutput(0))
      For i = 0 To 20
        Box(Random(320), Random(240), Random(320), Random(240), Random($FFFFFF) )
      Next i
    StopDrawing()
    
    SignalSemaphore(sem4)
  ForEver
EndProcedure

InitSprite()
OpenWindow(0, 0, 0, #screenwidth, #screenheight, "PB Drawing", #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, #screenwidth, #screenheight, 0, 0, 0, #PB_Screen_NoSynchronization)

fpscounter.i = 0
timer.i = 0
fps.i = 0
frames.i = 0
totalTime.l = 0
curTime.l = ElapsedMilliseconds()

CreateThread(@draw1(), 0)
CreateThread(@draw2(), 0)

Repeat
  lastTime = curTime
  curTime = ElapsedMilliseconds()
  totalTime + (curTime-lastTime)
  If(totalTime>1000)
    totalTime-1000
    fps = frames*2
    frames = 0
  EndIf
  frames + 1
  
  SignalSemaphore(sem1)
  SignalSemaphore(sem3)
  WaitSemaphore(sem2)
  WaitSemaphore(sem4)
  
  FlipBuffers()
Until WindowEvent() = #PB_Event_CloseWindow
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: 2D drawing slow as hell

Post by Swos2009 »

I hate StartDrawing() and StopDrawing() as they are really Horrible !!!

It would be nice to using 2D Drawings Commands without the horrible StartDrawing() and StopDrawing() !!! :x
User avatar
kenmo
Addict
Addict
Posts: 2069
Joined: Tue Dec 23, 2003 3:54 am

Re: 2D drawing slow as hell

Post by kenmo »

Swos2009 wrote:I hate StartDrawing() and StopDrawing() as they are really Horrible !!!

It would be nice to using 2D Drawings Commands without the horrible StartDrawing() and StopDrawing() !!! :x
Eh? What makes them horrible or hate-worthy?
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: 2D drawing slow as hell

Post by Swos2009 »

I hate StartDrawing() and StopDrawing() as they are really Horrible !!!

It would be nice to using 2D Drawings Commands without the horrible StartDrawing() and StopDrawing() !!!

Eh? What makes them horrible or hate-worthy?
I just dont like using them as why cant do coding like this

Code: Select all

If   InitSprite() = 0 Or InitKeyboard() = 0
Else
window=OpenWindow(0, 0, 0, 800, 600, "ATX Remake",#PB_Window_BorderLess | #PB_Window_ScreenCentered )
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 728, #True, 0, 0)   
EndIf 

Repeat
          ClearScreen(0)

         For lines = 0 To 1000
         ; choose random colors
         r = Random(255)
         g = Random(255)
         b = Random(255)       
         ; draw out the line in the selected color, at random places and sizes
         Line(Random(639),Random(449),Random(150),Random(150),RGB(r,g,b))
     Next     
     DrawText(0,460,"Press any key to exit...")
   Else
     MessageRequester("Error!", "Unable to Draw to ScreenOutput()", #PB_MessageRequester_Ok)
     End
   EndIf

   ; show the output to the users
   FlipBuffers()
   
   ; see if the user has pressed a key
   ExamineKeyboard()
   Delay(1)
Until KeyboardPushed(#PB_Key_All)

; end the program
End
To make things less hassle than using StartDrawing() and StopDrawing() all the time when come to drawing Graphics as Freebasic, Blitzplus, BlitzMax and Darkbasic pro dont used those Daft Commands just to draw the graphics!!

Why couldnt Purebasic be simple to just draw the graphics without needless StartDrawing() and StopDrawing() ! sigh! :(
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 »

StartDrawing() and StopDrawing() are necessary because the OS needs time to prepare the graphics context for drawing. :)
When using plain WinAPI, you also have to use BeginPaint_() and EndPaint_() which are more or less the same as StartDrawing() and StopDrawing().

Of course they can be "hidden" by creating another abstraction layer, but that doesn't make too much sense actually.
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 »

would be simple to make framework without using StartDrawing() and StopDrawing() :?:

Yes? :D
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 »

Yeah well but what's the point of it?
Somewhere it has to be called...and PB also has to know where you wish to be drawing to. :)

Why do you hate StartDrawing() and StopDrawing() so much anyway? :)
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 »

Why do you hate StartDrawing() and StopDrawing() so much anyway?
IT SUCK....No offonce

Every Programming Language had own Pros(Strength) and Cons(Weakness) :)
User avatar
kenmo
Addict
Addict
Posts: 2069
Joined: Tue Dec 23, 2003 3:54 am

Re: 2D drawing slow as hell

Post by kenmo »

I don't know the other languages you mentioned very well... but in those cases, when you call drawing commands with no sort of context, aren't you ONLY drawing to the screen?

StartDrawing() lets you specify what you want to draw to... a screen, an image, a window, a gadget, etc.

It also gives you more control, you only need to open a drawing environment when you want to update one of the outputs I just listed.

Other languages might hide this, but perhaps they are repeatedly requesting a drawing context for each draw function (less efficient? maybe?).

Anyway, I don't mind adding two tiny commands to a big render procedure. No harm at all. Doesn't suck. 8)
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: 2D drawing slow as hell

Post by Swos2009 »

OK :) Everyone is Different I guess :wink:

The reason why I like Purebasic is when come to Executable Files size as it is so Small and I like that! Freebasic come Close to it as well :)

When I was doing the Twister Demo on 3 Different Programming language when I was converting BlitzMax to Freebasic then Purebasic(I gave up because I was so annyoned on StartDrawing() and StopDrawing() )

So here your chance to see if you can Convert the BlitzMax Twister Demo to Purebasic :mrgreen:

Code: Select all

Const xRes = 800
Const yRes = 600

Const Pos1=175
Const Pos2=375

Global a#,x1#,x2#,x3#,x4#
Global ang#=0,amp#=7

HideMouse

Type Twister
     Field X1#,X2#,X3#,X4#

	Function twister()
		For a=1 To 600 Step 2
			x1=((Sin((a/amp)+ang))*100)+300
			x2=((Sin((a/amp)+ang+90))*100)+300
			x3=((Sin((a/amp)+ang+90*2))*100)+300
			x4=((Sin((a/amp)+ang+90*3))*100)+300
			
			SetColor 255,0,255
			If x1<x2
			   DrawLine x1-Pos1,a,x2-Pos1,a
  			   DrawLine x1+Pos2,a,x2+Pos2,a
			End If	
		
			SetColor 0,0,255
			If x2<x3
			   DrawLine x2-Pos1,a,x3-Pos1,a
			   DrawLine x2+Pos2,a,x3+Pos2,a
   			End If	
		
			SetColor 0,255,0
			If x3<x4
			   DrawLine x3-Pos1,a,x4-Pos1,a

			   DrawLine x3+Pos2,a,x4+Pos2,a
			End If
		
			SetColor 255,255,0
			If x4<x1
			   DrawLine x4-Pos1,a,x1-Pos1,a

			   DrawLine x4+Pos2,a,x1+Pos2,a
			End If						
		Next
		ang=ang+2
		If ang=360 Then ang=0
	End Function	
End Type

SetGraphicsDriver GLMax2DDriver()
			
Graphics xRes,yRes,32,60,GRAPHICS_BACKBUFFER
Global _Draw:Twister=New Twister

Repeat	
      _Draw.twister()		
	  Flip
	  Cls
	  Delay 20
Until KeyDown(KEY_ESCAPE)
End
and to Prove to me that StartDrawing and StopDrawing() isnt SUCK as they seem to be :D
Nituvious
Addict
Addict
Posts: 1030
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: 2D drawing slow as hell

Post by Nituvious »

Swos2009 wrote: ...
Hi, here is the conversion. I have never used blitzmax, though so there may be some mistakes here.

Code: Select all

#xRes = 800
#yRes = 600

#Pos1=175
#Pos2=375

Global a.l,x1.l,x2.l,x3.l,x4.l
Global ang.l=0,amp.l=7

;HideMouse

;Type Twister
 ;    Field X1.l,X2.l,X3.l,X4.l

Procedure twister()
      For a.l=1 To 600 Step 2
         x1.l=((Sin((a.l/amp.l)+ang.l))*100)+300
         x2.l=((Sin((a.l/amp.l)+ang.l+90))*100)+300
         x3.l=((Sin((a.l/amp.l)+ang.l+90*2))*100)+300
         x4.l=((Sin((a.l/amp.l)+ang.l+90*3))*100)+300
         
         ;SetColor 255,0,255
         If x1<x2
            Line(x1-Pos1,a,x2-Pos1,a, RGB(255,0,255))
            Line(x1+Pos2,a,x2+Pos2,a, RGB(255, 0, 255))
         EndIf   
      
         ;SetColor 0,0,255
         If x2<x3
            Line(x2-Pos1,a,x3-Pos1,a, RGB(0, 0, 255))
            Line(x2+Pos2,a,x3+Pos2,a, RGB(0, 0, 255))
          EndIf   
      
         ;SetColor 0,255,0
         If x3<x4
            Line(x3-Pos1,a,x4-Pos1,a, RGB(0, 255, 0))
            Line(x3+Pos2,a,x4+Pos2,a, RGB(0, 255, 0))
         EndIf
      
         ;SetColor 255,255,0
         If x4<x1
            Line(x4-Pos1,a,x1-Pos1,a, RGB(255, 255, 0))
            Line(x4+Pos2,a,x1+Pos2,a, RGB(255, 255, 0))
         EndIf                  
      Next
      ang=ang+2
      If ang=360
      	ang=0
      EndIf
 EndProcedure  
 
 If InitSprite()
 	If Not InitKeyboard()	
 		MessageRequester("Error","There was an error @ line 53")
 	EndIf
 Else
 	MessageRequester("Error", "There was an error @ line 52")
 EndIf
 	
 	
         
window = OpenWindow(#PB_Any, 0, 0, #xRes, #yRes, "Twister", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(window), 0, 0, #xRes, #yRes, 1, 0, 0)
Repeat   
	ExamineKeyboard()
	ClearScreen(RGB(0, 0, 0))
	StartDrawing(ScreenOutput())
	twister()
	StopDrawing()
	FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
▓▓▓▓▓▒▒▒▒▒░░░░░
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 »

Just a very quick hack, I'd never code like this but since it's only FFS. :mrgreen:
Still don't know where the problem is to call StartDrawing() and StopDrawing().

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 1 ; 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
			LineXY(*twister\x1-#Pos1,a,*twister\x2-#Pos1,a,$FF00FF)
			LineXY(*twister\x1+#Pos2,a,*twister\x2+#Pos2,a,$FF00FF)
		EndIf
		
		
		If *twister\x2<*twister\x3
			LineXY(*twister\x2-#Pos1,a,*twister\x3-#Pos1,a,$0000FF)
			LineXY(*twister\x2+#Pos2,a,*twister\x3+#Pos2,a,$0000FF)
		EndIf
		
		If *twister\x3<*twister\x4
			LineXY(*twister\x3-#Pos1,a,*twister\x4-#Pos1,a,$00FF00)
			LineXY(*twister\x3+#Pos2,a,*twister\x4+#Pos2,a,$00FF00)
		EndIf
		
		If *twister\x4<*twister\x1
			LineXY(*twister\x4-#Pos1,a,*twister\x1-#Pos1,a,$FFFF00)
			LineXY(*twister\x4+#Pos2,a,*twister\x1+#Pos2,a,$FFFF00)
		EndIf
	Next
	*twister\ang=*twister\ang+0.5
	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, 1, 1, 1)


Define twister.Twister
twister\amp = 7

SetFrameRate(30)
Repeat
	WindowEvent()
	ExamineKeyboard()
	
	StartDrawing(ScreenOutput())
	DrawTwister(twister)  
	StopDrawing()
	
	FlipBuffers()
	ClearScreen(0)
Until KeyboardPushed(#PB_Key_Escape)
End
Last edited by Shield on Mon Oct 10, 2011 6:02 pm, edited 1 time in total.
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 »

It really interesting to see that as you both guys used StartDrawing and StopDrawing just Once.........Very Good

The Executable Files Size is Impressive when come to Compress

Purebasic 19K
BlitzMax 59k
BlitzPlus 315k
Freebasic 45K

Here the Executable of Twister Demo(BlitzMax Version) that look like :)
http://www.mediafire.com/?a0112yah69hhdjc
Post Reply