13#include "background.h"
23#include "nlua_audio.h"
25#include "nlua_colour.h"
27#include "nlua_camera.h"
40typedef struct background_image_s {
82static int bkg_compare(
const void *p1,
const void *p2 );
110 ndust = (
unsigned int)(size/(800.*600.));
113 dust_vertex = malloc(
ndust *
sizeof(GLfloat) * 3 );
115 for (
unsigned int i=0; i <
ndust; i++) {
117 dust_vertex[3*i+0] = RNGF()*w - hw;
118 dust_vertex[3*i+1] = RNGF()*h - hh;
120 dust_vertex[3*i+2] = RNGF()*0.6 + 0.2;
126 ndust *
sizeof(GLfloat) * 3, dust_vertex );
157 projection = gl_view_matrix;
162 if ((
player.
p != NULL) && !player_isFlag(PLAYER_DESTROYED) &&
163 !player_isFlag(PLAYER_CREATING)) {
168 vmod = hypot( dx, dy );
170 if (pilot_isFlag(
player.
p,PILOT_HYPERSPACE)) {
174 m *= HYPERSPACE_DUST_LENGTH / HYPERSPACE_DUST_BLUR;
175 angle = atan2( dy, dx );
179 else if (
dt_mod * vmod > 500. ) {
180 angle = atan2( dy, dx );
181 m = (
dt_mod * vmod) / 25. - 20.;
193 glUseProgram(shaders.dust.program);
194 gl_uniformMat4(shaders.dust.projection, &projection);
201 glUniform1i(shaders.dust.use_lines, !points);
204 glEnableVertexAttribArray( shaders.dust.vertex );
205 glEnableVertexAttribArray( shaders.dust.brightness );
209 2, GL_FLOAT, 3 *
sizeof(GLfloat) );
211 1, GL_FLOAT, 3 *
sizeof(GLfloat) );
212 glDrawArrays( GL_POINTS, 0,
ndust );
215 glDisableVertexAttribArray( shaders.dust.vertex );
216 glDisableVertexAttribArray( shaders.dust.brightness );
233 lua_pushnumber( naevL, dt );
235 WARN( _(
"Background script 'renderbg' error:\n%s"), lua_tostring(naevL,-1));
244 lua_pushnumber( naevL, dt );
246 WARN( _(
"Background script 'rendermg' error:\n%s"), lua_tostring(naevL,-1));
256 lua_pushnumber( naevL, dt );
258 WARN( _(
"Background script 'renderfg' error:\n%s"), lua_tostring(naevL,-1));
271 lua_pushnumber( naevL, dt );
273 WARN( _(
"Background script 'renderov' error:\n%s"), lua_tostring(naevL,-1));
308 double move,
double scale,
double angle,
const glColour *col,
int foreground )
330 bkg->
col = (col!=NULL) ? *col : cWhite;
349 double cx,cy, x,y, gx,gy, z, m;
357 x = (bkg->
x - cx) * m - z*bkg->
image->
sw/2. + gx + SCREEN_W/2.;
358 y = (bkg->
y - cy) * m - z*bkg->
image->
sh/2. + gy + SCREEN_H/2.;
365 gl_renderTexture( bkg->
image, x, y, z*bkg->
image->
sw, z*bkg->
image->
sh, 0., 0., bkg->
image->
srw, bkg->
image->
srh, &col, bkg->
angle );
380 snprintf( path,
sizeof(path), BACKGROUND_PATH
"%s.lua", name );
394 WARN( _(
"Background script '%s' not found."), path);
400 if (nlua_dobufenv(env, buf, bufsize, path) != 0) {
401 WARN( _(
"Error loading background file: %s\n"
403 "Most likely Lua file has improper syntax, please check"),
404 path, lua_tostring(naevL,-1));
444 if (env == LUA_NOREF)
448 nlua_getenv(naevL, env,
"background");
449 ret = nlua_pcall(env, 0, 0);
451 const char *err = (lua_isstring(naevL,-1)) ? lua_tostring(naevL,-1) : NULL;
452 WARN( _(
"Background -> 'background' : %s"),
453 (err) ? err : _(
"unknown error"));
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
#define array_end(array)
Returns a pointer to the end of the reserved memory space.
#define array_create_size(basic_type, capacity)
Creates a new dynamic array of ‘basic_type’ with an initial capacity.
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
#define array_begin(array)
Returns a pointer to the beginning of the reserved memory space.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
static background_image_t * bkg_image_arr_bk
glTexture ** background_getStarTextures(void)
Returns an array (array.h) of star background images in the system background.
void background_clear(void)
Cleans up the background stuff.
int background_init(void)
Initializes the background system.
static unsigned int bkg_idgen
static int bkg_L_renderov
static background_image_t * bkg_image_arr_ft
void background_initDust(int n)
Initializes background dust.
static int bkg_L_renderbg
void background_renderOverlay(double dt)
Renders the background overlay.
static int bkg_L_rendermg
static nlua_env bkg_def_env
unsigned int background_addImage(const glTexture *image, double x, double y, double move, double scale, double angle, const glColour *col, int foreground)
Adds a new background image.
void background_moveDust(double x, double y)
Displaces the dust, useful with camera.
static int bkg_L_renderfg
void background_free(void)
Cleans up and frees memory after the backgrounds.
static void background_clearCurrent(void)
Destroys the current running background script.
static int bkg_compare(const void *p1, const void *p2)
Compares two different backgrounds and sorts them.
static void background_renderImages(background_image_t *bkg_arr)
Renders the background images.
glTexture * background_getAmbientTexture(void)
Returns an overall background image (nebula, for instance), or NULL if none exists.
void background_renderDust(const double dt)
Renders the dustry background.
static void background_clearImgArr(background_image_t **arr)
Clears a background image array.
static void bkg_sort(background_image_t *arr)
Sorts the backgrounds by movement.
static gl_vbo * dust_vertexVBO
static nlua_env background_create(const char *path)
Creates a background Lua state from a script.
void background_render(double dt)
Render the background.
static nlua_env bkg_cur_env
Backgrounds.
static unsigned int ndust
int background_load(const char *name)
Loads a background script by name.
void cam_getVel(double *vx, double *vy)
Gets the camera velocity.
void cam_getPos(double *x, double *y)
Gets the camera position.
double cam_getZoom(void)
Gets the camera zoom.
void gui_getOffset(double *x, double *y)
Gets the GUI offset.
void mat4_translate(mat4 *m, double x, double y, double z)
Translates a homogenous transformation matrix.
void mat4_scale(mat4 *m, double x, double y, double z)
Scales a homogeneous transformation matrix.
Header file with generic functions and naev-specifics.
void * ndata_read(const char *path, size_t *filesize)
Reads a file from the ndata (will be NUL terminated).
int nlua_refenv(nlua_env env, const char *name)
Gets the reference of a global in a lua environment.
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
int nlua_loadBackground(nlua_env env)
Loads the graphics library.
int nlua_loadCamera(nlua_env env)
Loads the camera library.
int nlua_loadCol(nlua_env env)
Loads the colour library.
int nlua_loadGFX(nlua_env env)
Loads the graphics library.
int nlua_loadTex(nlua_env env)
Loads the texture library.
void gl_renderTexture(const glTexture *texture, double x, double y, double w, double h, double tx, double ty, double tw, double th, const glColour *c, double angle)
Texture blitting backend.
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
void gl_freeTexture(glTexture *texture)
Frees a texture.
void gl_vboDestroy(gl_vbo *vbo)
Destroys a VBO.
void gl_vboActivateAttribOffset(gl_vbo *vbo, GLuint index, GLuint offset, GLint size, GLenum type, GLsizei stride)
Activates a VBO's offset.
gl_vbo * gl_vboCreateStatic(GLsizei size, const void *data)
Creates a stream vbo.
Represents a background image like say a Nebula.
Abstraction for rendering sprite sheets.