Пример #1
0
extern void
resources_cleanup(void) {
    for (int i = 0; i < MAX_TEXTURES; i++)
        if (all_resources.textures[i].id != 0)
            free_texture(&all_resources.textures[i]);

    for (int i = 0; i < MAX_SAMPLERS; i++)
        if (all_resources.samplers[i].id != 0)
            free_sampler(&all_resources.samplers[i]);

    for (int i = 0; i < MAX_RENDERBUFFERS; i++)
        if (all_resources.renderbuffers[i].id)
            free_renderbuffer(&all_resources.renderbuffers[i]);

    for (int i = 0; i < MAX_FRAMEBUFFERS; i++)
        if (all_resources.framebuffers[i].id)
            free_framebuffer(&all_resources.framebuffers[i]);

    for (int i = 0; i < MAX_BUFFERS; i++)
        if (all_resources.buffers[i].id)
            free_video_buffer(&all_resources.buffers[i]);

    for (int i = 0; i < MAX_VERTEX_ARRAYS; i++)
        if (all_resources.arrays[i].id)
            free_vertex_array(&all_resources.arrays[i]);

    for (int i = 0; i < MAX_SHADERS; i++)
        free_shader(&all_resources.shaders[i]);        
}
Пример #2
0
/*
--------------------------------------------------------------------------------
                           CREATE TEXTURE FROM STRING
--------------------------------------------------------------------------------
 *  This method creates a texture from a string of text using SDL_ttf.
*/
bool Texture::create_texture_from_string( TTF_Font *font, const char *string,
        SDL_Color textColor )
{
    /*  Free texture if it exists */
    free_texture();

    /*  Create a surface from rendered text */
    SDL_Surface *tempSurface = TTF_RenderText_Blended( font, string, textColor);
    if( tempSurface == NULL )
    {
        printf("ERROR:  Could not create rendered text.  TTF Error:  %s\n",
                TTF_GetError() );
        return( false );
    }

    /*  Create a texture from the loaded surface */
    mTexture = SDL_CreateTextureFromSurface( gRenderer, tempSurface );
    if( mTexture == NULL )
    {
        printf("ERROR:  Cannot create texture from surface.  SDL Error:  %s\n",
                SDL_GetError() );
        SDL_FreeSurface( tempSurface );
        return( false );
    }

    /*  Get dimensions from surface */
    mTextureWidth = mWidth = tempSurface->w;
    mTextureHeight = mHeight = tempSurface->h;

    /*  Free surface */
    SDL_FreeSurface( tempSurface );

    /*  We're good */
    return( true );
}
Пример #3
0
void draw_text(Alignment align, float x, float y, const char *text, TTF_Font *font) {
	char *nl;
	char *buf = malloc(strlen(text)+1);
	strcpy(buf, text);
	
	if((nl = strchr(buf, '\n')) != NULL && strlen(nl) > 1) {
		draw_text(align, x, y + 20, nl+1, font);
		*nl = '\0';
	}
		
	Texture *tex = load_text(buf, font);
	
	switch(align) {
	case AL_Center:
		draw_texture_p(x, y, tex);
		break;
	case AL_Left:
		draw_texture_p(x + tex->w/2.0, y, tex);
		break;
	case AL_Right:
		draw_texture_p(x - tex->w/2.0, y, tex);
		break;
	}
		
	free_texture(tex);
	free(buf);
}
Пример #4
0
    bool Image32::load_pixels_from_file_32(const char *path)
    {
        // free texture
        free_texture();
        
        // generate image
        ILuint image_id = 0;
        ilGenImages(1, &image_id);
        ilBindImage(image_id);

        //Load image
        bool loaded_image = ilLoadImage( path );
        
        if (loaded_image) {
            
            // convert image
            bool converted = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
            
            if (converted == IL_TRUE)
            {
                // init dimensions
                GLuint image_width  = (GLuint)ilGetInteger(IL_IMAGE_WIDTH);
                GLuint image_height = (GLuint)ilGetInteger(IL_IMAGE_HEIGHT);
                
                GLuint texture_width = power_of_two(image_width);
                GLuint texture_height = power_of_two(image_height);
                
                if (image_width != texture_width || image_height != texture_height)
                {
                    // place image in upper left
                    iluImageParameter(ILU_PLACEMENT, ILU_UPPER_LEFT);
                    
                    // resize image
                    iluEnlargeCanvas((int)texture_width, (int)texture_height, 1);
                }
                
                // allocate memory for texture
                GLuint size = texture_height*texture_width;
                _pixels_32 = new GLuint[size];
                
                _image_width    = image_width;
                _image_height   = image_height;
                _texture_width  = texture_width;
                _texture_height = texture_height;
                
                memcpy(_pixels_32, ilGetData(), size*4);
                
                
            }
            ilDeleteImages(1,&image_id);
            
        } else {
            std::cout << "unable to load pixels from file: " << path << std::endl;
            return false;
        }

        _pixel_format = GL_RGBA;
        return true;
    }
