Help with converted code

Just starting out? Need help? Post your questions and find answers here.
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Help with converted code

Post by Pot Noodle »

I have a routine from BlitzMax and I have converted it to PB
But I can't get it to work like the Original.
Any help would be great, Thanks.

Code: Select all

; ----------------------------------------------------------
; BlitzMax Animated Chequerboard Pattern Converted To PB
; ----------------------------------------------------------

#xres = 1024
#yres = 768

InitSprite()
Global Whdc = OpenWindow(0,0,0,#xres,#yres,"3D",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)

;{ Declarations
Declare SetGrid()
Declare DrawGrid()
;}

Global Grid.i = 30
Global Size.i = 150

Global halfX = 512
Global halfY = 384

Global Move.d = 0.0

Global Dim GridX.d(Grid,Grid)
Global Dim GridY.d(Grid,Grid)
Global Dim GridZ.d(Grid,Grid)

SetGrid()

Repeat

	ClearScreen($FFFFFF)
	
	Event = WindowEvent()
	Select Event
		Case #PB_Event_CloseWindow
			Quit = 1
	EndSelect
	
	DrawGrid()
	Move + 0.025 : If Move >= 0.5 : Move -0.5 : EndIf
	
	FlipBuffers()
		
	Until GetAsyncKeyState_(#VK_ESCAPE) Or Quit
End

Procedure DrawGrid()
	
	Static Z.i
	Static X.i
	
	Static TX.i
	Static TY.i
	
	Static Clr.i
	Static Shade.i
	Static ClrStrt.i
	
	ClrStrt = 1
	Static Dim PolyTransform.f(8)
	DC = StartDrawing(WindowOutput(0))
		
	For Z = 0 To Grid - 2	
		
		ClrStrt + 1 :	If ClrStrt > 2 : ClrStrt = 1 : EndIf
		Clr = ClrStrt
		
		For X = 0 To Grid - 2
			
			Clr + 1 :	If Clr > 2 : Clr = 1 : EndIf
			
			TX = (GridX(X,Z)     / (GridZ(X,Z)-Move)) + halfX
			TY = (GridY(X,Z)     / (GridZ(X,Z)-Move)) + halfY			
			
			PolyTransform(0) = TX
			PolyTransform(1) = TY
			
			TX = (GridX(X+1,Z)   / (GridZ(X+1,Z)-Move)) + halfX
			TY = (GridY(X+1,Z)   / (GridZ(X+1,Z)-Move)) + halfY			
			
			PolyTransform(2) = TX
			PolyTransform(3) = TY
			
			TX = (GridX(X+1,Z+1) / (GridZ(X+1,Z+1)-Move)) + halfX
			TY = (GridY(X+1,Z+1) / (GridZ(X+1,Z+1)-Move)) + halfY			
			
			PolyTransform(4) = TX
			PolyTransform(5) = TY
			
			TX = (GridX(X,Z+1)   / (GridZ(X,Z+1)-Move)) + halfX
			TY = (GridY(X,Z+1)   / (GridZ(X,Z+1)-Move)) + halfY			
			
			PolyTransform(6) = TX
			PolyTransform(7) = TY
			
			Shade=(TY/4) - 75 :	If Shade < 0 : Shade = 0 : EndIf
			; ----- Not Implemented! -----
			If Clr =1 : Color =(RGB(Shade,Shade,Shade)) : EndIf
			If Clr =2 : Color =(RGB(0, 0, Shade)) : EndIf

			Polygon_(DC,@PolyTransform(),5)
			
		Next
	Next
StopDrawing()

EndProcedure

Procedure SetGrid()

	Static Xpos.d
	Static Zpos.d
	Static Ypos.d
	Static Jump.d
	Static Xscale.d
	
	Static Z.i
	Static X.i
	
	Jump = Size / Grid
	
	Xscale = 55	
	Ypos = 500
	Zpos = 8.4
	
	For Z = 0 To Grid -1 

		Xpos = -((Size*Xscale) /2)	

		For X = 0 To Grid -1

			GridX(X, Z) = Xpos
			GridY(X, Z) = Ypos
			GridZ(X, Z) = Zpos

			Xpos = Xpos + (Jump * Xscale)
		Next		
		Zpos = Zpos - (Jump / 20)				
	Next
	
EndProcedure
Original Code:

Code: Select all


Strict

	SetGraphicsDriver GLMax2DDriver()
	
	Const xres = 1024
	Const yres = 768
	
	Const halfX = 512
	Const halfY = 384
	
	Graphics xres,yres,32,60,GRAPHICS_BACKBUFFER
	Const Grid = 30:Int
	Const Size = 150 : Int
	
	Global GridX : Double[Grid,Grid]
	Global GridY : Double[Grid,Grid]
	Global GridZ : Double[Grid,Grid]

	Global Move : Double

	Move = 0.0
	SetGrid()
	
	Repeat

		SetColor (255,255,255)
		
		DrawGrid()
		Move = Move + .025
		If Move >= .5 Then Move =Move -.5
		Flip(-1)
		Cls
	
	Until KeyDown(KEY_ESCAPE)

	EndGraphics
	End

Function DrawGrid()

	Local Z:Int
	Local X : Int
	
	Local TX : Int
	Local TY : Int
	
	Local Clr     : Int
	Local Shade   : Int
	Local ClrStrt : Int

	ClrStrt=1
	
	Local PolyTransform : Float [8]

	For Z = 0 To Grid-2	
	
		ClrStrt=ClrStrt+1
		If ClrStrt>2 Then ClrStrt=1
		Clr = ClrStrt
		For X = 0 To Grid-2

			Clr=Clr+1
			If Clr>2 Then Clr=1
											
			TX = (GridX[X,Z]     / (GridZ[X,Z]-Move))+HalfX
			TY = (GridY[X,Z]     / (GridZ[X,Z]-Move))+HalfY			
			
			Polytransform[0]=TX
			Polytransform[1]=TY

			TX = (GridX[X+1,Z] / (GridZ[X+1,Z]-Move))+HalfX
			TY = (GridY[X+1,Z]     / (GridZ[X+1,Z]-Move))+HalfY			
			
			Polytransform[2]=TX
			Polytransform[3]=TY

			TX = (GridX[X+1,Z+1] / (GridZ[X+1,Z+1]-Move))+HalfX
			TY = (GridY[X+1,Z+1] / (GridZ[X+1,Z+1]-Move))+HalfY			
			
			Polytransform[4]=TX
			Polytransform[5]=TY

			TX = (GridX[X,Z+1] / (GridZ[X,Z+1]-Move))+HalfX
			TY = (GridY[X,Z+1] / (GridZ[X,Z+1]-Move))+HalfY			
			
			Polytransform[6]=TX
			Polytransform[7]=TY

			shade=(ty/4)-75
			If shade<0 Then shade=0

			If Clr =1 Then SetColor(Shade,Shade,Shade)
			If Clr =2 Then SetColor( 0, 0,Shade)			
		
			DrawPoly(Polytransform)
						
		Next
		
	Next


End Function


Function SetGrid()


	Local Xpos : Double
	Local Zpos : Double
	Local Ypos : Double
	Local Jump : Double
	Local Xscale : Double

	Local Z:Int
	Local X:Int
	
	JUMP = Size / Grid

	Xscale = 55	
	Ypos = 500
	Zpos = 8.4
	
	For Z = 0 To Grid -1 

		Xpos = -((Size*Xscale) /2)	
	
			For X = 0 To Grid -1
					
				GridX[X, Z] = Xpos
				GridY[X, Z] = Ypos
				GridZ[X, Z] = Zpos
				
				Xpos = Xpos + (JUMP * Xscale)
		
			Next		
		
		Zpos = Zpos - (JUMP / 20)
	
	Next

End Function

P.N.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Help with converted code

Post by doctorized »

Can you post a working exe from the original code to see what exactly is doing on screen?
I did some changes just to see something on screen....

Code: Select all

; ----------------------------------------------------------
; BlitzMax Animated Chequerboard Pattern Converted To PB
; ----------------------------------------------------------

#xres = 1024
#yres = 768

InitSprite()
;Global Whdc = OpenWindow(0,0,0,#xres,#yres,"3D",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Global Whdc = OpenScreen(#xres,#yres,32,"3D");,#PB_Window_SystemMenu | #PB_Window_ScreenCentered)

;{ Declarations
Declare SetGrid()
Declare DrawGrid()
;}

Global Grid.i = 30
Global Size.i = 150

Global halfX = 512
Global halfY = 384

Global Move.d = 0.0

Global Dim GridX.d(Grid,Grid)
Global Dim GridY.d(Grid,Grid)
Global Dim GridZ.d(Grid,Grid)

SetGrid()

Repeat

   ClearScreen($FFFFFF)
   
   ;Event = WindowEvent()
   ;Select Event
   ;   Case #PB_Event_CloseWindow
   ;      Quit = 1
   ;EndSelect
   
   DrawGrid()
   Move + 0.025 : If Move >= 0.5 : Move -0.5 : EndIf
   
   FlipBuffers()
      
   Until GetAsyncKeyState_(#VK_ESCAPE); Or Quit
End

Procedure DrawGrid()
   
   Static Z.i
   Static X.i
   
   Static TX.i
   Static TY.i
   
   Static Clr.i
   Static Shade.i
   Static ClrStrt.i
   
   ClrStrt = 1
   Static Dim PolyTransform.f(8)
   DC = StartDrawing(ScreenOutput())
      
   For Z = 0 To Grid - 2   
      
      ClrStrt + 1 :   If ClrStrt > 2 : ClrStrt = 1 : EndIf
      Clr = ClrStrt
      
      For X = 0 To Grid - 2
         
         Clr + 1 :   If Clr > 2 : Clr = 1 : EndIf
         
         TX = (GridX(X,Z)     / (GridZ(X,Z)-Move)) + halfX
         TY = (GridY(X,Z)     / (GridZ(X,Z)-Move)) + halfY         
         
         PolyTransform(0) = TX
         PolyTransform(1) = TY
         
         TX = (GridX(X+1,Z)   / (GridZ(X+1,Z)-Move)) + halfX
         TY = (GridY(X+1,Z)   / (GridZ(X+1,Z)-Move)) + halfY         
         
         PolyTransform(2) = TX
         PolyTransform(3) = TY
         
         TX = (GridX(X+1,Z+1) / (GridZ(X+1,Z+1)-Move)) + halfX
         TY = (GridY(X+1,Z+1) / (GridZ(X+1,Z+1)-Move)) + halfY         
         
         PolyTransform(4) = TX
         PolyTransform(5) = TY
         
         TX = (GridX(X,Z+1)   / (GridZ(X,Z+1)-Move)) + halfX
         TY = (GridY(X,Z+1)   / (GridZ(X,Z+1)-Move)) + halfY         
         
         PolyTransform(6) = TX
         PolyTransform(7) = TY
         
         Shade=(TY/4) - 75 :   If Shade < 0 : Shade = 0 : EndIf
         ; ----- Not Implemented! -----
         If Clr =1 : Color =(RGB(Shade,Shade,Shade)) : EndIf
         If Clr =2 : Color =(RGB(0, 0, Shade)) : EndIf

         Polygon_(DC,@PolyTransform(),5)
         
      Next
   Next
StopDrawing()

EndProcedure

Procedure SetGrid()

   Static Xpos.d
   Static Zpos.d
   Static Ypos.d
   Static Jump.d
   Static Xscale.d
   
   Static Z.i
   Static X.i
   
   Jump = Size / Grid
   
   Xscale = 55   
   Ypos = 500
   Zpos = 8.4
   
   For Z = 0 To Grid -1

      Xpos = -((Size*Xscale) /2)   

      For X = 0 To Grid -1

         GridX(X, Z) = Xpos
         GridY(X, Z) = Ypos
         GridZ(X, Z) = Zpos

         Xpos = Xpos + (Jump * Xscale)
      Next      
      Zpos = Zpos - (Jump / 20)            
   Next
   
EndProcedure
User avatar
JHPJHP
Addict
Addict
Posts: 2258
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with converted code

Post by JHPJHP »

Hi doctorized,

Here is the original source (no exe): http://www.dbfinteractive.com/forum/ind ... pic=5207.0

But I think Demivec had it mostly right in this post: http://www.forums.purebasic.com/english ... 13&t=49525

The problem I see is substituting BlitzMax's DrawPoly function for the Windows API Polygon:
- Polygon requires a pointer to a POINT Structure array: http://msdn.microsoft.com/en-us/library ... s.85).aspx
- POINT Structure's parameters are LONGs: http://msdn.microsoft.com/en-us/library ... s.85).aspx

I think not using a pointer, or a POINT Structure, and passing a Float is messing things up, but if you know of a work-around to the Polygon API...

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Re: Help with converted code

Post by Pot Noodle »

Hi doctorized,

Here is the exe https://dl.dropboxusercontent.com/u/718 ... rboard.exe

Thanks
P.N.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Help with converted code

Post by Demivec »

Here is a working version. I followed my own advice from the link JHPJHP provided and modified the code to use an array of 'point' and opened a windowed screen and used API to draw the shapes. :)

Code: Select all

; ----------------------------------------------------------
; BlitzMax Animated Chequerboard Pattern Converted To PB
; ----------------------------------------------------------

#xres = 1024
#yres = 768

InitSprite()
Global Whdc = OpenWindow(0,0,0,#xres,#yres,"3D",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #xres, #yres)

;{ Declarations
Declare SetGrid()
Declare DrawGrid()
;}

Global Grid.i = 30
Global Size.i = 150

Global halfX = 512
Global halfY = 384

Global Move.d = 0.0

Global Dim GridX.d(Grid,Grid)
Global Dim GridY.d(Grid,Grid)
Global Dim GridZ.d(Grid,Grid)

SetGrid()

Repeat
  
  ClearScreen($000000)
  
  Event = WindowEvent()
  Select Event
    Case #PB_Event_CloseWindow
      Quit = 1
  EndSelect
  
  DrawGrid()
  Move + 0.025 : If Move >= 0.5 : Move -0.5 : EndIf
  
  FlipBuffers()
  
Until GetAsyncKeyState_(#VK_ESCAPE) Or Quit
End

Procedure DrawGrid()
  
  Static Z.i
  Static X.i
  
  Static TX.i
  Static TY.i
  
  Static Clr.i
  Static Shade.i
  Static ClrStrt.i

  Static pen, hbrush

  ClrStrt = 1
  Static Dim PolyTransform.point(3)
  DC = StartDrawing(ScreenOutput())
  
  For Z = 0 To Grid - 2   
    
    ClrStrt + 1 :   If ClrStrt > 2 : ClrStrt = 1 : EndIf
    Clr = ClrStrt
    
    For X = 0 To Grid - 2
      
      Clr + 1 :   If Clr > 2 : Clr = 1 : EndIf
      
      TX = (GridX(X,Z)     / (GridZ(X,Z)-Move)) + halfX
      TY = (GridY(X,Z)     / (GridZ(X,Z)-Move)) + halfY         
      
      PolyTransform(0)\x = TX
      PolyTransform(0)\y = TY
      
      TX = (GridX(X+1,Z)   / (GridZ(X+1,Z)-Move)) + halfX
      TY = (GridY(X+1,Z)   / (GridZ(X+1,Z)-Move)) + halfY         
      
      PolyTransform(1)\x = TX
      PolyTransform(1)\y = TY
      
      TX = (GridX(X+1,Z+1) / (GridZ(X+1,Z+1)-Move)) + halfX
      TY = (GridY(X+1,Z+1) / (GridZ(X+1,Z+1)-Move)) + halfY         
      
      PolyTransform(2)\x = TX
      PolyTransform(2)\y = TY
      
      TX = (GridX(X,Z+1)   / (GridZ(X,Z+1)-Move)) + halfX
      TY = (GridY(X,Z+1)   / (GridZ(X,Z+1)-Move)) + halfY         
      
      PolyTransform(3)\x = TX
      PolyTransform(3)\y = TY
      
      Shade=(TY/4) - 75 :   If Shade < 0 : Shade = 0 : EndIf
      ; ----- Not Implemented! -----
      ;          If Clr =1 : Color =(RGB(Shade,Shade,Shade)) : EndIf
      ;          If Clr =2 : Color =(RGB(0, 0, Shade)) : EndIf
      
      
      If Clr = 1
        pen = CreatePen_(#PS_SOLID, 1, RGB(Shade,Shade,Shade))
        hBrush = CreateSolidBrush_(RGB(Shade,Shade,Shade))
      Else
        pen = CreatePen_(#PS_SOLID, 1, RGB(0, 0, Shade))
        hBrush = CreateSolidBrush_(RGB(0,0,Shade))
      EndIf 
      
      If dc
        SelectObject_(dc, pen)
        SelectObject_(dc, hbrush)
        Polygon_(DC,@PolyTransform(),4)
        
        DeleteObject_(pen): pen = 0
        DeleteObject_(hbrush): hbrush = 0
      EndIf 
      
    Next
  Next
  
  StopDrawing()
  
EndProcedure

Procedure SetGrid()
  
  Static Xpos.d
  Static Zpos.d
  Static Ypos.d
  Static Jump.d
  Static Xscale.d
  
  Static Z.i
  Static X.i
  
  Jump = Size / Grid
  
  Xscale = 55   
  Ypos = 500
  Zpos = 8.4
  
  For Z = 0 To Grid -1 
    
    Xpos = -((Size*Xscale) /2)   
    
    For X = 0 To Grid -1
      
      GridX(X, Z) = Xpos
      GridY(X, Z) = Ypos
      GridZ(X, Z) = Zpos
      
      Xpos = Xpos + (Jump * Xscale)
    Next      
    Zpos = Zpos - (Jump / 20)            
  Next
  
EndProcedure
User avatar
JHPJHP
Addict
Addict
Posts: 2258
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with converted code

Post by JHPJHP »

Sorry Demivec, I should have said you had it completely right. :)

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Help with converted code

Post by Demivec »

JHPJHP wrote:Sorry Demivec, I should have said you had it completely right. :)
Thanks for the complement. I won't even mention what my first attempts looked like. :D
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Re: Help with converted code

Post by Pot Noodle »

Thanks Demivec, I wish I had the skill you have :( being dyslexic does not help in this game.
P.N.
Jagermeister
Enthusiast
Enthusiast
Posts: 137
Joined: Thu Nov 15, 2012 11:38 pm
Location: Los Angeles

Re: Help with converted code

Post by Jagermeister »

Pot Noodle, if you ain't kidding about being dyslexic this might help. I work with an accommodations department at a college. They started using the OpenDyslexic font recently and students are liking it. Maybe it'll help you out: http://www.opendyslexic.org

Peace.
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Re: Help with converted code

Post by Pot Noodle »

Thanks Jagermeister
That looks interesting, I have been using computers since the C64 days and have been programming since then
But I have never rely made anything of use, But I am getting there with the help I get here, trouble is
I keep getting stuck and have to keep asking questions and I don't think the guys here like it :oops:
P.N.
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Re: Help with converted code

Post by Pot Noodle »

@Demivec

Having put the code in to my program I find it now run's really slow, So I was wondering if it's possible to
save the numbers generated by the code and load them in to the PolyTransform() array at run time, from a data section or something.
Would this be possible ?

Regards
P.N.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Help with converted code

Post by Demivec »

Pot Noodle wrote:Having put the code in to my program I find it now run's really slow, So I was wondering if it's possible to
save the numbers generated by the code and load them in to the PolyTransform() array at run time, from a data section or something.
Would this be possible ?
It can be optimized in more ways than one. When I have a moment I will see what can be done.
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Re: Help with converted code

Post by Pot Noodle »

Thanks, Looking forward to it.
P.N.
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Re: Help with converted code

Post by Pot Noodle »

Hi Demivec,
It can be optimized in more ways than one. When I have a moment I will see what can be done
Any luck with it yet, I have tried a few different things but I think it is the Rectangle command that is slow :|

Regards
P.N.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Help with converted code

Post by Demivec »

Pot Noodle wrote: Any luck with it yet, I have tried a few different things but I think it is the Rectangle command that is slow :|
Yes, I've had some luck with it. Here it is (additional explanations follow):

Code: Select all

; ----------------------------------------------------------
; BlitzMax Animated Chequerboard Pattern Converted To PB
; ----------------------------------------------------------

#xres = 1024
#yres = 768

InitSprite()
Global Whdc = OpenWindow(0,0,0,#xres,#yres,"3D",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #xres, #yres)

;{ Declarations
Declare SetGrid()
Declare DrawGrid()
;}

Structure point_3f
  x.f
  y.f
  z.f
EndStructure

Global Grid.i = 30
Global Size.i = 150

Global halfX = 512
Global halfY = 384

Global Move.d = 0.0

Global Dim Grid.point_3f(Grid - 1, Grid - 1)

Global shadeSprite ;used to shade the checkerboard
Global checkerSprite ;used to render the dark squares of the checkerboard
Global checkerboardSprite ;used to render all light portions of the checkerboard
Global gridYScreenStart = 444 ;precalculated (Y value based on projected Z distance) for top of grid


SetGrid()

Repeat
  
  ClearScreen($000000)
  
  Event = WindowEvent()
  Select Event
    Case #PB_Event_CloseWindow
      quit = 1
  EndSelect
  
  DrawGrid()
  ;Move + 0.025 : If Move >= 0.5 : Move -0.5 : EndIf
  Move + 0.025 : If Move >= 0.35 : Move -0.5 : EndIf ;this small change is to show more of grid on screen
  
  FlipBuffers()
  
Until GetAsyncKeyState_(#VK_ESCAPE) Or quit
End

Procedure DrawGrid()
  
  Protected.i x, z
  Protected.i TX, TY
  Protected.i ClrStrt, Clr, Shade

  Static Dim pen(1)
  Static Dim HBrush(1)
   
  Static Dim PolyTransform.POINT(3)
  
  DisplaySprite(checkerboardSprite, 0, gridYScreenStart)
  For z = 0 To Grid - 2   
    
    ClrStrt ! 1 ;alternates between 0 and 1
    Clr = ClrStrt
    
    For x = 0 To Grid - 2
      Debug "x,z:" + x + "," + z
      Clr ! 1 ;alternates between 0 and 1
      If Clr = 0
        TX = (Grid(x,z)\x / (Grid(x,z)\z - Move)) + halfX
        TY = (Grid(x,z)\y / (Grid(x,z)\z - Move)) + halfY         
        
        PolyTransform(0)\x = TX
        PolyTransform(0)\y = TY
        
        TX = (Grid(x+1,z)\x / (Grid(x+1,z)\z - Move)) + halfX
        ;TY = (Grid(x+1,z)\y / (Grid(x+1,z)\z - Move)) + halfY ;same as for index 0
        
        PolyTransform(1)\x = TX
        PolyTransform(1)\y = TY
        
        TX = (Grid(x+1,z+1)\x / (Grid(x+1,z+1)\z - Move)) + halfX
        TY = (Grid(x+1,z+1)\y / (Grid(x+1,z+1)\z - Move)) + halfY         
        
        PolyTransform(2)\x = TX
        PolyTransform(2)\y = TY
        
        TX = (Grid(x,z+1)\x / (Grid(x,z+1)\z - Move)) + halfX
        ;TY = (Grid(x,z+1)\y / (GridZ(x,z+1) - Move)) + halfY  ;same as for index 2     
        
        PolyTransform(3)\x = TX
        PolyTransform(3)\y = TY
        
        ;crude form of clipping for furthest checkers
        If z = 0 And PolyTransform(0)\y < gridYScreenStart
          PolyTransform(0)\y = gridYScreenStart
          PolyTransform(1)\y = gridYScreenStart
        EndIf 
        ;clipping for checkers that are partially drawn on left of screen,
        ;ensures top-left coordinate is on screen if any of the sprite is visible
        If PolyTransform(1)\x > 0 And PolyTransform(0)\x < 0
          PolyTransform(0)\x = 0
        EndIf 
        TransformSprite(checkerSprite, 0, 0, PolyTransform(1)\x - PolyTransform(0)\x, 0, PolyTransform(2)\x - PolyTransform(0)\x, PolyTransform(2)\y - PolyTransform(0)\y, PolyTransform(3)\x - PolyTransform(0)\x, PolyTransform(3)\y - PolyTransform(0)\y)
        DisplaySprite(checkerSprite, PolyTransform(0)\x, PolyTransform(0)\y)
      EndIf 
    Next 
    
  Next
  
  DisplayTransparentSprite(shadeSprite, 0, gridYScreenStart)
EndProcedure

Procedure SetGrid()
  
  Protected.d Xpos, Ypos, Zpos, Jump, Xscale
  Protected.i x, z, y
  
  Jump = Size / Grid
  
  Xscale = 55   
  Ypos = 500
  Zpos = 8.4
  
  For z = 0 To Grid - 1 
    
    Xpos = -((Size * Xscale) / 2)   
    
    For x = 0 To Grid - 1
      
      Grid(x, z)\x = Xpos
      Grid(x, z)\y = Ypos
      Grid(x, z)\z = Zpos
      
      Xpos + (Jump * Xscale)
    Next      
    Zpos - (Jump / 20)            
  Next
  
  Protected gridSpriteHeight = 767 - gridYScreenStart + 1
  shadeSprite = CreateSprite(#PB_Any, 1, gridSpriteHeight, #PB_Sprite_AlphaBlending)
  If shadeSprite And StartDrawing(SpriteOutput(shadeSprite))
      Protected *bufferPixel.Long = DrawingBuffer(), pitch = DrawingBufferPitch(), Shade, shadeRange = 205
      
      For y = 1 To OutputHeight()
        Shade = shadeRange - y / (OutputHeight() / shadeRange)
        If Shade < 0: Shade = 0: EndIf ;near (bright) side
        If Shade > shadeRange: Shade = shadeRange: EndIf ;far (dark) side
        *bufferPixel\l = RGBA(0, 0, 0, Shade)
        *bufferPixel + pitch
      Next
    StopDrawing()
    
    TransformSprite(shadeSprite, 0, 0, #xres, 0, #xres, gridSpriteHeight, 0, gridSpriteHeight)
  EndIf
  
  checkerboardSprite = CreateSprite(#PB_Any, 1, 1)
  If checkerboardSprite And StartDrawing(SpriteOutput(checkerboardSprite))
      Plot(0, 0, RGB(152, 152, 152))
    StopDrawing()
    
    TransformSprite(checkerboardSprite, 0, 0, #xres, 0, #xres, gridSpriteHeight, 0, gridSpriteHeight)
  EndIf
  
  checkerSprite = CreateSprite(#PB_Any, 2, 2)
  If checkerSprite And StartDrawing(SpriteOutput(checkerSprite))
        Plot(0, 0, RGB(  0,   0, 152))
        Plot(0, 1, RGB(  0,   0, 152))
        Plot(1, 0, RGB(  0,   0, 152))
        Plot(1, 1, RGB(  0,   0, 152))
    StopDrawing()
  EndIf 
    
EndProcedure
I switched over to sprites to create the grid.
  • * I use a 1x1 sprite that is stretched to the width of the screen to draw the 'white' portions of the grid.
    * I use a 2x2 sprite that is stretched to the calculated grid coordinates for each blue square.
    * I use a 1x376 sprite that is stretched to the width of the screen to shade the grid after it is drawn.
Only half the grid (the blue squares) is calculated so that cuts the time in half as well. I used the shading sprite for alphablending so that I wouldn't have to use a transparent sprite with color changes to draw each of the squares (though that could have been done).

I tried as much as possible to match the visual results of the previous code. In the process I made some minor speed optimizations and simplifications. For instance I combined all three grid arrays into one using a structure and changed the sub-element types to float instead of double.

I also tried to correct some minor flaws in the previous BlitzMax routine. One of these was that the furthest row of the grid that is scrolled doesn't also show the next row (or new row) coming into view. I fudged it a little bit but it seems better. Another flaw I saw didn't have any obvious solutions (at least ones I could see right away). It involves the far right of the grid where it isn't displayed at all. In my code it is currently displayed as a white area instead (not much of an improvement).

I did some simple timing tests where I executed the DrawGrid() procedure and the update for the 'Move variable' 1000 times. I found at least a 20x increase in speed. I am sure your results will depend a lot on your other code.

I tested the code with the Windowx 8.1 , Direct X subsystem x32. The results under OpenGL came out a little warped and the shading was reversed (closer things were darker). Some of the OpenGL problems I think can be corrected if need be, at least the shading ones. The results were the same with 64-bit compilations.
Post Reply