Ejemplo n.º 1
0
int64_t Dx_FileRead_size(const char *filename) {
    SDL_RWops *rwops = Dx_File_OpenStream(filename);
    if (rwops != NULL) {
        int64_t size = rwops->size(rwops);
        rwops->close(rwops);
        return size;
    }
    return 0;
}
Ejemplo n.º 2
0
void AddResource(ResourceManager resourceManager, std::string identifier, ResourceType resourceType) {
    ResourceHandle resourceHandle = CreateResourceHandle(resourceType);

    switch (resourceType) {
    case TEXTURE: {
        std::string strId =  resourceManager->assetBaseDir + identifier;
        resourceHandle->textureRes->texture = IMG_LoadTexture(resourceManager->renderer, strId.c_str());
        if(!resourceHandle->textureRes->texture) {
            LOGE("Unable to load resource: %s\n", strId.c_str());
            FreeResourceHandle(resourceManager, resourceHandle);
            assert(0);
        }
        resourceHandle->textureRes->name = identifier; }
        break;
    case TMX_MAP: {
        std::string strId = resourceManager->assetBaseDir + identifier;
        resourceHandle->tmxMapResource->map.ParseFile(strId);
        break;
    }
    case ENTITY: {
        std::string strId = resourceManager->assetBaseDir + identifier;
        SDL_RWops * file = SDL_RWFromFile (strId.c_str(), "rb");

        if (!file) {
            assert(0);
        }

        int fileSize = file->size(file);

        if (fileSize <= 0) {
            assert(0);
        }

        char* fileText = new char[fileSize + 1];
        fileText[fileSize] = 0;
        file->read(file, fileText, 1, fileSize);

        file->close(file);

        std::string text(fileText, fileText+fileSize);
        delete [] fileText;

        resourceHandle->entityTemplateResource->xml.Parse(text.c_str());

        break;
    }
    default:
        assert(0);
    }

    resourceManager->resources[identifier] = resourceHandle;
}
Ejemplo n.º 3
0
std::string LoadAllText(std::string file)
{
	SDL_RWops *rw = SDL_RWFromFile(file.c_str(), "r");

	auto size = rw->size(rw);
	char* script = new char[size + 1];
	memset(script, 0, size + 1);
	rw->read(rw, script, sizeof(char), size);
	rw->close(rw);

	std::string result = script;
	delete[] script;

	return result;
}
Ejemplo n.º 4
0
void Purity::LuaManager::doFile(const std::string& luaFileName)
{
    SDL_RWops* file = SDL_RWFromFile(luaFileName.c_str(), "rb");

    int fileSize = file->size(file);
    char* fileContents = new char[fileSize + 1];

    file->read(file, fileContents, 1, fileSize);
    fileContents[fileSize] = '\0';

    file->close(file);

    luaL_dostring(mLuaState, fileContents);

    delete [] fileContents;
}
Ejemplo n.º 5
0
void Purity::Font::loadFont()
{
    SDL_RWops* fontFile = SDL_RWFromFile(mFontFileName.c_str(), "rb");

    if (fontFile == nullptr)
    {
        std::cerr << "Error loading font file"
                  << ": " << SDL_GetError() << std::endl;
    }

    const auto fontFileSize = fontFile->size(fontFile);
    mFontData.reserve(fontFileSize);

    fontFile->read(fontFile, &mFontData[0], 1, fontFileSize);

    fontFile->close(fontFile);
}
int SDLJBN_AddMappingsFromFile(const char *file)
{
	int ret = 0;
	char *s = NULL;

	SDLJBN_Init();
	SDL_RWops *rwops = SDL_RWFromFile(file, "r");
	if (rwops == NULL)
	{
		err = "Cannot open file";
		ret = -1;
		goto bail;
	}

	// Read file into memory
	const Sint64 fsize = rwops->size(rwops);
	if (fsize == -1)
	{
		err = "Cannot find file size";
		ret = -1;
		goto bail;
	}
	s = SDL_malloc((size_t)fsize + 1);
	if (s == NULL)
	{
		err = "Out of memory";
		ret = -1;
		goto bail;
	}
	if (SDL_RWread(rwops, s, (size_t)fsize, 1) == 0)
	{
		err = "Cannot read file";
		ret = -1;
		goto bail;
	}
	s[fsize] = '\0';

	ret = ReadMappingsString(s);

bail:
	SDL_RWclose(rwops);
	SDL_free(s);
	return ret;
}
Ejemplo n.º 7
0
Archivo: io.c Proyecto: mbelicki/warp
extern warp_result_t read_file(const char *path, warp_array_t *out_buffer) {
    SDL_RWops *io = SDL_RWFromFile(path, "r");
    if (io == NULL) {
        return warp_failure("Cannot open the file: %s.", path);
    }
    Sint64 size = io->size(io);
    char *content = malloc(sizeof (char) * (size + 1));
    if (content == NULL) {
        io->close(io);
        return warp_failure("Cannot allocate file buffer for file: %s", path);
    }
    if (io->read(io, content, size, 1) == 0) {
        free(content);
        io->close(io);
        return warp_failure("Error while reading file: %s.", path);
    }
    io->close(io);

    content[size] = '\0';
    *out_buffer = warp_array_create_from_buffer(content, sizeof (char), size + 1, NULL);
    free(content);

    return warp_success();
}
Ejemplo n.º 8
0
void
APKProtocol::load()
{
    auto resolvedFilename = this->resolvedFilename();

    auto options = _options;

    auto protocolPrefixPosition = resolvedFilename.find("://");

    if (protocolPrefixPosition != std::string::npos)
    {
        resolvedFilename = resolvedFilename.substr(protocolPrefixPosition + 3);
    }

    if (resolvedFilename.find("./") == 0u)
    {
        resolvedFilename = resolvedFilename.substr(2u);
    }

    _options = options;

    SDL_RWops* file = SDL_RWFromFile(resolvedFilename.c_str(), "rb");

    auto loader = shared_from_this();

    if (file)
    {
        if (_options->loadAsynchronously() &&
            AbstractCanvas::defaultCanvas() != nullptr &&
            AbstractCanvas::defaultCanvas()->isWorkerRegistered("apk-protocol"))
        {
            file->close(file);

            auto worker = AbstractCanvas::defaultCanvas()->getWorker("apk-protocol");
            auto instance = std::static_pointer_cast<APKProtocol>(shared_from_this());

            _workerSlot = worker->message()->connect(
                [this, instance](async::Worker::Ptr, async::Worker::Message message)
                {
                    if (message.type == "complete")
                    {
                        data().assign(message.data.begin(), message.data.end());

                        complete()->execute(instance);

                        _activeInstances.erase(instance);

                        _workerSlot = nullptr;
                    }
                    else if (message.type == "progress")
                    {
                        // FIXME
                    }
                    else if (message.type == "error")
                    {
                        error()->execute(instance);

                        _activeInstances.erase(instance);

                        _workerSlot = nullptr;
                    }
                }
            );

            auto offset = options->seekingOffset();
            auto length = options->seekedLength();

            std::stringstream inputStream;

            inputStream.write(reinterpret_cast<const char*>(&offset), 4u);
            inputStream.write(reinterpret_cast<const char*>(&length), 4u);
            inputStream.write(resolvedFilename.data(), resolvedFilename.size());

            const auto input = inputStream.str();

            worker->start(std::vector<char>(input.begin(), input.end()));
        }
        else
        {
            auto offset = options->seekingOffset();
            auto size = options->seekedLength() > 0 ? options->seekedLength() : file->size(file);

            _progress->execute(shared_from_this(), 0.0);

            data().resize(size);

            file->seek(file, offset, RW_SEEK_SET);
            file->read(file, (char*) &data()[0], size, 1);
            file->close(file);

            _progress->execute(loader, 1.0);

            _complete->execute(shared_from_this());
        }
    }
    else
    {
        _error->execute(shared_from_this());
    }
}
Ejemplo n.º 9
0
	void Map::ParseFile(const string &fileName)
	{
		file_name = fileName;

		int lastSlash = fileName.find_last_of("/");

		// Get the directory of the file using substring.
		if (lastSlash > 0)
		{
			file_path = fileName.substr(0, lastSlash + 1);
		}
		else
		{
			file_path = "";
		}

		char* fileText;
		int fileSize;

		// Open the file for reading.
#ifdef USE_SDL2_LOAD
		SDL_RWops * file = SDL_RWFromFile (fileName.c_str(), "rb");
#else
		FILE *file = fopen(fileName.c_str(), "rb");
#endif

		// Check if the file could not be opened.
		if (!file)
		{
			has_error = true;
			error_code = TMX_COULDNT_OPEN;
			error_text = "Could not open the file.";
			return;
		}

		// Find out the file size.
#ifdef USE_SDL2_LOAD
		fileSize = file->size(file);
#else
		fseek(file, 0, SEEK_END);
		fileSize = ftell(file);
		fseek(file, 0, SEEK_SET);
#endif

		// Check if the file size is valid.
		if (fileSize <= 0)
		{
			has_error = true;
			error_code = TMX_INVALID_FILE_SIZE;
			error_text = "The size of the file is invalid.";
			return;
		}

		// Allocate memory for the file and read it into the memory.
		fileText = new char[fileSize + 1];
		fileText[fileSize] = 0;
#ifdef USE_SDL2_LOAD
		file->read(file, fileText, 1, fileSize);
#else
		fread(fileText, 1, fileSize, file);
#endif

#ifdef USE_SDL2_LOAD
		file->close(file);
#else
		fclose(file);
#endif

		// Copy the contents into a C++ string and delete it from memory.
		std::string text(fileText, fileText+fileSize);
		delete [] fileText;

		ParseText(text);
	}