Example #1
0
static size_t LoadDDSFromFile(const std::string &filename, PicoDDS::DDSImage& dds)
{
	RefCountedPtr<FileSystem::FileData> filedata = FileSystem::gameDataFiles.ReadFile(filename);
	if (!filedata) {
		Output("LoadDDSFromFile: %s: could not read file\n", filename.c_str());
		return 0;
	}

	// read the dds file
	const size_t sizeRead = dds.Read( filedata->GetData(), filedata->GetSize() );
	return sizeRead;
}
Example #2
0
SDLSurfacePtr LoadSurfaceFromFile(const std::string &fname, FileSystem::FileSource &source)
{
	RefCountedPtr<FileSystem::FileData> filedata = FileSystem::gameDataFiles.ReadFile(fname);
	if (!filedata) {
		Output("LoadSurfaceFromFile: %s: could not read file\n", fname.c_str());
		return SDLSurfacePtr();
	}

	SDL_RWops *datastream = SDL_RWFromConstMem(filedata->GetData(), filedata->GetSize());
	SDL_Surface *surface = IMG_Load_RW(datastream, 1);
	if (!surface) {
		Output("LoadSurfaceFromFile: %s: %s\n", fname.c_str(), IMG_GetError());
		return SDLSurfacePtr();
	}

	return SDLSurfacePtr::WrapNew(surface);
}
Example #3
0
LuaManager::LuaManager() : m_lua(NULL) {
	if (instantiated) {
		fprintf(stderr, "Can't instantiate more than one LuaManager");
		abort();
	}

	m_lua = lua_open();

	luaL_openlibs(m_lua);

	lua_atpanic(m_lua, pi_lua_panic);

	RefCountedPtr<FileSystem::FileData> code = FileSystem::gameDataFiles.ReadFile(luaDebugFilename);
	if (!code) {
		fprintf(stderr, "could not read Lua file '%s'\n", luaDebugFilename.c_str());
		abort();
	}

	int ret = luaL_loadbuffer(m_lua, code->GetData(), code->GetSize(), code->GetInfo().GetPath().c_str());
	if (ret) {
		if (ret == LUA_ERRSYNTAX) {
			const char* message = lua_tostring(m_lua, -1);
			fprintf(stderr, "Syntax error in '%s:\n%s\n", code->GetInfo().GetAbsolutePath().c_str(), message);
		}
		else
			fprintf(stderr, "Error while loading '%s'\n", code->GetInfo().GetAbsolutePath().c_str());
		abort();
	}
	if (lua_pcall(m_lua, 0, 1, 0)) {
		fprintf(stderr, "Fatal Lua error: pidebug.lua failed to initialise.");
		abort();
	}
	if (lua_type(m_lua, -1) != LUA_TTABLE) {
		fprintf(stderr, "Fatal Lua error: pidebug.lua did not return module table.");
		abort();
	}
	lua_getfield(m_lua, -1, "error_handler");
	if (lua_type(m_lua, -1) != LUA_TFUNCTION) {
		fprintf(stderr, "Fatal Lua error: pidebug.lua did not define error_handler function.");
		abort();
	}
	lua_pop(m_lua, 1);
	lua_setfield(m_lua, LUA_REGISTRYINDEX, "PiDebug");

	instantiated = true;
}
Example #4
0
void TextureBuilder::LoadSurface()
{
	assert(!m_surface);

	RefCountedPtr<FileSystem::FileData> filedata = FileSystem::gameDataFiles.ReadFile(m_filename);
	if (!filedata) {
		fprintf(stderr, "TextureBuilder::CreateFromFile: %s: could not read file\n", m_filename.c_str());
		return;
	}

	SDL_RWops *datastream = SDL_RWFromConstMem(filedata->GetData(), filedata->GetSize());
	m_surface = IMG_Load_RW(datastream, 1);
	if (!m_surface) {
		fprintf(stderr, "TextureBuilder::CreateFromFile: %s: %s\n", m_filename.c_str(), IMG_GetError());
		//m_surface = IMG_Load(unknownTextureFilename.c_str());
		return;
	}
}
Example #5
0
Galaxy::Galaxy() : GALAXY_RADIUS(50000.0), SOL_OFFSET_X(25000.0), SOL_OFFSET_Y(0.0), m_galaxybmp(nullptr), m_factions(this), m_customSystems(this)
{
    static const std::string filename("galaxy.bmp");

    RefCountedPtr<FileSystem::FileData> filedata = FileSystem::gameDataFiles.ReadFile(filename);
    if (!filedata) {
        Output("Galaxy: couldn't load '%s'\n", filename.c_str());
        Pi::Quit();
    }

    SDL_RWops *datastream = SDL_RWFromConstMem(filedata->GetData(), filedata->GetSize());
    m_galaxybmp = SDL_LoadBMP_RW(datastream, 1);
    if (!m_galaxybmp) {
        Output("Galaxy: couldn't load: %s (%s)\n", filename.c_str(), SDL_GetError());
        Pi::Quit();
    }

    m_starSystemCache = m_starSystemAttic.NewSlaveCache();
}
Example #6
0
	Shader(GLenum type, const std::string &filename, const std::string &defines) 
	{
		RefCountedPtr<FileSystem::FileData> filecode = FileSystem::gameDataFiles.ReadFile(filename);

		if (!filecode.Valid())
			Error("Could not load %s", filename.c_str());

		std::string strCode(filecode->AsStringRange().ToString());
		size_t found = strCode.find("#include");
		while (found != std::string::npos) 
		{
			// find the name of the file to include
			const size_t begFilename = strCode.find_first_of("\"", found + 8) + 1;
			const size_t endFilename = strCode.find_first_of("\"", begFilename + 1);

			const std::string incFilename = strCode.substr(begFilename, endFilename - begFilename);

			// check we haven't it already included it (avoids circular dependencies)
			const std::set<std::string>::const_iterator foundIt = previousIncludes.find(incFilename);
			if (foundIt != previousIncludes.end()) {
				Error("Circular, or multiple, include of %s\n", incFilename.c_str());
			}
			else {
				previousIncludes.insert(incFilename);
			}

			// build path for include
			const std::string incPathBuffer = stringf("shaders/opengl/%0", incFilename);

			// read included file
			RefCountedPtr<FileSystem::FileData> incCode = FileSystem::gameDataFiles.ReadFile(incPathBuffer);
			assert(incCode.Valid());

			if (incCode.Valid()) {
				// replace the #include and filename with the included files text
				strCode.replace(found, (endFilename + 1) - found, incCode->GetData(), incCode->GetSize());
				found = strCode.find("#include");
			}
			else {
				Error("Could not load %s", incPathBuffer.c_str());
			}
		}
		// Store the modified text with the included files (if any)
		const StringRange code(strCode.c_str(), strCode.size());

		// Build the final shader text to be compiled
		AppendSource(s_glslVersion);
		AppendSource(defines.c_str());
		if (type == GL_VERTEX_SHADER) {
			AppendSource("#define VERTEX_SHADER\n");
		} else {
			AppendSource("#define FRAGMENT_SHADER\n");
		}
		AppendSource(code.StripUTF8BOM());
#if 0
		static bool s_bDumpShaderSource = true;
		if (s_bDumpShaderSource) {
			const char SHADER_OUT_DIR_NAME[] = "shaders";
			const char SHADER_OGL_OUT_DIR_NAME[] = "shaders/opengl";
			FileSystem::userFiles.MakeDirectory(SHADER_OUT_DIR_NAME);
			FileSystem::userFiles.MakeDirectory(SHADER_OGL_OUT_DIR_NAME);
			const std::string outFilename(FileSystem::GetUserDir() + "/" + filename);
			FILE *tmp = fopen(outFilename.c_str(), "wb");
			if(tmp) {
				Output("%s", filename);
				for( Uint32 i=0; i<blocks.size(); i++ ) {
					const char *block = blocks[i];
					const GLint sizes = block_sizes[i];
					if(block && sizes>0) {
						fprintf(tmp, "%.*s", sizes, block);
					}
				}
				fclose(tmp);
			} else {
				Output("Could not open file %s", outFilename.c_str());
			}
		}
#endif
		shader = glCreateShader(type);
		if(glIsShader(shader)!=GL_TRUE)
			throw ShaderException();

		Compile(shader);

		// CheckGLSL may use OS::Warning instead of Error so the game may still (attempt to) run
		if (!check_glsl_errors(filename.c_str(), shader))
			throw ShaderException();
	};
