PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/RELEASE-1_0A4/IronHells/src/client/displays/iso/bfont.c

#
C | 928 lines | 652 code | 148 blank | 128 comment | 54 complexity | 78476329ad6b7b214921f60064eadefb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, Apache-2.0
  1. /************************************************************/
  2. /* */
  3. /* BFONT.c v. 1.0.4-1 - Billi Font Library by Diego Billi */
  4. /* */
  5. /* mail: dbilli@cs.unibo.it */
  6. /* home: http://www.cs.unibo.it/~dbilli (ITALIAN) */
  7. /* */
  8. /************************************************************/
  9. /* Config headers */
  10. #ifdef HAVE_CONFIG_H
  11. #include "autoconf.h"
  12. #endif
  13. #ifndef USE_SDLTTF
  14. /* Standard headers */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. /* SDL headers */
  20. #include "SDL_image.h"
  21. /* Internal headers */
  22. #include "angband/angband.h"
  23. #include "displays/sdl-iso/bfont.h"
  24. // ATTENTION: MS Visual C++ do not declarate vsnprintf in <stdio.h>
  25. #ifdef WIN32
  26. #define vsnprintf _vsnprintf
  27. #endif
  28. /* Current font */
  29. static BFont_Info *CurrentFont;
  30. /* buffer size for buffered prints*/
  31. #define BFONT_BUFFER_LEN 1024
  32. /* Single global var for buffered prints */
  33. static char bfont_buffer[BFONT_BUFFER_LEN];
  34. /* utility functions */
  35. static Uint32 GetPixel(SDL_Surface * Surface,
  36. Sint32 X,
  37. Sint32 Y);
  38. static void PutPixel(SDL_Surface * surface,
  39. int x,
  40. int y,
  41. Uint32 pixel);
  42. int
  43. InitFont(BFont_Info * Font)
  44. {
  45. int x = 0, i = 0;
  46. Uint32 sentry;
  47. i = '!';
  48. sentry = GetPixel(Font->Surface, 0, 0);
  49. /* sentry = SDL_MapRGB(Font->Surface->format, 255, 0, 255); */
  50. if(Font->Surface == NULL)
  51. {
  52. fprintf(stderr, "BFont: The font has not been loaded!\n");
  53. return 1;
  54. }
  55. if(SDL_MUSTLOCK(Font->Surface))
  56. SDL_LockSurface(Font->Surface);
  57. x = 0;
  58. while(x < (Font->Surface->w - 1))
  59. {
  60. if(GetPixel(Font->Surface, x, 0) != sentry)
  61. {
  62. Font->Chars[i].x = x;
  63. Font->Chars[i].y = 1;
  64. Font->Chars[i].h = Font->Surface->h;
  65. for(;
  66. GetPixel(Font->Surface, x, 0) != sentry &&
  67. x < (Font->Surface->w); ++x) ;
  68. Font->Chars[i].w = (x - Font->Chars[i].x);
  69. i++;
  70. }
  71. else
  72. {
  73. x++;
  74. }
  75. }
  76. Font->Chars[' '].x = 0;
  77. Font->Chars[' '].y = 0;
  78. Font->Chars[' '].h = Font->Surface->h;
  79. Font->Chars[' '].w = Font->Chars['!'].w;
  80. if(SDL_MUSTLOCK(Font->Surface))
  81. SDL_UnlockSurface(Font->Surface);
  82. Font->h = Font->Surface->h;
  83. SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY,
  84. GetPixel(Font->Surface, 0, Font->Surface->h - 1));
  85. return 0;
  86. }
  87. /* Load the font and stores it in the BFont_Info structure */
  88. BFont_Info *
  89. LoadFont(const char *filename)
  90. {
  91. SDL_Surface *surface = NULL;
  92. int x;
  93. BFont_Info *Font = NULL;
  94. if(filename != NULL)
  95. {
  96. Font = (BFont_Info *) malloc(sizeof(BFont_Info));
  97. if(Font != NULL)
  98. {
  99. surface = (SDL_Surface *) IMG_Load(filename);
  100. if(surface != NULL)
  101. {
  102. Font->Surface = surface;
  103. for(x = 0; x < 256; x++)
  104. {
  105. Font->Chars[x].x = 0;
  106. Font->Chars[x].y = 0;
  107. Font->Chars[x].h = 0;
  108. Font->Chars[x].w = 0;
  109. }
  110. /* Init the font */
  111. InitFont(Font);
  112. /* Set the font as the current font */
  113. SetCurrentFont(Font);
  114. }
  115. else
  116. {
  117. /* free memory allocated for the BFont_Info structure */
  118. free(Font);
  119. Font = NULL;
  120. }
  121. }
  122. }
  123. return Font;
  124. }
  125. BFont_Info *
  126. LoadFontFromSurface(SDL_Surface * Surface)
  127. {
  128. int x;
  129. BFont_Info *Font = NULL;
  130. if(Surface != NULL)
  131. {
  132. Font = (BFont_Info *) malloc(sizeof(BFont_Info));
  133. if(Font != NULL)
  134. {
  135. Font->Surface = Surface;
  136. for(x = 0; x < 256; x++)
  137. {
  138. Font->Chars[x].x = 0;
  139. Font->Chars[x].y = 0;
  140. Font->Chars[x].h = 0;
  141. Font->Chars[x].w = 0;
  142. }
  143. /* Init the font */
  144. InitFont(Font);
  145. /* Set the font as the current font */
  146. SetCurrentFont(Font);
  147. }
  148. }
  149. return Font;
  150. }
  151. void
  152. FreeFont(BFont_Info * Font)
  153. {
  154. fprintf(stderr, "FreeFont()\n");
  155. if(!Font)
  156. return;
  157. fprintf(stderr, "Freeing font surface.\n");
  158. if(Font->Surface)
  159. SDL_FreeSurface(Font->Surface);
  160. fprintf(stderr, "Freeing font structure.\n");
  161. free(Font);
  162. }
  163. BFont_Info *
  164. SetFontColor(BFont_Info * Font,
  165. Uint8 r,
  166. Uint8 g,
  167. Uint8 b)
  168. {
  169. int x, y;
  170. BFont_Info *newfont;
  171. SDL_Surface *surface = NULL;
  172. Uint32 pixel;
  173. Uint8 old_r, old_g, old_b;
  174. Uint8 new_r, new_g, new_b;
  175. Uint32 color_key;
  176. newfont = (BFont_Info *) malloc(sizeof(BFont_Info));
  177. memset(newfont, 0, sizeof(BFont_Info));
  178. if(newfont != NULL)
  179. {
  180. newfont->h = Font->h;
  181. for(x = 0; x < 256; x++)
  182. {
  183. newfont->Chars[x].x = Font->Chars[x].x;
  184. newfont->Chars[x].y = Font->Chars[x].y;
  185. newfont->Chars[x].h = Font->Chars[x].h;
  186. newfont->Chars[x].w = Font->Chars[x].w;
  187. }
  188. surface =
  189. SDL_ConvertSurface(Font->Surface, Font->Surface->format,
  190. Font->Surface->flags);
  191. if(surface != NULL)
  192. {
  193. if(SDL_MUSTLOCK(surface))
  194. SDL_LockSurface(surface);
  195. if(SDL_MUSTLOCK(Font->Surface))
  196. SDL_LockSurface(Font->Surface);
  197. color_key = GetPixel(surface, 0, surface->h - 1);
  198. printf("looking...\n");
  199. for(x = 0; x < Font->Surface->w; x++)
  200. {
  201. for(y = 0; y < Font->Surface->h; y++)
  202. {
  203. old_r = old_g = old_b = 0;
  204. pixel = GetPixel(Font->Surface, x, y);
  205. if(pixel != color_key)
  206. {
  207. SDL_GetRGB(pixel, surface->format, &old_r,
  208. &old_g, &old_b);
  209. new_r = (Uint8) ((old_r * r) / 255);
  210. new_g = (Uint8) ((old_g * g) / 255);
  211. new_b = (Uint8) ((old_b * b) / 255);
  212. pixel =
  213. SDL_MapRGB(surface->format, new_r, new_g,
  214. new_b);
  215. PutPixel(surface, x, y, pixel);
  216. }
  217. }
  218. }
  219. printf("unlooking...\n");
  220. if(SDL_MUSTLOCK(surface))
  221. SDL_UnlockSurface(surface);
  222. if(SDL_MUSTLOCK(Font->Surface))
  223. SDL_UnlockSurface(Font->Surface);
  224. SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color_key);
  225. }
  226. newfont->Surface = surface;
  227. }
  228. return newfont;
  229. }
  230. /* Set the current font */
  231. void
  232. SetCurrentFont(BFont_Info * Font)
  233. {
  234. CurrentFont = Font;
  235. }
  236. /* Returns the pointer to the current font strucure in use */
  237. BFont_Info *
  238. GetCurrentFont(void)
  239. {
  240. return CurrentFont;
  241. }
  242. /* Return the font height */
  243. int
  244. FontHeight(BFont_Info * Font)
  245. {
  246. return (Font->h);
  247. }
  248. void
  249. SetFontHeight(BFont_Info * Font,
  250. int height)
  251. {
  252. Font->h = height;
  253. }
  254. /* Return the width of the "c" character */
  255. int
  256. CharWidth(BFont_Info * Font,
  257. int c)
  258. {
  259. return Font->Chars[c].w;
  260. }
  261. /* Puts a single char on the surface */
  262. int
  263. PutChar(SDL_Surface * Surface,
  264. int x,
  265. int y,
  266. int c)
  267. {
  268. return PutCharFont(Surface, CurrentFont, x, y, c);
  269. }
  270. /* Puts a single char on the surface with the specified font */
  271. int
  272. PutCharFont(SDL_Surface * Surface,
  273. BFont_Info * Font,
  274. int x,
  275. int y,
  276. int c)
  277. {
  278. int r = 0;
  279. SDL_Rect dest;
  280. dest.w = CharWidth(Font, ' ');
  281. dest.h = FontHeight(Font);
  282. dest.x = x;
  283. dest.y = y;
  284. if(c != ' ')
  285. {
  286. SDL_BlitSurface(Font->Surface, &Font->Chars[c], Surface, &dest);
  287. }
  288. r = dest.w;
  289. return r;
  290. }
  291. void
  292. PutString(SDL_Surface * Surface,
  293. int x,
  294. int y,
  295. const char *text)
  296. {
  297. PutStringFont(Surface, CurrentFont, x, y, text);
  298. }
  299. void
  300. PutStringFont(SDL_Surface * Surface,
  301. BFont_Info * Font,
  302. int x,
  303. int y,
  304. const char *text)
  305. {
  306. int i = 0;
  307. while(text[i] != '\0')
  308. {
  309. x += PutCharFont(Surface, Font, x, y, text[i]);
  310. i++;
  311. }
  312. }
  313. int
  314. TextWidth(const char *text)
  315. {
  316. return TextWidthFont(CurrentFont, text);
  317. }
  318. int
  319. TextWidthFont(BFont_Info * Font,
  320. const char *text)
  321. {
  322. int i = 0, x = 0;
  323. while(text[i] != '\0')
  324. {
  325. x += CharWidth(Font, text[i]);
  326. i++;
  327. }
  328. return x;
  329. }
  330. /* counts the spaces of the strings */
  331. static int
  332. count(const char *text)
  333. {
  334. char *p = NULL;
  335. int pos = -1;
  336. int i = 0;
  337. /* Calculate the space occupied by the text without spaces */
  338. while((p = strchr(&text[pos + 1], ' ')) != NULL)
  339. {
  340. i++;
  341. pos = p - text;
  342. }
  343. return i;
  344. }
  345. void
  346. JustifiedPutString(SDL_Surface * Surface,
  347. int y,
  348. const char *text)
  349. {
  350. JustifiedPutStringFont(Surface, CurrentFont, y, text);
  351. }
  352. void
  353. JustifiedPutStringFont(SDL_Surface * Surface,
  354. BFont_Info * Font,
  355. int y,
  356. const char *text)
  357. {
  358. int spaces = 0;
  359. int gap;
  360. int single_gap;
  361. int dif;
  362. char *strtmp;
  363. char *p;
  364. int pos = -1;
  365. int xpos = 0;
  366. if(strchr(text, ' ') == NULL)
  367. {
  368. PutStringFont(Surface, Font, 0, y, text);
  369. }
  370. else
  371. {
  372. gap = (Surface->w - 1) - TextWidthFont(Font, text);
  373. if(gap <= 0)
  374. {
  375. PutStringFont(Surface, Font, 0, y, text);
  376. }
  377. else
  378. {
  379. spaces = count(text);
  380. dif = gap % spaces;
  381. single_gap = (gap - dif) / spaces;
  382. xpos = 0;
  383. pos = -1;
  384. while(spaces > 0)
  385. {
  386. p = strstr(&text[pos + 1], " ");
  387. strtmp = NULL;
  388. strtmp =
  389. (char *) calloc((p - &text[pos + 1]) + 1,
  390. sizeof(char));
  391. if(strtmp != NULL)
  392. {
  393. strncpy(strtmp, &text[pos + 1],
  394. (p - &text[pos + 1]));
  395. PutStringFont(Surface, Font, xpos, y, strtmp);
  396. xpos =
  397. xpos + TextWidthFont(Font,
  398. strtmp) + single_gap +
  399. CharWidth(Font, ' ');
  400. if(dif >= 0)
  401. {
  402. xpos++;
  403. dif--;
  404. }
  405. pos = p - text;
  406. spaces--;
  407. free(strtmp);
  408. }
  409. }
  410. strtmp = NULL;
  411. strtmp =
  412. (char *) calloc(strlen(&text[pos + 1]) + 1,
  413. sizeof(char));
  414. if(strtmp != NULL)
  415. {
  416. strncpy(strtmp, &text[pos + 1],
  417. strlen(&text[pos + 1]));
  418. PutStringFont(Surface, Font, xpos, y, strtmp);
  419. free(strtmp);
  420. }
  421. }
  422. }
  423. }
  424. void
  425. CenteredPutString(SDL_Surface * Surface,
  426. int y,
  427. const char *text)
  428. {
  429. CenteredPutStringFont(Surface, CurrentFont, y, text);
  430. }
  431. void
  432. CenteredPutStringFont(SDL_Surface * Surface,
  433. BFont_Info * Font,
  434. int y,
  435. const char *text)
  436. {
  437. PutStringFont(Surface, Font,
  438. Surface->w / 2 - TextWidthFont(Font, text) / 2, y,
  439. text);
  440. }
  441. void
  442. RightPutString(SDL_Surface * Surface,
  443. int y,
  444. const char *text)
  445. {
  446. RightPutStringFont(Surface, CurrentFont, y, text);
  447. }
  448. void
  449. RightPutStringFont(SDL_Surface * Surface,
  450. BFont_Info * Font,
  451. int y,
  452. const char *text)
  453. {
  454. PutStringFont(Surface, Font,
  455. Surface->w - TextWidthFont(Font, text) - 1, y, text);
  456. }
  457. void
  458. LeftPutString(SDL_Surface * Surface,
  459. int y,
  460. const char *text)
  461. {
  462. LeftPutStringFont(Surface, CurrentFont, y, text);
  463. }
  464. void
  465. LeftPutStringFont(SDL_Surface * Surface,
  466. BFont_Info * Font,
  467. int y,
  468. const char *text)
  469. {
  470. PutStringFont(Surface, Font, 0, y, text);
  471. }
  472. /******/
  473. void
  474. PrintString(SDL_Surface * Surface,
  475. int x,
  476. int y,
  477. const char *fmt,
  478. ...)
  479. {
  480. va_list args;
  481. va_start(args, fmt);
  482. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  483. va_end(args);
  484. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  485. PutStringFont(Surface, CurrentFont, x, y, bfont_buffer);
  486. // va_list args;
  487. // char *temp;
  488. // va_start (args,fmt);
  489. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  490. // vsprintf(temp,fmt,args);
  491. // PutStringFont(Surface, CurrentFont, x, y, temp);
  492. // free (temp);
  493. // }
  494. // va_end(args);
  495. }
  496. void
  497. PrintStringFont(SDL_Surface * Surface,
  498. BFont_Info * Font,
  499. int x,
  500. int y,
  501. const char *fmt,
  502. ...)
  503. {
  504. va_list args;
  505. va_start(args, fmt);
  506. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  507. va_end(args);
  508. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  509. PutStringFont(Surface, Font, x, y, bfont_buffer);
  510. // va_list args;
  511. // char *temp;
  512. // va_start (args,fmt);
  513. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  514. // vsprintf(temp,fmt,args);
  515. // PutStringFont(Surface, Font, x, y, temp);
  516. // free (temp);
  517. // }
  518. // va_end(args);
  519. }
  520. void
  521. CenteredPrintString(SDL_Surface * Surface,
  522. int y,
  523. const char *fmt,
  524. ...)
  525. {
  526. va_list args;
  527. va_start(args, fmt);
  528. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  529. va_end(args);
  530. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  531. CenteredPutString(Surface, y, bfont_buffer);
  532. // va_list args;
  533. // char *temp;
  534. // va_start (args,fmt);
  535. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  536. // vsprintf(temp,fmt,args);
  537. // CenteredPutString(Surface, y, temp);
  538. // free (temp);
  539. // }
  540. // va_end(args);
  541. }
  542. void
  543. CenteredPrintStringFont(SDL_Surface * Surface,
  544. BFont_Info * Font,
  545. int y,
  546. const char *fmt,
  547. ...)
  548. {
  549. va_list args;
  550. va_start(args, fmt);
  551. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  552. va_end(args);
  553. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  554. CenteredPutStringFont(Surface, Font, y, bfont_buffer);
  555. // va_list args;
  556. // char *temp;
  557. // va_start (args,fmt);
  558. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  559. // vsprintf(temp,fmt,args);
  560. // CenteredPutStringFont(Surface, Font, y, temp);
  561. // free (temp);
  562. // }
  563. // va_end(args);
  564. }
  565. void
  566. RightPrintString(SDL_Surface * Surface,
  567. int y,
  568. const char *fmt,
  569. ...)
  570. {
  571. va_list args;
  572. va_start(args, fmt);
  573. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  574. va_end(args);
  575. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  576. RightPutString(Surface, y, bfont_buffer);
  577. // va_list args;
  578. // char *temp;
  579. // va_start (args,fmt);
  580. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  581. // vsprintf(temp,fmt,args);
  582. // RightPutString(Surface, y, temp);
  583. // free (temp);
  584. // }
  585. // va_end(args);
  586. }
  587. void
  588. RightPrintStringFont(SDL_Surface * Surface,
  589. BFont_Info * Font,
  590. int y,
  591. const char *fmt,
  592. ...)
  593. {
  594. va_list args;
  595. va_start(args, fmt);
  596. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  597. va_end(args);
  598. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  599. RightPutStringFont(Surface, Font, y, bfont_buffer);
  600. // va_list args;
  601. // char *temp;
  602. // va_start (args,fmt);
  603. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  604. // vsprintf(temp,fmt,args);
  605. // RightPutStringFont(Surface, Font, y, temp);
  606. // free (temp);
  607. // }
  608. // va_end(args);
  609. }
  610. void
  611. LeftPrintString(SDL_Surface * Surface,
  612. int y,
  613. const char *fmt,
  614. ...)
  615. {
  616. va_list args;
  617. va_start(args, fmt);
  618. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  619. va_end(args);
  620. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  621. LeftPutString(Surface, y, bfont_buffer);
  622. // va_list args;
  623. // char *temp;
  624. // va_start (args,fmt);
  625. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  626. // vsprintf(temp,fmt,args);
  627. // LeftPutString(Surface, y, temp);
  628. // free (temp);
  629. // }
  630. // va_end(args);
  631. }
  632. void
  633. LeftPrintStringFont(SDL_Surface * Surface,
  634. BFont_Info * Font,
  635. int y,
  636. const char *fmt,
  637. ...)
  638. {
  639. va_list args;
  640. va_start(args, fmt);
  641. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  642. va_end(args);
  643. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  644. LeftPutStringFont(Surface, Font, y, bfont_buffer);
  645. // va_list args;
  646. // char *temp;
  647. // va_start (args,fmt);
  648. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  649. // vsprintf(temp,fmt,args);
  650. // LeftPutStringFont(Surface, Font, y, temp);
  651. // free (temp);
  652. // }
  653. // va_end(args);
  654. }
  655. void
  656. JustifiedPrintString(SDL_Surface * Surface,
  657. int y,
  658. const char *fmt,
  659. ...)
  660. {
  661. va_list args;
  662. va_start(args, fmt);
  663. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  664. va_end(args);
  665. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  666. JustifiedPutString(Surface, y, bfont_buffer);
  667. // va_list args;
  668. // char *temp;
  669. // va_start (args,fmt);
  670. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  671. // vsprintf(temp,fmt,args);
  672. // JustifiedPutString( Surface, y,temp);
  673. // free (temp);
  674. // }
  675. // va_end(args);
  676. }
  677. void
  678. JustifiedPrintStringFont(SDL_Surface * Surface,
  679. BFont_Info * Font,
  680. int y,
  681. const char *fmt,
  682. ...)
  683. {
  684. va_list args;
  685. va_start(args, fmt);
  686. vsnprintf(bfont_buffer, BFONT_BUFFER_LEN, fmt, args);
  687. va_end(args);
  688. bfont_buffer[BFONT_BUFFER_LEN - 1] = '\0';
  689. JustifiedPutStringFont(Surface, Font, y, bfont_buffer);
  690. // va_list args;
  691. // char *temp;
  692. // va_start (args,fmt);
  693. // if ( (temp = (char *) malloc(1000+1)) != NULL) {
  694. // vsprintf(temp,fmt,args);
  695. // JustifiedPutStringFont( Surface, Font, y,temp);
  696. // free (temp);
  697. // }
  698. // va_end(args);
  699. }
  700. /*********************************************************************************************************/
  701. /*********************************************************************************************************/
  702. /*********************************************************************************************************/
  703. void
  704. PutPixel(SDL_Surface * surface,
  705. int x,
  706. int y,
  707. Uint32 pixel)
  708. {
  709. int bpp = surface->format->BytesPerPixel;
  710. /* Here p is the address to the pixel we want to set */
  711. Uint8 *p =
  712. (Uint8 *) surface->pixels + y * surface->pitch + x * bpp;
  713. switch (bpp)
  714. {
  715. case 1:
  716. *p = pixel;
  717. break;
  718. case 2:
  719. *(Uint16 *) p = pixel;
  720. break;
  721. case 3:
  722. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  723. {
  724. p[0] = (pixel >> 16) & 0xff;
  725. p[1] = (pixel >> 8) & 0xff;
  726. p[2] = pixel & 0xff;
  727. }
  728. else
  729. {
  730. p[0] = pixel & 0xff;
  731. p[1] = (pixel >> 8) & 0xff;
  732. p[2] = (pixel >> 16) & 0xff;
  733. }
  734. break;
  735. case 4:
  736. *(Uint32 *) p = pixel;
  737. break;
  738. }
  739. }
  740. Uint32
  741. GetPixel(SDL_Surface * Surface,
  742. Sint32 X,
  743. Sint32 Y)
  744. {
  745. Uint8 *bits;
  746. Uint32 Bpp;
  747. if(X < 0)
  748. puts("x too small in GetPixel!");
  749. if(X >= Surface->w)
  750. puts("x too big in GetPixel!");
  751. Bpp = Surface->format->BytesPerPixel;
  752. bits = ((Uint8 *) Surface->pixels) + Y * Surface->pitch + X * Bpp;
  753. // Get the pixel
  754. switch (Bpp)
  755. {
  756. case 1:
  757. return *((Uint8 *) Surface->pixels + Y * Surface->pitch +
  758. X);
  759. break;
  760. case 2:
  761. return *((Uint16 *) Surface->pixels +
  762. Y * Surface->pitch / 2 + X);
  763. break;
  764. case 3:
  765. { // Format/endian independent
  766. Uint8 r, g, b;
  767. r = *((bits) + Surface->format->Rshift / 8);
  768. g = *((bits) + Surface->format->Gshift / 8);
  769. b = *((bits) + Surface->format->Bshift / 8);
  770. return SDL_MapRGB(Surface->format, r, g, b);
  771. }
  772. break;
  773. case 4:
  774. return *((Uint32 *) Surface->pixels +
  775. Y * Surface->pitch / 4 + X);
  776. break;
  777. }
  778. return -1;
  779. }
  780. #endif /* USE_SDLTTF */