Hello everyone!
I'm teaching myself GLbasic and thought it would be helpful to post my progress with examples so others can learn it too. For anyone that doesn't know, GLbasic is a B.A.S.I.C. based language that is used with a compiler and tools that can output applications, specifically video games, to multiple platforms including the GP2X and Wiz. GLbasic also can produce 3D games and 3D is supported by GLbasic for the wiz.
I'm writing this in a way that even someone with no knowledge in programming can learn GLbasic.
My first attempt at programming in GLbasic is simply the loading of a background image and one sprite in the center, using the left and right D-pad buttons to rotate the sprite. The start button is used to exit (menu button on the wiz). The entire code:
We declare variables with LET. Although, typing LET before ang=0 is optional.
LOADBMP loads a bitmap for use as the background, and paints it to the second screen thats hidden to the user before you use the SHOWSCREEN command that switches the screens and shows the changes made. So simply put, your program does all it's work hidden on the second screen or buffer, and then when all work is done, you use the SHOWSCREEN to display the changes. I made the graphic to the dimensions of the Wiz's screen: 320 by 240 pixels.
LOADSPRITE is similar to LOADBMP but it doesn't automatically draw to screen, but is necessary to load the image. The usage of LOADSPRITE is:
Where 0 is the Sprite ID. You give each sprite you are working with a unique ID. You can use bitmaps too. I just use the png's out of laziness as png's have the alpha channel for transparency. bitmaps can have transparency in GLbasic too, you just have to use the special color to fill the transparent area(Hex: FF 00 80) (Pink).
You then need to use the DRAWSPRITE command to place the sprite where you want it. Afterward, you reuse this command each time you would like to move the sprite if it doesn't need to be rotated or zoomed. Usage:
0 being where you place the sprite ID of the sprite you want to place or move and of course, x and y are the coordinates of the top left corner of the sprite along the surface of the grid. In GLbasic, the wiz's screen is divided into an x y grid. (and you thought middle school math would never be needed! ). Top left corner of the screen is 0, 0. The bottom right is 320, 240.
The SHOWSCREEN command, which is used to show visual changes made hidden from the user. At this point in the code, we use SHOWSCREEN to show the initial state of the game. Without this, the user would only see a blank black screen. Be watch full of where you place this command as using it more than is needed or putting it in an awkward place in your code may produce odd results. This is mostly a concern as your games get more and more complicated code-wise.
presses: is simply a name used at the beginning of a subroutine, but in the form of the old-school GOTO command. This is now of course made obsolete by functions but in this simple example, an oldie but goodie GOTO subroutine will suffice. Take note that at the end of the name a regular colon is used NOT a semi colon. Also names of subroutines cannot be the same as commands.
Next, I am using the KEY() command with the IF command to check for key presses of specific keys or buttons.
the number 28 is the key code for the menu button on the wiz. 01 is the key code for the ESC key on the PC. This helps for quick testing before the game is finished for the wiz. "IF KEY(01) = 1;" the 1 means true in this example. The semi colon separates the IF command and the next part that is executed if the IF statement is true (when code is written all on the same line). Then I use the END command which ends and exits the game. ENDIF is used to show the end of the IF statement's code.
// at the beginning of a line is used for comments. Putting comments in your code is a good way to remember what each part does and also helps another person understand your code if you have other people working with you or if a project of yours is passed to someone else. You don't have to go overboard, I know some programmers secretly wished they were novel writers and compensate with writing too many comments! To the rest of us, it might seem silly but it really is helpful.
I then check for the key presses of the left arrow and right arrow...
If the left arrow (203) is pressed, increase the ang variable by 1, if the right arrow (205) is pressed, decrease the variable ang by 1. As you might have noticed, I'm using the variable name "ang" as it is short for the word "angle." Any variable name can be used as long is it isn't the same as a command.
Next we can use that ang variable in the ROTOSPRITE command which is a variation of DRAWSPRITE with the ability to rotate the sprite. 0 being the ID of the sprite you're wanting to rotate, x and y being it's coordinates or location, and lastly the angle at which you want the sprite rotated to.
Next is the SHOWSCREEN command that will be used repeatedly after work is done. If this example were developed further, I would evoke the SHOWSCREEN command only when changes are actually made, and unused when visual changes aren't happening. I would do this by including it within the IF statements like this:
And finally, as I am sure you've realized on your own, the GOTO command is used to return to the beginning of the presses: subroutine, completing the continuous loop of the main program.
I hope you find my example helpful! This is literally my first program written in GLbasic.
Once you get familiar with the commands, learn their usage (syntax) and get your feet wet with your own simple programs that experiment with the commands, you'll be learning GLbasic in no time.
I'm going to put up more examples soon and I hope others can contribute there's too.
-Johnnie
I'm teaching myself GLbasic and thought it would be helpful to post my progress with examples so others can learn it too. For anyone that doesn't know, GLbasic is a B.A.S.I.C. based language that is used with a compiler and tools that can output applications, specifically video games, to multiple platforms including the GP2X and Wiz. GLbasic also can produce 3D games and 3D is supported by GLbasic for the wiz.
I'm writing this in a way that even someone with no knowledge in programming can learn GLbasic.
My first attempt at programming in GLbasic is simply the loading of a background image and one sprite in the center, using the left and right D-pad buttons to rotate the sprite. The start button is used to exit (menu button on the wiz). The entire code:
Code:
LET ang=0
LOADBMP "enders.bmp"
LOADSPRITE "firefox.png", 0
DRAWSPRITE 0, 96, 56
SHOWSCREEN
presses:
IF KEY(28) = 1; END //WIZ ESC
ENDIF
IF KEY(01) = 1; END //PC ESC
ENDIF
// DPAD movement
IF KEY(203) = 1; ang=ang+1 //left
ENDIF
IF KEY(205) = 1; ang=ang-1 //right
ENDIF
ROTOSPRITE 0, 96, 56, ang
SHOWSCREEN
GOTO presses;
We declare variables with LET. Although, typing LET before ang=0 is optional.
Code:
LET ang=0
LOADBMP loads a bitmap for use as the background, and paints it to the second screen thats hidden to the user before you use the SHOWSCREEN command that switches the screens and shows the changes made. So simply put, your program does all it's work hidden on the second screen or buffer, and then when all work is done, you use the SHOWSCREEN to display the changes. I made the graphic to the dimensions of the Wiz's screen: 320 by 240 pixels.
Code:
LOADBMP "enders.bmp"
LOADSPRITE is similar to LOADBMP but it doesn't automatically draw to screen, but is necessary to load the image. The usage of LOADSPRITE is:
Code:
LOADSPRITE "filename.png", 0
Where 0 is the Sprite ID. You give each sprite you are working with a unique ID. You can use bitmaps too. I just use the png's out of laziness as png's have the alpha channel for transparency. bitmaps can have transparency in GLbasic too, you just have to use the special color to fill the transparent area(Hex: FF 00 80) (Pink).
You then need to use the DRAWSPRITE command to place the sprite where you want it. Afterward, you reuse this command each time you would like to move the sprite if it doesn't need to be rotated or zoomed. Usage:
Code:
DRAWSPRITE 0, x, y
0 being where you place the sprite ID of the sprite you want to place or move and of course, x and y are the coordinates of the top left corner of the sprite along the surface of the grid. In GLbasic, the wiz's screen is divided into an x y grid. (and you thought middle school math would never be needed! ). Top left corner of the screen is 0, 0. The bottom right is 320, 240.
The SHOWSCREEN command, which is used to show visual changes made hidden from the user. At this point in the code, we use SHOWSCREEN to show the initial state of the game. Without this, the user would only see a blank black screen. Be watch full of where you place this command as using it more than is needed or putting it in an awkward place in your code may produce odd results. This is mostly a concern as your games get more and more complicated code-wise.
presses: is simply a name used at the beginning of a subroutine, but in the form of the old-school GOTO command. This is now of course made obsolete by functions but in this simple example, an oldie but goodie GOTO subroutine will suffice. Take note that at the end of the name a regular colon is used NOT a semi colon. Also names of subroutines cannot be the same as commands.
Code:
presses:
Next, I am using the KEY() command with the IF command to check for key presses of specific keys or buttons.
Code:
IF KEY(28) = 1; END //WIZ ESC
ENDIF
IF KEY(01) = 1; END //PC ESC
ENDIF
the number 28 is the key code for the menu button on the wiz. 01 is the key code for the ESC key on the PC. This helps for quick testing before the game is finished for the wiz. "IF KEY(01) = 1;" the 1 means true in this example. The semi colon separates the IF command and the next part that is executed if the IF statement is true (when code is written all on the same line). Then I use the END command which ends and exits the game. ENDIF is used to show the end of the IF statement's code.
// at the beginning of a line is used for comments. Putting comments in your code is a good way to remember what each part does and also helps another person understand your code if you have other people working with you or if a project of yours is passed to someone else. You don't have to go overboard, I know some programmers secretly wished they were novel writers and compensate with writing too many comments! To the rest of us, it might seem silly but it really is helpful.
Code:
// DPAD movement
I then check for the key presses of the left arrow and right arrow...
Code:
IF KEY(203) = 1; ang=ang+1 //left
ENDIF
IF KEY(205) = 1; ang=ang-1 //right
ENDIF
If the left arrow (203) is pressed, increase the ang variable by 1, if the right arrow (205) is pressed, decrease the variable ang by 1. As you might have noticed, I'm using the variable name "ang" as it is short for the word "angle." Any variable name can be used as long is it isn't the same as a command.
Next we can use that ang variable in the ROTOSPRITE command which is a variation of DRAWSPRITE with the ability to rotate the sprite. 0 being the ID of the sprite you're wanting to rotate, x and y being it's coordinates or location, and lastly the angle at which you want the sprite rotated to.
Code:
ROTOSPRITE 0, 96, 56, ang
Next is the SHOWSCREEN command that will be used repeatedly after work is done. If this example were developed further, I would evoke the SHOWSCREEN command only when changes are actually made, and unused when visual changes aren't happening. I would do this by including it within the IF statements like this:
Code:
IF KEY(203) = 1; ang=ang+1 //left
ROTOSPRITE 0, 96, 56, ang;
SHOWSCREEN
ENDIF
IF KEY(205) = 1; ang=ang-1 //right
ROTOSPRITE 0, 96, 56, ang
SHOWSCREEN
ENDIF
And finally, as I am sure you've realized on your own, the GOTO command is used to return to the beginning of the presses: subroutine, completing the continuous loop of the main program.
Code:
GOTO presses;
I hope you find my example helpful! This is literally my first program written in GLbasic.
Once you get familiar with the commands, learn their usage (syntax) and get your feet wet with your own simple programs that experiment with the commands, you'll be learning GLbasic in no time.
I'm going to put up more examples soon and I hope others can contribute there's too.
-Johnnie