hexchar.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* $Id$
  2. * MegaZeux
  3. *
  4. * Copyright (C) 1996 Greg Janson
  5. * Copyright (C) 1998 Matthew D. Williams - dbwilli@scsn.net
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. // Procedures to write hex strs onscreen
  22. #include "hexchar.h"
  23. #include "graphics.h"
  24. #include <stdlib.h>
  25. #include "string.h"
  26. #define num2hex(x) ((x)>9 ? 87+(x) : 48+(x))
  27. //DOES NOT PRESERVE MOUSE CURSOR
  28. void write_hex_byte(unsigned char byte,unsigned char color,int x,int y,
  29. unsigned int segment) {
  30. int t1,t2;
  31. t1=(byte&240)>>4;
  32. t1=num2hex(t1);
  33. t2=byte&15;
  34. t2=num2hex(t2);
  35. draw_char(t1,color,x,y,segment);
  36. draw_char(t2,color,x+1,y,segment);
  37. }
  38. //Set rightalign to print the rightmost char at xy and proceed to the left
  39. //minlen is the minimum length to print. Pad with 0.
  40. //DOES NOT PRESERVE MOUSE CURSOR
  41. void write_number(int number,unsigned char color,int x,int y,
  42. unsigned int segment,char minlen,char rightalign,int base) {
  43. char temp[7];
  44. int t1,t2;
  45. itoa(number,temp,base);
  46. if(rightalign) {
  47. t1=str_len(temp);
  48. if(minlen>t1) t1=minlen;
  49. x-=t1-1;
  50. }
  51. if((t2=str_len(temp))<minlen) {
  52. t2=minlen-t2;
  53. for(t1=0;t1<t2;t1++)
  54. draw_char('0',color,x++,y,segment);
  55. }
  56. write_string(temp,x,y,color,segment);
  57. }