Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

NumBlocks: the outline on the block looked at

Mason Chen758dd7a0edf9
diff --git a/games/numblocks/src/render.c b/games/numblocks/src/render.c
index c352a7b7..e99d4c85 100644
--- a/games/numblocks/src/render.c
+++ b/games/numblocks/src/render.c
@@ -1308,6 +1308,82 @@ static void line_strip(int py0, int rows, uint16_t (*cb)[RW], float (*zb)[RW]) {
}
}
+/* ---------------------------------------------------------------- the block looked at */
+/* RenderGlobal.drawSelectionBox: the edges of the block's selection box (its shape, grown
+ * by 0.002) in black at 40%, hidden where something is in front of them */
+static float sel_pt[8][3]; /* the corners on the picture: x, y, 1 / depth (0: behind the camera) */
+static float sel_w[8][3]; /* and in the cache */
+static bool sel_on;
+
+/* the selection box (sixteenths) of block b: its shape's bounds; plants as BlockBush and its kin */
+static void select_box(int b, int x, int y, int z, float *o) {
+ int8_t bx[5][6];
+ int n = block_boxes(b, x, y, z, bx);
+ float lo[3] = {0, 0, 0}, hi[3] = {16, 16, 16};
+ if (n) {
+ for (int i = 0; i < 3; i++) lo[i] = 16, hi[i] = 0;
+ for (int k = 0; k < n; k++)
+ for (int i = 0; i < 3; i++) {
+ if (bx[k][i] < lo[i]) lo[i] = bx[k][i];
+ if (bx[k][i + 3] > hi[i]) hi[i] = bx[k][i + 3];
+ }
+ } else if (blk_model[b] == M_CROSS) {
+ float r = 6.4f, h = 12.8f; /* tall grass, ferns, saplings, dead bushes: 0.4 around, 0.8 high */
+ if (b >= B_DANDELION && b <= B_OXEYE_DAISY) r = 3.2f, h = 9.6f; /* flowers */
+ else if (b == B_BROWN_MUSHROOM || b == B_RED_MUSHROOM) r = 3.2f, h = 6.4f; /* mushrooms */
+ else if (b == B_SUGAR_CANE) r = 6, h = 16;
+ else if ((b >= B_WHEAT_0 && b <= B_WHEAT_7) || (b >= B_CARROTS_0 && b <= B_CARROTS_3) ||
+ (b >= B_POTATOES_0 && b <= B_POTATOES_3))
+ r = 8, h = 4; /* crops */
+ lo[0] = lo[2] = 8 - r, hi[0] = hi[2] = 8 + r, hi[1] = h;
+ }
+ for (int i = 0; i < 3; i++) o[i] = lo[i] / 16 - 0.002f, o[i + 3] = hi[i] / 16 + 0.002f;
+}
+
+static void select_frame(void) {
+ sel_on = false;
+ if (pl.hit_face < 0 || gui != GUI_NONE || !world_loaded(pl.hit_x, pl.hit_y, pl.hit_z)) return;
+ int b = world_get(pl.hit_x, pl.hit_y, pl.hit_z);
+ if (b == B_AIR || blk_model[b] == M_LIQUID) return;
+ float s[6];
+ select_box(b, pl.hit_x, pl.hit_y, pl.hit_z, s);
+ float bx = (float)(pl.hit_x - vc_x0), by = (float)(pl.hit_y - vc_y0), bz = (float)(pl.hit_z - vc_z0);
+ for (int k = 0; k < 8; k++) {
+ float *w = sel_w[k], *q = sel_pt[k];
+ w[0] = bx + s[k & 1 ? 3 : 0], w[1] = by + s[k & 2 ? 4 : 1], w[2] = bz + s[k & 4 ? 5 : 2];
+ float rx = w[0] - ox, ry = w[1] - oy, rz = w[2] - oz, cz = rx * fwx + ry * fwy + rz * fwz;
+ q[2] = 0;
+ if (cz < 0.05f) continue;
+ float cx = rx * rgx + rz * rgz, cy = rx * upx + ry * upy + rz * upz;
+ q[0] = (cx / cz / TAN_H + 1) * RW / 2, q[1] = (1 - cy / cz / TAN_V) * RH / 2, q[2] = 1 / cz;
+ }
+ sel_on = true;
+}
+
+static void select_strip(int py0, int rows, uint16_t (*cb)[RW], float (*zb)[RW]) {
+ if (!sel_on) return;
+ static const uint8_t edge[12][2] = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {0, 2}, {1, 3},
+ {4, 6}, {5, 7}, {0, 4}, {1, 5}, {2, 6}, {3, 7}};
+ for (int e = 0; e < 12; e++) {
+ const float *a = sel_pt[edge[e][0]], *b = sel_pt[edge[e][1]];
+ if (a[2] <= 0 || b[2] <= 0) continue; /* (an edge reaching behind the camera: not drawn) */
+ if ((a[1] < py0 - 1 && b[1] < py0 - 1) || (a[1] > py0 + rows && b[1] > py0 + rows)) continue;
+ float dx = b[0] - a[0], dy = b[1] - a[1];
+ int n = (int)ceilf(fabsf(dx) > fabsf(dy) ? fabsf(dx) : fabsf(dy));
+ if (n < 1) n = 1;
+ int lx = -1, ly = -1;
+ for (int k = 0; k <= n; k++) {
+ float s = (float)k / n;
+ int x = (int)floorf(a[0] + dx * s), y = (int)floorf(a[1] + dy * s);
+ if ((x == lx && y == ly) || y < py0 || y >= py0 + rows || (unsigned)x >= RW) continue;
+ lx = x, ly = y;
+ /* in front of what was drawn there (or the face it outlines: a little give) */
+ float t = 1 / (a[2] + (b[2] - a[2]) * s);
+ if (zb[y - py0][x] * 1.02f + 0.02f > t) cb[y - py0][x] = mix565(cb[y - py0][x], 0, 13);
+ }
+ }
+}
+
static void ents_strip(int py0, int rows, uint16_t (*cb)[RW], float (*zb)[RW]) {
Box bx[MAX_PARTS];
particles_strip(py0, rows, cb, zb);
@@ -1338,6 +1414,7 @@ static void ents_strip(int py0, int rows, uint16_t (*cb)[RW], float (*zb)[RW]) {
zb[py - py0][px] = t;
}
}
+ select_strip(py0, rows, cb, zb);
line_strip(py0, rows, cb, zb);
rain_strip(py0, rows, cb, zb);
bolt_strip(py0, rows, cb, zb);
@@ -1520,6 +1597,7 @@ void render_frame(const Camera *c, uint32_t tod) {
rain_frame();
bolt_frame();
line_frame();
+ select_frame();
/* every other pixel of every other row is traced; the others come from
* their neighbours when those met the same face (or the sky) */
Sample *top = rows[0], *mid = rows[1], *bot = rows[2];