Exemple #1
0
picture_t *gli_picture_load(unsigned long id)
{
    picture_t *pic;
    FILE *fl;
    int closeafter;
    glui32 chunktype;

    pic = gli_picture_retrieve(id, 0);

    if (pic)
        return pic;

    if (!giblorb_is_resource_map())
    {
        char filename[1024];
        unsigned char buf[8];

        sprintf(filename, "%s/PIC%lu", gli_workdir, id); 

        closeafter = TRUE;
        fl = fopen(filename, "rb");
        if (!fl)
            return NULL;

        if (fread(buf, 1, 8, fl) != 8)
        {
            /* Can't read the first few bytes. Forget it. */
            fclose(fl);
            return NULL;
        }

        if (!png_sig_cmp(buf, 0, 8))
        {
            chunktype = giblorb_ID_PNG;
        }
        else if (buf[0] == 0xFF && buf[1] == 0xD8 && buf[2] == 0xFF)
        {
            chunktype = giblorb_ID_JPEG;
        }
        else
        {
            /* Not a readable file. Forget it. */
            fclose(fl);
            return NULL;
        }

        fseek(fl, 0, 0);
    }

    else
    {
        long pos;
        giblorb_get_resource(giblorb_ID_Pict, id, &fl, &pos, NULL, &chunktype);
        if (!fl)
            return NULL;
        fseek(fl, pos, 0);
        closeafter = FALSE;
    }

    pic = malloc(sizeof(picture_t));
    pic->refcount = 1;
    pic->w = 0;
    pic->h = 0;
    pic->rgba = NULL;
    pic->id = id;
    pic->scaled = FALSE;

    if (chunktype == giblorb_ID_PNG)
        load_image_png(fl, pic);

    if (chunktype == giblorb_ID_JPEG)
        load_image_jpeg(fl, pic);

    if (closeafter)
        fclose(fl);

    if (!pic->rgba)
    {
        free(pic);
        return NULL;
    }

    gli_picture_store(pic);

    return pic;
}
Exemple #2
0
glui32 glk_schannel_play_ext(schanid_t chan, glui32 snd, glui32 repeats, glui32 notify)
{
    FILE *file;
    long pos, len;
    glui32 type;
    char *buf;
    int offset = 0;
    int i;

    if (!chan) {
	gli_strict_warning("schannel_play_ext: invalid id.");
	return 0;
    }

    /* stop previous noise */
    glk_schannel_stop(chan);

    if (repeats == 0)
	return 1;

    /* load sound resource into memory */

    if (!giblorb_is_resource_map())
    {
	char name[1024];

	sprintf(name, "%s/SND%ld", gli_workdir, snd); 

	file = fopen(name, "rb");
	if (!file)
	    return 0;

	fseek(file, 0, SEEK_END);
	len = ftell(file);

	buf = malloc(len);
	if (!buf) {
	    fclose(file);
	    return 0;
	}

	fseek(file, 0, 0);
	fread(buf, 1, len, file);
	fclose(file);

	/* identify by file magic the two types that fmod can do... */

	type = 0;	/* unidentified */

	/* AIFF */
	if (len > 4 && !memcmp(buf, "FORM", 4))		
	    type = giblorb_ID_FORM;

	/* WAVE */
	if (len > 4 && !memcmp(buf, "WAVE", 4))
	    type = giblorb_ID_WAVE;

	/* MIDI */
	if (len > 4 && !memcmp(buf, "MThd", 4))
	    type = giblorb_ID_MIDI;

	/* Ogg Vorbis */
	if (len > 4 && !memcmp(buf, "OggS", 4))
	    type = giblorb_ID_OGG;

	/* s3m */
	if (len > 0x30 && !memcmp(buf + 0x2c, "SCRM", 4))
	    type = giblorb_ID_MOD;

	/* XM */
	if (len > 20 && !memcmp(buf, "Extended Module: ", 17))
	    type = giblorb_ID_MOD;

	/* MOD */
	if (len > 1084)
	{
	    char resname[4];
	    memcpy(resname, buf + 1080, 4);
	    if (!strcmp(resname+1, "CHN") ||	/* 4CHN, 6CHN, 8CHN */
		    !strcmp(resname+2, "CN") ||	 /* 16CN, 32CN */
		    !strcmp(resname, "M.K.") || !strcmp(resname, "M!K!") ||
		    !strcmp(resname, "FLT4") || !strcmp(resname, "CD81") ||
		    !strcmp(resname, "OKTA") || !strcmp(resname, "    "))
		type = giblorb_ID_MOD;
	}

	if (!memcmp(buf, "\377\372", 2))	/* mp3 */
	    type = giblorb_ID_MP3;

	/* look for RIFF (future boy has broken resources...?) */
	if (len > 128 && type == 0)
	    for (i = 0; i < 124; i++)
		if (!memcmp(buf+i, "RIFF", 4))
		{
		    offset = i;
		    type = giblorb_ID_WAVE;
		    break;
		}

	if (type == 0)
	    type = giblorb_ID_MP3;
    }

    else
    {
	giblorb_get_resource(giblorb_ID_Snd, snd, &file, &pos, &len, &type);
	if (!file)
	    return 0;

	buf = malloc(len);
	if (!buf)
	    return 0;

	fseek(file, pos, 0);
	fread(buf, 1, len, file);
    }

    switch (type)
    {
    case giblorb_ID_FORM:
    case giblorb_ID_WAVE:
    case giblorb_ID_MP3:
	chan->sample = FSOUND_Sample_Load(FSOUND_UNMANAGED, buf + offset,
		FSOUND_NORMAL|FSOUND_LOADMEMORY, 0, len);
	if (!chan->sample) { free(buf); return 0; }
	if (repeats != 1)
	    FSOUND_Sample_SetMode(chan->sample, FSOUND_LOOP_NORMAL);
	else
	    FSOUND_Sample_SetMode(chan->sample, FSOUND_LOOP_OFF);
	chan->channel = FSOUND_PlaySound(FSOUND_FREE, chan->sample);
	FSOUND_SetVolume(chan->channel, chan->volume / 256);
	FSOUND_SetPaused(chan->channel, 0);
	break;

    case giblorb_ID_MOD:
    case giblorb_ID_MIDI:
	chan->module = FMUSIC_LoadSongEx(buf, 0, len, FSOUND_LOADMEMORY, 0, 0);
	if (!chan->module) { free(buf); return 0; }
	if (repeats != 1)
	    FMUSIC_SetLooping(chan->module, 1);
	else
	    FMUSIC_SetLooping(chan->module, 0);
	FMUSIC_SetMasterVolume(chan->module, chan->volume / 256);
	FMUSIC_PlaySong(chan->module);
	break;

    default:
	gli_strict_warning("schannel_play_ext: unknown resource type.");
    }

    free(buf);

    return 1;
}
static glui32 load_sound_resource(glui32 snd, long *len, char **buf)
{
    if (!giblorb_is_resource_map())
    {
        FILE *file;
        char name[1024];

        sprintf(name, "%s/SND%ld", gli_workdir, (long) snd);

        file = fopen(name, "rb");
        if (!file)
            return 0;

        fseek(file, 0, SEEK_END);
        *len = ftell(file);

        *buf = malloc(*len);
        if (!*buf)
        {
            fclose(file);
            return 0;
        }

        fseek(file, 0, 0);
        if (fread(*buf, 1, *len, file) != *len && !feof(file)) return 0;
        fclose(file);

        /* AIFF */
        if (*len > 4 && !memcmp(*buf, "FORM", 4))
            return giblorb_ID_FORM;

        /* WAVE */
        if (*len > 4 && !memcmp(*buf, "WAVE", 4))
            return giblorb_ID_WAVE;

        /* s3m */
        if (*len > 0x30 && !memcmp(*buf + 0x2c, "SCRM", 4))
            return giblorb_ID_MOD;

        /* XM */
        if (*len > 20 && !memcmp(*buf, "Extended Module: ", 17))
            return giblorb_ID_MOD;

        /* MOD */
        if (*len > 1084)
        {
            char resname[4];
            memcpy(resname, (*buf) + 1080, 4);
            if (!strcmp(resname+1, "CHN") ||        /* 4CHN, 6CHN, 8CHN */
                    !strcmp(resname+2, "CN") ||         /* 16CN, 32CN */
                    !strcmp(resname, "M.K.") || !strcmp(resname, "M!K!") ||
                    !strcmp(resname, "FLT4") || !strcmp(resname, "CD81") ||
                    !strcmp(resname, "OKTA") || !strcmp(resname, "    "))
                return giblorb_ID_MOD;
        }

        /* ogg */
        if (*len > 4 && !memcmp(*buf, "OggS", 4))
            return giblorb_ID_OGG;

        /* mp3 */
        if (*len > 2 && !memcmp(*buf, "\377\372", 2))
            return giblorb_ID_MP3;

        return giblorb_ID_MP3;
    }
    else
    {
        FILE *file;
        glui32 type;
        long pos;

        giblorb_get_resource(giblorb_ID_Snd, snd, &file, &pos, len, &type);
        if (!file)
            return 0;

        *buf = malloc(*len);
        if (!*buf)
            return 0;

        fseek(file, pos, 0);
        if (fread(*buf, 1, *len, file) != *len && !feof(file)) return 0;
        return type;
    }
}