Exemplo n.º 1
0
static void CompileShaders()
{
	GLuint ShaderProgram = glCreateProgram();
	if (ShaderProgram == 0)
	{
		fprintf(stderr, "glCreateProgram() error\n");
		exit(1);
	}

	std::string vs, fs;
	if (!Util_ReadFile(pVSFileName, vs))
	{
		fprintf(stderr, "ReadFile(VS) fail, file = %s\n", pVSFileName);
		exit(1);
	}

	if (!Util_ReadFile(pFSFileName, fs))
	{
		fprintf(stderr, "ReadFile(FS) fail, file = %s\n", pFSFileName);
		exit(1);
	}

	AddShader(ShaderProgram, vs.c_str(), GL_VERTEX_SHADER);
	AddShader(ShaderProgram, fs.c_str(), GL_FRAGMENT_SHADER);


	GLint success = 0;
	GLchar log[1024] = { 0 };

	glLinkProgram(ShaderProgram);
	glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &success);
	if (success == 0)
	{
		glGetProgramInfoLog(ShaderProgram, sizeof(log), NULL, log);
		fprintf(stderr, "glLinkProgram fail: '%s'\n", log);
		exit(1);
	}

	glValidateProgram(ShaderProgram);
	glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &success);
	if (success == 0)
	{
		glGetProgramInfoLog(ShaderProgram, sizeof(log), NULL, log);
		fprintf(stderr, "glValidateProgram fail: '%s'\n", log);
		exit(1);
	}

	glUseProgram(ShaderProgram);

	gWorldLocation = glGetUniformLocation(ShaderProgram, "gWorld");
	assert(gWorldLocation != 0xFFFFFFFF);
}
Exemplo n.º 2
0
int
CONFLoad(const char* fileName)
{
    int i;
    int rv = CONF_OK;
    char* line = NULL;
    int length = 0;
    char* buffer = NULL;
    void* vbuffer = NULL;
    char* savePtr = NULL;

    LOG_DEBUG("CONFLoad %s", fileName);

    if (fileName == NULL) {
        LOG_ERROR("fileName = NULL");
        rv = CONF_ERROR_PARAMETER;
        goto out;
    }

    if (__confInit == 0) {
        LOG_ERROR("libconf not initialized");
        rv = CONF_ERROR_INIT;
        goto out;
    }

    LOG_INFO("Reading configuration from %s", fileName);
    length = Util_ReadFile(fileName, &vbuffer, 1);
    if (length < 0) {
        if (length == VPL_ERR_NOENT) {
            LOG_INFO("%s does not exist; skipping", fileName);
        } else {
            LOG_ERROR("Util_ReadFile(%s) failed: %d", fileName, length);
        }
        rv = length;
        goto out;
    }

    buffer = vbuffer;
    // Add a null-terminator so strtok doesn't read past the end of the buffer.
    buffer[length] = '\0';
    
    line = safe_strtok(buffer, "\n", &savePtr);
    while (line != NULL) {
        CONFVariable var;
        size_t lineLen = strlen(line);
        
        if (lineLen < 2 || (line[0] == '/' && line[1] == '/')) {
            goto skip;
        }

        memset(&var, 0, sizeof(var));
        sscanf(line, "%"VPL_STRING(CONF_MAX_STR_LENGTH)"s = %"VPL_STRING(CONF_MAX_STR_LENGTH)"s\n",
                var.name, var.value);
        LOG_DEBUG("name,value = '%s','%s'", var.name, var.value);

        // Check if the length of name and value are less than buffer size.
        // 3 is the length of ' = ' in configuration format.
        // Still regard as success but put an error in log.
        // Add 3 (instead of subtracting 3) to avoid wrapping around if lineLen == strlen(var.name),
        // which can happen if there is no ' = ' in the line.
        if ( (lineLen - strlen(var.name) > CONF_MAX_STR_LENGTH + 3) ||
             (lineLen - strlen(var.value) > CONF_MAX_STR_LENGTH + 3)   ) {
            LOG_ERROR("Configuration exceeds buffer size: '%s'", line);
        }

        for (i = 0; i < CONF_MAX_LENGTH; i++) {
            var.name[i] = tolower(var.name[i]);
        }

        // overwrite duplicate entries
        if (CONFOverWriteDuplicate(&var) == CONF_ERROR_NOT_FOUND) {
            memcpy(&__confVars[__confFilled], &var, sizeof(var));
            __confFilled++;
        }

        // grow array
        if (__confFilled >= __confSize) {
            CONFVariable* tempVars;
            __confSize *= 2;
            tempVars = __confVars;
            __confVars = (CONFVariable*)malloc(__confSize * sizeof(CONFVariable));
            memset(__confVars, 0, __confSize * sizeof(CONFVariable));
            memcpy(__confVars, tempVars, __confFilled * sizeof(CONFVariable));
            free(tempVars);
        }

skip:
        line = safe_strtok(NULL, "\n", &savePtr);
    }

    qsort(__confVars, __confFilled, sizeof(CONFVariable), CONFCompare);

out:
    free(buffer);

    return rv;
}