xLIBC tutorial

I've been looking around the forums to find a xLIBC tutorial, but I found only this, but that is only for drawing sprites, so I decided to create one. I will do this, because there are tons of questions about it, especially the DrawMap and DrawSprite routines.

I will first handle all these functions and after that I will create a game to make a good example.

For the people who found this useful: I am using DoorsCSE 8.2 and TokenIDE. There are probably more useful programs for that, but I chose these. I based my explanation on the xLIBC libraries wiki, so if you don't follow me, take a look there.

I'm going to start; if you don't have DoorsCSE 8.2 and TokenIDE installed, please do that first. Each function is supported by some images.
For all the colors xLIBC use, take a look here. If you have any question, feel free to ask!


For a tutorial how to make sprites and a tilemap, please look at my next post.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
real(0 - xLIBCSetup

The basic of the the xLIBC functions is the half-resolution. This works as follows: the screen as you normally see will being splitted in two halves. One half would be stretched, the other half you don't see, that would being our buffer. That would look like this:

As you can see, there is no second half.
Very important! All the xLIBC functions expected to be in half-res mode!

These are the functions of real(0 :

GetXLIBCVersion
Code:
- real(0,0) -> returns the version of xLIBC in Ans, for me it returns 2.

SetupGraphics

Code:
- real(0,1,X)
  - real(0,1,0,X)
    - real(0,1,0,0) -> this returns to the normal mode, WITHOUT redrawing the status bar! This might being useful for some games, which are not in the half-res mode, but do not want to see the status bar.
    - real(0,1,0,1) -> this returns to the normal mode, and redraws the statusbar.
  - real(0,1,1) -> this enables the half-res mode, as you can see in the previous image.

Very important! Don't forget to switch the half-res mode off, or the TIOS will stuck in this mode!


SetSpeed

Code:
- real(0,2,X)
  - real(0,2,0) -> Disable fast (15MHz) mode, switching to 6MHz mode
  - real(0,2,1) -> Enable fast (15MHz) mode

Note: the default CPU speed is 15MHz. Try it, and see the difference.

SetupColorMode

Code:
- real(0,3,X)
  - real(0,3,0) -> enables all the possible colors in xLIBC
  - real(0,3,1) -> enables 8COLOUR - I have no idea what changes with this.
  - real(0,3,2) -> inverts the screen, like black->white and white->black. It gets each color value per pixel, and then 255-(color_value) would be the new color value. To restore it back, invert it again, or ->
  - real(0,3,3) -> restores the colors back to normal, useful for moments if you do not know whether the colors are inverted or not.
  - real(0,3,4,(color_value),(update_LCD)) -> fills the active half of the screen with one color from the color palette, useful for backgroud. I will explain the update_LCD later.
  - real(0,3,5,(color_value)) -> I cannot get this working, but the wiki says that it will change the color value per pixel with shapes and sprites



---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
real(1 - UserVariables

xLIBC has the facility to utilize 'internal user variables' for data storage and calculations as opposed to using TI-OS variables. They work exactly the same, but the main difference is, is that they are faster, and temporary, which means that you cannot 'save' them for other programs. Some xLIBC functions expected (only) UserVars, so it is always handy to learn and use these.

These are the functions of real(1:

GetUservar

Code:
real(1,0,(uservar_num))
This returns the value of the UserVar in Ans

SetUservar

Code:
real(1,1,(uservar_num),(value))
This function writes (value) to UserVar

AddToUservar

Code:
real(1,2,(uservar_num),(value))
This function add (value) to UserVar

SubFromUservar

Code:
real(1,3,(uservar_num),(value))
This function subtracts (value) to UserVar

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
real(2 - GetKey

These routines have been significantly overhauled from the TI-BASIC getKey function and are even more comprehensive.

These are the functions of real(2:

GetKey

Code:
real(2,0,0)
Key code stored in Ans
Very important! These values are NOT the same as the values of getKey, but it returns the values of _GetCSC:


GetKeyCheckList

Code:
real(2,0,1,(getkey_checknum),(getkey_keyvalue),(getKey_uservar),(getkey_value),... )
This updates (getkey_uservar) by (getkey_value) if you pressed (getkey_keyvalue). (getkey_checknum) is the amount of UserVars to update. Useful for counters or sprites.

GetKeyArrows

Code:
real(2,1,(uservar_x),(uservar_y),(value_x),(value_y))
This updates (uservar_x) and (uservar_y) with respective (value_x) and (value_y) if you pressed any arrow.

If up is pressed then USERVAR_Y = USERVAR_Y - VALUE_Y
If down is pressed then USERVAR_Y = USERVAR_Y + VALUE_Y
If left is pressed then USERVAR_X = USERVAR_X - VALUE_X
If right is pressed then USERVAR_X = USERVAR_X + VALUE_X

GetKeyArrowsDiagonals

Code:
real(2,2,(uservar_x),(uservar_y),(value_x),(value_y))
This works the same as GetKeyArrows, with the exception that a combination of the arrows also will update the UserVars.

If up is pressed then USERVAR_Y = USERVAR_Y - VALUE_Y
If down is pressed then USERVAR_Y = USERVAR_Y + VALUE_Y
If left is pressed then USERVAR_X = USERVAR_X - VALUE_X
If right is pressed then USERVAR_X = USERVAR_X + VALUE_X
If up+left is pressed then USERVAR_Y = USERVAR_Y - VALUE_Y, USERVAR_X = USERVAR_X - VALUE_X
If up+right is pressed then USERVAR_Y = USERVAR_Y - VALUE_Y, USERVAR_X = USERVAR_X + VALUE_X
If down+left is pressed then USERVAR_Y = USERVAR_Y + VALUE_Y, USERVAR_X = USERVAR_X - VALUE_X
If down+right is pressed then USERVAR_Y = USERVAR_Y + VALUE_Y, USERVAR_X = USERVAR_X + VALUE_X

GetKeyArrowsCheckTile

Code:
real(2,3,(uservar_x),(uservar_y),(value_x),(value_y),(uservar_mapwidth),(collisiontile),(mapstring),(x_left),(y_up),(x_right),(y_bottom))
This routine will only update the UserVars with their values if the move is to a walkable tile (see real(3) - DrawMap). You must specify an UserVar with the width of the map (that is often 20, 160/Cool. It checks for a box from (X_left, Y_top) to (X_right, Y_bottom). For a single sprite of 8x8 pixels, that would be (X_sprite+0, Y_sprite+0, X_sprite+7, Y_sprite+7).

GetKeyArrowsDiagonalsCheckTile

Code:
- real(2,4,(uservar_x),(uservar_y),(value_x),(value_y),(uservar_mapwidth),(collisiontile),(mapstring),(x_left),(y_up),(x_right),(y_bottom)) -> same as above, now with diagonals.


GetKeyArrowsCheckTileList

Code:
real(2,5,(uservar_x),(uservar_y),(value_x),(value_y),(uservar_mapwidth),(collisiontile),(mapstring),(x_left),(y_up),(x_right),(y_bottom))
It works the same as real(2,3....), but now it will return information regarding any keypress and any collided tiles in a 'real list' contained in a user-defined list named "XL".
XL have the following format:
{key_press , num_collided_tiles , collided_tiles_list}
key_press returns ~1 for noarrow, 0 for up, 1 for down, 2 for left and 3 for right.
num_collided_tiles is the number of tiles collided against
collided_tiles_list is a list of collided tiles against.

GetKeyArrowsDiagonalsCheckTileList

Code:
- real(2,6,(uservar_x),(uservar_y),(value_x),(value_y),(uservar_mapwidth),(collisiontile),(mapstring),(x_left),(y_up),(x_right),(y_bottom))
Same as above, now with diagonals.

All of these routines are pretty useful, for smarter and better movement.


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
real(3 - DrawMap

There exist many games which have a background. That background can't you (often) make with only shapes, that is why there DrawMap exist. This routine displays a bunch of sprites, which would being a fixed background.

These are the functions of real(3:

DrawMapA (TI-OS Values)

Code:
real(3,0,(x),(y),(mapwidth),(mapstring),(x_start),(y_start),(x_end),(y_end),(update_lcd))
This routine draws a tilemap at position (X,Y). Mapwidth is the count of sprites next to each other, counted in blocks of 8x8, which is often 20 (160/Cool. The height will be automatic calculated. Mapstring is the NUMBER of string which contains the tilemap data. For Str9, it will be 9. If you don't want do draw the whole background, you can specify the bounds of drawing, by changing x_start, y_starat, x_end and y_end. With these values you can display a specific box instead of the whole background.

DrawMapB (Uservar Values)

Code:
real(3,1,(uservar_x),(uservar_y),(uservar_mapwidth),(mapstring),(x_start),(y_start),(x_end),(y_end),(update_lcd))
This is the same routine as above, but now instead of TI-OS values, like A or G, it takes UserVars as the X- and Y-position. And the mapwidth must be stored to an UserVar.

DrawMap_GetTileA (TI-OS Values)

Code:
real(3,2,(x),(y),(mapwidth),(mapstring),(x_offset),(y_offset))
This routine gets the tile at position (X,Y). Mapwidth and mapstring works the same way as real(3,0). Most people don't use xOffset and yOffset, just put 0 there.
Very important! X and Y must be in pixels, not in blocks of 8x8!!

DrawMap_GetTileB (Uservar Values)

Code:
real(3,3,(uservar_x),(uservar_y),(uservar_mapwidth),(mapstring),(x_offset),(y_offset))
Same as above, now with UserVars. Also here are uservar_x and uservar_y in pixels.

DrawMap_SetTile (TI-OS Values)

Code:
real(3,4,(x),(y),(mapwidth),(mapstring),(tile_id),(x_offset),(y_offset))
This routine replaces a tile at (X,Y) width tile tile_id.
Very important! X and Y must be in blocks of 8x8!!

DrawMap_ReplaceTile (TI-OS Values)

Code:
real(3,5,(mapstring),(checknum),(tile_id_0),(replace_tile_id_0),(tild_id_1),....)
This routine can replace very fast multiple tiles, instead of one with real(3,4). Checknum is the amount of tiles you want to replace. All the tiles with tile_id_X are replaced with replace_tile_id_X.
->
I replaced only one tile_id, here are the results. Note: you must redraw the map to actually see it. My code was

Code:
real(3,5,9,1,5,2


real(3,6 - real(3,9 are coming soon!

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
real(4 - DrawSprite

Almost every game will use sprites, which are easy to display and move on the screen. That is why it is important to understand this right, that is always useful.
Very important! You must load tiledata into temp memory before drawing tilemaps. See real(5).

These are the functions of real(4:

DrawSpriteA (TI-OS Values)

Code:
real(4,0,(x),(y),(width),(height),(x_offset),(y_offset),(transindex),(update_lcd),(pic_index_start),(pic_index_0),(pic_index_1),...)

This routine draws a sprite at (X,Y) with a width and height of WIDTH*HEIGHT 8x8 blocks. xOffset and yOffset are unused, so set these always to 0. Transindex means the color of the color palette, that every pixel that matches that color, will not being drawn. This is very useful for sprites which are not in blocks of 8x8. Update_LCD will I explain later. Then follows pic_index_N. Note that N must be equal to WIDTH*HEIGHT. Here you must enter a list of argument(s) for each index of the sprite (in 8x8 chunks). Pic_index_start equals the number, from where the list of argument(s) will start.
Very important! The sprites draws *column* first. So a 16x16 sprite will look like

Code:
---------
| 1 | 3 |
---------
| 2 | 4 |
---------
and a 16x24
Code:
---------
| 1 | 4 |
---------
| 2 | 5 |
---------
| 3 | 6 |
---------
For the actual tile-index, look at my next post, at the location "Some general information". This routine has even an addition: it can display a sprite based on a variable. You can use the PICINDEXSTART argument to specify which sprite to draw out of a list of sprites. This can be useful for drawing a different sprite depending on a direction variable. For example drawing an 8x8 sprite where pic index 10=up, 11=down, 12=left & 13=right, and you have a direction variable "A" which holds 0=up, 1=down, 2=left & 3=right you can do:

Code:
real(4,0,(some stuff),...,A,10,11,12,13
This will select index 10 if A=0, 11 if A=1, 12 if A=2 & 13 if A=3 and so on.

DrawSpriteB (Uservar Values)

Code:
real(4,1,(uservar_x),(uservar_y),(width),(height),(x_offset),(y_offset),(transindex),(update_lcd),(pic_index_start),(pic_index_0),(pic_index_1),...)
This works exactly the same as above, but now takes uservar_x and uservar_y as (X,Y).


...will be continued...
In this post I will describe how to create sprites and/or a tilemap.
First you absolutely need is TokenIDE. I will make screenshots if it's too difficult to explain in words.

--------------------------------------------------------------------------------------------------------------------------------------------------------------
How to make sprites
1) Open TokenIDE
2) Open the Hex Sprite Editor by pressing Ctrl+Shift+H or click on Tools > Hex Sprite Editor > Color Sprites.
3) Set the width to 128 and the height to 64, zoom to 7, and select the "Draw Grid" checkbox. From the dropdown-menu, select the option "xLIBC".
For me it looks like this:



4) Create some sprites, by selecting a color and painting in the editor. This could be anything, but it would be nice, if the width and height are products of 8.



5) Save the sprites, by pressing Ctrl+S or click on File > Save (as). Be sure to save this as "xLIBC Tiles", and name it anything you want.


Now you're ready to use all the sprites in your program. Just send this appvar, name_of_appvar.8xv to you calculator, and voilĂ .

Some general information:
When displaying sprites, you should know the tile-index. The numbers correspond to each 8x8 square.


If I take my sprites, then my happy face is tile index 0, the large circle 1, and my rectangle 5.


--------------------------------------------------------------------------------------------------------------------------------------------------------------
How to make a tilemap

1) Open the xLIBC Map Editor, by pressing Ctrl+M or click on Tools > xLIBC Map Editor.
2) Add Tiles, by pressing Ctrl+O or click on File > Add Tiles. Select your spritesheet.
3) Set zoom to whatever you want, and select the "Draw Grid" checkbox.
For me it looks like this:



4) Make any tilemap you want, by selecting a sprite and paint the editor with that sprite. I will make this:



5) When you are ready, import and exit, by pressing Ctrl+I or click on File > Import and Exit. You will see now a bunch of characters in the TokenIDE editor. This is very important. Copy this to your program, this is your tilemap-data, which is necessary for real(3 - DrawMap (see my previous post).

Good luck with this, and if you have any questions, feel free to ask!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement