joey.h 9.49 KB
Newer Older
1 2
/*
 *  JoeyLib
Scott Duensing's avatar
Scott Duensing committed
3
 *  Copyright (C) 2018-2019 Scott Duensing <scott@kangaroopunch.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty.  In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *  3. This notice may not be removed or altered from any source distribution.
*/

#ifndef H_JOEY_
#define H_JOEY_

#define JOEY_DEBUG


#include <stdlib.h>
Scott Duensing's avatar
Scott Duensing committed
29
#include <limits.h>
30 31


32 33 34
// Determine platform and settings
#ifdef __linux__

Scott Duensing's avatar
Scott Duensing committed
35 36
#include <linux/limits.h>

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#define JOEY_LINUX
#define JOEY_PC
#define JOEY_LITLE_ENDIAN
#define JOEY_PATH_SEPARATOR  '/'
#define segment(x)
typedef short jint16;
typedef unsigned short juint16;

#elif _WIN32

#define JOEY_WINDOWS
#define JOEY_PC
#define JOEY_LITLE_ENDIAN
#define JOEY_PATH_SEPARATOR  '\\'
#define segment(x)
typedef short jint16;
typedef unsigned short juint16;

#elif __APPLE__

#define JOEY_MACOS
#define JOEY_PC
#define JOEY_LITLE_ENDIAN
#define JOEY_PATH_SEPARATOR  '/'
#define segment(x)
typedef short jint16;
typedef unsigned short juint16;

#elif __ORCAC__

#define JOEY_IIGS
#define JOEY_LITLE_ENDIAN
#define JOEY_PATH_SEPARATOR  '/'
#define __attribute__(x)
typedef int jint16;
typedef unsigned int juint16;
Scott Duensing's avatar
Scott Duensing committed
73
#ifndef JOEY_MAIN
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#pragma noroot
#endif
#pragma memorymodel  1
#pragma optimize    -1
#pragma lint        -1
#pragma debug        0

#elif AMIGA

#define JOEY_AMIGA
#define JOEY_BIG_ENDIAN
#define JOEY_PATH_SEPARATOR  '/'
#define __attribute__(x)
#define segment(x)
typedef int jint16;
typedef unsigned int juint16;

#else

#define JOEY_ST
#define JOEY_BIG_ENDIAN
#define JOEY_PATH_SEPARATOR  '/'
#define __attribute__(x)
#define segment(x)
typedef int jint16;
typedef unsigned int juint16;

#endif


104 105 106 107 108 109 110 111 112
#ifndef bool
#define bool unsigned char
#define true  1
#define false 0
#endif

#define byte unsigned char


113 114 115 116
extern byte  _jlDrawColor;
extern byte  _jlDrawColorNibbles;
extern byte  _jlBorderColor;
extern char  _jlTempString[1024];
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152


enum _jlBorderColorsE {
	BORDER_BLACK       =  0,
	BORDER_DEEP_RED    =  1,
	BORDER_DEEP_BLUE   =  2,
	BORDER_PURPLE      =  3,
	BORDER_DARK_GREEN  =  4,
	BORDER_DARK_GRAY   =  5,
	BORDER_MEDIUM_BLUE =  6,
	BORDER_LIGHT_BLUE  =  7,
	BORDER_BROWN       =  8,
	BORDER_ORANGE      =  9,
	BORDER_LIGHT_GRAY  = 10,
	BORDER_PINK        = 11,
	BORDER_GREEN       = 12,
	BORDER_YELLOW      = 13,
	BORDER_AQUAMARINE  = 14,
	BORDER_WHITE       = 15
};
typedef enum _jlBorderColorsE jlBorderColorsE;


typedef struct {
	 byte b : 4;
	 byte g : 4;
	 byte r : 4;
	 byte reserved : 4;
} jlColorT;

typedef struct {
	byte l : 4;
	byte r : 4;
} jlPixelPairT;

typedef struct {
153 154
		void   *data;
		jint16  channel;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
} jlSoundT;

typedef struct {
	char         id[3];
	byte         version;
	jlColorT     palette[16];    // 4 bits reserved, 4 bits red, 4 green, 4 blue
	jlPixelPairT pixels[32000];  // 320x200, 4 bits per pixel
} jlStaT;

