naev 0.12.6
opengl_tex.c File Reference

This file handles the opengl texture wrapper routines. More...

#include "array.h"
#include "distance_field.h"
#include "log.h"
#include "md5.h"
#include "nfile.h"
#include "opengl.h"
Include dependency graph for opengl_tex.c:

Go to the source code of this file.

Data Structures

struct  glTexList
 Represents a node in the texture list. More...

Functions

static uint8_t SDL_GetAlpha (SDL_Surface *s, int x, int y)
 Gets the alpha value of a pixel.
static int SDL_IsTrans (SDL_Surface *s, int x, int y)
 Checks to see if a position of the surface is transparent.
static USE_RESULT uint8_t * SDL_MapAlpha (SDL_Surface *s, int tight)
 Maps the surface transparency.
static size_t gl_transSize (const int w, const int h)
static USE_RESULT GLuint gl_texParameters (unsigned int flags)
 Sets default texture parameters.
static USE_RESULT GLuint gl_loadSurface (SDL_Surface *surface, unsigned int flags, int freesur, double *vmax)
 Loads a surface into an opengl texture.
static int gl_loadNewImage (glTexture *tex, const char *path, int sx, int sy, unsigned int flags)
 Only loads the image, does not add to stack unlike gl_newImage.
static int gl_loadNewImageRWops (glTexture *tex, const char *path, SDL_RWops *rw, int sx, int sy, unsigned int flags)
 The heavy loading image backend image function. It loads images and does transparency mapping if necessary.
static glTexturegl_texCreate (const char *path, int sx, int sy, unsigned int flags)
 Creates a new texture.
static int gl_texAdd (glTexture *tex, int sx, int sy, unsigned int flags)
 Adds a texture to the list under the name of path.
static int tex_cmp (const void *p1, const void *p2)
static void tex_ctxSet (void)
static void tex_ctxUnset (void)
void gl_contextSet (void)
void gl_contextUnset (void)
int gl_fboCreate (GLuint *fbo, GLuint *tex, GLsizei width, GLsizei height)
 Creates a framebuffer and its associated texture.
int gl_fboAddDepth (GLuint fbo, GLuint *tex, GLsizei width, GLsizei height)
 Adds a depth attachment to an FBO.
glTexturegl_loadImageData (float *data, int w, int h, int sx, int sy, const char *name)
USE_RESULT glTexturegl_texExistsOrCreate (const char *path, unsigned int flags, int sx, int sy, int *created)
 Check to see if a texture matching a path already exists.
glTexturegl_newImage (const char *path, const unsigned int flags)
 Loads an image as a texture.
glTexturegl_newImageRWops (const char *path, SDL_RWops *rw, const unsigned int flags)
 Loads an image as a texture.
glTexturegl_newSprite (const char *path, const int sx, const int sy, const unsigned int flags)
 Loads the texture immediately, but also sets it as a sprite.
glTexturegl_newSpriteRWops (const char *path, SDL_RWops *rw, const int sx, const int sy, const unsigned int flags)
 Loads the texture immediately, but also sets it as a sprite.
void gl_freeTexture (glTexture *texture)
 Frees a texture.
glTexturegl_dupTexture (const glTexture *texture)
 Duplicates a texture.
USE_RESULT glTexturegl_rawTexture (const char *name, GLuint texid, double w, double h)
 Creates a texture from a raw opengl index.
int gl_isTrans (const glTexture *t, const int x, const int y)
 Checks to see if a pixel is transparent in a texture.
void gl_getSpriteFromDir (int *x, int *y, int sx, int sy, double dir)
 Sets x and y to be the appropriate sprite for glTexture using dir.
int gl_initTextures (void)
 Initializes the opengl texture subsystem.
void gl_exitTextures (void)
 Cleans up the opengl texture subsystem.
glTexture ** gl_copyTexArray (glTexture **tex)
 Copy a texture array.
glTexture ** gl_addTexArray (glTexture **tex, glTexture *t)
 Adds an element to a texture array.

Variables

static glTexListtexture_list = NULL
static SDL_threadID tex_mainthread
static SDL_mutex * gl_lock = NULL
static SDL_mutex * tex_lock = NULL

Detailed Description

This file handles the opengl texture wrapper routines.

Definition in file opengl_tex.c.

Function Documentation

◆ gl_addTexArray()

glTexture ** gl_addTexArray ( glTexture ** tex,
glTexture * t )

Adds an element to a texture array.

Definition at line 1067 of file opengl_tex.c.

◆ gl_contextSet()

void gl_contextSet ( void )

Definition at line 84 of file opengl_tex.c.

◆ gl_contextUnset()

void gl_contextUnset ( void )

Definition at line 90 of file opengl_tex.c.

◆ gl_copyTexArray()

