reformatted original source to my editor settings

This commit is contained in:
Ilu
2023-04-28 15:08:06 +02:00
parent 60a2f78b46
commit e781aa5a1a
36 changed files with 744 additions and 1021 deletions

View File

@@ -1,7 +1,6 @@
#include "lib/cmixer/cmixer.h"
#include "soundblaster.h"
static short audio_buffer[SOUNDBLASTER_SAMPLES_PER_BUFFER * 2];
static const short *audio_callback(void) {
@@ -17,13 +16,9 @@ static const short* audio_callback(void) {
return audio_buffer;
}
void audio_init(void) {
cm_init(soundblaster_getSampleRate());
soundblaster_init(audio_callback);
}
void audio_deinit(void) {
soundblaster_deinit();
}
void audio_deinit(void) { soundblaster_deinit(); }

View File

@@ -19,32 +19,35 @@ static struct {
unsigned writei, readi;
} events;
const char *event_typestr(int type) {
switch (type) {
case EVENT_QUIT : return "quit";
case EVENT_KEYBOARD_PRESSED : return "keypressed";
case EVENT_KEYBOARD_RELEASED : return "keyreleased";
case EVENT_KEYBOARD_TEXTINPUT : return "textinput";
case EVENT_MOUSE_MOVED : return "mousemoved";
case EVENT_MOUSE_PRESSED : return "mousepressed";
case EVENT_MOUSE_RELEASED : return "mousereleased";
case EVENT_QUIT:
return "quit";
case EVENT_KEYBOARD_PRESSED:
return "keypressed";
case EVENT_KEYBOARD_RELEASED:
return "keyreleased";
case EVENT_KEYBOARD_TEXTINPUT:
return "textinput";
case EVENT_MOUSE_MOVED:
return "mousemoved";
case EVENT_MOUSE_PRESSED:
return "mousepressed";
case EVENT_MOUSE_RELEASED:
return "mousereleased";
}
return "?";
}
void event_push(event_t *e) {
events.buffer[events.writei++ & BUFFER_MASK] = *e;
}
void event_pump(void) {
keyboard_update();
mouse_update();
}
int event_poll(event_t *e) {
if (events.readi != events.writei) {
*e = events.buffer[events.readi++ & BUFFER_MASK];

View File

@@ -43,7 +43,6 @@ typedef union {
} event_t;
const char *event_typestr(int type);
void event_push(event_t *e);
void event_pump(void);

View File

@@ -44,9 +44,7 @@ char filesystem_writeDir[MAX_PATH];
#define FOREACH_MOUNT(var) \
for (mount_t *var = &filesystem_mounts[filesystem_mountIdx - 1]; \
var >= filesystem_mounts;\
var--)
var >= filesystem_mounts; var--)
static int get_file_type(const char *filename) {
/* The use of `stat` is intentionally avoided here, a stat call seems to
@@ -64,7 +62,6 @@ static int get_file_type(const char *filename) {
return FILESYSTEM_TNONE;
}
static int concat_path(char *dst, const char *dir, const char *filename) {
int dirlen = strlen(dir);
int filenamelen = strlen(filename);
@@ -83,7 +80,6 @@ static int concat_path(char *dst, const char *dir, const char *filename) {
return FILESYSTEM_ESUCCESS;
}
static int concat_and_get_file_type(const char *dir, const char *filename) {
char buf[MAX_PATH];
/* Make fullpath */
@@ -95,7 +91,6 @@ static int concat_and_get_file_type(const char *dir, const char *filename) {
return get_file_type(buf);
}
static unsigned hash_string_ignore_case(const char *str) {
unsigned hash = 5381;
while (*str) {
@@ -104,7 +99,6 @@ static unsigned hash_string_ignore_case(const char *str) {
return hash;
}
static int strings_equal_ignore_case(const char *a, const char *b) {
while (*a) {
if (tolower(*a++) != tolower(*b++)) {
@@ -114,7 +108,6 @@ static int strings_equal_ignore_case(const char *a, const char *b) {
return !*b;
}
static void strip_trailing_slash(char *str) {
int len = strlen(str);
if (len > 0 && str[len - 1] == '/') {
@@ -122,11 +115,7 @@ static void strip_trailing_slash(char *str) {
}
}
static int is_separator(int chr) {
return (chr == '/' || chr == '\\');
}
static int is_separator(int chr) { return (chr == '/' || chr == '\\'); }
static int make_dirs(const char *path) {
char str[MAX_PATH];
@@ -135,8 +124,10 @@ static int make_dirs(const char *path) {
if (err) {
return err;
}
if (p[0] == '/') p++;
if (p[0] && p[1] == ':' && p[2] == '\\') p += 3;
if (p[0] == '/')
p++;
if (p[0] && p[1] == ':' && p[2] == '\\')
p += 3;
while (*p) {
if (is_separator(*p)) {
*p = '\0';
@@ -152,31 +143,24 @@ static int make_dirs(const char *path) {
return FILESYSTEM_ESUCCESS;
}
/*==================*/
/* Directory mount */
/*==================*/
static void dir_unmount(mount_t *mnt) {
/* Intentionally empty */
}
static void dir_unmount(mount_t *mnt) { /* Intentionally empty */ }
static int dir_exists(mount_t *mnt, const char *filename) {
return concat_and_get_file_type(mnt->path, filename) != FILESYSTEM_TNONE;
}
static int dir_isFile(mount_t *mnt, const char *filename) {
return concat_and_get_file_type(mnt->path, filename) == FILESYSTEM_TREG;
}
static int dir_isDirectory(mount_t *mnt, const char *filename) {
return concat_and_get_file_type(mnt->path, filename) == FILESYSTEM_TDIR;
}
static void *dir_read(mount_t *mnt, const char *filename, int *size) {
char buf[MAX_PATH];
/* Make fullpath */
@@ -204,7 +188,6 @@ static void* dir_read(mount_t *mnt, const char *filename, int *size) {
return p;
}
static int dir_mount(mount_t *mnt, const char *path) {
/* Check the path is actually a directory */
if (get_file_type(path) != FILESYSTEM_TDIR) {
@@ -223,12 +206,13 @@ static int dir_mount(mount_t *mnt, const char *path) {
return FILESYSTEM_ESUCCESS;
}
/*==================*/
/* Tar mount */
/*==================*/
typedef struct { unsigned hash, pos; } tar_file_ref_t;
typedef struct {
unsigned hash, pos;
} tar_file_ref_t;
typedef struct {
mtar_t tar;
@@ -238,7 +222,6 @@ typedef struct {
int nfiles;
} tar_mount_t;
static int tar_find(mount_t *mnt, const char *filename, mtar_header_t *h) {
/* Hash filename and linear search map for matching hash, read header and
* check against filename if the hashes match */
@@ -257,12 +240,10 @@ static int tar_find(mount_t *mnt, const char *filename, mtar_header_t *h) {
return FILESYSTEM_ESUCCESS;
}
}
}
return FILESYSTEM_EFAILURE;
}
static void tar_unmount(mount_t *mnt) {
tar_mount_t *tm = mnt->udata;
mtar_close(&tm->tar);
@@ -270,13 +251,11 @@ static void tar_unmount(mount_t *mnt) {
dmt_free(tm);
}
static int tar_exists(mount_t *mnt, const char *filename) {
mtar_header_t h;
return tar_find(mnt, filename, &h) == FILESYSTEM_ESUCCESS;
}
static int tar_isFile(mount_t *mnt, const char *filename) {
mtar_header_t h;
int err = tar_find(mnt, filename, &h);
@@ -286,7 +265,6 @@ static int tar_isFile(mount_t *mnt, const char *filename) {
return h.type == MTAR_TREG;
}
static int tar_isDirectory(mount_t *mnt, const char *filename) {
mtar_header_t h;
int err = tar_find(mnt, filename, &h);
@@ -296,7 +274,6 @@ static int tar_isDirectory(mount_t *mnt, const char *filename) {
return h.type == MTAR_TDIR;
}
static void *tar_read(mount_t *mnt, const char *filename, int *size) {
mtar_t *tar = mnt->udata;
int err;
@@ -319,28 +296,24 @@ static void* tar_read(mount_t *mnt, const char *filename, int *size) {
return p;
}
static int tar_stream_read(mtar_t *tar, void *data, unsigned size) {
tar_mount_t *tm = tar->stream;
unsigned res = fread(data, 1, size, tm->fp);
return (res == size) ? MTAR_ESUCCESS : MTAR_EREADFAIL;
}
static int tar_stream_seek(mtar_t *tar, unsigned offset) {
tar_mount_t *tm = tar->stream;
int res = fseek(tm->fp, tm->offset + offset, SEEK_SET);
return (res == 0) ? MTAR_ESUCCESS : MTAR_ESEEKFAIL;
}
static int tar_stream_close(mtar_t *tar) {
tar_mount_t *tm = tar->stream;
fclose(tm->fp);
return MTAR_ESUCCESS;
}
static int tar_mount(mount_t *mnt, const char *path) {
tar_mount_t *tm = NULL;
FILE *fp = NULL;
@@ -420,7 +393,8 @@ static int tar_mount(mount_t *mnt, const char *path) {
return FILESYSTEM_ESUCCESS;
fail:
if (fp) fclose(fp);
if (fp)
fclose(fp);
if (tm) {
dmt_free(tm->map);
dmt_free(tm);
@@ -428,35 +402,39 @@ fail:
return FILESYSTEM_EFAILURE;
}
/*==================*/
/* Filesystem */
/*==================*/
const char *filesystem_strerror(int err) {
switch (err) {
case FILESYSTEM_ESUCCESS : return "success";
case FILESYSTEM_EFAILURE : return "failure";
case FILESYSTEM_ETOOLONG : return "path too long";
case FILESYSTEM_EMOUNTED : return "path already mounted";
case FILESYSTEM_ENOMOUNT : return "path is not mounted";
case FILESYSTEM_EMOUNTFAIL : return "could not mount path";
case FILESYSTEM_ENOWRITEDIR : return "no write directory set";
case FILESYSTEM_EWRITEFAIL : return "could not write file";
case FILESYSTEM_EMKDIRFAIL : return "could not make directory";
case FILESYSTEM_ESUCCESS:
return "success";
case FILESYSTEM_EFAILURE:
return "failure";
case FILESYSTEM_ETOOLONG:
return "path too long";
case FILESYSTEM_EMOUNTED:
return "path already mounted";
case FILESYSTEM_ENOMOUNT:
return "path is not mounted";
case FILESYSTEM_EMOUNTFAIL:
return "could not mount path";
case FILESYSTEM_ENOWRITEDIR:
return "no write directory set";
case FILESYSTEM_EWRITEFAIL:
return "could not write file";
case FILESYSTEM_EMKDIRFAIL:
return "could not make directory";
}
return "unknown error";
}
void filesystem_deinit(void) {
FOREACH_MOUNT(mnt) {
mnt->unmount(mnt);
}
FOREACH_MOUNT(mnt) { mnt->unmount(mnt); }
filesystem_mountIdx = 0;
}
int filesystem_mount(const char *path) {
/* Check path length is ok */
if (strlen(path) >= MAX_PATH) {
@@ -479,8 +457,10 @@ int filesystem_mount(const char *path) {
strcpy(mnt->path, path);
/* Try to mount path */
if ( tar_mount(mnt, path) == FILESYSTEM_ESUCCESS ) goto success;
if ( dir_mount(mnt, path) == FILESYSTEM_ESUCCESS ) goto success;
if (tar_mount(mnt, path) == FILESYSTEM_ESUCCESS)
goto success;
if (dir_mount(mnt, path) == FILESYSTEM_ESUCCESS)
goto success;
/* Fail */
filesystem_mountIdx--;
@@ -490,7 +470,6 @@ success:
return FILESYSTEM_ESUCCESS;
}
int filesystem_unmount(const char *path) {
FOREACH_MOUNT(mnt) {
if (!strcmp(mnt->path, path)) {
@@ -506,7 +485,6 @@ int filesystem_unmount(const char *path) {
return FILESYSTEM_ENOMOUNT;
}
int filesystem_exists(const char *filename) {
FOREACH_MOUNT(mnt) {
if (mnt->exists(mnt, filename)) {
@@ -516,7 +494,6 @@ int filesystem_exists(const char *filename) {
return 0;
}
int filesystem_isFile(const char *filename) {
FOREACH_MOUNT(mnt) {
if (mnt->exists(mnt, filename)) {
@@ -526,7 +503,6 @@ int filesystem_isFile(const char *filename) {
return 0;
}
int filesystem_isDirectory(const char *filename) {
FOREACH_MOUNT(mnt) {
if (mnt->exists(mnt, filename)) {
@@ -536,7 +512,6 @@ int filesystem_isDirectory(const char *filename) {
return 0;
}
void *filesystem_read(const char *filename, int *size) {
FOREACH_MOUNT(mnt) {
if (mnt->exists(mnt, filename) && mnt->isFile(mnt, filename)) {
@@ -546,11 +521,7 @@ void* filesystem_read(const char *filename, int *size) {
return NULL;
}
void filesystem_free(void *ptr) {
dmt_free(ptr);
}
void filesystem_free(void *ptr) { dmt_free(ptr); }
int filesystem_setWriteDir(const char *path) {
if (strlen(path) >= MAX_PATH) {
@@ -564,7 +535,6 @@ int filesystem_setWriteDir(const char *path) {
return FILESYSTEM_ESUCCESS;
}
int filesystem_write(const char *filename, const void *data, int size) {
int err, n;
char buf[MAX_PATH];

View File

@@ -14,7 +14,6 @@
#include "filesystem.h"
#include "font.h"
static const char *initFont(font_t *self, const void *data, int ptsize) {
int i;
@@ -38,8 +37,8 @@ retry:
/* Load glyphs */
float s = stbtt_ScaleForMappingEmToPixels(&font, 1) /
stbtt_ScaleForPixelHeight(&font, 1);
int res = stbtt_BakeFontBitmap(
data, 0, ptsize * s, self->image.data, w, h, 0, 128, self->glyphs);
int res = stbtt_BakeFontBitmap(data, 0, ptsize * s, self->image.data, w, h, 0,
128, self->glyphs);
/* Retry with a larger image buffer if the buffer wasn't large enough */
if (res < 0) {
@@ -65,7 +64,6 @@ retry:
return NULL;
}
const char *font_init(font_t *self, const char *filename, int ptsize) {
const char *errmsg = NULL;
void *data = NULL;
@@ -93,29 +91,24 @@ const char *font_init(font_t *self, const char *filename, int ptsize) {
return NULL;
fail:
if (fp) fclose(fp);
if (fp)
fclose(fp);
filesystem_free(data);
return errmsg;
}
const char *font_initEmbedded(font_t *self, int ptsize) {
#include "font_ttf.h"
return initFont(self, font_ttf, ptsize);
}
void font_deinit(font_t *self) {
image_deinit(&self->image);
}
void font_deinit(font_t *self) { image_deinit(&self->image); }
extern int image_blendMode;
extern int image_flip;
void font_blit(font_t *self, pixel_t *buf, int bufw, int bufh,
const char *str, int dx, int dy
) {
void font_blit(font_t *self, pixel_t *buf, int bufw, int bufh, const char *str,
int dx, int dy) {
const char *p = str;
int x = dx;
int y = dy;
@@ -133,8 +126,8 @@ void font_blit(font_t *self, pixel_t *buf, int bufw, int bufh,
stbtt_bakedchar *g = &self->glyphs[(int)(*p & 127)];
int w = g->x1 - g->x0;
int h = g->y1 - g->y0;
image_blit(&self->image, buf, bufw, bufh,
x + g->xoff, y + g->yoff, g->x0, g->y0, w, h);
image_blit(&self->image, buf, bufw, bufh, x + g->xoff, y + g->yoff, g->x0,
g->y0, w, h);
x += g->xadvance;
}
p++;

View File

@@ -20,8 +20,7 @@ typedef struct {
const char *font_init(font_t *self, const char *filename, int ptsize);
const char *font_initEmbedded(font_t *self, int ptsize);
void font_deinit(font_t *self);
void font_blit(font_t *self, pixel_t *buf, int bufw, int bufh,
const char *str, int dx, int dy);
void font_blit(font_t *self, pixel_t *buf, int bufw, int bufh, const char *str,
int dx, int dy);
#endif

View File

@@ -19,20 +19,13 @@ int image_blendMode = IMAGE_NORMAL;
int image_flip = 0;
unsigned int image_color = 0x0f0f0f0f;
void image_setBlendMode(int mode) {
image_blendMode = mode;
}
void image_setBlendMode(int mode) { image_blendMode = mode; }
void image_setColor(pixel_t color) {
image_color = color | (color << 8) | (color << 16) | (color << 24);
}
void image_setFlip(int mode) {
image_flip = !!mode;
}
void image_setFlip(int mode) { image_flip = !!mode; }
const char *image_init(image_t *self, const char *filename) {
/* Loads an image file into the struct and inits the mask */
@@ -101,7 +94,6 @@ fail:
return errmsg;
}
void image_initBlank(image_t *self, int width, int height) {
/* Creates a blank zeroset image with a zeroset mask. This function can be
* used to init the image instead of image_init() */
@@ -113,31 +105,58 @@ void image_initBlank(image_t *self, int width, int height) {
self->mask = dmt_calloc(1, width * height);
}
void image_blit(image_t *self, pixel_t *buf, int bufw, int bufh,
int dx, int dy, int sx, int sy, int sw, int sh
) {
void image_blit(image_t *self, pixel_t *buf, int bufw, int bufh, int dx, int dy,
int sx, int sy, int sw, int sh) {
int diff;
/* Clip to source buffer */
if (sx < 0) { sw -= sx; sx = 0; }
if (sy < 0) { sy -= sy; sy = 0; }
if ((diff = (sx + sw) - self->width) > 0) { sw -= diff; }
if ((diff = (sy + sh) - self->height) > 0) { sh -= diff; }
if (sx < 0) {
sw -= sx;
sx = 0;
}
if (sy < 0) {
sy -= sy;
sy = 0;
}
if ((diff = (sx + sw) - self->width) > 0) {
sw -= diff;
}
if ((diff = (sy + sh) - self->height) > 0) {
sh -= diff;
}
/* Clip to destination buffer */
if (!image_flip) {
if ((diff = -dx) > 0) { sw -= diff; sx += diff; dx += diff; }
if ((diff = dx + sw - bufw) >= 0) { sw -= diff; }
} else {
if ((diff = -dx) > 0) { sw -= diff; dx += diff; }
if ((diff = dx + sw - bufw) >= 0) { sx += diff; sw -= diff; }
if ((diff = -dx) > 0) {
sw -= diff;
sx += diff;
dx += diff;
}
if ((diff = dx + sw - bufw) >= 0) {
sw -= diff;
}
} else {
if ((diff = -dx) > 0) {
sw -= diff;
dx += diff;
}
if ((diff = dx + sw - bufw) >= 0) {
sx += diff;
sw -= diff;
}
}
if ((diff = dy + sh - bufh) >= 0) {
sh -= diff;
}
if ((diff = -dy) > 0) {
sh -= diff;
sy += diff;
dy += diff;
}
if ((diff = dy + sh - bufh) >= 0) { sh -= diff; }
if ((diff = -dy) > 0) { sh -= diff; sy += diff; dy += diff; }
/* Return early if we're clipped entirely off the dest / source */
if (sw <= 0 || sh <= 0) return;
if (sw <= 0 || sh <= 0)
return;
/* Blit */
#define BLIT_LOOP_NORMAL(func) \
@@ -150,15 +169,12 @@ void image_blit(image_t *self, pixel_t *buf, int bufw, int bufh,
int sw32 = sw - (sw & 3); \
for (y = 0; y < sh; y++) { \
for (x = 0; x < sw32; x += 4) { \
func(*(unsigned int*)&buf[dsti],\
*(unsigned int*)&self->data[srci],\
*(unsigned int*)&self->mask[srci])\
srci += 4;\
func(*(unsigned int *)&buf[dsti], *(unsigned int *)&self->data[srci], \
*(unsigned int *)&self->mask[srci]) srci += 4; \
dsti += 4; \
} \
for (; x < sw; x++) { \
func(buf[dsti], self->data[srci], self->mask[srci])\
srci++;\
func(buf[dsti], self->data[srci], self->mask[srci]) srci++; \
dsti++; \
} \
srci += srcrowdiff; \
@@ -175,8 +191,7 @@ void image_blit(image_t *self, pixel_t *buf, int bufw, int bufh,
int dstrowdiff = bufw - sw; \
for (y = 0; y < sh; y++) { \
for (x = 0; x < sw; x++) { \
func(buf[dsti], self->data[srci], self->mask[srci])\
srci--;\
func(buf[dsti], self->data[srci], self->mask[srci]) srci--; \
dsti++; \
} \
srci += srcrowdiff; \
@@ -188,11 +203,9 @@ void image_blit(image_t *self, pixel_t *buf, int bufw, int bufh,
(dst) &= (msk); \
(dst) |= (src);
#define BLIT_AND(dst, src, msk)\
(dst) &= (src);
#define BLIT_AND(dst, src, msk) (dst) &= (src);
#define BLIT_OR(dst, src, msk)\
(dst) |= (src);
#define BLIT_OR(dst, src, msk) (dst) |= (src);
#define BLIT_COLOR(dst, src, msk) \
(dst) &= (msk); \
@@ -201,11 +214,15 @@ void image_blit(image_t *self, pixel_t *buf, int bufw, int bufh,
#define BLIT(blit_loop) \
switch (image_blendMode) { \
default: \
case IMAGE_NORMAL : blit_loop(BLIT_NORMAL) break;\
case IMAGE_AND : blit_loop(BLIT_AND) break;\
case IMAGE_OR : blit_loop(BLIT_OR) break;\
case IMAGE_COLOR : blit_loop(BLIT_COLOR) break;\
}\
case IMAGE_NORMAL: \
blit_loop(BLIT_NORMAL) break; \
case IMAGE_AND: \
blit_loop(BLIT_AND) break; \
case IMAGE_OR: \
blit_loop(BLIT_OR) break; \
case IMAGE_COLOR: \
blit_loop(BLIT_COLOR) break; \
}
if (!image_flip) {
if (image_blendMode == IMAGE_FAST) {
@@ -223,10 +240,8 @@ void image_blit(image_t *self, pixel_t *buf, int bufw, int bufh,
} else {
BLIT(BLIT_LOOP_FLIPPED);
}
}
void image_deinit(image_t *self) {
dmt_free(self->data);
dmt_free(self->mask);

View File

@@ -11,8 +11,7 @@
#include "vga.h"
#include "luaobj.h"
enum {
typedef enum {
IMAGE_NORMAL,
IMAGE_FAST,
IMAGE_AND,
@@ -20,23 +19,20 @@ enum {
IMAGE_COLOR,
} IMAGE_BLEND_MODE;
typedef struct {
pixel_t *data;
pixel_t *mask;
int width, height;
} image_t;
static inline
void image_setPixel(image_t* self, int x, int y, pixel_t val) {
static inline void image_setPixel(image_t *self, int x, int y, pixel_t val) {
if (x >= 0 && x < self->width && y >= 0 && y < self->height) {
self->data[x + y * self->width] = val;
}
}
static inline
void image_setMaskPixel(image_t* self, int x, int y, pixel_t val) {
static inline void image_setMaskPixel(image_t *self, int x, int y,
pixel_t val) {
if (x >= 0 && x < self->width && y >= 0 && y < self->height) {
self->mask[x + y * self->width] = val;
}
@@ -48,8 +44,8 @@ void image_setFlip(int mode);
const char *image_init(image_t *self, const char *filename);
void image_initBlank(image_t *, int, int);
void image_blit(image_t *self, pixel_t *buf, int bufw, int bufh,
int dx, int dy, int sx, int sy, int sw, int sh);
void image_blit(image_t *self, pixel_t *buf, int bufw, int bufh, int dx, int dy,
int sx, int sy, int sw, int sh);
void image_deinit(image_t *);
#endif

View File

@@ -19,143 +19,48 @@
#define BUFFER_MASK (BUFFER_SIZE - 1)
static const char *scancodeMap[] = {
[ 0] = "?",
[ 1] = "escape",
[ 2] = "1",
[ 3] = "2",
[ 4] = "3",
[ 5] = "4",
[ 6] = "5",
[ 7] = "6",
[ 8] = "7",
[ 9] = "8",
[ 10] = "9",
[ 11] = "0",
[ 12] = "-",
[ 13] = "=",
[ 14] = "backspace",
[ 15] = "tab",
[ 16] = "q",
[ 17] = "w",
[ 18] = "e",
[ 19] = "r",
[ 20] = "t",
[ 21] = "y",
[ 22] = "u",
[ 23] = "i",
[ 24] = "o",
[ 25] = "p",
[ 26] = "[",
[ 27] = "]",
[ 28] = "return",
[ 29] = "lctrl",
[ 30] = "a",
[ 31] = "s",
[ 32] = "d",
[ 33] = "f",
[ 34] = "g",
[ 35] = "h",
[ 36] = "j",
[ 37] = "k",
[ 38] = "l",
[ 39] = ";",
[ 40] = "\"",
[ 41] = "`",
[ 42] = "lshift",
[ 43] = "\\",
[ 44] = "z",
[ 45] = "x",
[ 46] = "c",
[ 47] = "v",
[ 48] = "b",
[ 49] = "n",
[ 50] = "m",
[ 51] = ",",
[ 52] = ".",
[ 53] = "/",
[ 54] = "rshift",
[ 55] = "*",
[ 56] = "lalt",
[ 57] = "space",
[ 58] = "capslock",
[ 59] = "f1",
[ 60] = "f2",
[ 61] = "f3",
[ 62] = "f4",
[ 63] = "f5",
[ 64] = "f6",
[ 65] = "f7",
[ 66] = "f8",
[ 67] = "f9",
[ 68] = "f10",
[ 69] = "numlock",
[ 70] = "scrolllock",
[ 71] = "home",
[ 72] = "up",
[ 73] = "pageup",
[ 74] = "-",
[ 75] = "left",
[ 76] = "5",
[ 77] = "right",
[ 78] = "+",
[ 79] = "end",
[ 80] = "down",
[ 81] = "pagedown",
[ 82] = "insert",
[ 83] = "delete",
[ 84] = "?",
[ 85] = "?",
[ 86] = "?",
[ 87] = "?",
[ 88] = "f12",
[ 89] = "?",
[ 90] = "?",
[ 91] = "?",
[ 92] = "?",
[ 93] = "?",
[ 94] = "?",
[ 95] = "?",
[ 96] = "enter",
[ 97] = "lctrl",
[ 98] = "/",
[ 99] = "f12",
[100] = "rctrl",
[101] = "pause",
[102] = "home",
[103] = "up",
[104] = "pageup",
[105] = "left",
[106] = "right",
[107] = "end",
[108] = "down",
[109] = "pagedown",
[110] = "insert",
[111] = "?",
[112] = "?",
[113] = "?",
[114] = "?",
[115] = "?",
[116] = "?",
[117] = "?",
[118] = "?",
[119] = "pause",
[120] = "?",
[121] = "?",
[122] = "?",
[123] = "?",
[124] = "?",
[125] = "?",
[126] = "?",
[127] = "?",
[0] = "?", [1] = "escape", [2] = "1", [3] = "2",
[4] = "3", [5] = "4", [6] = "5", [7] = "6",
[8] = "7", [9] = "8", [10] = "9", [11] = "0",
[12] = "-", [13] = "=", [14] = "backspace", [15] = "tab",
[16] = "q", [17] = "w", [18] = "e", [19] = "r",
[20] = "t", [21] = "y", [22] = "u", [23] = "i",
[24] = "o", [25] = "p", [26] = "[", [27] = "]",
[28] = "return", [29] = "lctrl", [30] = "a", [31] = "s",
[32] = "d", [33] = "f", [34] = "g", [35] = "h",
[36] = "j", [37] = "k", [38] = "l", [39] = ";",
[40] = "\"", [41] = "`", [42] = "lshift", [43] = "\\",
[44] = "z", [45] = "x", [46] = "c", [47] = "v",
[48] = "b", [49] = "n", [50] = "m", [51] = ",",
[52] = ".", [53] = "/", [54] = "rshift", [55] = "*",
[56] = "lalt", [57] = "space", [58] = "capslock", [59] = "f1",
[60] = "f2", [61] = "f3", [62] = "f4", [63] = "f5",
[64] = "f6", [65] = "f7", [66] = "f8", [67] = "f9",
[68] = "f10", [69] = "numlock", [70] = "scrolllock", [71] = "home",
[72] = "up", [73] = "pageup", [74] = "-", [75] = "left",
[76] = "5", [77] = "right", [78] = "+", [79] = "end",
[80] = "down", [81] = "pagedown", [82] = "insert", [83] = "delete",
[84] = "?", [85] = "?", [86] = "?", [87] = "?",
[88] = "f12", [89] = "?", [90] = "?", [91] = "?",
[92] = "?", [93] = "?", [94] = "?", [95] = "?",
[96] = "enter", [97] = "lctrl", [98] = "/", [99] = "f12",
[100] = "rctrl", [101] = "pause", [102] = "home", [103] = "up",
[104] = "pageup", [105] = "left", [106] = "right", [107] = "end",
[108] = "down", [109] = "pagedown", [110] = "insert", [111] = "?",
[112] = "?", [113] = "?", [114] = "?", [115] = "?",
[116] = "?", [117] = "?", [118] = "?", [119] = "pause",
[120] = "?", [121] = "?", [122] = "?", [123] = "?",
[124] = "?", [125] = "?", [126] = "?", [127] = "?",
[128] = NULL,
};
volatile int keyboard_allowKeyRepeat = 0;
volatile char keyboard_keyStates[KEY_MAX];
enum { KEYPRESSED, KEYRELEASED };
typedef struct { unsigned char type, code, isrepeat; } key_event_t;
typedef struct {
unsigned char type, code, isrepeat;
} key_event_t;
volatile struct {
key_event_t data[32];
@@ -164,7 +69,6 @@ volatile struct {
_go32_dpmi_seginfo old_keyb_handler_seginfo, new_keyb_handler_seginfo;
void keyboard_handler() {
static unsigned char code;
code = inportb(0x60);
@@ -199,10 +103,8 @@ void keyboard_handler() {
outportb(0x20, 0x20);
}
void keyboard_handler_end() {}
int keyboard_init(void) {
_go32_dpmi_lock_data((char *)&keyboard_keyStates, KEY_MAX);
_go32_dpmi_lock_data((char *)&keyboard_events, sizeof(keyboard_events));
@@ -210,20 +112,16 @@ int keyboard_init(void) {
(unsigned long)keyboard_handler);
_go32_dpmi_get_protected_mode_interrupt_vector(9, &old_keyb_handler_seginfo);
new_keyb_handler_seginfo.pm_offset = (int)keyboard_handler;
_go32_dpmi_chain_protected_mode_interrupt_vector(9, &new_keyb_handler_seginfo);
_go32_dpmi_chain_protected_mode_interrupt_vector(9,
&new_keyb_handler_seginfo);
return 0;
}
void keyboard_deinit(void) {
_go32_dpmi_set_protected_mode_interrupt_vector(9, &old_keyb_handler_seginfo);
}
void keyboard_setKeyRepeat(int allow) {
keyboard_allowKeyRepeat = allow;
}
void keyboard_setKeyRepeat(int allow) { keyboard_allowKeyRepeat = allow; }
int keyboard_isDown(const char *key) {
int i;
@@ -235,11 +133,11 @@ int keyboard_isDown(const char *key) {
return 0;
}
void keyboard_update(void) {
/* Handle key press / release */
while (keyboard_events.readi != keyboard_events.writei) {
key_event_t ke = keyboard_events.data[keyboard_events.readi++ & BUFFER_MASK];
key_event_t ke =
keyboard_events.data[keyboard_events.readi++ & BUFFER_MASK];
event_t e;
if (ke.type == KEYPRESSED) {
e.type = EVENT_KEYBOARD_PRESSED;

View File

@@ -11,10 +11,8 @@
#include "luaobj.h"
int luaobj_newclass(lua_State *L, const char *name, const char *extends,
int (*constructor)(lua_State*), luaL_Reg* reg
) {
int (*constructor)(lua_State *), luaL_Reg *reg) {
/* Creates and pushes a new metatable which represents a class containing the
* functions in the `reg` argument. If the `extends` argument is not NULL
* this class will use the specified class as a super class */
@@ -53,7 +51,6 @@ int luaobj_newclass(lua_State *L, const char *name, const char *extends,
return 1;
}
void luaobj_setclass(lua_State *L, uint32_t type, char *name) {
/* Sets the the metatable of the member at the top of the stack to the
* class-metatable of the given `name` */
@@ -61,13 +58,12 @@ void luaobj_setclass(lua_State *L, uint32_t type, char *name) {
udata->type = type;
luaL_getmetatable(L, name);
if (lua_isnil(L, -1)) {
luaL_error(
L, "missing metatable: assure class '%s' is inited in love.c\n", name);
luaL_error(L, "missing metatable: assure class '%s' is inited in love.c\n",
name);
}
lua_setmetatable(L, -2);
}
void *luaobj_newudata(lua_State *L, int size) {
/* Creates a udata of the requested size, plus space for the luaobj_head_t
* header. Returns the pointer to the udata's body */
@@ -77,7 +73,6 @@ void *luaobj_newudata(lua_State *L, int size) {
return udata + 1;
}
void *luaobj_checkudata(lua_State *L, int index, uint32_t type) {
/* Checks the header of a luaobj udata at the given index, taking into
* account super-classes it may have extended. Returns the udata's body if
@@ -89,4 +84,3 @@ void *luaobj_checkudata(lua_State *L, int index, uint32_t type) {
}
return udata + 1;
}

View File

@@ -14,12 +14,10 @@
#include "lib/lua/lualib.h"
#include "lib/lua/lauxlib.h"
typedef struct {
uint32_t type;
} luaobj_head_t;
/* Each mask should consist of its unique bit and the unique bit of all its
* super classes */
#define LUAOBJ_TYPE_IMAGE (1 << 0)
@@ -27,12 +25,10 @@ typedef struct {
#define LUAOBJ_TYPE_FONT (1 << 2)
#define LUAOBJ_TYPE_SOURCE (1 << 3)
int luaobj_newclass(lua_State *L, const char *name, const char *extends,
int (*constructor)(lua_State *), luaL_Reg *reg);
void luaobj_setclass(lua_State *L, uint32_t type, char *name);
void *luaobj_newudata(lua_State *L, int size);
void *luaobj_checkudata(lua_State *L, int index, uint32_t type);
#endif

View File

@@ -23,7 +23,6 @@
#include "palette.h"
#include "package.h"
static lua_State *L;
static void deinit(void) {
@@ -38,7 +37,6 @@ static void deinit(void) {
}
}
static int onLuaPanic(lua_State *L) {
vga_deinit();
const char *err = lua_tostring(L, -1);
@@ -46,7 +44,6 @@ static int onLuaPanic(lua_State *L) {
return 0;
}
int luaopen_love(lua_State *L);
int main(int argc, char **argv) {
@@ -87,12 +84,11 @@ int main(int argc, char **argv) {
#include "nogame_lua.h"
#include "boot_lua.h"
struct {
const char *name, *data; int size;
} items[] = {
{ "nogame.lua", nogame_lua, sizeof(nogame_lua) },
const char *name, *data;
int size;
} items[] = {{"nogame.lua", nogame_lua, sizeof(nogame_lua)},
{"boot.lua", boot_lua, sizeof(boot_lua)},
{ NULL, NULL, 0 }
};
{NULL, NULL, 0}};
int i;
for (i = 0; items[i].name; i++) {
int err = luaL_loadbuffer(L, items[i].data, items[i].size, items[i].name);

View File

@@ -8,14 +8,12 @@
#include "lib/cmixer/cmixer.h"
#include "luaobj.h"
int l_audio_setVolume(lua_State *L) {
double n = luaL_checknumber(L, 1);
cm_set_master_gain(n);
return 0;
}
int l_source_new(lua_State *L);
int luaopen_audio(lua_State *L) {

View File

@@ -8,13 +8,11 @@
#include "luaobj.h"
#include "event.h"
int l_event_pump(lua_State *L) {
event_pump();
return 0;
}
int l_event_poll(lua_State *L) {
event_t e;
if (event_poll(&e)) {
@@ -57,7 +55,6 @@ int l_event_poll(lua_State *L) {
return 0;
}
int l_event_quit(lua_State *L) {
int status = luaL_optnumber(L, 1, 0);
event_t e;
@@ -67,8 +64,6 @@ int l_event_quit(lua_State *L) {
return 0;
}
int luaopen_event(lua_State *L) {
luaL_Reg reg[] = {
{"pump", l_event_pump},

View File

@@ -8,7 +8,6 @@
#include "filesystem.h"
#include "luaobj.h"
int l_filesystem_mount(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
int err = filesystem_mount(path);
@@ -21,7 +20,6 @@ int l_filesystem_mount(lua_State *L) {
return 1;
}
int l_filesystem_unmount(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
int err = filesystem_unmount(path);
@@ -34,28 +32,24 @@ int l_filesystem_unmount(lua_State *L) {
return 1;
}
int l_filesystem_exists(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
lua_pushboolean(L, filesystem_exists(filename));
return 1;
}
int l_filesystem_isFile(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
lua_pushboolean(L, filesystem_isFile(filename));
return 1;
}
int l_filesystem_isDirectory(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
lua_pushboolean(L, filesystem_isDirectory(filename));
return 1;
}
int l_filesystem_read(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
int size;
@@ -68,7 +62,6 @@ int l_filesystem_read(lua_State *L) {
return 1;
}
int l_filesystem_setWriteDir(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
int err = filesystem_setWriteDir(path);
@@ -81,7 +74,6 @@ int l_filesystem_setWriteDir(lua_State *L) {
return 1;
}
int l_filesystem_write(lua_State *L) {
size_t sz;
const char *filename = luaL_checkstring(L, 1);
@@ -94,7 +86,6 @@ int l_filesystem_write(lua_State *L) {
return 1;
}
int luaopen_filesystem(lua_State *L) {
luaL_Reg reg[] = {
{"mount", l_filesystem_mount},

View File

@@ -11,7 +11,6 @@
#define CLASS_TYPE LUAOBJ_TYPE_FONT
#define CLASS_NAME "Font"
int l_font_new(lua_State *L) {
const char *filename;
int ptsize = 8;
@@ -26,21 +25,20 @@ int l_font_new(lua_State *L) {
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
if (filename) {
const char *err = font_init(self, filename, ptsize);
if (err) luaL_error(L, err);
if (err)
luaL_error(L, err);
} else {
font_initEmbedded(self, ptsize);
}
return 1;
}
int l_font_gc(lua_State *L) {
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
font_deinit(self);
return 0;
}
int l_font_getWidth(lua_State *L) {
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
const char *p = luaL_checkstring(L, 2);
@@ -52,15 +50,12 @@ int l_font_getWidth(lua_State *L) {
return 1;
}
int l_font_getHeight(lua_State *L) {
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
lua_pushinteger(L, self->height);
return 1;
}
int luaopen_font(lua_State *L) {
luaL_Reg reg[] = {
{"new", l_font_new},

View File

@@ -26,7 +26,6 @@ pixel_t graphics_color;
int graphics_color_rgb[3];
int graphics_blendMode;
static int getColorFromArgs(lua_State *L, int *rgb, const int *def) {
int r, g, b;
if (lua_isnoneornil(L, 1)) {
@@ -50,7 +49,6 @@ static int getColorFromArgs(lua_State *L, int *rgb, const int *def) {
return idx;
}
static int pushColor(lua_State *L, int *rgb) {
lua_pushinteger(L, rgb[0]);
lua_pushinteger(L, rgb[1]);
@@ -58,31 +56,26 @@ static int pushColor(lua_State *L, int *rgb) {
return 3;
}
int l_graphics_getDimensions(lua_State *L) {
lua_pushinteger(L, graphics_screen->width);
lua_pushinteger(L, graphics_screen->height);
return 2;
}
int l_graphics_getWidth(lua_State *L) {
lua_pushinteger(L, graphics_screen->width);
return 1;
}
int l_graphics_getHeight(lua_State *L) {
lua_pushinteger(L, graphics_screen->height);
return 1;
}
int l_graphics_getBackgroundColor(lua_State *L) {
return pushColor(L, graphics_backgroundColor_rgb);
}
int l_graphics_setBackgroundColor(lua_State *L) {
static const int def[] = {0, 0, 0};
int idx = getColorFromArgs(L, graphics_backgroundColor_rgb, def);
@@ -90,12 +83,10 @@ int l_graphics_setBackgroundColor(lua_State *L) {
return 0;
}
int l_graphics_getColor(lua_State *L) {
return pushColor(L, graphics_color_rgb);
}
int l_graphics_setColor(lua_State *L) {
static const int def[] = {0xff, 0xff, 0xff};
graphics_color = getColorFromArgs(L, graphics_color_rgb, def);
@@ -103,20 +94,28 @@ int l_graphics_setColor(lua_State *L) {
return 0;
}
int l_graphics_getBlendMode(lua_State *L) {
switch (graphics_blendMode) {
default:
case IMAGE_NORMAL : lua_pushstring(L, "normal"); break;
case IMAGE_FAST : lua_pushstring(L, "fast"); break;
case IMAGE_AND : lua_pushstring(L, "and"); break;
case IMAGE_OR : lua_pushstring(L, "or"); break;
case IMAGE_COLOR : lua_pushstring(L, "color"); break;
case IMAGE_NORMAL:
lua_pushstring(L, "normal");
break;
case IMAGE_FAST:
lua_pushstring(L, "fast");
break;
case IMAGE_AND:
lua_pushstring(L, "and");
break;
case IMAGE_OR:
lua_pushstring(L, "or");
break;
case IMAGE_COLOR:
lua_pushstring(L, "color");
break;
}
return 1;
}
int l_graphics_setBlendMode(lua_State *L) {
const char *str = lua_isnoneornil(L, 1) ? "normal" : luaL_checkstring(L, 1);
#define SET_BLEND_MODE(str, e) \
@@ -129,25 +128,33 @@ int l_graphics_setBlendMode(lua_State *L) {
} while (0)
switch (*str) {
case 'n' : SET_BLEND_MODE("normal", IMAGE_NORMAL); break;
case 'f' : SET_BLEND_MODE("fast", IMAGE_FAST); break;
case 'a' : SET_BLEND_MODE("and", IMAGE_AND); break;
case 'o' : SET_BLEND_MODE("or", IMAGE_OR); break;
case 'c' : SET_BLEND_MODE("color", IMAGE_COLOR); break;
case 'n':
SET_BLEND_MODE("normal", IMAGE_NORMAL);
break;
case 'f':
SET_BLEND_MODE("fast", IMAGE_FAST);
break;
case 'a':
SET_BLEND_MODE("and", IMAGE_AND);
break;
case 'o':
SET_BLEND_MODE("or", IMAGE_OR);
break;
case 'c':
SET_BLEND_MODE("color", IMAGE_COLOR);
break;
}
#undef SET_BLEND_MODE
luaL_argerror(L, 1, "bad blend mode");
return 0;
}
int l_graphics_getFont(lua_State *L) {
lua_pushlightuserdata(L, graphics_font);
lua_gettable(L, LUA_REGISTRYINDEX);
return 1;
}
int l_graphics_setFont(lua_State *L) {
font_t *oldFont = graphics_font;
if (lua_isnoneornil(L, 1)) {
@@ -172,14 +179,12 @@ int l_graphics_setFont(lua_State *L) {
return 0;
}
int l_graphics_getCanvas(lua_State *L) {
lua_pushlightuserdata(L, graphics_canvas);
lua_gettable(L, LUA_REGISTRYINDEX);
return 1;
}
int l_graphics_setCanvas(lua_State *L) {
image_t *oldCanvas = graphics_canvas;
if (lua_isnoneornil(L, 1)) {
@@ -204,15 +209,11 @@ int l_graphics_setCanvas(lua_State *L) {
return 0;
}
int l_graphics_reset(lua_State *L) {
int (*funcs[])(lua_State *) = {
l_graphics_setBackgroundColor,
l_graphics_setColor,
l_graphics_setBlendMode,
l_graphics_setFont,
l_graphics_setCanvas,
NULL,
l_graphics_setBackgroundColor, l_graphics_setColor,
l_graphics_setBlendMode, l_graphics_setFont,
l_graphics_setCanvas, NULL,
};
int i;
for (i = 0; funcs[i]; i++) {
@@ -222,7 +223,6 @@ int l_graphics_reset(lua_State *L) {
return 0;
}
int l_graphics_clear(lua_State *L) {
int idx = getColorFromArgs(L, NULL, graphics_backgroundColor_rgb);
int sz = graphics_canvas->width * graphics_canvas->height;
@@ -230,13 +230,11 @@ int l_graphics_clear(lua_State *L) {
return 0;
}
int l_graphics_present(lua_State *L) {
vga_update(graphics_screen->data);
return 0;
}
int l_graphics_draw(lua_State *L) {
image_t *img = luaobj_checkudata(L, 1, LUAOBJ_TYPE_IMAGE);
quad_t *quad = NULL;
@@ -256,16 +254,14 @@ int l_graphics_draw(lua_State *L) {
int bufh = graphics_canvas->height;
image_setFlip(flip);
if (quad) {
image_blit(img, buf, bufw, bufh, x, y,
quad->x, quad->y, quad->width, quad->height);
image_blit(img, buf, bufw, bufh, x, y, quad->x, quad->y, quad->width,
quad->height);
} else {
image_blit(img, buf, bufw, bufh, x, y,
0, 0, img->width, img->height);
image_blit(img, buf, bufw, bufh, x, y, 0, 0, img->width, img->height);
}
return 0;
}
int l_graphics_point(lua_State *L) {
int x = luaL_checknumber(L, 1);
int y = luaL_checknumber(L, 2);
@@ -273,7 +269,6 @@ int l_graphics_point(lua_State *L) {
return 0;
}
int l_graphics_line(lua_State *L) {
int argc = lua_gettop(L);
int lastx = luaL_checknumber(L, 1);
@@ -320,7 +315,6 @@ int l_graphics_line(lua_State *L) {
return 0;
}
int l_graphics_rectangle(lua_State *L) {
const char *mode = luaL_checkstring(L, 1);
int x = luaL_checknumber(L, 2);
@@ -336,14 +330,25 @@ int l_graphics_rectangle(lua_State *L) {
luaL_error(L, "bad mode");
}
/* Clip to screen */
if (x < 0) { x2 += x; x = 0; }
if (y < 0) { y2 += y; y = 0; }
if (x2 > graphics_canvas->width) { x2 = graphics_canvas->width; }
if (y2 > graphics_canvas->height) { y2 = graphics_canvas->height; }
if (x < 0) {
x2 += x;
x = 0;
}
if (y < 0) {
y2 += y;
y = 0;
}
if (x2 > graphics_canvas->width) {
x2 = graphics_canvas->width;
}
if (y2 > graphics_canvas->height) {
y2 = graphics_canvas->height;
}
/* Get width/height and Abort early if we're off screen */
int width = x2 - x;
int height = y2 - y;
if (width <= 0 || height <= 0) return 0;
if (width <= 0 || height <= 0)
return 0;
/* Draw */
if (fill) {
int i;
@@ -366,7 +371,6 @@ int l_graphics_rectangle(lua_State *L) {
return 0;
}
int l_graphics_circle(lua_State *L) {
const char *mode = luaL_checkstring(L, 1);
int x = luaL_checknumber(L, 2);
@@ -390,12 +394,18 @@ int l_graphics_circle(lua_State *L) {
int sx = (startx); \
int ex = (endx); \
int sy = (starty); \
if (sy < 0 || sy >= graphics_canvas->height) break;\
if (sx < 0) sx = 0;\
if (sx > graphics_canvas->width) sx = graphics_canvas->width;\
if (ex < 0) ex = 0;\
if (ex > graphics_canvas->width) ex = graphics_canvas->width;\
if (sx == ex) break;\
if (sy < 0 || sy >= graphics_canvas->height) \
break; \
if (sx < 0) \
sx = 0; \
if (sx > graphics_canvas->width) \
sx = graphics_canvas->width; \
if (ex < 0) \
ex = 0; \
if (ex > graphics_canvas->width) \
ex = graphics_canvas->width; \
if (sx == ex) \
break; \
memset(graphics_canvas->data + sx + sy * graphics_canvas->width, \
graphics_color, ex - sx); \
} while (0)
@@ -438,7 +448,6 @@ int l_graphics_circle(lua_State *L) {
return 0;
}
int l_graphics_print(lua_State *L) {
luaL_checkany(L, 1);
const char *str = luaL_tolstring(L, 1, NULL);
@@ -449,8 +458,6 @@ int l_graphics_print(lua_State *L) {
return 0;
}
int l_image_new(lua_State *L);
int l_image_newCanvas(lua_State *L);
int l_quad_new(lua_State *L);

View File

@@ -9,29 +9,29 @@
#include "palette.h"
#include "image.h"
#define CLASS_TYPE LUAOBJ_TYPE_IMAGE
#define CLASS_NAME "Image"
int l_image_new(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
image_t *self = luaobj_newudata(L, sizeof(*self));
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
const char *err = image_init(self, filename);
if (err) luaL_error(L, err);
if (err)
luaL_error(L, err);
return 1;
}
int l_image_newCanvas(lua_State *L) {
int width = VGA_WIDTH;
int height = VGA_HEIGHT;
if (lua_gettop(L) > 0) {
width = luaL_checknumber(L, 1);
height = luaL_checknumber(L, 2);
if (width <= 0) luaL_argerror(L, 1, "width must be larger than 0");
if (height <= 0) luaL_argerror(L, 2, "height must be larger than 0");
if (width <= 0)
luaL_argerror(L, 1, "width must be larger than 0");
if (height <= 0)
luaL_argerror(L, 2, "height must be larger than 0");
}
image_t *self = luaobj_newudata(L, sizeof(*self));
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
@@ -39,14 +39,12 @@ int l_image_newCanvas(lua_State *L) {
return 1;
}
int l_image_gc(lua_State *L) {
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
image_deinit(self);
return 0;
}
int l_image_getDimensions(lua_State *L) {
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
lua_pushinteger(L, self->width);
@@ -54,21 +52,18 @@ int l_image_getDimensions(lua_State *L) {
return 2;
}
int l_image_getWidth(lua_State *L) {
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
lua_pushinteger(L, self->width);
return 1;
}
int l_image_getHeight(lua_State *L) {
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
lua_pushinteger(L, self->height);
return 1;
}
int l_image_getPixel(lua_State *L) {
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
int x = luaL_checknumber(L, 2);
@@ -94,7 +89,6 @@ int l_image_getPixel(lua_State *L) {
}
}
int l_image_setPixel(lua_State *L) {
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
int x = luaL_checknumber(L, 2);
@@ -118,7 +112,6 @@ int l_image_setPixel(lua_State *L) {
return 0;
}
int luaopen_image(lua_State *L) {
luaL_Reg reg[] = {
{"new", l_image_new},

View File

@@ -5,17 +5,14 @@
* under the terms of the MIT license. See LICENSE for details.
*/
#include "keyboard.h"
#include "luaobj.h"
int l_keyboard_setKeyRepeat(lua_State *L) {
keyboard_setKeyRepeat(lua_toboolean(L, 1));
return 0;
}
int l_keyboard_isDown(lua_State *L) {
int n = lua_gettop(L);
int res = 0;
@@ -28,7 +25,6 @@ int l_keyboard_isDown(lua_State *L) {
return 1;
}
int luaopen_keyboard(lua_State *L) {
luaL_Reg reg[] = {
{"setKeyRepeat", l_keyboard_setKeyRepeat},

View File

@@ -7,15 +7,13 @@
#include "luaobj.h"
#define LOVE_VERSION "0.2.1"
#define LOVE_VERSION "0.42.23"
int l_love_getVersion(lua_State *L) {
lua_pushstring(L, LOVE_VERSION);
return 1;
}
int luaopen_image(lua_State *L);
int luaopen_quad(lua_State *L);
int luaopen_font(lua_State *L);
@@ -35,11 +33,7 @@ int luaopen_love(lua_State *L) {
* metatables required by their constructors. Any new classes must also be
* registered here before they will work. */
int (*classes[])(lua_State * L) = {
luaopen_image,
luaopen_quad,
luaopen_font,
luaopen_source,
NULL,
luaopen_image, luaopen_quad, luaopen_font, luaopen_source, NULL,
};
for (i = 0; classes[i]; i++) {
classes[i](L);
@@ -54,7 +48,10 @@ int luaopen_love(lua_State *L) {
luaL_newlib(L, reg);
/* Init submodules */
struct { char *name; int (*fn)(lua_State *L); } mods[] = {
struct {
char *name;
int (*fn)(lua_State *L);
} mods[] = {
{"system", luaopen_system},
{"event", luaopen_event},
{"filesystem", luaopen_filesystem},

View File

@@ -8,26 +8,22 @@
#include "mouse.h"
#include "luaobj.h"
int l_mouse_getPosition(lua_State *L) {
lua_pushinteger(L, mouse_getX());
lua_pushinteger(L, mouse_getY());
return 2;
}
int l_mouse_getX(lua_State *L) {
lua_pushinteger(L, mouse_getX());
return 1;
}
int l_mouse_getY(lua_State *L) {
lua_pushinteger(L, mouse_getY());
return 1;
}
int l_mouse_isDown(lua_State *L) {
int n = lua_gettop(L);
int res = 0;
@@ -42,8 +38,6 @@ int l_mouse_isDown(lua_State *L) {
return 1;
}
int luaopen_mouse(lua_State *L) {
luaL_Reg reg[] = {
{"getPosition", l_mouse_getPosition},

View File

@@ -13,11 +13,9 @@
#include "quad.h"
#include "luaobj.h"
#define CLASS_TYPE LUAOBJ_TYPE_QUAD
#define CLASS_NAME "Quad"
int l_quad_new(lua_State *L) {
quad_t *self = luaobj_newudata(L, sizeof(*self));
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
@@ -28,7 +26,6 @@ int l_quad_new(lua_State *L) {
return 1;
}
int l_quad_setViewport(lua_State *L) {
quad_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
self->x = luaL_checknumber(L, 2);
@@ -38,7 +35,6 @@ int l_quad_setViewport(lua_State *L) {
return 0;
}
int l_quad_getViewport(lua_State *L) {
quad_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
lua_pushnumber(L, self->x);
@@ -48,7 +44,6 @@ int l_quad_getViewport(lua_State *L) {
return 4;
}
int luaopen_quad(lua_State *L) {
luaL_Reg reg[] = {
{"new", l_quad_new},
@@ -59,4 +54,3 @@ int luaopen_quad(lua_State *L) {
luaobj_newclass(L, CLASS_NAME, NULL, l_quad_new, reg);
return 1;
}

View File

@@ -10,17 +10,14 @@
#include "filesystem.h"
#include "luaobj.h"
#define CLASS_TYPE LUAOBJ_TYPE_SOURCE
#define CLASS_NAME "Source"
typedef struct {
cm_Source *source;
void *data;
} source_t;
int l_source_new(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
/* Create object */
@@ -42,15 +39,15 @@ int l_source_new(lua_State *L) {
return 1;
}
int l_source_gc(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
if (self->source) cm_destroy_source(self->source);
if (self->data) filesystem_free(self->data);
if (self->source)
cm_destroy_source(self->source);
if (self->data)
filesystem_free(self->data);
return 0;
}
int l_source_setVolume(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
double n = luaL_checknumber(L, 2);
@@ -58,7 +55,6 @@ int l_source_setVolume(lua_State *L) {
return 0;
}
int l_source_setPitch(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
double n = luaL_checknumber(L, 2);
@@ -66,7 +62,6 @@ int l_source_setPitch(lua_State *L) {
return 0;
}
int l_source_setLooping(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
int enable = lua_toboolean(L, 2);
@@ -74,7 +69,6 @@ int l_source_setLooping(lua_State *L) {
return 0;
}
int l_source_getDuration(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
double n = cm_get_length(self->source);
@@ -82,28 +76,24 @@ int l_source_getDuration(lua_State *L) {
return 1;
}
int l_source_isPlaying(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
lua_pushboolean(L, cm_get_state(self->source) == CM_STATE_PLAYING);
return 1;
}
int l_source_isPaused(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
lua_pushboolean(L, cm_get_state(self->source) == CM_STATE_PAUSED);
return 1;
}
int l_source_isStopped(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
lua_pushboolean(L, cm_get_state(self->source) == CM_STATE_STOPPED);
return 1;
}
int l_source_tell(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
double n = cm_get_position(self->source);
@@ -111,28 +101,24 @@ int l_source_tell(lua_State *L) {
return 1;
}
int l_source_play(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
cm_play(self->source);
return 0;
}
int l_source_pause(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
cm_pause(self->source);
return 0;
}
int l_source_stop(lua_State *L) {
source_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
cm_stop(self->source);
return 0;
}
int luaopen_source(lua_State *L) {
luaL_Reg reg[] = {
{"new", l_source_new},

View File

@@ -11,20 +11,16 @@
#include "luaobj.h"
#include "vga.h"
int l_system_getOS(lua_State *L) {
lua_pushstring(L, "DOS");
return 1;
}
int l_system_getMemUsage(lua_State *L) {
lua_pushnumber(L, lua_gc(L, LUA_GCCOUNT, 0) + dmt_usage() / 1024);
return 1;
}
int luaopen_system(lua_State *L) {
luaL_Reg reg[] = {
{"getOS", l_system_getOS},

View File

@@ -19,7 +19,6 @@ double timer_avgAcc = 1;
int timer_avgCount;
double timer_avgTimer;
int l_timer_step(lua_State *L) {
/* Do delta */
long long now;
@@ -44,37 +43,31 @@ int l_timer_step(lua_State *L) {
return 0;
}
int l_timer_sleep(lua_State *L) {
delay(luaL_checknumber(L, 1) * 1000.);
return 1;
}
int l_timer_getDelta(lua_State *L) {
lua_pushnumber(L, timer_lastDt);
return 1;
}
int l_timer_getAverageDelta(lua_State *L) {
lua_pushnumber(L, timer_avgLastDt);
return 1;
}
int l_timer_getFPS(lua_State *L) {
lua_pushnumber(L, (int)(1. / timer_avgLastDt));
return 1;
}
int l_timer_getTime(lua_State *L) {
lua_pushnumber(L, uclock() / (double)UCLOCKS_PER_SEC);
return 1;
}
int luaopen_timer(lua_State *L) {
luaL_Reg reg[] = {
{"step", l_timer_step},

View File

@@ -17,14 +17,12 @@ int mouse_x, mouse_y;
int mouse_lastX, mouse_lastY;
int mouse_buttonStates[MOUSE_BUTTON_MAX];
void mouse_init(void) {
union REGS regs = {};
int86(0x33, &regs, &regs);
mouse_inited = regs.x.ax ? 1 : 0;
}
void mouse_update(void) {
if (!mouse_inited) {
return;
@@ -75,18 +73,8 @@ void mouse_update(void) {
}
}
int mouse_isDown(int button) { return mouse_buttonStates[button]; }
int mouse_getX(void) { return mouse_x; }
int mouse_isDown(int button) {
return mouse_buttonStates[button];
}
int mouse_getX(void) {
return mouse_x;
}
int mouse_getY(void) {
return mouse_y;
}
int mouse_getY(void) { return mouse_y; }

View File

@@ -15,7 +15,6 @@ enum {
MOUSE_BUTTON_MAX
};
void mouse_init(void);
void mouse_update(void);
int mouse_isDown(int button);

View File

@@ -15,7 +15,6 @@
#include "package.h"
static void error(const char *fmt, ...) {
va_list argp;
printf("Package error: ");
@@ -26,7 +25,6 @@ static void error(const char *fmt, ...) {
exit(EXIT_FAILURE);
}
static int tar_stream_write(mtar_t *tar, const void *data, unsigned size) {
unsigned res = fwrite(data, 1, size, tar->stream);
if (res != size) {
@@ -35,7 +33,6 @@ static int tar_stream_write(mtar_t *tar, const void *data, unsigned size) {
return MTAR_ESUCCESS;
}
static void concat(char *dst, int dstsz, ...) {
const char *s;
va_list argp;
@@ -53,9 +50,8 @@ static void concat(char *dst, int dstsz, ...) {
va_end(argp);
}
static
void concat_path(char *dst, int dstsz, const char *dir, const char *filename) {
static void concat_path(char *dst, int dstsz, const char *dir,
const char *filename) {
int dirlen = strlen(dir);
if (dir[dirlen - 1] == '/' || *dir == '\0') {
concat(dst, dstsz, dir, filename, NULL);
@@ -64,7 +60,6 @@ void concat_path(char *dst, int dstsz, const char *dir, const char *filename) {
}
}
static void write_file(mtar_t *tar, const char *inname, const char *outname) {
FILE *fp = fopen(inname, "rb");
if (!fp) {
@@ -95,7 +90,6 @@ static void write_file(mtar_t *tar, const char *inname, const char *outname) {
fclose(fp);
}
static void write_dir(mtar_t *tar, const char *indir, const char *outdir) {
char inbuf[256];
char outbuf[256];
@@ -138,8 +132,8 @@ static void write_dir(mtar_t *tar, const char *indir, const char *outdir) {
closedir(dir);
}
void package_make(const char *indir, const char *outfile, const char *exefile, int type) {
void package_make(const char *indir, const char *outfile, const char *exefile,
int type) {
/* Open file */
FILE *fp = fopen(outfile, "wb");
if (!fp) {
@@ -182,7 +176,6 @@ void package_make(const char *indir, const char *outfile, const char *exefile, i
fclose(fp);
}
int package_run(int argc, char **argv) {
/* Check for `--pack` argument; return failure if it isn't present */
if (argc < 2) {

View File

@@ -5,15 +5,10 @@
* under the terms of the MIT license. See LICENSE for details.
*/
enum {
PACKAGE_TTAR,
PACKAGE_TEXE
};
enum { PACKAGE_TTAR, PACKAGE_TEXE };
enum {
PACKAGE_ESUCCESS = 0,
PACKAGE_EFAILURE = -1
};
enum { PACKAGE_ESUCCESS = 0, PACKAGE_EFAILURE = -1 };
void package_make(const char *indir, const char *outfile, const char *exefile, int type);
void package_make(const char *indir, const char *outfile, const char *exefile,
int type);
int package_run(int argc, char **argv);

View File

@@ -15,19 +15,21 @@
#define MAP_SIZE 1024
#define MAP_MASK (MAP_SIZE - 1)
struct { unsigned color; int idx; } palette_map[MAP_SIZE];
struct {
unsigned color;
int idx;
} palette_map[MAP_SIZE];
unsigned palette_palette[MAX_IDX];
int palette_nextIdx;
int palette_inited;
void palette_init(void) {
if (palette_inited) return;
if (palette_inited)
return;
palette_reset();
palette_inited = 1;
}
void palette_reset(void) {
/* Reset nextIdx -- start at idx 1 as 0 is used for transparency */
palette_nextIdx = 1;
@@ -38,7 +40,6 @@ void palette_reset(void) {
}
}
static unsigned hash(const void *data, int size) {
unsigned hash = 5381;
const unsigned char *p = data;
@@ -48,7 +49,6 @@ static unsigned hash(const void *data, int size) {
return hash;
}
int palette_colorToIdx(int r, int g, int b) {
palette_init();
@@ -85,7 +85,6 @@ int palette_colorToIdx(int r, int g, int b) {
return idx;
}
int palette_idxToColor(int idx, int *rgb) {
/* Bounds check, return -1 on error */
if (idx <= 0 || idx >= MAX_IDX) {

View File

@@ -15,6 +15,4 @@ typedef struct {
lua_Number width, height;
} quad_t;
#endif

View File

@@ -19,10 +19,10 @@
#define BYTE(val, byte) (((val) >> ((byte)*8)) & 0xFF)
#define SOUNDBLASTER_SAMPLES_PER_BUFFER 2048
#define SAMPLE_BUFFER_SIZE (SOUNDBLASTER_SAMPLES_PER_BUFFER * sizeof(uint16_t) * 2)
#define SAMPLE_BUFFER_SIZE \
(SOUNDBLASTER_SAMPLES_PER_BUFFER * sizeof(uint16_t) * 2)
#define SAMPLE_RATE 22050
// SB16
#define BLASTER_RESET_PORT 0x6
#define BLASTER_READ_PORT 0xA
@@ -49,7 +49,6 @@
#define BLASTER_SPEAKER_OFF_CMD 0xD3
#define BLASTER_EXIT_AUTO_DMA 0xD9
// PIC
#define PIC1_COMMAND 0x20
#define PIC2_COMMAND 0xA0
@@ -59,12 +58,10 @@
#define PIC_IRQ07_MAP 0x08
#define PIC_IRQ8F_MAP 0x70
// DMA
#define DMA_DIRECTION_READ_FROM_MEMORY 0x08
#define DMA_TRANSFER_MODE_BLOCK 0x80
static const struct {
uint8_t startAddressRegister;
uint8_t countRegister;
@@ -73,15 +70,10 @@ static const struct {
uint8_t flipFlopResetRegister;
uint8_t pageRegister;
} dmaRegisters[] = {
{ 0x00, 0x01, 0x0A, 0x0B, 0x0C, 0x87 },
{ 0x02, 0x03, 0x0A, 0x0B, 0x0C, 0x83 },
{ 0x04, 0x05, 0x0A, 0x0B, 0x0C, 0x81 },
{ 0x06, 0x07, 0x0A, 0x0B, 0x0C, 0x82 },
{ 0xC0, 0xC2, 0xD4, 0xD6, 0xD8, 0x8F },
{ 0xC4, 0xC6, 0xD4, 0xD6, 0xD8, 0x8B },
{ 0xC8, 0xCA, 0xD4, 0xD6, 0xD8, 0x89 },
{ 0xCC, 0xCE, 0xD4, 0xD6, 0xD8, 0x8A }
};
{0x00, 0x01, 0x0A, 0x0B, 0x0C, 0x87}, {0x02, 0x03, 0x0A, 0x0B, 0x0C, 0x83},
{0x04, 0x05, 0x0A, 0x0B, 0x0C, 0x81}, {0x06, 0x07, 0x0A, 0x0B, 0x0C, 0x82},
{0xC0, 0xC2, 0xD4, 0xD6, 0xD8, 0x8F}, {0xC4, 0xC6, 0xD4, 0xD6, 0xD8, 0x8B},
{0xC8, 0xCA, 0xD4, 0xD6, 0xD8, 0x89}, {0xCC, 0xCE, 0xD4, 0xD6, 0xD8, 0x8A}};
static volatile int stopDma = 0;
static uint16_t *sampleBuffer;
@@ -95,25 +87,23 @@ static bool blasterInitialized = false;
static _go32_dpmi_seginfo oldBlasterHandler, newBlasterHandler;
static soundblaster_getSampleProc getSamples;
static void writeDSP(uint8_t value) {
while ((inportb(baseAddress + BLASTER_WRITE_PORT) &
BLASTER_WRITE_BUFFER_STATUS_UNAVAIL) != 0) {}
BLASTER_WRITE_BUFFER_STATUS_UNAVAIL) != 0) {
}
outportb(baseAddress + BLASTER_WRITE_PORT, value);
}
static uint8_t readDSP() {
uint8_t status;
while(((status = inportb(baseAddress + BLASTER_READ_BUFFER_STATUS_PORT))
& BLASTER_READ_BUFFER_STATUS_AVAIL) == 0) {
while (((status = inportb(baseAddress + BLASTER_READ_BUFFER_STATUS_PORT)) &
BLASTER_READ_BUFFER_STATUS_AVAIL) == 0) {
}
return inportb(baseAddress + BLASTER_READ_PORT);
}
static inline void delay3us() {
uint64_t waited = 0;
uclock_t lastTime = uclock();
@@ -130,7 +120,6 @@ static inline void delay3us() {
}
}
static int resetBlaster(void) {
for (int j = 0; j < 1000; ++j) {
outportb(baseAddress + BLASTER_RESET_PORT, 1);
@@ -144,7 +133,6 @@ static int resetBlaster(void) {
return SOUNDBLASTER_RESET_ERROR;
}
static void soundblasterISR(void) {
outportb(baseAddress + BLASTER_MIXER_OUT_PORT,
BLASTER_MIXER_INTERRUPT_STATUS);
@@ -155,8 +143,8 @@ static void soundblasterISR(void) {
writeDSP(BLASTER_EXIT_AUTO_DMA);
stopDma = 2;
} else {
uint8_t* dst = (uint8_t*)(sampleBuffer)
+ writePage * SAMPLE_BUFFER_SIZE / 2;
uint8_t *dst =
(uint8_t *)(sampleBuffer) + writePage * SAMPLE_BUFFER_SIZE / 2;
memcpy(dst, getSamples(), SAMPLE_BUFFER_SIZE / 2);
@@ -171,19 +159,18 @@ static void soundblasterISR(void) {
outportb(PIC1_COMMAND, PIC_EOI);
}
static void setBlasterISR(void) {
// Map IRQ to interrupt number on the CPU
uint16_t interruptVector = irq + irq + (irq < 8)
? PIC_IRQ07_MAP
: PIC_IRQ8F_MAP;
uint16_t interruptVector =
irq + irq + (irq < 8) ? PIC_IRQ07_MAP : PIC_IRQ8F_MAP;
_go32_dpmi_get_protected_mode_interrupt_vector(interruptVector,
&oldBlasterHandler);
newBlasterHandler.pm_offset = (int)soundblasterISR;
newBlasterHandler.pm_selector = _go32_my_cs();
_go32_dpmi_chain_protected_mode_interrupt_vector(interruptVector, &newBlasterHandler);
_go32_dpmi_chain_protected_mode_interrupt_vector(interruptVector,
&newBlasterHandler);
// PIC: unmask SB IRQ
if (irq < 8) {
@@ -197,7 +184,6 @@ static void setBlasterISR(void) {
isrInstalled = true;
}
static int parseBlasterSettings(void) {
char const *blasterEnv = getenv("BLASTER");
if (blasterEnv == 0) {
@@ -209,7 +195,8 @@ static int parseBlasterSettings(void) {
return SOUNDBLASTER_ENV_INVALID;
}
// "H" field may be preceeded by any number of other fields, so let's just search it
// "H" field may be preceeded by any number of other fields, so let's just
// search it
char const *dmaLoc = strchr(blasterEnv, 'H');
if (dmaLoc == NULL) {
return SOUNDBLASTER_ENV_INVALID;
@@ -220,14 +207,14 @@ static int parseBlasterSettings(void) {
return 0;
}
static int allocSampleBuffer(void) {
static int maxRetries = 10;
int selectors[maxRetries];
int current;
for (current = 0; current < maxRetries; ++current) {
int segment = __dpmi_allocate_dos_memory((SAMPLE_BUFFER_SIZE+15)>>4, &selectors[current]);
int segment = __dpmi_allocate_dos_memory((SAMPLE_BUFFER_SIZE + 15) >> 4,
&selectors[current]);
if (segment == -1) {
break;
}
@@ -256,30 +243,16 @@ static int allocSampleBuffer(void) {
return 0;
}
static void turnSpeakerOn(void) { writeDSP(BLASTER_SPEAKER_ON_CMD); }
static void turnSpeakerOn(void) {
writeDSP(BLASTER_SPEAKER_ON_CMD);
}
static void turnSpeakerOff(void) { writeDSP(BLASTER_SPEAKER_OFF_CMD); }
static void turnSpeakerOff(void) {
writeDSP(BLASTER_SPEAKER_OFF_CMD);
}
static void dmaSetupTransfer(int channel,
uint8_t direction,
bool autoReload,
bool down,
uint8_t mode,
uint32_t startAddress,
static void dmaSetupTransfer(int channel, uint8_t direction, bool autoReload,
bool down, uint8_t mode, uint32_t startAddress,
uint32_t count) {
uint8_t modeByte = direction
| mode
| ((uint8_t)autoReload << 4)
| ((uint8_t)down << 5)
| (channel & 0x03);
uint8_t modeByte = direction | mode | ((uint8_t)autoReload << 4) |
((uint8_t)down << 5) | (channel & 0x03);
uint8_t maskEnable = (channel & 0x03) | 0x04;
@@ -306,7 +279,6 @@ static void dmaSetupTransfer(int channel,
outportb(dmaRegisters[channel].singleChannelMaskRegister, maskEnable & 0x03);
}
static void startDMAOutput(void) {
uint32_t offset = ((uint32_t)sampleBuffer) - __djgpp_conventional_base;
@@ -319,15 +291,13 @@ static void startDMAOutput(void) {
writeDSP(BLASTER_SET_OUTPUT_SAMPLING_RATE);
writeDSP(BYTE(SAMPLE_RATE, 1));
writeDSP(BYTE(SAMPLE_RATE, 0));
writeDSP(BLASTER_PROGRAM_16BIT_IO_CMD
| BLASTER_PROGRAM_FLAG_AUTO_INIT
| BLASTER_PROGRAM_FLAG_FIFO);
writeDSP(BLASTER_PROGRAM_16BIT_IO_CMD | BLASTER_PROGRAM_FLAG_AUTO_INIT |
BLASTER_PROGRAM_FLAG_FIFO);
writeDSP(BLASTER_PROGRAM_SIGNED);
writeDSP(BYTE(samples / 2 - 1, 0));
writeDSP(BYTE(samples / 2 - 1, 1));
}
int soundblaster_init(soundblaster_getSampleProc getsamplesproc) {
if (!__djgpp_nearptr_enable()) {
return SOUNDBLASTER_DOS_ERROR;
@@ -347,7 +317,8 @@ int soundblaster_init(soundblaster_getSampleProc getsamplesproc) {
err = allocSampleBuffer();
if (err != 0) {
fprintf(stderr, "Could not allocate sample buffer in conventional memory\n");
fprintf(stderr,
"Could not allocate sample buffer in conventional memory\n");
return err;
}
@@ -362,29 +333,26 @@ int soundblaster_init(soundblaster_getSampleProc getsamplesproc) {
return 0;
}
static void deallocSampleBuffer(void) {
__dpmi_free_dos_memory(sampleBufferSelector);
}
static void resetBlasterISR(void) {
if (isrInstalled) {
uint16_t interruptVector = irq + irq + (irq < 8)
? PIC_IRQ07_MAP
: PIC_IRQ8F_MAP;
uint16_t interruptVector =
irq + irq + (irq < 8) ? PIC_IRQ07_MAP : PIC_IRQ8F_MAP;
_go32_dpmi_set_protected_mode_interrupt_vector(interruptVector, &oldBlasterHandler);
_go32_dpmi_set_protected_mode_interrupt_vector(interruptVector,
&oldBlasterHandler);
isrInstalled = false;
}
}
static void stopDMAOutput(void) {
stopDma = 1;
while(stopDma == 1) {}
while (stopDma == 1) {
}
}
void soundblaster_deinit(void) {
if (blasterInitialized) {
@@ -397,11 +365,7 @@ void soundblaster_deinit(void) {
}
}
int soundblaster_getSampleRate(void) {
return SAMPLE_RATE;
}
int soundblaster_getSampleRate(void) { return SAMPLE_RATE; }
int soundblaster_getSampleBufferSize(void) {
return SOUNDBLASTER_SAMPLES_PER_BUFFER;

View File

@@ -15,25 +15,24 @@
int vga_inited = 0;
void vga_init(void) {
if (vga_inited) return;
if (vga_inited)
return;
vga_inited = 1;
union REGS regs = {};
regs.h.al = 0x13;
int86(0x10, &regs, &regs);
}
void vga_deinit(void) {
if (!vga_inited) return;
if (!vga_inited)
return;
vga_inited = 0;
union REGS regs = {};
regs.h.al = 0x3;
int86(0x10, &regs, &regs);
}
void vga_setPalette(int idx, int r, int g, int b) {
outp(0x03c8, idx);
outp(0x03c9, (r >> 2) & 0x3f);
@@ -41,7 +40,6 @@ void vga_setPalette(int idx, int r, int g, int b) {
outp(0x03c9, (b >> 2) & 0x3f);
}
void vga_update(pixel_t *buffer) {
dosmemput(buffer, VGA_WIDTH * VGA_HEIGHT, 0xa0000);
}