Merge branch 'master' of https://github.com/pret/pokeemerald into porymap-6

This commit is contained in:
GriffinR
2024-09-19 12:27:36 -04:00
53 changed files with 905 additions and 2709 deletions

1247
src/bg.c Normal file

File diff suppressed because it is too large Load Diff

209
src/blit.c Normal file
View File

@@ -0,0 +1,209 @@
#include "global.h"
#include "blit.h"
void BlitBitmapRect4BitWithoutColorKey(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height)
{
BlitBitmapRect4Bit(src, dst, srcX, srcY, dstX, dstY, width, height, 0xFF);
}
void BlitBitmapRect4Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey)
{
s32 xEnd;
s32 yEnd;
s32 multiplierSrcY;
s32 multiplierDstY;
s32 loopSrcY, loopDstY;
s32 loopSrcX, loopDstX;
const u8 *pixelsSrc;
u8 *pixelsDst;
s32 toOrr;
s32 toAnd;
s32 toShift;
if (dst->width - dstX < width)
xEnd = (dst->width - dstX) + srcX;
else
xEnd = srcX + width;
if (dst->height - dstY < height)
yEnd = (dst->height - dstY) + srcY;
else
yEnd = height + srcY;
multiplierSrcY = (src->width + (src->width & 7)) >> 3;
multiplierDstY = (dst->width + (dst->width & 7)) >> 3;
if (colorKey == 0xFF)
{
for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++)
{
for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++)
{
pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1B);
pixelsDst = dst->pixels + ((loopDstX >> 1) & 3) + ((loopDstX >> 3) << 5) + (((loopDstY >> 3) * multiplierDstY) << 5) + ((u32)(loopDstY << 0x1d) >> 0x1B);
toOrr = ((*pixelsSrc >> ((loopSrcX & 1) << 2)) & 0xF);
toShift = ((loopDstX & 1) << 2);
toOrr <<= toShift;
toAnd = 0xF0 >> (toShift);
*pixelsDst = toOrr | (*pixelsDst & toAnd);
}
}
}
else
{
for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++)
{
for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++)
{
pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1B);
pixelsDst = dst->pixels + ((loopDstX >> 1) & 3) + ((loopDstX >> 3) << 5) + (((loopDstY >> 3) * multiplierDstY) << 5) + ((u32)(loopDstY << 0x1d) >> 0x1B);
toOrr = ((*pixelsSrc >> ((loopSrcX & 1) << 2)) & 0xF);
if (toOrr != colorKey)
{
toShift = ((loopDstX & 1) << 2);
toOrr <<= toShift;
toAnd = 0xF0 >> (toShift);
*pixelsDst = toOrr | (*pixelsDst & toAnd);
}
}
}
}
}
void FillBitmapRect4Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue)
{
s32 xEnd;
s32 yEnd;
s32 multiplierY;
s32 loopX, loopY;
u8 toOrr1, toOrr2;
xEnd = x + width;
if (xEnd > surface->width)
xEnd = surface->width;
yEnd = y + height;
if (yEnd > surface->height)
yEnd = surface->height;
multiplierY = (surface->width + (surface->width & 7)) >> 3;
toOrr1 = fillValue << 4;
toOrr2 = fillValue & 0xF;
for (loopY = y; loopY < yEnd; loopY++)
{
for (loopX = x; loopX < xEnd; loopX++)
{
u8 *pixels = surface->pixels + ((loopX >> 1) & 3) + ((loopX >> 3) << 5) + (((loopY >> 3) * multiplierY) << 5) + ((u32)(loopY << 0x1d) >> 0x1B);
if ((loopX << 0x1F) != 0)
*pixels = toOrr1 | (*pixels & 0xF);
else
*pixels = toOrr2 | (*pixels & 0xF0);
}
}
}
void BlitBitmapRect4BitTo8Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey, u8 paletteOffset)
{
s32 palOffsetBits;
s32 xEnd;
s32 yEnd;
s32 multiplierSrcY;
s32 multiplierDstY;
s32 loopSrcY, loopDstY;
s32 loopSrcX, loopDstX;
const u8 *pixelsSrc;
u8 *pixelsDst;
s32 colorKeyBits;
palOffsetBits = (u32)(paletteOffset << 0x1C) >> 0x18;
colorKeyBits = (u32)(colorKey << 0x1C) >> 0x18;
if (dst->width - dstX < width)
xEnd = (dst->width - dstX) + srcX;
else
xEnd = width + srcX;
if (dst->height - dstY < height)
yEnd = (srcY + dst->height) - dstY;
else
yEnd = srcY + height;
multiplierSrcY = (src->width + (src->width & 7)) >> 3;
multiplierDstY = (dst->width + (dst->width & 7)) >> 3;
if (colorKey == 0xFF)
{
for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++)
{
pixelsSrc = src->pixels + ((srcX >> 1) & 3) + ((srcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b);
for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++)
{
pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a);
if (loopSrcX & 1)
{
*pixelsDst = palOffsetBits + (*pixelsSrc >> 4);
}
else
{
pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b);
*pixelsDst = palOffsetBits + (*pixelsSrc & 0xF);
}
}
}
}
else
{
for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++)
{
pixelsSrc = src->pixels + ((srcX >> 1) & 3) + ((srcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b);
for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++)
{
if (loopSrcX & 1)
{
if ((*pixelsSrc & 0xF0) != colorKeyBits)
{
pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a);
*pixelsDst = palOffsetBits + (*pixelsSrc >> 4);
}
}
else
{
pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b);
if ((*pixelsSrc & 0xF) != colorKey)
{
pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a);
*pixelsDst = palOffsetBits + (*pixelsSrc & 0xF);
}
}
}
}
}
}
void FillBitmapRect8Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue)
{
s32 xEnd;
s32 yEnd;
s32 multiplierY;
s32 loopX, loopY;
xEnd = x + width;
if (xEnd > surface->width)
xEnd = surface->width;
yEnd = y + height;
if (yEnd > surface->height)
yEnd = surface->height;
multiplierY = (surface->width + (surface->width & 7)) >> 3;
for (loopY = y; loopY < yEnd; loopY++)
{
for (loopX = x; loopX < xEnd; loopX++)
{
u8 *pixels = surface->pixels + (loopX & 7) + ((loopX >> 3) << 6) + (((loopY >> 3) * multiplierY) << 6) + ((u32)(loopY << 0x1d) >> 0x1a);
*pixels = fillValue;
}
}
}

183
src/dma3_manager.c Normal file
View File

