library: Cleanup and translate some source files

This commit is contained in:
Antonio Niño Díaz 2023-05-26 00:54:32 +01:00
parent 0428b7a1ea
commit 0329934b90
4 changed files with 242 additions and 295 deletions

View File

@ -81,21 +81,21 @@ void NF_Reset16bitsBgBuffers(void);
/// Use this function once before using the backbuffer.
///
/// @param screen Screen (0 - 1).
void NF_Init16bitsBackBuffer(u8 screen);
void NF_Init16bitsBackBuffer(int screen);
/// Enables the 16-bit backbuffer of the selected screen.
///
/// If the backbuffer is already enabled, the contents are cleared.
///
/// @param screen Screen (0 - 1).
void NF_Enable16bitsBackBuffer(u8 screen);
void NF_Enable16bitsBackBuffer(int screen);
/// Disables the 16-bit backbuffer of selected screen.
///
/// It frees the RAM used by it (128 KB).
///
/// @param screen Screen (0 - 1).
void NF_Disble16bitsBackBuffer(u8 screen);
void NF_Disble16bitsBackBuffer(int screen);
/// Sends the 16-bit backbuffer to the VRAM of the selected screen.
///
@ -103,7 +103,7 @@ void NF_Disble16bitsBackBuffer(u8 screen);
/// on the screen without showing the drawing process (which may be very slow).
///
/// @param screen Screen (0 - 1).
void NF_Flip16bitsBackBuffer(u8 screen);
void NF_Flip16bitsBackBuffer(int screen);
/// Initializes the selected screen in "bitmap" mode.
///
@ -119,7 +119,7 @@ void NF_Flip16bitsBackBuffer(u8 screen);
///
/// @param screen Screen (0 - 1).
/// @param mode Depth mode (0: 8 bits / 256 colors; 1: 16 bits)
void NF_InitBitmapBgSys(u8 screen, u8 mode);
void NF_InitBitmapBgSys(int screen, u32 mode);
/// Loads a 16-bit bitmap from the filesystem.
///
@ -142,11 +142,11 @@ void NF_InitBitmapBgSys(u8 screen, u8 mode);
///
/// @param file File name without extension.
/// @param slot Slot number (0 - 15).
void NF_Load16bitsBg(const char *file, u8 slot);
void NF_Load16bitsBg(const char *file, u32 slot);
/// Loads a 16 bits image into a RAM slot.
///
/// The image must be in ".img" format, and it has a max size of 256x256 pixels
/// The image must be in ".img" format, and it has a max size of 256x256 pixels
/// You must also specify the size of the image. The image will be loaded into
/// the specified 16-bits bitmap background slot.
///
@ -163,10 +163,10 @@ void NF_Load16bitsBg(const char *file, u8 slot);
/// @param slot Slot number (0 - 15).
/// @param size_x Width in pixels.
/// @param size_y Height in pixels.
void NF_Load16bitsImage(const char *file, u8 slot, u16 size_x, u16 size_y);
void NF_Load16bitsImage(const char *file, u32 slot, u32 size_x, u32 size_y);
// Internal use only. Generic loader of 16-bit data to RAM.
void NF_Load16bImgData(const char *file, u8 slot, u16 x, u16 y, u8 type);
void NF_Load16bImgData(const char *file, u32 slot, u32 x, u32 y, u32 type);
/// Deletes from RAM the 16-bit image stored in the selected slot.
///
@ -180,7 +180,7 @@ void NF_Load16bImgData(const char *file, u8 slot, u16 x, u16 y, u8 type);
/// ```
///
/// @param slot Slot number (0 - 15).
void NF_Unload16bitsBg(u8 slot);
void NF_Unload16bitsBg(u32 slot);
/// Copy the selected 16-bit slot to VRAM or the backbuffer.
///
@ -193,7 +193,7 @@ void NF_Unload16bitsBg(u8 slot);
/// @param screen Screen (0 - 1).
/// @param destination Use 0 for VRAM or 1 for backbuffer.
/// @param slot Slot number (0 - 15).
void NF_Copy16bitsBuffer(u8 screen, u8 destination, u8 slot);
void NF_Copy16bitsBuffer(int screen, int destination, u32 slot);
/// Draws the image in a slot into the backbuffer of the selected screen.
///
@ -213,7 +213,7 @@ void NF_Copy16bitsBuffer(u8 screen, u8 destination, u8 slot);
/// @param x X coordinate.
/// @param y Y coordinate.
/// @param alpha True to make magenta pixels transparent.
void NF_Draw16bitsImage(u8 screen, u8 slot, s16 x, s16 y, bool alpha);
void NF_Draw16bitsImage(int screen, u32 slot, s32 x, s32 y, bool alpha);
/// Initialize buffers to store 8-bit bitmap backgrounds.
///
@ -256,7 +256,7 @@ void NF_Reset8bitsBgBuffers(void);
///
/// @param file File name without extension.
/// @param slot Slot number (0 - 15).
void NF_Load8bitsBg(const char *file, u8 slot);
void NF_Load8bitsBg(const char *file, u32 slot);
/// Deletes from RAM the 8-bit image stored in the selected slot.
///
@ -270,7 +270,7 @@ void NF_Load8bitsBg(const char *file, u8 slot);
/// ```
///
/// @param slot Slot number (0 - 15).
void NF_Unload8bitsBg(u8 slot);
void NF_Unload8bitsBg(u32 slot);
/// Copy the selected 8-bit slot to VRAM or the backbuffer.
///
@ -283,28 +283,28 @@ void NF_Unload8bitsBg(u8 slot);
/// @param screen Screen (0 - 1).
/// @param destination Use 0 for VRAM or 1 for backbuffer.
/// @param slot Slot number (0 - 15).
void NF_Copy8bitsBuffer(u8 screen, u8 destination, u8 slot);
void NF_Copy8bitsBuffer(int screen, int destination, u32 slot);
/// Initialize the 8 bit background backbuffer of the selected screen.
///
/// Use this function once before using the backbuffer.
///
/// @param screen Screen (0 - 1).
void NF_Init8bitsBackBuffer(u8 screen);
void NF_Init8bitsBackBuffer(int screen);
/// Enables the 8-bit backbuffer of the selected screen.
///
/// If the backbuffer is already enabled, the contents are cleared.
///
/// @param screen Screen (0 - 1).
void NF_Enable8bitsBackBuffer(u8 screen);
void NF_Enable8bitsBackBuffer(int screen);
/// Disables the 8-bit backbuffer of selected screen.
///
/// It frees the RAM used by it (64 KB).
///
/// @param screen Screen (0 - 1).
void NF_Disble8bitsBackBuffer(u8 screen);
void NF_Disble8bitsBackBuffer(int screen);
/// Sends the 8-bit backbuffer to the VRAM of the selected screen.
///
@ -315,7 +315,7 @@ void NF_Disble8bitsBackBuffer(u8 screen);
///
/// @param screen Screen (0 - 1).
/// @param destination Destination layer (0: layer 2, 1: layer 3).
void NF_Flip8bitsBackBuffer(u8 screen, u8 destination);
void NF_Flip8bitsBackBuffer(int screen, int destination);
/// @}