glTexture ** gl_copyTexArray ( glTexture ** tex)

Copy a texture array.

Definition at line 1050 of file opengl_tex.c.

◆ gl_dupTexture()

glTexture * gl_dupTexture ( const glTexture * texture)

Duplicates a texture.

Parameters
textureTexture to duplicate.
Returns
Duplicate of texture.

Definition at line 891 of file opengl_tex.c.

◆ gl_exitTextures()

void gl_exitTextures ( void )

Cleans up the opengl texture subsystem.

Definition at line 1023 of file opengl_tex.c.

◆ gl_fboAddDepth()

int gl_fboAddDepth ( GLuint fbo,
GLuint * tex,
GLsizei width,
GLsizei height )

Adds a depth attachment to an FBO.

Definition at line 313 of file opengl_tex.c.

◆ gl_fboCreate()

int gl_fboCreate ( GLuint * fbo,
GLuint * tex,
GLsizei width,
GLsizei height )

Creates a framebuffer and its associated texture.

Parameters
[out]fboFramebuffer object id.
[out]texTexture id.
widthWidth to use.
heightHeight to use.
Returns
0 on success.

Definition at line 268 of file opengl_tex.c.

◆ gl_freeTexture()

void gl_freeTexture ( glTexture * texture)

Frees a texture.

Parameters
textureTexture to free. (If NULL, function does nothing.)

Definition at line 835 of file opengl_tex.c.

◆ gl_getSpriteFromDir()

void gl_getSpriteFromDir ( int * x,
int * y,
int sx,
int sy,
double dir )

Sets x and y to be the appropriate sprite for glTexture using dir.

Very slow, try to cache if possible like the pilots do instead of using in O(n^2) or worse functions.

Parameters
[out]xX sprite to use.
[out]yY sprite to use.
sxNumber of sprites in X direction.
syNumber of sprites in Y direction.
dirDirection to get sprite from.

Definition at line 977 of file opengl_tex.c.

◆ gl_initTextures()

int gl_initTextures ( void )

Initializes the opengl texture subsystem.

Returns
0 on success.

Definition at line 1012 of file opengl_tex.c.

◆ gl_isTrans()

int gl_isTrans ( const glTexture * t,
const int x,
const int y )

Checks to see if a pixel is transparent in a texture.

Parameters
tTexture to check for transparency.
xX position of the pixel.
yY position of the pixel.
Returns
1 if the pixel is transparent or 0 if it isn't.

Definition at line 957 of file opengl_tex.c.

◆ gl_loadImageData()

glTexture * gl_loadImageData ( float * data,
int w,
int h,
int sx,
int sy,
const char * name )

Definition at line 352 of file opengl_tex.c.

◆ gl_loadNewImage()

int gl_loadNewImage ( glTexture * tex,
const char * path,
int sx,
int sy,
unsigned int flags )
static

Only loads the image, does not add to stack unlike gl_newImage.

Parameters
texTexture to load to.
pathImage to load.
sxX sprites to load.
syY sprites to load.
flagsFlags to control image parameters.
Returns
0 on success.

Definition at line 634 of file opengl_tex.c.

◆ gl_loadNewImageRWops()

int gl_loadNewImageRWops ( glTexture * tex,
const char * path,
SDL_RWops * rw,
int sx,
int sy,
unsigned int flags )
static

The heavy loading image backend image function. It loads images and does transparency mapping if necessary.

Parameters
texTexture to load to.
pathImage to load.
sxX sprites to load.
syY sprites to load.
rwSDL_Rwops structure to use to load.
flagsFlags to control image parameters.
Returns
0 on success.

Definition at line 668 of file opengl_tex.c.

◆ gl_loadSurface()

GLuint gl_loadSurface ( SDL_Surface * surface,
unsigned int flags,
int freesur,
double * vmax )
static

Loads a surface into an opengl texture.

Parameters
surfaceSurface to load into a texture.
flagsFlags to use.
freesurWhether or not to free the surface.
[out]vmaxThe maximum value in the case of an SDF texture.
Returns
The opengl texture id.

Definition at line 402 of file opengl_tex.c.

◆ gl_newImage()

glTexture * gl_newImage ( const char * path,
const unsigned int flags )

Loads an image as a texture.

May not necessarily load the image but use one if it's already open.

Parameters
pathImage to load.
flagsFlags to control image parameters.
Returns
Texture loaded from image.

Definition at line 587 of file opengl_tex.c.

◆ gl_newImageRWops()

glTexture * gl_newImageRWops ( const char * path,
SDL_RWops * rw,
const unsigned int flags )

Loads an image as a texture.

May not necessarily load the image but use one if it's already open.