@@ -0,0 +1,183 @@
#include "global.h"
#include "dma3.h"
#define MAX_DMA_REQUESTS 128
#define DMA_REQUEST_COPY32 1
#define DMA_REQUEST_FILL32 2
#define DMA_REQUEST_COPY16 3
#define DMA_REQUEST_FILL16 4
struct Dma3Request
{
const u8 *src;
u8 *dest;
u16 size;
u16 mode;
u32 value;
};
static struct Dma3Request sDma3Requests[MAX_DMA_REQUESTS];
static vbool8 sDma3ManagerLocked;
static u8 sDma3RequestCursor;
void ClearDma3Requests(void)
{
int i;
sDma3ManagerLocked = TRUE;
sDma3RequestCursor = 0;
for (i = 0; i < MAX_DMA_REQUESTS; i++)
{
sDma3Requests[i].size = 0;
sDma3Requests[i].src = NULL;
sDma3Requests[i].dest = NULL;
}
sDma3ManagerLocked = FALSE;
}
void ProcessDma3Requests(void)
{
u16 bytesTransferred;
if (sDma3ManagerLocked)
return;
bytesTransferred = 0;
// as long as there are DMA requests to process (unless size or vblank is an issue), do not exit
while (sDma3Requests[sDma3RequestCursor].size != 0)
{
bytesTransferred += sDma3Requests[sDma3RequestCursor].size;
if (bytesTransferred > 40 * 1024)
return; // don't transfer more than 40 KiB
if (*(u8 *)REG_ADDR_VCOUNT > 224)
return; // we're about to leave vblank, stop
switch (sDma3Requests[sDma3RequestCursor].mode)
{
case DMA_REQUEST_COPY32: // regular 32-bit copy
Dma3CopyLarge32_(sDma3Requests[sDma3RequestCursor].src,
sDma3Requests[sDma3RequestCursor].dest,
sDma3Requests[sDma3RequestCursor].size);
break;
case DMA_REQUEST_FILL32: // repeat a single 32-bit value across RAM
Dma3FillLarge32_(sDma3Requests[sDma3RequestCursor].value,
sDma3Requests[sDma3RequestCursor].dest,
sDma3Requests[sDma3RequestCursor].size);
break;
case DMA_REQUEST_COPY16: // regular 16-bit copy
Dma3CopyLarge16_(sDma3Requests[sDma3RequestCursor].src,
sDma3Requests[sDma3RequestCursor].dest,
sDma3Requests[sDma3RequestCursor].size);
break;
case DMA_REQUEST_FILL16: // repeat a single 16-bit value across RAM
Dma3FillLarge16_(sDma3Requests[sDma3RequestCursor].value,
sDma3Requests[sDma3RequestCursor].dest,
sDma3Requests[sDma3RequestCursor].size);
break;
}
// Free the request
sDma3Requests[sDma3RequestCursor].src = NULL;
sDma3Requests[sDma3RequestCursor].dest = NULL;
sDma3Requests[sDma3RequestCursor].size = 0;
sDma3Requests[sDma3RequestCursor].mode = 0;
sDma3Requests[sDma3RequestCursor].value = 0;
sDma3RequestCursor++;
if (sDma3RequestCursor >= MAX_DMA_REQUESTS) // loop back to the first DMA request
sDma3RequestCursor = 0;
}
}
s16 RequestDma3Copy(const void *src, void *dest, u16 size, u8 mode)
{
int cursor;
int i = 0;
sDma3ManagerLocked = TRUE;
cursor = sDma3RequestCursor;
while (i < MAX_DMA_REQUESTS)
{
if (sDma3Requests[cursor].size == 0) // an empty request was found.
{
sDma3Requests[cursor].src = src;
sDma3Requests[cursor].dest = dest;
sDma3Requests[cursor].size = size;
if (mode == 1)
sDma3Requests[cursor].mode = DMA_REQUEST_COPY32;
else
sDma3Requests[cursor].mode = DMA_REQUEST_COPY16;
sDma3ManagerLocked = FALSE;
return cursor;
}
if (++cursor >= MAX_DMA_REQUESTS) // loop back to start.
cursor = 0;
i++;
}
sDma3ManagerLocked = FALSE;
return -1; // no free DMA request was found
}
s16 RequestDma3Fill(s32 value, void *dest, u16 size, u8 mode)
{
int cursor;
int i = 0;
cursor = sDma3RequestCursor;
sDma3ManagerLocked = TRUE;
while (i < MAX_DMA_REQUESTS)
{
if (sDma3Requests[cursor].size == 0) // an empty request was found.
{
sDma3Requests[cursor].dest = dest;
sDma3Requests[cursor].size = size;
sDma3Requests[cursor].mode = mode;
sDma3Requests[cursor].value = value;
if(mode == 1)
sDma3Requests[cursor].mode = DMA_REQUEST_FILL32;
else
sDma3Requests[cursor].mode = DMA_REQUEST_FILL16;
sDma3ManagerLocked = FALSE;
return cursor;
}
if (++cursor >= MAX_DMA_REQUESTS) // loop back to start.
cursor = 0;
i++;
}
sDma3ManagerLocked = FALSE;
return -1; // no free DMA request was found
}
s16 CheckForSpaceForDma3Request(s16 index)
{
int i = 0;
if (index == -1) // check if all requests are free
{
while (i < MAX_DMA_REQUESTS)
{
if (sDma3Requests[i].size != 0)
return -1;
i++;
}
return 0;
}
else // check the specified request
{
if (sDma3Requests[index].size != 0)
return -1;
return 0;
}
}

View File

@@ -263,7 +263,7 @@ u16 GetRecordedCyclingRoadResults(void)
void UpdateCyclingRoadState(void)
{
if (gLastUsedWarp.mapNum == MAP_NUM(MAP_ROUTE110_SEASIDE_CYCLING_ROAD_SOUTH_ENTRANCE) && gLastUsedWarp.mapGroup == MAP_GROUP(MAP_ROUTE110_SEASIDE_CYCLING_ROAD_SOUTH_ENTRANCE))
if (gLastUsedWarp.mapNum == MAP_NUM(MAP_ROUTE110_SEASIDE_CYCLING_ROAD_NORTH_ENTRANCE) && gLastUsedWarp.mapGroup == MAP_GROUP(MAP_ROUTE110_SEASIDE_CYCLING_ROAD_NORTH_ENTRANCE))
return;
if (VarGet(VAR_CYCLING_CHALLENGE_STATE) == 2 || VarGet(VAR_CYCLING_CHALLENGE_STATE) == 3)

195
src/gpu_regs.c Normal file
View File

@@ -0,0 +1,195 @@
#include "global.h"
#include "gpu_regs.h"
#define GPU_REG_BUF_SIZE 0x60
#define GPU_REG_BUF(offset) (*(u16 *)(&sGpuRegBuffer[offset]))
#define GPU_REG(offset) (*(vu16 *)(REG_BASE + offset))
#define EMPTY_SLOT 0xFF
static u8 sGpuRegBuffer[GPU_REG_BUF_SIZE];
static u8 sGpuRegWaitingList[GPU_REG_BUF_SIZE];
static volatile bool8 sGpuRegBufferLocked;
static volatile bool8 sShouldSyncRegIE;
static vu16 sRegIE;
static void CopyBufferedValueToGpuReg(u8 regOffset);
static void SyncRegIE(void);
static void UpdateRegDispstatIntrBits(u16 regIE);
void InitGpuRegManager(void)
{
s32 i;
for (i = 0; i < GPU_REG_BUF_SIZE; i++)
{
sGpuRegBuffer[i] = 0;
sGpuRegWaitingList[i] = EMPTY_SLOT;
}
sGpuRegBufferLocked = FALSE;
sShouldSyncRegIE = FALSE;
sRegIE = 0;
}
static void CopyBufferedValueToGpuReg(u8 regOffset)
{
if (regOffset == REG_OFFSET_DISPSTAT)
{
REG_DISPSTAT &= ~(DISPSTAT_HBLANK_INTR | DISPSTAT_VBLANK_INTR);
REG_DISPSTAT |= GPU_REG_BUF(REG_OFFSET_DISPSTAT);
}
else
{
GPU_REG(regOffset) = GPU_REG_BUF(regOffset);
}
}
void CopyBufferedValuesToGpuRegs(void)
{
if (!sGpuRegBufferLocked)
{
s32 i;
for (i = 0; i < GPU_REG_BUF_SIZE; i++)
{
u8 regOffset = sGpuRegWaitingList[i];
if (regOffset == EMPTY_SLOT)
return;
CopyBufferedValueToGpuReg(regOffset);
sGpuRegWaitingList[i] = EMPTY_SLOT;
}
}
}
void SetGpuReg(u8 regOffset, u16 value)
{
if (regOffset < GPU_REG_BUF_SIZE)
{
u16 vcount;
GPU_REG_BUF(regOffset) = value;
vcount = REG_VCOUNT & 0xFF;
if ((vcount >= 161 && vcount <= 225) || (REG_DISPCNT & DISPCNT_FORCED_BLANK))
{
CopyBufferedValueToGpuReg(regOffset);
}
else
{
s32 i;
sGpuRegBufferLocked = TRUE;
for (i = 0; i < GPU_REG_BUF_SIZE && sGpuRegWaitingList[i] != EMPTY_SLOT; i++)
{
if (sGpuRegWaitingList[i] == regOffset)
{
sGpuRegBufferLocked = FALSE;
return;
}
}
sGpuRegWaitingList[i] = regOffset;
sGpuRegBufferLocked = FALSE;
}
}
}
void SetGpuReg_ForcedBlank(u8 regOffset, u16 value)
{
if (regOffset < GPU_REG_BUF_SIZE)
{
GPU_REG_BUF(regOffset) = value;
if (REG_DISPCNT & DISPCNT_FORCED_BLANK)
{
CopyBufferedValueToGpuReg(regOffset);
}
else
{
s32 i;
sGpuRegBufferLocked = TRUE;
for (i = 0; i < GPU_REG_BUF_SIZE && sGpuRegWaitingList[i] != EMPTY_SLOT; i++)
{
if (sGpuRegWaitingList[i] == regOffset)
{
sGpuRegBufferLocked = FALSE;
return;
}
}
sGpuRegWaitingList[i] = regOffset;
sGpuRegBufferLocked = FALSE;
}
}
}
u16 GetGpuReg(u8 regOffset)
{
if (regOffset == REG_OFFSET_DISPSTAT)
return REG_DISPSTAT;
if (regOffset == REG_OFFSET_VCOUNT)
return REG_VCOUNT;
return GPU_REG_BUF(regOffset);
}
void SetGpuRegBits(u8 regOffset, u16 mask)
{
u16 regValue = GPU_REG_BUF(regOffset);
SetGpuReg(regOffset, regValue | mask);
}
void ClearGpuRegBits(u8 regOffset, u16 mask)
{
u16 regValue = GPU_REG_BUF(regOffset);
SetGpuReg(regOffset, regValue & ~mask);
}
static void SyncRegIE(void)
{
if (sShouldSyncRegIE)
{
u16 temp = REG_IME;
REG_IME = 0;
REG_IE = sRegIE;
REG_IME = temp;
sShouldSyncRegIE = FALSE;
}
}
void EnableInterrupts(u16 mask)
{
sRegIE |= mask;
sShouldSyncRegIE = TRUE;
SyncRegIE();
UpdateRegDispstatIntrBits(sRegIE);
}
void DisableInterrupts(u16 mask)
{
sRegIE &= ~mask;
sShouldSyncRegIE = TRUE;
SyncRegIE();
UpdateRegDispstatIntrBits(sRegIE);
}
static void UpdateRegDispstatIntrBits(u16 regIE)
{
u16 oldValue = GetGpuReg(REG_OFFSET_DISPSTAT) & (DISPSTAT_HBLANK_INTR | DISPSTAT_VBLANK_INTR);
u16 newValue = 0;
if (regIE & INTR_FLAG_VBLANK)
newValue |= DISPSTAT_VBLANK_INTR;
if (regIE & INTR_FLAG_HBLANK)
newValue |= DISPSTAT_HBLANK_INTR;
if (oldValue != newValue)
SetGpuReg(REG_OFFSET_DISPSTAT, newValue);
}

36
src/io_reg.c Normal file
View File

@@ -0,0 +1,36 @@
#include "global.h"
#include "io_reg.h"
#include "gba/io_reg.h"
static const u32 sUnused[] = {
0,
0,
(1 << 26) | (1 << 3),
(1 << 26) | (1 << 3) | (1 << 1),
(1 << 26) | (1 << 3) | (1 << 2),
(1 << 26) | (1 << 3) | (1 << 2) | (1 << 1),
(1 << 26) | (1 << 4),
(1 << 26) | (1 << 4) | (1 << 2),
(1 << 26) | (1 << 4) | (1 << 3),
(1 << 26) | (1 << 4) | (1 << 3) | (1 << 2),
(1 << 26) | (1 << 4) | (1 << 1),
(1 << 26) | (1 << 4) | (1 << 2) | (1 << 1),
(1 << 26) | (1 << 4) | (1 << 3) | (1 << 1),
(1 << 26) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1),
(1 << 25) | (1 << 8),
(1 << 27) | (1 << 10),
};
const u16 gOverworldBackgroundLayerFlags[] = {
BLDCNT_TGT2_BG0,
BLDCNT_TGT2_BG1,
BLDCNT_TGT2_BG2,
BLDCNT_TGT2_BG3,
};
const u16 gOrbEffectBackgroundLayerFlags[] = {
BLDCNT_TGT1_BG0,
BLDCNT_TGT1_BG1,
BLDCNT_TGT1_BG2,
BLDCNT_TGT1_BG3,
};