Example #7
0
bool Resource::Load()
{
	if (m_loaded)
		return true;

	Json::Reader reader;
	Json::Value data;

	std::string filename = "lang/" + m_name + "/" + m_langCode + ".json";
	RefCountedPtr<FileSystem::FileData> fd = FileSystem::gameDataFiles.ReadFile(filename);
	if (!fd) {
		Output("couldn't open language file '%s'\n", filename.c_str());
		return false;
	}

	if (!reader.parse(fd->GetData(), fd->GetData()+fd->GetSize(), data)) {
		Output("couldn't read language file '%s': %s\n", filename.c_str(), reader.getFormattedErrorMessages().c_str());
		return false;
	}

	fd.Reset();

	for (Json::Value::iterator i = data.begin(); i != data.end(); ++i) {
		const std::string token(i.key().asString());
		if (token.empty()) {
			Output("%s: found empty token, skipping it\n", filename.c_str());
			continue;
		}
		if (!valid_token(token)) {
			Output("%s: invalid token '%s', skipping it\n", filename.c_str(), token.c_str());
			continue;
		}

		Json::Value message((*i).get("message", Json::nullValue));
		if (message.isNull()) {
			Output("%s: no 'message' key for token '%s', skipping it\n", filename.c_str(), token.c_str());
			continue;
		}

		if (!message.isString()) {
			Output("%s: value for token '%s' is not a string, skipping it\n", filename.c_str(), token.c_str());
			continue;
		}

		std::string text(message.asString());
		if (text.empty()) {
			Output("%s: empty value for token '%s', skipping it\n", filename.c_str(), token.c_str());
			continue;
		}

		// extracted quoted string
		if (text[0] == '"' && text[text.size()-1] == '"')
			text = text.substr(1, text.size()-2);

		// adjust for escaped newlines
		{
			std::string adjustedText;
			adjustedText.reserve(text.size());

			unsigned int ii;
			for (ii = 0; ii < text.size()-1; ii++) {
				const char *c = &text[ii];
				if (c[0] == '\\' && c[1] == 'n') {
					ii++;
					adjustedText += '\n';
				}
				else
					adjustedText += *c;
			}
			if (ii != text.size())
				adjustedText += text[ii++];
			assert(ii == text.size());
			text = adjustedText;
		}

		m_strings[token] = text;
	}

	m_loaded = true;
	return true;
}