delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/14/10:12:23

From: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: allegro problems (gfx + kb)
Date: 14 Mar 1997 09:34:23 GMT
Organization: The National Capital FreeNet
Lines: 61
Message-ID: <5gb62v$h99@freenet-news.carleton.ca>
References: <199703122105 DOT VAA01257 AT post DOT dialin DOT co DOT uk>
Reply-To: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
NNTP-Posting-Host: freenet3.carleton.ca
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp


#define textoutf(b,f,fmt,x,y,c,args...)  ({ char foobuf[256];\
                              sprintf (foobuf,fmt, ## args);\
                              textout (b,f,foobuf,x,y,c);\
                            })

textoutf (screen,font,"The value is %d, in hex %x.\n",5,20,15,val,val);

(Shawn: want to verify this works and stick it in allegro.h? Of course
this will forever divorce allegro from Borland C, which is perfectly fine
by me. :))

(Portable but lacking textoutf on certain platforms: surround the above in
#ifdef __GNUC__ and #endif.)

(I tried making this as a function and could not figure out how to pass a
runtime variable number of args to sprintf. I assumed just passing my
param_pt with sprintf (fmt,param_pt) would not work. I imagine it would
likely take inline asm! Hence the vararg macro. There is really a lot to
be said for the versatility of #define. I have used it to write "functions"
that actually accept a type name like "int" or "char" as a parameter
specifying what to return. It's easy:

#define castmalloc(amt,type)  ( (type)malloc(amt) )

int *my_ptr;
char *my_string;
my_ptr=castmalloc(sizeof(int),int); // No error in C or C++
my_string=castmalloc(256,char); // Still no complaint in C++!

or even:

#define easymalloc(amt,type) ( (type)malloc(amt*sizeof(type)));

or eve better:

#define trapmalloc(amt,type,errfunc)  ({ type *fooptr;\
                                  fooptr=(type)malloc(amt*sizeof(type));\
                                  if (!fooptr) errfunc();\
                                  fooptr;\
                               })

void malloc_failed (void) {
  set_gfx_mode (GFX_TEXT,0,0,0,0);
  clrscr();
  printf ("\nSorry, out of memory. Try getting more RAM or freeing disk \
    space for swap.\n\n");
  exit(1);
}


...
int *array_of_a_thousand_ints;
array_of_a_thousand_ints=trapmalloc(100,int,malloc_failed);
textout(screen,font,"Found enough RAM for a thousand ints.",5,20,15);
--
    .*.  Where feelings are concerned, answers are rarely simple [GeneDeWeese]
 -()  <  When I go to the theater, I always go straight to the "bag and mix"
    `*'  bulk candy section...because variety is the spice of life... [me]
Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019