224
src/malloc.c Normal file
View File

@@ -0,0 +1,224 @@
#include "global.h"
#include "malloc.h"
static void *sHeapStart;
static u32 sHeapSize;
ALIGNED(4) EWRAM_DATA u8 gHeap[HEAP_SIZE] = {0};
#define MALLOC_SYSTEM_ID 0xA3A3
struct MemBlock {
// Whether this block is currently allocated.
bool16 flag;
// Magic number used for error checking. Should equal MALLOC_SYSTEM_ID.
u16 magic;
// Size of the block (not including this header struct).
u32 size;
// Previous block pointer. Equals sHeapStart if this is the first block.
struct MemBlock *prev;
// Next block pointer. Equals sHeapStart if this is the last block.
struct MemBlock *next;
// Data in the memory block. (Arrays of length 0 are a GNU extension.)
u8 data[0];
};
void PutMemBlockHeader(void *block, struct MemBlock *prev, struct MemBlock *next, u32 size)
{
struct MemBlock *header = (struct MemBlock *)block;
header->flag = FALSE;
header->magic = MALLOC_SYSTEM_ID;
header->size = size;
header->prev = prev;
header->next = next;
}
void PutFirstMemBlockHeader(void *block, u32 size)
{
PutMemBlockHeader(block, (struct MemBlock *)block, (struct MemBlock *)block, size - sizeof(struct MemBlock));
}
void *AllocInternal(void *heapStart, u32 size)
{
struct MemBlock *pos = (struct MemBlock *)heapStart;
struct MemBlock *head = pos;
struct MemBlock *splitBlock;
u32 foundBlockSize;
// Alignment
if (size & 3)
size = 4 * ((size / 4) + 1);
for (;;)
{
// Loop through the blocks looking for unused block that's big enough.
if (!pos->flag)
{
foundBlockSize = pos->size;
if (foundBlockSize >= size)
{
if (foundBlockSize - size < 2 * sizeof(struct MemBlock))
{
// The block isn't much bigger than the requested size,
// so just use it.
pos->flag = TRUE;
}
else
{
// The block is significantly bigger than the requested
// size, so split the rest into a separate block.
foundBlockSize -= sizeof(struct MemBlock);
foundBlockSize -= size;
splitBlock = (struct MemBlock *)(pos->data + size);
pos->flag = TRUE;
pos->size = size;
PutMemBlockHeader(splitBlock, pos, pos->next, foundBlockSize);
pos->next = splitBlock;
if (splitBlock->next != head)
splitBlock->next->prev = splitBlock;
}
return pos->data;
}
}
if (pos->next == head)
return NULL;
pos = pos->next;
}
}
void FreeInternal(void *heapStart, void *pointer)
{
if (pointer)
{
struct MemBlock *head = (struct MemBlock *)heapStart;
struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock));
block->flag = FALSE;
// If the freed block isn't the last one, merge with the next block
// if it's not in use.
if (block->next != head)
{
if (!block->next->flag)
{
block->size += sizeof(struct MemBlock) + block->next->size;
block->next->magic = 0;
block->next = block->next->next;
if (block->next != head)
block->next->prev = block;
}
}
// If the freed block isn't the first one, merge with the previous block
// if it's not in use.
if (block != head)
{
if (!block->prev->flag)
{
block->prev->next = block->next;
if (block->next != head)
block->next->prev = block->prev;
block->magic = 0;
block->prev->size += sizeof(struct MemBlock) + block->size;
}
}
}
}
void *AllocZeroedInternal(void *heapStart, u32 size)
{
void *mem = AllocInternal(heapStart, size);
if (mem != NULL)
{
if (size & 3)
size = 4 * ((size / 4) + 1);
CpuFill32(0, mem, size);
}
return mem;
}
bool32 CheckMemBlockInternal(void *heapStart, void *pointer)
{
struct MemBlock *head = (struct MemBlock *)heapStart;
struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock));
if (block->magic != MALLOC_SYSTEM_ID)
return FALSE;
if (block->next->magic != MALLOC_SYSTEM_ID)
return FALSE;
if (block->next != head && block->next->prev != block)
return FALSE;
if (block->prev->magic != MALLOC_SYSTEM_ID)
return FALSE;
if (block->prev != head && block->prev->next != block)
return FALSE;
if (block->next != head && block->next != (struct MemBlock *)(block->data + block->size))
return FALSE;
return TRUE;
}
void InitHeap(void *heapStart, u32 heapSize)
{
sHeapStart = heapStart;
sHeapSize = heapSize;
PutFirstMemBlockHeader(heapStart, heapSize);
}
void *Alloc(u32 size)
{
return AllocInternal(sHeapStart, size);
}
void *AllocZeroed(u32 size)
{
return AllocZeroedInternal(sHeapStart, size);
}
void Free(void *pointer)
{
FreeInternal(sHeapStart, pointer);
}
bool32 CheckMemBlock(void *pointer)
{
return CheckMemBlockInternal(sHeapStart, pointer);
}
bool32 CheckHeap()
{
struct MemBlock *pos = (struct MemBlock *)sHeapStart;
do {
if (!CheckMemBlockInternal(sHeapStart, pos->data))
return FALSE;
pos = pos->next;
} while (pos != (struct MemBlock *)sHeapStart);
return TRUE;
}

