Пример #1
0
/// \brief reads the mesh data.
void TR_Level::read_mesh_data(SDL_RWops * const src)
{
	bitu8 *buffer;
	SDL_RWops *newsrc = NULL;
	bitu32 size;
	bitu32 pos = 0;
	int mesh = 0;
	bitu32 i;
	bitu32 num_mesh_data;

	num_mesh_data = read_bitu32(src);

	size = num_mesh_data * 2;
	buffer = new bitu8[size];

	try {
		if (SDL_RWread(src, buffer, 1, size) < (int)size)
			throw TR_ReadError ("read_tr_mesh_data: SDL_RWread(buffer)", __FILE__, __LINE__, RCSID);

		if ((newsrc = SDL_RWFromMem(buffer, size)) == NULL)
			throw TR_ReadError ("read_tr_mesh_data: SDL_RWFromMem", __FILE__, __LINE__, RCSID);

		this->mesh_indices.resize(read_bitu32(src));
		for (i = 0; i < this->mesh_indices.size(); i++)
			this->mesh_indices[i] = read_bitu32(src);

		this->meshes.resize(this->mesh_indices.size());

		for (i = 0; i < this->mesh_indices.size(); i++) {
			bitu32 j;

			for (j = 0; j < this->mesh_indices.size(); j++)
				if (this->mesh_indices[j] == pos)
					this->mesh_indices[j] = mesh;

			SDL_RWseek(newsrc, pos, SEEK_SET);

			if (this->game_version >= TR_IV)
				read_tr4_mesh(newsrc, this->meshes[mesh]);
			else
				read_tr_mesh(newsrc, this->meshes[mesh]);

			mesh++;

			for (j = 0; j < this->mesh_indices.size(); j++)
				if (this->mesh_indices[j] > pos) {
					pos = this->mesh_indices[j];
					break;
				}
		}
		SDL_RWclose(newsrc);
		newsrc = NULL;
		delete [] buffer;
	}
	catch(...) {
		if (newsrc)
			SDL_RWclose(newsrc);
		delete [] buffer;

		throw;
	}
}
Пример #2
0
int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst) 
{
	png_structp png_ptr;
	png_infop info_ptr;
	png_colorp pal_ptr;
	SDL_Palette *pal;
	int i, colortype;
#ifdef USE_ROW_POINTERS
	png_bytep *row_pointers;
#endif
	/* Initialize and do basic error checking */
	if (!dst)
	{
		SDL_SetError("Argument 2 to SDL_SavePNG_RW can't be NULL, expecting SDL_RWops*\n");
		return (SAVEPNG_ERROR);
	}
	if (!surface)
	{
		SDL_SetError("Argument 1 to SDL_SavePNG_RW can't be NULL, expecting SDL_Surface*\n");
		if (freedst) SDL_RWclose(dst);
		return (SAVEPNG_ERROR);
	}
	png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, png_error_SDL, NULL); /* err_ptr, err_fn, warn_fn */
	if (!png_ptr) 
	{
		SDL_SetError("Unable to png_create_write_struct on %s\n", PNG_LIBPNG_VER_STRING);
		if (freedst) SDL_RWclose(dst);
		return (SAVEPNG_ERROR);
	}
	info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr)
	{
		SDL_SetError("Unable to png_create_info_struct\n");
		png_destroy_write_struct(&png_ptr, NULL);
		if (freedst) SDL_RWclose(dst);
		return (SAVEPNG_ERROR);
	}
	if (setjmp(png_jmpbuf(png_ptr)))	/* All other errors, see also "png_error_SDL" */
	{
		png_destroy_write_struct(&png_ptr, &info_ptr);
		if (freedst) SDL_RWclose(dst);
		return (SAVEPNG_ERROR);
	}

	/* Setup our RWops writer */
	png_set_write_fn(png_ptr, dst, png_write_SDL, NULL); /* w_ptr, write_fn, flush_fn */

	/* Prepare chunks */
	colortype = PNG_COLOR_MASK_COLOR;
	if (surface->format->BytesPerPixel > 0
	&&  surface->format->BytesPerPixel <= 8
	&& (pal = surface->format->palette))
	{
		colortype |= PNG_COLOR_MASK_PALETTE;
		pal_ptr = (png_colorp)malloc(pal->ncolors * sizeof(png_color));
		for (i = 0; i < pal->ncolors; i++) {
			pal_ptr[i].red   = pal->colors[i].r;
			pal_ptr[i].green = pal->colors[i].g;
			pal_ptr[i].blue  = pal->colors[i].b;
		}
		png_set_PLTE(png_ptr, info_ptr, pal_ptr, pal->ncolors);
		free(pal_ptr);
	}
	else if (surface->format->BytesPerPixel > 3 || surface->format->Amask)
		colortype |= PNG_COLOR_MASK_ALPHA;

	png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h, 8, colortype,
		PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

