Beispiel #1
0
void sdlgl_load_face_dirs(const char *filename, char *face_dirs)
{
  FILE *fp;

  int mon = 0;
  int ch;

#ifdef FILE_AREAS
  fp = fopen_datafile_area(FILE_AREA_SHARE, filename, RDTMODE, FALSE);
#else
  fp = fopen_datafile(filename, RDTMODE, FALSE);
#endif
  if (!fp)
  {
    sdlgl_error("Failed to open file: %s\n", filename);
    return;  /* NOT REACHED */
  }

  while ((ch = fgetc(fp)) != EOF)
  {
    ch = highc(ch);

    if (isspace(ch) || !isprint(ch))
      continue;

    if (ch != '.' && ch != 'L' && ch != 'R')
    {
      sdlgl_warning("Ignoring bad character `%c' in face file: %s\n",
          ch, filename);
      continue;
    }

    face_dirs[mon++] = ch;

    if (mon >= SIZE(tile_16_face_dirs))
    {
      sdlgl_warning("Too many monsters in face file: %s\n", filename);
      break;
    }
  }

  fclose(fp);
}
Beispiel #2
0
unsigned char *sdlgl_load_png_file(const char *filename,
    int *image_w, int *image_h)
{
  /* -AJA- all these volatiles here may seem strange.  They are needed
   * because the ANSI C standard (which GCC adheres to) says that when
   * setjmp/longjmp is being used, only volatile local variables are
   * guaranteed to keep their state if longjmp() gets called.
   */
  FILE * volatile fp = NULL;
  unsigned char * volatile image_dat = NULL;
  png_bytep * volatile row_pointers = NULL;

  /* we take the address of these two, so we shouldn't need the
   * volatile.  (GCC complains about discarding qualifiers if the
   * volatile is there).
   */
  png_structp /*volatile*/ png_ptr = NULL;
  png_infop   /*volatile*/ info_ptr = NULL;

  char sig_buf[CHECK_PNG_BYTES];
  png_uint_32 width, height;
  int bit_depth, color_type, interlace_type;
  int row, stride;

  /* open the prospective PNG file */
#ifdef FILE_AREAS
  fp = fopen_datafile_area(FILE_AREA_SHARE, filename, RDBMODE, FALSE);
#else
  fp = fopen_datafile(filename, RDBMODE, FALSE);
#endif
  if (!fp)
  {
    sdlgl_warning("Failed to open file: %s\n", filename);
    return NULL;
  }

  /* read in some of the signature bytes */
  if (fread(sig_buf, 1, CHECK_PNG_BYTES, fp) != CHECK_PNG_BYTES)
  {
    sdlgl_warning("Failed to read from file: %s\n", filename);
    goto failed;
  }

  /* compare the first CHECK_PNG_BYTES bytes of the signature */
  if (png_sig_cmp(sig_buf, (png_size_t)0, CHECK_PNG_BYTES) != 0)
  {
    sdlgl_warning("File is not a PNG file: %s\n", filename);
    goto failed;
  }

  /* pass NULLs for the error functions -- thus use the setjump stuff
   */
  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

  if (png_ptr == NULL)
  {
    sdlgl_warning("Problem within LibPNG (no memory ?)\n");
    goto failed;
  }

  /* allocate/initialize the memory for image information */
  info_ptr = png_create_info_struct(png_ptr);
  if (info_ptr == NULL)
  {
    sdlgl_warning("Out of memory with LibPNG\n");
    goto failed;
  }

  /* set error handling since we are using the setjmp/longjmp method
   * (this is the normal method of doing things with libpng).
   */
  if (setjmp(png_ptr->jmpbuf))
  {
    sdlgl_warning("Problem within LibPNG (unknown)\n");
    goto failed;
  }

  /* set up the input control since we're using standard C streams */
  png_init_io(png_ptr, fp);
  png_set_sig_bytes(png_ptr, CHECK_PNG_BYTES);

  /* the call to png_read_info() gives us all of the information from the
   * PNG file before the first IDAT (image data chunk)
   */
  png_read_info(png_ptr, info_ptr);

  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
      &color_type, &interlace_type, NULL, NULL);

  *image_w = (int)width;
  *image_h = (int)height;

  /* tell libpng to strip 16 bit/color down to 8 bits/color */
  png_set_strip_16(png_ptr);

  /* expand paletted colors into true RGB triplets */
  if (color_type == PNG_COLOR_TYPE_PALETTE ||
      color_type == PNG_COLOR_TYPE_GRAY ||
      png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
  {
     png_set_expand(png_ptr);
  }

  /* set alpha position and filler value */
  png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
 
  /* let all the above calls take effect */
  png_read_update_info(png_ptr, info_ptr);
  
  /* allocate the memory for the image */
  stride = png_get_rowbytes(png_ptr, info_ptr);

  image_dat = (unsigned char *) alloc(height * stride);
  row_pointers = (png_bytep *) alloc(height * sizeof(png_bytep));
   
  for (row=0; row < height; row++)
  {
     row_pointers[row] = image_dat + row * stride;
  }

  /* now read in the image.  Yeah baby ! */
  png_read_image(png_ptr, row_pointers);
  png_read_end(png_ptr, info_ptr);

  /* free stuff & close the file */
  png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
  free(row_pointers);
  fclose(fp);

  return image_dat;

  /* -AJA- Normally I don't like gotos.  In this situation where there
   * are lots of points of possible failure and a growing set of
   * things to be undone, it makes for nicer code.
   */
failed:

  if (image_dat)
    free(image_dat);
  
  if (png_ptr)
  {
    /* assume NULLs not allowed (png docs don't say, bad bad) */
    if (info_ptr)
      png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
    else
      png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
  }

  if (row_pointers)
    free(row_pointers);

  if (fp)
    fclose(fp);
  
  return NULL;
}
Beispiel #3
0
/* Locks the live log file and writes 'buffer' */
void livelog_write_string(char* buffer)
{
    FILE* livelogfile;
#ifdef FILE_AREAS
    if (lock_file_area(LOGAREA, LIVELOGFILE, 10)) {
#else
    if (lock_file(LIVELOGFILE, SCOREPREFIX, 10)) {
#endif
        if(!(livelogfile = fopen_datafile_area(LOGAREA, LIVELOGFILE, "a", SCOREPREFIX))) {
            pline("Cannot open live log file!");
        } else {
            fprintf(livelogfile, "%s", buffer);
            (void) fclose(livelogfile);
        }
        unlock_file_area(LOGAREA, LIVELOGFILE);
    }
}

static
char *livelog_prefix()
{
    s_level *lev = Is_special(&u.uz);
    snprintf(prefixbuf, STRBUF_LEN,
             "version=%s-%d.%d.%d:"
             "player=%s:turns=%ld:starttime=%ld:"
             "currenttime=%ld:"
             "dnum=%d:dname=%s:dlev=%d:maxlvl=%d:"
             "dlev_name=%s:"
             "hp=%d:maxhp=%d:deaths=%d:"
#ifdef RECORD_REALTIME
             "realtime=%ld:"
#endif
             "conduct=0x%lx:"
             "role=%s:race=%s:"
             "gender=%s:align=%s:"
             "gender0=%s:align0=%s:"
             "explvl=%d:exp=%ld:"
             "elbereths=%ld:"
             "xplevel=%d:" /* XP level */
             "exp=%ld:" /* Experience points */
             "mode=%s:"
             "gold=%ld",
             GAME_SHORT_NAME, VERSION_MAJOR, VERSION_MINOR, PATCHLEVEL,
             plname,
             moves,
             (long)u.ubirthday,
             (long)current_epoch(),
             u.uz.dnum, dungeons[u.uz.dnum].dname, depth(&u.uz), deepest_lev_reached(TRUE),
             lev ? lev->proto : "", /* proto level name if special level */
             u.uhp, u.uhpmax, u.umortality,
#ifdef RECORD_REALTIME
             (long)realtime_data.realtime,
#endif
             encodeconduct(),
             urole.filecode, urace.filecode,
             genders[flags.female].filecode, aligns[1-u.ualign.type].filecode,
             genders[flags.initgend].filecode, aligns[1-u.ualignbase[A_ORIGINAL]].filecode,
             u.ulevel,u.uexp,
             u.uconduct.elbereths,
             u.ulevel, /* XP level */
             (long)u.uexp, /* Experience points */
             (flags.debug ? "debug" : /* mode */
              flags.explore ? "explore" :
              hell_and_hell_mode ? "hah" :
              heaven_or_hell_mode ? "hoh" :
              "normal"),
#ifndef GOLDOBJ
             (u.ugold + hidden_gold())
#else
             (money_cnt(invent) + hidden_gold())
#endif
            );
    return prefixbuf;
}