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 "lib/cmixer/cmixer.h"
#include "soundblaster.h" #include "soundblaster.h"
static short audio_buffer[SOUNDBLASTER_SAMPLES_PER_BUFFER * 2]; static short audio_buffer[SOUNDBLASTER_SAMPLES_PER_BUFFER * 2];
static const short *audio_callback(void) { static const short *audio_callback(void) {
@@ -17,13 +16,9 @@ static const short* audio_callback(void) {
return audio_buffer; return audio_buffer;
} }
void audio_init(void) { void audio_init(void) {
cm_init(soundblaster_getSampleRate()); cm_init(soundblaster_getSampleRate());
soundblaster_init(audio_callback); 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; unsigned writei, readi;
} events; } events;
const char *event_typestr(int type) { const char *event_typestr(int type) {
switch (type) { switch (type) {
case EVENT_QUIT : return "quit"; case EVENT_QUIT:
case EVENT_KEYBOARD_PRESSED : return "keypressed"; return "quit";
case EVENT_KEYBOARD_RELEASED : return "keyreleased"; case EVENT_KEYBOARD_PRESSED:
case EVENT_KEYBOARD_TEXTINPUT : return "textinput"; return "keypressed";
case EVENT_MOUSE_MOVED : return "mousemoved"; case EVENT_KEYBOARD_RELEASED:
case EVENT_MOUSE_PRESSED : return "mousepressed"; return "keyreleased";
case EVENT_MOUSE_RELEASED : return "mousereleased"; 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 "?"; return "?";
} }
void event_push(event_t *e) { void event_push(event_t *e) {
events.buffer[events.writei++ & BUFFER_MASK] = *e; events.buffer[events.writei++ & BUFFER_MASK] = *e;
} }
void event_pump(void) { void event_pump(void) {
keyboard_update(); keyboard_update();
mouse_update(); mouse_update();
} }
int event_poll(event_t *e) { int event_poll(event_t *e) {
if (events.readi != events.writei) { if (events.readi != events.writei) {
*e = events.buffer[events.readi++ & BUFFER_MASK]; *e = events.buffer[events.readi++ & BUFFER_MASK];

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -23,7 +23,6 @@
#include "palette.h" #include "palette.h"
#include "package.h" #include "package.h"
static lua_State *L; static lua_State *L;
static void deinit(void) { static void deinit(void) {
@@ -38,7 +37,6 @@ static void deinit(void) {
} }
} }
static int onLuaPanic(lua_State *L) { static int onLuaPanic(lua_State *L) {
vga_deinit(); vga_deinit();
const char *err = lua_tostring(L, -1); const char *err = lua_tostring(L, -1);
@@ -46,7 +44,6 @@ static int onLuaPanic(lua_State *L) {
return 0; return 0;
} }
int luaopen_love(lua_State *L); int luaopen_love(lua_State *L);
int main(int argc, char **argv) { int main(int argc, char **argv) {
@@ -87,12 +84,11 @@ int main(int argc, char **argv) {
#include "nogame_lua.h" #include "nogame_lua.h"
#include "boot_lua.h" #include "boot_lua.h"
struct { struct {
const char *name, *data; int size; const char *name, *data;
} items[] = { int size;
{ "nogame.lua", nogame_lua, sizeof(nogame_lua) }, } items[] = {{"nogame.lua", nogame_lua, sizeof(nogame_lua)},
{"boot.lua", boot_lua, sizeof(boot_lua)}, {"boot.lua", boot_lua, sizeof(boot_lua)},
{ NULL, NULL, 0 } {NULL, NULL, 0}};
};
int i; int i;
for (i = 0; items[i].name; i++) { for (i = 0; items[i].name; i++) {
int err = luaL_loadbuffer(L, items[i].data, items[i].size, items[i].name); 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 "lib/cmixer/cmixer.h"
#include "luaobj.h" #include "luaobj.h"
int l_audio_setVolume(lua_State *L) { int l_audio_setVolume(lua_State *L) {
double n = luaL_checknumber(L, 1); double n = luaL_checknumber(L, 1);
cm_set_master_gain(n); cm_set_master_gain(n);
return 0; return 0;
} }
int l_source_new(lua_State *L); int l_source_new(lua_State *L);
int luaopen_audio(lua_State *L) { int luaopen_audio(lua_State *L) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -17,14 +17,12 @@ int mouse_x, mouse_y;
int mouse_lastX, mouse_lastY; int mouse_lastX, mouse_lastY;
int mouse_buttonStates[MOUSE_BUTTON_MAX]; int mouse_buttonStates[MOUSE_BUTTON_MAX];
void mouse_init(void) { void mouse_init(void) {
union REGS regs = {}; union REGS regs = {};
int86(0x33, &regs, &regs); int86(0x33, &regs, &regs);
mouse_inited = regs.x.ax ? 1 : 0; mouse_inited = regs.x.ax ? 1 : 0;
} }
void mouse_update(void) { void mouse_update(void) {
if (!mouse_inited) { if (!mouse_inited) {
return; 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) { int mouse_getY(void) { return mouse_y; }
return mouse_buttonStates[button];
}
int mouse_getX(void) {
return mouse_x;
}
int mouse_getY(void) {
return mouse_y;
}

View File

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

View File

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

View File

@@ -5,15 +5,10 @@
* under the terms of the MIT license. See LICENSE for details. * under the terms of the MIT license. See LICENSE for details.
*/ */
enum { enum { PACKAGE_TTAR, PACKAGE_TEXE };
PACKAGE_TTAR,
PACKAGE_TEXE
};
enum { enum { PACKAGE_ESUCCESS = 0, PACKAGE_EFAILURE = -1 };
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); int package_run(int argc, char **argv);

View File

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

View File

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

View File

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

View File

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