Пример #5
0
void freetype_font::clear_cache()
{
    for(cached_text* pEntry = cache;
        pEntry != cache + (1 << cache_size_log2); ++pEntry)
    {
        pEntry->is_valid = false;
        free_texture(pEntry);
    }
}
Пример #6
0
void endSharedState(GLContext *c) {
	GLSharedState *s = &c->shared_state;

	free_texture(c, 0);
	for (int i = 0; i < MAX_DISPLAY_LISTS; i++) {
		// TODO
	}
	gl_free(s->lists);

	gl_free(s->texture_hash_table);
}
Пример #7
0
 void Image32::create_pixels_32(GLuint image_width, GLuint image_height)
 {
     if (image_height > 0 && image_width > 0) {
         free_texture();
         
         GLuint size = image_width*image_height;
         _pixels_32 = new GLuint[size];
         
         _image_height = image_height;
         _image_width = image_width;
         
         _texture_height = image_height;
         _texture_width = image_width;
     }
     _pixel_format = GL_RGBA;
 }
Пример #8
0
void glDeleteTextures(int n, const unsigned int *textures)
{
    GLContext *c=gl_get_context();
    int i;
    GLTexture *t;

    for(i=0;i<n;i++) {
        t=find_texture(c,textures[i]);
        if (t!=NULL && t!=0) {
            if (t==c->current_texture) {
                glBindTexture(GL_TEXTURE_2D,0);
            }
            free_texture(c,textures[i]);
        }
    }
}
Пример #9
0
 void Image32::copy_pixels_32(GLuint *pixels, GLuint image_width, GLuint image_height)
 {
     if (image_height > 0 && image_width > 0) {
         free_texture();
         
         GLuint size = image_width*image_height;
         _pixels_32 = new GLuint[size];
         memcpy(_pixels_32, pixels, size*4);
         
         _image_height = image_height;
         _image_width = image_width;
         
         _texture_height = image_height;
         _texture_width = image_width;
         _pixel_format = GL_RGBA;
     }
 }
