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 FunctionThen 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: |
| ||
Thanks for all the help guys! |