ucmd_solitaire.c 61 KB

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