View File

@@ -36,7 +36,7 @@
#include "gba/types.h"
#include "gba/defines.h"
#include "config.h"
#include "characters.h"
#include "constants/characters.h"
#include "string_util.h"
#ifndef NDEBUG

1757
src/sprite.c Normal file

File diff suppressed because it is too large Load Diff

781
src/string_util.c Normal file
View File

@@ -0,0 +1,781 @@
#include "global.h"
#include "string_util.h"
#include "text.h"
#include "strings.h"
EWRAM_DATA u8 gStringVar1[0x100] = {0};
EWRAM_DATA u8 gStringVar2[0x100] = {0};
EWRAM_DATA u8 gStringVar3[0x100] = {0};
EWRAM_DATA u8 gStringVar4[0x3E8] = {0};
EWRAM_DATA static u8 sUnknownStringVar[16] = {0};
static const u8 sDigits[] = __("0123456789ABCDEF");
static const s32 sPowersOfTen[] =
{
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
};
u8 *StringCopy_Nickname(u8 *dest, const u8 *src)
{
u8 i;
u32 limit = POKEMON_NAME_LENGTH;
for (i = 0; i < limit; i++)
{
dest[i] = src[i];
if (dest[i] == EOS)
return &dest[i];
}
dest[i] = EOS;
return &dest[i];
}
u8 *StringGet_Nickname(u8 *str)
{
u8 i;
u32 limit = POKEMON_NAME_LENGTH;
for (i = 0; i < limit; i++)
if (str[i] == EOS)
return &str[i];
str[i] = EOS;
return &str[i];
}
u8 *StringCopy_PlayerName(u8 *dest, const u8 *src)
{
s32 i;
s32 limit = PLAYER_NAME_LENGTH;
for (i = 0; i < limit; i++)
{
dest[i] = src[i];
if (dest[i] == EOS)
return &dest[i];
}
dest[i] = EOS;
return &dest[i];
}
u8 *StringCopy(u8 *dest, const u8 *src)
{
while (*src != EOS)
{
*dest = *src;
dest++;
src++;
}
*dest = EOS;
return dest;
}
u8 *StringAppend(u8 *dest, const u8 *src)
{
while (*dest != EOS)
dest++;
return StringCopy(dest, src);
}
u8 *StringCopyN(u8 *dest, const u8 *src, u8 n)
{
u16 i;
for (i = 0; i < n; i++)
dest[i] = src[i];
return &dest[n];
}
u8 *StringAppendN(u8 *dest, const u8 *src, u8 n)
{
while (*dest != EOS)
dest++;
return StringCopyN(dest, src, n);
}
u16 StringLength(const u8 *str)
{
u16 length = 0;
while (str[length] != EOS)
length++;
return length;
}
s32 StringCompare(const u8 *str1, const u8 *str2)
{
while (*str1 == *str2)
{
if (*str1 == EOS)
return 0;
str1++;
str2++;
}
return *str1 - *str2;
}
s32 StringCompareN(const u8 *str1, const u8 *str2, u32 n)
{
while (*str1 == *str2)
{
if (*str1 == EOS)
return 0;
str1++;
str2++;
if (--n == 0)
return 0;
}
return *str1 - *str2;
}
bool8 IsStringLengthAtLeast(const u8 *str, s32 n)
{
u8 i;
for (i = 0; i < n; i++)
if (str[i] && str[i] != EOS)
return TRUE;
return FALSE;
}
u8 *ConvertIntToDecimalStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n)
{
enum { WAITING_FOR_NONZERO_DIGIT, WRITING_DIGITS, WRITING_SPACES } state;
s32 powerOfTen;
s32 largestPowerOfTen = sPowersOfTen[n - 1];
state = WAITING_FOR_NONZERO_DIGIT;
if (mode == STR_CONV_MODE_RIGHT_ALIGN)
state = WRITING_SPACES;
if (mode == STR_CONV_MODE_LEADING_ZEROS)
state = WRITING_DIGITS;
for (powerOfTen = largestPowerOfTen; powerOfTen > 0; powerOfTen /= 10)
{
u8 c;
u16 digit = value / powerOfTen;
s32 temp = value - (powerOfTen * digit);
if (state == WRITING_DIGITS)
{
u8 *out = dest++;
if (digit <= 9)
c = sDigits[digit];
else
c = CHAR_QUESTION_MARK;
*out = c;
}
else if (digit != 0 || powerOfTen == 1)
{
u8 *out;
state = WRITING_DIGITS;
out = dest++;
if (digit <= 9)
c = sDigits[digit];
else
c = CHAR_QUESTION_MARK;
*out = c;
}
else if (state == WRITING_SPACES)
{
*dest++ = CHAR_SPACER;
}
value = temp;
}
*dest = EOS;
return dest;
}
u8 *ConvertUIntToDecimalStringN(u8 *dest, u32 value, enum StringConvertMode mode, u8 n)
{
enum { WAITING_FOR_NONZERO_DIGIT, WRITING_DIGITS, WRITING_SPACES } state;
s32 powerOfTen;
s32 largestPowerOfTen = sPowersOfTen[n - 1];
state = WAITING_FOR_NONZERO_DIGIT;
if (mode == STR_CONV_MODE_RIGHT_ALIGN)
state = WRITING_SPACES;
if (mode == STR_CONV_MODE_LEADING_ZEROS)
state = WRITING_DIGITS;
for (powerOfTen = largestPowerOfTen; powerOfTen > 0; powerOfTen /= 10)
{
u8 c;
u16 digit = value / powerOfTen;
u32 temp = value - (powerOfTen * digit);
if (state == WRITING_DIGITS)
{
u8 *out = dest++;
if (digit <= 9)
c = sDigits[digit];
else
c = CHAR_QUESTION_MARK;
*out = c;
}
else if (digit != 0 || powerOfTen == 1)
{
u8 *out;
state = WRITING_DIGITS;
out = dest++;
if (digit <= 9)
c = sDigits[digit];
else
c = CHAR_QUESTION_MARK;
*out = c;
}
else if (state == WRITING_SPACES)
{
*dest++ = CHAR_SPACER;
}
value = temp;
}
*dest = EOS;
return dest;
}
u8 *ConvertIntToHexStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n)
{
enum { WAITING_FOR_NONZERO_DIGIT, WRITING_DIGITS, WRITING_SPACES } state;
u8 i;
s32 powerOfSixteen;
s32 largestPowerOfSixteen = 1;
for (i = 1; i < n; i++)
largestPowerOfSixteen *= 16;
state = WAITING_FOR_NONZERO_DIGIT;
if (mode == STR_CONV_MODE_RIGHT_ALIGN)
state = WRITING_SPACES;
if (mode == STR_CONV_MODE_LEADING_ZEROS)
state = WRITING_DIGITS;
for (powerOfSixteen = largestPowerOfSixteen; powerOfSixteen > 0; powerOfSixteen /= 16)
{
u8 c;
u32 digit = value / powerOfSixteen;
s32 temp = value % powerOfSixteen;
if (state == WRITING_DIGITS)
{
u8 *out = dest++;
if (digit <= 0xF)
c = sDigits[digit];
else
c = CHAR_QUESTION_MARK;
*out = c;
}
else if (digit != 0 || powerOfSixteen == 1)
{
u8 *out;
state = WRITING_DIGITS;
out = dest++;
if (digit <= 0xF)
c = sDigits[digit];
else
c = CHAR_QUESTION_MARK;
*out = c;
}
else if (state == WRITING_SPACES)
{
*dest++ = CHAR_SPACER;
}
value = temp;
}
*dest = EOS;
return dest;
}
u8 *StringExpandPlaceholders(u8 *dest, const u8 *src)
{
for (;;)
{
u8 c = *src++;
u8 placeholderId;
const u8 *expandedString;
switch (c)
{
case PLACEHOLDER_BEGIN:
placeholderId = *src++;
expandedString = GetExpandedPlaceholder(placeholderId);
dest = StringExpandPlaceholders(dest, expandedString);
break;
case EXT_CTRL_CODE_BEGIN:
*dest++ = c;
c = *src++;
*dest++ = c;
switch (c)
{
case EXT_CTRL_CODE_RESET_FONT:
case EXT_CTRL_CODE_PAUSE_UNTIL_PRESS:
case EXT_CTRL_CODE_FILL_WINDOW:
case EXT_CTRL_CODE_JPN:
case EXT_CTRL_CODE_ENG:
case EXT_CTRL_CODE_PAUSE_MUSIC:
case EXT_CTRL_CODE_RESUME_MUSIC:
break;
case EXT_CTRL_CODE_COLOR_HIGHLIGHT_SHADOW:
*dest++ = *src++;
case EXT_CTRL_CODE_PLAY_BGM:
*dest++ = *src++;
default:
*dest++ = *src++;
}
break;
case EOS:
*dest = EOS;
return dest;
case CHAR_PROMPT_SCROLL:
case CHAR_PROMPT_CLEAR:
case CHAR_NEWLINE:
default:
*dest++ = c;
}
}
}
u8 *StringBraille(u8 *dest, const u8 *src)
{
const u8 setBrailleFont[] = {
EXT_CTRL_CODE_BEGIN,
EXT_CTRL_CODE_FONT,
FONT_BRAILLE,
EOS
};
const u8 gotoLine2[] = {
CHAR_NEWLINE,
EXT_CTRL_CODE_BEGIN,
EXT_CTRL_CODE_SHIFT_DOWN,
2,
EOS
};
dest = StringCopy(dest, setBrailleFont);
for (;;)
{
u8 c = *src++;
switch (c)
{
case EOS:
*dest = c;
return dest;
case CHAR_NEWLINE:
dest = StringCopy(dest, gotoLine2);
break;
default:
*dest++ = c;
*dest++ = c + NUM_BRAILLE_CHARS;
break;
}
}
}
static const u8 *ExpandPlaceholder_UnknownStringVar(void)
{
return sUnknownStringVar;
}
static const u8 *ExpandPlaceholder_PlayerName(void)
{
return gSaveBlock2Ptr->playerName;
}
static const u8 *ExpandPlaceholder_StringVar1(void)
{
return gStringVar1;
}
static const u8 *ExpandPlaceholder_StringVar2(void)
{
return gStringVar2;
}
static const u8 *ExpandPlaceholder_StringVar3(void)
{
return gStringVar3;
}
static const u8 *ExpandPlaceholder_KunChan(void)
{
if (gSaveBlock2Ptr->playerGender == MALE)
return gText_ExpandedPlaceholder_Kun;
else
return gText_ExpandedPlaceholder_Chan;
}
static const u8 *ExpandPlaceholder_RivalName(void)
{
if (gSaveBlock2Ptr->playerGender == MALE)
return gText_ExpandedPlaceholder_May;
else
return gText_ExpandedPlaceholder_Brendan;
}
static const u8 *ExpandPlaceholder_Version(void)
{
return gText_ExpandedPlaceholder_Emerald;
}
static const u8 *ExpandPlaceholder_Aqua(void)
{
return gText_ExpandedPlaceholder_Aqua;
}
static const u8 *ExpandPlaceholder_Magma(void)
{
return gText_ExpandedPlaceholder_Magma;
}
static const u8 *ExpandPlaceholder_Archie(void)
{
return gText_ExpandedPlaceholder_Archie;
}
static const u8 *ExpandPlaceholder_Maxie(void)
{
return gText_ExpandedPlaceholder_Maxie;
}
static const u8 *ExpandPlaceholder_Kyogre(void)
{
return gText_ExpandedPlaceholder_Kyogre;
}
static const u8 *ExpandPlaceholder_Groudon(void)
{
return gText_ExpandedPlaceholder_Groudon;
}
const u8 *GetExpandedPlaceholder(u32 id)
{
typedef const u8 *(*ExpandPlaceholderFunc)(void);
static const ExpandPlaceholderFunc funcs[] =
{
[PLACEHOLDER_ID_UNKNOWN] = ExpandPlaceholder_UnknownStringVar,
[PLACEHOLDER_ID_PLAYER] = ExpandPlaceholder_PlayerName,
[PLACEHOLDER_ID_STRING_VAR_1] = ExpandPlaceholder_StringVar1,
[PLACEHOLDER_ID_STRING_VAR_2] = ExpandPlaceholder_StringVar2,
[PLACEHOLDER_ID_STRING_VAR_3] = ExpandPlaceholder_StringVar3,
[PLACEHOLDER_ID_KUN] = ExpandPlaceholder_KunChan,
[PLACEHOLDER_ID_RIVAL] = ExpandPlaceholder_RivalName,
[PLACEHOLDER_ID_VERSION] = ExpandPlaceholder_Version,
[PLACEHOLDER_ID_AQUA] = ExpandPlaceholder_Aqua,
[PLACEHOLDER_ID_MAGMA] = ExpandPlaceholder_Magma,
[PLACEHOLDER_ID_ARCHIE] = ExpandPlaceholder_Archie,
[PLACEHOLDER_ID_MAXIE] = ExpandPlaceholder_Maxie,
[PLACEHOLDER_ID_KYOGRE] = ExpandPlaceholder_Kyogre,
[PLACEHOLDER_ID_GROUDON] = ExpandPlaceholder_Groudon,
};
if (id >= ARRAY_COUNT(funcs))
return gText_ExpandedPlaceholder_Empty;
else
return funcs[id]();
}
u8 *StringFill(u8 *dest, u8 c, u16 n)
{
u16 i;
for (i = 0; i < n; i++)
*dest++ = c;
*dest = EOS;
return dest;
}
u8 *StringCopyPadded(u8 *dest, const u8 *src, u8 c, u16 n)
{
while (*src != EOS)
{
*dest++ = *src++;
if (n)
n--;
}
n--;
while (n != (u16)-1)
{
*dest++ = c;
n--;
}
*dest = EOS;
return dest;
}
u8 *StringFillWithTerminator(u8 *dest, u16 n)
{
return StringFill(dest, EOS, n);
}
u8 *StringCopyN_Multibyte(u8 *dest, u8 *src, u32 n)
{
u32 i;
for (i = n - 1; i != (u32)-1; i--)
{
if (*src == EOS)
{
break;
}
else
{
*dest++ = *src++;
if (*(src - 1) == CHAR_EXTRA_SYMBOL)
*dest++ = *src++;
}
}
*dest = EOS;
return dest;
}
u32 StringLength_Multibyte(const u8 *str)
{
u32 length = 0;
while (*str != EOS)
{
if (*str == CHAR_EXTRA_SYMBOL)
str++;
str++;
length++;
}
return length;
}
u8 *WriteColorChangeControlCode(u8 *dest, u32 colorType, u8 color)
{
*dest = EXT_CTRL_CODE_BEGIN;
dest++;
switch (colorType)
{
case 0:
*dest = EXT_CTRL_CODE_COLOR;
dest++;
break;
case 1:
*dest = EXT_CTRL_CODE_SHADOW;
dest++;
break;
case 2:
*dest = EXT_CTRL_CODE_HIGHLIGHT;
dest++;
break;
}
*dest = color;
dest++;
*dest = EOS;
return dest;
}
bool32 IsStringJapanese(u8 *str)
{
while (*str != EOS)
{
if (*str <= JAPANESE_CHAR_END)
if (*str != CHAR_SPACE)
return TRUE;
str++;
}
return FALSE;
}
bool32 IsStringNJapanese(u8 *str, s32 n)
{
s32 i;
for (i = 0; *str != EOS && i < n; i++)
{
if (*str <= JAPANESE_CHAR_END)
if (*str != CHAR_SPACE)
return TRUE;
str++;
}
return FALSE;
}
u8 GetExtCtrlCodeLength(u8 code)
{
static const u8 lengths[] =
{
[0] = 1,
[EXT_CTRL_CODE_COLOR] = 2,
[EXT_CTRL_CODE_HIGHLIGHT] = 2,
[EXT_CTRL_CODE_SHADOW] = 2,
[EXT_CTRL_CODE_COLOR_HIGHLIGHT_SHADOW] = 4,
[EXT_CTRL_CODE_PALETTE] = 2,
[EXT_CTRL_CODE_FONT] = 2,
[EXT_CTRL_CODE_RESET_FONT] = 1,
[EXT_CTRL_CODE_PAUSE] = 2,
[EXT_CTRL_CODE_PAUSE_UNTIL_PRESS] = 1,
[EXT_CTRL_CODE_WAIT_SE] = 1,
[EXT_CTRL_CODE_PLAY_BGM] = 3,
[EXT_CTRL_CODE_ESCAPE] = 2,
[EXT_CTRL_CODE_SHIFT_RIGHT] = 2,
[EXT_CTRL_CODE_SHIFT_DOWN] = 2,
[EXT_CTRL_CODE_FILL_WINDOW] = 1,
[EXT_CTRL_CODE_PLAY_SE] = 3,
[EXT_CTRL_CODE_CLEAR] = 2,
[EXT_CTRL_CODE_SKIP] = 2,
[EXT_CTRL_CODE_CLEAR_TO] = 2,
[EXT_CTRL_CODE_MIN_LETTER_SPACING] = 2,
[EXT_CTRL_CODE_JPN] = 1,
[EXT_CTRL_CODE_ENG] = 1,
[EXT_CTRL_CODE_PAUSE_MUSIC] = 1,
[EXT_CTRL_CODE_RESUME_MUSIC] = 1,
};
u8 length = 0;
if (code < ARRAY_COUNT(lengths))
length = lengths[code];
return length;
}
static const u8 *SkipExtCtrlCode(const u8 *s)
{
while (*s == EXT_CTRL_CODE_BEGIN)
{
s++;
s += GetExtCtrlCodeLength(*s);
}
return s;
}
s32 StringCompareWithoutExtCtrlCodes(const u8 *str1, const u8 *str2)
{
s32 retVal = 0;
while (1)
{
str1 = SkipExtCtrlCode(str1);
str2 = SkipExtCtrlCode(str2);
if (*str1 > *str2)
break;
if (*str1 < *str2)
{
retVal = -1;
if (*str2 == EOS)
retVal = 1;
}
if (*str1 == EOS)
return retVal;
str1++;
str2++;
}
retVal = 1;
if (*str1 == EOS)
retVal = -1;
return retVal;
}
void ConvertInternationalString(u8 *s, u8 language)
{
if (language == LANGUAGE_JAPANESE)
{
u8 i;
StripExtCtrlCodes(s);
i = StringLength(s);
s[i++] = EXT_CTRL_CODE_BEGIN;
s[i++] = EXT_CTRL_CODE_ENG;
s[i++] = EOS;
i--;
while (i != (u8)-1)
{
s[i + 2] = s[i];
i--;
}
s[0] = EXT_CTRL_CODE_BEGIN;
s[1] = EXT_CTRL_CODE_JPN;
}
}
void StripExtCtrlCodes(u8 *str)
{
u16 srcIndex = 0;
u16 destIndex = 0;
while (str[srcIndex] != EOS)
{
if (str[srcIndex] == EXT_CTRL_CODE_BEGIN)
{
srcIndex++;
srcIndex += GetExtCtrlCodeLength(str[srcIndex]);
}
else
{
str[destIndex++] = str[srcIndex++];
}
}
str[destIndex] = EOS;
}

