delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/29/22:49:03

Date: Thu, 29 Jan 1998 19:47:16 -0800 (PST)
Message-Id: <199801300347.TAA01901@adit.ap.net>
Mime-Version: 1.0
To: riker AT pipcom DOT com, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: Allegro Datafiles - More Problems

At 01:07  1/29/1998 -0500, Matt Riker wrote:
>In article <C125659B DOT 002E50AC DOT 00 AT vega DOT ads DOT it>, G DOT DegliEsposti AT ads DOT it 
>says:
>
> > >Error: array subscript is not an integer
> > 
> > of course: you are trying to do this illegal operation:
> > 
> >   draw_sprite(screen,datafile["BLANK"].dat,16,16);
>
>No, I wasn't using quotes :)
>
> > I suppose this is what you really want to do:
> > 
> >   draw_sprite(screen,datafile[BLANK].dat,16,16);
> > 
> > where BLANK is #define'd somewhere...
>
>No, what I really want to do is this:
>
>   draw_sprite(screen,datafile[spritename].dat,16,16);
>
>Where 'spritename' is a constantly changing variable.  In the middle of 
>my program, I want 'spritename' to be anything, depending on what sprite 
>the user chooses.
Then I think you need to make a table of the names of the sprites (which are
strings), and their #define constant numbers. e.g:

struct spritename
  {
     char *name;
     int value;
  } names[] = {{"BLANK", BLANK},
               {"SOME_OTHER_SPRITE",SOME_OTHER_SPRITE} /* etc */,
               {NULL,0} /* mark end */ };

int lookup_sprite_name(const char *s)
{
   int i;
   for (i=0; names[i].name != NULL; i++)
        if (strcmp(s,names[i].name) == 0)
                return names[i].value;
   /* No match */
   return -1;
}

void do_stuff(void)
{
   char s[LARGE_SIZE];
   int spritenum;
   get_name_of_sprite_from_user(s);
   spritenum = lookup_sprite_name(s);
   if (spritenum == -1) print_error("No such sprite");
   else draw_sprite(screen,datafile[spritenum].dat,16,16);
}

Of course, much more optimum searching methods are possible.
>
> > the 'spritename' must be an int containing BLANK. If you need to use
> > different
> > values assign different ints to spritename.
>
>I could be just dumb :), but how can you but a string of letters into an 
>int?
You can't, that's the point. In the construct
datafile[MY_SPRITE].dat

`MY_SPRITE' *is* an int. It is defined in the header file which matches your
datafile, and is replaced with its integer value by the preprocessor. If you
look in that header file you will see something to the effect of:
#define MY_SPRITE 42

You seem to be confusing objects with their names. An object's name is only
known by the compiler and goes away at runtime. For more information, see
your favorite C[++] text.

Incidentally, your fake e-mail address is very annoying and rude.

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


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