View File

@ -72,12 +72,12 @@ void NF_ResetCmapBuffers(void);
/// @param id Slot number (0 - 31)
/// @param width Map width (in pixels)
/// @param height Map height (in pixels)
void NF_LoadCollisionMap(const char *file, u8 id, u16 width, u16 height);
void NF_LoadCollisionMap(const char *file, u32 id, u32 width, u32 height);
/// Unload from RAM the collision map at the specified slot.
///
/// @param id Slot number (0 - 31)
void NF_UnloadCollisionMap(u8 id);
void NF_UnloadCollisionMap(u32 id);
/// Return the tile number at the specified position.
///
@ -87,7 +87,7 @@ void NF_UnloadCollisionMap(u8 id);
/// @param x X coordinate in pixels.
/// @param y Y coordinate in pixels.
/// @return Tile index.
u16 NF_GetTile(u8 slot, s32 x, s32 y);
u32 NF_GetTile(u32 slot, s32 x, s32 y);
/// Set the value of the tile of a collision map at the specified position.
///
@ -95,7 +95,7 @@ u16 NF_GetTile(u8 slot, s32 x, s32 y);
/// @param x X coordinate in pixels.
/// @param y Y coordinate in pixels.
/// @param value New tile.
void NF_SetTile(u8 slot, s32 x, s32 y, u16 value);
void NF_SetTile(u32 slot, s32 x, s32 y, u32 value);
/// Load a collision background to RAM at the specified slot.
///
@ -111,12 +111,12 @@ void NF_SetTile(u8 slot, s32 x, s32 y, u16 value);
/// @param id Slot number (0 - 31)
/// @param width Map width (in pixels)
/// @param height Map height (in pixels)
void NF_LoadCollisionBg(const char *file, u8 id, u16 width, u16 height);
void NF_LoadCollisionBg(const char *file, u32 id, u32 width, u32 height);
/// Unload from RAM the collision background at the specified slot.
///
/// @param id Slot number (0 - 31)
void NF_UnloadCollisionBg(u8 id);
void NF_UnloadCollisionBg(u32 id);
/// Returns the color number at the specified coordinates.
///
@ -126,7 +126,7 @@ void NF_UnloadCollisionBg(u8 id);
/// @param x X coordinate in pixels.
/// @param y Y coordinate in pixels.
/// @return Tile index (0 - 255).
u8 NF_GetPoint(u8 slot, s32 x, s32 y);
u8 NF_GetPoint(u32 slot, s32 x, s32 y);
// Defines for backwards compatibility
#define NF_LoadColisionMap NF_LoadCollisionMap

