Example #1
0
static void cleanUpThreadedSoundLoadData( ThreadedSoundLoadData* data )
{
	mem_Release( data->loadConverter.buf );
	data->loadConverter.buf = NULL;

	mem_Release( data );
}
Example #2
0
//********** Diagnostic Functions
void shaders_ListUniforms( GLuint shaderID )
{
	GLint count;
	GLint maxSize;
	GL( glGetProgramiv( shaderID, GL_ACTIVE_UNIFORMS, &count) );
	llog( LOG_INFO, "Uniforms for program %i:\n", shaderID );
	if( count == 0 ) {
		llog( LOG_INFO, "    none\n" );
	}

	GL( glGetProgramiv( shaderID, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxSize ) );
	char* uniformName = (char*)mem_Allocate( maxSize );
	if( uniformName == NULL ) {
		llog( LOG_WARN, "Error allocating memory for listing shader uniforms.\n" );
	}

	GLsizei length;
	GLint size;
	GLenum type;
	for( GLint i = 0; i < count; ++i ) {		
		GL( glGetActiveUniform( shaderID, i, maxSize, &length, &size, &type, uniformName ) );
		llog( LOG_INFO, "   %s\n", uniformName );
	}

	mem_Release( uniformName );
}
Example #3
0
void page_aligned_free(void* p, size_t size)
{
	if(!p)
		return;
	ENSURE(IsAligned(p, pageSize));
	const size_t alignedSize = Align<pageSize>(size);
	(void)mem_Release((u8*)p, alignedSize);
}
Example #4
0
// Closes the file and cleans up after it. Should always be called.
void cfg_CloseFile( void* cfgFile )
{
	assert( cfgFile != NULL );
	CFGFile* data = (CFGFile*)cfgFile;
	if( ( data == NULL ) || ( data->sbAttributes == NULL  ) ) {
		return;
	}

	sb_Release( data->sbAttributes );
	mem_Release( data );
}
Example #5
0
static void bindImageJob( void* data )
{
	if( data == NULL ) {
		llog( LOG_INFO, "No data, unable to bind." );
		return;
	}

	ThreadedLoadImageData* loadData = (ThreadedLoadImageData*)data;

	(*(loadData->outIdx)) = -1;

	if( loadData->loadedImage.data == NULL ) {
		llog( LOG_INFO, "Failed to load image %s", loadData->fileName );
		goto clean_up;
	}

	// find the first empty spot, make sure we won't go over our maximum
	int newIdx = findAvailableImageIndex( );
	if( newIdx < 0 ) {
		llog( LOG_INFO, "Unable to bind image %s! Image storage full.", loadData->fileName );
		goto clean_up;
	}

	Texture texture;
	if( gfxUtil_CreateTextureFromLoadedImage( GL_RGBA, &( loadData->loadedImage ), &texture ) < 0 ) {
		llog( LOG_INFO, "Unable to bind image %s!", loadData->fileName );
		goto clean_up;
	}

	images[newIdx].textureObj = texture.textureID;
	images[newIdx].size.v[0] = (float)texture.width;
	images[newIdx].size.v[1] = (float)texture.height;
	images[newIdx].offset = VEC2_ZERO;
	images[newIdx].packageID = -1;
	images[newIdx].flags = IMGFLAG_IN_USE;
	images[newIdx].nextInPackage = -1;
	images[newIdx].uvMin = VEC2_ZERO;
	images[newIdx].uvMax = VEC2_ONE;
	images[newIdx].shaderType = loadData->shaderType;
	if( texture.flags & TF_IS_TRANSPARENT ) {
		images[newIdx].flags |= IMGFLAG_HAS_TRANSPARENCY;
	}
	(*(loadData->outIdx)) = newIdx;

	llog( LOG_INFO, "Setting outIdx to %i", newIdx );

clean_up:
	gfxUtil_ReleaseLoadedImage( &( loadData->loadedImage ) );
	mem_Release( loadData );
}
Example #6
0
void snd_CleanUp( )
{
	snd_StopStreamingAllBut( -1 );
	if( workingBuffer != NULL ) {
		SDL_LockAudioDevice( devID ); {
			sb_Release( sbStreamWorkingBuffer );
			mem_Release( workingBuffer );
			workingBuffer = NULL;
		} SDL_UnlockAudioDevice( devID );
	}

	if( devID == 0 ) return;
	SDL_CloseAudioDevice( devID );
}
Example #7
0
static GLuint compileShader( const char* text, GLenum type )
{
	GLchar *shaderSourceStrs[1];
	GLint testVal;
	GLuint shaderID = 0;

	llog( LOG_VERBOSE, "Compiling shader" );

	if( text != NULL ) {
		GLR( shaderID, glCreateShader( type ) );
		llog( LOG_VERBOSE, "-- Created shader ID: %u", shaderID );

		if( shaderID != 0 ) {
			shaderSourceStrs[0] = (GLchar*)text;
			llog( LOG_VERBOSE, "-- Setting source and compiling" );
			GL( glShaderSource( shaderID, 1, (const GLchar**)shaderSourceStrs, NULL ) );
			GL( glCompileShader( shaderID ) );

			// now check for errors
			GL( glGetShaderiv( shaderID, GL_COMPILE_STATUS, &testVal ) );
			llog( LOG_VERBOSE, "-- GL_COMPILE_STATUS: %i", testVal );

			GLint logSize = 0;
			GL( glGetShaderiv( shaderID, GL_INFO_LOG_LENGTH, &logSize ) );
			if( logSize > 1 ) {
				GLchar* errorStr = (GLchar*)mem_Allocate( sizeof(GLchar) * logSize );
				if( errorStr != NULL ) {
					GL( glGetShaderInfoLog( shaderID, (GLsizei)logSize, NULL, errorStr ) );
					llog( LOG_ERROR, "Error compiling shader:\n - %s", errorStr );
					mem_Release( errorStr );
				} else {
					llog( LOG_ERROR, "Error allocating memory for error string" );
				}
			}

			if( testVal == GL_FALSE ) {
				llog( LOG_ERROR, "Error compiling shader" );

				GL( glDeleteShader( shaderID ) );
				shaderID = 0;
			}
		} else {
			llog( LOG_ERROR, "glCreateShader had a problem creating a shader." );
		}
	}

	llog( LOG_VERBOSE, "Returning shader: %u", shaderID );

	return shaderID;
}
Example #8
0
void snd_UnloadSample( int sampleID )
{
	assert( sampleID >= 0 );
	assert( sampleID < MAX_SAMPLES );

	if( samples[sampleID].data == NULL ) {
		return;
	}

	SDL_LockAudioDevice( devID ); {
		// find all playing sounds using this sample and stop them
		for( EntityID id = idSet_GetFirstValidID( &playingIDSet ); id != INVALID_ENTITY_ID; id = idSet_GetNextValidID( &playingIDSet, id ) ) {
			int idx = idSet_GetIndex( id );
			if( playingSounds[idx].sample == sampleID ) {
				idSet_ReleaseID( &playingIDSet, id );
			}
		}

		mem_Release( samples[sampleID].data );
		samples[sampleID].data = NULL;
	} SDL_UnlockAudioDevice( devID );
}
Example #9
0
/*
Loads the image in a seperate thread. Puts the resulting image index into outIdx.
 Returns -1 if there was an issue, 0 otherwise.
*/
void img_ThreadedLoad( const char* fileName, ShaderType shaderType, int* outIdx )
{
	// set it to something that won't draw anything
	(*outIdx) = -1;

	// this isn't something that should be happening all the time, so allocating and freeing
	//  should be fine, if we want to do continous streaming it would probably be better to
	//  make a specialty queue for it
	ThreadedLoadImageData* data = mem_Allocate( sizeof( ThreadedLoadImageData ) );
	if( data == NULL ) {
		llog( LOG_WARN, "Unable to create data for threaded image load for file %s", fileName );
		return;
	}

	data->fileName = fileName;
	data->shaderType = shaderType;
	data->outIdx = outIdx;
	data->loadedImage.data = NULL;

	if( !jq_AddJob( loadImageJob, data ) ) {
		mem_Release( data );
	}
}
Example #10
0
int snd_LoadSample( const char* fileName, Uint8 desiredChannels, bool loops )
{
	assert( ( desiredChannels >= 1 ) && ( desiredChannels <= 2 ) );

	int newIdx = -1;
	for( int i = 0; ( i < ARRAY_SIZE( samples ) ) && ( newIdx < 0 ); ++i ) {
		if( samples[i].data == NULL ) {
			newIdx = i;
		}
	}

	if( newIdx < 0 ) {
		llog( LOG_ERROR, "Unable to find free space for sample." );
		return -1;
	}

	// read the entire file into memory and decode it
	int channels;
	int rate;
	short* data;
	int numSamples = stb_vorbis_decode_filename( fileName, &channels, &rate, &data );

	// convert it
	SDL_AudioCVT loadConverter;
	if( SDL_BuildAudioCVT( &loadConverter,
		AUDIO_S16, (Uint8)channels, rate,
		WORKING_FORMAT, desiredChannels, WORKING_RATE ) < 0 ) {
		llog( LOG_ERROR, "Unable to create converter for sound." );
		newIdx = -1;
		goto clean_up;
	}

	loadConverter.len = numSamples * channels * sizeof( data[0] );
	size_t totLen = loadConverter.len * loadConverter.len_mult;
	if( loadConverter.len_mult > 1 ) {
		data = mem_Resize( data, loadConverter.len * loadConverter.len_mult ); // need to make sure there's enough room
		if( data == NULL ) {
			llog( LOG_ERROR, "Unable to allocate more memory for converting." );
			newIdx = -1;
			goto clean_up;
		}
	}
	loadConverter.buf = (Uint8*)data;

	if( SDL_ConvertAudio( &loadConverter ) < 0 ) {
		llog( LOG_ERROR, "Unable to convert sound: %s", SDL_GetError( ) );
		newIdx = -1;
		goto clean_up;
	}

	// store it
	samples[newIdx].data = mem_Allocate( loadConverter.len_cvt );

	memcpy( samples[newIdx].data, loadConverter.buf, loadConverter.len_cvt );

	samples[newIdx].numChannels = desiredChannels;
	samples[newIdx].numSamples = loadConverter.len_cvt / ( desiredChannels * ( ( SDL_AUDIO_MASK_BITSIZE & WORKING_FORMAT ) / 8 ) );
	samples[newIdx].loops = loops;

clean_up:
	// clean up the working data
	mem_Release( data );
	return newIdx;
}
Example #11
0
void jrq_CleanUp( JobRingQueue* queue )
{
	assert( queue != NULL );

	mem_Release( queue->ringBuffer );
}
Example #12
0
/* You create an array of ShaderDefinitions and ShaderProgramDefinitions that determine what is loaded.
You also create an empty array of GLuints to use as the final indices for the generated shader programs.
Returns the number of shader programs successfully created. */
size_t shaders_Load( const ShaderDefinition* shaderDefs, size_t numShaderDefs,
				const ShaderProgramDefinition* shaderProgDefs,
				ShaderProgram* shaderPrograms, size_t numShaderPrograms )
{
	llog( LOG_VERBOSE, "Loading shaders - definitions: %u  programs: %u", numShaderDefs, numShaderPrograms );
	size_t numSuccessful = 0;
	size_t i;

	// zero out, this also sets all the programs to invalid
	memset( shaderPrograms, 0, sizeof(GLuint) * numShaderPrograms );

	numShaders = numShaderDefs;
	shaders = (struct Shader*)mem_Allocate( sizeof(struct Shader) * numShaders );
	if( shaders == NULL ) {
		llog( LOG_ERROR, "Problem allocating memory for shaders." );
		return 0;
	}

	// first load all the individual shaders
	for( i = 0; i < numShaderDefs; ++i ) {
		shaders[i] = loadShader( shaderDefs + i );
	}

	// then set up the shader
	for( i = 0; i < numShaderPrograms; ++i ) {
		shaderPrograms[i].programID = createShaderProgram( shaderProgDefs+i, i );
		if( shaderPrograms[i].programID != 0 ) {
			++numSuccessful;
			llog( LOG_VERBOSE, "Successfully created shader program %i: %u", i, shaderPrograms[i].programID );
			// load the positions for the uniform and inputs
			size_t defIdx = 0;
			if( shaderPrograms[i].uniformLocs != NULL ) {
				llog( LOG_VERBOSE, "Finding shader program uniforms" );
				size_t namesLen = strlen( shaderProgDefs[i].uniformNames ) + 1;
				char namesCopy[256];
				if( namesLen < sizeof( namesCopy ) ) {
					memcpy( namesCopy, shaderProgDefs[i].uniformNames, namesLen );
					char* token = strtok( namesCopy, " " );
			
					while( token != NULL ) {
						GLR( shaderPrograms[i].uniformLocs[defIdx], glGetUniformLocation( shaderPrograms[i].programID, token ) );
						llog( LOG_VERBOSE, "-- Found uniform %u %s at %i", defIdx, token, shaderPrograms[i].uniformLocs[defIdx] );
						token = strtok( NULL, " " );
						++defIdx;
					}
				} else {
					llog( LOG_ERROR, "Error copying uniform name string for parsing shader program definition %d. String too long.", i );
				}

				llog( LOG_VERBOSE, "Found %u uniforms", defIdx );
			}

			while( defIdx < ARRAY_SIZE( shaderPrograms[i].uniformLocs ) ) {
				shaderPrograms[i].uniformLocs[defIdx] = -1;
				++defIdx;
			}
		}
	}

	// don't need the shaders any more
	llog( LOG_VERBOSE, "Deleting shaders" );
	for( i = 0; i < numShaderDefs; ++i ) {
		if( isValidShader( i, shaders ) ) {
			GL( glDeleteShader( shaders[i].id ) );
		}
	}
	llog( LOG_VERBOSE, "Freeing shaders" );
	mem_Release( shaders );

	return numSuccessful;
}
Example #13
0
/*
This opens up the sprite sheet file and loads all the images, putting the ids into imgOutArray. The returned array
 uses the stretchy buffer file, so you can use that to find the size, but you shouldn't do anything that modifies
 the size of it.
 Returns the number of images loaded if it was successful, otherwise returns -1.
*/
int img_LoadSpriteSheet( char* fileName, ShaderType shaderType, int** imgOutArray )
{
    int returnVal = 0;
    Vector2* mins = NULL;
    Vector2* maxes = NULL;
    char* fileText = NULL;

    char buffer[512];
    SDL_RWops* rwopsFile = SDL_RWFromFile( fileName, "r" );
    if( rwopsFile == NULL ) {
        returnVal = -1;
        SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "Unable to open sprite sheet definition file: %s", fileName );
        goto clean_up;
    }

    // format version 1
    //  version#
    //  image file name
    //  sprite count
    //  sprite rectangles
    //  final blank line
    int version;
    int numSprites = 0;
    int numSpritesRead = 0;

    ReadState currentState = RS_VERSION;

    // first read in the text from the file, should never be too large
    int readAmt;
    while( ( readAmt = SDL_RWread( rwopsFile, (void*)buffer, sizeof( char ), ( sizeof( buffer ) / sizeof( buffer[0] ) ) ) ) != 0 ) {
        char* c = sb_Add( fileText, readAmt );
        for( int i = 0; i < readAmt; ++i ) {
            *c++ = buffer[i];
        }
    }
    sb_Push( fileText, 0 );

    const char* delim = "\r\n";
    char* line = strtok( fileText, delim );
    char* fileNameLoc;
    char imgFileName[256] = { 0 };

    // now go through individual lines, parsing stuff as necessary
    while( line != NULL ) {
        switch( currentState ) {
        case RS_VERSION:
            version = strtol( line, NULL, 10 );
            currentState = RS_FILENAME;
            break;
        case RS_FILENAME:
            // assuming the file name will be local the .ss file, so we need to rip the directory off it and
            //  append the file name to it
            // line will be the file name for the image, attempt to create the texture
            strncpy( imgFileName, fileName, ( sizeof( imgFileName ) / sizeof( imgFileName[0] ) ) - 1 );
            fileNameLoc = strrchr( imgFileName, '/' );

            if( fileNameLoc != NULL ) {
                ++fileNameLoc;
                strncpy( fileNameLoc, line, ( ( imgFileName + 255 ) - fileNameLoc ) );
            }
            currentState = RS_COUNT;
            break;
        case RS_COUNT:
            numSprites = strtol( line, NULL, 10 );
            if( numSprites == 0 ) {
                SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "Invalid number of sprites in sprite sheet definition file: %s", fileName );
                returnVal = -1;
                goto clean_up;
            } else {
                if( ( mins = mem_Allocate( sizeof( Vector2 ) * numSprites ) ) == NULL ) {
                    SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "Unable to allocate minimums array for sprite sheet definition file: %s", fileName );
                    returnVal = -1;
                    goto clean_up;
                }

                if( ( maxes = mem_Allocate( sizeof( Vector2 ) * numSprites ) ) == NULL ) {
                    SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "Unable to allocate maximums array for sprite sheet definition file: %s", fileName );
                    returnVal = -1;
                    goto clean_up;
                }

                if( ( (*imgOutArray) = mem_Allocate( sizeof( int ) * numSprites ) ) == NULL ) {
                    SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "Unable to allocate image IDs array for sprite sheet definition file: %s", fileName );
                    returnVal = -1;
                    goto clean_up;
                }
            }
            numSpritesRead = 0;
            currentState = RS_SPRITES;
            break;
        case RS_SPRITES:
            // x, y, w, h
            mins[numSpritesRead].x = (float)strtol( line, &line, 10 );
            mins[numSpritesRead].y = (float)strtol( line, &line, 10 );
            maxes[numSpritesRead].x = mins[numSpritesRead].x + ( (float)strtol( line, &line, 10 ) );
            maxes[numSpritesRead].y = mins[numSpritesRead].y + ( (float)strtol( line, &line, 10 ) );
            ++numSpritesRead;

            if( numSpritesRead >= numSprites ) {
                currentState = RS_FINISHED;
            }
            break;
        }
        line = strtok( NULL, delim );
    }

    if( currentState != RS_FINISHED ) {
        mem_Release( *imgOutArray );
        returnVal = -1;
        SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "Problem reading sprite sheet definition file: %s", fileName );
        goto clean_up;
    }

    // now go through and create all the images
    if( img_SplitImageFile( imgFileName, numSpritesRead, shaderType, mins, maxes, (*imgOutArray) ) < 0 ) {
        mem_Release( *imgOutArray );
        returnVal = -1;
        SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "Problem splitting image for sprite sheet definition file: %s", fileName );
        goto clean_up;
    }

    returnVal = numSpritesRead;

clean_up:

    sb_Release( fileText );

    mem_Release( mins );
    mem_Release( maxes );

    if( rwopsFile != NULL ) {
        SDL_RWclose( rwopsFile );
    }

    return returnVal;
}