@@ 91,17 91,6 @@ set(Grid *g, int x, int y, char c)
g->data[x + (y * g->w)] = c;
}
-/* Locks */
-
-void
-lock(Grid *g, int x, int y)
-{
- if(valid(g, x, y)) {
- g->lock[x + (y * g->w)] = 1;
- g->type[x + (y * g->w)] = 1;
- }
-}
-
/* Variables */
void
@@ 133,6 122,18 @@ settype(Grid *g, int x, int y, int t)
g->type[x + (y * g->w)] = t;
}
+/* Locks */
+
+void
+lock(Grid *g, int x, int y)
+{
+ if(valid(g, x, y)) {
+ g->lock[x + (y * g->w)] = 1;
+ if(!gettype(g, x, y))
+ settype(g, x, y, 1);
+ }
+}
+
/* Port Setters */
void
@@ 220,6 221,7 @@ ope(Grid *g, int x, int y, char c)
settype(g, x, y, 0);
setport(g, x + 1, y, c);
lock(g, x + 1, y);
+ settype(g, x + 1, y, 0);
}
}
@@ 325,6 327,7 @@ opn(Grid *g, int x, int y, char c)
settype(g, x, y, 0);
setport(g, x, y - 1, c);
lock(g, x, y - 1);
+ settype(g, x, y - 1, 0);
}
}
@@ 369,9 372,11 @@ opq(Grid *g, int x, int y, char c)
void
opr(Grid *g, int x, int y, char c)
{
- int min = cint(getport(g, x - 1, y, 0));
+ char min = getport(g, x - 1, y, 0);
char max = getport(g, x + 1, y, 1);
- setport(g, x, y + 1, cchr((random(g) % ((cint(max) - min) || 1)) + min, ciuc(max)));
+ int min_ = cint(min);
+ int max_ = cint(max);
+ setport(g, x, y + 1, cchr((random(g) % ((cint(max_) - min_) || 1)) + min_, ciuc(max)));
(void)c;
}
@@ 405,10 410,17 @@ opt(Grid *g, int x, int y, char c)
void
opu(Grid *g, int x, int y, char c)
{
- int max = cint(getport(g, x - 1, y, 0));
- int step = cint(getport(g, x + 1, y, 1));
- int bucket = (step * (g->f + max - 1)) % max + step;
- setport(g, x, y + 1, bucket >= max ? '*' : '.');
+ char step = getport(g, x - 1, y, 1);
+ char max = getport(g, x + 1, y, 0);
+ int step_ = cint(step);
+ int max_ = cint(max);
+ int bucket;
+ if(!step_)
+ step_ = 1;
+ if(!max_)
+ max_ = 8;
+ bucket = (step_ * (g->f + max_ - 1)) % max_ + step_;
+ setport(g, x, y + 1, bucket >= max_ ? '*' : '.');
(void)c;
}
@@ 434,6 446,7 @@ opw(Grid *g, int x, int y, char c)
settype(g, x, y, 0);
setport(g, x - 1, y, c);
lock(g, x - 1, y);
+ settype(g, x - 1, y, 0);
}
}
@@ 469,7 482,11 @@ opz(Grid *g, int x, int y, char c)
int rate_ = cint(rate);
int target_ = cint(target);
int val_ = cint(val);
- setport(g, x, y + 1, cchr(val_ + val_ < target_ ? rate_ : val_ > target_ ? -rate_ : 0, ciuc(target)));
+ int mod;
+ if(!rate_)
+ rate_ = 1;
+ mod = val_ <= target_ - rate_ ? rate_ : val_ >= target_ + rate_ ? -rate_ : target_ - val_;
+ setport(g, x, y + 1, cchr(val_ + mod, ciuc(target)));
(void)c;
}
@@ 1,6 1,7 @@
#include <SDL2/SDL.h>
#include <stdio.h>
#include "sim.h"
+#include "midi.h"
#define HOR 32
#define VER 16
@@ 35,6 36,7 @@ SDL_Renderer *gRenderer = NULL;
SDL_Texture *gTexture = NULL;
uint32_t *pixels;
+int down = 0;
Rect2d selection;
Grid g;
@@ 168,10 170,10 @@ draw(uint32_t *dst)
void
select(int x, int y, int w, int h)
{
- selection.x = x;
- selection.y = y;
- selection.w = w;
- selection.h = h;
+ selection.x = clamp(x, 0, HOR - 1);
+ selection.y = clamp(y, 0, VER - 1);
+ selection.w = clamp(w, 1, 36);
+ selection.h = clamp(h, 1, 36);
draw(pixels);
}
@@ 180,8 182,8 @@ move(int x, int y)
{
selection.x += x;
selection.y += y;
- selection.x = clamp(selection.x, 0, HOR);
- selection.y = clamp(selection.y, 0, VER);
+ selection.x = clamp(selection.x, 0, HOR - 1);
+ selection.y = clamp(selection.y, 0, VER - 1);
draw(pixels);
}
@@ 209,6 211,7 @@ play(void)
for(i = 0; i < g.msg_len; ++i) {
printf("%c", g.msg[i]);
}
+ fflush(stdout);
}
void
@@ 236,11 239,15 @@ domouse(SDL_Event *event)
switch(event->type) {
case SDL_MOUSEBUTTONUP:
select(selection.x, selection.y, touch.x / 8 - selection.x + 1, touch.y / 8 - selection.y + 1);
+ down = 0;
break;
case SDL_MOUSEBUTTONDOWN:
select(touch.x / 8, touch.y / 8, 1, 1);
+ down = 1;
break;
case SDL_MOUSEMOTION:
+ if(down)
+ select(selection.x, selection.y, touch.x / 8 - selection.x + 1, touch.y / 8 - selection.y + 1);
break;
}
}
@@ 337,7 344,7 @@ init(void)
int
loadfont(void)
{
- FILE *f = fopen("font-bold.chr", "rb");
+ FILE *f = fopen("font-light.chr", "rb");
if(f == NULL)
return error("Font", "Invalid input file");
if(!fread(font, sizeof(font), 1, f))