//	png_set_packing(png_ptr);

	/* Allow BGR surfaces */
	if (surface->format->Rmask == bmask
	&& surface->format->Gmask == gmask
	&& surface->format->Bmask == rmask)
		png_set_bgr(png_ptr);

	/* Write everything */
	png_write_info(png_ptr, info_ptr);
#ifdef USE_ROW_POINTERS
	row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*surface->h);
	for (i = 0; i < surface->h; i++)
		row_pointers[i] = (png_bytep)(Uint8*)surface->pixels + i * surface->pitch;
	png_write_image(png_ptr, row_pointers);
	free(row_pointers);
#else
	for (i = 0; i < surface->h; i++)
		png_write_row(png_ptr, (png_bytep)(Uint8*)surface->pixels + i * surface->pitch);
#endif
	png_write_end(png_ptr, info_ptr);

	/* Done */
	png_destroy_write_struct(&png_ptr, &info_ptr);
	if (freedst) SDL_RWclose(dst);
	return (SAVEPNG_SUCCESS);
}
Пример #3
0
int
main(int argc, char **argv)
{
    Uint8 *RawMooseData;
    SDL_RWops *handle;
    SDL_Window *window;
    int j;
    int fps = 12;
    int nodelay = 0;
#ifdef TEST_NV12
    Uint32 pixel_format = SDL_PIXELFORMAT_NV12;
#else
    Uint32 pixel_format = SDL_PIXELFORMAT_YV12;
#endif
    int scale = 5;

    /* Enable standard application logging */
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
        return 3;
    }

    while (argc > 1) {
        if (strcmp(argv[1], "-fps") == 0) {
            if (argv[2]) {
                fps = atoi(argv[2]);
                if (fps == 0) {
                    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                            "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
                    quit(10);
                }
                if ((fps < 0) || (fps > 1000)) {
                    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                            "The -fps option must be in range from 1 to 1000, default is 12.\n");
                    quit(10);
                }
                argv += 2;
                argc -= 2;
            } else {
                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                        "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
                quit(10);
            }
        } else if (strcmp(argv[1], "-nodelay") == 0) {
            nodelay = 1;
            argv += 1;
            argc -= 1;
        } else if (strcmp(argv[1], "-scale") == 0) {
            if (argv[2]) {
                scale = atoi(argv[2]);
                if (scale == 0) {
                    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                            "The -scale option requires an argument [from 1 to 50], default is 5.\n");
                    quit(10);
                }
                if ((scale < 0) || (scale > 50)) {
                    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                            "The -scale option must be in range from 1 to 50, default is 5.\n");
                    quit(10);
                }
                argv += 2;
                argc -= 2;
            } else {
                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                        "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
                quit(10);
            }
        } else if ((strcmp(argv[1], "-help") == 0)
                   || (strcmp(argv[1], "-h") == 0)) {
            PrintUsage(argv[0]);
            quit(0);
        } else {
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized option: %s.\n", argv[1]);
            quit(10);
        }
        break;
    }

    RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
    if (RawMooseData == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
        free(RawMooseData);
        quit(1);
    }

    /* load the trojan moose images */
    handle = SDL_RWFromFile("moose.dat", "rb");
    if (handle == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
        free(RawMooseData);
        quit(2);
    }

    SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);

    SDL_RWclose(handle);

    /* Create the window and renderer */
    window_w = MOOSEPIC_W * scale;
    window_h = MOOSEPIC_H * scale;
    window = SDL_CreateWindow("Happy Moose",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              window_w, window_h,
                              SDL_WINDOW_RESIZABLE);
    if (!window) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
        free(RawMooseData);
        quit(4);
    }

    renderer = SDL_CreateRenderer(window, -1, 0);
    if (!renderer) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
        free(RawMooseData);
        quit(4);
    }

    MooseTexture = SDL_CreateTexture(renderer, pixel_format, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
    if (!MooseTexture) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
        free(RawMooseData);
        quit(5);
    }
    /* Uncomment this to check vertex color with a YUV texture */
    /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */

    for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
        Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
        Uint8 *rgb;
        Uint8 *frame;

        rgb = MooseFrameRGB;
        frame = RawMooseData + i * MOOSEFRAME_SIZE;
        for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
            rgb[0] = MooseColors[frame[j]].r;
            rgb[1] = MooseColors[frame[j]].g;
            rgb[2] = MooseColors[frame[j]].b;
            rgb += 3;
        }
        switch (pixel_format) {
        case SDL_PIXELFORMAT_YV12:
            ConvertRGBtoYV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
            break;
        case SDL_PIXELFORMAT_NV12:
            ConvertRGBtoNV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
            break;
        default:
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unsupported pixel format\n");
            break;
        }
    }

    free(RawMooseData);

    /* set the start frame */
    i = 0;
    if (nodelay) {
        fpsdelay = 0;
    } else {
        fpsdelay = 1000 / fps;
    }

    displayrect.x = 0;
    displayrect.y = 0;
    displayrect.w = window_w;
    displayrect.h = window_h;

    /* Ignore key up events, they don't even get filtered */
    SDL_EventState(SDL_KEYUP, SDL_IGNORE);

    /* Loop, waiting for QUIT or RESIZE */
