New to Programming

BlitzMax Forums/BlitzMax Beginners Area/New to Programming

Uniquekind(Posted 2005) [#1]
Hello everyone,

I'm completely new to programming in general. I've been trying to teach myself BlitzMax for the past 3 days reading through tutorials and picking through the code available through the forums, but I'm still having a lot of trouble understanding the basics.

I can't seem to find any simple code, so I was hoping someone could help me out with some code for simply loading an image and being able to rotate it 360, or to even rotate a rectangle created from the DrawRect command.

These probably sound like really stupid questions, but I'm just trying to build the foundation for understanding how to program.

Any help would be greatly appreciated.


Who was John Galt?(Posted 2005) [#2]
Graphics 800,600
rot#=0.0
Repeat
        Cls
	SetRotation rot
	DrawRect 200,200,100,100
	rot=rot+1.0
	If rot>360.0 Then rot=rot-360.0
	Flip
	FlushMem()
Until KeyHit(KEY_ESCAPE)


There you go mate. Welcome to Blitz!

Your program always starts with a graphics command to open a screen - here we opened in 800 by 600 pixel mode. The repeat/until command loop until the user presses the escape key. Everything inside this is indented to make it easy to see that it's running inside a loop (you don't have to indent, but it helps readability.) We declare a variable rot to hold the angle of rotation of the rectangle. The # on the end tells Blitz the variable is a floating point number (a number that can hold fractional values). Every loop, we clear the screen, set the required rotation and draw the rectangle. rot is increased by 1 every loop to change the rotation. Flip flips the screen - all drawing is done on a second copy of the screen called the backbuffer that is not visible to the user. When flip is called, this is brought to the front so you can see the results. Always stick flushmem() once in your main loop - although it probably isnt required here. Try changing the rotation speed and the size of the rectangle. Look up the loadimage and drawimage commands and try to get the example rotating an image.


kfprimm(Posted 2005) [#3]
heres another example i whipped up...

'Set the complier to strict mode
Strict

'Change the title of the graphics window
AppTitle="Rotation Example"

'Set the mask color to white
SetMaskColor 255,255,255

'Load the BlitzMax logo from the doc folder in the BlitzMax path and masked
'all the white pixels in the file so they will not show up when the pic. is drawn
Local bmax_logo:TImage=LoadImage("..\doc\bmax120.png",MASKEDIMAGE)

'Set the logo's handle to the middle
MidHandleImage bmax_logo

'Declare the rotation value varible
Local rotation:Float=0

'Declare the logo's x coordinate varible
Local bmax_logo_x:Int=400

'Declare the logo's y coordinate varible
Local bmax_logo_y:Int=300

'Create the graphics window
Graphics 800,600,0

'Begin the main loop and wait for the user to press the ESC key
Repeat

'clear the screen
Cls

	'Check to see if the left arrow key has been pressed
	If KeyDown(KEY_LEFT)
		'If the left key was pressed then subtract one from the logo's x
		'moving the logo to the left one pixel
		bmax_logo_x:-1
	EndIf
	
	'Check to see if the right arrow key has been pressed
	If KeyDown(KEY_RIGHT)
		'If the right key was pressed then subtract one from the logo's x
		'moving the logo to the right one pixel
		bmax_logo_x:+1
	EndIf
	
	'Check to see if the up arrow key has been pressed
	If KeyDown(KEY_UP)
		'If the up key was pressed then subtract one from the logo's x
		'moving the logo up one pixel
		bmax_logo_y:-1
	EndIf
	
	'Check to see if the down arrow key has been pressed
	If KeyDown(KEY_DOWN)
		'If the down key was pressed then subtract one from the logo's x
		'moving the logo down one pixel
		bmax_logo_y:+1
	EndIf
	
	'Add one to the rotation number, turning the logo one degrees
	rotation:+1
	
	'Check to see if the rotation number has become greater than 360
	If rotation>360
		'If the rotation number has become greater than 360
		'make the rotation value equal 0 degrees
		rotation=0
	EndIf
	
	'Set the rotation of the logo to the value of rotation
	SetRotation rotation
	
	'Draw the logo at its x and y coordinates
	DrawImage bmax_logo,bmax_logo_x,bmax_logo_y
	
'Show the logo
Flip

Until KeyDown(KEY_ESCAPE)
'End of main loop

Hopefully that will help you to and welcome to the community! :-)
If you have any questions feel free to ask.


deps(Posted 2005) [#4]
And here is how you load an image and rotate it:
Graphics 800,600
rot#=0.0
image:TImage = loadimage("image.png") ' load an image
MidHandleImage( image ) ' When drawing the image, the center of the image is at the coordinates, instead of the top left corner
Repeat
        Cls
        SetRotation rot
        drawimage( image, 400,300 ) ' Draw our image
        rot=rot+1.0
        If rot>360.0 Then rot=rot-360.0
        Flip
        FlushMem()
Until KeyHit(KEY_ESCAPE)


You can leave out MidHandleImage() if you want. But by using it the image is rotated around it's center (wich we place in the middle of the screen in this example)
But do try to comment that line out so you can see how it works.

Edit: I was too slow. :)

Edit again: Khomy Prime, sure it works if you load the image before setting a graphics mode? I think it's much better to load all graphics after the call to graphics()


kfprimm(Posted 2005) [#5]
@deps
i do that so a blank black screen will not just sit there while the graphics load. Well, in an example this small i dont think it will really make a difference. Im pretty sure it works fine that way, but i could be wrong.


deps(Posted 2005) [#6]
The manual says "This command must be executed before any other Max2D commands." and "Changing graphics modes will automatically invalidate all images and image fonts".
So Graphics() should be called before loadimage().

... But enough of this rant. :)
I hope the examples makes things much more clear to Uniquekind, if there is any other questions then just ask away.


FlameDuck(Posted 2005) [#7]
So Graphics() should be called before loadimage().
Actually, I'm pretty sure that doesn't apply anymore...


ImaginaryHuman(Posted 2005) [#8]
I think in the manual it still says that when you call Graphics() it loses any images that were previously loaded.
??


kfprimm(Posted 2005) [#9]
yeah it does but that doesnt seem to be the case.


Duckstab[o](Posted 2005) [#10]
in my previous bmax code you lost all images havent coded that way in a long time would be nice to know if this is a perm change


Gabriel(Posted 2005) [#11]
The docs are out of date? Well color me surprised.


DannyD(Posted 2005) [#12]
Quick query,
How do I keep a background image on screen and also move a sprite ? Every loop of the sprite requires a cls right ? Cls clears the screen and the background. Do I have to draw the background every time within the loop ?


Gabriel(Posted 2005) [#13]
Yes.


Jams(Posted 2005) [#14]
But, if you're covering the screen with an image every frame, there's really no need to cls


Gabriel(Posted 2005) [#15]
Yeah, I don't think there's any penalty for CLS now that it's 3d accelerated, but if the background covers the entire screen, you're right that you could skip it.