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 nstars = (
unsigned int)(size/(800.*600.));
113 star_vertex = malloc(
nstars *
sizeof(GLfloat) * 6 );
115 for (
unsigned int i=0; i <
nstars; i++) {
117 star_vertex[6*i+0] = RNGF()*w - hw;
118 star_vertex[6*i+1] = RNGF()*h - hh;
119 star_vertex[6*i+3] = star_vertex[6*i+0];
120 star_vertex[6*i+4] = star_vertex[6*i+1];
122 star_vertex[6*i+2] = RNGF()*0.6 + 0.2;
123 star_vertex[6*i+5] = star_vertex[6*i+2];
129 nstars *
sizeof(GLfloat) * 6, star_vertex );
151 GLfloat x, y, h, w, m;
160 projection = gl_view_matrix;
165 if ((
player.
p != NULL) && !player_isFlag(PLAYER_DESTROYED) &&
166 !player_isFlag(PLAYER_CREATING)) {
171 vmod = hypot( dx, dy );
173 if (pilot_isFlag(
player.
p,PILOT_HYPERSPACE)) {
176 m /= HYPERSPACE_STARS_BLUR;
177 m *= HYPERSPACE_STARS_LENGTH;
179 double angle = atan2( dy, dx );
180 x = m * cos( angle );
181 y = m * sin( angle );
185 else if (
dt_mod * vmod > 500. ) {
190 double angle = atan2( dy, dx );
192 x = m * cos( angle );
193 y = m * sin( angle );
205 glUseProgram(shaders.stars.program);
206 gl_uniformMat4(shaders.stars.projection, &projection);
209 glUniform1i(shaders.stars.use_lines, !points);
210 glUniform1f(shaders.stars.dim,
CLAMP(0.5, 1., 1.-(m-1.)/25.) );
213 glEnableVertexAttribArray( shaders.stars.vertex );
214 glEnableVertexAttribArray( shaders.stars.brightness );
219 2, GL_FLOAT, 6 *
sizeof(GLfloat) );
221 1, GL_FLOAT, 6 *
sizeof(GLfloat) );
222 glUniform2f(shaders.stars.xy, 0., 0.);
223 glDrawArrays( GL_POINTS, 0,
nstars/2 );
227 2, GL_FLOAT, 3 *
sizeof(GLfloat) );
229 1, GL_FLOAT, 3 *
sizeof(GLfloat) );
230 glUniform2f(shaders.stars.xy, x, y);
231 glDrawArrays( GL_LINES, 0,
nstars );
235 glDisableVertexAttribArray( shaders.stars.vertex );
236 glDisableVertexAttribArray( shaders.stars.brightness );
253 lua_pushnumber( naevL, dt );
255 WARN( _(
"Background script 'renderbg' error:\n%s"), lua_tostring(naevL,-1));
264 lua_pushnumber( naevL, dt );
266 WARN( _(
"Background script 'rendermg' error:\n%s"), lua_tostring(naevL,-1));
276 lua_pushnumber( naevL, dt );
278 WARN( _(
"Background script 'renderfg' error:\n%s"), lua_tostring(naevL,-1));
291 lua_pushnumber( naevL, dt );
293 WARN( _(
"Background script 'renderov' error:\n%s"), lua_tostring(naevL,-1));
328 double move,
double scale,
double angle,
const glColour *col,
int foreground )
350 bkg->
col = (col!=NULL) ? *col : cWhite;
369 double cx,cy, x,y, gx,gy, z, m;
377 x = (bkg->
x - cx) * m - z*bkg->
image->
sw/2. + gx + SCREEN_W/2.;
378 y = (bkg->
y - cy) * m - z*bkg->
image->
sh/2. + gy + SCREEN_H/2.;
385 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 );
400 snprintf( path,
sizeof(path), BACKGROUND_PATH
"%s.lua", name );
414 WARN( _(
"Background script '%s' not found."), path);
420 if (nlua_dobufenv(env, buf, bufsize, path) != 0) {
421 WARN( _(
"Error loading background file: %s\n"
423 "Most likely Lua file has improper syntax, please check"),
424 path, lua_tostring(naevL,-1));
464 if (env == LUA_NOREF)
468 nlua_getenv(naevL, env,
"background");
469 ret = nlua_pcall(env, 0, 0);
471 const char *err = (lua_isstring(naevL,-1)) ? lua_tostring(naevL,-1) : NULL;
472 WARN( _(
"Background -> 'background' : %s"),
473 (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
unsigned int background_addImage(glTexture *image, double x, double y, double move, double scale, double angle, const glColour *col, int foreground)
Adds a new background image.
glTexture ** background_getTextures(void)
returns the background images, and number of these
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 stars.
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
void background_moveDust(double x, double y)
Displaces the stars, useful with camera.
static int bkg_L_renderfg
static unsigned int nstars
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.
static gl_vbo * star_vertexVBO
void background_renderDust(const double dt)
Renders the starry 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 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.
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).
static char buf[NEWS_MAX_LENGTH]
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.