102 lines
2.2 KiB
C
102 lines
2.2 KiB
C
#include "zbuffer.h"
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <gint/display.h>
|
|
#include <gint/std/stdio.h>
|
|
#include <gint/std/stdlib.h>
|
|
#include <gint/keyboard.h>
|
|
#include <gint/dma.h>
|
|
#include <gint/hardware.h>
|
|
|
|
|
|
|
|
|
|
/** size_uint32
|
|
* taille du zbuffer exprimée en uint32_t
|
|
* utile pour l'effacement du zbuffer sur sh3
|
|
**/
|
|
static const uint32_t size_uint32 = FE_ZB_SIZE_X*FE_ZB_SIZE_Y;
|
|
|
|
/** size_char
|
|
* taille du zbuffer exprimée en octets
|
|
* utile pour malloc
|
|
**/
|
|
static const uint32_t size_char = size_uint32*sizeof(int32_t);
|
|
|
|
/* size_char
|
|
taille du zbuffer exprimée en octets
|
|
utile pour le DMA */
|
|
static const uint32_t size_blocks = size_char/32;
|
|
|
|
|
|
/** address
|
|
* addresse du zbuffer
|
|
**/
|
|
static int32_t* address=0;
|
|
|
|
static const int32_t clearval[8]={3000,3000,3000,3000,3000,3000,3000,3000};
|
|
|
|
#define ALIGN 32
|
|
static void* buffer_malloc(uint_fast16_t size)
|
|
{
|
|
void *mem = malloc(size+ALIGN+sizeof(void*));
|
|
void **ptr = (void**)((uintptr_t)(mem+ALIGN+sizeof(void*)) & ~(ALIGN-1));
|
|
ptr[-1] = mem;
|
|
return ptr;
|
|
}
|
|
|
|
static void buffer_free(void *ptr)
|
|
{
|
|
free(((void**)ptr)[-1]);
|
|
}
|
|
|
|
void FE_zbuffer_clear()
|
|
{
|
|
if (address==0)
|
|
{
|
|
address = buffer_malloc(size_char);
|
|
|
|
if (address==0) // cas de figure où il n'y a plus assez de RAM
|
|
{
|
|
dclear(C_WHITE);
|
|
dtext(1, 1, "Not enough RAM...", C_BLACK, C_NONE);
|
|
dupdate();
|
|
while (1==1)
|
|
getkey();
|
|
}
|
|
}
|
|
// TODO déterminer le type d'effacement
|
|
if (isSH3())
|
|
{ // effacement CPU
|
|
uint_fast16_t indice;
|
|
for (indice = 0; indice < size_uint32; indice ++)
|
|
address[indice] = clearval[0];
|
|
}
|
|
else
|
|
{ // effacement DMA
|
|
dma_transfer(0, DMA_32B, size_blocks, &clearval, DMA_FIXED, address, DMA_INC);
|
|
}
|
|
}
|
|
|
|
void FE_start_rendering()
|
|
{
|
|
if (!isSH3())
|
|
dma_transfer_wait(0);
|
|
|
|
}
|
|
|
|
bool FE_zbuffer_set_dist(int32_t x, int32_t y, int32_t dist)
|
|
{
|
|
x %= FE_ZB_SIZE_X;
|
|
y %= FE_ZB_SIZE_Y;
|
|
const uint32_t indice = x * 64 + y;
|
|
if (address[indice]>dist && dist>0)
|
|
{
|
|
address[indice] = dist;
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|