Example #1
0
// remove data item
void datahash::removedata(const unsigned char *str,const unsigned char *str2,const unsigned char *str3)
   {
   unsigned int id;

   datahash_ptr ptr,last;

   if (HASHMAP==NULL) return;

   id=(calcid(str)+calcid(str2)+calcid(str3))%HASHSIZE;

   ptr=HASHMAP[id];
   last=NULL;

   while (ptr!=NULL)
      {
      if (calceq(ptr,str,str2,str3))
         {
         if (last==NULL) HASHMAP[id]=ptr->next;
         else last->next=ptr->next;

         delete ptr;

         HASHNUM--;

         break;
         }

      last=ptr;
      ptr=ptr->next;
      }
   }
Example #2
0
// insert data item
void datahash::insertdata(const unsigned char *str,const unsigned char *str2,const unsigned char *str3,void *elem)
   {
   int i;

   unsigned int id;

   datahash_ptr ptr,item;

   if (str==NULL) ERRORMSG();

   if (HASHMAP==NULL)
      {
      HASHMAP=new datahash_ptr[HASHSIZE];

      for (i=0; i<HASHSIZE; i++) HASHMAP[i]=NULL;
      }

   id=(calcid(str)+calcid(str2)+calcid(str3))%HASHSIZE;

   ptr=HASHMAP[id];

   while (ptr!=NULL)
      {
      if (calceq(ptr,str,str2,str3)) return;

      ptr=ptr->next;
      }

   item=new datahash_type;

   item->str=str;
   item->str2=str2;
   item->str3=str3;

   item->elem=elem;

   HASHNUM++;

   item->next=HASHMAP[id];
   HASHMAP[id]=item;
   }
Example #3
0
// check for data item
void *datahash::checkdata(const unsigned char *str,const unsigned char *str2,const unsigned char *str3) const
   {
   unsigned int id;

   datahash_ptr ptr;

   if (HASHMAP==NULL) return(NULL);

   id=(calcid(str)+calcid(str2)+calcid(str3))%HASHSIZE;

   ptr=HASHMAP[id];

   while (ptr!=NULL)
      {
      if (calceq(ptr,str,str2,str3)) return(ptr->elem);

      ptr=ptr->next;
      }

   return(NULL);
   }
Example #4
0
int main(int argc, char *argv[])
{
  /* the program's name, d'oh */
  progname = argv[0];
  /* the whole file's name */
  char *fname;
  /* here the file's extension will be stored */
  char fext[FEXT_MAX_SIZE + 1] = { 0 };
  /* the unit's description (it's color, etc) */
  struct unit_desc desc = {{ 0 }};
  /* the user's unit */
  struct unit *unit = nmalloc(sizeof(struct unit));

  /* make sure there is at least one argument supplied */
  if (argc < 2){
    fprintf(stderr, "usage: %s <file>\n", progname);
    return 1;
  }

  fname = argv[1];

  /* fetch the extension, and store it in `fext' */
  {
    /* {{{ */
    int i = strlen(fname) - 1; /* minus one to not start at the '\0' */
    /* search for the first dot, starting from the end of the string */
    int j = 0; /* puts the characters into the `fext' array */

    /* set `i' to the last dot's position */
    for (; fname[i] != '.' && i >= 0; i--)
      ;

    if (fname[i] == '.'){
      /* we found a dot, let's see if it would fit into `fext' */
      /* `i + 1' because `i' points at the dot, not at the very first character
       * after it */
      if (strlen(fname) - (i + 1) > FEXT_MAX_SIZE){
        fprintf(stderr, "%s: file extension too long (max. %u chars)\n", progname, FEXT_MAX_SIZE);
        return 1;
      }

      /* see if there is anything after the dot (like, if it's not something
       * like `filename.') */
      if (fname[i + 1] == '\0'){
        fprintf(stderr, "%s: file extension empty\n", progname);
        return 1;
      }
    } else {
      /* we haven't seen any dots, that's a bummer */
      fprintf(stderr, "%s: file extension not found\n", progname);
      return 1;
    }

    /* here, everything should be all set and ready to roll */
    for (i++ /* skip over the dot */; j < FEXT_MAX_SIZE; j++, i++)
      fext[j] = fname[i];
    /* }}} */
  }

  /* call the function that will handle the file, based on it's extension */
  {
    /* {{{ */
    struct file_handlers *p = file_handlers;
    int found = 0;

    for (; p->fext != NULL && p->handler != NULL; p++){
      if (!strcmp(p->fext, fext)){
        found = 1;
        break;
      }
    }

    if (found){
      /* we know the extension, and can handle it */
      if (p->handler(unit, &desc, fname) == NULL){
        return 1;
      }
    } else {
      /* oops! */
      fprintf(stderr, "%s: file extension `%s' not supported\n", progname, fext);
      return 1;
    }
    /* }}} */
  }

  /* TODO: safety checks for the `desc' values */
  unit->desc = desc;
  /* set the defaults */
  unit->hp = UNIT_MAX_HP / 2;
  /* give the unit an id */
  unit->id = calcid(unit);
  /* push the unit onto the `unit_list' */
  push_unit(unit);
  /* call the `fetch' function to give the user his unit's id */
  unit->fun.fetch(unit->id);

  /* initialize the SDL */
  SDL_Init(SDL_INIT_EVERYTHING);
  /* set the title */
  SDL_WM_SetCaption("Poligon", NULL);
  /* create the window */
  SDL_Surface *screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF);

  SDL_Event event;
  int running = 1;

  /* put the unit in the middle of the screen */
  unit->x = screen->w / 2;
  unit->y = screen->h / 2;

  while (running){
    /* fill the background */
    SDL_FillRect(screen, NULL, 0x111111);

    if (SDL_PollEvent(&event)){
      switch (event.type){
        case SDL_QUIT:
          running = 0;
          break;
      }
    }

    draw_unit(screen, unit);
    unit->hp += sge_Random(-3, 3);
    /*unit->rot--;*/

    /* update the screen */
    SDL_UpdateRect(screen, 0, 0, 0, 0);
  }

  /* clean up after SDL */
  SDL_Quit();
  /* clean up after ourselves */
  free_unit_list();

  return 0;
}