mirror of https://git.sr.ht/~rabbits/thuesday
Housekeeping
This commit is contained in:
parent
b12d1be3b7
commit
d10dec803e
4
makefile
4
makefile
|
@ -11,9 +11,9 @@ all: dest
|
|||
dest:
|
||||
@ mkdir -p bin
|
||||
run: all bin/thuesday
|
||||
@ bin/thuesday examples/text.modal
|
||||
@ bin/thuesday examples/draw.modal
|
||||
debug: all bin/thuesday-debug
|
||||
@ bin/thuesday-debug examples/text.modal
|
||||
@ bin/thuesday-debug examples/draw.modal
|
||||
install: all
|
||||
@ cp bin/thuesday ~/bin/
|
||||
uninstall:
|
||||
|
|
25
src/modal.c
25
src/modal.c
|
@ -17,6 +17,31 @@ static int flip;
|
|||
|
||||
#define spacer(c) (c <= ' ' || c == '(' || c == ')')
|
||||
|
||||
int
|
||||
chex(char c)
|
||||
{
|
||||
c -= 0x30;
|
||||
if(c < 10) return c;
|
||||
c -= 0x31;
|
||||
if(c < 6) return 10 + c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sint(char *s)
|
||||
{
|
||||
char c = *s, *cap = walk(s);
|
||||
int r = 0, n = 1;
|
||||
if(c == '#') {
|
||||
s++;
|
||||
while((c = *s) && s++ < cap) r = (r << 4) | chex(c);
|
||||
return r;
|
||||
}
|
||||
if(c == '-') { n = -1, s++; }
|
||||
while((c = *s) && s++ < cap) r = r * 10 + c - '0';
|
||||
return r * n;
|
||||
}
|
||||
|
||||
char *
|
||||
walk(char *s)
|
||||
{
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
of, or relating to structure as opposed to substance.
|
||||
*/
|
||||
|
||||
int chex(char c);
|
||||
int sint(char *s);
|
||||
int load(char *filepath);
|
||||
void eval(char *event);
|
||||
char *walk(char *s);
|
||||
|
|
|
@ -54,37 +54,11 @@ wcmp(char *a, char *b)
|
|||
return !*b;
|
||||
}
|
||||
|
||||
int
|
||||
chex(char c)
|
||||
{
|
||||
c -= 0x30;
|
||||
if(c < 10) return c;
|
||||
c -= 0x31;
|
||||
if(c < 6) return 10 + c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sint(char *s)
|
||||
{
|
||||
char c = *s, *cap = walk(s);
|
||||
int r = 0, n = 1;
|
||||
if(c == '#') {
|
||||
s++;
|
||||
while((c = *s) && s++ < cap) r = (r << 4) | chex(c);
|
||||
return r;
|
||||
}
|
||||
if(c == '-') { n = -1, s++; }
|
||||
while((c = *s) && s++ < cap) r = r * 10 + c - '0';
|
||||
return r * n;
|
||||
}
|
||||
|
||||
static int
|
||||
emu_resize(int w, int h)
|
||||
{
|
||||
if(window) {
|
||||
static Visual *visual;
|
||||
w *= viewport.scale, h *= viewport.scale;
|
||||
visual = DefaultVisual(display, 0);
|
||||
ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)viewport.pixels, w, h, 32, 0);
|
||||
XResizeWindow(display, window, w + PAD * 2, h + PAD * 2);
|
||||
|
@ -96,16 +70,17 @@ emu_resize(int w, int h)
|
|||
static void
|
||||
screen_resize(Uint16 width, Uint16 height, int scale)
|
||||
{
|
||||
int i, length = width * height * sizeof(Uint32);
|
||||
Uint32 *pixels = NULL;
|
||||
if(width < 0x8 || height < 0x8 || width >= 0x800 || height >= 0x800 || scale < 1 || scale >= 4)
|
||||
return;
|
||||
if(viewport.width == width && viewport.height == height && viewport.scale == scale)
|
||||
if(viewport.width == width && viewport.height == height)
|
||||
return;
|
||||
pixels = realloc(viewport.pixels, width * height * sizeof(Uint32) * scale * scale);
|
||||
pixels = realloc(viewport.pixels, length);
|
||||
if(!pixels)
|
||||
return;
|
||||
viewport.pixels = pixels;
|
||||
viewport.width = width, viewport.height = height, viewport.scale = scale;
|
||||
viewport.width = width, viewport.height = height;
|
||||
emu_resize(width, height);
|
||||
screen_change(0, 0, width, height);
|
||||
}
|
||||
|
@ -116,21 +91,6 @@ clamp(int val, int min, int max)
|
|||
return (val >= min) ? (val <= max) ? val : max : min;
|
||||
}
|
||||
|
||||
static void
|
||||
toggle_scale(void)
|
||||
{
|
||||
int s = viewport.scale + 1;
|
||||
if(s > 3) s = 1;
|
||||
screen_resize(viewport.width, viewport.height, s);
|
||||
}
|
||||
|
||||
static void
|
||||
emu_restart(int soft)
|
||||
{
|
||||
(void)soft;
|
||||
screen_resize(viewport.width, viewport.height, viewport.scale);
|
||||
}
|
||||
|
||||
static int
|
||||
emu_end(void)
|
||||
{
|
||||
|
@ -233,9 +193,8 @@ echo(char *s)
|
|||
static int
|
||||
argnum(char **args, int count)
|
||||
{
|
||||
int acc = 0;
|
||||
while(*args++) acc++;
|
||||
return count == acc;
|
||||
while(*args++) count--;
|
||||
return !count;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -265,6 +224,8 @@ interrupt(char *s, char **args)
|
|||
draw_pixel(sint(args[0]), sint(args[1]), sint(args[2])), viewport.full = 1;
|
||||
else if(wcmp(s, "@pixel") && argnum(args, 3))
|
||||
dst_ += snprintf(dst_, 0x10, "#%06x", read_pixel(sint(args[0]), sint(args[1]), sint(args[2])));
|
||||
else if(wcmp(s, "line") && argnum(args, 5))
|
||||
draw_line(sint(args[0]), sint(args[1]), sint(args[2]), sint(args[3]), sint(args[4]));
|
||||
else if(wcmp(s, "rect") && argnum(args, 5))
|
||||
line_rect(sint(args[0]), sint(args[1]), sint(args[2]), sint(args[3]), sint(args[4]));
|
||||
else if(wcmp(s, "fill-rect") && argnum(args, 5))
|
||||
|
@ -273,12 +234,11 @@ interrupt(char *s, char **args)
|
|||
line_circ(sint(args[0]), sint(args[1]), sint(args[2]), sint(args[3]));
|
||||
else if(wcmp(s, "fill-circ") && argnum(args, 4))
|
||||
fill_circ(sint(args[0]), sint(args[1]), sint(args[2]), sint(args[3]));
|
||||
else if(wcmp(s, "line") && argnum(args, 5))
|
||||
draw_line(sint(args[0]), sint(args[1]), sint(args[2]), sint(args[3]), sint(args[4]));
|
||||
else if(wcmp(s, "size") && argnum(args, 2))
|
||||
screen_resize(sint(args[0]), sint(args[1]), 1);
|
||||
else if(wcmp(s, "@size"))
|
||||
dst_ += snprintf(dst_, 0x10, "(%d %d)", viewport.width, viewport.height);
|
||||
/* Sprite */
|
||||
else if(wcmp(s, "save") && argnum(args, 2))
|
||||
draw_save(sint(args[0]), args[1]);
|
||||
else if(wcmp(s, "icn") && argnum(args, 4))
|
||||
|
@ -309,8 +269,8 @@ emu_event(void)
|
|||
XNextEvent(display, &ev);
|
||||
switch(ev.type) {
|
||||
case Expose: {
|
||||
int w = viewport.width * viewport.scale;
|
||||
int h = viewport.height * viewport.scale;
|
||||
int w = viewport.width;
|
||||
int h = viewport.height;
|
||||
XResizeWindow(display, window, w + PAD * 2, h + PAD * 2);
|
||||
XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, PAD, PAD, w, h);
|
||||
} break;
|
||||
|
@ -321,11 +281,6 @@ emu_event(void)
|
|||
KeySym sym;
|
||||
char buf[7];
|
||||
XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0);
|
||||
switch(sym) {
|
||||
case XK_F1: toggle_scale(); break;
|
||||
case XK_F4: emu_restart(0); break;
|
||||
case XK_F5: emu_restart(1); break;
|
||||
}
|
||||
on_controller_down(get_button(sym));
|
||||
on_controller_key(sym < 0x80 ? sym : (Uint8)buf[0]);
|
||||
} break;
|
||||
|
@ -345,8 +300,8 @@ emu_event(void)
|
|||
} break;
|
||||
case MotionNotify: {
|
||||
XMotionEvent *e = (XMotionEvent *)&ev;
|
||||
int x = clamp((e->x - PAD) / viewport.scale, 0, viewport.width - 1);
|
||||
int y = clamp((e->y - PAD) / viewport.scale, 0, viewport.height - 1);
|
||||
int x = clamp((e->x - PAD), 0, viewport.width - 1);
|
||||
int y = clamp((e->y - PAD), 0, viewport.height - 1);
|
||||
on_mouse_mouse(x, y);
|
||||
} break;
|
||||
}
|
||||
|
@ -365,6 +320,7 @@ display_init(void)
|
|||
if(!display)
|
||||
return !printf("init Display failed");
|
||||
screen_resize(WIDTH, HEIGHT, 1);
|
||||
fill_rect(0, 0, WIDTH, HEIGHT, 0);
|
||||
/* start window */
|
||||
visual = DefaultVisual(display, 0);
|
||||
if(visual->class != TrueColor)
|
||||
|
@ -376,12 +332,6 @@ display_init(void)
|
|||
XStoreName(display, window, "Thuesday");
|
||||
XMapWindow(display, window);
|
||||
ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)viewport.pixels, viewport.width, viewport.height, 32, 0);
|
||||
/* hide cursor
|
||||
bitmap = XCreateBitmapFromData(display, window, empty, 1, 1);
|
||||
blank = XCreatePixmapCursor(display, bitmap, bitmap, &black, &black, 0, 0);
|
||||
XDefineCursor(display, window, blank);
|
||||
XFreeCursor(display, blank);
|
||||
XFreePixmap(display, bitmap); */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -416,8 +366,8 @@ emu_run(void)
|
|||
if(viewport.full)
|
||||
screen_change(0, 0, viewport.width, viewport.height), viewport.full = 0;
|
||||
if(screen_changed()) {
|
||||
int x = viewport.x1 * viewport.scale, y = viewport.y1 * viewport.scale;
|
||||
int w = viewport.x2 * viewport.scale - x, h = viewport.y2 * viewport.scale - y;
|
||||
int x = viewport.x1, y = viewport.y1;
|
||||
int w = viewport.x2 - x, h = viewport.y2 - y;
|
||||
XPutImage(display, window, DefaultGC(display, 0), ximage, x, y, x + PAD, y + PAD, w, h);
|
||||
viewport.x2 = viewport.y2 = 0;
|
||||
viewport.x1 = viewport.width, viewport.y1 = viewport.height;
|
||||
|
|
|
@ -157,7 +157,6 @@ draw_icns(int x, int y, int w, int h, Uint8 *sprite, Uint32 color)
|
|||
{
|
||||
int x_, y_;
|
||||
for(y_ = 0; y_ < h; y_++)
|
||||
for(x_ = 0; x_ < w; x_++) {
|
||||
for(x_ = 0; x_ < w; x_++)
|
||||
draw_icn(x + (x_ << 3), y + (y_ << 3), sprite, color), sprite += 8;
|
||||
}
|
||||
}
|
|
@ -6,8 +6,8 @@ typedef signed short Sint16;
|
|||
typedef unsigned int Uint32;
|
||||
|
||||
typedef struct Viewport {
|
||||
int width, height, x1, y1, x2, y2, full, scale;
|
||||
Uint32 palette[4], *pixels;
|
||||
int width, height, x1, y1, x2, y2, full;
|
||||
Uint32 *pixels;
|
||||
Uint8 ppu[0x1000];
|
||||
} Viewport;
|
||||
|
||||
|
@ -17,8 +17,10 @@ int screen_changed(void);
|
|||
void screen_change(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2);
|
||||
|
||||
Uint32 read_pixel(unsigned int x, unsigned int y, Uint32 default_color);
|
||||
|
||||
void draw_pixel(unsigned int x, unsigned int y, Uint32 color);
|
||||
void draw_line(int ax, int ay, int bx, int by, Uint32 color);
|
||||
|
||||
void fill_rect(int x, int y, int width, int height, Uint32 color);
|
||||
void line_rect(int x, int y, int width, int height, Uint32 color);
|
||||
void fill_circ(int x, int y, int radius, Uint32 color);
|
||||
|
|
Loading…
Reference in New Issue