Licar/general.h

133 lines
2.4 KiB
C
Raw Normal View History

2025-01-14 13:59:44 +01:00
#ifndef _LCR_GENERAL_H
#define _LCR_GENERAL_H
2023-08-03 21:12:23 +02:00
2025-01-29 16:27:47 +01:00
/** @file general.h
2025-01-20 21:33:05 +01:00
Licar: general
2025-06-19 02:28:51 +02:00
This file holds general definitions used by all Licar modules.
2025-06-08 15:33:43 +02:00
All Licar code uses LCR_ (or _LCR_) prefix as a kind of namespace preventing
2025-06-19 02:28:51 +02:00
collisions with 3rd party identifiers.
2025-01-14 13:59:44 +01:00
*/
#include <stdint.h>
2023-09-17 13:21:19 +02:00
#include "settings.h"
2025-06-20 01:52:00 +02:00
#if defined(_WIN32) || defined(WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__APPLE__)
#warning "Your OS sucks, go fuck yourself."
#endif
2025-06-13 15:59:05 +02:00
#define LCR_EFFECTIVE_RESOLUTION_X \
(LCR_SETTING_RESOLUTION_X / LCR_SETTING_RESOLUTION_SUBDIVIDE)
#define LCR_EFFECTIVE_RESOLUTION_Y \
(LCR_SETTING_RESOLUTION_Y / LCR_SETTING_RESOLUTION_SUBDIVIDE)
2025-06-08 15:33:43 +02:00
#ifdef LCR_MODULE_NAME
#undef LCR_MODULE_NAME
#endif
#define LCR_MODULE_NAME "general" ///< Used by logging functions.
2025-05-28 22:34:20 +02:00
#ifndef LCR_LOG0
#define LCR_LOG0(s) ;
#endif
#ifndef LCR_LOG1
#define LCR_LOG1(s) ;
#endif
#ifndef LCR_LOG2
#define LCR_LOG2(s) ;
#endif
#ifndef LCR_LOG0_NUM
#define LCR_LOG0_NUM(x) ;
#endif
#ifndef LCR_LOG1
#define LCR_LOG1_NUM(x) ;
#endif
#ifndef LCR_LOG2
#define LCR_LOG2_NUM(x) ;
#endif
2025-04-22 23:29:16 +02:00
#if LCR_SETTING_332_COLOR
#define LCR_CONVERT_COLOR(c) \
(((c & 0xe000) >> 8) | ((c & 0x0700) >> 6) | ((c & 0x001f) >> 3))
#else
#define LCR_CONVERT_COLOR(c) c
#endif
2025-01-14 13:59:44 +01:00
char _LCR_hexDigit(int i)
{
return i < 10 ? '0' + i : ('a' - 10 + i);
}
2025-01-15 23:08:31 +01:00
int _LCR_hexDigitVal(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return -1;
}
2025-01-19 22:19:44 +01:00
/**
2025-06-19 02:28:51 +02:00
Computes a simple hash of a string represented by a function returning next
2025-01-19 22:19:44 +01:00
string character, ending at 0 or endChar. This is intended for simple (but
not 100% reliable) string comparison.
*/
uint16_t _LCR_simpleStrHash(char (*nextChar)(void), char endChar)
{
uint16_t r = 0;
while (1)
{
char c = nextChar();
if (c == 0 || c == endChar)
break;
r = ((r << 5) | (r >> 11)) + c;
}
return r;
}
2025-01-27 23:46:44 +01:00
int _LCR_strCmp(const char *s1, const char *s2)
{
while (1)
{
if (*s1 != *s2)
return 0;
if (*s1 == 0)
break;
s1++;
s2++;
}
return 1;
}
2025-06-23 17:14:29 +02:00
int _LCR_min(int a, int b)
{
return a <= b ? a : b;
}
2025-06-18 03:53:35 +02:00
int _LCR_triangleWinding(int_least32_t x0, int_least32_t y0, int_least32_t x1,
int_least32_t y1, int_least32_t x2, int_least32_t y2)
2025-02-04 00:15:45 +01:00
{
x0 = (y1 - y0) * (x2 - x1) - (x1 - x0) * (y2 - y1);
return x0 != 0 ? (x0 > 0 ? 1 : -1) : 0;
}
2025-06-08 15:33:43 +02:00
#undef LCR_MODULE_NAME
2025-01-14 13:59:44 +01:00
#endif // guard