ucmd_solitaire.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  1. /* solitaire: a Klondike solitaire for a canvas window.
  2. *
  3. * Seven tableau piles, four foundations, a stock and a waste. Draw one or
  4. * three, drag cards or whole runs, double-click to send a card to a
  5. * foundation, and click the stock to deal / recycle. The table keeps the
  6. * classic look (green felt, white cards, a blue patterned back and the
  7. * bouncing cards after a win), while the menu, status bar and dialogs are
  8. * built from the shared ngui toolkit and its dark theme.
  9. *
  10. * Mouse only (the kernel keyboard path has no F2), so N starts a new game.
  11. * The window can be resized; the table is re-laid out.
  12. */
  13. #include "ucmd_lib.h"
  14. #include "ngui.h"
  15. #define TABLEAU_PILES 7
  16. #define FOUNDATION_PILES 4
  17. #define RANK_COUNT 13
  18. #define DECK_SIZE 52
  19. #define MENU_H 34
  20. #define STATUS_H 22
  21. #define START_W 760
  22. #define START_H 540
  23. #define C_FELT NGUI_RGB(0, 128, 0)
  24. #define C_SLOT_FILL NGUI_RGB(0, 112, 0)
  25. #define C_SLOT_LINE NGUI_RGB(0, 84, 0)
  26. #define C_SLOT_ICON NGUI_RGB(96, 190, 96)
  27. #define C_CARD NGUI_RGB(255, 255, 255)
  28. #define C_CARD_EDGE NGUI_RGB(40, 40, 40)
  29. #define C_SHADOW NGUI_RGB(0, 82, 0)
  30. #define C_RED NGUI_RGB(200, 0, 0)
  31. #define C_BLACK NGUI_RGB(0, 0, 0)
  32. #define C_GOLD NGUI_RGB(184, 144, 64)
  33. #define C_CREAM NGUI_RGB(255, 248, 220)
  34. #define C_SKIN NGUI_RGB(255, 222, 189)
  35. #define C_HAIR NGUI_RGB(72, 48, 24)
  36. enum {
  37. SRC_NONE = 0,
  38. SRC_TABLEAU,
  39. SRC_WASTE,
  40. SRC_FOUNDATION
  41. };
  42. enum {
  43. TGT_NONE = 0,
  44. TGT_TABLEAU,
  45. TGT_FOUNDATION
  46. };
  47. enum {
  48. HIT_NONE = 0,
  49. HIT_STOCK,
  50. HIT_WASTE,
  51. HIT_TABLEAU,
  52. HIT_FOUNDATION
  53. };
  54. enum {
  55. MENU_NONE = 0,
  56. MENU_GAME,
  57. MENU_HELP
  58. };
  59. enum {
  60. HELP_NONE = 0,
  61. HELP_HOWTO,
  62. HELP_ABOUT
  63. };
  64. enum {
  65. ACT_NONE = 0,
  66. ACT_NEW,
  67. ACT_DRAW1,
  68. ACT_DRAW3,
  69. ACT_BLUE,
  70. ACT_RED,
  71. ACT_HOWTO,
  72. ACT_ABOUT
  73. };
  74. #define ID_MENU_GAME 0x101u
  75. #define ID_MENU_HELP 0x102u
  76. #define ID_MENU_ITEM 0x120u
  77. #define ID_HELP_CLOSE 0x140u
  78. #define ID_HELP_X 0x141u
  79. #define BOUNCE_MAX 18
  80. struct card_pile {
  81. unsigned char id[52];
  82. unsigned char up[52];
  83. int n;
  84. };
  85. struct sol_layout {
  86. int cw, ch;
  87. int gap;
  88. int col_x[7];
  89. int top_y;
  90. int tab_y;
  91. int status_y;
  92. };
  93. struct sel {
  94. int kind;
  95. int pile;
  96. int index;
  97. };
  98. struct target {
  99. int kind;
  100. int pile;
  101. };
  102. struct hit {
  103. int kind;
  104. int pile;
  105. int index;
  106. };
  107. struct menu_entry {
  108. int action;
  109. const char *label;
  110. const char *shortcut;
  111. };
  112. struct bounce_card {
  113. int active;
  114. int settled;
  115. int id;
  116. int x, y, vx, vy;
  117. };
  118. static struct card_pile stock;
  119. static struct card_pile waste;
  120. static struct card_pile foundation[FOUNDATION_PILES];
  121. static struct card_pile tableau[TABLEAU_PILES];
  122. static int draw_three;
  123. static int deck_style;
  124. static int score;
  125. static int started;
  126. static int won;
  127. static int quit;
  128. static unsigned int start_ms;
  129. static unsigned int end_ms;
  130. static unsigned int rng_state;
  131. static unsigned int game_seq;
  132. static struct sel select_sel;
  133. static struct sel press_sel;
  134. static struct sel drag_sel;
  135. static struct target press_target;
  136. static int press_active;
  137. static int drag_active;
  138. static int gesture_consumed;
  139. static int press_x, press_y;
  140. static int press_card_x, press_card_y;
  141. static int drag_dx, drag_dy;
  142. static unsigned int last_click_ms;
  143. static struct sel last_click_sel;
  144. static int last_click_valid;
  145. static int menu_open;
  146. static int help_open;
  147. static int pending_action;
  148. static int last_sec = -1;
  149. static struct bounce_card bounce_cards[BOUNCE_MAX];
  150. static int bounce_count;
  151. static int bounce_active;
  152. static int bounce_show;
  153. static unsigned int bounce_start_ms;
  154. static unsigned int bounce_spawn_ms;
  155. static const struct menu_entry game_menu[] = {
  156. { ACT_NEW, "New game", "N" },
  157. { ACT_NONE, 0, 0 },
  158. { ACT_DRAW1, "Draw one", 0 },
  159. { ACT_DRAW3, "Draw three", 0 },
  160. { ACT_NONE, 0, 0 },
  161. { ACT_BLUE, "Blue deck", 0 },
  162. { ACT_RED, "Red deck", 0 }
  163. };
  164. static const struct menu_entry help_menu[] = {
  165. { ACT_HOWTO, "How to play", 0 },
  166. { ACT_ABOUT, "About Solitaire", 0 }
  167. };
  168. static const char *const howto_lines[] = {
  169. "Goal: build each suit from Ace to King.",
  170. "Drag cards to move them.",
  171. "Drop a card on one rank higher and of the opposite colour.",
  172. "Empty tableau piles accept a King.",
  173. "Double-click a card to send it to a foundation.",
  174. "Click the deck to draw; click an empty deck to recycle the waste.",
  175. "N: new game Esc: close"
  176. };
  177. static const char *const about_lines[] = {
  178. "NebOS edition",
  179. "",
  180. "A classic Klondike built with ngui.",
  181. "Drag, drop, and bounce."
  182. };
  183. /* ------------------------------------------------------------------ */
  184. /* small helpers */
  185. static struct ngui_rect rect_make(int x, int y, int w, int h)
  186. {
  187. struct ngui_rect r;
  188. r.x = x;
  189. r.y = y;
  190. r.w = w;
  191. r.h = h;
  192. return r;
  193. }
  194. static int point_in(const struct ngui_rect *r, int x, int y)
  195. {
  196. return x >= r->x && y >= r->y &&
  197. x < r->x + r->w && y < r->y + r->h;
  198. }
  199. static int clampi(int v, int lo, int hi)
  200. {
  201. if (v < lo)
  202. return lo;
  203. if (v > hi)
  204. return hi;
  205. return v;
  206. }
  207. static int imin(int a, int b)
  208. {
  209. return a < b ? a : b;
  210. }
  211. static int imax(int a, int b)
  212. {
  213. return a > b ? a : b;
  214. }
  215. static int card_suit(int id)
  216. {
  217. return id / RANK_COUNT;
  218. }
  219. static int card_rank(int id)
  220. {
  221. return id % RANK_COUNT;
  222. }
  223. static int suit_is_red(int suit)
  224. {
  225. return suit == 1 || suit == 2;
  226. }
  227. static const char *rank_text(int rank)
  228. {
  229. static const char *const text[RANK_COUNT] = {
  230. "A", "2", "3", "4", "5", "6", "7",
  231. "8", "9", "10", "J", "Q", "K"
  232. };
  233. if (rank < 0 || rank >= RANK_COUNT)
  234. return "?";
  235. return text[rank];
  236. }
  237. static unsigned int text_len(const char *s)
  238. {
  239. unsigned int n = 0;
  240. while (s && s[n])
  241. n++;
  242. return n;
  243. }
  244. static void fmt_int(char *out, int v)
  245. {
  246. char tmp[12];
  247. int n = 0, o = 0;
  248. unsigned int uv;
  249. if (v < 0) {
  250. out[o++] = '-';
  251. uv = (unsigned int)(-v);
  252. } else {
  253. uv = (unsigned int)v;
  254. }
  255. do {
  256. tmp[n++] = (char)('0' + (uv % 10));
  257. uv /= 10;
  258. } while (uv);
  259. while (n > 0)
  260. out[o++] = tmp[--n];
  261. out[o] = '\0';
  262. }
  263. static unsigned int rng_next(void)
  264. {
  265. unsigned int x = rng_state;
  266. x ^= x << 13;
  267. x ^= x >> 17;
  268. x ^= x << 5;
  269. rng_state = x ? x : 0x6D2B79F5u;
  270. return rng_state;
  271. }
  272. static void frame2(struct ngui *ui, struct ngui_rect r, unsigned short color)
  273. {
  274. if (r.w <= 0 || r.h <= 0)
  275. return;
  276. ngui_frame_round(ui, r, 4, 4, 4, 4, 2, color);
  277. }
  278. static void fill_circle(struct ngui *ui, int cx, int cy, int r,
  279. unsigned short color)
  280. {
  281. int dy;
  282. if (r < 1)
  283. r = 1;
  284. for (dy = -r; dy <= r; dy++) {
  285. int dx = 0;
  286. while ((dx + 1) * (dx + 1) + dy * dy <= r * r)
  287. dx++;
  288. if (dx > 0)
  289. ngui_fill_rect(ui,
  290. rect_make(cx - dx, cy + dy,
  291. dx * 2 + 1, 1), color);
  292. }
  293. }
  294. /* ------------------------------------------------------------------ */
  295. /* layout */
  296. static void compute_layout(struct ngui *ui, struct sol_layout *l)
  297. {
  298. int margin, gap, cw, ch, max_ch, total, i;
  299. margin = clampi(ui->w / 64, 6, 16);
  300. gap = clampi(ui->w / 110, 4, 10);
  301. cw = (ui->w - 2 * margin - 6 * gap) / 7;
  302. cw = clampi(cw, 14, 92);
  303. ch = cw * 135 / 100;
  304. max_ch = (ui->h - MENU_H - STATUS_H - 30) / 2;
  305. if (max_ch < 18)
  306. max_ch = 18;
  307. if (ch > max_ch) {
  308. ch = max_ch;
  309. cw = ch * 100 / 135;
  310. if (cw < 14)
  311. cw = 14;
  312. }
  313. total = 7 * cw + 6 * gap;
  314. if (total > ui->w - 2) {
  315. if (gap > 2)
  316. gap = 2;
  317. total = 7 * cw + 6 * gap;
  318. if (total > ui->w - 2) {
  319. cw = (ui->w - 2 - 6 * gap) / 7;
  320. if (cw < 10)
  321. cw = 10;
  322. total = 7 * cw + 6 * gap;
  323. }
  324. }
  325. l->cw = cw;
  326. l->ch = ch;
  327. l->gap = gap;
  328. for (i = 0; i < 7; i++)
  329. l->col_x[i] = (ui->w - total) / 2 + i * (cw + gap);
  330. l->top_y = MENU_H + 8;
  331. l->tab_y = l->top_y + ch + 18;
  332. l->status_y = ui->h - STATUS_H;
  333. if (l->status_y < MENU_H + 20)
  334. l->status_y = MENU_H + 20;
  335. if (l->tab_y + ch > l->status_y - 2) {
  336. l->tab_y = l->status_y - ch - 2;
  337. if (l->tab_y < l->top_y + 4)
  338. l->tab_y = l->top_y + 4;
  339. }
  340. }
  341. static struct ngui_rect stock_rect(const struct sol_layout *l)
  342. {
  343. return rect_make(l->col_x[0], l->top_y, l->cw, l->ch);
  344. }
  345. static struct ngui_rect waste_slot_rect(const struct sol_layout *l)
  346. {
  347. return rect_make(l->col_x[1], l->top_y, l->cw, l->ch);
  348. }
  349. static struct ngui_rect foundation_rect(const struct sol_layout *l, int f)
  350. {
  351. return rect_make(l->col_x[3 + f], l->top_y, l->cw, l->ch);
  352. }
  353. static struct ngui_rect tableau_slot_rect(const struct sol_layout *l, int p)
  354. {
  355. return rect_make(l->col_x[p], l->tab_y, l->cw, l->ch);
  356. }
  357. static int waste_fan(const struct sol_layout *l)
  358. {
  359. return imax(4, l->cw / 5);
  360. }
  361. static void pile_metrics(const struct card_pile *p,
  362. const struct sol_layout *l, int *fd, int *fu)
  363. {
  364. int down = 0, up, extra, base_extra;
  365. while (down < p->n && !p->up[down])
  366. down++;
  367. up = p->n - down;
  368. *fd = imax(4, l->ch / 10);
  369. *fu = imax(9, l->ch / 4);
  370. extra = l->status_y - l->tab_y - 2 - l->ch;
  371. if (extra < 4)
  372. extra = 4;
  373. base_extra = down * (*fd) + (up > 0 ? up - 1 : 0) * (*fu);
  374. if (base_extra > extra) {
  375. *fd = (*fd) * extra / base_extra;
  376. *fu = (*fu) * extra / base_extra;
  377. if (*fd < 2)
  378. *fd = 2;
  379. if (*fu < 4)
  380. *fu = 4;
  381. }
  382. }
  383. static int pile_y(const struct card_pile *p, const struct sol_layout *l,
  384. int index)
  385. {
  386. int fd, fu, y = l->tab_y, i;
  387. pile_metrics(p, l, &fd, &fu);
  388. for (i = 0; i < index && i < p->n; i++)
  389. y += p->up[i] ? fu : fd;
  390. return y;
  391. }
  392. /* ------------------------------------------------------------------ */
  393. /* selection helpers */
  394. static int sel_count(const struct sel *s)
  395. {
  396. if (s->kind == SRC_TABLEAU)
  397. return tableau[s->pile].n - s->index;
  398. if (s->kind == SRC_WASTE || s->kind == SRC_FOUNDATION)
  399. return 1;
  400. return 0;
  401. }
  402. static int sel_first_id(const struct sel *s)
  403. {
  404. if (s->kind == SRC_TABLEAU) {
  405. if (s->index < 0 || s->index >= tableau[s->pile].n)
  406. return -1;
  407. return tableau[s->pile].id[s->index];
  408. }
  409. if (s->kind == SRC_WASTE) {
  410. if (waste.n <= 0)
  411. return -1;
  412. return waste.id[waste.n - 1];
  413. }
  414. if (s->kind == SRC_FOUNDATION) {
  415. if (foundation[s->pile].n <= 0)
  416. return -1;
  417. return foundation[s->pile].id[foundation[s->pile].n - 1];
  418. }
  419. return -1;
  420. }
  421. static int sel_valid(const struct sel *s)
  422. {
  423. int i, id, next;
  424. if (s->kind == SRC_NONE)
  425. return 0;
  426. if (s->kind == SRC_TABLEAU) {
  427. struct card_pile *p = &tableau[s->pile];
  428. if (s->pile < 0 || s->pile >= TABLEAU_PILES)
  429. return 0;
  430. if (s->index < 0 || s->index >= p->n)
  431. return 0;
  432. if (!p->up[s->index])
  433. return 0;
  434. for (i = s->index; i < p->n; i++) {
  435. if (!p->up[i])
  436. return 0;
  437. if (i + 1 < p->n) {
  438. id = p->id[i];
  439. next = p->id[i + 1];
  440. if (card_rank(next) != card_rank(id) - 1)
  441. return 0;
  442. if (suit_is_red(card_suit(next)) ==
  443. suit_is_red(card_suit(id)))
  444. return 0;
  445. }
  446. }
  447. return 1;
  448. }
  449. if (s->kind == SRC_WASTE)
  450. return waste.n > 0;
  451. if (s->kind == SRC_FOUNDATION)
  452. return s->pile >= 0 && s->pile < FOUNDATION_PILES &&
  453. foundation[s->pile].n > 0;
  454. return 0;
  455. }
  456. static int same_sel(const struct sel *a, const struct sel *b)
  457. {
  458. return a->kind == b->kind && a->pile == b->pile &&
  459. a->index == b->index;
  460. }
  461. static void clear_selection(void)
  462. {
  463. select_sel.kind = SRC_NONE;
  464. select_sel.pile = 0;
  465. select_sel.index = 0;
  466. }
  467. static struct ngui_rect sel_rect(const struct sol_layout *l,
  468. const struct sel *s)
  469. {
  470. struct card_pile *p;
  471. if (s->kind == SRC_TABLEAU) {
  472. p = &tableau[s->pile];
  473. return rect_make(l->col_x[s->pile],
  474. pile_y(p, l, s->index), l->cw, l->ch);
  475. }
  476. if (s->kind == SRC_WASTE) {
  477. int x = l->col_x[1];
  478. if (draw_three && waste.n > 1) {
  479. int start = waste.n > 3 ? waste.n - 3 : 0;
  480. int shown = waste.n - start;
  481. x += (shown - 1) * waste_fan(l);
  482. }
  483. return rect_make(x, l->top_y, l->cw, l->ch);
  484. }
  485. if (s->kind == SRC_FOUNDATION)
  486. return foundation_rect(l, s->pile);
  487. return rect_make(0, 0, 0, 0);
  488. }
  489. /* ------------------------------------------------------------------ */
  490. /* hit testing and drop targets */
  491. static struct hit hit_test(const struct sol_layout *l, int x, int y)
  492. {
  493. struct hit h;
  494. int i, f;
  495. h.kind = HIT_NONE;
  496. h.pile = 0;
  497. h.index = -1;
  498. {
  499. struct ngui_rect r = stock_rect(l);
  500. if (point_in(&r, x, y)) {
  501. h.kind = HIT_STOCK;
  502. return h;
  503. }
  504. }
  505. {
  506. struct ngui_rect r = waste_slot_rect(l);
  507. int fan = waste_fan(l);
  508. r.w += 2 * fan;
  509. if (point_in(&r, x, y)) {
  510. h.kind = HIT_WASTE;
  511. h.pile = 0;
  512. h.index = waste.n - 1;
  513. return h;
  514. }
  515. }
  516. for (f = 0; f < FOUNDATION_PILES; f++) {
  517. struct ngui_rect r = foundation_rect(l, f);
  518. if (point_in(&r, x, y)) {
  519. h.kind = HIT_FOUNDATION;
  520. h.pile = f;
  521. h.index = foundation[f].n - 1;
  522. return h;
  523. }
  524. }
  525. if (y >= l->tab_y && y < l->status_y) {
  526. for (i = 0; i < TABLEAU_PILES; i++) {
  527. struct card_pile *p = &tableau[i];
  528. if (x < l->col_x[i] || x >= l->col_x[i] + l->cw)
  529. continue;
  530. h.kind = HIT_TABLEAU;
  531. h.pile = i;
  532. h.index = -1;
  533. if (p->n == 0)
  534. return h;
  535. {
  536. int k;
  537. for (k = p->n - 1; k >= 0; k--) {
  538. int y0 = pile_y(p, l, k);
  539. int y1 = (k == p->n - 1) ? y0 + l->ch :
  540. pile_y(p, l, k + 1);
  541. if (y >= y0 && y < y1) {
  542. h.index = k;
  543. return h;
  544. }
  545. }
  546. }
  547. return h;
  548. }
  549. }
  550. return h;
  551. }
  552. static struct sel selection_from_hit(const struct hit *h)
  553. {
  554. struct sel s;
  555. s.kind = SRC_NONE;
  556. s.pile = 0;
  557. s.index = 0;
  558. if (h->kind == HIT_WASTE && waste.n > 0) {
  559. s.kind = SRC_WASTE;
  560. s.index = waste.n - 1;
  561. } else if (h->kind == HIT_FOUNDATION && h->index >= 0) {
  562. s.kind = SRC_FOUNDATION;
  563. s.pile = h->pile;
  564. s.index = foundation[h->pile].n - 1;
  565. } else if (h->kind == HIT_TABLEAU && h->index >= 0) {
  566. s.kind = SRC_TABLEAU;
  567. s.pile = h->pile;
  568. s.index = h->index;
  569. }
  570. if (!sel_valid(&s))
  571. s.kind = SRC_NONE;
  572. return s;
  573. }
  574. static struct target target_from_hit(const struct hit *h)
  575. {
  576. struct target t;
  577. t.kind = TGT_NONE;
  578. t.pile = 0;
  579. if (h->kind == HIT_FOUNDATION) {
  580. t.kind = TGT_FOUNDATION;
  581. t.pile = h->pile;
  582. } else if (h->kind == HIT_TABLEAU) {
  583. t.kind = TGT_TABLEAU;
  584. t.pile = h->pile;
  585. }
  586. return t;
  587. }
  588. static struct target drop_target_at(const struct sol_layout *l, int x, int y)
  589. {
  590. struct target t;
  591. int f, p;
  592. t.kind = TGT_NONE;
  593. t.pile = 0;
  594. if (y >= l->top_y - 10 && y <= l->top_y + l->ch + 10) {
  595. for (f = 0; f < FOUNDATION_PILES; f++) {
  596. int x0 = l->col_x[3 + f] - l->gap / 2;
  597. int x1 = l->col_x[3 + f] + l->cw + l->gap / 2;
  598. if (x >= x0 && x < x1) {
  599. t.kind = TGT_FOUNDATION;
  600. t.pile = f;
  601. return t;
  602. }
  603. }
  604. }
  605. if (y >= l->tab_y - 8 && y < l->status_y) {
  606. for (p = 0; p < TABLEAU_PILES; p++) {
  607. int x0 = l->col_x[p] - l->gap / 2;
  608. int x1 = l->col_x[p] + l->cw + l->gap / 2;
  609. if (x >= x0 && x < x1) {
  610. t.kind = TGT_TABLEAU;
  611. t.pile = p;
  612. return t;
  613. }
  614. }
  615. }
  616. return t;
  617. }
  618. static int can_drop(const struct sel *s, const struct target *t)
  619. {
  620. int id, r, suit;
  621. if (s->kind == SRC_NONE || t->kind == TGT_NONE)
  622. return 0;
  623. if (!sel_valid(s))
  624. return 0;
  625. id = sel_first_id(s);
  626. if (id < 0)
  627. return 0;
  628. r = card_rank(id);
  629. suit = card_suit(id);
  630. if (t->kind == TGT_FOUNDATION) {
  631. struct card_pile *f;
  632. if (s->kind == SRC_FOUNDATION)
  633. return 0;
  634. if (sel_count(s) != 1)
  635. return 0;
  636. if (t->pile < 0 || t->pile >= FOUNDATION_PILES)
  637. return 0;
  638. f = &foundation[t->pile];
  639. if (f->n == 0)
  640. return r == 0;
  641. if (card_suit(f->id[f->n - 1]) != suit)
  642. return 0;
  643. return r == f->n;
  644. }
  645. if (t->kind == TGT_TABLEAU) {
  646. struct card_pile *p;
  647. int top;
  648. if (t->pile < 0 || t->pile >= TABLEAU_PILES)
  649. return 0;
  650. if (s->kind == SRC_TABLEAU && s->pile == t->pile)
  651. return 0;
  652. p = &tableau[t->pile];
  653. if (p->n == 0)
  654. return r == 12;
  655. if (!p->up[p->n - 1])
  656. return 0;
  657. top = p->id[p->n - 1];
  658. if (card_rank(top) != r + 1)
  659. return 0;
  660. return suit_is_red(card_suit(top)) != suit_is_red(suit);
  661. }
  662. return 0;
  663. }
  664. static struct ngui_rect target_rect(const struct sol_layout *l,
  665. const struct target *t)
  666. {
  667. if (t->kind == TGT_FOUNDATION)
  668. return foundation_rect(l, t->pile);
  669. if (t->kind == TGT_TABLEAU) {
  670. struct card_pile *p = &tableau[t->pile];
  671. if (p->n > 0)
  672. return rect_make(l->col_x[t->pile],
  673. pile_y(p, l, p->n - 1), l->cw, l->ch);
  674. return tableau_slot_rect(l, t->pile);
  675. }
  676. return rect_make(0, 0, 0, 0);
  677. }
  678. static int hit_within_selection(const struct hit *h, const struct sel *s)
  679. {
  680. if (s->kind == SRC_TABLEAU && h->kind == HIT_TABLEAU &&
  681. h->pile == s->pile && h->index >= s->index)
  682. return 1;
  683. if (s->kind == SRC_WASTE && h->kind == HIT_WASTE)
  684. return 1;
  685. if (s->kind == SRC_FOUNDATION && h->kind == HIT_FOUNDATION &&
  686. h->pile == s->pile)
  687. return 1;
  688. return 0;
  689. }
  690. /* ------------------------------------------------------------------ */
  691. /* game actions */
  692. static void shuffle_deck(unsigned char *deck)
  693. {
  694. int i;
  695. for (i = DECK_SIZE - 1; i > 0; i--) {
  696. int j = (int)(rng_next() % (unsigned int)(i + 1));
  697. unsigned char t = deck[i];
  698. deck[i] = deck[j];
  699. deck[j] = t;
  700. }
  701. }
  702. static void new_game(unsigned int now)
  703. {
  704. unsigned char deck[DECK_SIZE];
  705. struct card_pile *p;
  706. int i, row, pos, f;
  707. game_seq++;
  708. rng_state = (u_ticks_ms() * 2654435761u) ^
  709. (game_seq * 2246822519u) ^ 0x9E3779B9u;
  710. if (!rng_state)
  711. rng_state = 0x6D2B79F5u;
  712. for (i = 0; i < DECK_SIZE; i++)
  713. deck[i] = (unsigned char)i;
  714. shuffle_deck(deck);
  715. stock.n = 0;
  716. waste.n = 0;
  717. for (f = 0; f < FOUNDATION_PILES; f++) {
  718. foundation[f].n = 0;
  719. for (i = 0; i < 52; i++)
  720. foundation[f].up[i] = 1;
  721. }
  722. for (i = 0; i < TABLEAU_PILES; i++) {
  723. tableau[i].n = 0;
  724. for (f = 0; f < 52; f++)
  725. tableau[i].up[f] = 0;
  726. }
  727. pos = 0;
  728. for (row = 0; row < TABLEAU_PILES; row++) {
  729. for (i = row; i < TABLEAU_PILES; i++) {
  730. p = &tableau[i];
  731. p->id[p->n] = deck[pos++];
  732. p->up[p->n] = 0;
  733. p->n++;
  734. }
  735. }
  736. for (i = 0; i < TABLEAU_PILES; i++) {
  737. p = &tableau[i];
  738. if (p->n > 0)
  739. p->up[p->n - 1] = 1;
  740. }
  741. while (pos < DECK_SIZE) {
  742. stock.id[stock.n] = deck[pos++];
  743. stock.up[stock.n] = 0;
  744. stock.n++;
  745. }
  746. score = 0;
  747. started = 1;
  748. won = 0;
  749. start_ms = now;
  750. end_ms = 0;
  751. last_sec = -1;
  752. clear_selection();
  753. press_active = 0;
  754. drag_active = 0;
  755. gesture_consumed = 0;
  756. last_click_valid = 0;
  757. menu_open = MENU_NONE;
  758. help_open = HELP_NONE;
  759. pending_action = ACT_NONE;
  760. bounce_active = 0;
  761. bounce_show = 0;
  762. bounce_count = 0;
  763. for (i = 0; i < BOUNCE_MAX; i++) {
  764. bounce_cards[i].active = 0;
  765. bounce_cards[i].settled = 0;
  766. }
  767. }
  768. static int foundation_for_id(int id)
  769. {
  770. int f, suit = card_suit(id), rank = card_rank(id);
  771. for (f = 0; f < FOUNDATION_PILES; f++) {
  772. if (foundation[f].n > 0 &&
  773. card_suit(foundation[f].id[foundation[f].n - 1]) == suit)
  774. return f;
  775. }
  776. if (rank == 0) {
  777. for (f = 0; f < FOUNDATION_PILES; f++)
  778. if (foundation[f].n == 0)
  779. return f;
  780. }
  781. return -1;
  782. }
  783. static void start_bounce(unsigned int now)
  784. {
  785. int i;
  786. for (i = 0; i < BOUNCE_MAX; i++) {
  787. bounce_cards[i].active = 0;
  788. bounce_cards[i].settled = 0;
  789. }
  790. bounce_count = 0;
  791. bounce_active = 1;
  792. bounce_show = 1;
  793. bounce_start_ms = now;
  794. bounce_spawn_ms = now;
  795. }
  796. static int check_win(unsigned int now)
  797. {
  798. int f, total = 0;
  799. for (f = 0; f < FOUNDATION_PILES; f++)
  800. total += foundation[f].n;
  801. if (total == DECK_SIZE && !won) {
  802. won = 1;
  803. end_ms = now;
  804. clear_selection();
  805. start_bounce(now);
  806. return 1;
  807. }
  808. return 0;
  809. }
  810. static int perform_move(const struct sel *s, const struct target *t,
  811. unsigned int now)
  812. {
  813. struct card_pile *src;
  814. unsigned char id;
  815. int i, count;
  816. if (!can_drop(s, t))
  817. return 0;
  818. count = sel_count(s);
  819. if (count <= 0)
  820. return 0;
  821. if (s->kind == SRC_TABLEAU) {
  822. src = &tableau[s->pile];
  823. id = src->id[s->index];
  824. if (t->kind == TGT_FOUNDATION) {
  825. struct card_pile *d = &foundation[t->pile];
  826. d->id[d->n] = id;
  827. d->up[d->n] = 1;
  828. d->n++;
  829. score += 10;
  830. } else {
  831. struct card_pile *d = &tableau[t->pile];
  832. for (i = 0; i < count; i++) {
  833. d->id[d->n] = src->id[s->index + i];
  834. d->up[d->n] = 1;
  835. d->n++;
  836. }
  837. }
  838. src->n = s->index;
  839. if (src->n > 0 && !src->up[src->n - 1]) {
  840. src->up[src->n - 1] = 1;
  841. score += 5;
  842. }
  843. } else if (s->kind == SRC_WASTE) {
  844. id = waste.id[waste.n - 1];
  845. waste.n--;
  846. if (t->kind == TGT_FOUNDATION) {
  847. struct card_pile *d = &foundation[t->pile];
  848. d->id[d->n] = id;
  849. d->up[d->n] = 1;
  850. d->n++;
  851. score += 10;
  852. } else {
  853. struct card_pile *d = &tableau[t->pile];
  854. d->id[d->n] = id;
  855. d->up[d->n] = 1;
  856. d->n++;
  857. score += 5;
  858. }
  859. } else if (s->kind == SRC_FOUNDATION) {
  860. src = &foundation[s->pile];
  861. id = src->id[src->n - 1];
  862. src->n--;
  863. if (t->kind == TGT_TABLEAU) {
  864. struct card_pile *d = &tableau[t->pile];
  865. d->id[d->n] = id;
  866. d->up[d->n] = 1;
  867. d->n++;
  868. score -= 15;
  869. }
  870. }
  871. clear_selection();
  872. drag_active = 0;
  873. last_click_valid = 0;
  874. check_win(now);
  875. return 1;
  876. }
  877. static int auto_to_foundation(const struct sel *s, unsigned int now)
  878. {
  879. struct target t;
  880. int id, f;
  881. if (sel_count(s) != 1)
  882. return 0;
  883. id = sel_first_id(s);
  884. if (id < 0)
  885. return 0;
  886. f = foundation_for_id(id);
  887. if (f < 0)
  888. return 0;
  889. t.kind = TGT_FOUNDATION;
  890. t.pile = f;
  891. if (!can_drop(s, &t))
  892. return 0;
  893. return perform_move(s, &t, now);
  894. }
  895. static void stock_click(unsigned int now)
  896. {
  897. int count;
  898. (void)now;
  899. if (won)
  900. return;
  901. if (stock.n > 0) {
  902. count = draw_three ? 3 : 1;
  903. while (count-- > 0 && stock.n > 0) {
  904. unsigned char id = stock.id[stock.n - 1];
  905. stock.n--;
  906. waste.id[waste.n] = id;
  907. waste.up[waste.n] = 1;
  908. waste.n++;
  909. }
  910. } else if (waste.n > 0) {
  911. while (waste.n > 0) {
  912. unsigned char id = waste.id[waste.n - 1];
  913. waste.n--;
  914. stock.id[stock.n] = id;
  915. stock.up[stock.n] = 0;
  916. stock.n++;
  917. }
  918. score += draw_three ? -20 : -100;
  919. }
  920. clear_selection();
  921. press_active = 0;
  922. last_click_valid = 0;
  923. }
  924. static int elapsed_seconds(unsigned int now)
  925. {
  926. unsigned int end = won ? end_ms : now;
  927. if (!started)
  928. return 0;
  929. if ((int)(end - start_ms) < 0)
  930. return 0;
  931. return (int)((end - start_ms) / 1000u);
  932. }
  933. /* ------------------------------------------------------------------ */
  934. /* card drawing */
  935. static void fill_taper(struct ngui *ui, int cx, int y0, int y1,
  936. int half0, int half1, unsigned short color)
  937. {
  938. int t, y;
  939. if (y0 > y1) {
  940. t = y0;
  941. y0 = y1;
  942. y1 = t;
  943. t = half0;
  944. half0 = half1;
  945. half1 = t;
  946. }
  947. if (y1 <= y0)
  948. return;
  949. for (y = y0; y <= y1; y++) {
  950. int half = half0 + (half1 - half0) * (y - y0) / (y1 - y0);
  951. if (half > 0)
  952. ngui_fill_rect(ui,
  953. rect_make(cx - half, y, half * 2 + 1, 1),
  954. color);
  955. }
  956. }
  957. /* The familiar suit shapes. rot=1 flips the glyph vertically for the
  958. * upside-down pip above a card's bottom-right index. */
  959. static void draw_suit(struct ngui *ui, int cx, int cy, int suit,
  960. unsigned short color, int size, int rot)
  961. {
  962. int s = size;
  963. if (s < 7)
  964. s = 7;
  965. if (suit == 0) {
  966. /* spade: point up, rounded lobes below, small stem */
  967. int r = imax(2, s / 5);
  968. fill_circle(ui, cx - s / 5, cy + (rot ? -s / 8 : s / 8),
  969. r, color);
  970. fill_circle(ui, cx + s / 5, cy + (rot ? -s / 8 : s / 8),
  971. r, color);
  972. if (rot)
  973. fill_taper(ui, cx, cy + s / 2, cy - s / 6,
  974. 0, s / 2, color);
  975. else
  976. fill_taper(ui, cx, cy - s / 2, cy + s / 6,
  977. 0, s / 2, color);
  978. if (rot) {
  979. ngui_fill_rect(ui, rect_make(cx - s / 12,
  980. cy - s / 5 - s / 5,
  981. s / 6 + 1, s / 5),
  982. color);
  983. ngui_fill_rect(ui, rect_make(cx - s / 5, cy - s / 2,
  984. s * 2 / 5, 3), color);
  985. } else {
  986. ngui_fill_rect(ui, rect_make(cx - s / 12, cy + s / 5,
  987. s / 6 + 1, s / 5),
  988. color);
  989. ngui_fill_rect(ui, rect_make(cx - s / 5,
  990. cy + s / 2 - 2,
  991. s * 2 / 5, 3), color);
  992. }
  993. } else if (suit == 1) {
  994. /* heart */
  995. int r = imax(2, s / 4);
  996. int lobe_y = cy + (rot ? s / 6 : -s / 6);
  997. fill_circle(ui, cx - s / 5, lobe_y, r, color);
  998. fill_circle(ui, cx + s / 5, lobe_y, r, color);
  999. if (rot)
  1000. fill_taper(ui, cx, cy - s / 2, lobe_y - r / 2,
  1001. 0, s * 45 / 100, color);
  1002. else
  1003. fill_taper(ui, cx, lobe_y + r / 2, cy + s / 2,
  1004. s * 45 / 100, 0, color);
  1005. } else if (suit == 2) {
  1006. /* diamond */
  1007. int hh = s / 2;
  1008. int hw = s * 3 / 8;
  1009. int y;
  1010. for (y = -hh; y <= hh; y++) {
  1011. int dy = y < 0 ? -y : y;
  1012. int half = hw * (hh - dy) / hh;
  1013. if (half > 0)
  1014. ngui_fill_rect(ui,
  1015. rect_make(cx - half, cy + y,
  1016. half * 2 + 1, 1),
  1017. color);
  1018. }
  1019. } else {
  1020. /* club */
  1021. int r = imax(2, s / 5);
  1022. fill_circle(ui, cx, cy + (rot ? s / 4 : -s / 4), r, color);
  1023. fill_circle(ui, cx - s / 4, cy + (rot ? -s / 10 : s / 10),
  1024. r, color);
  1025. fill_circle(ui, cx + s / 4, cy + (rot ? -s / 10 : s / 10),
  1026. r, color);
  1027. if (rot) {
  1028. ngui_fill_rect(ui, rect_make(cx - s / 12,
  1029. cy - s / 5 - s / 5,
  1030. s / 6 + 1, s / 5),
  1031. color);
  1032. ngui_fill_rect(ui, rect_make(cx - s / 5, cy - s / 2,
  1033. s * 2 / 5, 3), color);
  1034. } else {
  1035. ngui_fill_rect(ui, rect_make(cx - s / 12, cy + s / 5,
  1036. s / 6 + 1, s / 5),
  1037. color);
  1038. ngui_fill_rect(ui, rect_make(cx - s / 5,
  1039. cy + s / 2 - 2,
  1040. s * 2 / 5, 3), color);
  1041. }
  1042. }
  1043. }
  1044. static int card_notch(const struct ngui_rect *r)
  1045. {
  1046. int n = r->w / 30;
  1047. if (n < 2)
  1048. n = 2;
  1049. if (n > 4)
  1050. n = 4;
  1051. return n;
  1052. }
  1053. static struct ngui_rect inset_rect(struct ngui_rect r, int n)
  1054. {
  1055. if (r.w > 2 * n && r.h > 2 * n) {
  1056. r.x += n;
  1057. r.y += n;
  1058. r.w -= 2 * n;
  1059. r.h -= 2 * n;
  1060. }
  1061. return r;
  1062. }
  1063. /* White playing cards have a small square cut out of each corner. Filling a
  1064. * plain rectangle would paint over the card underneath, so build the shape
  1065. * from three bands. */
  1066. static void fill_card_shape(struct ngui *ui, struct ngui_rect r,
  1067. unsigned short color, int notch)
  1068. {
  1069. if (notch <= 0 || r.w <= 2 * notch || r.h <= 2 * notch) {
  1070. ngui_fill_rect(ui, r, color);
  1071. return;
  1072. }
  1073. ngui_fill_rect(ui, rect_make(r.x + notch, r.y,
  1074. r.w - 2 * notch, notch), color);
  1075. ngui_fill_rect(ui, rect_make(r.x, r.y + notch,
  1076. r.w, r.h - 2 * notch), color);
  1077. ngui_fill_rect(ui, rect_make(r.x + notch, r.y + r.h - notch,
  1078. r.w - 2 * notch, notch), color);
  1079. }
  1080. struct pip_pos {
  1081. unsigned short x, y;
  1082. };
  1083. static const struct pip_pos pips2[] = {
  1084. { 500, 260 }, { 500, 740 }
  1085. };
  1086. static const struct pip_pos pips3[] = {
  1087. { 500, 260 }, { 500, 500 }, { 500, 740 }
  1088. };
  1089. static const struct pip_pos pips4[] = {
  1090. { 320, 260 }, { 680, 260 }, { 320, 740 }, { 680, 740 }
  1091. };
  1092. static const struct pip_pos pips5[] = {
  1093. { 320, 260 }, { 680, 260 }, { 500, 500 },
  1094. { 320, 740 }, { 680, 740 }
  1095. };
  1096. static const struct pip_pos pips6[] = {
  1097. { 320, 260 }, { 680, 260 }, { 320, 500 }, { 680, 500 },
  1098. { 320, 740 }, { 680, 740 }
  1099. };
  1100. static const struct pip_pos pips7[] = {
  1101. { 320, 260 }, { 680, 260 }, { 500, 380 },
  1102. { 320, 500 }, { 680, 500 }, { 320, 740 }, { 680, 740 }
  1103. };
  1104. static const struct pip_pos pips8[] = {
  1105. { 320, 260 }, { 680, 260 }, { 500, 380 }, { 320, 500 },
  1106. { 680, 500 }, { 500, 620 }, { 320, 740 }, { 680, 740 }
  1107. };
  1108. static const struct pip_pos pips9[] = {
  1109. { 320, 220 }, { 680, 220 }, { 320, 390 }, { 680, 390 },
  1110. { 500, 500 }, { 320, 610 }, { 680, 610 },
  1111. { 320, 780 }, { 680, 780 }
  1112. };
  1113. static const struct pip_pos pips10[] = {
  1114. { 320, 220 }, { 680, 220 }, { 320, 390 }, { 680, 390 },
  1115. { 500, 305 }, { 500, 695 }, { 320, 610 }, { 680, 610 },
  1116. { 320, 780 }, { 680, 780 }
  1117. };
  1118. static void draw_pips(struct ngui *ui, const struct ngui_rect *r, int rank,
  1119. int suit, unsigned short ink)
  1120. {
  1121. const struct pip_pos *p = 0;
  1122. int n = 0, i, ps;
  1123. switch (rank) {
  1124. case 1: p = pips2; n = 2; break;
  1125. case 2: p = pips3; n = 3; break;
  1126. case 3: p = pips4; n = 4; break;
  1127. case 4: p = pips5; n = 5; break;
  1128. case 5: p = pips6; n = 6; break;
  1129. case 6: p = pips7; n = 7; break;
  1130. case 7: p = pips8; n = 8; break;
  1131. case 8: p = pips9; n = 9; break;
  1132. case 9: p = pips10; n = 10; break;
  1133. default: return;
  1134. }
  1135. ps = clampi(r->w * 20 / 100, 7, 20);
  1136. for (i = 0; i < n; i++) {
  1137. int cx = r->x + (int)r->w * p[i].x / 1000;
  1138. int cy = r->y + (int)r->h * p[i].y / 1000;
  1139. draw_suit(ui, cx, cy, suit, ink, ps, 0);
  1140. }
  1141. }
  1142. static void draw_card_corner(struct ngui *ui, const struct ngui_rect *r,
  1143. int suit, unsigned short ink,
  1144. const char *rt, int rl, int top)
  1145. {
  1146. int cs = clampi(r->w / 8, 7, 12);
  1147. int x, y, cx, cy;
  1148. if (top) {
  1149. x = r->x + 3;
  1150. y = r->y + 2;
  1151. ngui_text_styled(ui, x, y, rt, (unsigned int)rl, ink, 1, 1);
  1152. cx = x + (rl * 8) / 2;
  1153. cy = y + 17 + cs / 2;
  1154. if (cy + cs / 2 > r->y + r->h - 2)
  1155. cy = r->y + r->h - 2 - cs / 2;
  1156. draw_suit(ui, cx, cy, suit, ink, cs, 0);
  1157. } else {
  1158. x = r->x + r->w - 3 - rl * 8;
  1159. y = r->y + r->h - 18;
  1160. if (y < r->y + 20)
  1161. y = r->y + 20;
  1162. ngui_text_rot180(ui, x, y, rt, (unsigned int)rl, ink);
  1163. cx = r->x + r->w - 4 - cs / 2;
  1164. cy = y - 2 - cs / 2;
  1165. if (cy - cs / 2 < r->y + 2)
  1166. cy = r->y + 2 + cs / 2;
  1167. draw_suit(ui, cx, cy, suit, ink, cs, 1);
  1168. }
  1169. }
  1170. static const char *const court_king[48] = {
  1171. "........YYY....YYY....YYY.......",
  1172. "........YYY....YYY....YYY.......",
  1173. "........YYY....YYY....YYY.......",
  1174. "........#################.......",
  1175. "........#YYRRYYYRRYYYRRY#.......",
  1176. "........#################.......",
  1177. ".........HHHHHHHHHHHHHH.........",
  1178. ".........HHHHHHHHHHHHHH.........",
  1179. "........HHHHHHHHHHHHHHHH........",
  1180. "........HHSSSSSSSSSSSSHH........",
  1181. "........HHSSSSSSSSSSSSHH........",
  1182. "........HHSSSSSSSSSSSSHH........",
  1183. "........HHSS##SSSS##SSHH........",
  1184. "........HHSS##SSSS##SSHH........",
  1185. "........HHSSSSS##SSSSSHH........",
  1186. "........HHSSSSS##SSSSSHH........",
  1187. "........HHSSSSS##SSSSSHH........",
  1188. "........HHSSggggSggggSHH........",
  1189. "........HHSSggggRggggSHH........",
  1190. "........Hggggggggggggggg........",
  1191. "........Hggggggggggggggg........",
  1192. "........Hggggggggggggggg........",
  1193. ".........ggggggggggggggg........",
  1194. ".........ggggggggggggggg........",
  1195. "..........############..........",
  1196. "..........#WWWWWWWWWW#..........",
  1197. "..........#WWWWWWWWWW#..........",
  1198. "..........############..........",
  1199. "........#RRRRRRRRRRRRRRR#.......",
  1200. "........#RYRRRRRRRRRRRYR#.......",
  1201. "....#####YRRRRRRRRRRRRRY#####...",
  1202. "....#RRR#YRRRRRRRRRRRRRY#RRR#...",
  1203. "....#RRR#RRRRRRRRRRRRRRR#RRR#...",
  1204. "....#RRR#RRRRRRRRRRRRRRR#RRR#...",
  1205. "....#RRR#RRRRRRRRRRRRRRR#RRR#...",
  1206. "....#YYYYYRRRRRRRRRRRRRYYYYY#...",
  1207. "...##YYYYYRRRRRRRRRRRRRYYYYY##..",
  1208. "...#SS#RRRRRRRRRRRRRRRRRRR#SS#..",
  1209. "...#SS#R#YYYYYRRRRYYYYY#RR#SS#..",
  1210. "..##SS#R#YYYYYRRRRYYYYY#RR#SS##.",
  1211. ".#R####R################RR####R#",
  1212. ".#RYRRRRRRRRRRRRRRRRRRRRRRRRRYR#",
  1213. "#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
  1214. "#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
  1215. "RRRRRR####################RRRRRR",
  1216. "......#RRRRRRRRRRRRRRRRRR#......",
  1217. "......####################......",
  1218. "................................",
  1219. };
  1220. static const char *const court_queen[48] = {
  1221. "..........YWY..YWY..YWY.........",
  1222. "..........YYY..YYY..YYY.........",
  1223. "..........YYY..YYY..YYY.........",
  1224. ".........#YYY#RRRR##YYY#........",
  1225. ".........#YYYYRRRRYYYYY#........",
  1226. ".........###############........",
  1227. ".........HHHHHHHHHHHHHH.........",
  1228. ".........HHHHHHHHHHHHHH.........",
  1229. ".......HHHHHHHHHHHHHHHHHH.......",
  1230. ".......HHHSSSSSSSSSSSSHHH.......",
  1231. ".......HHHSSSSSSSSSSSSHHH.......",
  1232. ".......HHHSSSSSSSSSSSSHHH.......",
  1233. ".......HHHSS##SSSS##SSHHH.......",
  1234. ".......HHHSS##SSSS##SSHHH.......",
  1235. ".......HHHSSSSS##SSSSSHHH.......",
  1236. ".......HHHSSSSS##SSSSSHHH.......",
  1237. ".......HHHSSSSS##SSSSSHHH.......",
  1238. ".......HHHSSSSSSSSSSSSHHH.......",
  1239. ".......HHHSSSRRRRRRSSSHHH.......",
  1240. ".......HHHSSSRRRRRRSSSHHH.......",
  1241. ".......HHHSSSSSSSSSSSSHHH.......",
  1242. ".......HHH############HHH.......",
  1243. ".......HHH..SSSSSSSS..HHH.......",
  1244. ".......HHH..SSSSSSSS..HHH.......",
  1245. ".......HHH############HHH.......",
  1246. ".......HHH#WWWWWWWWWW#HHH.......",
  1247. ".......HHH#WWWWWWWWWW#HHH.......",
  1248. ".......HHH############HHH.......",
  1249. "........#RRRRRRRRRRRRRRR#.......",
  1250. "........#RYRRRRRRRRRRRYR#.......",
  1251. "....#####YRRRRRRRRRRRRRY#####...",
  1252. "....#RRR#YRRRRRRRRRRRRRY#RRR#...",
  1253. "....#RRR#RRRRRRRRRRRRRRR#RRR#...",
  1254. "....#RRR#RRRRRRRRRRRRRRR#RRR#...",
  1255. "....#RRR#RRRRRRRRRRRRRRR#RRR#...",
  1256. "....#YYYYYRRRRRRRRRRRRRYYYYY#...",
  1257. "...##YYYYYRRRRRRRRRRRRRYYYYY##..",
  1258. "...#SS#RRRRRRRRRRRRRRRRRRR#SS#..",
  1259. "...#SS#R#YYYYYRRRRYYYYY#RR#SS#..",
  1260. "..##SS#R#YYYYYRRRRYYYYY#RR#SS##.",
  1261. ".#R####R################RR####R#",
  1262. ".#RYRRRRRRRRRRRRRRRRRRRRRRRRRYR#",
  1263. "#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
  1264. "#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
  1265. "RRRRRR####################RRRRRR",
  1266. "......#RRRRRRRRRRRRRRRRRR#......",
  1267. "......####################......",
  1268. "................................",
  1269. };
  1270. static const char *const court_jack[48] = {
  1271. ".......................RRY......",
  1272. ".......................RRRY.....",
  1273. "........###############RRR......",
  1274. "........#RRRRRRRRRRRRRRRRR......",
  1275. "........#RRRRRRRRRRRRRRRRR......",
  1276. "........#RRRRRRRRRRRRRRR#.......",
  1277. "........#RRRRRRRRRRRRRRR#.......",
  1278. "........YYYYYYYYYYYYYYYYY.......",
  1279. "........HHHHHHHHHHHHHHHH........",
  1280. "........HHSSSSSSSSSSSSHH........",
  1281. "........HHSSSSSSSSSSSSHH........",
  1282. "........HHSSSSSSSSSSSSHH........",
  1283. "........HHSS##SSSS##SSHH........",
  1284. "........HHSS##SSSS##SSHH........",
  1285. "........HHSSSSS##SSSSSHH........",
  1286. "........HHSSSSS##SSSSSHH........",
  1287. "........HHSSSSS##SSSSSHH........",
  1288. "........HHSSSSSSSSSSSSHH........",
  1289. "........HHSSSRRRRRRSSSHH........",
  1290. "........HHSSSRRRRRRSSSHH........",
  1291. "........HHSSSSSSSSSSSSHH........",
  1292. "........HH############HH........",
  1293. "............SSSSSSSS............",
  1294. "............SSSSSSSS............",
  1295. "..........############..........",
  1296. "..........#WWWWWWWWWW#..........",
  1297. "..........#WWWWYYWWWW#..........",
  1298. "..........#####YY#####..........",
  1299. "........#RRRRRRRRRRRRRRR#.......",
  1300. "........#RYRRRRRRRRRRRYR#.......",
  1301. "....#####YRRRRRRRRRRRRRY#####...",
  1302. "....#RRR#YRRRRRRRRRRRRRY#RRR#...",
  1303. "....#RRR#RRRRRRRRRRRRRRR#RRR#...",
  1304. "....#RRR#RRRRRRRRRRRRRRR#RRR#...",
  1305. "....#RRR#RRRRRRRRRRRRRRR#RRR#...",
  1306. "....#YYYYYRRRRRRRRRRRRRYYYYY#...",
  1307. "...##YYYYYRRRRRRRRRRRRRYYYYY##..",
  1308. "...#SS#RRRRRRRRRRRRRRRRRRR#SS#..",
  1309. "...#SS#R#YYYYYRRRRYYYYY#RR#SS#..",
  1310. "..##SS#R#YYYYYRRRRYYYYY#RR#SS##.",
  1311. ".#R####R################RR####R#",
  1312. ".#RYRRRRRRRRRRRRRRRRRRRRRRRRRYR#",
  1313. "#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
  1314. "#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
  1315. "RRRRRR####################RRRRRR",
  1316. "......#RRRRRRRRRRRRRRRRRR#......",
  1317. "......####################......",
  1318. "................................",
  1319. };
  1320. static const char *const *court_rows(int rank)
  1321. {
  1322. if (rank == 12)
  1323. return court_king;
  1324. if (rank == 11)
  1325. return court_queen;
  1326. if (rank == 10)
  1327. return court_jack;
  1328. return 0;
  1329. }
  1330. static unsigned short court_color(char c)
  1331. {
  1332. switch (c) {
  1333. case '#': return C_BLACK;
  1334. case 'W': return C_CARD;
  1335. case 'S': return C_SKIN;
  1336. case 'H': return C_HAIR;
  1337. case 'Y': return C_GOLD;
  1338. case 'R': return C_RED;
  1339. case 'g': return NGUI_RGB(184, 184, 184);
  1340. default: return 0;
  1341. }
  1342. }
  1343. /* Blit the 32x48 court portrait with one fill per same-colour run. */
  1344. static void draw_court_bitmap(struct ngui *ui, int px, int py, int pw, int ph,
  1345. int rank)
  1346. {
  1347. const char *const *rows = court_rows(rank);
  1348. int x, y;
  1349. if (!rows)
  1350. return;
  1351. for (y = 0; y < 48; y++) {
  1352. int y0 = py + y * ph / 48;
  1353. int y1 = py + (y + 1) * ph / 48;
  1354. for (x = 0; x < 32; ) {
  1355. char ch = rows[y][x];
  1356. int x2, x0, x1;
  1357. unsigned short color;
  1358. if (ch == '.') {
  1359. x++;
  1360. continue;
  1361. }
  1362. x2 = x;
  1363. while (x2 + 1 < 32 && rows[y][x2 + 1] == ch)
  1364. x2++;
  1365. color = court_color(ch);
  1366. if (color) {
  1367. x0 = px + x * pw / 32;
  1368. x1 = px + (x2 + 1) * pw / 32;
  1369. ngui_fill_rect(ui, rect_make(x0, y0,
  1370. x1 - x0, y1 - y0),
  1371. color);
  1372. }
  1373. x = x2 + 1;
  1374. }
  1375. }
  1376. }
  1377. static void draw_court_card(struct ngui *ui, const struct ngui_rect *r,
  1378. int rank, int suit, unsigned short ink)
  1379. {
  1380. int px = r->x + r->w * 19 / 100;
  1381. int py = r->y + r->h * 11 / 100;
  1382. int pw = r->w * 62 / 100;
  1383. int ph = r->h * 78 / 100;
  1384. if (pw < 24 || ph < 40) {
  1385. int ss = imin(r->w * 45 / 100, r->h * 30 / 100);
  1386. ss = clampi(ss, 9, 40);
  1387. draw_suit(ui, r->x + r->w / 2, r->y + r->h / 2 + 2,
  1388. suit, ink, ss, 0);
  1389. return;
  1390. }
  1391. ngui_fill_rect(ui, rect_make(px, py, pw, ph), C_CREAM);
  1392. draw_court_bitmap(ui, px, py, pw, ph, rank);
  1393. ngui_frame(ui, rect_make(px, py, pw, ph), C_BLACK);
  1394. }
  1395. static void draw_card_face_content(struct ngui *ui, struct ngui_rect r, int id)
  1396. {
  1397. int rank = card_rank(id);
  1398. int suit = card_suit(id);
  1399. unsigned short ink = suit_is_red(suit) ? C_RED : C_BLACK;
  1400. const char *rt = rank_text(rank);
  1401. int rl = (int)text_len(rt);
  1402. int notch = card_notch(&r);
  1403. fill_card_shape(ui, r, C_CARD_EDGE, notch);
  1404. fill_card_shape(ui, inset_rect(r, 1), C_CARD, imax(0, notch - 1));
  1405. ngui_push_clip(ui, r);
  1406. if (rank >= 10) {
  1407. draw_court_card(ui, &r, rank, suit, ink);
  1408. } else if (rank == 0) {
  1409. int ss = imin(r.w * 45 / 100, r.h * 30 / 100);
  1410. ss = clampi(ss, 9, 40);
  1411. draw_suit(ui, r.x + r.w / 2, r.y + r.h / 2 + 2,
  1412. suit, ink, ss, 0);
  1413. } else if (r.w < 30) {
  1414. int ss = imin(r.w * 42 / 100, r.h * 28 / 100);
  1415. ss = clampi(ss, 8, 30);
  1416. draw_suit(ui, r.x + r.w / 2, r.y + r.h / 2 + 2,
  1417. suit, ink, ss, 0);
  1418. } else {
  1419. draw_pips(ui, &r, rank, suit, ink);
  1420. }
  1421. draw_card_corner(ui, &r, suit, ink, rt, rl, 1);
  1422. draw_card_corner(ui, &r, suit, ink, rt, rl, 0);
  1423. ngui_pop_clip(ui);
  1424. }
  1425. /* The Windows Solitaire back is a little castle on a night hill. */
  1426. static void draw_card_back_content(struct ngui *ui, struct ngui_rect r)
  1427. {
  1428. struct ngui_rect inner;
  1429. unsigned short sky, sky2, moon, hill, tower, tower_dark, lit;
  1430. int notch = card_notch(&r);
  1431. int mx, my, mr, base, keep_w, keep_h, kx, ky, i, tw, th;
  1432. if (deck_style == 1) {
  1433. sky = NGUI_RGB(96, 0, 0);
  1434. sky2 = NGUI_RGB(128, 24, 24);
  1435. moon = NGUI_RGB(255, 208, 128);
  1436. hill = NGUI_RGB(48, 0, 0);
  1437. tower = NGUI_RGB(224, 150, 128);
  1438. tower_dark = NGUI_RGB(104, 40, 40);
  1439. lit = NGUI_RGB(255, 232, 144);
  1440. } else {
  1441. sky = NGUI_RGB(0, 0, 96);
  1442. sky2 = NGUI_RGB(0, 28, 128);
  1443. moon = NGUI_RGB(236, 236, 168);
  1444. hill = NGUI_RGB(0, 0, 48);
  1445. tower = NGUI_RGB(184, 196, 220);
  1446. tower_dark = NGUI_RGB(56, 68, 112);
  1447. lit = NGUI_RGB(255, 232, 144);
  1448. }
  1449. fill_card_shape(ui, r, C_CARD_EDGE, notch);
  1450. fill_card_shape(ui, inset_rect(r, 1), C_CARD, imax(0, notch - 1));
  1451. inner = inset_rect(r, 3);
  1452. if (inner.w < 6 || inner.h < 6)
  1453. return;
  1454. ngui_fill_rect(ui, inner, sky);
  1455. ngui_fill_rect(ui, rect_make(inner.x, inner.y + inner.h * 58 / 100,
  1456. inner.w, inner.h * 42 / 100), sky2);
  1457. mx = inner.x + inner.w * 74 / 100;
  1458. my = inner.y + inner.h * 22 / 100;
  1459. mr = imax(2, inner.w / 10);
  1460. fill_circle(ui, mx, my, mr, moon);
  1461. fill_circle(ui, mx + mr / 2, my - mr / 3, mr * 4 / 5, sky);
  1462. {
  1463. static const unsigned char star_x[] = { 18, 34, 48, 62, 24, 84 };
  1464. static const unsigned char star_y[] = { 12, 30, 16, 34, 48, 40 };
  1465. for (i = 0; i < 6; i++) {
  1466. int sx = inner.x + inner.w * star_x[i] / 100;
  1467. int sy = inner.y + inner.h * star_y[i] / 100;
  1468. ngui_fill_rect(ui, rect_make(sx, sy, 1, 1), moon);
  1469. }
  1470. }
  1471. base = inner.y + inner.h * 76 / 100;
  1472. ngui_fill_rect(ui, rect_make(inner.x, base, inner.w,
  1473. inner.y + inner.h - base), hill);
  1474. if (inner.w < 26 || inner.h < 34) {
  1475. ngui_fill_rect(ui, rect_make(inner.x + inner.w / 2 - 2,
  1476. base - inner.h / 4, 5,
  1477. inner.h / 4), tower);
  1478. ngui_frame(ui, inner, tower_dark);
  1479. return;
  1480. }
  1481. /* central keep */
  1482. keep_w = inner.w * 34 / 100;
  1483. keep_h = inner.h * 34 / 100;
  1484. kx = inner.x + (inner.w - keep_w) / 2;
  1485. ky = base - keep_h;
  1486. ngui_fill_rect(ui, rect_make(kx, ky, keep_w, keep_h), tower);
  1487. ngui_frame(ui, rect_make(kx, ky, keep_w, keep_h), tower_dark);
  1488. for (i = 0; i < 3; i++)
  1489. ngui_fill_rect(ui, rect_make(kx + i * keep_w / 3 + 1,
  1490. ky - 3, keep_w / 5, 3), tower);
  1491. ngui_fill_rect(ui, rect_make(kx + keep_w / 2 - 4,
  1492. ky + keep_h * 35 / 100, 3, 5), lit);
  1493. ngui_fill_rect(ui, rect_make(kx + keep_w / 2 + 2,
  1494. ky + keep_h * 35 / 100, 3, 5), lit);
  1495. ngui_fill_rect(ui, rect_make(kx + keep_w / 2 - 4, base - 12,
  1496. 9, 12), tower_dark);
  1497. /* side towers */
  1498. tw = inner.w * 18 / 100;
  1499. th = inner.h * 26 / 100;
  1500. for (i = 0; i < 2; i++) {
  1501. int tx = i ? inner.x + inner.w - tw - inner.w * 12 / 100 :
  1502. inner.x + inner.w * 12 / 100;
  1503. ngui_fill_rect(ui, rect_make(tx, base - th, tw, th), tower);
  1504. ngui_frame(ui, rect_make(tx, base - th, tw, th), tower_dark);
  1505. ngui_fill_rect(ui, rect_make(tx + tw / 2 - 1,
  1506. base - th + 4, 3, 4), lit);
  1507. ngui_fill_rect(ui, rect_make(tx + tw / 4, base - th - 3,
  1508. tw / 2, 3), tower);
  1509. }
  1510. /* flag */
  1511. ngui_line(ui, kx + keep_w / 2, ky - 10, kx + keep_w / 2, ky - 3,
  1512. tower_dark);
  1513. ngui_fill_rect(ui, rect_make(kx + keep_w / 2 + 1, ky - 10,
  1514. 5, 3), C_RED);
  1515. ngui_frame(ui, inner, tower_dark);
  1516. }
  1517. static void draw_card(struct ngui *ui, struct ngui_rect r, int id, int up)
  1518. {
  1519. int notch = card_notch(&r);
  1520. fill_card_shape(ui, rect_make(r.x + 2, r.y + 2, r.w, r.h),
  1521. C_SHADOW, notch);
  1522. if (up)
  1523. draw_card_face_content(ui, r, id);
  1524. else
  1525. draw_card_back_content(ui, r);
  1526. }
  1527. static void draw_empty_slot(struct ngui *ui, struct ngui_rect r)
  1528. {
  1529. ngui_fill_rect(ui, r, C_SLOT_FILL);
  1530. ngui_frame(ui, r, C_SLOT_LINE);
  1531. }
  1532. static void draw_recycle_icon(struct ngui *ui, struct ngui_rect r)
  1533. {
  1534. int cx = r.x + r.w / 2;
  1535. int cy = r.y + r.h / 2;
  1536. int rad = imin(r.w, r.h) / 5;
  1537. int q = rad * 7 / 10;
  1538. if (rad < 5)
  1539. return;
  1540. ngui_line(ui, cx, cy - rad, cx + q, cy - q, C_SLOT_ICON);
  1541. ngui_line(ui, cx + q, cy - q, cx + rad, cy, C_SLOT_ICON);
  1542. ngui_line(ui, cx + rad, cy, cx + q, cy + q, C_SLOT_ICON);
  1543. ngui_line(ui, cx + q, cy + q, cx, cy + rad, C_SLOT_ICON);
  1544. ngui_line(ui, cx, cy + rad, cx - q, cy + q, C_SLOT_ICON);
  1545. ngui_line(ui, cx - q, cy + q, cx - rad, cy, C_SLOT_ICON);
  1546. ngui_line(ui, cx - rad, cy, cx - q, cy - q, C_SLOT_ICON);
  1547. ngui_line(ui, cx - q, cy - q, cx - rad / 2, cy - rad, C_SLOT_ICON);
  1548. ngui_line(ui, cx, cy - rad, cx + rad / 2, cy - rad, C_SLOT_ICON);
  1549. ngui_line(ui, cx, cy - rad, cx, cy - rad / 2, C_SLOT_ICON);
  1550. }
  1551. /* ------------------------------------------------------------------ */
  1552. /* table drawing */
  1553. static void draw_stock(struct ngui *ui, const struct sol_layout *l)
  1554. {
  1555. struct ngui_rect r = stock_rect(l);
  1556. if (stock.n > 0) {
  1557. draw_card(ui, r, stock.id[stock.n - 1], 0);
  1558. } else {
  1559. draw_empty_slot(ui, r);
  1560. if (waste.n > 0)
  1561. draw_recycle_icon(ui, r);
  1562. }
  1563. }
  1564. static void draw_waste(struct ngui *ui, const struct sol_layout *l)
  1565. {
  1566. struct ngui_rect r = waste_slot_rect(l);
  1567. int fan = waste_fan(l);
  1568. int top = waste.n;
  1569. int start, i;
  1570. if (drag_active && drag_sel.kind == SRC_WASTE)
  1571. top--;
  1572. if (top <= 0) {
  1573. draw_empty_slot(ui, r);
  1574. return;
  1575. }
  1576. if (draw_three)
  1577. start = top > 3 ? top - 3 : 0;
  1578. else
  1579. start = top - 1;
  1580. for (i = start; i < top; i++) {
  1581. struct ngui_rect cr = r;
  1582. cr.x += (i - start) * fan;
  1583. draw_card(ui, cr, waste.id[i], 1);
  1584. }
  1585. }
  1586. static void draw_foundations(struct ngui *ui, const struct sol_layout *l)
  1587. {
  1588. int f;
  1589. for (f = 0; f < FOUNDATION_PILES; f++) {
  1590. struct ngui_rect r = foundation_rect(l, f);
  1591. int top = foundation[f].n;
  1592. if (drag_active && drag_sel.kind == SRC_FOUNDATION &&
  1593. drag_sel.pile == f)
  1594. top--;
  1595. if (top > 0)
  1596. draw_card(ui, r, foundation[f].id[top - 1], 1);
  1597. else
  1598. draw_empty_slot(ui, r);
  1599. }
  1600. }
  1601. static void draw_tableau(struct ngui *ui, const struct sol_layout *l)
  1602. {
  1603. int p, i;
  1604. for (p = 0; p < TABLEAU_PILES; p++) {
  1605. struct card_pile *pile = &tableau[p];
  1606. int n = pile->n;
  1607. if (drag_active && drag_sel.kind == SRC_TABLEAU &&
  1608. drag_sel.pile == p)
  1609. n = drag_sel.index;
  1610. if (n <= 0) {
  1611. draw_empty_slot(ui, tableau_slot_rect(l, p));
  1612. continue;
  1613. }
  1614. for (i = 0; i < n; i++) {
  1615. struct ngui_rect r;
  1616. r = rect_make(l->col_x[p], pile_y(pile, l, i),
  1617. l->cw, l->ch);
  1618. draw_card(ui, r, pile->id[i], pile->up[i]);
  1619. }
  1620. }
  1621. }
  1622. static void draw_drop_highlight(struct ngui *ui, const struct sol_layout *l)
  1623. {
  1624. const struct sel *s = 0;
  1625. struct target t;
  1626. struct ngui_rect r;
  1627. if (drag_active)
  1628. s = &drag_sel;
  1629. else if (select_sel.kind != SRC_NONE)
  1630. s = &select_sel;
  1631. if (!s)
  1632. return;
  1633. t = drop_target_at(l, ui->mouse_x, ui->mouse_y);
  1634. if (!can_drop(s, &t))
  1635. return;
  1636. r = target_rect(l, &t);
  1637. if (r.w > 0 && r.h > 0)
  1638. frame2(ui, r, ui->theme.accent);
  1639. }
  1640. static void draw_selection_outline(struct ngui *ui, const struct sol_layout *l)
  1641. {
  1642. struct ngui_rect r;
  1643. if (drag_active || select_sel.kind == SRC_NONE)
  1644. return;
  1645. if (select_sel.kind == SRC_TABLEAU) {
  1646. struct card_pile *p = &tableau[select_sel.pile];
  1647. int y0 = pile_y(p, l, select_sel.index);
  1648. int y1 = pile_y(p, l, p->n - 1) + l->ch;
  1649. r = rect_make(l->col_x[select_sel.pile], y0, l->cw, y1 - y0);
  1650. } else {
  1651. r = sel_rect(l, &select_sel);
  1652. }
  1653. frame2(ui, r, ui->theme.accent);
  1654. }
  1655. static void draw_drag_cards(struct ngui *ui, const struct sol_layout *l)
  1656. {
  1657. struct card_pile *p;
  1658. int count, i, step, x, y;
  1659. if (!drag_active || drag_sel.kind == SRC_NONE)
  1660. return;
  1661. count = sel_count(&drag_sel);
  1662. if (count <= 0)
  1663. return;
  1664. x = ui->mouse_x - drag_dx;
  1665. y = ui->mouse_y - drag_dy;
  1666. step = imax(8, l->ch / 5);
  1667. for (i = 0; i < count; i++) {
  1668. struct ngui_rect r = rect_make(x, y + i * step, l->cw, l->ch);
  1669. if (drag_sel.kind == SRC_TABLEAU) {
  1670. p = &tableau[drag_sel.pile];
  1671. draw_card(ui, r, p->id[drag_sel.index + i], 1);
  1672. } else if (drag_sel.kind == SRC_WASTE) {
  1673. draw_card(ui, r, waste.id[waste.n - 1], 1);
  1674. } else {
  1675. p = &foundation[drag_sel.pile];
  1676. draw_card(ui, r, p->id[p->n - 1], 1);
  1677. }
  1678. }
  1679. }
  1680. static void draw_bounce_cards(struct ngui *ui, const struct sol_layout *l)
  1681. {
  1682. int i;
  1683. if (!bounce_show)
  1684. return;
  1685. for (i = 0; i < BOUNCE_MAX; i++) {
  1686. struct bounce_card *b = &bounce_cards[i];
  1687. if (!b->active && !b->settled)
  1688. continue;
  1689. draw_card(ui, rect_make(b->x, b->y, l->cw, l->ch), b->id, 1);
  1690. }
  1691. }
  1692. /* ------------------------------------------------------------------ */
  1693. /* menu, status and help */
  1694. static int menu_entries(int which, const struct menu_entry **out)
  1695. {
  1696. if (which == MENU_GAME) {
  1697. *out = game_menu;
  1698. return (int)(sizeof(game_menu) / sizeof(game_menu[0]));
  1699. }
  1700. *out = help_menu;
  1701. return (int)(sizeof(help_menu) / sizeof(help_menu[0]));
  1702. }
  1703. static int menu_entry_h(struct ngui *ui, const struct menu_entry *e)
  1704. {
  1705. int lh = ngui_text_line_height(ui, ui->font_px, 0);
  1706. return e->label ? lh + 10 : 9;
  1707. }
  1708. static int menu_dropdown_h(struct ngui *ui, int which)
  1709. {
  1710. const struct menu_entry *e;
  1711. int n, i, h = 10;
  1712. n = menu_entries(which, &e);
  1713. for (i = 0; i < n; i++)
  1714. h += menu_entry_h(ui, &e[i]);
  1715. return h;
  1716. }
  1717. /* Geometry of the Game/Help buttons in the ngui toolbar. */
  1718. static void menu_button_rect(struct ngui *ui, int which, struct ngui_rect *out)
  1719. {
  1720. int gw = (int)ngui_text_width_ui(ui, "Game") + 24;
  1721. int hw = (int)ngui_text_width_ui(ui, "Help") + 24;
  1722. int y = (MENU_H - 24) / 2;
  1723. if (gw < 56)
  1724. gw = 56;
  1725. if (hw < 56)
  1726. hw = 56;
  1727. if (which == MENU_GAME)
  1728. *out = rect_make(8, y, gw, 24);
  1729. else
  1730. *out = rect_make(8 + gw + 6, y, hw, 24);
  1731. }
  1732. static int menu_w(struct ngui *ui, int which)
  1733. {
  1734. const struct menu_entry *e;
  1735. int n, i, w = 150;
  1736. n = menu_entries(which, &e);
  1737. for (i = 0; i < n; i++) {
  1738. int lw;
  1739. if (!e[i].label)
  1740. continue;
  1741. lw = (int)ngui_text_width_ui(ui, e[i].label) + 40;
  1742. if (e[i].shortcut)
  1743. lw += (int)ngui_text_width_ui(ui, e[i].shortcut) + 24;
  1744. if (lw > w)
  1745. w = lw;
  1746. }
  1747. if (w > ui->w - 12)
  1748. w = ui->w - 12;
  1749. if (w < 120)
  1750. w = 120;
  1751. return w;
  1752. }
  1753. /* The open menu's panel rectangle, clamped into the window. */
  1754. static int dropdown_rect(struct ngui *ui, struct ngui_rect *out)
  1755. {
  1756. struct ngui_rect a;
  1757. int w, h, x;
  1758. if (menu_open == MENU_NONE)
  1759. return 0;
  1760. menu_button_rect(ui, menu_open, &a);
  1761. w = menu_w(ui, menu_open);
  1762. h = menu_dropdown_h(ui, menu_open);
  1763. x = a.x;
  1764. if (x + w > ui->w - 6)
  1765. x = ui->w - 6 - w;
  1766. if (x < 6)
  1767. x = 6;
  1768. *out = rect_make(x, MENU_H + 2, w, h);
  1769. return 1;
  1770. }
  1771. static int action_checked(int action)
  1772. {
  1773. switch (action) {
  1774. case ACT_DRAW1:
  1775. return !draw_three;
  1776. case ACT_DRAW3:
  1777. return draw_three;
  1778. case ACT_BLUE:
  1779. return deck_style == 0;
  1780. case ACT_RED:
  1781. return deck_style == 1;
  1782. default:
  1783. return 0;
  1784. }
  1785. }
  1786. static int menu_bar_button(struct ngui *ui, unsigned int id,
  1787. struct ngui_rect r, const char *label, int active)
  1788. {
  1789. unsigned short ob = ui->theme.button;
  1790. unsigned short oh = ui->theme.button_hot;
  1791. unsigned short od = ui->theme.button_down;
  1792. int fired;
  1793. if (active) {
  1794. ui->theme.button = ui->theme.selection;
  1795. ui->theme.button_hot = ui->theme.selection;
  1796. ui->theme.button_down = ui->theme.selection;
  1797. } else {
  1798. ui->theme.button = ui->theme.panel;
  1799. ui->theme.button_hot = ui->theme.panel_alt;
  1800. ui->theme.button_down = ui->theme.selection;
  1801. }
  1802. fired = ngui_button(ui, id, r, label);
  1803. ui->theme.button = ob;
  1804. ui->theme.button_hot = oh;
  1805. ui->theme.button_down = od;
  1806. return fired;
  1807. }
  1808. static void draw_menu_bar(struct ngui *ui, const struct sol_layout *l)
  1809. {
  1810. static const char *const hint = "N new game H help Esc quit";
  1811. struct ngui_rect gr, hr;
  1812. int hw, hy;
  1813. (void)l;
  1814. ngui_fill_rect(ui, rect_make(0, 0, ui->w, MENU_H), ui->theme.panel);
  1815. ngui_fill_rect(ui, rect_make(0, MENU_H - 1, ui->w, 1),
  1816. ui->theme.border);
  1817. menu_button_rect(ui, MENU_GAME, &gr);
  1818. menu_button_rect(ui, MENU_HELP, &hr);
  1819. if (menu_bar_button(ui, ID_MENU_GAME, gr, "Game",
  1820. menu_open == MENU_GAME)) {
  1821. menu_open = (menu_open == MENU_GAME) ? MENU_NONE : MENU_GAME;
  1822. ngui_invalidate(ui);
  1823. }
  1824. if (menu_bar_button(ui, ID_MENU_HELP, hr, "Help",
  1825. menu_open == MENU_HELP)) {
  1826. menu_open = (menu_open == MENU_HELP) ? MENU_NONE : MENU_HELP;
  1827. ngui_invalidate(ui);
  1828. }
  1829. hw = (int)ngui_text_width_ui(ui, hint);
  1830. hy = (MENU_H - ngui_text_line_height(ui, ui->font_px, 0)) / 2;
  1831. if (hy < 0)
  1832. hy = 0;
  1833. if (hw + 24 < ui->w - (hr.x + hr.w))
  1834. ngui_text(ui, ui->w - 12 - hw, hy, hint, ui->theme.text_dim);
  1835. }
  1836. static void draw_dropdown(struct ngui *ui)
  1837. {
  1838. const struct menu_entry *e;
  1839. struct ngui_rect r;
  1840. int n, i, ey;
  1841. if (!dropdown_rect(ui, &r))
  1842. return;
  1843. n = menu_entries(menu_open, &e);
  1844. ngui_fill_round_rect(ui, rect_make(r.x + 2, r.y + 4, r.w, r.h),
  1845. 8, 8, 8, 8, ui->theme.bg);
  1846. ngui_fill_round_rect(ui, r, 8, 8, 8, 8, ui->theme.panel);
  1847. ngui_frame_round(ui, r, 8, 8, 8, 8, 1, ui->theme.border);
  1848. ey = r.y + 5;
  1849. for (i = 0; i < n; i++) {
  1850. int eh = menu_entry_h(ui, &e[i]);
  1851. if (!e[i].label) {
  1852. ngui_fill_rect(ui, rect_make(r.x + 12, ey + eh / 2,
  1853. r.w - 24, 1), ui->theme.border);
  1854. ey += eh;
  1855. continue;
  1856. }
  1857. if (ngui_menu_item(ui, ID_MENU_ITEM + (unsigned int)e[i].action,
  1858. rect_make(r.x + 5, ey, r.w - 10, eh),
  1859. e[i].label, e[i].shortcut,
  1860. action_checked(e[i].action))) {
  1861. pending_action = e[i].action;
  1862. menu_open = MENU_NONE;
  1863. ngui_invalidate(ui);
  1864. }
  1865. ey += eh;
  1866. }
  1867. }
  1868. static void fmt_time(char *out, int sec)
  1869. {
  1870. int m, s, n = 0;
  1871. if (sec < 0)
  1872. sec = 0;
  1873. m = sec / 60;
  1874. s = sec % 60;
  1875. if (m > 0) {
  1876. fmt_int(out, m);
  1877. while (out[n])
  1878. n++;
  1879. out[n++] = ':';
  1880. out[n++] = (char)('0' + s / 10);
  1881. out[n++] = (char)('0' + s % 10);
  1882. out[n] = '\0';
  1883. } else {
  1884. fmt_int(out, s);
  1885. }
  1886. }
  1887. static void draw_status(struct ngui *ui, const struct sol_layout *l)
  1888. {
  1889. char b[24], t[24];
  1890. int lh = ngui_text_line_height(ui, ui->font_px, 0);
  1891. int y = l->status_y + (STATUS_H - lh) / 2;
  1892. int x = 12, lw, tw, rx;
  1893. unsigned short dim = ui->theme.text_dim;
  1894. ngui_fill_rect(ui, rect_make(0, l->status_y, ui->w,
  1895. ui->h - l->status_y), ui->theme.panel);
  1896. ngui_fill_rect(ui, rect_make(0, l->status_y, ui->w, 1),
  1897. ui->theme.border);
  1898. if (y < l->status_y + 1)
  1899. y = l->status_y + 1;
  1900. ngui_text(ui, x, y, "Score", dim);
  1901. lw = (int)ngui_text_width_ui(ui, "Score") + 6;
  1902. fmt_int(b, score);
  1903. ngui_text(ui, x + lw, y, b, ui->theme.text);
  1904. x += lw + (int)ngui_text_width_ui(ui, b);
  1905. fmt_time(t, elapsed_seconds(u_ticks_ms()));
  1906. tw = (int)ngui_text_width_ui(ui, "Time") + 6 +
  1907. (int)ngui_text_width_ui(ui, t);
  1908. rx = ui->w - 12 - tw;
  1909. if (rx > x + 16) {
  1910. ngui_text(ui, rx, y, "Time", dim);
  1911. ngui_text(ui, rx + (int)ngui_text_width_ui(ui, "Time") + 6,
  1912. y, t, ui->theme.text);
  1913. }
  1914. if (won) {
  1915. const char *msg = "You win!";
  1916. int mw = (int)ngui_text_width_ui(ui, msg);
  1917. int mx = (ui->w - mw) / 2;
  1918. if (mx > x + 16 && mx + mw < rx - 12)
  1919. ngui_text(ui, mx, y, msg, ui->theme.warning);
  1920. }
  1921. }
  1922. static int wrap_count(struct ngui *ui, int w, const char *s)
  1923. {
  1924. int lines = 1, line_w = 0;
  1925. while (s && *s) {
  1926. const char *word = s;
  1927. int ww;
  1928. while (*s && *s != ' ')
  1929. s++;
  1930. ww = (int)ngui_text_width_px(ui, word,
  1931. (unsigned int)(s - word),
  1932. ui->font_px, 0, 0, 0);
  1933. if (line_w > 0 && line_w + 6 + ww > w) {
  1934. lines++;
  1935. line_w = ww;
  1936. } else {
  1937. line_w += (line_w ? 6 : 0) + ww;
  1938. }
  1939. while (*s == ' ')
  1940. s++;
  1941. }
  1942. return lines;
  1943. }
  1944. static int help_line_count(struct ngui *ui, int w,
  1945. const char *const *lines, int n)
  1946. {
  1947. int i, count = 0;
  1948. for (i = 0; i < n; i++)
  1949. count += wrap_count(ui, w, lines[i]);
  1950. return count;
  1951. }
  1952. /* Draw one paragraph word-wrapped into `w`; returns the next line's y. */
  1953. static int draw_wrapped(struct ngui *ui, int x, int y, int w,
  1954. const char *s, unsigned short color, int lh,
  1955. int bottom)
  1956. {
  1957. int line_w = 0;
  1958. while (s && *s) {
  1959. const char *word = s;
  1960. int ww, lx;
  1961. while (*s && *s != ' ')
  1962. s++;
  1963. ww = (int)ngui_text_width_px(ui, word,
  1964. (unsigned int)(s - word),
  1965. ui->font_px, 0, 0, 0);
  1966. if (line_w > 0 && line_w + 6 + ww > w) {
  1967. y += lh;
  1968. line_w = 0;
  1969. }
  1970. if (y + lh > bottom)
  1971. return y;
  1972. lx = line_w > 0 ? x + line_w + 6 : x;
  1973. ngui_text_n(ui, lx, y, word, (unsigned int)(s - word), color);
  1974. line_w = lx + ww - x;
  1975. while (*s == ' ')
  1976. s++;
  1977. }
  1978. return y + lh;
  1979. }
  1980. static void help_geometry(struct ngui *ui, struct ngui_rect *box,
  1981. struct ngui_rect *btn)
  1982. {
  1983. const char *const *lines;
  1984. int n, w, h, lh;
  1985. if (help_open == HELP_ABOUT) {
  1986. lines = about_lines;
  1987. n = (int)(sizeof(about_lines) / sizeof(about_lines[0]));
  1988. } else {
  1989. lines = howto_lines;
  1990. n = (int)(sizeof(howto_lines) / sizeof(howto_lines[0]));
  1991. }
  1992. w = 470;
  1993. if (w > ui->w - 24)
  1994. w = ui->w - 24;
  1995. if (w < 280)
  1996. w = 280;
  1997. lh = ngui_text_line_height(ui, ui->font_px, 0) + 5;
  1998. h = 120 + help_line_count(ui, w - 40, lines, n) * lh;
  1999. if (h > ui->h - 40)
  2000. h = ui->h - 40;
  2001. if (h < 220)
  2002. h = 220;
  2003. box->x = (ui->w - w) / 2;
  2004. box->y = (ui->h - h) / 2;
  2005. box->w = w;
  2006. box->h = h;
  2007. btn->x = box->x + (w - 84) / 2;
  2008. btn->y = box->y + h - 34;
  2009. btn->w = 84;
  2010. btn->h = 24;
  2011. }
  2012. static void draw_help(struct ngui *ui)
  2013. {
  2014. struct ngui_rect box, btn;
  2015. const char *title = help_open == HELP_ABOUT ?
  2016. "About Solitaire" : "How to play";
  2017. const char *const *lines;
  2018. int n, i, y, lh;
  2019. help_geometry(ui, &box, &btn);
  2020. ngui_fill_round_rect(ui, rect_make(box.x + 3, box.y + 4, box.w, box.h),
  2021. 10, 10, 10, 10, ui->theme.bg);
  2022. ngui_fill_round_rect(ui, box, 10, 10, 10, 10, ui->theme.panel);
  2023. ngui_frame_round(ui, box, 10, 10, 10, 10, 1, ui->theme.border);
  2024. ngui_text_px(ui, box.x + 20, box.y + 16, title, text_len(title),
  2025. ui->theme.text, 18, 1, 0, 0);
  2026. if (ngui_icon_button(ui, ID_HELP_X,
  2027. rect_make(box.x + box.w - 36, box.y + 12, 24, 24),
  2028. NGUI_ICON_CLOSE)) {
  2029. help_open = HELP_NONE;
  2030. ngui_invalidate(ui);
  2031. }
  2032. ngui_fill_rect(ui, rect_make(box.x + 20, box.y + 46, box.w - 40, 1),
  2033. ui->theme.border);
  2034. if (help_open == HELP_ABOUT) {
  2035. lines = about_lines;
  2036. n = (int)(sizeof(about_lines) / sizeof(about_lines[0]));
  2037. } else {
  2038. lines = howto_lines;
  2039. n = (int)(sizeof(howto_lines) / sizeof(howto_lines[0]));
  2040. }
  2041. lh = ngui_text_line_height(ui, ui->font_px, 0) + 5;
  2042. y = box.y + 60;
  2043. for (i = 0; i < n; i++) {
  2044. if (y + lh > btn.y - 12)
  2045. break;
  2046. y = draw_wrapped(ui, box.x + 20, y, box.w - 40, lines[i],
  2047. ui->theme.text, lh, btn.y - 12);
  2048. }
  2049. if (ngui_button(ui, ID_HELP_CLOSE, btn, "Close")) {
  2050. help_open = HELP_NONE;
  2051. ngui_invalidate(ui);
  2052. }
  2053. }
  2054. /* ------------------------------------------------------------------ */
  2055. /* interaction */
  2056. static void menu_action(int action, unsigned int now)
  2057. {
  2058. switch (action) {
  2059. case ACT_NEW:
  2060. new_game(now);
  2061. break;
  2062. case ACT_DRAW1:
  2063. draw_three = 0;
  2064. break;
  2065. case ACT_DRAW3:
  2066. draw_three = 1;
  2067. break;
  2068. case ACT_BLUE:
  2069. deck_style = 0;
  2070. break;
  2071. case ACT_RED:
  2072. deck_style = 1;
  2073. break;
  2074. case ACT_HOWTO:
  2075. help_open = HELP_HOWTO;
  2076. break;
  2077. case ACT_ABOUT:
  2078. help_open = HELP_ABOUT;
  2079. break;
  2080. default:
  2081. break;
  2082. }
  2083. }
  2084. static void game_press(struct ngui *ui, const struct sol_layout *l,
  2085. unsigned int now, int *changed)
  2086. {
  2087. struct hit h;
  2088. struct sel s;
  2089. struct ngui_rect sr;
  2090. if (won) {
  2091. if (bounce_active || bounce_show) {
  2092. bounce_active = 0;
  2093. bounce_show = 0;
  2094. *changed = 1;
  2095. }
  2096. return;
  2097. }
  2098. h = hit_test(l, ui->press_x, ui->press_y);
  2099. if (h.kind == HIT_STOCK) {
  2100. stock_click(now);
  2101. *changed = 1;
  2102. return;
  2103. }
  2104. s = selection_from_hit(&h);
  2105. if (select_sel.kind != SRC_NONE && hit_within_selection(&h, &select_sel))
  2106. s = select_sel;
  2107. press_sel = s;
  2108. press_target = target_from_hit(&h);
  2109. press_x = ui->press_x;
  2110. press_y = ui->press_y;
  2111. press_active = 1;
  2112. sr = sel_rect(l, &s);
  2113. press_card_x = sr.x;
  2114. press_card_y = sr.y;
  2115. if (s.kind == SRC_NONE) {
  2116. press_card_x = ui->press_x;
  2117. press_card_y = ui->press_y;
  2118. }
  2119. }
  2120. static void select_click(const struct sol_layout *l, unsigned int now,
  2121. int *changed)
  2122. {
  2123. int dbl = 0;
  2124. if (last_click_valid && same_sel(&press_sel, &last_click_sel) &&
  2125. (unsigned int)(now - last_click_ms) < 800u)
  2126. dbl = 1;
  2127. if (dbl && press_sel.kind != SRC_NONE) {
  2128. if (auto_to_foundation(&press_sel, now)) {
  2129. *changed = 1;
  2130. last_click_valid = 0;
  2131. return;
  2132. }
  2133. }
  2134. if (select_sel.kind != SRC_NONE && press_target.kind != TGT_NONE) {
  2135. int same_source = (select_sel.kind == SRC_TABLEAU &&
  2136. press_target.kind == TGT_TABLEAU &&
  2137. select_sel.pile == press_target.pile) ||
  2138. (select_sel.kind == SRC_FOUNDATION &&
  2139. press_target.kind == TGT_FOUNDATION &&
  2140. select_sel.pile == press_target.pile);
  2141. if (!same_source) {
  2142. if (perform_move(&select_sel, &press_target, now)) {
  2143. *changed = 1;
  2144. last_click_valid = 0;
  2145. return;
  2146. }
  2147. }
  2148. }
  2149. if (press_sel.kind != SRC_NONE) {
  2150. if (same_sel(&press_sel, &select_sel)) {
  2151. clear_selection();
  2152. last_click_valid = 0;
  2153. } else {
  2154. select_sel = press_sel;
  2155. last_click_sel = press_sel;
  2156. last_click_ms = now;
  2157. last_click_valid = 1;
  2158. }
  2159. *changed = 1;
  2160. return;
  2161. }
  2162. if (press_target.kind == TGT_NONE) {
  2163. if (select_sel.kind != SRC_NONE) {
  2164. clear_selection();
  2165. *changed = 1;
  2166. }
  2167. last_click_valid = 0;
  2168. }
  2169. (void)l;
  2170. }
  2171. static int ui_press_consumed(struct ngui *ui)
  2172. {
  2173. if (help_open) {
  2174. struct ngui_rect box, btn;
  2175. help_geometry(ui, &box, &btn);
  2176. if (!point_in(&box, ui->press_x, ui->press_y)) {
  2177. help_open = HELP_NONE;
  2178. ngui_invalidate(ui);
  2179. }
  2180. return 1;
  2181. }
  2182. if (ui->press_y < MENU_H) {
  2183. struct ngui_rect gr, hr;
  2184. menu_button_rect(ui, MENU_GAME, &gr);
  2185. menu_button_rect(ui, MENU_HELP, &hr);
  2186. if (menu_open != MENU_NONE &&
  2187. !point_in(&gr, ui->press_x, ui->press_y) &&
  2188. !point_in(&hr, ui->press_x, ui->press_y)) {
  2189. menu_open = MENU_NONE;
  2190. ngui_invalidate(ui);
  2191. }
  2192. return 1;
  2193. }
  2194. if (menu_open != MENU_NONE) {
  2195. struct ngui_rect dr;
  2196. if (!dropdown_rect(ui, &dr)) {
  2197. menu_open = MENU_NONE;
  2198. ngui_invalidate(ui);
  2199. return 0;
  2200. }
  2201. if (!point_in(&dr, ui->press_x, ui->press_y)) {
  2202. menu_open = MENU_NONE;
  2203. ngui_invalidate(ui);
  2204. }
  2205. return 1;
  2206. }
  2207. return 0;
  2208. }
  2209. static int handle_input(struct ngui *ui, const struct sol_layout *l,
  2210. unsigned int now)
  2211. {
  2212. int changed = 0;
  2213. int k;
  2214. while ((k = ngui_key(ui)) != 0) {
  2215. if (k == NGUI_KEY_ESC) {
  2216. if (help_open) {
  2217. help_open = HELP_NONE;
  2218. changed = 1;
  2219. } else if (menu_open != MENU_NONE) {
  2220. menu_open = MENU_NONE;
  2221. changed = 1;
  2222. } else {
  2223. quit = 1;
  2224. }
  2225. } else if (k == 'n' || k == 'N' || k == 'r' || k == 'R') {
  2226. new_game(now);
  2227. changed = 1;
  2228. } else if (k == 'h' || k == 'H') {
  2229. help_open = HELP_HOWTO;
  2230. menu_open = MENU_NONE;
  2231. changed = 1;
  2232. }
  2233. }
  2234. if (ui->pressed) {
  2235. if (ui_press_consumed(ui)) {
  2236. gesture_consumed = 1;
  2237. changed = 1;
  2238. } else {
  2239. game_press(ui, l, now, &changed);
  2240. }
  2241. }
  2242. if (press_active && !drag_active && !ui->released &&
  2243. press_sel.kind != SRC_NONE && (ui->buttons & 1u)) {
  2244. int dx = ui->mouse_x - press_x;
  2245. int dy = ui->mouse_y - press_y;
  2246. if (dx < 0)
  2247. dx = -dx;
  2248. if (dy < 0)
  2249. dy = -dy;
  2250. if (dx > 4 || dy > 4) {
  2251. drag_active = 1;
  2252. drag_sel = press_sel;
  2253. drag_dx = press_x - press_card_x;
  2254. drag_dy = press_y - press_card_y;
  2255. select_sel = press_sel;
  2256. last_click_valid = 0;
  2257. changed = 1;
  2258. }
  2259. }
  2260. if (ui->released) {
  2261. if (gesture_consumed) {
  2262. gesture_consumed = 0;
  2263. } else if (press_active) {
  2264. if (drag_active) {
  2265. struct target t;
  2266. t = drop_target_at(l, ui->release_x,
  2267. ui->release_y);
  2268. if (can_drop(&drag_sel, &t))
  2269. perform_move(&drag_sel, &t, now);
  2270. else
  2271. clear_selection();
  2272. drag_active = 0;
  2273. press_active = 0;
  2274. changed = 1;
  2275. } else {
  2276. select_click(l, now, &changed);
  2277. press_active = 0;
  2278. }
  2279. }
  2280. }
  2281. return changed;
  2282. }
  2283. /* ------------------------------------------------------------------ */
  2284. /* win animation */
  2285. static int bounce_update(struct ngui *ui, const struct sol_layout *l,
  2286. unsigned int now)
  2287. {
  2288. int i;
  2289. int changed = 0;
  2290. if (!bounce_active)
  2291. return 0;
  2292. if (bounce_count < BOUNCE_MAX) {
  2293. unsigned int wait = bounce_count == 0 ? 0u : 105u;
  2294. if ((unsigned int)(now - bounce_spawn_ms) >= wait) {
  2295. struct bounce_card *b = &bounce_cards[bounce_count];
  2296. int idx = (bounce_count * 7 + 3) % 48;
  2297. b->active = 1;
  2298. b->settled = 0;
  2299. /* A unique, non-king card per bouncing card: the four
  2300. * visible foundation kings stay unique. */
  2301. b->id = (idx / 12) * 13 + (idx % 12);
  2302. b->x = l->col_x[3 + (int)(rng_next() % 4)];
  2303. b->y = l->top_y;
  2304. b->vx = (int)(rng_next() % 7) - 3;
  2305. if (b->vx == 0)
  2306. b->vx = 2;
  2307. b->vy = -2 - (int)(rng_next() % 4);
  2308. bounce_count++;
  2309. bounce_spawn_ms = now;
  2310. changed = 1;
  2311. }
  2312. }
  2313. for (i = 0; i < BOUNCE_MAX; i++) {
  2314. struct bounce_card *b = &bounce_cards[i];
  2315. if (!b->active)
  2316. continue;
  2317. b->x += b->vx;
  2318. b->y += b->vy;
  2319. b->vy += 1;
  2320. if (b->x < 0) {
  2321. b->x = 0;
  2322. b->vx = -b->vx;
  2323. }
  2324. if (b->x + l->cw > ui->w) {
  2325. b->x = ui->w - l->cw;
  2326. if (b->x < 0)
  2327. b->x = 0;
  2328. b->vx = -b->vx;
  2329. }
  2330. if (b->y < 0) {
  2331. b->y = 0;
  2332. if (b->vy < 0)
  2333. b->vy = -b->vy;
  2334. }
  2335. if (b->y + l->ch > l->status_y - 2) {
  2336. b->y = l->status_y - 2 - l->ch;
  2337. if (b->y < 0)
  2338. b->y = 0;
  2339. if (b->vy > 0)
  2340. b->vy = -(b->vy * 5 / 8);
  2341. b->vx = b->vx * 7 / 8;
  2342. if (b->vy > -2) {
  2343. b->vy = 0;
  2344. if (b->vx < 2 && b->vx > -2) {
  2345. b->vx = 0;
  2346. b->active = 0;
  2347. b->settled = 1;
  2348. }
  2349. }
  2350. }
  2351. changed = 1;
  2352. }
  2353. if (bounce_count >= BOUNCE_MAX) {
  2354. int any = 0;
  2355. for (i = 0; i < BOUNCE_MAX; i++)
  2356. if (bounce_cards[i].active)
  2357. any = 1;
  2358. if (!any)
  2359. bounce_active = 0;
  2360. } else {
  2361. changed = 1;
  2362. }
  2363. if ((unsigned int)(now - bounce_start_ms) > 20000u) {
  2364. for (i = 0; i < bounce_count; i++) {
  2365. bounce_cards[i].active = 0;
  2366. bounce_cards[i].settled = 1;
  2367. }
  2368. bounce_active = 0;
  2369. }
  2370. return changed;
  2371. }
  2372. /* ------------------------------------------------------------------ */
  2373. /* app */
  2374. static void draw_app(struct ngui *ui, const struct sol_layout *l)
  2375. {
  2376. ngui_fill_rect(ui, rect_make(0, 0, ui->w, ui->h), C_FELT);
  2377. draw_stock(ui, l);
  2378. draw_waste(ui, l);
  2379. draw_foundations(ui, l);
  2380. draw_tableau(ui, l);
  2381. draw_drop_highlight(ui, l);
  2382. draw_selection_outline(ui, l);
  2383. draw_bounce_cards(ui, l);
  2384. if (drag_active)
  2385. draw_drag_cards(ui, l);
  2386. draw_menu_bar(ui, l);
  2387. draw_status(ui, l);
  2388. draw_dropdown(ui);
  2389. if (help_open)
  2390. draw_help(ui);
  2391. }
  2392. void _start(const char *arg)
  2393. {
  2394. static const struct {
  2395. int w, h;
  2396. } sizes[] = {
  2397. { START_W, START_H },
  2398. { 700, 520 },
  2399. { 640, 460 },
  2400. { 600, 400 },
  2401. { 520, 380 },
  2402. { 440, 340 },
  2403. { 360, 300 }
  2404. };
  2405. struct win_info info;
  2406. struct ngui ui;
  2407. unsigned int now;
  2408. unsigned int i;
  2409. int created = 0;
  2410. (void)arg;
  2411. for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
  2412. if (u_win_create((unsigned int)sizes[i].w,
  2413. (unsigned int)sizes[i].h,
  2414. "Solitaire", &info) == 0) {
  2415. created = 1;
  2416. break;
  2417. }
  2418. }
  2419. if (!created) {
  2420. u_puts("solitaire: no graphics window\n");
  2421. u_exit(1);
  2422. }
  2423. ngui_init(&ui, (unsigned short *)info.fb, (int)info.width,
  2424. (int)info.height);
  2425. ngui_font_attach(&ui);
  2426. draw_three = 0;
  2427. deck_style = 0;
  2428. quit = 0;
  2429. now = u_ticks_ms();
  2430. new_game(now);
  2431. for (;;) {
  2432. struct sol_layout l;
  2433. int want, changed, sec;
  2434. now = u_ticks_ms();
  2435. want = ngui_begin(&ui);
  2436. if (quit)
  2437. break;
  2438. changed = 0;
  2439. if (pending_action != ACT_NONE) {
  2440. int act = pending_action;
  2441. pending_action = ACT_NONE;
  2442. menu_action(act, now);
  2443. changed = 1;
  2444. }
  2445. compute_layout(&ui, &l);
  2446. changed |= handle_input(&ui, &l, now);
  2447. if (bounce_active)
  2448. changed |= bounce_update(&ui, &l, now);
  2449. sec = elapsed_seconds(now);
  2450. if (sec != last_sec) {
  2451. last_sec = sec;
  2452. changed = 1;
  2453. }
  2454. if (want || changed) {
  2455. draw_app(&ui, &l);
  2456. ngui_end(&ui);
  2457. } else {
  2458. ngui_end_ex(&ui, 0);
  2459. }
  2460. if (quit)
  2461. break;
  2462. u_sleep_ms(bounce_active ? 20 : 10);
  2463. }
  2464. u_win_close();
  2465. u_exit(0);
  2466. }