1904
src/text.c Normal file

File diff suppressed because it is too large Load Diff

714
src/window.c Normal file
View File

@@ -0,0 +1,714 @@
#include "global.h"
#include "window.h"
#include "malloc.h"
#include "bg.h"
#include "blit.h"
// This global is set to 0 and never changed.
u8 gTransparentTileNumber;
void *gWindowBgTilemapBuffers[NUM_BACKGROUNDS];
extern u32 gWindowTileAutoAllocEnabled;
EWRAM_DATA struct Window gWindows[WINDOWS_MAX] = {0};
EWRAM_DATA static struct Window* sWindowPtr = NULL;
EWRAM_DATA static u16 sWindowSize = 0;
static u8 GetNumActiveWindowsOnBg(u8 bgId);
static u8 GetNumActiveWindowsOnBg8Bit(u8 bgId);
static const struct WindowTemplate sDummyWindowTemplate = DUMMY_WIN_TEMPLATE;
static void DummyWindowBgTilemap(void)
{
}
bool16 InitWindows(const struct WindowTemplate *templates)
{
int i;
void *bgTilemapBuffer;
int j;
u8 bgLayer;
u16 attrib;
u8 *allocatedTilemapBuffer;
int allocatedBaseBlock;
for (i = 0; i < NUM_BACKGROUNDS; ++i)
{
bgTilemapBuffer = GetBgTilemapBuffer(i);
if (bgTilemapBuffer != NULL)
gWindowBgTilemapBuffers[i] = DummyWindowBgTilemap;
else
gWindowBgTilemapBuffers[i] = bgTilemapBuffer;
}
for (i = 0; i < WINDOWS_MAX; ++i)
{
gWindows[i].window = sDummyWindowTemplate;
gWindows[i].tileData = NULL;
}
for (i = 0, allocatedBaseBlock = 0, bgLayer = templates[i].bg; bgLayer != 0xFF && i < WINDOWS_MAX; ++i, bgLayer = templates[i].bg)
{
if (gWindowTileAutoAllocEnabled == TRUE)
{
allocatedBaseBlock = BgTileAllocOp(bgLayer, 0, templates[i].width * templates[i].height, 0);
if (allocatedBaseBlock == -1)
return FALSE;
}
if (gWindowBgTilemapBuffers[bgLayer] == NULL)
{
attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC);
if (attrib != 0xFFFF)
{
allocatedTilemapBuffer = AllocZeroed(attrib);
if (allocatedTilemapBuffer == NULL)
{
FreeAllWindowBuffers();
return FALSE;
}
for (j = 0; j < attrib; ++j)
allocatedTilemapBuffer[j] = 0;
gWindowBgTilemapBuffers[bgLayer] = allocatedTilemapBuffer;
SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer);
}
}
allocatedTilemapBuffer = AllocZeroed((u16)(32 * (templates[i].width * templates[i].height)));
if (allocatedTilemapBuffer == NULL)
{
if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gWindowBgTilemapBuffers[bgLayer] != DummyWindowBgTilemap))
{
Free(gWindowBgTilemapBuffers[bgLayer]);
gWindowBgTilemapBuffers[bgLayer] = allocatedTilemapBuffer;
}
return FALSE;
}
gWindows[i].tileData = allocatedTilemapBuffer;
gWindows[i].window = templates[i];
if (gWindowTileAutoAllocEnabled == TRUE)
{
gWindows[i].window.baseBlock = allocatedBaseBlock;
BgTileAllocOp(bgLayer, allocatedBaseBlock, templates[i].width * templates[i].height, 1);
}
}
gTransparentTileNumber = 0;
return TRUE;
}
u16 AddWindow(const struct WindowTemplate *template)
{
u16 win;
u8 bgLayer;
int allocatedBaseBlock;
u16 attrib;
u8 *allocatedTilemapBuffer;
int i;
for (win = 0; win < WINDOWS_MAX; ++win)
{
if ((bgLayer = gWindows[win].window.bg) == 0xFF)
break;
}
if (win == WINDOWS_MAX)
return WINDOW_NONE;
bgLayer = template->bg;
allocatedBaseBlock = 0;
if (gWindowTileAutoAllocEnabled == TRUE)
{
allocatedBaseBlock = BgTileAllocOp(bgLayer, 0, template->width * template->height, 0);
if (allocatedBaseBlock == -1)
return WINDOW_NONE;
}
if (gWindowBgTilemapBuffers[bgLayer] == NULL)
{
attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC);
if (attrib != 0xFFFF)
{
allocatedTilemapBuffer = AllocZeroed(attrib);
if (allocatedTilemapBuffer == NULL)
return WINDOW_NONE;
for (i = 0; i < attrib; ++i)
allocatedTilemapBuffer[i] = 0;
gWindowBgTilemapBuffers[bgLayer] = allocatedTilemapBuffer;
SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer);
}
}
allocatedTilemapBuffer = AllocZeroed((u16)(32 * (template->width * template->height)));
if (allocatedTilemapBuffer == NULL)
{
if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gWindowBgTilemapBuffers[bgLayer] != DummyWindowBgTilemap))
{
Free(gWindowBgTilemapBuffers[bgLayer]);
gWindowBgTilemapBuffers[bgLayer] = allocatedTilemapBuffer;
}
return WINDOW_NONE;
}
gWindows[win].tileData = allocatedTilemapBuffer;
gWindows[win].window = *template;
if (gWindowTileAutoAllocEnabled == TRUE)
{
gWindows[win].window.baseBlock = allocatedBaseBlock;
BgTileAllocOp(bgLayer, allocatedBaseBlock, gWindows[win].window.width * gWindows[win].window.height, 1);
}
return win;
}
int AddWindowWithoutTileMap(const struct WindowTemplate *template)
{
u16 win;
u8 bgLayer;
int allocatedBaseBlock;
for (win = 0; win < WINDOWS_MAX; ++win)
{
if (gWindows[win].window.bg == 0xFF)
break;
}
if (win == WINDOWS_MAX)
return WINDOW_NONE;
bgLayer = template->bg;
allocatedBaseBlock = 0;
if (gWindowTileAutoAllocEnabled == TRUE)
{
allocatedBaseBlock = BgTileAllocOp(bgLayer, 0, template->width * template->height, 0);
if (allocatedBaseBlock == -1)
return WINDOW_NONE;
}
gWindows[win].window = *template;
if (gWindowTileAutoAllocEnabled == TRUE)
{
gWindows[win].window.baseBlock = allocatedBaseBlock;
BgTileAllocOp(bgLayer, allocatedBaseBlock, gWindows[win].window.width * gWindows[win].window.height, 1);
}
return win;
}
void RemoveWindow(u8 windowId)
{
u8 bgLayer = gWindows[windowId].window.bg;
if (gWindowTileAutoAllocEnabled == TRUE)
BgTileAllocOp(bgLayer, gWindows[windowId].window.baseBlock, gWindows[windowId].window.width * gWindows[windowId].window.height, 2);
gWindows[windowId].window = sDummyWindowTemplate;
if (GetNumActiveWindowsOnBg(bgLayer) == 0)
{
if (gWindowBgTilemapBuffers[bgLayer] != DummyWindowBgTilemap)
{
Free(gWindowBgTilemapBuffers[bgLayer]);
gWindowBgTilemapBuffers[bgLayer] = NULL;
}
}
if (gWindows[windowId].tileData != NULL)
{
Free(gWindows[windowId].tileData);
gWindows[windowId].tileData = NULL;
}
}
void FreeAllWindowBuffers(void)
{
int i;
for (i = 0; i < NUM_BACKGROUNDS; ++i)
{
if (gWindowBgTilemapBuffers[i] != NULL && gWindowBgTilemapBuffers[i] != DummyWindowBgTilemap)
{
Free(gWindowBgTilemapBuffers[i]);
gWindowBgTilemapBuffers[i] = NULL;
}
}
for (i = 0; i < WINDOWS_MAX; ++i)
{
if (gWindows[i].tileData != NULL)
{
Free(gWindows[i].tileData);
gWindows[i].tileData = NULL;
}
}
}
void CopyWindowToVram(u8 windowId, u8 mode)
{
struct Window windowLocal = gWindows[windowId];
u16 windowSize = 32 * (windowLocal.window.width * windowLocal.window.height);
switch (mode)
{
case COPYWIN_MAP:
CopyBgTilemapBufferToVram(windowLocal.window.bg);
break;
case COPYWIN_GFX:
LoadBgTiles(windowLocal.window.bg, windowLocal.tileData, windowSize, windowLocal.window.baseBlock);
break;
case COPYWIN_FULL:
LoadBgTiles(windowLocal.window.bg, windowLocal.tileData, windowSize, windowLocal.window.baseBlock);
CopyBgTilemapBufferToVram(windowLocal.window.bg);
break;
}
}
void CopyWindowRectToVram(u32 windowId, u32 mode, u32 x, u32 y, u32 w, u32 h)
{
struct Window windowLocal;
int rectSize;
int rectPos;
if (w != 0 && h != 0)
{
windowLocal = gWindows[windowId];
rectSize = ((h - 1) * windowLocal.window.width);
rectSize += (windowLocal.window.width - x);
rectSize -= (windowLocal.window.width - (x + w));
rectSize *= 32;
rectPos = (y * windowLocal.window.width) + x;
switch (mode)
{
case COPYWIN_MAP:
CopyBgTilemapBufferToVram(windowLocal.window.bg);
break;
case COPYWIN_GFX:
LoadBgTiles(windowLocal.window.bg, windowLocal.tileData + (rectPos * 32), rectSize, windowLocal.window.baseBlock + rectPos);
break;
case COPYWIN_FULL:
LoadBgTiles(windowLocal.window.bg, windowLocal.tileData + (rectPos * 32), rectSize, windowLocal.window.baseBlock + rectPos);
CopyBgTilemapBufferToVram(windowLocal.window.bg);
break;
}
}
}
void PutWindowTilemap(u8 windowId)
{
struct Window windowLocal = gWindows[windowId];
WriteSequenceToBgTilemapBuffer(
windowLocal.window.bg,
GetBgAttribute(windowLocal.window.bg, BG_ATTR_BASETILE) + windowLocal.window.baseBlock,
windowLocal.window.tilemapLeft,
windowLocal.window.tilemapTop,
windowLocal.window.width,
windowLocal.window.height,
windowLocal.window.paletteNum,
1);
}
void PutWindowRectTilemapOverridePalette(u8 windowId, u8 x, u8 y, u8 width, u8 height, u8 palette)
{
struct Window windowLocal = gWindows[windowId];
u16 currentRow = windowLocal.window.baseBlock + (y * windowLocal.window.width) + x + GetBgAttribute(windowLocal.window.bg, BG_ATTR_BASETILE);
int i;
for (i = 0; i < height; ++i)
{
WriteSequenceToBgTilemapBuffer(
windowLocal.window.bg,
currentRow,
windowLocal.window.tilemapLeft + x,
windowLocal.window.tilemapTop + y + i,
width,
1,
palette,
1);
currentRow += windowLocal.window.width;
}
}
// Fills a window with transparent tiles.
void ClearWindowTilemap(u8 windowId)
{
struct Window windowLocal = gWindows[windowId];
FillBgTilemapBufferRect(
windowLocal.window.bg,
gTransparentTileNumber,
windowLocal.window.tilemapLeft,
windowLocal.window.tilemapTop,
windowLocal.window.width,
windowLocal.window.height,
windowLocal.window.paletteNum);
}
void PutWindowRectTilemap(u8 windowId, u8 x, u8 y, u8 width, u8 height)
{
struct Window windowLocal = gWindows[windowId];
u16 currentRow = windowLocal.window.baseBlock + (y * windowLocal.window.width) + x + GetBgAttribute(windowLocal.window.bg, BG_ATTR_BASETILE);
int i;
for (i = 0; i < height; ++i)
{
WriteSequenceToBgTilemapBuffer(
windowLocal.window.bg,
currentRow,
windowLocal.window.tilemapLeft + x,
windowLocal.window.tilemapTop + y + i,
width,
1,
windowLocal.window.paletteNum,
1);
currentRow += windowLocal.window.width;
}
}
void BlitBitmapToWindow(u8 windowId, const u8 *pixels, u16 x, u16 y, u16 width, u16 height)
{
BlitBitmapRectToWindow(windowId, pixels, 0, 0, width, height, x, y, width, height);
}
void BlitBitmapRectToWindow(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight)
{
struct Bitmap sourceRect;
struct Bitmap destRect;
sourceRect.pixels = (u8 *)pixels;
sourceRect.width = srcWidth;
sourceRect.height = srcHeight;
destRect.pixels = gWindows[windowId].tileData;
destRect.width = 8 * gWindows[windowId].window.width;
destRect.height = 8 * gWindows[windowId].window.height;
BlitBitmapRect4Bit(&sourceRect, &destRect, srcX, srcY, destX, destY, rectWidth, rectHeight, 0);
}
static void UNUSED BlitBitmapRectToWindowWithColorKey(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight, u8 colorKey)
{
struct Bitmap sourceRect;
struct Bitmap destRect;
sourceRect.pixels = (u8 *)pixels;
sourceRect.width = srcWidth;
sourceRect.height = srcHeight;
destRect.pixels = gWindows[windowId].tileData;
destRect.width = 8 * gWindows[windowId].window.width;
destRect.height = 8 * gWindows[windowId].window.height;
BlitBitmapRect4Bit(&sourceRect, &destRect, srcX, srcY, destX, destY, rectWidth, rectHeight, colorKey);
}
void FillWindowPixelRect(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height)
{
struct Bitmap pixelRect;
pixelRect.pixels = gWindows[windowId].tileData;
pixelRect.width = 8 * gWindows[windowId].window.width;
pixelRect.height = 8 * gWindows[windowId].window.height;
FillBitmapRect4Bit(&pixelRect, x, y, width, height, fillValue);
}
void CopyToWindowPixelBuffer(u8 windowId, const void *src, u16 size, u16 tileOffset)
{
if (size != 0)
CpuCopy16(src, gWindows[windowId].tileData + (32 * tileOffset), size);
else
LZ77UnCompWram(src, gWindows[windowId].tileData + (32 * tileOffset));
}
// Sets all pixels within the window to the fillValue color.
void FillWindowPixelBuffer(u8 windowId, u8 fillValue)
{
int fillSize = gWindows[windowId].window.width * gWindows[windowId].window.height;
CpuFastFill8(fillValue, gWindows[windowId].tileData, 32 * fillSize);
}
#define MOVE_TILES_DOWN(a) \
{ \
destOffset = i + (a); \
srcOffset = i + (((width * (distanceLoop & ~7)) | (distanceLoop & 7)) * 4); \
if (srcOffset < size) \
*(u32 *)(tileData + destOffset) = *(u32 *)(tileData + srcOffset); \
else \
*(u32 *)(tileData + destOffset) = fillValue32; \
distanceLoop++; \
}
#define MOVE_TILES_UP(a) \
{ \
destOffset = i + (a); \
srcOffset = i + (((width * (distanceLoop & ~7)) | (distanceLoop & 7)) * 4); \
if (srcOffset < size) \
*(u32 *)(tileData - destOffset) = *(u32 *)(tileData - srcOffset); \
else \
*(u32 *)(tileData - destOffset) = fillValue32; \
distanceLoop++; \
}
void ScrollWindow(u8 windowId, u8 direction, u8 distance, u8 fillValue)
{
struct WindowTemplate window = gWindows[windowId].window;
u8 *tileData = gWindows[windowId].tileData;
u32 fillValue32 = (fillValue << 24) | (fillValue << 16) | (fillValue << 8) | fillValue;
s32 size = window.height * window.width * 32;
u32 width = window.width;
s32 i;
s32 srcOffset, destOffset;
u32 distanceLoop;
switch (direction)
{
case 0:
for (i = 0; i < size; i += 32)
{
distanceLoop = distance;
MOVE_TILES_DOWN(0)
MOVE_TILES_DOWN(4)
MOVE_TILES_DOWN(8)
MOVE_TILES_DOWN(12)
MOVE_TILES_DOWN(16)
MOVE_TILES_DOWN(20)
MOVE_TILES_DOWN(24)
MOVE_TILES_DOWN(28)
}
break;
case 1:
tileData += size - 4;
for (i = 0; i < size; i += 32)
{
distanceLoop = distance;
MOVE_TILES_UP(0)
MOVE_TILES_UP(4)
MOVE_TILES_UP(8)
MOVE_TILES_UP(12)
MOVE_TILES_UP(16)
MOVE_TILES_UP(20)
MOVE_TILES_UP(24)
MOVE_TILES_UP(28)
}
break;
case 2:
break;
}
}
void CallWindowFunction(u8 windowId, void ( *func)(u8, u8, u8, u8, u8, u8))
{
struct WindowTemplate window = gWindows[windowId].window;
func(window.bg, window.tilemapLeft, window.tilemapTop, window.width, window.height, window.paletteNum);
}
bool8 SetWindowAttribute(u8 windowId, u8 attributeId, u32 value)
{
switch (attributeId)
{
case WINDOW_TILEMAP_LEFT:
gWindows[windowId].window.tilemapLeft = value;
return FALSE;
case WINDOW_TILEMAP_TOP:
gWindows[windowId].window.tilemapTop = value;
return FALSE;
case WINDOW_PALETTE_NUM:
gWindows[windowId].window.paletteNum = value;
return FALSE;
case WINDOW_BASE_BLOCK:
gWindows[windowId].window.baseBlock = value;
return FALSE;
case WINDOW_TILE_DATA:
gWindows[windowId].tileData = (u8 *)(value);
return TRUE;
case WINDOW_BG:
case WINDOW_WIDTH:
case WINDOW_HEIGHT:
default:
return TRUE;
}
}
u32 GetWindowAttribute(u8 windowId, u8 attributeId)
{
switch (attributeId)
{
case WINDOW_BG:
return gWindows[windowId].window.bg;
case WINDOW_TILEMAP_LEFT:
return gWindows[windowId].window.tilemapLeft;
case WINDOW_TILEMAP_TOP:
return gWindows[windowId].window.tilemapTop;
case WINDOW_WIDTH:
return gWindows[windowId].window.width;
case WINDOW_HEIGHT:
return gWindows[windowId].window.height;
case WINDOW_PALETTE_NUM:
return gWindows[windowId].window.paletteNum;
case WINDOW_BASE_BLOCK:
return gWindows[windowId].window.baseBlock;
case WINDOW_TILE_DATA:
return (u32)(gWindows[windowId].tileData);
default:
return 0;
}
}
static u8 GetNumActiveWindowsOnBg(u8 bgId)
{
u8 windowsNum = 0;
s32 i;
for (i = 0; i < WINDOWS_MAX; i++)
{
if (gWindows[i].window.bg == bgId)
windowsNum++;
}
return windowsNum;
}
static void DummyWindowBgTilemap8Bit(void)
{
}
u16 AddWindow8Bit(const struct WindowTemplate *template)
{
u16 windowId;
u8 *memAddress;
u8 bgLayer;
for (windowId = 0; windowId < WINDOWS_MAX; windowId++)
{
if (gWindows[windowId].window.bg == 0xFF)
break;
}
if (windowId == WINDOWS_MAX)
return WINDOW_NONE;
bgLayer = template->bg;
if (gWindowBgTilemapBuffers[bgLayer] == NULL)
{
u16 attribute = GetBgAttribute(bgLayer, BG_ATTR_METRIC);
if (attribute != 0xFFFF)
{
s32 i;
memAddress = Alloc(attribute);
if (memAddress == NULL)
return WINDOW_NONE;
for (i = 0; i < attribute; i++) // if we're going to zero out the memory anyway, why not call AllocZeroed?
memAddress[i] = 0;
gWindowBgTilemapBuffers[bgLayer] = memAddress;
SetBgTilemapBuffer(bgLayer, memAddress);
}
}
memAddress = Alloc((u16)(64 * (template->width * template->height)));
if (memAddress == NULL)
{
if (GetNumActiveWindowsOnBg8Bit(bgLayer) == 0 && gWindowBgTilemapBuffers[bgLayer] != DummyWindowBgTilemap8Bit)
{
Free(gWindowBgTilemapBuffers[bgLayer]);
gWindowBgTilemapBuffers[bgLayer] = NULL;
}
return WINDOW_NONE;
}
else
{
gWindows[windowId].tileData = memAddress;
gWindows[windowId].window = *template;
return windowId;
}
}
void FillWindowPixelBuffer8Bit(u8 windowId, u8 fillValue)
{
s32 i;
s32 size;
size = (u16)(64 * (gWindows[windowId].window.width * gWindows[windowId].window.height));
for (i = 0; i < size; i++)
gWindows[windowId].tileData[i] = fillValue;
}
void FillWindowPixelRect8Bit(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height)
{
struct Bitmap pixelRect;
pixelRect.pixels = gWindows[windowId].tileData;
pixelRect.width = 8 * gWindows[windowId].window.width;
pixelRect.height = 8 * gWindows[windowId].window.height;
FillBitmapRect8Bit(&pixelRect, x, y, width, height, fillValue);
}
void BlitBitmapRectToWindow4BitTo8Bit(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight, u8 paletteNum)
{
struct Bitmap sourceRect;
struct Bitmap destRect;
sourceRect.pixels = (u8 *) pixels;
sourceRect.width = srcWidth;
sourceRect.height = srcHeight;
destRect.pixels = gWindows[windowId].tileData;
destRect.width = 8 * gWindows[windowId].window.width;
destRect.height = 8 * gWindows[windowId].window.height;
BlitBitmapRect4BitTo8Bit(&sourceRect, &destRect, srcX, srcY, destX, destY, rectWidth, rectHeight, 0, paletteNum);
}
void CopyWindowToVram8Bit(u8 windowId, u8 mode)
{
sWindowPtr = &gWindows[windowId];
sWindowSize = 64 * (sWindowPtr->window.width * sWindowPtr->window.height);
switch (mode)
{
case COPYWIN_MAP:
CopyBgTilemapBufferToVram(sWindowPtr->window.bg);
break;
case COPYWIN_GFX:
LoadBgTiles(sWindowPtr->window.bg, sWindowPtr->tileData, sWindowSize, sWindowPtr->window.baseBlock);
break;
case COPYWIN_FULL:
LoadBgTiles(sWindowPtr->window.bg, sWindowPtr->tileData, sWindowSize, sWindowPtr->window.baseBlock);
CopyBgTilemapBufferToVram(sWindowPtr->window.bg);
break;
}
}
static u8 GetNumActiveWindowsOnBg8Bit(u8 bgId)
{
u8 windowsNum = 0;
s32 i;
for (i = 0; i < WINDOWS_MAX; i++)
{
if (gWindows[i].window.bg == bgId)
windowsNum++;
}
return windowsNum;
}