View File

@ -48,7 +48,7 @@ void NF_Reset16bitsBgBuffers(void)
NF_Init16bitsBgBuffers();
}
void NF_Init16bitsBackBuffer(u8 screen)
void NF_Init16bitsBackBuffer(int screen)
{
if (screen > 1)
screen = 1;
@ -56,7 +56,7 @@ void NF_Init16bitsBackBuffer(u8 screen)
NF_16BITS_BACKBUFFER[screen] = NULL;
}
void NF_Enable16bitsBackBuffer(u8 screen)
void NF_Enable16bitsBackBuffer(int screen)
{
if (screen > 1)
screen = 1;
@ -73,7 +73,7 @@ void NF_Enable16bitsBackBuffer(u8 screen)
NF_Error(102, NULL, 131072);
}
void NF_Disble16bitsBackBuffer(u8 screen)
void NF_Disble16bitsBackBuffer(int screen)
{
if (screen > 1)
screen = 1;
@ -83,7 +83,7 @@ void NF_Disble16bitsBackBuffer(u8 screen)
NF_16BITS_BACKBUFFER[screen] = NULL;
}
void NF_Flip16bitsBackBuffer(u8 screen)
void NF_Flip16bitsBackBuffer(int screen)
{
// Copy contents of the backuffer to VRAM
if (screen == 0)
@ -92,7 +92,7 @@ void NF_Flip16bitsBackBuffer(u8 screen)
NF_DmaMemCopy((void *)0x06200000, NF_16BITS_BACKBUFFER[1], 131072);
}
void NF_InitBitmapBgSys(u8 screen, u8 mode)
void NF_InitBitmapBgSys(int screen, u32 mode)
{
// Setup layer 3 (and optionally layer 2) of the selected screen as a bitmap
@ -181,17 +181,17 @@ void NF_InitBitmapBgSys(u8 screen, u8 mode)
}
}
void NF_Load16bitsBg(const char *file, u8 slot)
void NF_Load16bitsBg(const char *file, u32 slot)
{
NF_Load16bImgData(file, slot, 256, 256, 0);
}
void NF_Load16bitsImage(const char *file, u8 slot, u16 size_x, u16 size_y)
void NF_Load16bitsImage(const char *file, u32 slot, u32 size_x, u32 size_y)
{
NF_Load16bImgData(file, slot, size_x, size_y, 1);
}
void NF_Load16bImgData(const char *file, u8 slot, u16 x, u16 y, u8 type)
void NF_Load16bImgData(const char *file, u32 slot, u32 x, u32 y, u32 type)
{
// Verify that the slot is in the valid range
if (slot >= NF_SLOTS_BG16B)
@ -245,7 +245,7 @@ void NF_Load16bImgData(const char *file, u8 slot, u16 x, u16 y, u8 type)
NF_BG16B[slot].inuse = true; // Set slot as being used
}
void NF_Unload16bitsBg(u8 slot)
void NF_Unload16bitsBg(u32 slot)
{
// Verify that the slot contains data
if (!NF_BG16B[slot].inuse)
@ -259,7 +259,7 @@ void NF_Unload16bitsBg(u8 slot)
NF_BG16B[slot].inuse = false; // Mark slot as being free
}
void NF_Copy16bitsBuffer(u8 screen, u8 destination, u8 slot)
void NF_Copy16bitsBuffer(int screen, int destination, u32 slot)
{
// Verify that the slot contains data
if (!NF_BG16B[slot].inuse)
@ -281,7 +281,7 @@ void NF_Copy16bitsBuffer(u8 screen, u8 destination, u8 slot)
}
}
void NF_Draw16bitsImage(u8 screen, u8 slot, s16 x, s16 y, bool alpha)
void NF_Draw16bitsImage(int screen, u32 slot, s32 x, s32 y, bool alpha)
{
// Verify that the slot contains data
if (!NF_BG16B[slot].inuse)
@ -339,7 +339,7 @@ void NF_Reset8bitsBgBuffers(void)
NF_Init8bitsBgBuffers();
}
void NF_Load8bitsBg(const char *file, u8 slot)
void NF_Load8bitsBg(const char *file, u32 slot)
{
// Verify that the slot is in the valid range
if (slot >= NF_SLOTS_BG8B)
@ -410,7 +410,7 @@ void NF_Load8bitsBg(const char *file, u8 slot)
NF_BG8B[slot].inuse = true;
}
void NF_Unload8bitsBg(u8 slot)
void NF_Unload8bitsBg(u32 slot)
{
// Verify that the slot contains data
if (!NF_BG8B[slot].inuse)
@ -427,7 +427,7 @@ void NF_Unload8bitsBg(u8 slot)
NF_BG8B[slot].inuse = false; // Mark slot as free
}
void NF_Copy8bitsBuffer(u8 screen, u8 destination, u8 slot)
void NF_Copy8bitsBuffer(int screen, int destination, u32 slot)
{
// Verify that the slot contains data
if (!NF_BG8B[slot].inuse) NF_Error(110, "8 Bits Bg", slot);
@ -468,7 +468,7 @@ void NF_Copy8bitsBuffer(u8 screen, u8 destination, u8 slot)
}
}
void NF_Init8bitsBackBuffer(u8 screen)
void NF_Init8bitsBackBuffer(int screen)
{
if (screen > 1)
screen = 1;
@ -477,7 +477,7 @@ void NF_Init8bitsBackBuffer(u8 screen)
NF_8BITS_BACKBUFFER[screen].pal = NULL;
}
void NF_Enable8bitsBackBuffer(u8 screen)
void NF_Enable8bitsBackBuffer(int screen)
{
if (screen > 1)
screen = 1;
@ -499,7 +499,7 @@ void NF_Enable8bitsBackBuffer(u8 screen)
NF_Error(102, NULL, 512);
}
void NF_Disble8bitsBackBuffer(u8 screen)
void NF_Disble8bitsBackBuffer(int screen)
{
if (screen > 1)
screen = 1;
@ -511,7 +511,7 @@ void NF_Disble8bitsBackBuffer(u8 screen)
NF_8BITS_BACKBUFFER[screen].pal = NULL;
}
void NF_Flip8bitsBackBuffer(u8 screen, u8 destination)
void NF_Flip8bitsBackBuffer(int screen, int destination)
{
// Copy contents of the backuffer to VRAM

View File

@ -7,321 +7,268 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nds.h>
#include "nf_basic.h"
#include "nf_collision.h"
// Define los buffers y estructuras de control de los mapas de colision
// Struct that holds information about all collision maps
NF_TYPE_CMAP_INFO NF_CMAP[NF_SLOTS_CMAP];
void NF_InitCmapBuffers(void) {
for (int n = 0; n < NF_SLOTS_CMAP; n ++) {
NF_CMAP[n].tiles = NULL; // Inicializa los punteros de los buffers
NF_CMAP[n].map = NULL;
NF_CMAP[n].tiles_size = 0; // Tamaño de archivo
NF_CMAP[n].map_size = 0;
NF_CMAP[n].width = 0; // Ancho del mapa
NF_CMAP[n].height = 0; // Alto del mapa
NF_CMAP[n].inuse = false; // Esta en uso el slot?
}
void NF_InitCmapBuffers(void)
{
for (int n = 0; n < NF_SLOTS_CMAP; n++)
{
NF_CMAP[n].tiles = NULL;
NF_CMAP[n].map = NULL;
NF_CMAP[n].tiles_size = 0;
NF_CMAP[n].map_size = 0;
NF_CMAP[n].width = 0;
NF_CMAP[n].height = 0;
NF_CMAP[n].inuse = false; // Mark as unused
}
}
void NF_ResetCmapBuffers(void) {
for (int n = 0; n < NF_SLOTS_CMAP; n ++) {
free(NF_CMAP[n].tiles); // Vacia los buffers
free(NF_CMAP[n].map);
}
NF_InitCmapBuffers(); // Y reinicia todas las variables
void NF_ResetCmapBuffers(void)
{
// Free all buffers
for (int n = 0; n < NF_SLOTS_CMAP; n++)
{
free(NF_CMAP[n].tiles);
free(NF_CMAP[n].map);
}
// Reset state of the collision maps
NF_InitCmapBuffers();
}
void NF_LoadCollisionMap(const char* file, u8 id, u16 width, u16 height) {
void NF_LoadCollisionMap(const char *file, u32 id, u32 width, u32 height)
{
// Verify that the slot ID is valid
if (id >= NF_SLOTS_CMAP)
NF_Error(106, "Collision Map", NF_SLOTS_CMAP);
// Verifica el rango de Id's
if (id >= NF_SLOTS_CMAP) {
NF_Error(106, "Collision Map", NF_SLOTS_CMAP);
}
// Verify that this slot is free
if (NF_CMAP[id].inuse)
NF_Error(109, "Collision Map", id);
// Verifica si la Id esta libre
if (NF_CMAP[id].inuse) {
NF_Error(109, "Collision Map", id);
}
// Free buffers if they were in use
free(NF_CMAP[id].map);
NF_CMAP[id].map = NULL;
// Vacia los buffers que se usaran
free(NF_CMAP[id].map);
NF_CMAP[id].map = NULL;
// File path
char filename[256];
// Declara los punteros a los ficheros
FILE* file_id;
// Load .CMP file (tilemap)
snprintf(filename, sizeof(filename), "%s/%s.cmp", NF_ROOTFOLDER, file);
FILE *file_id = fopen(filename, "rb");
if (file_id == NULL)
NF_Error(101, filename, 0);
// Variable para almacenar el path al archivo
char filename[256];
// Get file size
fseek(file_id, 0, SEEK_END);
NF_CMAP[id].map_size = ftell(file_id);
rewind(file_id);
// Carga el archivo .CMP
snprintf(filename, sizeof(filename), "%s/%s.cmp", NF_ROOTFOLDER, file);
file_id = fopen(filename, "rb");
if (file_id) { // Si el archivo existe...
// Obten el tamaño del archivo
fseek(file_id, 0, SEEK_END);
NF_CMAP[id].map_size = ftell(file_id);
rewind(file_id);
// Reserva el espacio en RAM
NF_CMAP[id].map = (char*) calloc (NF_CMAP[id].map_size, sizeof(char));
if (NF_CMAP[id].map == NULL) { // Si no hay suficiente RAM libre
NF_Error(102, NULL, NF_CMAP[id].map_size);
}
// Lee el archivo y ponlo en la RAM
fread(NF_CMAP[id].map, 1, NF_CMAP[id].map_size, file_id);
} else { // Si el archivo no existe...
NF_Error(101, filename, 0);
}
fclose(file_id); // Cierra el archivo
// Allocate space in RAM
NF_CMAP[id].map = malloc(NF_CMAP[id].map_size);
if (NF_CMAP[id].map == NULL)
NF_Error(102, NULL, NF_CMAP[id].map_size);
// Guarda las medidas
NF_CMAP[id].width = width;
NF_CMAP[id].height = height;
// Load file into RAM
fread(NF_CMAP[id].map, 1, NF_CMAP[id].map_size, file_id);
fclose(file_id);
// Y marca esta ID como usada
NF_CMAP[id].inuse = true;
// Save map size
NF_CMAP[id].width = width;
NF_CMAP[id].height = height;
// Mark this slot as in use
NF_CMAP[id].inuse = true;
}
void NF_UnloadCollisionMap(u8 id) {
void NF_UnloadCollisionMap(u32 id)
{
// Verify that the slot ID is valid
if (id >= NF_SLOTS_CMAP)
NF_Error(106, "Collision Map", NF_SLOTS_CMAP);
// Verifica el rango de Id's
if (id >= NF_SLOTS_CMAP) {
NF_Error(106, "Collision Map", NF_SLOTS_CMAP);
}
// Verify that this slot is used
if (!NF_CMAP[id].inuse)
NF_Error(110, "Collision Map", id);
// Verifica si la Id esta libre
if (!NF_CMAP[id].inuse) {
NF_Error(110, "Collision Map", id);
}
// Vacia los buffers que se usaran
free(NF_CMAP[id].map);
NF_CMAP[id].map = NULL;
// Y marca esta ID como usada
NF_CMAP[id].inuse = false;
// Free buffer
free(NF_CMAP[id].map);
NF_CMAP[id].map = NULL;
// Mark this slot as free
NF_CMAP[id].inuse = false;
}
u16 NF_GetTile(u8 slot, s32 x, s32 y) {
u32 NF_GetTile(u32 slot, s32 x, s32 y)
{
// If the coordinate is outside of the map, return 0
if ((x < 0) || (y < 0) || (x >= NF_CMAP[slot].width) || (y >= NF_CMAP[slot].height))
return 0;
// Si la coordenada esta fuera de rango, devuelve 0
if (
(x < 0)
||
(y < 0)
||
(x >= NF_CMAP[slot].width)
||
(y >= NF_CMAP[slot].height)
) {
// Devuelve 0
return 0;
// Calculate width of the map in tiles
u32 columns = NF_CMAP[slot].width / 8;
} else { // Si la coordenada esta dentro del rango...
// Calculate tile where the pixel is located
u32 tile_x = x / 8;
u32 tile_y = (y / 8) + 1; // Skip first row, it's used for the tile reference
// Calcula el ancho en tiles del mapa
u16 columns = (NF_CMAP[slot].width >> 3); // (width / 8);
// Calculate the address of the tile in the map
u32 address = ((tile_y * columns) + tile_x) * 2;
// Calcula los tiles de posicion (x / 8); (y / 8);
u16 tile_x = (x >> 3);
u16 tile_y = (y >> 3) + 1; // +1, por que la primera fila se reserva para la referencia de tiles
// Calcula el nº de tile
u32 address = (((tile_y * columns) + tile_x) << 1);
// Obten los bytes
u8 lobyte = *(NF_CMAP[slot].map + address);
u8 hibyte = *(NF_CMAP[slot].map + (address + 1));
// Devuelve el valor del tile
return ((hibyte << 8) | lobyte);
}
// Read tile number
u32 lobyte = *(NF_CMAP[slot].map + address);
u32 hibyte = *(NF_CMAP[slot].map + (address + 1));
return (hibyte << 8) | lobyte;
}
void NF_SetTile(u8 slot, s32 x, s32 y, u16 value) {
void NF_SetTile(u32 slot, s32 x, s32 y, u32 value)
{
// If the coordinate is outside of the map, return
if ((x < 0) && (y < 0) && (x >= NF_CMAP[slot].width) && (y >= NF_CMAP[slot].height))
return;
// Si la coordenada esta dentro del rango...
if (
(x >= 0)
&&
(y >= 0)
&&
(x < NF_CMAP[slot].width)
&&
(y < NF_CMAP[slot].height)
) {
// Calculate width of the map in tiles
u32 columns = NF_CMAP[slot].width / 8;
// Calcula el ancho en tiles del mapa
u16 columns = (NF_CMAP[slot].width >> 3); // (width / 8);
// Calculate tile where the pixel is located
u32 tile_x = x / 8;
u32 tile_y = (y / 8) + 1; // Skip first row, it's used for the tile reference
// Calcula los tiles de posicion (x / 8); (y / 8);
u16 tile_x = (x >> 3);
u16 tile_y = (y >> 3) + 1; // +1, por que la primera fila se reserva para la referencia de tiles
// Calculate the address of the tile in the map
s32 address = ((tile_y * columns) + tile_x) * 2; // Each tile uses 2 bytes
// Calcula el nº de tile
u32 address = (((tile_y * columns) + tile_x) << 1);
// nº de tile x2, dado que el mapa es de 16 bits (2 bytes por dato) y el buffer
// es de 8 bits, se lee el 2do byte, por eso se multiplica por 2.
// Calcula los valores de los bytes
u8 hibyte = ((value >> 8) & 0xff); // HI Byte
u8 lobyte = (value & 0xff); // LO Byte
// Escribe el nuevo valor en el mapa de colisiones
*(NF_CMAP[slot].map + address) = lobyte;
*(NF_CMAP[slot].map + (address + 1)) = hibyte;
}
// Split new tile number
u32 hibyte = (value >> 8) & 0xff;
u32 lobyte = value & 0xff;
// Write the new tile number
*(NF_CMAP[slot].map + address) = lobyte;
*(NF_CMAP[slot].map + (address + 1)) = hibyte;
}
void NF_LoadCollisionBg(const char* file, u8 id, u16 width, u16 height) {
void NF_LoadCollisionBg(const char *file, u32 id, u32 width, u32 height)
{
// Verify that the slot ID is valid
if (id >= NF_SLOTS_CMAP)
NF_Error(106, "Collision map", NF_SLOTS_CMAP);
// Verifica el rango de Id's
if (id >= NF_SLOTS_CMAP) {
NF_Error(106, "Collision Map", NF_SLOTS_CMAP);
}
// Verify that this slot is free
if (NF_CMAP[id].inuse)
NF_Error(109, "Collision map", id);
// Verifica si la Id esta libre
if (NF_CMAP[id].inuse) {
NF_Error(109, "Collision Map", id);
}
// Free buffers if they were in use
free(NF_CMAP[id].tiles);
NF_CMAP[id].tiles = NULL;
free(NF_CMAP[id].map);
NF_CMAP[id].map = NULL;
// Vacia los buffers que se usaran
free(NF_CMAP[id].tiles);
NF_CMAP[id].tiles = NULL;
free(NF_CMAP[id].map);
NF_CMAP[id].map = NULL;
// File path
char filename[256];
// Declara los punteros a los ficheros
FILE* file_id;
// Load .DAT file (tileset)
snprintf(filename, sizeof(filename), "%s/%s.dat", NF_ROOTFOLDER, file);
FILE *file_id = fopen(filename, "rb");
if (file_id == NULL)
NF_Error(101, filename, 0);
// Variable para almacenar el path al archivo
char filename[256];
// Get file size
fseek(file_id, 0, SEEK_END);
NF_CMAP[id].tiles_size = ftell(file_id);
rewind(file_id);
// Carga el archivo .DAT (TILES)
snprintf(filename, sizeof(filename), "%s/%s.dat", NF_ROOTFOLDER, file);
file_id = fopen(filename, "rb");
if (file_id) { // Si el archivo existe...
// Obten el tamaño del archivo
fseek(file_id, 0, SEEK_END);
NF_CMAP[id].tiles_size = ftell(file_id);
rewind(file_id);
// Reserva el espacio en RAM
NF_CMAP[id].tiles = (char*) calloc (NF_CMAP[id].tiles_size, sizeof(char));
if (NF_CMAP[id].tiles == NULL) { // Si no hay suficiente RAM libre
NF_Error(102, NULL, NF_CMAP[id].tiles_size);
}
// Lee el archivo y ponlo en la RAM
fread(NF_CMAP[id].tiles, 1, NF_CMAP[id].tiles_size, file_id);
} else { // Si el archivo no existe...
NF_Error(101, filename, 0);
}
fclose(file_id); // Cierra el archivo
// Allocate space in RAM
NF_CMAP[id].tiles = malloc(NF_CMAP[id].tiles_size);
if (NF_CMAP[id].tiles == NULL)
NF_Error(102, NULL, NF_CMAP[id].tiles_size);
// Carga el archivo .CMP
snprintf(filename, sizeof(filename), "%s/%s.cmp", NF_ROOTFOLDER, file);
file_id = fopen(filename, "rb");
if (file_id) { // Si el archivo existe...
// Obten el tamaño del archivo
fseek(file_id, 0, SEEK_END);
NF_CMAP[id].map_size = ftell(file_id);
rewind(file_id);
// Reserva el espacio en RAM
NF_CMAP[id].map = (char*) calloc (NF_CMAP[id].map_size, sizeof(char));
if (NF_CMAP[id].map == NULL) { // Si no hay suficiente RAM libre
NF_Error(102, NULL, NF_CMAP[id].map_size);
}
// Lee el archivo y ponlo en la RAM
fread(NF_CMAP[id].map, 1, NF_CMAP[id].map_size, file_id);
} else { // Si el archivo no existe...
NF_Error(101, filename, 0);
}
fclose(file_id); // Cierra el archivo
// Load file into RAM
fread(NF_CMAP[id].tiles, 1, NF_CMAP[id].tiles_size, file_id);
fclose(file_id);
// Guarda las medidas
NF_CMAP[id].width = width;
NF_CMAP[id].height = height;
// Load .CMP file (tilemap)
snprintf(filename, sizeof(filename), "%s/%s.cmp", NF_ROOTFOLDER, file);
file_id = fopen(filename, "rb");
if (file_id == NULL)
NF_Error(101, filename, 0);
// Y marca esta ID como usada
NF_CMAP[id].inuse = true;
// Get file size
fseek(file_id, 0, SEEK_END);
NF_CMAP[id].map_size = ftell(file_id);
rewind(file_id);
// Allocate space in RAM
NF_CMAP[id].map = malloc(NF_CMAP[id].map_size);
if (NF_CMAP[id].map == NULL)
NF_Error(102, NULL, NF_CMAP[id].map_size);
// Load file into RAM
fread(NF_CMAP[id].map, 1, NF_CMAP[id].map_size, file_id);
fclose(file_id);
// Save map size
NF_CMAP[id].width = width;
NF_CMAP[id].height = height;
// Mark this slot as in use
NF_CMAP[id].inuse = true;
}
void NF_UnloadCollisionBg(u8 id) {
void NF_UnloadCollisionBg(u32 id)
{
// Verify that the slot ID is valid
if (id >= NF_SLOTS_CMAP)
NF_Error(106, "Collision map", NF_SLOTS_CMAP);
// Verifica el rango de Id's
if (id >= NF_SLOTS_CMAP) {
NF_Error(106, "Collision Map", NF_SLOTS_CMAP);
}
// Verify that this slot is used
if (!NF_CMAP[id].inuse)
NF_Error(110, "Collision map", id);
// Verifica si la Id esta libre
if (!NF_CMAP[id].inuse) {
NF_Error(110, "Collision Map", id);
}
// Vacia los buffers que se usaran
free(NF_CMAP[id].tiles);
NF_CMAP[id].tiles = NULL;
free(NF_CMAP[id].map);
NF_CMAP[id].map = NULL;
// Y marca esta ID como usada
NF_CMAP[id].inuse = false;
// Free the buffers
free(NF_CMAP[id].tiles);
NF_CMAP[id].tiles = NULL;
free(NF_CMAP[id].map);
NF_CMAP[id].map = NULL;
// Mark this map as unused
NF_CMAP[id].inuse = false;
}
u8 NF_GetPoint(u8 slot, s32 x, s32 y) {
u8 NF_GetPoint(u32 slot, s32 x, s32 y)
{
// If the coordinate is outside of the map, return 0
if ((x < 0) || (y < 0) || (x >= NF_CMAP[slot].width) || (y >= NF_CMAP[slot].height))
return 0;
// Si la coordenada esta fuera de rango, devuelve 0
if (
(x < 0)
||
(y < 0)
||
(x >= NF_CMAP[slot].width)
||
(y >= NF_CMAP[slot].height)
) {
// Devuelve 0
return 0;
// Calculate width of the map in tiles
u32 columns = NF_CMAP[slot].width / 8;
} else { // Si la coordenada esta dentro del rango...
// Calculate tile where the pixel is located
u32 tile_x = x / 8;
u32 tile_y = (y / 8) + 1; // Skip first row, it's used for the tile reference
// Calcula el ancho en tiles del mapa
u16 columns = (NF_CMAP[slot].width >> 3); // (width / 8);
// Calculate pixel coordinates inside the tile
u32 pixel_x = x & 7;
u32 pixel_y = y & 7;
// Calcula los tiles de posicion (x / 8); (y / 8);
u16 tile_x = (x >> 3);
u16 tile_y = (y >> 3) + 1; // +1, por que la primera fila se reserva para la referencia de tiles
// Calculate the address of the tile in the map
s32 address = ((tile_y * columns) + tile_x) * 2; // Each tile uses 2 bytes
// Calcula los pixeles relativos
u16 pixel_x = x - (tile_x << 3);
u16 pixel_y = (y + 8) - (tile_y << 3);
// Read tile number
u32 lobyte = *(NF_CMAP[slot].map + address);
u32 hibyte = *(NF_CMAP[slot].map + (address + 1));
u32 tile = (hibyte << 8) | lobyte;
// Calcula la posicion de tile dentro del archivo de mapa
s32 address = (((tile_y * columns) + tile_x) << 1);
u8 lobyte = *(NF_CMAP[slot].map + address);
u8 hibyte = *(NF_CMAP[slot].map + (address + 1));
u16 tile = ((hibyte << 8) | lobyte);
// Obten el valor del pixel leyendola del archivo de tiles
address = ((tile << 6) + (pixel_y << 3) + pixel_x); // (tile * 64) + (y * 8) + x
lobyte = *(NF_CMAP[slot].tiles + address);
// Devuelve el valor del pixel
return lobyte;
}
// Read value of the pixel inside the tile
address = (tile * 64) + (pixel_y * 8) + pixel_x;
lobyte = *(NF_CMAP[slot].tiles + address);
return lobyte;
}