typedef struct _jlStackS {
	struct _jlStackS *previous;
	void *data;
} jlStackT;

typedef struct {
170 171 172 173
	char     id[3];
	byte     version;
	juint16  length;
	byte    *data;
174 175 176 177 178 179
} jlVecT;


// Memory Management
#ifdef JOEY_DEBUG

180
#define JOEY_MEM_BLOCKS 128
181 182 183 184 185

typedef struct {
	void   *addr;
	char   *file;
	size_t  size;
186
	jint16  line;
187 188 189 190 191 192
} jlMemoryBlockT;

#define jlFree(p) _jlFree((void **)&(p))
void   _jlFree(void **pointer);

#define jlMalloc(s) _jlMalloc(s, __LINE__, __FILE__)
193
void  *_jlMalloc(size_t size, jint16 line, char *file);
194 195 196 197 198 199 200 201 202 203 204 205

#define jlRealloc(p, s) _jlRealloc(p, s)
void  *_jlRealloc(void *pointer, size_t size);

#else

#define jlFree free
#define jlMalloc malloc

#endif


206 207 208
#define jlByteSwap(twoBytes)   ((juint16)((twoBytes) & 0xff) >> 8) | (juint16)((twoBytes) << 8)


209 210
void          jlDisplayBorder(jlBorderColorsE color);
void          jlDisplayPresent(void);
211 212

void          jlDrawBlit8x8(jlStaT *sta, jint16 cx1, jint16 cy1, jint16 cx2, jint16 cy2);
213 214
void          jlDrawBlit8x8a(jlStaT *sta, jint16 cx1, jint16 cy1, jint16 cx2, jint16 cy2, byte offset);
void          jlDrawBlitMap(jint16 startX, jint16 startY, jint16 width, jint16 height, byte *mapData, juint16 stride, jlStaT *sta);
215 216 217
void          jlDrawBox(jint16 x1, jint16 y1, jint16 x2, jint16 y2);
void          jlDrawBoxFilled(jint16 x1, jint16 y1, jint16 x2, jint16 y2);
void          jlDrawCircle(jint16 x, jint16 y, jint16 radius);
218 219
void          jlDrawClear(void);
void          jlDrawColor(byte index);
220 221 222 223 224 225 226 227 228
void          jlDrawEllipse(jint16 x1, jint16 y1, jint16 x2, jint16 y2);
void          jlDrawFill(jint16 x, jint16 y);
void          jlDrawFillTo(jint16 x, jint16 y, byte color);
byte          jlDrawGetColor(void);
byte          jlDrawGetPixel(jint16 x, jint16 y);
void          jlDrawLine(jint16 x1, jint16 y1, jint16 x2, jint16 y2);
void          jlDrawPoint(jint16 x, jint16 y);

jint16        jlGameGetAxis(byte which);
229
bool          jlGameGetButton(byte which);
230

231 232 233
bool          jlKeyPressed(void);
char          jlKeyRead(void);
void          jlKeyWaitForAny(void);
234

235 236
void          jlPaletteDefault(void);
void          jlPaletteSet(byte index, byte r, byte g, byte b);
237
void          jlPaletteSetFromSta(jlStaT *sta);
238

239 240 241 242
void          jlSoundFree(jlSoundT *sound);
bool          jlSoundIsPlaying(jlSoundT *sound);
#define       jlSoundLoad(sound, filename) _jlSoundLoad((jlSoundT **)&(sound), filename)  // Syntatic Sugar
bool         _jlSoundLoad(jlSoundT **sound, char *filename);
243

244 245 246 247 248 249
void          jlSoundMusicContinue(void);
bool          jlSoundMusicIsPlaying(void);
void          jlSoundMusicPause(void);
void          jlSoundMusicPlay(char *name);
void          jlSoundMusicStop(void);
void          jlSoundPlay(jlSoundT *sound);
250

251 252 253 254 255 256 257 258 259
#define       jlStaCopy(source, target) _jlStaCopy(source, (jlStaT **)&(target))  // Syntatic Sugar
bool         _jlStaCopy(jlStaT *source, jlStaT **target);
#define       jlStaCreate(sta) _jlStaCreate((jlStaT **)&(sta))  // Syntatic Sugar
bool         _jlStaCreate(jlStaT **sta);
void          jlStaDisplay(jlStaT *sta);
void          jlStaFree(jlStaT *sta);
#define       jlStaLoad(sta, filename) _jlStaLoad((jlStaT **)&(sta), filename)  // Syntatic Sugar
bool         _jlStaLoad(jlStaT **sta, char *filename);
bool          jlStaSave(jlStaT *sta, char *filename);
260

