PAlib: PA_BgTiles.h Source File

Main Page | Modules | File List | Globals | Search for

# PA_BgTiles.h

Go to the documentation of this file.

    00001 #ifndef _PA_BGTILES
    00002 #define _PA_BGTILES
    00003
    00004 //#include <PA9.h>
    00005 #include <nds.h>
    00006 #include "PA_General.h"
    00007
    00008
    00009 // Extracts the size from PAGfx convertions
    00010 extern inline u8 PA_GetPAGfxBgSize(u16 width, u16 height)
    00011 {
    00012        u8 bg_size = 0;
    00013        if (height == 512)   bg_size = 2;
    00014        if (width == 512)    bg_size ++;
    00015        return bg_size;
    00016 }
    00017
    00018 // Extracts the size from PAGfx convertions
    00019 extern inline u8 PA_GetPAGfxRotBgSize(u16 width)
    00020 {
    00021        u8 bg_size = 1; //256x256 by default
    00022        if (width <= 128)    bg_size = 0;
    00023        else if (width <= 256)      bg_size = 1;
    00024        else if (width <= 512)      bg_size = 2;
    00025        else if (width <= 1024) bg_size = 3;
    00026
    00027        return bg_size;
    00028 }
    00029
    00039
    00040 // Système de Tiles
    00042
    00043
    00044 //background memory offset macros
    00045 #define CharBaseBlock(screen, n) (((n)*0x4000) + 0x6000000 + (0x200000 *  screen))
    00046 #define ScreenBaseBlock(screen, n) (((n)*0x800) + 0x6000000 + (0x200000 *  screen))
    00047 extern u32 PA_bgmap[2][4]; // Pointeur vers les maps, 4 maps par screen
    00048 extern u8 tilesetchar[2][4];
    00049
    00050 // Quantité de données à charger en fonction de la taille de la map...
    00051 extern u16 bg_sizes[4];
    00052 extern u8 bg_place[4];
    00053
    00054 extern u16 *PA_DrawBg[2]; // Fond dessinable
    00055
    00056
    00057 extern u8 charblocks[2][70];
    00058 extern u16 tilesetsize[2][4]; // Place utilisée pour chaque tileset
    00059 extern u16 mapsize[2][4]; // Place utilisée pour chaque map
    00060 extern u8 tilesetchar[2][4];  // Emplacement mémoire de chaque tileset
    00061 extern u8 mapchar[2][4];  // Emplacement mémoire de chaque map
    00062
    00063 extern u8 charsetstart[2];
    00064
    00065
    00066
    00067
    00068
    00069 extern s32 PA_parallaxX[2][4];
    00070 extern s32 PA_parallaxY[2][4];
    00071
    00072
    00073
    00074
    00075 typedef struct {
    00076        u8 infscroll; // Passe à 1 si scrolling infini activé...
    00077        u16* bg_map; // Map
    00078 //     s32 x, y;
    00079        s32 lx, ly;
    00080        s32 scrollx, scrolly;
    00081        s32 maxx; // 32 ou 64, en fonction de la largeur...
    00082 //     s32 startx, starty;
    00083 }scrollpositions;
    00084
    00085 extern scrollpositions scrollpos[2][4]; // Pour chaque écran et pour chaque fond :)
    00086
    00087
    00088
    00089
    00090
    00091
    00092 // Mode de couleur
    00093 #define BG_COLOR16 0x00
    00094 #define BG_COLOR256 0x80
    00095
    00096
    00097 #define CHAR_SHIFT 2
    00098 #define SCREEN_SHIFT 8
    00099 #define WRAPAROUND 0x1
    00100
    00101 #define SCREEN_TILES 24576  // Taille à convertir pour avoir un écran complet...
    00102
    00103
    00104 //scrolling registers for background 0
    00105
    00106
    00107 // Registre pour les controles de chaque écran
    00108
    00109 #define REG_BGSCREEN0  0x04000000
    00110 #define REG_BGSCREEN1  0x04001000
    00111 #define REG_BGSCREEN(screen) (0x04000000 + (screen * 0x1000))
    00112
    00113 #define REG_BGCNT(screen, bg_number) (0x4000008 + (screen * 0x1000) + (bg_number << 1))
    00114
    00115 // Premier registre pour les scroll. On en déduit les autres en ajoutant pour le screen et le numéro du Bg
    00116 #define REG_BGSCROLLX 0x4000010
    00117 #define REG_BGSCROLLY 0x4000012
    00118
    00119
    00120 // Tailles pour les Bg :
    00121 #define BG_256X256 0
    00122 #define BG_512X256 1
    00123 #define BG_256X512 2
    00124 #define BG_512X512 3
    00125
    00126
    00127
    00128 //ALL_BUT() pour faire tout sauf...
    00129 #define TILE_N 1023
    00130 #define TILE_PAL 61440
    00131 #define TILE_HFLIP 1024
    00132 #define TILE_VFLIP 2048
    00133
    00134
    00135
    00136
    00137
    00149 void PA_ResetBgSys(void);
    00150
    00173 void PA_InitBg(u8 screen, u8 bg_select, u8 bg_size, u8 wraparound, u8 color_mode);
    00174 //#define PA_InitBg(screen, bg_select, bg_size, wraparound, color_mode) {    PA_bgmap[screen][bg_select] = ScreenBaseBlock(screen, ((bg_select + 1) << 3) - bg_place[bg_size]);_REG16(REG_BGSCREEN(screen)) |= (0x100 << (bg_select)); _REG16(REG_BGCNT(screen, bg_select)) = ((bg_select) | (bg_size << 14) |((((bg_select + 1) << 3) - bg_place[bg_size]) << SCREEN_SHIFT) | (wraparound << 13) | ((bg_select) << 2)) + (color_mode << 7);}
    00175
    00176
    00189 #define PA_HideBg(screen, bg_select) _REG16(REG_BGSCREEN(screen)) &= ~(0x100 << (bg_select))
    00190
    00191
    00204 #define PA_ShowBg(screen, bg_select) _REG16(REG_BGSCREEN(screen)) |= (0x100 << (bg_select))
    00205
    00206
    00216 #define PA_ResetBg(screen) _REG16(REG_BGSCREEN(screen)) &= ~(0xF00)
    00217
    00218
    00219
    00235 #define PA_LoadBgTiles(screen, bg_select, bg_tiles) PA_LoadBgTilesEx(screen, bg_select, (void*)bg_tiles, SIZEOF_16BIT(bg_tiles))
    00236 //#define PA_LoadBgTiles(screen, bg_select, bg_tiles) {DMA_Copy((void*)(bg_tiles), (void*)CharBaseBlock(screen, bg_select),SIZEOF_16BIT(bg_tiles), DMA_16NOW);}
    00237
    00238
    00258 void PA_LoadBgTilesEx(u8 screen, u8 bg_select, void* bg_tiles, u16 size);
    00259
    00260
    00276 void PA_ReLoadBgTiles(u8 screen, u8 bg_select, void* bg_tiles);
    00277
    00278
    00291 void PA_DeleteTiles(u8 screen, u8 bg_select);
    00292
    00293
    00306 void PA_DeleteMap(u8 screen, u8 bg_select);
    00307
    00308
    00309
    00322 extern inline void PA_DeleteBg(u8 screen, u8 bg_select) {
    00323        PA_HideBg(screen, bg_select);
    00324        PA_DeleteTiles(screen, bg_select);
    00325        PA_DeleteMap(screen, bg_select);
    00326 }
    00327
    00347 void PA_LoadBgMap(u8 screen, u8 bg_select, void* bg_map, u8 bg_size);
    00348
    00349
    00350
    00351
    00352
    00369 #define PA_LoadTiledBg(screen, bg_number, bg_name){\
    00370        PA_LoadBgPal(screen, bg_number, (void*)bg_name##_Pal); \
    00371        PA_LoadSimpleBg(screen, bg_number, bg_name##_Tiles, bg_name##_Map, PA_GetPAGfxBgSize(bg_name##_Width, bg_name##_Height), 0, 1);}
    00372
    00373
    00374
    00375
    00404 #define PA_LoadSimpleBg(screen, bg_select, bg_tiles, bg_map, bg_size, wraparound, color_mode) {\
    00405 PA_DeleteBg(screen, bg_select);\
    00406 PA_LoadBgTiles(screen, bg_select, bg_tiles); \
    00407 PA_LoadBgMap(screen, bg_select, (void*)bg_map, bg_size); \
    00408 PA_InitBg(screen, bg_select, bg_size, 0, color_mode);\
    00409 PA_BGScrollXY(screen, bg_select, 0, 0);}
    00410
    00411
    00412
    00413
    00445 #define PA_LoadBg(screen, bg_select, bg_tiles, tile_size, bg_map, bg_size, wraparound, color_mode) {\
    00446 PA_LoadBgTilesEx(screen, bg_select, (void*)bg_tiles, tile_size); \
    00447 PA_LoadBgMap(screen, bg_select, (void*)bg_map, bg_size); \
    00448 PA_InitBg(screen, bg_select, bg_size, 0, color_mode);\
    00449 PA_BGScrollXY(screen, bg_select, 0, 0);}
    00450
    00451
    00452
    00468 #define PA_BGScrollX(screen, bg_number, x) _REG16(REG_BGSCROLLX + ((screen) * 0x1000) + ((bg_number) << 2)) = (x)&1023
    00469
    00485 #define PA_BGScrollY(screen, bg_number, y) _REG16(REG_BGSCROLLY + ((screen) * 0x1000) + ((bg_number) << 2)) = (y)&1023
    00486
    00487
    00488
    00489
    00508 #define PA_BGScrollXY(screen, bg_number, x, y) {PA_BGScrollX(screen, bg_number, x); PA_BGScrollY(screen, bg_number, y);}
    00509
    00510
    00511
    00512
    00534 extern inline void PA_SetMapTile(u8 screen, u8 bg_select, s16 x, s16 y, s16 tile_number) {
    00535 *(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) &= ~(TILE_N);
    00536 *(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) |= ((tile_number)&TILE_N);
    00537 }
    00538
    00539
    00561 #define PA_SetMapTileAll(screen, bg_select, x, y, tile_info) *(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) = (tile_info)
    00562
    00563
    00564
    00565
    00566
    00588 extern inline void PA_SetLargeMapTile(u8 screen, u8 bg_select, s32 x, s32 y, u32 tile_info) {
    00589        u32 truex;
    00590        u32 mapblock = 0;
    00591        truex = x&31;
    00592        mapblock = (x >> 5) << 11; // Permet d'avoir le bon block...
    00593
    00594        *(u16*)(PA_bgmap[screen][bg_select] + ((truex) << 1) + ((y) << 6) + mapblock) = tile_info;
    00595 }
    00596
    00597
    00598
    00620 #define PA_SetMapTileHflip(screen, bg_select, x, y, hflip) {*(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) &= ALL_BUT(TILE_HFLIP); *(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) |= ((hflip) << 10);}
    00621
    00622
    00644 #define PA_SetMapTileVflip(screen, bg_select, x, y, vflip) {*(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) &= ALL_BUT(TILE_VFLIP); *(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) |= ((vflip) << 11);}
    00645
    00646
    00668 #define PA_SetMapTilePal(screen, bg_select, x, y, palette_number) {*(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) &= ALL_BUT(TILE_PAL); *(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) |= ((palette_number) << 12);}
    00669
    00670
    00701 extern inline void PA_SetMapTileEx(u8 screen, u8 bg_select, s16 x, s16 y, u16 tile_number, u8 hflip, u8 vflip, u8 palette_number) {
    00702        *(u16*)(PA_bgmap[screen][bg_select] + ((x) << 1) + ((y) << 6)) = (tile_number) + ((hflip) << 10) + ((vflip) << 11) + ((palette_number) << 12);
    00703 }
    00704
    00705
    00706
    00707
    00708
    00709
    00710
    00711
    00712
    00713
    00729 extern inline void PA_SetBgPrio(u8 screen, u8 bg, u8 prio) {
    00730        _REG16(REG_BGCNT(screen, bg)) &= ~3;
    00731        _REG16(REG_BGCNT(screen, bg)) |= prio;
    00732 }
    00733
    00734 extern inline void PA_CreateBgFromTiles(u8 screen, u8 bg_select, u8 bg_tiles, void *bg_map, u8 bg_size){
    00735 PA_LoadBgMap(screen, bg_select, bg_map, bg_size);
    00736 scrollpos[screen][bg_select].infscroll = 0; // Par défaut pas de scrolling infini...
    00737 PA_bgmap[screen][bg_select] = ScreenBaseBlock(screen, mapchar[screen][bg_select]);
    00738 tilesetchar[screen][bg_select] = tilesetchar[screen][bg_tiles];
    00739 tilesetsize[screen][bg_select] = tilesetsize[screen][bg_tiles];
    00740
    00741 _REG16(REG_BGSCREEN(screen)) |= (0x100 << (bg_select));
    00742 _REG16(REG_BGCNT(screen, bg_select)) = bg_select | (bg_size << 14) |(mapchar[screen][bg_select] << SCREEN_SHIFT) | (1 << 13) | (tilesetchar[screen][bg_select] << 2) | (1 << 7);
    00743 PA_BGScrollXY(screen, bg_select, 0, 0);
    00744 }
    00745
    00746
    00758 extern inline void PA_ClearBg(u8 screen, u8 bg_select){
    00759 u8 i, j;
    00760 for (i = 0; i < 32; i++) for (j = 0; j < 32; j++) PA_SetMapTileAll(screen, bg_select, i, j, 0);
    00761 }
    00762
    00763  // end of BgTiles
    00765
    00766
    00767
    00768
    00769
    00770 #endif
    00771