Note
Does not close the SDL_RWops file.
Parameters
pathPath name used for checking cache and error reporting.
rwSDL_RWops structure to load from.
flagsFlags to control image parameters.
Returns
Texture loaded from image.

Definition at line 611 of file opengl_tex.c.

◆ gl_newSprite()

glTexture * gl_newSprite ( const char * path,
const int sx,
const int sy,
const unsigned int flags )

Loads the texture immediately, but also sets it as a sprite.

Parameters
pathImage to load.
sxNumber of X sprites in image.
syNumber of Y sprites in image.
flagsFlags to control image parameters.
Returns
Texture loaded.

Definition at line 785 of file opengl_tex.c.

◆ gl_newSpriteRWops()

glTexture * gl_newSpriteRWops ( const char * path,
SDL_RWops * rw,
const int sx,
const int sy,
const unsigned int flags )

Loads the texture immediately, but also sets it as a sprite.

Parameters
pathImage name for deduplication.
rwSDL_RWops structure to load for.
sxNumber of X sprites in image.
syNumber of Y sprites in image.
flagsFlags to control image parameters.
Returns
Texture loaded.

Definition at line 808 of file opengl_tex.c.

◆ gl_rawTexture()

USE_RESULT glTexture * gl_rawTexture ( const char * name,
GLuint texid,
double w,
double h )

Creates a texture from a raw opengl index.

Definition at line 917 of file opengl_tex.c.

◆ gl_texAdd()

int gl_texAdd ( glTexture * tex,
int sx,
int sy,
unsigned int flags )
static

Adds a texture to the list under the name of path.

Definition at line 555 of file opengl_tex.c.

◆ gl_texCreate()

glTexture * gl_texCreate ( const char * path,
int sx,
int sy,
unsigned int flags )
static

Creates a new texture.

Definition at line 483 of file opengl_tex.c.

◆ gl_texExistsOrCreate()

USE_RESULT glTexture * gl_texExistsOrCreate ( const char * path,
unsigned int flags,
int sx,
int sy,
int * created )

Check to see if a texture matching a path already exists.

Note this increments the used counter if it exists. Does not actually initialize and set otex->gtex->texture if created.

Parameters
pathPath to the texture.
flagsFlags used by the new texture.
sxX sprites.
syY sprites.
[out]createdWhether or not the texture was created.
Returns
The texture

Definition at line 508 of file opengl_tex.c.

◆ gl_texParameters()

GLuint gl_texParameters ( unsigned int flags)
static

Sets default texture parameters.

Definition at line 220 of file opengl_tex.c.

◆ gl_transSize()

size_t gl_transSize ( const int w,
const int h )
static

Definition at line 211 of file opengl_tex.c.

◆ SDL_GetAlpha()

uint8_t SDL_GetAlpha ( SDL_Surface * s,
int x,
int y )
static

Gets the alpha value of a pixel.

Parameters
sSurface to get value from.
xX position of the pixel to check.
yY position of the pixel to check.
Returns
Alpha value of the pixel.

Definition at line 122 of file opengl_tex.c.

◆ SDL_IsTrans()

int SDL_IsTrans ( SDL_Surface * s,
int x,
int y )
static

Checks to see if a position of the surface is transparent.

Parameters
sSurface to check for transparency.
xX position of the pixel to check.
yY position of the pixel to check.
Returns
0 if the pixel isn't transparent, 0 if it is.

Definition at line 150 of file opengl_tex.c.

◆ SDL_MapAlpha()

uint8_t * SDL_MapAlpha ( SDL_Surface * s,
int tight )
static

Maps the surface transparency.

Basically generates a map of what pixels are transparent. Good for pixel perfect collision routines.

Parameters
sSurface to map its transparency.
tightWhether or not to store transparency per bit or
Returns
0 on success.

Definition at line 169 of file opengl_tex.c.

◆ tex_cmp()

int tex_cmp ( const void * p1,
const void * p2 )
static

Definition at line 96 of file opengl_tex.c.

◆ tex_ctxSet()

void tex_ctxSet ( void )
static

Definition at line 72 of file opengl_tex.c.

◆ tex_ctxUnset()

void tex_ctxUnset ( void )
static

Definition at line 78 of file opengl_tex.c.

Variable Documentation

◆ gl_lock

SDL_mutex* gl_lock = NULL
static

Lock for OpenGL functions.

Definition at line 45 of file opengl_tex.c.

◆ tex_lock

SDL_mutex* tex_lock = NULL
static

Lock for texture list manipulation.

Definition at line 46 of file opengl_tex.c.

◆ tex_mainthread

SDL_threadID tex_mainthread
static

Definition at line 44 of file opengl_tex.c.

◆ texture_list

glTexList* texture_list = NULL
static

Texture list.

Definition at line 43 of file opengl_tex.c.