#ifdef __EMSCRIPTEN__
    emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
#else
    while (!done) {
        loop();
            }
#endif

    SDL_DestroyRenderer(renderer);
    quit(0);
    return 0;
}
Пример #4
0
level* level_load_file(char* filename) {

	for(int i = 0; i < num_tile_types; i++) {
		tile_counts[i] = 0;
	}

	level* l = malloc(sizeof(level));
	l->num_tile_sets = num_tile_types;
	l->tile_sets = malloc(sizeof(tile_set) * num_tile_types);
	l->tile_map = calloc(sizeof(int), MAX_WIDTH * MAX_HEIGHT);

	SDL_RWops* file = SDL_RWFromFile(filename, "r");
	char line[MAX_WIDTH];

	int y = 0;
	int x = 0;
	while(SDL_RWreadline(file, line, 1024)) {

		for(x = 0; x < strlen(line); x++) {
			char c = line[x];
			int type = tile_for_char(c);

			l->tile_map[x + y * MAX_WIDTH] = type;
			tile_counts[type]++;
		}

		y++;
	}

	SDL_RWclose(file);

	/* Start from 1, type 0 is none! */
	for(int i = 1; i < num_tile_types; i++) {

		int num_tiles = tile_counts[i];

		float* position_data = malloc(sizeof(float) * 3 * 4 * num_tiles);
		float* uv_data = malloc(sizeof(float) * 2 * 4 * num_tiles);

		int pos_i  = 0;
		int uv_i = 0;

		for(x = 0; x < MAX_WIDTH; x++)
			for(y = 0; y < MAX_HEIGHT; y++) {
				int type = l->tile_map[x + y * MAX_WIDTH];
				if( type == i ) {

					position_data[pos_i] = x * TILE_SIZE; pos_i++;
					position_data[pos_i] = y * TILE_SIZE; pos_i++;
					position_data[pos_i] = 0; pos_i++;

					position_data[pos_i] = (x+1) * TILE_SIZE; pos_i++;
					position_data[pos_i] = y * TILE_SIZE; pos_i++;
					position_data[pos_i] = 0; pos_i++;

					position_data[pos_i] = (x+1) * TILE_SIZE; pos_i++;
					position_data[pos_i] = (y+1) * TILE_SIZE; pos_i++;
					position_data[pos_i] = 0; pos_i++;

					position_data[pos_i] = x * TILE_SIZE; pos_i++;
					position_data[pos_i] = (y+1) * TILE_SIZE; pos_i++;
					position_data[pos_i] = 0; pos_i++;

					uv_data[uv_i] = 0; uv_i++;
					uv_data[uv_i] = 0; uv_i++;

					uv_data[uv_i] = 1; uv_i++;
					uv_data[uv_i] = 0; uv_i++;

					uv_data[uv_i] = 1; uv_i++;
					uv_data[uv_i] = 1; uv_i++;

					uv_data[uv_i] = 0; uv_i++;
					uv_data[uv_i] = 1; uv_i++;

				}
			}

		l->tile_sets[i].num_tiles = num_tiles;

		glGenBuffers(1, &l->tile_sets[i].positions_buffer);
		glGenBuffers(1, &l->tile_sets[i].texcoords_buffer);

		glBindBuffer(GL_ARRAY_BUFFER, l->tile_sets[i].positions_buffer);
		glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 4 * num_tiles, position_data, GL_STATIC_DRAW);

		glBindBuffer(GL_ARRAY_BUFFER, l->tile_sets[i].texcoords_buffer);
		glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * 4 * num_tiles, uv_data, GL_STATIC_DRAW);

		glBindBuffer(GL_ARRAY_BUFFER, 0);

		free(position_data);
		free(uv_data);

	}

	return l;
}
Пример #5
0
NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *src, int freesrc)
{
    NativeMidiSong *retval = NULL;
    void *buf = NULL;
    Sint64 len = 0;
    CFDataRef data = NULL;

    if (SDL_RWseek(src, 0, RW_SEEK_END) < 0)
        goto fail;
    len = SDL_RWtell(src);
    if (len < 0)
        goto fail;
    if (SDL_RWseek(src, 0, RW_SEEK_SET) < 0)
        goto fail;

    buf = malloc(len);
    if (buf == NULL)
        goto fail;

    if (SDL_RWread(src, buf, len, 1) != 1)
        goto fail;

    retval = malloc(sizeof(NativeMidiSong));
    if (retval == NULL)
        goto fail;

    memset(retval, '\0', sizeof (*retval));

    if (NewMusicPlayer(&retval->player) != noErr)
        goto fail;
    if (NewMusicSequence(&retval->sequence) != noErr)
        goto fail;

    data = CFDataCreate(NULL, (const UInt8 *) buf, len);
    if (data == NULL)
        goto fail;

    free(buf);
    buf = NULL;

    #if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
    /* MusicSequenceLoadSMFData() (avail. in 10.2, no 64 bit) is
     * equivalent to calling MusicSequenceLoadSMFDataWithFlags()
     * with a flags value of 0 (avail. in 10.3, avail. 64 bit).
     * So, we use MusicSequenceLoadSMFData() for powerpc versions
     * but the *WithFlags() on intel which require 10.4 anyway. */
    # if defined(__ppc__) || defined(__POWERPC__)
    if (MusicSequenceLoadSMFData(song->sequence, data) != noErr)
        goto fail;
    # else
    if (MusicSequenceLoadSMFDataWithFlags(retval->sequence, data, 0) != noErr)
        goto fail;
    # endif
    #else  /* MusicSequenceFileLoadData() requires 10.5 or later.  */
    if (MusicSequenceFileLoadData(retval->sequence, data, 0, 0) != noErr)
        goto fail;
    #endif

    CFRelease(data);
    data = NULL;

    if (GetSequenceLength(retval->sequence, &retval->endTime) != noErr)
        goto fail;

    if (MusicPlayerSetSequence(retval->player, retval->sequence) != noErr)
        goto fail;

    if (freesrc)
        SDL_RWclose(src);

    return retval;

fail:
    if (retval) {
        if (retval->sequence)
            DisposeMusicSequence(retval->sequence);
        if (retval->player)
            DisposeMusicPlayer(retval->player);
        free(retval);
    }

    if (data)
        CFRelease(data);

    if (buf)
        free(buf);

    return NULL;
}