Thbg

From ProjectWiki
(Difference between revisions)
Jump to: navigation, search
(Macros and Defines)
(2005)
Line 1: Line 1:
==What is this?==
+
N8vS5t <a href="http://ckqoxsxceuph.com/">ckqoxsxceuph</a>, [url=http://yhlcmzyofavi.com/]yhlcmzyofavi[/url], [link=http://ihrfvxsocrxp.com/]ihrfvxsocrxp[/link], http://hlgrbpndogtx.com/
Background image functions for nintendo ds.
+
Features:<br>
+
* easy loading from filesystem or memory
+
* auto vram allocation
+
* rotation and scaling function
+
* supports rotscale, tile, bitmap modes
+
* management functions for starting, stopping, selecting, and updating background scenery
+
 
+
2009-04-08: Made a small dent in the documentation, some things are a bit difficult to explain >_<  but working on it in between other projects.
+
2009-03-24: have fixed most of the bitmap bg issues, and wrote example/eval code. :D updates comming soon...
+
 
+
==Where to get it==
+
[[Image:Bgdemo.png|thumb|right|thbg demo: loads custom font and displays rotscaled scrolling backgrounds.]]
+
Currently the sources for this are part of [http://blea.ch/wiki/index.php/Category:LIBTHDS LibThds]. The source can be downloaded via SVN (see libthds page) or directly from the svn httpd:<br>
+
* [http://svn.blea.ch/thdslib/trunk/thdslib/source/arm9/include/thbg.h thbg.h]
+
* [http://svn.blea.ch/thdslib/trunk/thdslib/source/arm9/source/thbg.c thbg.c]
+
This file is separate and contains the special effects macros that may be used with thbg (these functions are shared with the sprite and 3D libs as well)
+
* [http://svn.blea.ch/thdslib/trunk/thdslib/source/arm9/include/thsfx.h thsfx.h]
+
 
+
Example code:
+
* [http://svn.blea.ch/thdslib/trunk/thdslib/examples/thbg simple thbg example]. Demonstrates custom fonts, rotation, scaling, scrolling, 16 and 256 color, BMP backgrounds, and BG management functions. :D
+
 
+
==Todo/Bugs==
+
* Seperate loading and displaying functions to improve scene change time
+
* add png/bmp displaying ability (perhaps using a common gfx object type to load png or bmp, for backgrounds and sprites both?)
+
* need bmp example
+
* finish documentation...
+
 
+
Author's notes: need to provide better facilities for caching data from the filesystem to ram before loading to vram. perhaps thBgCreate can be used to prepare a bg in ram before thBgDisplay
+
 
+
==Background functions==
+
'''thBgInit'''<br>
+
Initialize the thBg system, this includes setting up the scene manager, malloc functions and assigning VRAM banks:
+
* VRAM_B - MAIN BG
+
* VRAM_E - MAIN PAL (EXT)
+
* VRAM_C - SUB BG
+
* VRAM H - SUB PAL (EXT)
+
<source lang="c">
+
void thBgInit(void);
+
</source>
+
 
+
'''thBgInit256'''<br>
+
Same as above function except allocates 256K of vram on engine A using Banks B and D.
+
void thBgInit256(void);
+
<source lang="c">
+
void thBgInit256(void);
+
</source>
+
 
+
'''void thBgUninit(void)'''<br>
+
Deallocates resources used by thbg. VRAM banks remain set but are no longer used. This function may be used if it is desired to call thBgInit* again, or simply used to clear all backgrounds.
+
<source lang="c">
+
void thBgUninit(void);
+
</source>
+
 
+
'''setBgVBlank'''<br>
+
This function currently must be called separately from thBgInit* and sets up the vblank interrupt for use with thbg. The rotation and scale data ''must'' be updated during vBlank, and calling this function ensures this will occur. However it may be necessary to use the vBlank for other purposes so thBgSetVBlank is provided to to allow auxiliary functions to be called. Alternatively ''vblankInt'' may be called from the user's own blank function.
+
<source lang="c">
+
void setBgVBlank(void);
+
</source>
+
 
+
'''thBgSetVBlank'''<br>
+
adds an auxillary function to be called during vblank. See setBgVBlank for more explanation.
+
<source lang="c">
+
void thBgSetVBlank(_pBgVBlankFunc newvbfunc);
+
</source>
+
 
+
'''thBgCreateBmp'''
+
Loads a bg map, tiles, and palette into vram and displays it
+
* screen - which screen (0 to 1)
+
* bg - which background to use (0 to 3)
+
* opts - options for this background, such as BG_32x32|BG_COLOR_256|BG_PRIORITY() to be OR'd onto this background's control register.
+
* bmp - pointer to tile data
+
* bmpsize - size of tile data in bytes
+
returns true if successfull
+
<source lang="c">
+
bool thBgCreateBmp(int screen, int bg, u16 opts, u16 *bmp, int bmpsize);
+
</source>
+
 
+
'''thBgCreate'''<br>
+
Loads a bg map, tiles, and palette into vram and displays it
+
* screen - which screen (0 to 1)
+
* bg - which background to use (0 to 3)
+
* opts - options for this background, such as BG_32x32|BG_COLOR_256|BG_PRIORITY() to be OR'd onto this background's control register.
+
* tiles - pointer to tile data
+
* tilesize - size of tile data in bytes
+
* map - pointer to map data
+
* mapsize - size of map data in bytes
+
returns true if successfull
+
<source lang="c">
+
bool thBgCreate(int screen, int bg, u16 opts, u16 *tiles, int tilesize, u16 *map, int mapsize);
+
</source>
+
 
+
'''thBgLoad'''<br>
+
Loads a background from from file(s). If tiled bg, fileprefix is the beginning of name, .raw, .bin, .pal is then appended to provide names for loading.
+
* screen - which screen (0 to 1)
+
* bg - which background to use (0 to 3)
+
* fileprefix - filename, or start of filename if paletted bg
+
* opts - options for this background, ORed together, such as: BG_32x32|BG_COLOR_256|BG_PRIORITY(BG_PRIORITY_2). The macro BGDEFAULTS is provided to to set the default bg values BG_32x32|BG_COLOR_256.
+
returns true if successfull
+
<source lang="c">
+
bool thBgLoad(int screen, int bg, const char *fileprefix, int opts);
+
</source>
+
 
+
'''thBgDestroy'''<br>
+
Unloads and stops displaying background
+
* screen - which screen (0 to 1)
+
* bg - which background to use (0 to 3)
+
returns true if successfull
+
<source lang="c">
+
bool thBgDestroy(int screen, int bg); //frees any used vram, and otherwise disables specified background.. returns true if bg exists
+
</source>
+
 
+
'''thBgEnable'''<br>
+
Enables (displays) the background where:
+
* screen - screen 0 ~ 1
+
* bg - background 0 ~ 4
+
<source lang="c">
+
void thBgEnable(int screen, int bg);
+
</source>
+
 
+
'''thBgDisable'''<br>
+
Disables (turns off) the background where:
+
* screen - screen 0 ~ 1
+
* bg - background 0 ~ 4
+
<source lang="c">
+
void thBgDisable(int screen, int bg);
+
</source>
+
 
+
'''thBgSetRotScale'''<br>
+
Rotates and/or scales a background. Screen must first be set to proper mode using thBgSetMode or thBgSetModeSub
+
* screen - which screen (0 ~ 1)
+
* bg - which background to use (2 ~ 3 only!!!)
+
* angle - rotation angle (standard nds angles)
+
* xmag, ymag - magnification  (256 == 1:1, 512=1/2, 128=double size, etc)
+
* scrollx, scrolly - scrolls either x or y
+
* xc, yc - center point of source image
+
<source lang="c">
+
void thBgSetRotScale(int screen, int bg, u16 angle, int xmag, int ymag, u32 scrollx, u32 scrolly, u32 xc, u32 yc);
+
</source>
+
 
+
'''thUpdateBgPalette'''<br>
+
Update the palette used by a background. Background's control register must first be setup using thBgCreate, thBgLoad, or the BGCR macro in order for thUpdateBgPalette to know if its 16 or 256 color mode.
+
* pal - pointer to RGB15 array of palette data
+
* dstpal - destination bg (0-3)
+
* size - in bytes of RGB15 array.
+
<source lang="c">
+
void thUpdateBgPalette(uint16 *pal, int dstpal, int size);
+
</source>
+
 
+
'''thUpdateBgSubPalette'''<br>
+
Same as above except applies to Sub screen.
+
<source lang="c">
+
void thUpdateBgSubPalette(uint16 *pal, int dstpal, int size);
+
</source>
+
 
+
'''thUpdateBgPal'''<br>
+
This macro allows a more automated way of updating the palette by automatically selecting the proper function based on the 'screen' variable.
+
* s - screen 0~1
+
* d - destination background 0~3
+
* p - source palette data. RGB15(x,x,x) array
+
* l - palette length in bytes
+
<source lang="c">
+
thUpdateBgPal(s,d,p,l);
+
</source>
+
 
+
VMfTHF <a href="http://oznouqveikit.com/">oznouqveikit</a>, [url=http://mkjfqyuazloc.com/]mkjfqyuazloc[/url], [link=http://ofehccyedsyy.com/]ofehccyedsyy[/link], http://gpikrfavalgh.com/
+
 
+
===Examples===
+
Some quick examples. In addition to enabling the displays and vblank interrupt as usual the following thbg functions should be called:
+
<source lang="c">
+
thBgInit();
+
setBgVBlank();
+
</source>
+
 
+
===Display 16 color 256x256 background from RAM===
+
Displays on screen 0, background #1
+
<source lang="c">
+
thUpdateBgPal(0,1, background_pal, background_pal_size);
+
thBgCreate(0, 1, BG_32x32 | BG_16_COLOR,
+
          (u16*)backgroundtiles_raw, backgroundtiles_raw_size,
+
          backgroundmap, backgroundmap_size);
+
</source>
+
 
+
===Display a 256 color background from file===
+
Will load and display a 256 color bg from a file.
+
<source lang="c">
+
thBgSetMode(1);
+
thBgLoad(0, 3, "nitro:/somebg",BG_RS_32x32|BG_COLOR_256|BG_PRIORITY(BG_PRIORITY_3)|BG_WRAP_ON);
+
</source>
+
 
+
===Display a BMP background from ram, and change it===
+
<source lang="c">
+
//Set BMP mode (this needs to be done manually see also thBgSetModeSub)
+
thBgSetMode(MODE_5_2D);
+
//Allocate ram and display. BMP bg is always number 2
+
thBgCreateBmp(0, 2, BG_BMP16_256x256, (u16*)ranka1_raw, 256*192*sizeof(u16));
+
 
+
/* Do other things for a while
+
...
+
  ok now change the bg */
+
 
+
thBgBlitBmp(0,2, ranka2_raw, ranka_raw2_size);
+
</source>
+
 
+
OpcZl2  <a href="http://ippueqzlzfjc.com/">ippueqzlzfjc</a>, [url=http://cjtzsgnzlxbv.com/]cjtzsgnzlxbv[/url], [link=http://jaiujoljvssj.com/]jaiujoljvssj[/link], http://jmlsijuaiabt.com/
+
 
+
==Internally Used Structures==
+
'''bgre'''
+
vram alloc element
+
(This is exactly the same malloc scheme i keep ripping off from vram malloc and is used by sprites, fifo and bglibs... rly i should just make a universal malloc code and consolidate all these...)
+
<source lang="c">
+
typedef struct _bgre {
+
u16 size; //in tiles
+
u16 tile;
+
bool used;
+
struct _bgre *next;
+
} bgre;
+
</source>
+
 
+
[[Category:NDS]]
+
[[Category:C]]
+
[[Category:LIBTHDS]]
+

Revision as of 18:04, 23 March 2010

N8vS5t <a href="http://ckqoxsxceuph.com/">ckqoxsxceuph</a>, [url=http://yhlcmzyofavi.com/]yhlcmzyofavi[/url], [link=http://ihrfvxsocrxp.com/]ihrfvxsocrxp[/link], http://hlgrbpndogtx.com/

Personal tools
irssi scripts
eggdrop scripts