Two Unrelated questions.

BlitzMax Forums/BlitzMax Beginners Area/Two Unrelated questions.

Well since i tend to frequent this forum a little too much, i thought I might condence two posts into one. Unless a moderator feels that I need to split them.


Question 1:
I'm using alot of code to simply draw rotated rectangles. Would it be faster to draw rotated pixmaps? I'm trying to challange myself to make a game without using extenal graphics.

I was thinking of making an image from scratch and saving it to a pixmap and rotating it for the shot graphics. I suppose I might also use a drawrect command with rotation taken into account. If you have played Gun Ranger X, then you know that there can be many shots onscreen at once. I'm looking for small improvments here and there, I noticed that my draw rect command it quite time consuming.
here is the code:
Function draw_rect_filled(x#,y#,direction#,length#,width#)
		
		
		Local x1#=(x+Cos(direction)*width/2)-Cos(direction+90)*length/2
		Local y1#=(y+Sin(direction)*width/2)-Sin(direction+90)*length/2
		Local x2#=(x+Cos(direction)*width/2)+Cos(direction+90)*length/2
		Local y2#=(y+Sin(direction)*width/2)+Sin(direction+90)*length/2 
		Local x3#=(x+Cos(direction)*-width/2)-Cos(direction+90)*length/2
		Local y3#=(y+Sin(direction)*-width/2)-Sin(direction+90)*length/2
		Local x4#=(x+Cos(direction)*-width/2)+Cos(direction+90)*length/2
		Local y4#=(y+Sin(direction)*-width/2)+Sin(direction+90)*length/2
		Local temp_array#[]=[x1,y1,x2,y2,x4,y4,x3,y3]
	    DrawPoly(temp_array)
		
		
		
	End Function





Question 2:
In my game you set the color selection. It uses three colors each made of RGB components.

the vars are:
global red_1#
global green_1#
global blue_1#

global red_2#
global green_2#
global blue_2#

global red_3#
global green_3#
global blue_3#

Basicaly, I need to save these to an external file so the player can keep their palette. No encryption is necessary. I need two functions i guess, one to update and save the file, and one to load and set the values from the file.

I hope someone can help me here. Thanks in advance.

You can optimise that code pretty well just by doing 2 things.

1) Compute Cos/Sin once - and use the computed values to construct the 4 corners.

2) Don't use a temp array. Use a global one. That way it will not be created/destroyed on every call to draw_rect_filled().

Example of loading/saving data to a file (add your own error handling etc) :-

Function SavePalette()
	Local f:TStream = WriteStream( "palette.dat" )
	If f
		f.WriteFloat( r )
		f.WriteFloat( g )
		f.WriteFloat( b )
		CloseStream f
	EndIf
EndFunction

Function LoadPalette()
	Local f:TStream = ReadStream( "palette.dat" )
	If f
		r = f.ReadFloat()
		g = f.ReadFloat()
		b = f.ReadFloat()	
		CloseStream f
	EndIf
EndFunction



I suppose I might also use a drawrect command with rotation taken into account.


Seems straight forward. What happens when you *do* use drawrect with setrotation?

Yep, why arent you using Setrotation? Or at the least use the pre built Updatetransform.
Function UpdateTransform()
	Local s#=Sin(gc.tform_rot)
	Local c#=Cos(gc.tform_rot)
	gc.tform_ix= c*gc.tform_scale_x
	gc.tform_iy=-s*gc.tform_scale_y
	gc.tform_jx= s*gc.tform_scale_x
	gc.tform_jy= c*gc.tform_scale_y
	_max2dDriver.SetTransform gc.tform_ix,gc.tform_iy,gc.tform_jx,gc.tform_jy
	SetCollisions2DTransform gc.tform_ix,gc.tform_iy,gc.tform_jx,gc.tform_jy
End Function
Then its just x*ix+y*iy,x*jx+y+jy for each point.

But really really, you would just be recodeing setrotation.

You could use TImages also:
Strict
Graphics 800,600,32
Local Angle:Float = 0 'Angle of rectangle
Local Red:Int = 255 'color of rectangle
Local Green:Int = 255
Local Blue:Int = 255

Local Pixmap:TPixmap = CreatePixmap(32,32,PF_RGBA8888) 'create a 32x32 pixmap
For Local x:Int = 0 To 31
	For Local y:Int = 0 To 31
		WritePixel(Pixmap,x,y,$FFFFFFFF) 'Fill the pixmap with white
	Next
Next

AutoMidHandle True 'center the image's handle
Local Rectangle:TImage = LoadImage(Pixmap) 'load the pixmap into the image
Pixmap = Null 'free the pixmap

Local Time:Int = MilliSecs() 'for delta timing
Local NewTime:Int, Delta:Int

While Not KeyHit(KEY_ESCAPE)
	NewTime = MilliSecs()
	Delta = NewTime - Time
	Time = NewTime
	
	If KeyHit(KEY_ENTER) 'Enter key to create random color
		Red = Rand(0,255)
		Green = Rand(0,255)
		Blue = Rand(0,255)
	End If
	
	Angle :+ .0625 * delta 'Increase the angle
	If Angle >= 360.0 Then Angle :- 360.0 'Wrap the angle when >= 360
	
	Cls
	SetColor Red,Green,Blue
	SetRotation Angle
	DrawImage Rectangle,400,300 'Draw the image in the chosen color and angle.
	
	SetColor 255,255,255
	SetRotation 0
	DrawText "Press ENTER to change color",10,10
	
	Flip 
Wend



Thanks for all the help guys!