library: Cleanup and translate some source files

This commit is contained in:
Antonio Niño Díaz 2023-05-25 02:00:31 +01:00
parent 2f13ecd7b8
commit 6fc5d73d80
2 changed files with 133 additions and 161 deletions

View File

@ -395,17 +395,13 @@ void NF_DisableSpriteRotScale(int screen, u32 sprite)
void NF_SpriteRotScale(int screen, u8 id, s32 angle, u32 sx, u32 sy) void NF_SpriteRotScale(int screen, u8 id, s32 angle, u32 sx, u32 sy)
{ {
// Temporary variables
s32 in = 0; // Input angle
s32 out = 0; // Converted angle
in = angle;
// Angle limits // Angle limits
if (in < -512) if (angle < -512)
in += 512; angle += 512;
if (in > 512) if (angle > 512)
in -= 512; angle -= 512;
angle = -angle << 6; // Switch from base 512 to base 32768
// Scale X limits // Scale X limits
if (sx > 512) if (sx > 512)
@ -415,21 +411,9 @@ void NF_SpriteRotScale(int screen, u8 id, s32 angle, u32 sx, u32 sy)
if (sy > 512) if (sy > 512)
sy = 512; sy = 512;
if (in < 0) // If the angle is negative
{
in = -in; // Make it positive (to be able to do a bitshift)
out = in << 6; // (in * 64); Switch from base 512 to base 32768
// Leave it positive so that <0 rotates counter-clockwise
}
else
{
out = in << 6;
out = -out; // Make it negative so that <0 rotates clockwise
}
// Update rotation and scale in OAM // Update rotation and scale in OAM
if (screen == 0) if (screen == 0)
oamRotateScale(&oamMain, id, out, 512 - sx, 512 - sy); oamRotateScale(&oamMain, id, angle, 512 - sx, 512 - sy);
else else
oamRotateScale(&oamSub, id, out, 512 - sx, 512 - sy); oamRotateScale(&oamSub, id, angle, 512 - sx, 512 - sy);
} }

View File