Пример #10
0
freetype_font::~freetype_font()
{
    for(cached_text* pEntry = cache;
        pEntry != cache + (1 << cache_size_log2); ++pEntry)
    {
        free_texture(pEntry);
    }
    if(font_face != nullptr)
        FT_Done_Face(font_face);
    if(is_done_freetype_init)
    {
        if(--freetype_init_count == 0)
        {
            FT_Done_FreeType(freetype_library);
            freetype_library = nullptr;
        }
    }
}
Пример #11
0
/*
--------------------------------------------------------------------------------
                            CREATE TEXTURE FROM FILE
--------------------------------------------------------------------------------
 *  This method creates a texture from an image file (presumably a PNG since
 *  SDL_image was initialized with only that functionality).  It optionally
 *  generates colliders for sprite sheet images.
*/
bool Texture::create_texture_from_file( const char *path, bool genCol )
{
    /*  Free the texture if it exists */
    free_texture();

    /*  Load a surface from the path provided */
    SDL_Surface *tempSurface = IMG_Load( path );
    if( tempSurface == NULL )
    {
        printf("ERROR:  Could not create surface from '%s':  IMG Error:  %s\n",
                path, IMG_GetError() );
        return( false );
    }

    /*  We save the pixel format for later use */
    if( gPixelFormat == 0 )
        gPixelFormat = (Uint32)tempSurface->format->format;

    /*  Now, create a texture from the loaded surface */
    mTexture = SDL_CreateTextureFromSurface( gRenderer, tempSurface );
    if( mTexture == NULL )
    {
        printf("ERROR:  Cannot create texture from surface.  SDL Error:  %s\n",
                SDL_GetError() );
        SDL_FreeSurface( tempSurface );
        return( false );
    }

    /*  Generate colliders */
    if( genCol )
        generate_colliders( tempSurface, tempSurface->w );

    /*  Get dimensions from surface */
    mWidth = mTextureWidth = tempSurface->w;
    mHeight = mTextureHeight = tempSurface->h;

    /*  Free loaded surface */
    SDL_FreeSurface( tempSurface );

    /*  If we're here, we're good */
    return( true );
}
Пример #12
0
 bool Image32::load_texture_from_pixels_32(GLuint* pixels, GLuint img_width, GLuint img_height, GLuint tex_width, GLuint tex_height)
 {
     free_texture(); // free textue if it exists
     
     _image_width    = img_width;
     _image_height   = img_height;
     _texture_width  = tex_width;
     _texture_height = tex_height;
     
     // generate texture
     glGenTextures(1, &_texture_id);
     
     // bind the gexture
     glBindTexture(GL_TEXTURE_2D, _texture_id);
     
     // set texture from pixels
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _texture_width, _texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
     
     // set texture params
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     
     // Unbind
     glBindTexture(GL_TEXTURE_2D, NULL);
     
     GLenum error = glGetError();
     if (error != GL_NO_ERROR) {
         std::cout << "unable to set pixels: " << gluErrorString(error) << std::endl;
         return false;
     }
     
     _pixel_format = GL_RGBA;
     init_vbo();
     return true;
 }
