1 /*$ 2 Copyright (C) 2013-2020 Azel. 3 4 This file is part of AzPainter. 5 6 AzPainter is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 AzPainter is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 $*/ 19 20 #ifndef MLIB_DEF_H 21 #define MLIB_DEF_H 22 23 #include <stdint.h> 24 25 #include "mConfig.h" 26 27 #ifndef MLIB_NO_GUI 28 #include "mDefGui.h" 29 #endif 30 31 typedef uint8_t mBool; 32 typedef struct _mStr mStr; 33 typedef struct _mList mList; 34 typedef struct _mFont mFont; 35 typedef void (*mDefEmptyFunc)(void); 36 37 /* macro */ 38 39 #undef NULL 40 #undef TRUE 41 #undef FALSE 42 43 #define NULL ((void *)0) 44 #define TRUE (1) 45 #define FALSE (0) 46 #define M_MATH_PI (3.14159265358979323846) 47 48 #define M_MAKE_DW2(hi,low) ((uint32_t)(hi) << 16) | (low)) 49 #define M_MAKE_DW4(a,b,c,d) (((uint32_t)(a) << 24) | ((b) << 16) | ((c) << 8) | (d)) 50 #define M_RGB(r,g,b) (((r) << 16) | ((g) << 8) | (b)) 51 #define M_RGBA(r,g,b,a) (((uint32_t)(a) << 24) | ((r) << 16) | ((g) << 8) | (b)) 52 #define M_GET_R(c) (((c) >> 16) & 0xff) 53 #define M_GET_G(c) (((c) >> 8) & 0xff) 54 #define M_GET_B(c) ((c) & 0xff) 55 #define M_GET_A(c) ((c) >> 24) 56 #define M_FREE_NULL(p) mFree(p); p = NULL 57 #define M_BUF_UINT16(p) (*((uint16_t *)(p))) 58 #define M_BUF_UINT32(p) (*((uint32_t *)(p))) 59 #define M_FLAG_ON(v,f) v |= f 60 #define M_FLAG_OFF(v,f) v &= ~(f) 61 62 /* struct */ 63 64 typedef struct _mPoint 65 { 66 int x,y; 67 }mPoint; 68 69 typedef struct _mDoublePoint 70 { 71 double x,y; 72 }mDoublePoint; 73 74 typedef struct _mSize 75 { 76 int w,h; 77 }mSize; 78 79 typedef struct _mRect 80 { 81 int x1,y1,x2,y2; 82 }mRect; 83 84 typedef struct _mBox 85 { 86 int x,y,w,h; 87 }mBox; 88 89 typedef struct _mBuf 90 { 91 void *buf; 92 uintptr_t size; 93 }mBuf; 94 95 /* function */ 96 97 #ifdef __cplusplus 98 extern "C" { 99 #endif 100 101 void mDebug(const char *format,...); 102 103 void mFree(void *ptr); 104 void *__mMalloc(uint32_t size,mBool clear); 105 void *__mRealloc(void *ptr,uint32_t size); 106 char *__mStrdup(const char *str); 107 char *__mStrndup(const char *str,int len); 108 int mStrdup2(const char *str,char **ptr); 109 char *mStrdup_ptr(char **dst,const char *src); 110 int mStrcpy(char *dst,const char *src,int dstsize); 111 int mStrlen(const char *str); 112 void *mMemdup(const void *src,uint32_t size); 113 void mMemzero(void *buf,int size); 114 115 #ifdef MLIB_MEMDEBUG 116 117 void *__mMalloc_debug(uint32_t size,mBool clear,const char *filename,int line); 118 void *__mRealloc_debug(void *ptr,uint32_t size,const char *filename,int line); 119 char *__mStrdup_debug(const char *str,const char *filename,int line); 120 char *__mStrndup_debug(const char *str,int len,const char *filename,int line); 121 122 #define mMalloc(size,clear) __mMalloc_debug(size, clear, __FILE__, __LINE__) 123 #define mRealloc(ptr,size) __mRealloc_debug(ptr, size, __FILE__, __LINE__) 124 #define mStrdup(str) __mStrdup_debug(str, __FILE__, __LINE__) 125 #define mStrndup(str,len) __mStrndup_debug(str, len, __FILE__, __LINE__) 126 127 #else 128 129 #define mMalloc(size,clear) __mMalloc(size, clear) 130 #define mRealloc(ptr,size) __mRealloc(ptr, size) 131 #define mStrdup(str) __mStrdup(str) 132 #define mStrndup(str,len) __mStrndup(str, len) 133 134 #endif /* MLIB_MEMDEBUG */ 135 136 #ifdef __cplusplus 137 } 138 #endif 139 140 #endif 141