@ -2,7 +2,7 @@
// //
// Copyright (c) 2009-2014 Cesar Rincon "NightFox" // Copyright (c) 2009-2014 Cesar Rincon "NightFox"
// //
// NightFox LIB - Funciones de WI-FI // NightFox LIB - Wi-Fi functions
// http://www.nightfoxandco.com/ // http://www.nightfoxandco.com/
#include <netinet/in.h> #include <netinet/in.h>
@ -17,181 +17,169 @@
#include "nf_wifi.h" #include "nf_wifi.h"
// Parametros de la RED // Network parameters
struct in_addr NF_IP, NF_GATEWAY, NF_MASK, NF_DNS1, NF_DNS2; // Datos de la LAN struct in_addr NF_IP, NF_GATEWAY, NF_MASK, NF_DNS1, NF_DNS2;
// Estructura del socket // Socket information
s32 NF_SOCKET; // Id del socket (servidor) s32 NF_SOCKET; // ID of the server socket
s32 NF_CONNECTED; // Resultado de la conexion s32 NF_CONNECTED; // Result of the connection
int NF_SINSIZE; // Tamaño de la Struct .SIN int NF_SINSIZE; // Size of the .SIN struct
s32 NF_BYTES_RECIEVED; // Bytes recibidos s32 NF_BYTES_RECIEVED; // Received bytes
struct sockaddr_in NF_SA_SERVER; // Estructura Socket Adress In (Servidor) struct sockaddr_in NF_SA_SERVER; // Socket address structure (server)
struct sockaddr_in NF_SA_CLIENT; // Estructura Socket Adress In (Cliente) struct sockaddr_in NF_SA_CLIENT; // Socket address structure (client)
char NF_SEND_BUFFER[NF_WIFI_BUFFER_SIZE]; // Buffer de envio char NF_SEND_BUFFER[NF_WIFI_BUFFER_SIZE]; // Send buffer
char NF_RECV_BUFFER[NF_WIFI_BUFFER_SIZE]; // Buffer de recepcion char NF_RECV_BUFFER[NF_WIFI_BUFFER_SIZE]; // Receive buffer
bool NF_WIFI_IS_SERVER; // Almacena si eres servidor o cliente bool NF_WIFI_IS_SERVER; // Keeps track of whether you're a server or client
s32 NF_MAXFD; // Numero maximo de sockets a examinar por select(); s32 NF_MAXFD; // Max number of sockets to examine with select()
fd_set NF_READFDS; // Estructura donde se almacenaran los datos de los sockets para select(); fd_set NF_READFDS; // Struct where socket data is stored for select()
struct timeval NF_TIMEOUT; // Almacena el valor del time out struct timeval NF_TIMEOUT; // Stores the value of the timeout
bool NF_WiFiConnectDefaultAp(void)
{
// Try to connect to the default access point
if (Wifi_InitDefault(WFC_CONNECT))
{
// Get connection information
NF_IP = Wifi_GetIPInfo(&NF_GATEWAY, &NF_MASK, &NF_DNS1, &NF_DNS2);
return true;
}
bool NF_WiFiConnectDefaultAp(void) { return false;
// Variables locales
bool connect = false;
// Intenta conectarte al punto de acceso por defecto
if (Wifi_InitDefault(WFC_CONNECT)) {
// Obten los datos de la conexion
NF_IP = Wifi_GetIPInfo(&NF_GATEWAY, &NF_MASK, &NF_DNS1, &NF_DNS2);
connect = true;
}
// Devuelve el resultado
return connect;
} }
void NF_WiFiDisconnectAp(void) { void NF_WiFiDisconnectAp(void)
{
// Desconectate del punto de acceso // Disconnect from the access point and disable the WiFI hardware
Wifi_DisconnectAP(); Wifi_DisconnectAP();
// Y apagala WIFI Wifi_DisableWifi();
Wifi_DisableWifi();
} }
bool NF_WIFI_CreateUdpSender(const char* address, u16 port) { bool NF_WIFI_CreateUdpSender(const char *address, u16 port)
{
// Get host IP
char ip[24];
sprintf(ip, "%s", address);
// Variables locales // Cleanup sockets before starting
bool status = true; memset(&NF_SA_SERVER, 0, sizeof(NF_SA_SERVER));
char ip[24]; memset(&NF_SA_CLIENT, 0, sizeof(NF_SA_CLIENT));
// Obtiene la direccion de host // Tell the socket where to connect
sprintf(ip, "%s", address);
// Seguidamente, comunicale al Socket donde debe conectarse // IPv4 internet connection
// *** Vacia la estructura antes que nada *** NF_SA_SERVER.sin_family = AF_INET;
memset(&NF_SA_SERVER, 0, sizeof(NF_SA_SERVER));
memset(&NF_SA_CLIENT, 0, sizeof(NF_SA_CLIENT));
// *** Tipo de conexion *** " .sin_family = AF_INET" especifica que el tipo de socket IPv4 Internet.
NF_SA_SERVER.sin_family = AF_INET;
// *** Puerto de conexion *** "htons()" convierte el valor de u16 a "TCP/IP network byte order"
NF_SA_SERVER.sin_port = htons(port);
// *** Direccion IP de la conexion ***
// "inet_addr()" convierte una direccion IPv4 en formato texto ("192.168.0.1") al formato IN_ADDR
// "inet_ntoa()" convierte al formato texto la direccion almacenada en "struct sockaddr_in"
NF_SA_SERVER.sin_addr.s_addr = inet_addr(ip);
// Crea el socket // Specify the port in network byte order
if ((NF_SOCKET = socket(AF_INET, SOCK_DGRAM, 0)) == -1) status = false; NF_SA_SERVER.sin_port = htons(port);
// Marca esta DS como cliente (envia los datos a una IP conocida [SERVIDOR]) // Specify the IP address in the right format (converted from ASCII)
NF_WIFI_IS_SERVER = false; NF_SA_SERVER.sin_addr.s_addr = inet_addr(ip);
// Devuelve el estado de la funcion // Try to create socket
return status; if ((NF_SOCKET = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return false;
// Save that this DS is acting as a client
NF_WIFI_IS_SERVER = false;
// Return success
return true;
} }
bool NF_WIFI_CreateUdpListener(u16 port) { bool NF_WIFI_CreateUdpListener(u16 port)
{
// Cleanup sockets before starting
memset(&NF_SA_SERVER, 0, sizeof(NF_SA_SERVER));
memset(&NF_SA_CLIENT, 0, sizeof(NF_SA_CLIENT));
// Variables locales // IPv4 internet connection
bool status = true; NF_SA_SERVER.sin_family = AF_INET;
// *** Vacia la estructura antes que nada *** // Specify the port in network byte order
memset(&NF_SA_SERVER, 0, sizeof(NF_SA_SERVER)); NF_SA_SERVER.sin_port = htons(port);
memset(&NF_SA_CLIENT, 0, sizeof(NF_SA_CLIENT));
// *** Tipo de conexion *** " .sin_family = AF_INET" especifica que el tipo de socket IPv4 Internet.
NF_SA_SERVER.sin_family = AF_INET;
// *** Puerto de conexion *** "htons()" convierte el valor de u16 a "TCP/IP network byte order"
NF_SA_SERVER.sin_port = htons(port);
// *** Direccion IP de la conexion ***
// "inet_addr()" convierte una direccion IPv4 en formato texto ("192.168.0.1") al formato IN_ADDR
// "inet_ntoa()" convierte al formato texto la direccion almacenada en "struct sockaddr_in"
// "INADDR_ANY" como servidor, acepta conexiones desde cualquier IP
NF_SA_SERVER.sin_addr.s_addr = htonl(INADDR_ANY);
// Crea el socket // Specify the IP address in the right format (converted from ASCII).
if ((NF_SOCKET = socket(AF_INET, SOCK_DGRAM, 0)) == -1) status = false; // INADDR_ANY means that, as a server, it accepts connections from any IP
// address.
NF_SA_SERVER.sin_addr.s_addr = htonl(INADDR_ANY);
// Enlaza el socket a un puerto concreto // Try to create socket
if ((bind(NF_SOCKET, (struct sockaddr*) &NF_SA_SERVER, sizeof(NF_SA_SERVER))) == -1) status = false; if ((NF_SOCKET = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return false;
// Tamaño del struct sockaddr // Bind socket to the specified port
NF_SINSIZE = sizeof(struct sockaddr); if ((bind(NF_SOCKET, (struct sockaddr *)&NF_SA_SERVER, sizeof(NF_SA_SERVER))) == -1)
return false;
// Marca esta DS como servidor (recibe datos desde cualquier IP [CLIENTE]) // Save size of struct sockaddr
NF_WIFI_IS_SERVER = true; NF_SINSIZE = sizeof(struct sockaddr);
return status; // Save that this DS is acting as a server
NF_WIFI_IS_SERVER = true;
return true;
} }
bool NF_WIFI_UdpSend(const char* data) { bool NF_WIFI_UdpSend(const char *data)
{
// Clear send buffer and copy the string being sent
memset(&NF_SEND_BUFFER, 0, sizeof(NF_SEND_BUFFER));
snprintf(NF_SEND_BUFFER, sizeof(NF_SEND_BUFFER), "%s", data);
// Borra el buffer de envio // Send data through the currently open UDP port
memset(&NF_SEND_BUFFER, 0, sizeof(NF_SEND_BUFFER)); if (NF_WIFI_IS_SERVER)
{
// Pon los datos en el buffer de envio // If this DS is a server, it is in listening mode. Send the string to
sprintf(NF_SEND_BUFFER, "%s", data); // the last IP where it received data from.
sendto(NF_SOCKET, NF_SEND_BUFFER, strlen(NF_SEND_BUFFER), 0,
// Envia los datos a traves del puerto UDP abierto (struct sockaddr *)&NF_SA_CLIENT, sizeof(struct sockaddr));
if (NF_WIFI_IS_SERVER) { }
// Envia los datos a la IP desde la cual has recibido datos (esta DS esta en modo LISTENER) else
sendto(NF_SOCKET, NF_SEND_BUFFER, strlen(NF_SEND_BUFFER), 0, (struct sockaddr *)&NF_SA_CLIENT, sizeof(struct sockaddr)); {
} else { // Send data to the IP of the server (this DS is in sender mode).
// Envia los datos a la IP del servidor (esta DS esta en modo SENDER) sendto(NF_SOCKET, NF_SEND_BUFFER, strlen(NF_SEND_BUFFER), 0,
sendto(NF_SOCKET, NF_SEND_BUFFER, strlen(NF_SEND_BUFFER), 0, (struct sockaddr *)&NF_SA_SERVER, sizeof(struct sockaddr)); (struct sockaddr *)&NF_SA_SERVER, sizeof(struct sockaddr));
} }
// Devuelte el estado
return true;
// Return success
return true;
} }
s32 NF_WIFI_UdpListen(u32 timeout) { s32 NF_WIFI_UdpListen(u32 timeout)
{
// Clear the previous result of select()
FD_ZERO(&NF_READFDS);
s32 status = 0; // Specify the listener socket
FD_SET(NF_SOCKET, &NF_READFDS);
// Borra el estado de select(); // Calculate the maximum value to listen to
FD_ZERO(&NF_READFDS); NF_MAXFD = NF_SOCKET + 1;
// Asigna el socket a la escucha // Specify the timeout
FD_SET(NF_SOCKET, &NF_READFDS); NF_TIMEOUT.tv_sec = 0;
NF_TIMEOUT.tv_usec = timeout;
// Calcula el valor maximo a escuchar // Select this socket
NF_MAXFD = (NF_SOCKET + 1); s32 status = select(NF_MAXFD, &NF_READFDS, NULL, NULL, &NF_TIMEOUT);
// Y ahora aplica el timeout // If there is data available to be read
NF_TIMEOUT.tv_sec = 0; if ((status > 0) && FD_ISSET(NF_SOCKET, &NF_READFDS))
NF_TIMEOUT.tv_usec = timeout; {
// Clear the previous value of the reception buffer
memset(&NF_RECV_BUFFER, 0, sizeof(NF_RECV_BUFFER));
// Ejecuta el comando select // Receive data and save it to the buffer, but leave the last byte
status = select(NF_MAXFD, &NF_READFDS, NULL, NULL, &NF_TIMEOUT); // unused to be able to store a '\0'
NF_BYTES_RECIEVED = recvfrom(NF_SOCKET, NF_RECV_BUFFER, NF_WIFI_BUFFER_SIZE - 1,
// Si hay datos disponibles para ser leidos 0, (struct sockaddr *)&NF_SA_CLIENT, &NF_SINSIZE);
if ((status > 0) && FD_ISSET(NF_SOCKET, &NF_READFDS)) { NF_RECV_BUFFER[NF_BYTES_RECIEVED] = '\0';
}
// Borra el buffer de recepcion
memset(&NF_RECV_BUFFER, 0, sizeof(NF_RECV_BUFFER));
// Recive los datos y ponlos en el buffer
NF_BYTES_RECIEVED = recvfrom(NF_SOCKET, NF_RECV_BUFFER, NF_WIFI_BUFFER_SIZE, 0, (struct sockaddr *)&NF_SA_CLIENT, &NF_SINSIZE);
// Marca el final del buffer de recepcion
NF_RECV_BUFFER[NF_BYTES_RECIEVED] = '\0';
// Fuerza el vaciado de los buffers
fflush(stdout);
}
// Devuelte el estado
return status;
// Return result
return status;
} }