Пример #13
0
/* used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c */
void free_libblock(ListBase *lb, void *idv)
{
	ID *id= idv;

#ifdef WITH_PYTHON
	BPY_id_release(id);
#endif

	switch( GS(id->name) ) {	/* GetShort from util.h */
		case ID_SCE:
			free_scene((Scene *)id);
			break;
		case ID_LI:
			free_library((Library *)id);
			break;
		case ID_OB:
			free_object((Object *)id);
			break;
		case ID_ME:
			free_mesh((Mesh *)id);
			break;
		case ID_CU:
			free_curve((Curve *)id);
			break;
		case ID_MB:
			free_mball((MetaBall *)id);
			break;
		case ID_MA:
			free_material((Material *)id);
			break;
		case ID_TE:
			free_texture((Tex *)id);
			break;
		case ID_IM:
			free_image((Image *)id);
			break;
		case ID_LT:
			free_lattice((Lattice *)id);
			break;
		case ID_LA:
			free_lamp((Lamp *)id);
			break;
		case ID_CA:
			free_camera((Camera*) id);
			break;
		case ID_IP:
			free_ipo((Ipo *)id);
			break;
		case ID_KE:
			free_key((Key *)id);
			break;
		case ID_WO:
			free_world((World *)id);
			break;
		case ID_SCR:
			free_screen((bScreen *)id);
			break;
		case ID_VF:
			free_vfont((VFont *)id);
			break;
		case ID_TXT:
			free_text((Text *)id);
			break;
		case ID_SCRIPT:
			//XXX free_script((Script *)id);
			break;
		case ID_SPK:
			free_speaker((Speaker *)id);
			break;
		case ID_SO:
			sound_free((bSound*)id);
			break;
		case ID_GR:
			free_group_objects((Group *)id);
			break;
		case ID_AR:
			free_armature((bArmature *)id);
			break;
		case ID_AC:
			free_action((bAction *)id);
			break;
		case ID_NT:
			ntreeFreeTree((bNodeTree *)id);
			break;
		case ID_BR:
			free_brush((Brush *)id);
			break;
		case ID_PA:
			psys_free_settings((ParticleSettings *)id);
			break;
		case ID_WM:
			if(free_windowmanager_cb)
				free_windowmanager_cb(NULL, (wmWindowManager *)id);
			break;
		case ID_GD:
			free_gpencil_data((bGPdata *)id);
			break;
	}

	if (id->properties) {
		IDP_FreeProperty(id->properties);
		MEM_freeN(id->properties);
	}

	BLI_remlink(lb, id);

	/* this ID may be a driver target! */
	BKE_animdata_main_cb(G.main, animdata_dtar_clear_cb, (void *)id);

	MEM_freeN(id);
}
Пример #14
0
static void shader_preview_free(void *customdata)
{
	ShaderPreview *sp= customdata;
	
	if(sp->matcopy) {
		struct IDProperty *properties;
		int a;
		
		/* node previews */
		shader_preview_updatejob(sp);
		
		/* get rid of copied material */
		BLI_remlink(&pr_main->mat, sp->matcopy);
		
		/* free_material decrements texture, prevent this. hack alert! */
		for(a=0; a<MAX_MTEX; a++) {
			MTex *mtex= sp->matcopy->mtex[a];
			if(mtex && mtex->tex) mtex->tex= NULL;
		}
		
		free_material(sp->matcopy);

		properties= IDP_GetProperties((ID *)sp->matcopy, FALSE);
		if (properties) {
			IDP_FreeProperty(properties);
			MEM_freeN(properties);
		}
		MEM_freeN(sp->matcopy);
	}
	if(sp->texcopy) {
		struct IDProperty *properties;
		/* node previews */
		shader_preview_updatejob(sp);
		
		/* get rid of copied texture */
		BLI_remlink(&pr_main->tex, sp->texcopy);
		free_texture(sp->texcopy);
		
		properties= IDP_GetProperties((ID *)sp->texcopy, FALSE);
		if (properties) {
			IDP_FreeProperty(properties);
			MEM_freeN(properties);
		}
		MEM_freeN(sp->texcopy);
	}
	if(sp->worldcopy) {
		struct IDProperty *properties;
		/* node previews */
		shader_preview_updatejob(sp);
		
		/* get rid of copied world */
		BLI_remlink(&pr_main->world, sp->worldcopy);
		free_world(sp->worldcopy);
		
		properties= IDP_GetProperties((ID *)sp->worldcopy, FALSE);
		if (properties) {
			IDP_FreeProperty(properties);
			MEM_freeN(properties);
		}
		MEM_freeN(sp->worldcopy);
	}
	if(sp->lampcopy) {
		struct IDProperty *properties;
		/* node previews */
		shader_preview_updatejob(sp);
		
		/* get rid of copied lamp */
		BLI_remlink(&pr_main->lamp, sp->lampcopy);
		free_lamp(sp->lampcopy);
		
		properties= IDP_GetProperties((ID *)sp->lampcopy, FALSE);
		if (properties) {
			IDP_FreeProperty(properties);
			MEM_freeN(properties);
		}
		MEM_freeN(sp->lampcopy);
	}
	
	MEM_freeN(sp);
}
Пример #15
0
/*
--------------------------------------------------------------------------------
                                   DESTRUCTOR
--------------------------------------------------------------------------------
*/
Texture::~Texture( void )
{
    /*  All we need to do is free the texture */
    free_texture();
}