From 5c42155c53be7dab7584f953343ad2ac10e7d1d5 Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Sep 07 2025 18:26:40 +0000 Subject: x11: shisen: improve 3 --- diff --git a/simple/x11/lib/img.c b/simple/x11/lib/img.c index 7db136f..f15931c 100644 --- a/simple/x11/lib/img.c +++ b/simple/x11/lib/img.c @@ -118,7 +118,7 @@ static void rowResample( ) { if (dcnt < scnt) { // downscale - unsigned int sistep = dcnt << 8, si = sistep, di1 = 0x100, w = 0, sum[4]; + unsigned int sistep = dcnt << 8, si = sistep, di1 = 0x100, w = 0, sum[4] = {}; for(Color *e = src + scnt*sstep; src != e; src += sstep, si += sistep) { unsigned int di = si/scnt; if (di < di1) { diff --git a/simple/x11/shisen/shisen.c b/simple/x11/shisen/shisen.c index da2c8ce..7c30e74 100644 --- a/simple/x11/shisen/shisen.c +++ b/simple/x11/shisen/shisen.c @@ -5,9 +5,11 @@ #include #include +#define MAXSIZE 40 +#define MAXCOUNT (MAXSIZE*MAXSIZE) +#define VBASE 8 // must be even +#define HBASE 10 // must be even -#define COLS 16 -#define ROWS 9 #define ACTIVELETTERS 20 #define MAXLETTERS 256 #define MAXSOUNDS 16 @@ -15,8 +17,6 @@ #define EXTIME 2.0 #define SNDRATE 44100 -#define COUNT (ROWS*COLS) - typedef struct { Image src; @@ -50,16 +50,22 @@ typedef struct { } Cell; +int cols = 4; +int rows = 4; +int count = 4*4; + Letter menuLetter; Letter letters[MAXLETTERS]; int lettersCount; +int baseCellWidth = 1; +int baseCellHeight = 1; int cellWidth = 1; int cellHeight = 1; -Cell board[ROWS][COLS]; +Cell board[MAXSIZE][MAXSIZE]; Pos hover = { -1, -1 }; Pos selected = { -1, -1 }; -Track tracks[COUNT]; +Track tracks[MAXCOUNT]; int tracksCount; int glyphsMask = 0; int mode; @@ -192,6 +198,8 @@ int loadLetters(const char *filename, const char *filesel, int rows, int cols) { if (letter->glyphsCount > 0) ++letter; } lettersCount = letter - letters; + baseCellWidth = wws; + baseCellHeight = hhs; cellWidth = ww; cellHeight = hh; assert(menuLetter.glyphsCount == 4); @@ -264,12 +272,31 @@ void shuffleLetters(int glyps) { } +void calcSize() { + if (winW*baseCellHeight < baseCellWidth*winH) { + cols = HBASE; // portrait + rows = winH*cols*baseCellWidth/(winW*baseCellHeight); + } else { + rows = VBASE; // landscape + cols = winW*rows*baseCellHeight/(winH*baseCellWidth); + } + cols -= 4; // place for buttons + rows -= 2; + if (rows < 1) rows = 1; + if (cols < 1) cols = 1; + if (rows > MAXSIZE) rows = MAXSIZE; + if (cols > MAXSIZE) cols = MAXSIZE; + count = rows*cols; +} + + void simpleBoard() { clearBoard(); - for(int i = 0; i < COUNT; ++i) { + calcSize(); + for(int i = 0; i < count; ++i) { Letter *l = &letters[i%lettersCount]; - board[i/COLS][i%COLS].l = l; - board[i/COLS][i%COLS].g = &l->glyphs[1]; + board[i/cols][i%cols].l = l; + board[i/cols][i%cols].g = &l->glyphs[1]; } mode = 1; } @@ -279,18 +306,19 @@ void generateBoard(int glyphsMask, int maxLetters) { if (maxLetters < 1) maxLetters = 1; clearBoard(); + calcSize(); shuffleLetters(glyphsMask); - int indices[COUNT]; - for(int i = 0; i < COUNT; ++i) indices[i] = i; + int indices[MAXCOUNT]; + for(int i = 0; i < count; ++i) indices[i] = i; int lc = lettersCount < maxLetters ? lettersCount : maxLetters; - for(int i = 0; i < COUNT; ++i) { - int j = rand()%(COUNT - i) + i; + for(int i = 0; i < count; ++i) { + int j = rand()%(count - i) + i; int id = indices[j]; indices[j] = indices[i]; indices[i] = id; Letter *l = &letters[ (id/4)%lc ]; - board[i/COLS][i%COLS].l = l; - board[i/COLS][i%COLS].g = l->g[id%4]; + board[i/cols][i%cols].l = l; + board[i/cols][i%cols].g = l->g[id%4]; } mode = 1; @@ -298,7 +326,7 @@ void generateBoard(int glyphsMask, int maxLetters) { Letter* get(Pos p) - { return p.r >= 0 && p.r < ROWS && p.c >= 0 && p.c < COLS ? board[p.r][p.c].l : NULL; } + { return p.r >= 0 && p.r < rows && p.c >= 0 && p.c < cols ? board[p.r][p.c].l : NULL; } int findSubTrack1(Track *t, Pos b) { @@ -347,7 +375,7 @@ int findSubTrack3(Track *t, Pos b) { ++t->len; *p = p0; - for(++p->c; p->c <= COLS; ++p->c) + for(++p->c; p->c <= cols; ++p->c) if (get(*p)) break; else if (findSubTrack2(t, b, 0)) return 1; @@ -357,7 +385,7 @@ int findSubTrack3(Track *t, Pos b) { if (findSubTrack2(t, b, 0)) return 1; *p = p0; - for(++p->r; p->r <= ROWS; ++p->r) + for(++p->r; p->r <= rows; ++p->r) if (get(*p)) break; else if (findSubTrack2(t, b, 1)) return 1; @@ -383,10 +411,10 @@ Track findTrack(Pos a, Pos b) { void hint() { if (tracksCount) return; - for(int i = 0; i < COUNT; ++i) { - Pos a = { i/COLS, i%COLS }; - for(int j = 0; j < COUNT; ++j) { - Pos b = { j/COLS, j%COLS }; + for(int i = 0; i < count; ++i) { + Pos a = { i/cols, i%cols }; + for(int j = 0; j < count; ++j) { + Pos b = { j/cols, j%cols }; Track t = findTrack(a, b); if (t.len) tracks[tracksCount++] = t; @@ -462,19 +490,19 @@ void mouseDown(int x, int y) { hover.r = hover.c = -1; hintLetter = NULL; tracksCount = 0; - for(int r = 0; r < ROWS; ++r) - for(int c = 0; c < COLS; ++c) + for(int r = 0; r < rows; ++r) + for(int c = 0; c < cols; ++c) if (!board[r][c].l) board[r][c].g = NULL; - if (mc >= COLS && mr >= ROWS) { + if (mc >= cols && mr >= rows) { hintLetter = get(selected); if (!hintLetter) hint(); selected.r = selected.c = -1; } else - if (mc >= COLS && mr < 0) { + if (mc >= cols && mr < 0) { mute = mute == 1 ? 2 : 1; } else - if (mc >= 0 && mr >= 0 && mc < COLS && mr < COLS) { + if (mc >= 0 && mr >= 0 && mc < cols && mr < rows) { hover.r = mr; hover.c = mc; Letter *l = get(hover); @@ -505,8 +533,8 @@ void mouseDown(int x, int y) { void mouseUp() { int cnt = 0; - for(int r = 0; r < ROWS; ++r) - for(int c = 0; c < COLS; ++c) + for(int r = 0; r < rows; ++r) + for(int c = 0; c < cols; ++c) if (!board[r][c].l && board[r][c].g) { ++cnt; board[r][c].g = NULL; } if (cnt) tracksCount = 0; @@ -540,25 +568,25 @@ void draw() { if (glyphsMask) imgExDraw(&starttex, ox + cw, oy + ch, 2*cw, ch); } else { - if (winW*cellHeight*(ROWS + 2) < winH*cellWidth*(COLS + 4)) { - cw = winW/(COLS + 4); + if (winW*cellHeight*(rows + 2) < winH*cellWidth*(cols + 4)) { + cw = winW/(cols + 4); ch = cellHeight*cw/cellWidth; } else { - ch = winH/(ROWS + 2); + ch = winH/(rows + 2); cw = cellWidth*ch/cellHeight; } - ox = (winW - cw*COLS)/2; - oy = (winH - ch*ROWS)/2; + ox = (winW - cw*cols)/2; + oy = (winH - ch*rows)/2; // draw buttons imgExDraw(&backtex[back], ox - 2*cw, oy - ch, 2*cw, ch); - imgExDraw(&hinttex, ox + COLS*cw, oy + ROWS*ch, 2*cw, ch); - imgExDraw(&sndtex[mute], ox + COLS*cw, oy - ch, 2*cw, ch); + imgExDraw(&hinttex, ox + cols*cw, oy + rows*ch, 2*cw, ch); + imgExDraw(&sndtex[mute], ox + cols*cw, oy - ch, 2*cw, ch); for(int i = 0; i < 3; ++i) imgExResample(&sndtex[i], cw, ch); // draw cells - for(int r = 0; r < ROWS; ++r) - for(int c = 0; c < COLS; ++c) { + for(int r = 0; r < rows; ++r) + for(int c = 0; c < cols; ++c) { Cell *cell = &board[r][c]; if (!cell->g) continue; int hilight = (r == hover.r && c == hover.c)