re3-wiiu/src/core/common.h

398 lines
12 KiB
C
Raw Normal View History

2019-05-15 16:52:37 +02:00
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#pragma warning(disable: 4244) // int to float
#pragma warning(disable: 4800) // int to bool
#pragma warning(disable: 4838) // narrowing conversion
2019-05-24 19:58:32 +02:00
#pragma warning(disable: 4996) // POSIX names
2019-05-15 16:52:37 +02:00
#include <stdint.h>
2020-03-27 20:53:47 +01:00
#include <string.h>
2019-05-15 16:52:37 +02:00
#include <math.h>
2020-03-27 20:53:47 +01:00
#ifdef WITHWINDOWS
#include <Windows.h>
2020-03-27 20:53:47 +01:00
#endif
2019-05-30 13:35:13 +02:00
#ifdef WITHD3D
2019-06-30 12:53:39 +02:00
#include <windows.h>
2019-05-30 13:35:13 +02:00
#include <d3d8types.h>
#endif
2019-05-15 16:52:37 +02:00
#include <rwcore.h>
#include <rpworld.h>
2020-04-15 14:05:24 +02:00
// gotta put this somewhere
#ifdef LIBRW
#define STREAMPOS(str) ((str)->tell())
#define STREAMFILE(str) (((rw::StreamFile*)(str))->file)
#else
#define STREAMPOS(str) ((str)->Type.memory.position)
#define STREAMFILE(str) ((str)->Type.file.fpFile)
#endif
2019-05-15 16:52:37 +02:00
#define rwVENDORID_ROCKSTAR 0x0253F2
2019-05-30 00:47:33 +02:00
// Get rid of bullshit windows definitions, we're not running on an 8086
#ifdef far
#undef far
#endif
#ifdef near
#undef near
#endif
2020-03-27 20:53:47 +01:00
#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
2019-06-17 00:16:38 +02:00
typedef uint8_t uint8;
typedef int8_t int8;
typedef uint16_t uint16;
typedef int16_t int16;
typedef uint32_t uint32;
typedef int32_t int32;
2019-05-15 16:52:37 +02:00
typedef uintptr_t uintptr;
2019-06-17 00:16:38 +02:00
typedef uint64_t uint64;
typedef int64_t int64;
2019-06-01 23:17:39 +02:00
// hardcode ucs-2
2019-06-17 00:16:38 +02:00
typedef uint16_t wchar;
2019-05-29 02:52:30 +02:00
2020-04-15 14:05:24 +02:00
#ifndef nil
#define nil nullptr
2020-04-15 14:05:24 +02:00
#endif
2019-05-15 16:52:37 +02:00
#include "config.h"
#define ALIGNPTR(p) (void*)((((uintptr)(void*)p) + sizeof(void*)-1) & ~(sizeof(void*)-1))
2019-07-05 22:19:52 +02:00
// PDP-10 like byte functions
#define MASK(p, s) (((1<<(s))-1) << (p))
inline uint32 dpb(uint32 b, uint32 p, uint32 s, uint32 w)
{
uint32 m = MASK(p,s);
return w & ~m | b<<p & m;
}
inline uint32 ldb(uint32 p, uint32 s, uint32 w)
{
return w>>p & (1<<s)-1;
}
2020-04-11 17:37:20 +02:00
#ifndef RWLIBS
2019-05-15 16:52:37 +02:00
// little hack
extern void **rwengine;
#define RwEngineInstance (*rwengine)
2020-04-11 17:37:20 +02:00
#endif
2019-05-15 16:52:37 +02:00
2019-06-30 12:53:39 +02:00
#include "skeleton.h"
2019-06-16 00:20:55 +02:00
#include "Draw.h"
#define DEFAULT_SCREEN_WIDTH (640)
#define DEFAULT_SCREEN_HEIGHT (448)
#define DEFAULT_ASPECT_RATIO (4.0f/3.0f)
#define DEFAULT_VIEWWINDOW (0.7f)
// game uses maximumWidth/Height, but this probably won't work
// with RW windowed mode
#define SCREEN_WIDTH ((float)RsGlobal.width)
#define SCREEN_HEIGHT ((float)RsGlobal.height)
2019-06-16 00:20:55 +02:00
#define SCREEN_ASPECT_RATIO (CDraw::GetAspectRatio())
#define SCREEN_VIEWWINDOW (Tan(DEGTORAD(CDraw::GetFOV() * 0.5f)))
2019-05-15 16:52:37 +02:00
// This scales from PS2 pixel coordinates to the real resolution
#define SCREEN_STRETCH_X(a) ((a) * (float) SCREEN_WIDTH / DEFAULT_SCREEN_WIDTH)
#define SCREEN_STRETCH_Y(a) ((a) * (float) SCREEN_HEIGHT / DEFAULT_SCREEN_HEIGHT)
#define SCREEN_STRETCH_FROM_RIGHT(a) (SCREEN_WIDTH - SCREEN_STRETCH_X(a))
#define SCREEN_STRETCH_FROM_BOTTOM(a) (SCREEN_HEIGHT - SCREEN_STRETCH_Y(a))
2019-05-15 16:52:37 +02:00
// This scales from PS2 pixel coordinates while optionally maintaining the aspect ratio
#define SCREEN_SCALE_X(a) SCREEN_SCALE_AR(SCREEN_STRETCH_X(a))
#define SCREEN_SCALE_Y(a) SCREEN_STRETCH_Y(a)
#define SCREEN_SCALE_FROM_RIGHT(a) (SCREEN_WIDTH - SCREEN_SCALE_X(a))
#define SCREEN_SCALE_FROM_BOTTOM(a) (SCREEN_HEIGHT - SCREEN_SCALE_Y(a))
2019-06-16 19:06:01 +02:00
#ifdef ASPECT_RATIO_SCALE
2019-10-05 15:04:27 +02:00
#define SCREEN_SCALE_AR(a) ((a) * DEFAULT_ASPECT_RATIO / SCREEN_ASPECT_RATIO)
#else
#define SCREEN_SCALE_AR(a) (a)
#endif
2019-07-08 21:44:32 +02:00
#include "maths.h"
#include "Vector.h"
#include "Vector2D.h"
#include "Matrix.h"
#include "Rect.h"
2019-05-15 16:52:37 +02:00
class CRGBA
{
public:
2019-05-29 02:52:30 +02:00
union
{
uint32 color32;
struct { uint8 r, g, b, a; };
struct { uint8 red, green, blue, alpha; };
#ifdef RWCORE_H
struct { RwRGBA rwRGBA; };
#endif
};
2019-05-15 16:52:37 +02:00
CRGBA(void) { }
CRGBA(uint8 r, uint8 g, uint8 b, uint8 a) : r(r), g(g), b(b), a(a) { }
2019-08-15 03:43:00 +02:00
2019-08-30 00:44:57 +02:00
CRGBA &operator =(const CRGBA &right)
2019-08-15 03:43:00 +02:00
{
this->r = right.r;
this->g = right.g;
this->b = right.b;
this->a = right.a;
return *this;
}
2019-05-29 02:52:30 +02:00
#ifdef RWCORE_H
2019-05-30 00:47:33 +02:00
operator RwRGBA &(void) {
2019-05-29 02:52:30 +02:00
return rwRGBA;
}
2019-05-30 00:47:33 +02:00
operator RwRGBA *(void) {
2019-05-29 02:52:30 +02:00
return &rwRGBA;
}
2019-05-30 00:47:33 +02:00
operator RwRGBA (void) const {
2019-05-29 02:52:30 +02:00
return rwRGBA;
}
2019-08-15 03:43:00 +02:00
2019-08-30 00:44:57 +02:00
CRGBA &operator =(const RwRGBA &right)
2019-08-15 03:43:00 +02:00
{
this->r = right.red;
this->g = right.green;
this->b = right.blue;
this->a = right.alpha;
return *this;
}
2019-05-29 02:52:30 +02:00
#endif
2019-05-15 16:52:37 +02:00
};
#if (defined(_MSC_VER))
extern int strcasecmp(const char *str1, const char *str2);
#endif
2019-05-30 00:47:33 +02:00
#define clamp(v, low, high) ((v)<(low) ? (low) : (v)>(high) ? (high) : (v))
2019-05-29 02:52:30 +02:00
2019-05-30 00:47:33 +02:00
inline float sq(float x) { return x*x; }
#define SQR(x) ((x) * (x))
2019-05-29 02:52:30 +02:00
2019-08-16 20:17:15 +02:00
#define PI (float)M_PI
2019-07-18 15:41:09 +02:00
#define TWOPI (PI*2)
#define HALFPI (PI/2)
2019-05-29 02:52:30 +02:00
#define DEGTORAD(x) ((x) * PI / 180.0f)
#define RADTODEG(x) ((x) * 180.0f / PI)
2019-05-15 16:52:37 +02:00
2019-06-03 00:25:46 +02:00
#ifdef USE_PS2_RAND
#define MYRAND_MAX 65535
#else
#define MYRAND_MAX 32767
#endif
2019-05-15 16:52:37 +02:00
int myrand(void);
void mysrand(unsigned int seed);
2019-08-16 20:17:15 +02:00
void re3_debug(const char *format, ...);
void re3_trace(const char *filename, unsigned int lineno, const char *func, const char *format, ...);
2019-06-02 23:42:51 +02:00
void re3_assert(const char *expr, const char *filename, unsigned int lineno, const char *func);
2019-06-11 11:30:53 +02:00
#define DEBUGBREAK() __debugbreak();
2019-07-31 17:54:18 +02:00
#define debug(f, ...) re3_debug("[DBG]: " f, ## __VA_ARGS__)
#define DEV(f, ...) re3_debug("[DEV]: " f, ## __VA_ARGS__)
#define TRACE(f, ...) re3_trace(__FILE__, __LINE__, __FUNCTION__, f, ## __VA_ARGS__)
#define Error(f, ...) re3_debug("[ERROR]: " f, ## __VA_ARGS__)
2019-06-02 23:42:51 +02:00
#define assert(_Expression) (void)( (!!(_Expression)) || (re3_assert(#_Expression, __FILE__, __LINE__, __FUNCTION__), 0) )
2019-05-29 02:52:30 +02:00
#define ASSERT assert
2019-05-15 16:52:37 +02:00
2019-06-19 15:22:42 +02:00
#define _TODO(x)
2019-05-29 20:02:58 +02:00
#define _TODOCONST(x) (x)
2019-05-15 16:52:37 +02:00
2019-05-29 20:02:58 +02:00
#define VALIDATE_SIZE(struc, size) static_assert(sizeof(struc) == size, "Invalid structure size of " #struc)
#define VALIDATE_OFFSET(struc, member, offset) static_assert(offsetof(struc, member) == offset, "The offset of " #member " in " #struc " is not " #offset "...")
2019-06-17 00:16:38 +02:00
#define PERCENT(x, p) ((float(x) * (float(p) / 100.0f)))
2019-05-29 02:52:30 +02:00
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
2019-05-30 00:47:33 +02:00
#define BIT(num) (1<<(num))
2019-06-30 12:53:39 +02:00
2019-08-06 23:32:19 +02:00
#define ABS(a) (((a) < 0) ? (-(a)) : (a))
2019-08-02 22:20:12 +02:00
#define norm(value, min, max) (((value) < (min)) ? 0 : (((value) > (max)) ? 1 : (((value) - (min)) / ((max) - (min)))))
2020-04-01 23:04:56 +02:00
#define lerp(norm, min, max) ( (norm) * ((max) - (min)) + (min) )
2019-07-24 18:55:43 +02:00
#define STRINGIFY(x) #x
#define STR(x) STRINGIFY(x)
#define CONCAT_(x,y) x##y
#define CONCAT(x,y) CONCAT_(x,y)
// Tweaking stuff for debugmenu
#define TWEAKPATH ___tw___TWEAKPATH
#define SETTWEAKPATH(path) static const char *___tw___TWEAKPATH = path;
#define TWEAKFUNC(v) static CTweakFunc CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), TWEAKPATH);
#define TWEAKFUNCN(v, name) static CTweakFunc CONCAT(___tw___tweak, __COUNTER__)(&v, name, TWEAKPATH);
#define TWEAKBOOL(v) static CTweakBool CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), TWEAKPATH);
#define TWEAKBOOLN(v, name) static CTweakBool CONCAT(___tw___tweak, __COUNTER__)(&v, name, TWEAKPATH);
#define TWEAKINT32(v, lower, upper, step) static CTweakInt32 CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), lower, upper, step, TWEAKPATH);
#define TWEAKINT32N(v, lower, upper, step, name) static CTweakInt32 CONCAT(___tw___tweak, __COUNTER__)(&v, name, lower, upper, step, TWEAKPATH);
#define TWEAKUINT32(v, lower, upper, step) static CTweakUInt32 CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), lower, upper, step, TWEAKPATH);
#define TWEAKUINT32N(v, lower, upper, step, name) static CTweakUInt32 CONCAT(___tw___tweak, __COUNTER__)(&v, name, lower, upper, step, TWEAKPATH);
#define TWEAKINT16(v, lower, upper, step) static CTweakInt16 CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), lower, upper, step, TWEAKPATH);
#define TWEAKINT16N(v, lower, upper, step, name) static CTweakInt16 CONCAT(___tw___tweak, __COUNTER__)(&v, name, lower, upper, step, TWEAKPATH);
#define TWEAKUINT16(v, lower, upper, step) static CTweakUInt16 CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), lower, upper, step, TWEAKPATH);
#define TWEAKUINT16N(v, lower, upper, step, name) static CTweakUInt16 CONCAT(___tw___tweak, __COUNTER__)(&v, name, lower, upper, step, TWEAKPATH);
#define TWEAKINT8(v, lower, upper, step) static CTweakInt8 CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), lower, upper, step, TWEAKPATH);
#define TWEAKINT8N(v, lower, upper, step, name) static CTweakInt8 CONCAT(___tw___tweak, __COUNTER__)(&v, name, lower, upper, step, TWEAKPATH);
#define TWEAKUINT8(v, lower, upper, step) static CTweakUInt8 CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), lower, upper, step, TWEAKPATH);
#define TWEAKUINT8N(v, lower, upper, step, name) static CTweakUInt8 CONCAT(___tw___tweak, __COUNTER__)(&v, name, lower, upper, step, TWEAKPATH);
#define TWEAKFLOAT(v, lower, upper, step) static CTweakFloat CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), lower, upper, step, TWEAKPATH);
#define TWEAKFLOATN(v, lower, upper, step, name) static CTweakFloat CONCAT(___tw___tweak, __COUNTER__)(&v, name, lower, upper, step, TWEAKPATH);
#define TWEAKSWITCH(v, lower, upper, str, f) static CTweakSwitch CONCAT(___tw___tweak, __COUNTER__)(&v, STR(v), lower, upper, str, f, TWEAKPATH);
#define TWEAKSWITCHN(v, lower, upper, str, f, name) static CTweakSwitch CONCAT(___tw___tweak, __COUNTER__)(&v, name, lower, upper, str, f, TWEAKPATH);
// interface
class CTweakVar
{
public:
virtual void AddDBG(const char *path) = 0;
};
class CTweakVars
{
public:
static void Add(CTweakVar *var);
static void AddDBG(const char *path);
};
class CTweakFunc : public CTweakVar
{
const char *m_pPath, *m_pVarName;
void (*m_pFunc)();
public:
CTweakFunc(void (*pFunc)(), const char *strName, const char *strPath) :
m_pFunc(pFunc), m_pVarName(strName), m_pPath(strPath)
{
CTweakVars::Add(this);
}
void AddDBG(const char *path);
};
class CTweakBool : public CTweakVar
{
const char *m_pPath, *m_pVarName;
bool *m_pBoolVar;
public:
CTweakBool(bool *pBool, const char *strName, const char *strPath) :
m_pBoolVar(pBool), m_pVarName(strName), m_pPath(strPath)
{
CTweakVars::Add(this);
}
void AddDBG(const char *path);
};
class CTweakSwitch : public CTweakVar
{
const char *m_pPath, *m_pVarName;
void *m_pIntVar;
int32 m_nMin, m_nMax;
const char **m_aStr;
void (*m_pFunc)();
public:
CTweakSwitch(void *pInt, const char *strName, int32 nMin, int32 nMax, const char **aStr, void (*pFunc)(), const char *strPath) :
m_pVarName(strName), m_pPath(strPath),
m_aStr(aStr), m_pIntVar(pInt), m_nMin(nMin), m_nMax(nMax)
{
CTweakVars::Add(this);
}
void AddDBG(const char *path);
};
#define _TWEEKCLASS(name, type) \
class name : public CTweakVar \
{ \
public: \
const char *m_pPath, *m_pVarName; \
type *m_pIntVar, m_nLoawerBound, m_nUpperBound, m_nStep; \
\
name(type *pInt, const char *strName, type nLower, type nUpper, type nStep, const char *strPath) : \
m_pIntVar(pInt), m_nLoawerBound(nLower), m_nUpperBound(nUpper), m_nStep(nStep), \
m_pVarName(strName), m_pPath(strPath) \
{ \
CTweakVars::Add(this); \
} \
\
void AddDBG(const char *path); \
};
_TWEEKCLASS(CTweakInt8, int8);
_TWEEKCLASS(CTweakUInt8, uint8);
_TWEEKCLASS(CTweakInt16, int16);
_TWEEKCLASS(CTweakUInt16, uint16);
_TWEEKCLASS(CTweakInt32, int32);
_TWEEKCLASS(CTweakUInt32, uint32);
_TWEEKCLASS(CTweakFloat, float);
2019-07-24 19:59:16 +02:00
#undef _TWEEKCLASS
#ifdef VALIDATE_SAVE_SIZE
extern int32 _saveBufCount;
#define INITSAVEBUF _saveBufCount = 0;
#define VALIDATESAVEBUF(b) assert(_saveBufCount == b);
#else
#define INITSAVEBUF
#define VALIDATESAVEBUF(b)
#endif
inline void SkipSaveBuf(uint8 *&buf, int32 skip)
{
buf += skip;
#ifdef VALIDATE_SAVE_SIZE
_saveBufCount += skip;
#endif
}
template<typename T>
inline const T ReadSaveBuf(uint8 *&buf)
2019-10-13 00:35:01 +02:00
{
T &value = *(T*)buf;
SkipSaveBuf(buf, sizeof(T));
return value;
}
template<typename T>
inline T *WriteSaveBuf(uint8 *&buf, const T &value)
2019-10-13 00:35:01 +02:00
{
T *p = (T*)buf;
*p = value;
SkipSaveBuf(buf, sizeof(T));
return p;
}
#define SAVE_HEADER_SIZE (4*sizeof(char)+sizeof(uint32))
2019-10-13 00:35:01 +02:00
#define WriteSaveHeader(buf,a,b,c,d,size) \
WriteSaveBuf(buf, a);\
WriteSaveBuf(buf, b);\
WriteSaveBuf(buf, c);\
WriteSaveBuf(buf, d);\
WriteSaveBuf(buf, size);
#define CheckSaveHeader(buf,a,b,c,d,size)\
assert(ReadSaveBuf<char>(buf) == a);\
assert(ReadSaveBuf<char>(buf) == b);\
assert(ReadSaveBuf<char>(buf) == c);\
assert(ReadSaveBuf<char>(buf) == d);\
assert(ReadSaveBuf<uint32>(buf) == size);
2020-03-22 12:26:35 +01:00
void cprintf(char*, ...);