261 262
void          jlUtilDie(const char *why, ...) __attribute__((noreturn));
void          jlUtilIdle(void);
263 264
#define       jlUtilIsOdd(x) (((x & 1) == 1) ? true : false)
char         *jlUtilMakePathname(char *filename, char *extension);
265
bool          jlUtilMustExit(void);
Scott Duensing's avatar
Scott Duensing committed
266
void          jlUtilNibbleSwap(byte *mem, jint16 count, byte old, byte new);
267 268 269 270 271
void          jlUtilShutdown(void) __attribute__((noreturn));
#define       jlUtilStackPop(stack) _jlUtilStackPop((jlStackT **)&(stack))  // Syntatic Sugar
void        *_jlUtilStackPop(jlStackT **stack);
#define       jlUtilStackPush(stack, data) _jlUtilStackPush((jlStackT **)&(stack), data)  // Syntatic Sugar
void         _jlUtilStackPush(jlStackT **stack, void *data);
272 273 274 275 276
void          jlUtilStartup(char *appTitle);
juint16       jlUtilTimer(void);
#define       jlUtilTimeSpan(past, current) (past < current ? current - past : 65536 - past - current)

void          jlVecDisplay(jlVecT *vec, jint16 x, jint16 y);
277 278 279 280 281 282 283
void          jlVecFree(jlVecT *vec);
#define       jlVecLoad(vec, filename) _jlVecLoad((jlVecT **)&(vec), filename)  // Syntatic Sugar
bool         _jlVecLoad(jlVecT **vec, char *filename);


#ifdef JOEY_IIGS
// Inlined functions - asm code
284
extern void     asmB88(byte *p, jint16 cx1, jint16 cy1, jint16 cx2, jint16 cy2);
285
extern void     asmB88a(byte *p, jint16 cx1, jint16 cy1, jint16 cx2, jint16 cy2, byte alpha);
286
extern void     asmDrawBM(jint16 startX, jint16 startY, jint16 width, jint16 height, byte *mapData, juint16 stride, byte *sta);
287
//extern void     asmDrawLine(byte *p, jint16 color, jint16 x1, jint16 y1, jint16 x2, jint16 y2);
288 289
extern jint16   asmGetPoint(jint16 x, jint16 y);
extern juint16  asmGetVbl(void);
Scott Duensing's avatar
Scott Duensing committed
290
extern void     asmNSwap(byte *mem, jint16 count, jint16 old, jint16 new);
291
extern void     asmPoint(jint16 color, jint16 x, jint16 y);
292 293

// Inlined functions
294 295 296 297 298
#define jlDrawBlit8x8(sta, cx1, cy1, cx2, cy2)                               asmB88((byte *)sta->pixels, cx1, cy1, cx2, cy2)
#define jlDrawBlit8x8a(sta, cx1, cy1, cx2, cy2, offset)                      asmB88a((byte *)sta->pixels, cx1, cy1, cx2, cy2, offset)
#define jlDrawBlitMap(startX, startY, width, height, mapData, stride, sta)   asmDrawBM(startX, startY, width, height, (byte *)mapData, stride, (byte *)sta->pixels)
#define jlDrawGetPixel(x, y)                                                 asmGetPoint(x, y)
#define jlDrawPoint(x, y)                                                    asmPoint(_jlDrawColorNibbles, x, y)
299
//#define jlDrawLine(x1, y1, x2, y2)                                           asmDrawLine((byte *)0x012000L, _jlDrawColor, x1, y1, x2, y2)
300 301
#define jlUtilNibbleSwap(mem, count, old, new)                               asmNSwap(mem, count, (jint16)old, (jint16)new)
#define jlUtilTimer                                                          asmGetVbl
302 303 304 305 306 307 308 309

#ifdef JOEY_DEBUG
void _jlDebugBorder(jlBorderColorsE color);
#endif
#endif // JOEY_IIGS


#endif // H_JOEY_