diff --git a/cmake_uninstall.cmake.in b/cmake_uninstall.cmake.in index 17376d2e..4ea57b1c 100644 --- a/cmake_uninstall.cmake.in +++ b/cmake_uninstall.cmake.in @@ -1,22 +1,29 @@ -IF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") - MESSAGE(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"") -ENDIF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") -FILE(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files) -STRING(REGEX REPLACE "\n" ";" files "${files}") -FOREACH(file ${files}) - MESSAGE(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"") - IF(EXISTS "$ENV{DESTDIR}${file}") - EXEC_PROGRAM( - "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" - OUTPUT_VARIABLE rm_out - RETURN_VALUE rm_retval - ) - IF(NOT "${rm_retval}" STREQUAL 0) +if (NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") + message(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"") +endif() + +file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files) +string(REGEX REPLACE "\n" ";" files "${files}") + +foreach (file ${files}) + message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"") + if (EXISTS "$ENV{DESTDIR}${file}") + exec_program("@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval) + if (NOT "${rm_retval}" STREQUAL 0) MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"") - ENDIF(NOT "${rm_retval}" STREQUAL 0) - ELSE(EXISTS "$ENV{DESTDIR}${file}") - MESSAGE(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.") - ENDIF(EXISTS "$ENV{DESTDIR}${file}") -ENDFOREACH(file) + endif() + elseif (IS_SYMLINK "$ENV{DESTDIR}${file}") + EXEC_PROGRAM("@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval) + if (NOT "${rm_retval}" STREQUAL 0) + message(FATAL_ERROR "Problem when removing symlink \"$ENV{DESTDIR}${file}\"") + endif() + else() + message(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.") + endif() +endforeach() diff --git a/examples/particles.c b/examples/particles.c deleted file mode 100644 index c7904e11..00000000 --- a/examples/particles.c +++ /dev/null @@ -1,1152 +0,0 @@ -//======================================================================== -// This is a simple, but cool particle engine (buzz-word meaning many -// small objects that are treated as points and drawn as textures -// projected on simple geometry). -// -// This demonstration generates a colorful fountain-like animation. It -// uses several advanced OpenGL teqhniques: -// -// 1) Lighting (per vertex) -// 2) Alpha blending -// 3) Fog -// 4) Texturing -// 5) Display lists (for drawing the static environment geometry) -// 6) Vertex arrays (for drawing the particles) -// 7) GL_EXT_separate_specular_color is used (if available) -// -// Even more so, this program uses multi threading. The program is -// essentialy divided into a main rendering thread and a particle physics -// calculation thread. My benchmarks under Windows 2000 on a single -// processor system show that running this program as two threads instead -// of a single thread means no difference (there may be a very marginal -// advantage for the multi threaded case). On dual processor systems I -// have had reports of 5-25% of speed increase when running this program -// as two threads instead of one thread. -// -// The default behaviour of this program is to use two threads. To force -// a single thread to be used, use the command line switch -s. -// -// To run a fixed length benchmark (60 s), use the command line switch -b. -// -// Benchmark results (640x480x16, best of three tests): -// -// CPU GFX 1 thread 2 threads -// Athlon XP 2700+ GeForce Ti4200 (oc) 757 FPS 759 FPS -// P4 2.8 GHz (SMT) GeForce FX5600 548 FPS 550 FPS -// -// One more thing: Press 'w' during the demo to toggle wireframe mode. -//======================================================================== - -#include -#include -#include -#include -#include - -// Define tokens for GL_EXT_separate_specular_color if not already defined -#ifndef GL_EXT_separate_specular_color -#define GL_LIGHT_MODEL_COLOR_CONTROL_EXT 0x81F8 -#define GL_SINGLE_COLOR_EXT 0x81F9 -#define GL_SEPARATE_SPECULAR_COLOR_EXT 0x81FA -#endif // GL_EXT_separate_specular_color - -// Some 's do not define M_PI -#ifndef M_PI -#define M_PI 3.141592654 -#endif - -// Desired fullscreen resolution -#define WIDTH 640 -#define HEIGHT 480 - - -//======================================================================== -// Type definitions -//======================================================================== - -typedef struct { float x,y,z; } VEC; - -// This structure is used for interleaved vertex arrays (see the -// DrawParticles function) - Note: This structure SHOULD be packed on most -// systems. It uses 32-bit fields on 32-bit boundaries, and is a multiple -// of 64 bits in total (6x32=3x64). If it does not work, try using pragmas -// or whatever to force the structure to be packed. -typedef struct { - GLfloat s, t; // Texture coordinates - GLuint rgba; // Color (four ubytes packed into an uint) - GLfloat x, y, z; // Vertex coordinates -} VERTEX; - - -//======================================================================== -// Program control global variables -//======================================================================== - -// "Running" flag (true if program shall continue to run) -int running; - -// Window dimensions -int width, height; - -// "wireframe" flag (true if we use wireframe view) -int wireframe; - -// "multithreading" flag (true if we use multithreading) -int multithreading; - -// Thread synchronization -struct { - double t; // Time (s) - float dt; // Time since last frame (s) - int p_frame; // Particle physics frame number - int d_frame; // Particle draw frame number - GLFWcond p_done; // Condition: particle physics done - GLFWcond d_done; // Condition: particle draw done - GLFWmutex particles_lock; // Particles data sharing mutex -} thread_sync; - - -//======================================================================== -// Texture declarations (we hard-code them into the source code, since -// they are so simple) -//======================================================================== - -#define P_TEX_WIDTH 8 // Particle texture dimensions -#define P_TEX_HEIGHT 8 -#define F_TEX_WIDTH 16 // Floor texture dimensions -#define F_TEX_HEIGHT 16 - -// Texture object IDs -GLuint particle_tex_id, floor_tex_id; - -// Particle texture (a simple spot) -const unsigned char particle_texture[ P_TEX_WIDTH * P_TEX_HEIGHT ] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x11, 0x22, 0x22, 0x11, 0x00, 0x00, - 0x00, 0x11, 0x33, 0x88, 0x77, 0x33, 0x11, 0x00, - 0x00, 0x22, 0x88, 0xff, 0xee, 0x77, 0x22, 0x00, - 0x00, 0x22, 0x77, 0xee, 0xff, 0x88, 0x22, 0x00, - 0x00, 0x11, 0x33, 0x77, 0x88, 0x33, 0x11, 0x00, - 0x00, 0x00, 0x11, 0x33, 0x22, 0x11, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; - -// Floor texture (your basic checkered floor) -const unsigned char floor_texture[ F_TEX_WIDTH * F_TEX_HEIGHT ] = { - 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0xff, 0xf0, 0xcc, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0xf0, 0xcc, 0xee, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0x30, 0x66, 0x30, 0x30, 0x30, 0x20, 0x30, 0x30, - 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xee, 0xf0, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0xf0, 0xf0, 0xf0, 0xf0, 0xcc, 0xf0, 0xf0, 0xf0, 0x30, 0x30, 0x55, 0x30, 0x30, 0x44, 0x30, 0x30, - 0xf0, 0xdd, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x60, 0x30, - 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x33, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x20, 0x30, 0x30, 0xf0, 0xff, 0xf0, 0xf0, 0xdd, 0xf0, 0xf0, 0xff, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x55, 0x33, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0xf0, - 0x30, 0x44, 0x66, 0x30, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xaa, 0xf0, 0xf0, 0xcc, 0xf0, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0xdd, 0xf0, - 0x30, 0x30, 0x30, 0x77, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, -}; - - -//======================================================================== -// These are fixed constants that control the particle engine. In a -// modular world, these values should be variables... -//======================================================================== - -// Maximum number of particles -#define MAX_PARTICLES 3000 - -// Life span of a particle (in seconds) -#define LIFE_SPAN 8.0f - -// A new particle is born every [BIRTH_INTERVAL] second -#define BIRTH_INTERVAL (LIFE_SPAN/(float)MAX_PARTICLES) - -// Particle size (meters) -#define PARTICLE_SIZE 0.7f - -// Gravitational constant (m/s^2) -#define GRAVITY 9.8f - -// Base initial velocity (m/s) -#define VELOCITY 8.0f - -// Bounce friction (1.0 = no friction, 0.0 = maximum friction) -#define FRICTION 0.75f - -// "Fountain" height (m) -#define FOUNTAIN_HEIGHT 3.0f - -// Fountain radius (m) -#define FOUNTAIN_RADIUS 1.6f - -// Minimum delta-time for particle phisics (s) -#define MIN_DELTA_T (BIRTH_INTERVAL * 0.5f) - - -//======================================================================== -// Particle system global variables -//======================================================================== - -// This structure holds all state for a single particle -typedef struct { - float x,y,z; // Position in space - float vx,vy,vz; // Velocity vector - float r,g,b; // Color of particle - float life; // Life of particle (1.0 = newborn, < 0.0 = dead) - int active; // Tells if this particle is active -} PARTICLE; - -// Global vectors holding all particles. We use two vectors for double -// buffering. -static PARTICLE particles[ MAX_PARTICLES ]; - -// Global variable holding the age of the youngest particle -static float min_age; - -// Color of latest born particle (used for fountain lighting) -static float glow_color[4]; - -// Position of latest born particle (used for fountain lighting) -static float glow_pos[4]; - - -//======================================================================== -// Object material and fog configuration constants -//======================================================================== - -const GLfloat fountain_diffuse[4] = {0.7f,1.0f,1.0f,1.0f}; -const GLfloat fountain_specular[4] = {1.0f,1.0f,1.0f,1.0f}; -const GLfloat fountain_shininess = 12.0f; -const GLfloat floor_diffuse[4] = {1.0f,0.6f,0.6f,1.0f}; -const GLfloat floor_specular[4] = {0.6f,0.6f,0.6f,1.0f}; -const GLfloat floor_shininess = 18.0f; -const GLfloat fog_color[4] = {0.1f, 0.1f, 0.1f, 1.0f}; - - -//======================================================================== -// InitParticle() - Initialize a new particle -//======================================================================== - -void InitParticle( PARTICLE *p, double t ) -{ - float xy_angle, velocity; - - // Start position of particle is at the fountain blow-out - p->x = 0.0f; - p->y = 0.0f; - p->z = FOUNTAIN_HEIGHT; - - // Start velocity is up (Z)... - p->vz = 0.7f + (0.3f/4096.f) * (float) (rand() & 4095); - - // ...and a randomly chosen X/Y direction - xy_angle = (2.f * (float)M_PI / 4096.f) * (float) (rand() & 4095); - p->vx = 0.4f * (float) cos( xy_angle ); - p->vy = 0.4f * (float) sin( xy_angle ); - - // Scale velocity vector according to a time-varying velocity - velocity = VELOCITY*(0.8f + 0.1f*(float)(sin( 0.5*t )+sin( 1.31*t ))); - p->vx *= velocity; - p->vy *= velocity; - p->vz *= velocity; - - // Color is time-varying - p->r = 0.7f + 0.3f * (float) sin( 0.34*t + 0.1 ); - p->g = 0.6f + 0.4f * (float) sin( 0.63*t + 1.1 ); - p->b = 0.6f + 0.4f * (float) sin( 0.91*t + 2.1 ); - - // Store settings for fountain glow lighting - glow_pos[0] = 0.4f * (float) sin( 1.34*t ); - glow_pos[1] = 0.4f * (float) sin( 3.11*t ); - glow_pos[2] = FOUNTAIN_HEIGHT + 1.0f; - glow_pos[3] = 1.0f; - glow_color[0] = p->r; - glow_color[1] = p->g; - glow_color[2] = p->b; - glow_color[3] = 1.0f; - - // The particle is new-born and active - p->life = 1.0f; - p->active = 1; -} - - -//======================================================================== -// UpdateParticle() - Update a particle -//======================================================================== - -#define FOUNTAIN_R2 (FOUNTAIN_RADIUS+PARTICLE_SIZE/2)*(FOUNTAIN_RADIUS+PARTICLE_SIZE/2) - -void UpdateParticle( PARTICLE *p, float dt ) -{ - // If the particle is not active, we need not do anything - if( !p->active ) - { - return; - } - - // The particle is getting older... - p->life = p->life - dt * (1.0f / LIFE_SPAN); - - // Did the particle die? - if( p->life <= 0.0f ) - { - p->active = 0; - return; - } - - // Update particle velocity (apply gravity) - p->vz = p->vz - GRAVITY * dt; - - // Update particle position - p->x = p->x + p->vx * dt; - p->y = p->y + p->vy * dt; - p->z = p->z + p->vz * dt; - - // Simple collision detection + response - if( p->vz < 0.0f ) - { - // Particles should bounce on the fountain (with friction) - if( (p->x*p->x + p->y*p->y) < FOUNTAIN_R2 && - p->z < (FOUNTAIN_HEIGHT + PARTICLE_SIZE/2) ) - { - p->vz = -FRICTION * p->vz; - p->z = FOUNTAIN_HEIGHT + PARTICLE_SIZE/2 + - FRICTION * (FOUNTAIN_HEIGHT + - PARTICLE_SIZE/2 - p->z); - } - - // Particles should bounce on the floor (with friction) - else if( p->z < PARTICLE_SIZE/2 ) - { - p->vz = -FRICTION * p->vz; - p->z = PARTICLE_SIZE/2 + - FRICTION * (PARTICLE_SIZE/2 - p->z); - } - - } -} - - -//======================================================================== -// ParticleEngine() - The main frame for the particle engine. Called once -// per frame. -//======================================================================== - -void ParticleEngine( double t, float dt ) -{ - int i; - float dt2; - - // Update particles (iterated several times per frame if dt is too - // large) - while( dt > 0.0f ) - { - // Calculate delta time for this iteration - dt2 = dt < MIN_DELTA_T ? dt : MIN_DELTA_T; - - // Update particles - for( i = 0; i < MAX_PARTICLES; i ++ ) - { - UpdateParticle( &particles[ i ], dt2 ); - } - - // Increase minimum age - min_age += dt2; - - // Should we create any new particle(s)? - while( min_age >= BIRTH_INTERVAL ) - { - min_age -= BIRTH_INTERVAL; - - // Find a dead particle to replace with a new one - for( i = 0; i < MAX_PARTICLES; i ++ ) - { - if( !particles[ i ].active ) - { - InitParticle( &particles[ i ], t + min_age ); - UpdateParticle( &particles[ i ], min_age ); - break; - } - } - } - - // Decrease frame delta time - dt -= dt2; - } -} - - -//======================================================================== -// DrawParticles() - Draw all active particles. We use OpenGL 1.1 vertex -// arrays for this in order to accelerate the drawing. -//======================================================================== - -#define BATCH_PARTICLES 70 // Number of particles to draw in each batch - // (70 corresponds to 7.5 KB = will not blow - // the L1 data cache on most CPUs) -#define PARTICLE_VERTS 4 // Number of vertices per particle - -void DrawParticles( double t, float dt ) -{ - int i, particle_count; - VERTEX vertex_array[ BATCH_PARTICLES * PARTICLE_VERTS ], *vptr; - float alpha; - GLuint rgba; - VEC quad_lower_left, quad_lower_right; - GLfloat mat[ 16 ]; - PARTICLE *pptr; - - // Here comes the real trick with flat single primitive objects (s.c. - // "billboards"): We must rotate the textured primitive so that it - // always faces the viewer (is coplanar with the view-plane). - // We: - // 1) Create the primitive around origo (0,0,0) - // 2) Rotate it so that it is coplanar with the view plane - // 3) Translate it according to the particle position - // Note that 1) and 2) is the same for all particles (done only once). - - // Get modelview matrix. We will only use the upper left 3x3 part of - // the matrix, which represents the rotation. - glGetFloatv( GL_MODELVIEW_MATRIX, mat ); - - // 1) & 2) We do it in one swift step: - // Although not obvious, the following six lines represent two matrix/ - // vector multiplications. The matrix is the inverse 3x3 rotation - // matrix (i.e. the transpose of the same matrix), and the two vectors - // represent the lower left corner of the quad, PARTICLE_SIZE/2 * - // (-1,-1,0), and the lower right corner, PARTICLE_SIZE/2 * (1,-1,0). - // The upper left/right corners of the quad is always the negative of - // the opposite corners (regardless of rotation). - quad_lower_left.x = (-PARTICLE_SIZE/2) * (mat[0] + mat[1]); - quad_lower_left.y = (-PARTICLE_SIZE/2) * (mat[4] + mat[5]); - quad_lower_left.z = (-PARTICLE_SIZE/2) * (mat[8] + mat[9]); - quad_lower_right.x = (PARTICLE_SIZE/2) * (mat[0] - mat[1]); - quad_lower_right.y = (PARTICLE_SIZE/2) * (mat[4] - mat[5]); - quad_lower_right.z = (PARTICLE_SIZE/2) * (mat[8] - mat[9]); - - // Don't update z-buffer, since all particles are transparent! - glDepthMask( GL_FALSE ); - - // Enable blending - glEnable( GL_BLEND ); - glBlendFunc( GL_SRC_ALPHA, GL_ONE ); - - // Select particle texture - if( !wireframe ) - { - glEnable( GL_TEXTURE_2D ); - glBindTexture( GL_TEXTURE_2D, particle_tex_id ); - } - - // Set up vertex arrays. We use interleaved arrays, which is easier to - // handle (in most situations) and it gives a linear memeory access - // access pattern (which may give better performance in some - // situations). GL_T2F_C4UB_V3F means: 2 floats for texture coords, - // 4 ubytes for color and 3 floats for vertex coord (in that order). - // Most OpenGL cards / drivers are optimized for this format. - glInterleavedArrays( GL_T2F_C4UB_V3F, 0, vertex_array ); - - // Is particle physics carried out in a separate thread? - if( multithreading ) - { - // Wait for particle physics thread to be done - glfwLockMutex( thread_sync.particles_lock ); - while( running && thread_sync.p_frame <= thread_sync.d_frame ) - { - glfwWaitCond( thread_sync.p_done, thread_sync.particles_lock, - 0.1 ); - } - - // Store the frame time and delta time for the physics thread - thread_sync.t = t; - thread_sync.dt = dt; - - // Update frame counter - thread_sync.d_frame ++; - } - else - { - // Perform particle physics in this thread - ParticleEngine( t, dt ); - } - - // Loop through all particles and build vertex arrays. - particle_count = 0; - vptr = vertex_array; - pptr = particles; - for( i = 0; i < MAX_PARTICLES; i ++ ) - { - if( pptr->active ) - { - // Calculate particle intensity (we set it to max during 75% - // of its life, then it fades out) - alpha = 4.0f * pptr->life; - if( alpha > 1.0f ) - { - alpha = 1.0f; - } - - // Convert color from float to 8-bit (store it in a 32-bit - // integer using endian independent type casting) - ((GLubyte *)&rgba)[0] = (GLubyte)(pptr->r * 255.0f); - ((GLubyte *)&rgba)[1] = (GLubyte)(pptr->g * 255.0f); - ((GLubyte *)&rgba)[2] = (GLubyte)(pptr->b * 255.0f); - ((GLubyte *)&rgba)[3] = (GLubyte)(alpha * 255.0f); - - // 3) Translate the quad to the correct position in modelview - // space and store its parameters in vertex arrays (we also - // store texture coord and color information for each vertex). - - // Lower left corner - vptr->s = 0.0f; - vptr->t = 0.0f; - vptr->rgba = rgba; - vptr->x = pptr->x + quad_lower_left.x; - vptr->y = pptr->y + quad_lower_left.y; - vptr->z = pptr->z + quad_lower_left.z; - vptr ++; - - // Lower right corner - vptr->s = 1.0f; - vptr->t = 0.0f; - vptr->rgba = rgba; - vptr->x = pptr->x + quad_lower_right.x; - vptr->y = pptr->y + quad_lower_right.y; - vptr->z = pptr->z + quad_lower_right.z; - vptr ++; - - // Upper right corner - vptr->s = 1.0f; - vptr->t = 1.0f; - vptr->rgba = rgba; - vptr->x = pptr->x - quad_lower_left.x; - vptr->y = pptr->y - quad_lower_left.y; - vptr->z = pptr->z - quad_lower_left.z; - vptr ++; - - // Upper left corner - vptr->s = 0.0f; - vptr->t = 1.0f; - vptr->rgba = rgba; - vptr->x = pptr->x - quad_lower_right.x; - vptr->y = pptr->y - quad_lower_right.y; - vptr->z = pptr->z - quad_lower_right.z; - vptr ++; - - // Increase count of drawable particles - particle_count ++; - } - - // If we have filled up one batch of particles, draw it as a set - // of quads using glDrawArrays. - if( particle_count >= BATCH_PARTICLES ) - { - // The first argument tells which primitive type we use (QUAD) - // The second argument tells the index of the first vertex (0) - // The last argument is the vertex count - glDrawArrays( GL_QUADS, 0, PARTICLE_VERTS * particle_count ); - particle_count = 0; - vptr = vertex_array; - } - - // Next particle - pptr ++; - } - - // We are done with the particle data: Unlock mutex and signal physics - // thread - if( multithreading ) - { - glfwUnlockMutex( thread_sync.particles_lock ); - glfwSignalCond( thread_sync.d_done ); - } - - // Draw final batch of particles (if any) - glDrawArrays( GL_QUADS, 0, PARTICLE_VERTS * particle_count ); - - // Disable vertex arrays (Note: glInterleavedArrays implicitly called - // glEnableClientState for vertex, texture coord and color arrays) - glDisableClientState( GL_VERTEX_ARRAY ); - glDisableClientState( GL_TEXTURE_COORD_ARRAY ); - glDisableClientState( GL_COLOR_ARRAY ); - - // Disable texturing and blending - glDisable( GL_TEXTURE_2D ); - glDisable( GL_BLEND ); - - // Allow Z-buffer updates again - glDepthMask( GL_TRUE ); -} - - -//======================================================================== -// Fountain geometry specification -//======================================================================== - -#define FOUNTAIN_SIDE_POINTS 14 -#define FOUNTAIN_SWEEP_STEPS 32 - -static const float fountain_side[ FOUNTAIN_SIDE_POINTS*2 ] = { - 1.2f, 0.0f, 1.0f, 0.2f, 0.41f, 0.3f, 0.4f, 0.35f, - 0.4f, 1.95f, 0.41f, 2.0f, 0.8f, 2.2f, 1.2f, 2.4f, - 1.5f, 2.7f, 1.55f,2.95f, 1.6f, 3.0f, 1.0f, 3.0f, - 0.5f, 3.0f, 0.0f, 3.0f -}; - -static const float fountain_normal[ FOUNTAIN_SIDE_POINTS*2 ] = { - 1.0000f, 0.0000f, 0.6428f, 0.7660f, 0.3420f, 0.9397f, 1.0000f, 0.0000f, - 1.0000f, 0.0000f, 0.3420f,-0.9397f, 0.4226f,-0.9063f, 0.5000f,-0.8660f, - 0.7660f,-0.6428f, 0.9063f,-0.4226f, 0.0000f,1.00000f, 0.0000f,1.00000f, - 0.0000f,1.00000f, 0.0000f,1.00000f -}; - - -//======================================================================== -// DrawFountain() - Draw a fountain -//======================================================================== - -void DrawFountain( void ) -{ - static GLuint fountain_list = 0; - double angle; - float x, y; - int m, n; - - // The first time, we build the fountain display list - if( !fountain_list ) - { - // Start recording of a new display list - fountain_list = glGenLists( 1 ); - glNewList( fountain_list, GL_COMPILE_AND_EXECUTE ); - - // Set fountain material - glMaterialfv( GL_FRONT, GL_DIFFUSE, fountain_diffuse ); - glMaterialfv( GL_FRONT, GL_SPECULAR, fountain_specular ); - glMaterialf( GL_FRONT, GL_SHININESS, fountain_shininess ); - - // Build fountain using triangle strips - for( n = 0; n < FOUNTAIN_SIDE_POINTS-1; n ++ ) - { - glBegin( GL_TRIANGLE_STRIP ); - for( m = 0; m <= FOUNTAIN_SWEEP_STEPS; m ++ ) - { - angle = (double) m * (2.0*M_PI/(double)FOUNTAIN_SWEEP_STEPS); - x = (float) cos( angle ); - y = (float) sin( angle ); - - // Draw triangle strip - glNormal3f( x * fountain_normal[ n*2+2 ], - y * fountain_normal[ n*2+2 ], - fountain_normal[ n*2+3 ] ); - glVertex3f( x * fountain_side[ n*2+2 ], - y * fountain_side[ n*2+2 ], - fountain_side[ n*2+3 ] ); - glNormal3f( x * fountain_normal[ n*2 ], - y * fountain_normal[ n*2 ], - fountain_normal[ n*2+1 ] ); - glVertex3f( x * fountain_side[ n*2 ], - y * fountain_side[ n*2 ], - fountain_side[ n*2+1 ] ); - } - glEnd(); - } - - // End recording of display list - glEndList(); - } - else - { - // Playback display list - glCallList( fountain_list ); - } -} - - -//======================================================================== -// TesselateFloor() - Recursive function for building variable tesselated -// floor -//======================================================================== - -void TesselateFloor( float x1, float y1, float x2, float y2, - int recursion ) -{ - float delta, x, y; - - // Last recursion? - if( recursion >= 5 ) - { - delta = 999999.0f; - } - else - { - x = (float) (fabs(x1) < fabs(x2) ? fabs(x1) : fabs(x2)); - y = (float) (fabs(y1) < fabs(y2) ? fabs(y1) : fabs(y2)); - delta = x*x + y*y; - } - - // Recurse further? - if( delta < 0.1f ) - { - x = (x1+x2) * 0.5f; - y = (y1+y2) * 0.5f; - TesselateFloor( x1,y1, x, y, recursion + 1 ); - TesselateFloor( x,y1, x2, y, recursion + 1 ); - TesselateFloor( x1, y, x,y2, recursion + 1 ); - TesselateFloor( x, y, x2,y2, recursion + 1 ); - } - else - { - glTexCoord2f( x1*30.0f, y1*30.0f ); - glVertex3f( x1*80.0f, y1*80.0f , 0.0f ); - glTexCoord2f( x2*30.0f, y1*30.0f ); - glVertex3f( x2*80.0f, y1*80.0f , 0.0f ); - glTexCoord2f( x2*30.0f, y2*30.0f ); - glVertex3f( x2*80.0f, y2*80.0f , 0.0f ); - glTexCoord2f( x1*30.0f, y2*30.0f ); - glVertex3f( x1*80.0f, y2*80.0f , 0.0f ); - } -} - - -//======================================================================== -// DrawFloor() - Draw floor. We builde the floor recursively, and let the -// tesselation in the centre (near x,y=0,0) be high, while the selleation -// around the edges be low. -//======================================================================== - -void DrawFloor( void ) -{ - static GLuint floor_list = 0; - - // Select floor texture - if( !wireframe ) - { - glEnable( GL_TEXTURE_2D ); - glBindTexture( GL_TEXTURE_2D, floor_tex_id ); - } - - // The first time, we build the floor display list - if( !floor_list ) - { - // Start recording of a new display list - floor_list = glGenLists( 1 ); - glNewList( floor_list, GL_COMPILE_AND_EXECUTE ); - - // Set floor material - glMaterialfv( GL_FRONT, GL_DIFFUSE, floor_diffuse ); - glMaterialfv( GL_FRONT, GL_SPECULAR, floor_specular ); - glMaterialf( GL_FRONT, GL_SHININESS, floor_shininess ); - - // Draw floor as a bunch of triangle strips (high tesselation - // improves lighting) - glNormal3f( 0.0f, 0.0f, 1.0f ); - glBegin( GL_QUADS ); - TesselateFloor( -1.0f,-1.0f, 0.0f,0.0f, 0 ); - TesselateFloor( 0.0f,-1.0f, 1.0f,0.0f, 0 ); - TesselateFloor( 0.0f, 0.0f, 1.0f,1.0f, 0 ); - TesselateFloor( -1.0f, 0.0f, 0.0f,1.0f, 0 ); - glEnd(); - - // End recording of display list - glEndList(); - } - else - { - // Playback display list - glCallList( floor_list ); - } - - glDisable( GL_TEXTURE_2D ); - -} - - -//======================================================================== -// SetupLights() - Position and configure light sources -//======================================================================== - -void SetupLights( void ) -{ - float l1pos[4], l1amb[4], l1dif[4], l1spec[4]; - float l2pos[4], l2amb[4], l2dif[4], l2spec[4]; - - // Set light source 1 parameters - l1pos[0] = 0.0f; l1pos[1] = -9.0f; l1pos[2] = 8.0f; l1pos[3] = 1.0f; - l1amb[0] = 0.2f; l1amb[1] = 0.2f; l1amb[2] = 0.2f; l1amb[3] = 1.0f; - l1dif[0] = 0.8f; l1dif[1] = 0.4f; l1dif[2] = 0.2f; l1dif[3] = 1.0f; - l1spec[0] = 1.0f; l1spec[1] = 0.6f; l1spec[2] = 0.2f; l1spec[3] = 0.0f; - - // Set light source 2 parameters - l2pos[0] = -15.0f; l2pos[1] = 12.0f; l2pos[2] = 1.5f; l2pos[3] = 1.0f; - l2amb[0] = 0.0f; l2amb[1] = 0.0f; l2amb[2] = 0.0f; l2amb[3] = 1.0f; - l2dif[0] = 0.2f; l2dif[1] = 0.4f; l2dif[2] = 0.8f; l2dif[3] = 1.0f; - l2spec[0] = 0.2f; l2spec[1] = 0.6f; l2spec[2] = 1.0f; l2spec[3] = 0.0f; - - // Configure light sources in OpenGL - glLightfv( GL_LIGHT1, GL_POSITION, l1pos ); - glLightfv( GL_LIGHT1, GL_AMBIENT, l1amb ); - glLightfv( GL_LIGHT1, GL_DIFFUSE, l1dif ); - glLightfv( GL_LIGHT1, GL_SPECULAR, l1spec ); - glLightfv( GL_LIGHT2, GL_POSITION, l2pos ); - glLightfv( GL_LIGHT2, GL_AMBIENT, l2amb ); - glLightfv( GL_LIGHT2, GL_DIFFUSE, l2dif ); - glLightfv( GL_LIGHT2, GL_SPECULAR, l2spec ); - glLightfv( GL_LIGHT3, GL_POSITION, glow_pos ); - glLightfv( GL_LIGHT3, GL_DIFFUSE, glow_color ); - glLightfv( GL_LIGHT3, GL_SPECULAR, glow_color ); - - // Enable light sources - glEnable( GL_LIGHT1 ); - glEnable( GL_LIGHT2 ); - glEnable( GL_LIGHT3 ); -} - - -//======================================================================== -// Draw() - Main rendering function -//======================================================================== - -void Draw( double t ) -{ - double xpos, ypos, zpos, angle_x, angle_y, angle_z; - static double t_old = 0.0; - float dt; - - // Calculate frame-to-frame delta time - dt = (float)(t-t_old); - t_old = t; - - // Setup viewport - glViewport( 0, 0, width, height ); - - // Clear color and Z-buffer - glClearColor( 0.1f, 0.1f, 0.1f, 1.0f ); - glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); - - // Setup projection - glMatrixMode( GL_PROJECTION ); - glLoadIdentity(); - gluPerspective( 65.0, (double)width/(double)height, 1.0, 60.0 ); - - // Setup camera - glMatrixMode( GL_MODELVIEW ); - glLoadIdentity(); - - // Rotate camera - angle_x = 90.0 - 10.0; - angle_y = 10.0 * sin( 0.3 * t ); - angle_z = 10.0 * t; - glRotated( -angle_x, 1.0, 0.0, 0.0 ); - glRotated( -angle_y, 0.0, 1.0, 0.0 ); - glRotated( -angle_z, 0.0, 0.0, 1.0 ); - - // Translate camera - xpos = 15.0 * sin( (M_PI/180.0) * angle_z ) + - 2.0 * sin( (M_PI/180.0) * 3.1 * t ); - ypos = -15.0 * cos( (M_PI/180.0) * angle_z ) + - 2.0 * cos( (M_PI/180.0) * 2.9 * t ); - zpos = 4.0 + 2.0 * cos( (M_PI/180.0) * 4.9 * t ); - glTranslated( -xpos, -ypos, -zpos ); - - // Enable face culling - glFrontFace( GL_CCW ); - glCullFace( GL_BACK ); - glEnable( GL_CULL_FACE ); - - // Enable lighting - SetupLights(); - glEnable( GL_LIGHTING ); - - // Enable fog (dim details far away) - glEnable( GL_FOG ); - glFogi( GL_FOG_MODE, GL_EXP ); - glFogf( GL_FOG_DENSITY, 0.05f ); - glFogfv( GL_FOG_COLOR, fog_color ); - - // Draw floor - DrawFloor(); - - // Enable Z-buffering - glEnable( GL_DEPTH_TEST ); - glDepthFunc( GL_LEQUAL ); - glDepthMask( GL_TRUE ); - - // Draw fountain - DrawFountain(); - - // Disable fog & lighting - glDisable( GL_LIGHTING ); - glDisable( GL_FOG ); - - // Draw all particles (must be drawn after all solid objects have been - // drawn!) - DrawParticles( t, dt ); - - // Z-buffer not needed anymore - glDisable( GL_DEPTH_TEST ); -} - - -//======================================================================== -// Resize() - GLFW window resize callback function -//======================================================================== - -void GLFWCALL Resize( int x, int y ) -{ - width = x; - height = y > 0 ? y : 1; // Prevent division by zero in aspect calc. -} - - -//======================================================================== -// Input callback functions -//======================================================================== - -void GLFWCALL KeyFun( int key, int action ) -{ - if( action == GLFW_PRESS ) - { - switch( key ) - { - case GLFW_KEY_ESC: - running = 0; - break; - case 'W': - wireframe = !wireframe; - glPolygonMode( GL_FRONT_AND_BACK, - wireframe ? GL_LINE : GL_FILL ); - break; - default: - break; - } - } -} - - -//======================================================================== -// PhysicsThreadFun() - Thread for updating particle physics -//======================================================================== - -void GLFWCALL PhysicsThreadFun( void *arg ) -{ - while( running ) - { - // Lock mutex - glfwLockMutex( thread_sync.particles_lock ); - - // Wait for particle drawing to be done - while( running && thread_sync.p_frame > thread_sync.d_frame ) - { - glfwWaitCond( thread_sync.d_done, thread_sync.particles_lock, - 0.1 ); - } - - // No longer running? - if( !running ) - { - break; - } - - // Update particles - ParticleEngine( thread_sync.t, thread_sync.dt ); - - // Update frame counter - thread_sync.p_frame ++; - - // Unlock mutex and signal drawing thread - glfwUnlockMutex( thread_sync.particles_lock ); - glfwSignalCond( thread_sync.p_done ); - } -} - - -//======================================================================== -// main() -//======================================================================== - -int main( int argc, char **argv ) -{ - int i, frames, benchmark; - double t0, t; - GLFWthread physics_thread = 0; - - // Use multithreading by default, but don't benchmark - multithreading = 1; - benchmark = 0; - - // Check command line arguments - for( i = 1; i < argc; i ++ ) - { - // Use benchmarking? - if( strcmp( argv[i], "-b" ) == 0 ) - { - benchmark = 1; - } - - // Force multithreading off? - else if( strcmp( argv[i], "-s" ) == 0 ) - { - multithreading = 0; - } - - // With a Finder launch on Mac OS X we get a bogus -psn_0_46268417 - // kind of argument (actual numbers vary). Ignore it. - else if( strncmp( argv[i], "-psn_", 5) == 0 ); - - // Usage - else - { - if( strcmp( argv[i], "-?" ) != 0 ) - { - printf( "Unknonwn option %s\n\n", argv[ i ] ); - } - printf( "Usage: %s [options]\n", argv[ 0 ] ); - printf( "\n"); - printf( "Options:\n" ); - printf( " -b Benchmark (run program for 60 s)\n" ); - printf( " -s Run program as single thread (default is to use two threads)\n" ); - printf( " -? Display this text\n" ); - printf( "\n"); - printf( "Program runtime controls:\n" ); - printf( " w Toggle wireframe mode\n" ); - printf( " ESC Exit program\n" ); - exit( 0 ); - } - } - - // Initialize GLFW - if( !glfwInit() ) - { - fprintf( stderr, "Failed to initialize GLFW\n" ); - exit( EXIT_FAILURE ); - } - - // Open OpenGL fullscreen window - if( !glfwCreateWindow( WIDTH, HEIGHT, 0,0,0,0, 16,0, GLFW_FULLSCREEN ) ) - { - fprintf( stderr, "Failed to open GLFW window\n" ); - glfwTerminate(); - exit( EXIT_FAILURE ); - } - - // Set window title - glfwSetWindowTitle( "Particle engine" ); - - // Disable VSync (we want to get as high FPS as possible!) - glfwSwapInterval( 0 ); - - // Window resize callback function - glfwSetWindowSizeCallback( Resize ); - - // Set keyboard input callback function - glfwSetKeyCallback( KeyFun ); - - // Upload particle texture - glGenTextures( 1, &particle_tex_id ); - glBindTexture( GL_TEXTURE_2D, particle_tex_id ); - glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE, P_TEX_WIDTH, P_TEX_HEIGHT, - 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, particle_texture ); - - // Upload floor texture - glGenTextures( 1, &floor_tex_id ); - glBindTexture( GL_TEXTURE_2D, floor_tex_id ); - glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE, F_TEX_WIDTH, F_TEX_HEIGHT, - 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, floor_texture ); - - // Check if we have GL_EXT_separate_specular_color, and if so use it - if( glfwExtensionSupported( "GL_EXT_separate_specular_color" ) ) - { - glLightModeli( GL_LIGHT_MODEL_COLOR_CONTROL_EXT, - GL_SEPARATE_SPECULAR_COLOR_EXT ); - } - - // Set filled polygon mode as default (not wireframe) - glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); - wireframe = 0; - - // Clear particle system - for( i = 0; i < MAX_PARTICLES; i ++ ) - { - particles[ i ].active = 0; - } - min_age = 0.0f; - - // Set "running" flag - running = 1; - - // Set initial times - thread_sync.t = 0.0; - thread_sync.dt = 0.001f; - - // Init threading - if( multithreading ) - { - thread_sync.p_frame = 0; - thread_sync.d_frame = 0; - thread_sync.particles_lock = glfwCreateMutex(); - thread_sync.p_done = glfwCreateCond(); - thread_sync.d_done = glfwCreateCond(); - physics_thread = glfwCreateThread( PhysicsThreadFun, NULL ); - } - - // Main loop - t0 = glfwGetTime(); - frames = 0; - while( running ) - { - // Get frame time - t = glfwGetTime() - t0; - - // Draw... - Draw( t ); - - // Swap buffers - glfwSwapBuffers(); - - // Check if window was closed - running = running && glfwGetWindowParam( GLFW_OPENED ); - - // Increase frame count - frames ++; - - // End of benchmark? - if( benchmark && t >= 60.0 ) - { - running = 0; - } - } - t = glfwGetTime() - t0; - - // Wait for particle physics thread to die - if( multithreading ) - { - glfwWaitThread( physics_thread, GLFW_WAIT ); - } - - // Display profiling information - printf( "%d frames in %.2f seconds = %.1f FPS", frames, t, - (double)frames / t ); - printf( " (multithreading %s)\n", multithreading ? "on" : "off" ); - - // Terminate OpenGL - glfwTerminate(); - - exit( EXIT_SUCCESS ); -} - diff --git a/examples/pong3d.c b/examples/pong3d.c deleted file mode 100644 index 58c9ee20..00000000 --- a/examples/pong3d.c +++ /dev/null @@ -1,854 +0,0 @@ -//======================================================================== -// This is a small test application for GLFW. -// This is an OpenGL port of the famous "PONG" game (the first computer -// game ever?). It is very simple, and could be improved alot. It was -// created in order to show off the gaming capabilities of GLFW. -//======================================================================== - -#include -#include -#include -#include - - -//======================================================================== -// Constants -//======================================================================== - -// Screen resolution -#define WIDTH 640 -#define HEIGHT 480 - -// Player size (units) -#define PLAYER_XSIZE 0.05f -#define PLAYER_YSIZE 0.15f - -// Ball size (units) -#define BALL_SIZE 0.02f - -// Maximum player movement speed (units / second) -#define MAX_SPEED 1.5f - -// Player movement acceleration (units / seconds^2) -#define ACCELERATION 4.0f - -// Player movement deceleration (units / seconds^2) -#define DECELERATION 2.0f - -// Ball movement speed (units / second) -#define BALL_SPEED 0.4f - -// Menu options -#define MENU_NONE 0 -#define MENU_PLAY 1 -#define MENU_QUIT 2 - -// Game events -#define NOBODY_WINS 0 -#define PLAYER1_WINS 1 -#define PLAYER2_WINS 2 - -// Winner ID -#define NOBODY 0 -#define PLAYER1 1 -#define PLAYER2 2 - -// Camera positions -#define CAMERA_CLASSIC 0 -#define CAMERA_ABOVE 1 -#define CAMERA_SPECTATOR 2 -#define CAMERA_DEFAULT CAMERA_CLASSIC - - -//======================================================================== -// Textures -//======================================================================== - -#define TEX_TITLE 0 -#define TEX_MENU 1 -#define TEX_INSTR 2 -#define TEX_WINNER1 3 -#define TEX_WINNER2 4 -#define TEX_FIELD 5 -#define NUM_TEXTURES 6 - -// Texture names -char * tex_name[ NUM_TEXTURES ] = { - "pong3d_title.tga", - "pong3d_menu.tga", - "pong3d_instr.tga", - "pong3d_winner1.tga", - "pong3d_winner2.tga", - "pong3d_field.tga" -}; - -// OpenGL texture object IDs -GLuint tex_id[ NUM_TEXTURES ]; - - -//======================================================================== -// Global variables -//======================================================================== - -// Display information -int width, height; - -// Frame information -double thistime, oldtime, dt, starttime; - -// Camera information -int camerapos; - -// Player information -struct { - double ypos; // -1.0 to +1.0 - double yspeed; // -MAX_SPEED to +MAX_SPEED -} player1, player2; - -// Ball information -struct { - double xpos, ypos; - double xspeed, yspeed; -} ball; - -// And the winner is... -int winner; - -// Lighting configuration -const GLfloat env_ambient[4] = {1.0f,1.0f,1.0f,1.0f}; -const GLfloat light1_position[4] = {-3.0f,3.0f,2.0f,1.0f}; -const GLfloat light1_diffuse[4] = {1.0f,1.0f,1.0f,0.0f}; -const GLfloat light1_ambient[4] = {0.0f,0.0f,0.0f,0.0f}; - -// Object material properties -const GLfloat player1_diffuse[4] = {1.0f,0.3f,0.3f,1.0f}; -const GLfloat player1_ambient[4] = {0.3f,0.1f,0.0f,1.0f}; -const GLfloat player2_diffuse[4] = {0.3f,1.0f,0.3f,1.0f}; -const GLfloat player2_ambient[4] = {0.1f,0.3f,0.1f,1.0f}; -const GLfloat ball_diffuse[4] = {1.0f,1.0f,0.5f,1.0f}; -const GLfloat ball_ambient[4] = {0.3f,0.3f,0.1f,1.0f}; -const GLfloat border_diffuse[4] = {0.3f,0.3f,1.0f,1.0f}; -const GLfloat border_ambient[4] = {0.1f,0.1f,0.3f,1.0f}; -const GLfloat floor_diffuse[4] = {1.0f,1.0f,1.0f,1.0f}; -const GLfloat floor_ambient[4] = {0.3f,0.3f,0.3f,1.0f}; - - -//======================================================================== -// LoadTextures() - Load textures from disk and upload to OpenGL card -//======================================================================== - -GLboolean LoadTextures( void ) -{ - int i; - - // Generate texture objects - glGenTextures( NUM_TEXTURES, tex_id ); - - // Load textures - for( i = 0; i < NUM_TEXTURES; i ++ ) - { - // Select texture object - glBindTexture( GL_TEXTURE_2D, tex_id[ i ] ); - - // Set texture parameters - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - - // Upload texture from file to texture memory - if( !glfwLoadTexture2D( tex_name[ i ], 0 ) ) - { - fprintf( stderr, "Failed to load texture %s\n", tex_name[ i ] ); - return GL_FALSE; - } - } - - return GL_TRUE; -} - - -//======================================================================== -// DrawImage() - Draw a 2D image as a texture -//======================================================================== - -void DrawImage( int texnum, float x1, float x2, float y1, float y2 ) -{ - glEnable( GL_TEXTURE_2D ); - glBindTexture( GL_TEXTURE_2D, tex_id[ texnum ] ); - glBegin( GL_QUADS ); - glTexCoord2f( 0.0f, 1.0f ); - glVertex2f( x1, y1 ); - glTexCoord2f( 1.0f, 1.0f ); - glVertex2f( x2, y1 ); - glTexCoord2f( 1.0f, 0.0f ); - glVertex2f( x2, y2 ); - glTexCoord2f( 0.0f, 0.0f ); - glVertex2f( x1, y2 ); - glEnd(); - glDisable( GL_TEXTURE_2D ); -} - - -//======================================================================== -// GameMenu() - Game menu (returns menu option) -//======================================================================== - -int GameMenu( void ) -{ - int option; - - // Enable sticky keys - glfwEnable( GLFW_STICKY_KEYS ); - - // Wait for a game menu key to be pressed - do - { - // Get window size - glfwGetWindowSize( &width, &height ); - - // Set viewport - glViewport( 0, 0, width, height ); - - // Clear display - glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); - glClear( GL_COLOR_BUFFER_BIT ); - - // Setup projection matrix - glMatrixMode( GL_PROJECTION ); - glLoadIdentity(); - glOrtho( 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f ); - - // Setup modelview matrix - glMatrixMode( GL_MODELVIEW ); - glLoadIdentity(); - - // Display title - glColor3f( 1.0f, 1.0f, 1.0f ); - DrawImage( TEX_TITLE, 0.1f, 0.9f, 0.0f, 0.3f ); - - // Display menu - glColor3f( 1.0f, 1.0f, 0.0f ); - DrawImage( TEX_MENU, 0.38f, 0.62f, 0.35f, 0.5f ); - - // Display instructions - glColor3f( 0.0f, 1.0f, 1.0f ); - DrawImage( TEX_INSTR, 0.32f, 0.68f, 0.65f, 0.85f ); - - // Swap buffers - glfwSwapBuffers(); - - // Check for keys - if( glfwGetKey( 'Q' ) || !glfwGetWindowParam( GLFW_OPENED ) ) - { - option = MENU_QUIT; - } - else if( glfwGetKey( GLFW_KEY_F1 ) ) - { - option = MENU_PLAY; - } - else - { - option = MENU_NONE; - } - - // To avoid horrible busy waiting, sleep for at least 20 ms - glfwSleep( 0.02 ); - } - while( option == MENU_NONE ); - - // Disable sticky keys - glfwDisable( GLFW_STICKY_KEYS ); - - return option; -} - - -//======================================================================== -// NewGame() - Initialize a new game -//======================================================================== - -void NewGame( void ) -{ - // Frame information - starttime = thistime = glfwGetTime(); - - // Camera information - camerapos = CAMERA_DEFAULT; - - // Player 1 information - player1.ypos = 0.0; - player1.yspeed = 0.0; - - // Player 2 information - player2.ypos = 0.0; - player2.yspeed = 0.0; - - // Ball information - ball.xpos = -1.0 + PLAYER_XSIZE; - ball.ypos = player1.ypos; - ball.xspeed = 1.0; - ball.yspeed = 1.0; -} - - -//======================================================================== -// PlayerControl() - Player control -//======================================================================== - -void PlayerControl( void ) -{ - float joy1pos[ 2 ], joy2pos[ 2 ]; - - // Get joystick X & Y axis positions - glfwGetJoystickPos( GLFW_JOYSTICK_1, joy1pos, 2 ); - glfwGetJoystickPos( GLFW_JOYSTICK_2, joy2pos, 2 ); - - // Player 1 control - if( glfwGetKey( 'A' ) || joy1pos[ 1 ] > 0.2f ) - { - player1.yspeed += dt * ACCELERATION; - if( player1.yspeed > MAX_SPEED ) - { - player1.yspeed = MAX_SPEED; - } - } - else if( glfwGetKey( 'Z' ) || joy1pos[ 1 ] < -0.2f ) - { - player1.yspeed -= dt * ACCELERATION; - if( player1.yspeed < -MAX_SPEED ) - { - player1.yspeed = -MAX_SPEED; - } - } - else - { - player1.yspeed /= exp( DECELERATION * dt ); - } - - // Player 2 control - if( glfwGetKey( 'K' ) || joy2pos[ 1 ] > 0.2f ) - { - player2.yspeed += dt * ACCELERATION; - if( player2.yspeed > MAX_SPEED ) - { - player2.yspeed = MAX_SPEED; - } - } - else if( glfwGetKey( 'M' ) || joy2pos[ 1 ] < -0.2f ) - { - player2.yspeed -= dt * ACCELERATION; - if( player2.yspeed < -MAX_SPEED ) - { - player2.yspeed = -MAX_SPEED; - } - } - else - { - player2.yspeed /= exp( DECELERATION * dt ); - } - - // Update player 1 position - player1.ypos += dt * player1.yspeed; - if( player1.ypos > 1.0 - PLAYER_YSIZE ) - { - player1.ypos = 1.0 - PLAYER_YSIZE; - player1.yspeed = 0.0; - } - else if( player1.ypos < -1.0 + PLAYER_YSIZE ) - { - player1.ypos = -1.0 + PLAYER_YSIZE; - player1.yspeed = 0.0; - } - - // Update player 2 position - player2.ypos += dt * player2.yspeed; - if( player2.ypos > 1.0 - PLAYER_YSIZE ) - { - player2.ypos = 1.0 - PLAYER_YSIZE; - player2.yspeed = 0.0; - } - else if( player2.ypos < -1.0 + PLAYER_YSIZE ) - { - player2.ypos = -1.0 + PLAYER_YSIZE; - player2.yspeed = 0.0; - } -} - - -//======================================================================== -// BallControl() - Ball control -//======================================================================== - -int BallControl( void ) -{ - int event; - double ballspeed; - - // Calculate new ball speed - ballspeed = BALL_SPEED * (1.0 + 0.02*(thistime-starttime)); - ball.xspeed = ball.xspeed > 0 ? ballspeed : -ballspeed; - ball.yspeed = ball.yspeed > 0 ? ballspeed : -ballspeed; - ball.yspeed *= 0.74321; - - // Update ball position - ball.xpos += dt * ball.xspeed; - ball.ypos += dt * ball.yspeed; - - // Did the ball hit a top/bottom wall? - if( ball.ypos >= 1.0 ) - { - ball.ypos = 2.0 - ball.ypos; - ball.yspeed = -ball.yspeed; - } - else if( ball.ypos <= -1.0 ) - { - ball.ypos = -2.0 - ball.ypos; - ball.yspeed = -ball.yspeed; - } - - // Did the ball hit/miss a player? - event = NOBODY_WINS; - - // Is the ball entering the player 1 goal? - if( ball.xpos < -1.0 + PLAYER_XSIZE ) - { - // Did player 1 catch the ball? - if( ball.ypos > (player1.ypos-PLAYER_YSIZE) && - ball.ypos < (player1.ypos+PLAYER_YSIZE) ) - { - ball.xpos = -2.0 + 2.0*PLAYER_XSIZE - ball.xpos; - ball.xspeed = -ball.xspeed; - } - else - { - event = PLAYER2_WINS; - } - } - - // Is the ball entering the player 2 goal? - if( ball.xpos > 1.0 - PLAYER_XSIZE ) - { - // Did player 2 catch the ball? - if( ball.ypos > (player2.ypos-PLAYER_YSIZE) && - ball.ypos < (player2.ypos+PLAYER_YSIZE) ) - { - ball.xpos = 2.0 - 2.0*PLAYER_XSIZE - ball.xpos; - ball.xspeed = -ball.xspeed; - } - else - { - event = PLAYER1_WINS; - } - } - - return event; -} - - -//======================================================================== -// DrawBox() - Draw a 3D box -//======================================================================== - -#define TEX_SCALE 4.0f - - -void DrawBox( float x1, float y1, float z1, float x2, float y2, float z2 ) -{ - // Draw six sides of a cube - glBegin( GL_QUADS ); - // Side 1 (down) - glNormal3f( 0.0f, 0.0f, -1.0f ); - glTexCoord2f( 0.0f, 0.0f ); - glVertex3f( x1,y2,z1 ); - glTexCoord2f( TEX_SCALE, 0.0f ); - glVertex3f( x2,y2,z1 ); - glTexCoord2f( TEX_SCALE, TEX_SCALE ); - glVertex3f( x2,y1,z1 ); - glTexCoord2f( 0.0f, TEX_SCALE ); - glVertex3f( x1,y1,z1 ); - // Side 2 (up) - glNormal3f( 0.0f, 0.0f, 1.0f ); - glTexCoord2f( 0.0f, 0.0f ); - glVertex3f( x1,y1,z2 ); - glTexCoord2f( TEX_SCALE, 0.0f ); - glVertex3f( x2,y1,z2 ); - glTexCoord2f( TEX_SCALE, TEX_SCALE ); - glVertex3f( x2,y2,z2 ); - glTexCoord2f( 0.0f, TEX_SCALE ); - glVertex3f( x1,y2,z2 ); - // Side 3 (backward) - glNormal3f( 0.0f, -1.0f, 0.0f ); - glTexCoord2f( 0.0f, 0.0f ); - glVertex3f( x1,y1,z1 ); - glTexCoord2f( TEX_SCALE, 0.0f ); - glVertex3f( x2,y1,z1 ); - glTexCoord2f( TEX_SCALE, TEX_SCALE ); - glVertex3f( x2,y1,z2 ); - glTexCoord2f( 0.0f, TEX_SCALE ); - glVertex3f( x1,y1,z2 ); - // Side 4 (forward) - glNormal3f( 0.0f, 1.0f, 0.0f ); - glTexCoord2f( 0.0f, 0.0f ); - glVertex3f( x1,y2,z2 ); - glTexCoord2f( TEX_SCALE, 0.0f ); - glVertex3f( x2,y2,z2 ); - glTexCoord2f( TEX_SCALE, TEX_SCALE ); - glVertex3f( x2,y2,z1 ); - glTexCoord2f( 0.0f, TEX_SCALE ); - glVertex3f( x1,y2,z1 ); - // Side 5 (left) - glNormal3f( -1.0f, 0.0f, 0.0f ); - glTexCoord2f( 0.0f, 0.0f ); - glVertex3f( x1,y1,z2 ); - glTexCoord2f( TEX_SCALE, 0.0f ); - glVertex3f( x1,y2,z2 ); - glTexCoord2f( TEX_SCALE, TEX_SCALE ); - glVertex3f( x1,y2,z1 ); - glTexCoord2f( 0.0f, TEX_SCALE ); - glVertex3f( x1,y1,z1 ); - // Side 6 (right) - glNormal3f( 1.0f, 0.0f, 0.0f ); - glTexCoord2f( 0.0f, 0.0f ); - glVertex3f( x2,y1,z1 ); - glTexCoord2f( TEX_SCALE, 0.0f ); - glVertex3f( x2,y2,z1 ); - glTexCoord2f( TEX_SCALE, TEX_SCALE ); - glVertex3f( x2,y2,z2 ); - glTexCoord2f( 0.0f, TEX_SCALE ); - glVertex3f( x2,y1,z2 ); - glEnd(); -} - - -//======================================================================== -// UpdateDisplay() - Draw graphics (all game related OpenGL stuff goes -// here) -//======================================================================== - -void UpdateDisplay( void ) -{ - // Get window size - glfwGetWindowSize( &width, &height ); - - // Set viewport - glViewport( 0, 0, width, height ); - - // Clear display - glClearColor( 0.02f, 0.02f, 0.02f, 0.0f ); - glClearDepth( 1.0f ); - glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); - - // Setup projection matrix - glMatrixMode( GL_PROJECTION ); - glLoadIdentity(); - gluPerspective( - 55.0f, // Angle of view - (GLfloat)width/(GLfloat)height, // Aspect - 1.0f, // Near Z - 100.0f // Far Z - ); - - // Setup modelview matrix - glMatrixMode( GL_MODELVIEW ); - glLoadIdentity(); - switch( camerapos ) - { - default: - case CAMERA_CLASSIC: - gluLookAt( - 0.0f, 0.0f, 2.5f, - 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f - ); - break; - case CAMERA_ABOVE: - gluLookAt( - 0.0f, 0.0f, 2.5f, - (float)ball.xpos, (float)ball.ypos, 0.0f, - 0.0f, 1.0f, 0.0f - ); - break; - case CAMERA_SPECTATOR: - gluLookAt( - 0.0f, -2.0, 1.2f, - (float)ball.xpos, (float)ball.ypos, 0.0f, - 0.0f, 0.0f, 1.0f - ); - break; - } - - // Enable depth testing - glEnable( GL_DEPTH_TEST ); - glDepthFunc( GL_LEQUAL ); - - // Enable lighting - glEnable( GL_LIGHTING ); - glLightModelfv( GL_LIGHT_MODEL_AMBIENT, env_ambient ); - glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE ); - glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE ); - glLightfv( GL_LIGHT1, GL_POSITION, light1_position ); - glLightfv( GL_LIGHT1, GL_DIFFUSE, light1_diffuse ); - glLightfv( GL_LIGHT1, GL_AMBIENT, light1_ambient ); - glEnable( GL_LIGHT1 ); - - // Front face is counter-clock-wise - glFrontFace( GL_CCW ); - - // Enable face culling (not necessary, but speeds up rendering) - glCullFace( GL_BACK ); - glEnable( GL_CULL_FACE ); - - // Draw Player 1 - glMaterialfv( GL_FRONT, GL_DIFFUSE, player1_diffuse ); - glMaterialfv( GL_FRONT, GL_AMBIENT, player1_ambient ); - DrawBox( -1.f, (GLfloat)player1.ypos-PLAYER_YSIZE, 0.f, - -1.f+PLAYER_XSIZE, (GLfloat)player1.ypos+PLAYER_YSIZE, 0.1f ); - - // Draw Player 2 - glMaterialfv( GL_FRONT, GL_DIFFUSE, player2_diffuse ); - glMaterialfv( GL_FRONT, GL_AMBIENT, player2_ambient ); - DrawBox( 1.f-PLAYER_XSIZE, (GLfloat)player2.ypos-PLAYER_YSIZE, 0.f, - 1.f, (GLfloat)player2.ypos+PLAYER_YSIZE, 0.1f ); - - // Draw Ball - glMaterialfv( GL_FRONT, GL_DIFFUSE, ball_diffuse ); - glMaterialfv( GL_FRONT, GL_AMBIENT, ball_ambient ); - DrawBox( (GLfloat)ball.xpos-BALL_SIZE, (GLfloat)ball.ypos-BALL_SIZE, 0.f, - (GLfloat)ball.xpos+BALL_SIZE, (GLfloat)ball.ypos+BALL_SIZE, BALL_SIZE*2 ); - - // Top game field border - glMaterialfv( GL_FRONT, GL_DIFFUSE, border_diffuse ); - glMaterialfv( GL_FRONT, GL_AMBIENT, border_ambient ); - DrawBox( -1.1f, 1.0f, 0.0f, 1.1f, 1.1f, 0.1f ); - // Bottom game field border - glColor3f( 0.0f, 0.0f, 0.7f ); - DrawBox( -1.1f, -1.1f, 0.0f, 1.1f, -1.0f, 0.1f ); - // Left game field border - DrawBox( -1.1f, -1.0f, 0.0f, -1.0f, 1.0f, 0.1f ); - // Left game field border - DrawBox( 1.0f, -1.0f, 0.0f, 1.1f, 1.0f, 0.1f ); - - // Enable texturing - glEnable( GL_TEXTURE_2D ); - glBindTexture( GL_TEXTURE_2D, tex_id[ TEX_FIELD ] ); - - // Game field floor - glMaterialfv( GL_FRONT, GL_DIFFUSE, floor_diffuse ); - glMaterialfv( GL_FRONT, GL_AMBIENT, floor_ambient ); - DrawBox( -1.01f, -1.01f, -0.01f, 1.01f, 1.01f, 0.0f ); - - // Disable texturing - glDisable( GL_TEXTURE_2D ); - - // Disable face culling - glDisable( GL_CULL_FACE ); - - // Disable lighting - glDisable( GL_LIGHTING ); - - // Disable depth testing - glDisable( GL_DEPTH_TEST ); -} - - -//======================================================================== -// GameOver() -//======================================================================== - -void GameOver( void ) -{ - // Enable sticky keys - glfwEnable( GLFW_STICKY_KEYS ); - - // Until the user presses ESC or SPACE - while( !glfwGetKey( GLFW_KEY_ESC ) && !glfwGetKey( ' ' ) && - glfwGetWindowParam( GLFW_OPENED ) ) - { - // Draw display - UpdateDisplay(); - - // Setup projection matrix - glMatrixMode( GL_PROJECTION ); - glLoadIdentity(); - glOrtho( 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f ); - - // Setup modelview matrix - glMatrixMode( GL_MODELVIEW ); - glLoadIdentity(); - - // Enable blending - glEnable( GL_BLEND ); - - // Dim background - glBlendFunc( GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA ); - glColor4f( 0.3f, 0.3f, 0.3f, 0.3f ); - glBegin( GL_QUADS ); - glVertex2f( 0.0f, 0.0f ); - glVertex2f( 1.0f, 0.0f ); - glVertex2f( 1.0f, 1.0f ); - glVertex2f( 0.0f, 1.0f ); - glEnd(); - - // Display winner text - glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_COLOR ); - if( winner == PLAYER1 ) - { - glColor4f( 1.0f, 0.5f, 0.5f, 1.0f ); - DrawImage( TEX_WINNER1, 0.35f, 0.65f, 0.46f, 0.54f ); - } - else if( winner == PLAYER2 ) - { - glColor4f( 0.5f, 1.0f, 0.5f, 1.0f ); - DrawImage( TEX_WINNER2, 0.35f, 0.65f, 0.46f, 0.54f ); - } - - // Disable blending - glDisable( GL_BLEND ); - - // Swap buffers - glfwSwapBuffers(); - } - - // Disable sticky keys - glfwDisable( GLFW_STICKY_KEYS ); -} - - -//======================================================================== -// GameLoop() - Game loop -//======================================================================== - -void GameLoop( void ) -{ - int playing, event; - - // Initialize a new game - NewGame(); - - // Enable sticky keys - glfwEnable( GLFW_STICKY_KEYS ); - - // Loop until the game ends - playing = GL_TRUE; - while( playing && glfwGetWindowParam( GLFW_OPENED ) ) - { - // Frame timer - oldtime = thistime; - thistime = glfwGetTime(); - dt = thistime - oldtime; - - // Get user input and update player positions - PlayerControl(); - - // Move the ball, and check if a player hits/misses the ball - event = BallControl(); - - // Did we have a winner? - switch( event ) - { - case PLAYER1_WINS: - winner = PLAYER1; - playing = GL_FALSE; - break; - case PLAYER2_WINS: - winner = PLAYER2; - playing = GL_FALSE; - break; - default: - break; - } - - // Did the user press ESC? - if( glfwGetKey( GLFW_KEY_ESC ) ) - { - playing = GL_FALSE; - } - - // Did the user change camera view? - if( glfwGetKey( '1' ) ) - { - camerapos = CAMERA_CLASSIC; - } - else if( glfwGetKey( '2' ) ) - { - camerapos = CAMERA_ABOVE; - } - else if( glfwGetKey( '3' ) ) - { - camerapos = CAMERA_SPECTATOR; - } - - // Draw display - UpdateDisplay(); - - // Swap buffers - glfwSwapBuffers(); - } - - // Disable sticky keys - glfwDisable( GLFW_STICKY_KEYS ); - - // Show winner - GameOver(); -} - - -//======================================================================== -// main() - Program entry point -//======================================================================== - -int main( void ) -{ - int menuoption; - - // Initialize GLFW - if( !glfwInit() ) - { - fprintf( stderr, "Failed to initialize GLFW\n" ); - exit( EXIT_FAILURE ); - } - - // Open OpenGL window - if( !glfwCreateWindow( WIDTH, HEIGHT, 0,0,0,0, 16,0, GLFW_FULLSCREEN ) ) - { - fprintf( stderr, "Failed to open GLFW window\n" ); - glfwTerminate(); - exit( EXIT_FAILURE ); - } - - glfwSwapInterval( 1 ); - - // Load all textures - if( !LoadTextures() ) - { - glfwTerminate(); - exit( EXIT_FAILURE ); - } - - // Main loop - do - { - // Get menu option - menuoption = GameMenu(); - - // If the user wants to play, let him... - if( menuoption == MENU_PLAY ) - { - GameLoop(); - } - } - while( menuoption != MENU_QUIT ); - - // Unload all textures - if( glfwGetWindowParam( GLFW_OPENED ) ) - { - glDeleteTextures( NUM_TEXTURES, tex_id ); - } - - // Terminate GLFW - glfwTerminate(); - - exit( EXIT_SUCCESS ); -} - diff --git a/examples/pong3d_field.tga b/examples/pong3d_field.tga deleted file mode 100644 index cc20bbdb..00000000 Binary files a/examples/pong3d_field.tga and /dev/null differ diff --git a/examples/pong3d_instr.tga b/examples/pong3d_instr.tga deleted file mode 100644 index 758eb447..00000000 Binary files a/examples/pong3d_instr.tga and /dev/null differ diff --git a/examples/pong3d_menu.tga b/examples/pong3d_menu.tga deleted file mode 100644 index d0d6c5a4..00000000 Binary files a/examples/pong3d_menu.tga and /dev/null differ diff --git a/examples/pong3d_title.tga b/examples/pong3d_title.tga deleted file mode 100644 index d0d8e36d..00000000 Binary files a/examples/pong3d_title.tga and /dev/null differ diff --git a/examples/pong3d_winner1.tga b/examples/pong3d_winner1.tga deleted file mode 100644 index f963720c..00000000 Binary files a/examples/pong3d_winner1.tga and /dev/null differ diff --git a/examples/pong3d_winner2.tga b/examples/pong3d_winner2.tga deleted file mode 100644 index ea8266de..00000000 Binary files a/examples/pong3d_winner2.tga and /dev/null differ diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index 00f832f0..712987af 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -67,12 +67,6 @@ extern "C" { #endif #endif /* APIENTRY */ -/* TEMPORARY MinGW-w64 hacks. - */ -#if __MINGW64__ - #define WINAPI -#endif - /* The following three defines are here solely to make some Windows-based * files happy. Theoretically we could include , but * it has the major drawback of severely polluting our namespace. @@ -595,7 +589,7 @@ GLFWAPI void glfwSetScrollCallback(GLFWscrollfun cbfun); /* Joystick input */ GLFWAPI int glfwGetJoystickParam(int joy, int param); -GLFWAPI int glfwGetJoystickPos(int joy, float* pos, int numaxes); +GLFWAPI int glfwGetJoystickAxes(int joy, float* axes, int numaxes); GLFWAPI int glfwGetJoystickButtons(int joy, unsigned char* buttons, int numbuttons); /* Clipboard */ diff --git a/readme.html b/readme.html index 0ef6875a..2eeda27b 100644 --- a/readme.html +++ b/readme.html @@ -300,6 +300,7 @@ version of GLFW.

  • Renamed GLFW_BUILD_DLL to _GLFW_BUILD_DLL
  • Renamed version test to glfwinfo
  • Renamed GLFW_NO_GLU to GLFW_INCLUDE_GLU and made it disabled by default
  • +
  • Renamed glfwGetJoystickPos to glfwGetJoystickAxes to match glfwGetJoystickButtons
  • Renamed mouse position functions to cursor position equivalents
  • Replaced glfwOpenWindow and glfwCloseWindow with glfwCreateWindow and glfwDestroyWindow
  • Replaced ad hoc build system with CMake
  • @@ -953,6 +954,8 @@ their skills. Special thanks go out to:

  • Samuli Tuomola, for support, bug reports and testing
  • +
  • Torsten Walluhn, for fixing various compilation issues on OS X
  • +
  • Frank Wille, for helping with the AmigaOS port and making GLFW compile under IRIX 5.3
  • diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 27fdf203..c7ffc3d9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,7 +3,7 @@ include_directories(${GLFW_SOURCE_DIR}/src ${glfw_INCLUDE_DIRS}) set(common_HEADERS ${GLFW_SOURCE_DIR}/include/GL/glfw3.h internal.h) -set(common_SOURCES clipboard.c error.c fullscreen.c gamma.c init.c input.c +set(common_SOURCES clipboard.c fullscreen.c gamma.c init.c input.c joystick.c monitor.c opengl.c time.c window.c) if (_GLFW_COCOA_NSGL) @@ -40,10 +40,13 @@ if (BUILD_SHARED_LIBS) if (_GLFW_WIN32_WGL) # The GLFW DLL needs a special compile-time macro and import library name - set_target_properties(glfw PROPERTIES - PREFIX "" - IMPORT_PREFIX "" - IMPORT_SUFFIX "dll.lib") + set_target_properties(glfw PROPERTIES PREFIX "" IMPORT_PREFIX "") + + if (MINGW) + set_target_properties(glfw PROPERTIES IMPORT_SUFFIX "dll.a") + else() + set_target_properties(glfw PROPERTIES IMPORT_SUFFIX "dll.lib") + endif() elseif (_GLFW_COCOA_NSGL) # Append -fno-common to the compile flags to work around a bug in the Apple GCC get_target_property(glfw_CFLAGS glfw COMPILE_FLAGS) diff --git a/src/cocoa_clipboard.m b/src/cocoa_clipboard.m index 56b98437..99918f77 100644 --- a/src/cocoa_clipboard.m +++ b/src/cocoa_clipboard.m @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Cocoa/NSOpenGL +// Platform: Cocoa // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -70,7 +70,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) if (!object) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Cocoa/NSGL: Failed to retrieve object from pasteboard"); + "Cocoa: Failed to retrieve object from pasteboard"); return NULL; } diff --git a/src/cocoa_fullscreen.m b/src/cocoa_fullscreen.m index 9382993b..47f86739 100644 --- a/src/cocoa_fullscreen.m +++ b/src/cocoa_fullscreen.m @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Cocoa/NSOpenGL +// Platform: Cocoa // API Version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/cocoa_gamma.c b/src/cocoa_gamma.c index 53c47a89..85d1e489 100644 --- a/src/cocoa_gamma.c +++ b/src/cocoa_gamma.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Cocoa/NSOpenGL +// Platform: Cocoa // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -40,7 +40,7 @@ //************************************************************************ //======================================================================== -// Save the original gamma ramp so that we can restore it later +// Save the original gamma ramp so that it can be restored later //======================================================================== void _glfwPlatformGetGammaRamp(GLFWgammaramp* ramp) diff --git a/src/cocoa_input.m b/src/cocoa_input.m index 869c9cd3..11e1083b 100644 --- a/src/cocoa_input.m +++ b/src/cocoa_input.m @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Cocoa/NSOpenGL +// Platform: Cocoa // API Version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -40,13 +40,14 @@ void _glfwPlatformEnableSystemKeys(_GLFWwindow* window) { - // This is checked in macosx_window.m; we take no action here + // This is checked in cocoa_window.m; no action needed here } void _glfwPlatformDisableSystemKeys(_GLFWwindow* window) { - // This is checked in macosx_window.m; we take no action here - // I don't think it's really possible to disable stuff like Exposé + // This is checked in cocoa_window.m; no action needed here + + // Note that it may not be possible to disable things like Exposé // except in full-screen mode. } diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index bf567f86..7eac7f91 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -1,10 +1,11 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Cocoa/NSOpenGL +// Platform: Cocoa // API Version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ // Copyright (c) 2009-2010 Camilla Berglund +// Copyright (c) 2012 Torsten Walluhn // // This software is provided 'as-is', without any express or implied // warranty. In no event will the authors be held liable for any damages @@ -46,7 +47,6 @@ //------------------------------------------------------------------------ // Joystick element information //------------------------------------------------------------------------ - typedef struct { IOHIDElementCookie cookie; @@ -65,7 +65,6 @@ typedef struct //------------------------------------------------------------------------ // Joystick information & state //------------------------------------------------------------------------ - typedef struct { int present; @@ -86,7 +85,7 @@ typedef struct static _glfwJoystick _glfwJoysticks[GLFW_JOYSTICK_LAST + 1]; -void getElementsCFArrayHandler(const void* value, void* parameter); +static void getElementsCFArrayHandler(const void* value, void* parameter); //======================================================================== @@ -213,7 +212,7 @@ static long getElementValue(_glfwJoystick* joystick, _glfwJoystickElement* eleme &hidEvent); if (kIOReturnSuccess == result) { - /* record min and max for auto calibration */ + // Record min and max for auto calibration if (hidEvent.value < element->minReport) element->minReport = hidEvent.value; if (hidEvent.value > element->maxReport) @@ -221,7 +220,7 @@ static long getElementValue(_glfwJoystick* joystick, _glfwJoystickElement* eleme } } - /* auto user scale */ + // Auto user scale return (long) hidEvent.value; } @@ -284,7 +283,7 @@ static void removalCallback(void* target, IOReturn result, void* refcon, void* s //======================================================================== -// Polls for joystick events and updates GFLW state +// Polls for joystick events and updates GLFW state //======================================================================== static void pollJoystickEvents(void) @@ -384,14 +383,14 @@ void _glfwInitJoysticks(void) if (result != kIOReturnSuccess) continue; - /* Check device type */ + // Check device type refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey)); if (refCF) { CFNumberGetValue(refCF, kCFNumberLongType, &usagePage); if (usagePage != kHIDPage_GenericDesktop) { - /* We are not interested in this device */ + // This device is not relevant to GLFW continue; } } @@ -405,7 +404,7 @@ void _glfwInitJoysticks(void) usage != kHIDUsage_GD_GamePad && usage != kHIDUsage_GD_MultiAxisController)) { - /* We are not interested in this device */ + // This device is not relevant to GLFW continue; } } @@ -439,7 +438,7 @@ void _glfwInitJoysticks(void) joystick, joystick); - /* Get product string */ + // Get product string refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey)); if (refCF) { @@ -529,7 +528,7 @@ int _glfwPlatformGetJoystickParam(int joy, int param) // Get joystick axis positions //======================================================================== -int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) +int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numaxes) { int i; @@ -557,14 +556,12 @@ int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) long readScale = axes->maxReport - axes->minReport; if (readScale == 0) - pos[i] = axes->value; + axes[i] = axes->value; else - pos[i] = (2.0f * (axes->value - axes->minReport) / readScale) - 1.0f; - - //printf("%ld, %ld, %ld\n", axes->value, axes->minReport, axes->maxReport); + axes[i] = (2.0f * (axes->value - axes->minReport) / readScale) - 1.0f; if (i & 1) - pos[i] = -pos[i]; + axes[i] = -axes[i]; } return numaxes; @@ -610,16 +607,16 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, { _glfwJoystickElement* hat = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.hats, i); - const int value = hat->value; + int value = hat->value; if (value < 0 || value > 8) value = 8; for (j = 0; j < 4 && button < numbuttons; j++) { if (directions[value] & (1 << j)) - buttons[button = GLFW_PRESS; + buttons[button] = GLFW_PRESS; else - buttons[button = GLFW_RELEASE; + buttons[button] = GLFW_RELEASE; button++; } diff --git a/src/cocoa_time.c b/src/cocoa_time.c index 745b4239..d80dd147 100644 --- a/src/cocoa_time.c +++ b/src/cocoa_time.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Cocoa/NSOpenGL +// Platform: Cocoa // API Version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/cocoa_window.m b/src/cocoa_window.m index d7764f92..5129a21f 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -277,9 +277,6 @@ static int convertMacKeyCode(unsigned int macKeyCode) if (macKeyCode >= 128) return -1; - // This treats keycodes as *positional*; that is, we'll return 'a' - // for the key left of 's', even on an AZERTY keyboard. The charInput - // function should still get 'q' though. return table[macKeyCode]; } @@ -541,7 +538,7 @@ static NSString* findAppName(void) } } - // If we get here, we're unbundled + // If we get here, the application is unbundled ProcessSerialNumber psn = { 0, kCurrentProcess }; TransformProcessType(&psn, kProcessTransformToForegroundApplication); @@ -551,10 +548,7 @@ static NSString* findAppName(void) char** progname = _NSGetProgname(); if (progname && *progname) - { - // TODO: UTF-8? return [NSString stringWithUTF8String:*progname]; - } // Really shouldn't get here return @"GLFW Application"; @@ -680,8 +674,7 @@ static GLboolean createWindow(_GLFWwindow* window, if (window->NS.object == nil) { - _glfwSetError(GLFW_PLATFORM_ERROR, - "Cocoa/NSOpenGL: Failed to create window"); + _glfwSetError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to create window"); return GL_FALSE; } @@ -723,8 +716,8 @@ static GLboolean createContext(_GLFWwindow* window, (wndconfig->glMajor == 3 && wndconfig->glMinor != 2)) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Cocoa/NSOpenGL: The targeted version of Mac OS X does " - "not support any OpenGL version above 2.1 except 3.2"); + "NSOpenGL: The targeted version of Mac OS X does not " + "support any OpenGL version above 2.1 except 3.2"); return GL_FALSE; } @@ -733,8 +726,8 @@ static GLboolean createContext(_GLFWwindow* window, if (!wndconfig->glForward) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Cocoa/NSOpenGL: The targeted version of Mac OS X " - "only supports OpenGL 3.2 contexts if they are " + "NSOpenGL: The targeted version of Mac OS X only " + "supports OpenGL 3.2 contexts if they are " "forward-compatible"); return GL_FALSE; } @@ -742,8 +735,8 @@ static GLboolean createContext(_GLFWwindow* window, if (wndconfig->glProfile != GLFW_OPENGL_CORE_PROFILE) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Cocoa/NSOpenGL: The targeted version of Mac OS X " - "only supports OpenGL 3.2 contexts if they use the " + "NSOpenGL: The targeted version of Mac OS X only " + "supports OpenGL 3.2 contexts if they use the " "core profile"); return GL_FALSE; } @@ -753,8 +746,8 @@ static GLboolean createContext(_GLFWwindow* window, if (wndconfig->glMajor > 2) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Cocoa/NSOpenGL: The targeted version of Mac OS X does " - "not support OpenGL version 3.0 or above"); + "NSOpenGL: The targeted version of Mac OS X does not " + "support OpenGL version 3.0 or above"); return GL_FALSE; } #endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/ @@ -763,8 +756,8 @@ static GLboolean createContext(_GLFWwindow* window, if (wndconfig->glRobustness) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Cocoa/NSOpenGL: Mac OS X does not support OpenGL " - "robustness strategies"); + "NSOpenGL: Mac OS X does not support OpenGL robustness " + "strategies"); return GL_FALSE; } @@ -827,7 +820,7 @@ static GLboolean createContext(_GLFWwindow* window, if (window->NSGL.pixelFormat == nil) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Cocoa/NSOpenGL: Failed to create OpenGL pixel format"); + "NSOpenGL: Failed to create OpenGL pixel format"); return GL_FALSE; } @@ -842,7 +835,7 @@ static GLboolean createContext(_GLFWwindow* window, if (window->NSGL.context == nil) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Cocoa/NSOpenGL: Failed to create OpenGL context"); + "NSOpenGL: Failed to create OpenGL context"); return GL_FALSE; } @@ -866,16 +859,15 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, if (!initializeAppKit()) return GL_FALSE; - // We can only have one application delegate, but we only allocate it the - // first time we create a window to keep all window code in this file + // There can only be one application delegate, but we allocate it the + // first time a window is created to keep all window code in this file if (_glfwLibrary.NS.delegate == nil) { _glfwLibrary.NS.delegate = [[GLFWApplicationDelegate alloc] init]; if (_glfwLibrary.NS.delegate == nil) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Cocoa/NSOpenGL: Failed to create application " - "delegate"); + "Cocoa: Failed to create application delegate"); return GL_FALSE; } @@ -886,7 +878,7 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, if (window->NS.delegate == nil) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Cocoa/NSOpenGL: Failed to create window delegate"); + "Cocoa: Failed to create window delegate"); return GL_FALSE; } diff --git a/src/error.c b/src/error.c deleted file mode 100644 index d64cb7cc..00000000 --- a/src/error.c +++ /dev/null @@ -1,149 +0,0 @@ -//======================================================================== -// GLFW - An OpenGL library -// Platform: All -// API version: 3.0 -// WWW: http://www.glfw.org/ -//------------------------------------------------------------------------ -// Copyright (c) 2008-2010 Camilla Berglund -// -// This software is provided 'as-is', without any express or implied -// warranty. In no event will the authors be held liable for any damages -// arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it -// freely, subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; you must not -// claim that you wrote the original software. If you use this software -// in a product, an acknowledgment in the product documentation would -// be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, and must not -// be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source -// distribution. -// -//======================================================================== - -#include "internal.h" - -#include -#include - - -////////////////////////////////////////////////////////////////////////// -////// GLFW internal API ////// -////////////////////////////////////////////////////////////////////////// - -//======================================================================== -// The current error value and callback -// These are not in _glfwLibrary since they need to be initialized and -// accessible before glfwInit so it can report errors -//======================================================================== - -static int _glfwError = GLFW_NO_ERROR; -static GLFWerrorfun _glfwErrorCallback = NULL; - - -//======================================================================== -// Sets the current error value -// This function may be called without GLFW having been initialized -//======================================================================== - -void _glfwSetError(int error, const char* format, ...) -{ - if (_glfwErrorCallback) - { - char buffer[16384]; - const char* description; - - if (format) - { - int count; - va_list vl; - - va_start(vl, format); - count = vsnprintf(buffer, sizeof(buffer), format, vl); - va_end(vl); - - if (count < 0) - buffer[sizeof(buffer) - 1] = '\0'; - - description = buffer; - } - else - description = glfwErrorString(error); - - _glfwErrorCallback(error, description); - } - else - _glfwError = error; -} - - -////////////////////////////////////////////////////////////////////////// -////// GLFW public API ////// -////////////////////////////////////////////////////////////////////////// - -//======================================================================== -// Returns the current error value -// This function may be called without GLFW having been initialized -//======================================================================== - -GLFWAPI int glfwGetError(void) -{ - int error = _glfwError; - _glfwError = GLFW_NO_ERROR; - return error; -} - - -//======================================================================== -// Returns a string representation of the specified error value -// This function may be called without GLFW having been initialized -//======================================================================== - -GLFWAPI const char* glfwErrorString(int error) -{ - switch (error) - { - case GLFW_NO_ERROR: - return "No error"; - case GLFW_NOT_INITIALIZED: - return "The GLFW library is not initialized"; - case GLFW_NO_CURRENT_CONTEXT: - return "There is no current OpenGL context"; - case GLFW_INVALID_ENUM: - return "Invalid argument for enum parameter"; - case GLFW_INVALID_VALUE: - return "Invalid value for parameter"; - case GLFW_OUT_OF_MEMORY: - return "Out of memory"; - case GLFW_OPENGL_UNAVAILABLE: - return "OpenGL is not available on this machine"; - case GLFW_VERSION_UNAVAILABLE: - return "The requested OpenGL version is unavailable"; - case GLFW_PLATFORM_ERROR: - return "A platform-specific error occurred"; - case GLFW_WINDOW_NOT_ACTIVE: - return "The specified window is not active"; - case GLFW_FORMAT_UNAVAILABLE: - return "The requested format is unavailable"; - } - - return "ERROR: UNKNOWN ERROR TOKEN PASSED TO glfwErrorString"; -} - - -//======================================================================== -// Sets the callback function for GLFW errors -// This function may be called without GLFW having been initialized -//======================================================================== - -GLFWAPI void glfwSetErrorCallback(GLFWerrorfun cbfun) -{ - _glfwErrorCallback = cbfun; -} - diff --git a/src/fullscreen.c b/src/fullscreen.c index 05b94d74..a169f196 100644 --- a/src/fullscreen.c +++ b/src/fullscreen.c @@ -6,6 +6,7 @@ //------------------------------------------------------------------------ // Copyright (c) 2002-2006 Marcus Geelnard // Copyright (c) 2006-2010 Camilla Berglund +// Copyright (c) 2012 Torsten Walluhn // // This software is provided 'as-is', without any express or implied // warranty. In no event will the authors be held liable for any damages @@ -31,7 +32,11 @@ #include "internal.h" #include +#ifdef __APPLE__ +#include +#else #include +#endif //======================================================================== @@ -87,7 +92,7 @@ void _glfwSplitBPP(int bpp, int* red, int* green, int* blue) { int delta; - // We assume that by 32 they really meant 24 + // We assume that by 32 the user really meant 24 if (bpp == 32) bpp = 24; diff --git a/src/init.c b/src/init.c index f59a4f59..d0882c49 100644 --- a/src/init.c +++ b/src/init.c @@ -28,11 +28,82 @@ // //======================================================================== -#define _init_c_ #include "internal.h" #include #include +#include +#include + + +//------------------------------------------------------------------------ +// Flag indicating whether GLFW has been successfully initialized +//------------------------------------------------------------------------ +GLboolean _glfwInitialized = GL_FALSE; + + +//------------------------------------------------------------------------ +// All shared and API-specific global data protected by _glfwInitialized +// This should only be touched after a call to glfwInit that has not been +// followed by a call to glfwTerminate +//------------------------------------------------------------------------ +_GLFWlibrary _glfwLibrary; + + +//------------------------------------------------------------------------ +// The current GLFW error code +// This is outside of _glfwLibrary so it can be initialized and usable +// before glfwInit is called, which lets that function report errors +// TODO: Make this thread-local +//------------------------------------------------------------------------ +static int _glfwError = GLFW_NO_ERROR; + + +//------------------------------------------------------------------------ +// The current error callback +// This is outside of _glfwLibrary so it can be initialized and usable +// before glfwInit is called, which lets that function report errors +//------------------------------------------------------------------------ +static GLFWerrorfun _glfwErrorCallback = NULL; + + +////////////////////////////////////////////////////////////////////////// +////// GLFW internal API ////// +////////////////////////////////////////////////////////////////////////// + +//======================================================================== +// Sets the current error value +//======================================================================== + +void _glfwSetError(int error, const char* format, ...) +{ + if (_glfwErrorCallback) + { + char buffer[16384]; + const char* description; + + if (format) + { + int count; + va_list vl; + + va_start(vl, format); + count = vsnprintf(buffer, sizeof(buffer), format, vl); + va_end(vl); + + if (count < 0) + buffer[sizeof(buffer) - 1] = '\0'; + + description = buffer; + } + else + description = glfwErrorString(error); + + _glfwErrorCallback(error, description); + } + else + _glfwError = error; +} ////////////////////////////////////////////////////////////////////////// @@ -50,8 +121,7 @@ GLFWAPI int glfwInit(void) memset(&_glfwLibrary, 0, sizeof(_glfwLibrary)); - // Not all window hints have zero as their default value, so this - // needs to be here despite the memset above + // Not all window hints have zero as their default value _glfwSetDefaultWindowHints(); if (!_glfwPlatformInit()) @@ -90,6 +160,7 @@ GLFWAPI void glfwTerminate(void) //======================================================================== // Get GLFW version +// This function may be called without GLFW having been initialized //======================================================================== GLFWAPI void glfwGetVersion(int* major, int* minor, int* rev) @@ -107,6 +178,7 @@ GLFWAPI void glfwGetVersion(int* major, int* minor, int* rev) //======================================================================== // Get the GLFW version string +// This function may be called without GLFW having been initialized //======================================================================== GLFWAPI const char* glfwGetVersionString(void) @@ -114,3 +186,64 @@ GLFWAPI const char* glfwGetVersionString(void) return _glfwPlatformGetVersionString(); } + +//======================================================================== +// Returns the current error value +// This function may be called without GLFW having been initialized +//======================================================================== + +GLFWAPI int glfwGetError(void) +{ + int error = _glfwError; + _glfwError = GLFW_NO_ERROR; + return error; +} + + +//======================================================================== +// Returns a string representation of the specified error value +// This function may be called without GLFW having been initialized +//======================================================================== + +GLFWAPI const char* glfwErrorString(int error) +{ + switch (error) + { + case GLFW_NO_ERROR: + return "No error"; + case GLFW_NOT_INITIALIZED: + return "The GLFW library is not initialized"; + case GLFW_NO_CURRENT_CONTEXT: + return "There is no current OpenGL context"; + case GLFW_INVALID_ENUM: + return "Invalid argument for enum parameter"; + case GLFW_INVALID_VALUE: + return "Invalid value for parameter"; + case GLFW_OUT_OF_MEMORY: + return "Out of memory"; + case GLFW_OPENGL_UNAVAILABLE: + return "OpenGL is not available on this machine"; + case GLFW_VERSION_UNAVAILABLE: + return "The requested OpenGL version is unavailable"; + case GLFW_PLATFORM_ERROR: + return "A platform-specific error occurred"; + case GLFW_WINDOW_NOT_ACTIVE: + return "The specified window is not active"; + case GLFW_FORMAT_UNAVAILABLE: + return "The requested format is unavailable"; + } + + return "ERROR: UNKNOWN ERROR TOKEN PASSED TO glfwErrorString"; +} + + +//======================================================================== +// Sets the callback function for GLFW errors +// This function may be called without GLFW having been initialized +//======================================================================== + +GLFWAPI void glfwSetErrorCallback(GLFWerrorfun cbfun) +{ + _glfwErrorCallback = cbfun; +} + diff --git a/src/internal.h b/src/internal.h index 266892e5..ac0946b5 100755 --- a/src/internal.h +++ b/src/internal.h @@ -31,17 +31,6 @@ #ifndef _internal_h_ #define _internal_h_ -//======================================================================== -// GLFWGLOBAL is a macro that places all global variables in the init.c -// module (all other modules reference global variables as 'extern') -//======================================================================== - -#if defined(_init_c_) - #define GLFWGLOBAL -#else - #define GLFWGLOBAL extern -#endif - //======================================================================== // Input handling definitions @@ -269,53 +258,50 @@ struct _GLFWlibrary }; -//======================================================================== -// System independent global variables (GLFW internals) -//======================================================================== - -// Flag indicating if GLFW has been initialized -#if defined(_init_c_) -GLboolean _glfwInitialized = GL_FALSE; -#else -GLFWGLOBAL GLboolean _glfwInitialized; -#endif - -GLFWGLOBAL _GLFWlibrary _glfwLibrary; +//------------------------------------------------------------------------ +// Global state shared between compilation units of GLFW +// These are exported from and documented in init.c +//------------------------------------------------------------------------ +extern GLboolean _glfwInitialized; +extern _GLFWlibrary _glfwLibrary; //======================================================================== -// Prototypes for platform specific implementation functions +// Prototypes for the platform API +// This is the interface exposed by the platform-specific code for each +// platform and is called by the shared code of the public API +// It mirrors the public API except it uses objects instead of handles //======================================================================== -// Init/terminate +// Platform init and version int _glfwPlatformInit(void); int _glfwPlatformTerminate(void); const char* _glfwPlatformGetVersionString(void); -// Input +// Input mode support void _glfwPlatformEnableSystemKeys(_GLFWwindow* window); void _glfwPlatformDisableSystemKeys(_GLFWwindow* window); void _glfwPlatformSetCursorPos(_GLFWwindow* window, int x, int y); void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode); -// Fullscreen +// Video mode support GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count); void _glfwPlatformGetDesktopMode(GLFWvidmode* mode); -// Gamma ramp +// Gamma ramp support void _glfwPlatformGetGammaRamp(GLFWgammaramp* ramp); void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp); -// Clipboard +// Clipboard support void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string); const char* _glfwPlatformGetClipboardString(_GLFWwindow* window); -// Joystick +// Joystick input int _glfwPlatformGetJoystickParam(int joy, int param); -int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes); +int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numaxes); int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, int numbuttons); -// Time +// Time input double _glfwPlatformGetTime(void); void _glfwPlatformSetTime(double time); @@ -328,7 +314,7 @@ void _glfwPlatformSetWindowPos(_GLFWwindow* window, int x, int y); void _glfwPlatformIconifyWindow(_GLFWwindow* window); void _glfwPlatformRestoreWindow(_GLFWwindow* window); -// Event management +// Event processing void _glfwPlatformPollEvents(void); void _glfwPlatformWaitEvents(void); @@ -343,19 +329,12 @@ void _glfwPlatformCopyContext(_GLFWwindow* src, _GLFWwindow* dst, unsigned long //======================================================================== -// Prototypes for platform independent internal functions +// Prototypes for the event API +// This is used by the platform-specific code to notify the shared code of +// events that can be translated into state changes and/or callback calls, +// instead of directly calling callbacks or modifying shared state //======================================================================== -// Fullscren management (fullscreen.c) -int _glfwCompareVideoModes(const GLFWvidmode* first, const GLFWvidmode* second); -void _glfwSplitBPP(int bpp, int* red, int* green, int* blue); - -// Error handling (error.c) -void _glfwSetError(int error, const char* format, ...); - -// Window management (window.c) -void _glfwSetDefaultWindowHints(void); - // Window event notification (window.c) void _glfwInputWindowFocus(_GLFWwindow* window, GLboolean activated); void _glfwInputWindowPos(_GLFWwindow* window, int x, int y); @@ -372,6 +351,24 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action); void _glfwInputCursorMotion(_GLFWwindow* window, int x, int y); void _glfwInputCursorEnter(_GLFWwindow* window, int entered); + +//======================================================================== +// Prototypes for internal utility functions +// These functions are shared code and may be used by any part of GLFW +// Each platform may add its own utility functions, but those may only be +// called by the platform-specific code +//======================================================================== + +// Fullscren management (fullscreen.c) +int _glfwCompareVideoModes(const GLFWvidmode* first, const GLFWvidmode* second); +void _glfwSplitBPP(int bpp, int* red, int* green, int* blue); + +// Error handling (init.c) +void _glfwSetError(int error, const char* format, ...); + +// Window management (window.c) +void _glfwSetDefaultWindowHints(void); + // OpenGL context helpers (opengl.c) int _glfwStringInExtensionString(const char* string, const GLubyte* extensions); const _GLFWfbconfig* _glfwChooseFBConfig(const _GLFWfbconfig* desired, diff --git a/src/joystick.c b/src/joystick.c index 682ba4b0..84763951 100644 --- a/src/joystick.c +++ b/src/joystick.c @@ -47,6 +47,12 @@ GLFWAPI int glfwGetJoystickParam(int joy, int param) return 0; } + if (joy < 0 || joy > GLFW_JOYSTICK_LAST) + { + _glfwSetError(GLFW_INVALID_ENUM, NULL); + return 0; + } + return _glfwPlatformGetJoystickParam(joy, param); } @@ -55,7 +61,7 @@ GLFWAPI int glfwGetJoystickParam(int joy, int param) // Get joystick axis positions //======================================================================== -GLFWAPI int glfwGetJoystickPos(int joy, float* pos, int numaxes) +GLFWAPI int glfwGetJoystickAxes(int joy, float* axes, int numaxes) { int i; @@ -65,11 +71,23 @@ GLFWAPI int glfwGetJoystickPos(int joy, float* pos, int numaxes) return 0; } + if (joy < 0 || joy > GLFW_JOYSTICK_LAST) + { + _glfwSetError(GLFW_INVALID_ENUM, NULL); + return 0; + } + + if (axes == NULL || numaxes < 0) + { + _glfwSetError(GLFW_INVALID_VALUE, NULL); + return 0; + } + // Clear positions for (i = 0; i < numaxes; i++) - pos[i] = 0.0f; + axes[i] = 0.0f; - return _glfwPlatformGetJoystickPos(joy, pos, numaxes); + return _glfwPlatformGetJoystickAxes(joy, axes, numaxes); } @@ -89,6 +107,18 @@ GLFWAPI int glfwGetJoystickButtons(int joy, return 0; } + if (joy < 0 || joy > GLFW_JOYSTICK_LAST) + { + _glfwSetError(GLFW_INVALID_ENUM, NULL); + return 0; + } + + if (buttons == NULL || numbuttons < 0) + { + _glfwSetError(GLFW_INVALID_VALUE, NULL); + return 0; + } + // Clear button states for (i = 0; i < numbuttons; i++) buttons[i] = GLFW_RELEASE; diff --git a/src/opengl.c b/src/opengl.c index 67c6f414..a23a5f34 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -509,7 +509,7 @@ GLFWAPI GLFWwindow glfwGetCurrentContext(void) if (!_glfwInitialized) { _glfwSetError(GLFW_NOT_INITIALIZED, NULL); - return GL_FALSE; + return NULL; } return _glfwLibrary.currentWindow; diff --git a/src/win32_clipboard.c b/src/win32_clipboard.c index ca31964d..22cdb8f6 100644 --- a/src/win32_clipboard.c +++ b/src/win32_clipboard.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Win32/WGL +// Platform: Win32 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -53,7 +53,7 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) if (!wideString) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to convert clipboard string to " + "Win32: Failed to convert clipboard string to " "wide string"); return; } @@ -66,7 +66,7 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) free(wideString); _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to allocate global handle for clipboard"); + "Win32: Failed to allocate global handle for clipboard"); return; } @@ -79,7 +79,7 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) free(wideString); _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to open clipboard"); + "Win32: Failed to open clipboard"); return; } @@ -108,7 +108,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) if (!OpenClipboard(window->Win32.handle)) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to open clipboard"); + "Win32: Failed to open clipboard"); return NULL; } @@ -118,7 +118,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) CloseClipboard(); _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to retrieve clipboard data"); + "Win32: Failed to retrieve clipboard data"); return NULL; } @@ -132,7 +132,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) if (!_glfwLibrary.Win32.clipboardString) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to convert wide string to UTF-8"); + "Win32: Failed to convert wide string to UTF-8"); return NULL; } diff --git a/src/win32_dllmain.c b/src/win32_dllmain.c index 95258ccc..98f9ab2a 100644 --- a/src/win32_dllmain.c +++ b/src/win32_dllmain.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Win32/WGL +// Platform: Win32 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/win32_fullscreen.c b/src/win32_fullscreen.c index cbb0bb77..a22abdfc 100644 --- a/src/win32_fullscreen.c +++ b/src/win32_fullscreen.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Win32/WGL +// Platform: Win32 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/win32_gamma.c b/src/win32_gamma.c index 36fbd3a4..74958191 100644 --- a/src/win32_gamma.c +++ b/src/win32_gamma.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Win32/WGL +// Platform: Win32 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/win32_input.c b/src/win32_input.c index d3785aa3..a9195838 100644 --- a/src/win32_input.c +++ b/src/win32_input.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Win32/WGL +// Platform: Win32 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/win32_joystick.c b/src/win32_joystick.c index c27218db..a51773d3 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Win32/WGL +// Platform: Win32 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -116,7 +116,7 @@ int _glfwPlatformGetJoystickParam(int joy, int param) // Get joystick axis positions //======================================================================== -int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) +int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numaxes) { JOYCAPS jc; JOYINFOEX ji; @@ -137,22 +137,22 @@ int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) // Get position values for all axes axis = 0; if (axis < numaxes) - pos[axis++] = calcJoystickPos(ji.dwXpos, jc.wXmin, jc.wXmax); + axes[axis++] = calcJoystickPos(ji.dwXpos, jc.wXmin, jc.wXmax); if (axis < numaxes) - pos[axis++] = -calcJoystickPos(ji.dwYpos, jc.wYmin, jc.wYmax); + axes[axis++] = -calcJoystickPos(ji.dwYpos, jc.wYmin, jc.wYmax); if (axis < numaxes && jc.wCaps & JOYCAPS_HASZ) - pos[axis++] = calcJoystickPos(ji.dwZpos, jc.wZmin, jc.wZmax); + axes[axis++] = calcJoystickPos(ji.dwZpos, jc.wZmin, jc.wZmax); if (axis < numaxes && jc.wCaps & JOYCAPS_HASR) - pos[axis++] = calcJoystickPos(ji.dwRpos, jc.wRmin, jc.wRmax); + axes[axis++] = calcJoystickPos(ji.dwRpos, jc.wRmin, jc.wRmax); if (axis < numaxes && jc.wCaps & JOYCAPS_HASU) - pos[axis++] = calcJoystickPos(ji.dwUpos, jc.wUmin, jc.wUmax); + axes[axis++] = calcJoystickPos(ji.dwUpos, jc.wUmin, jc.wUmax); if (axis < numaxes && jc.wCaps & JOYCAPS_HASV) - pos[axis++] = -calcJoystickPos(ji.dwVpos, jc.wVmin, jc.wVmax); + axes[axis++] = -calcJoystickPos(ji.dwVpos, jc.wVmin, jc.wVmax); return axis; } diff --git a/src/win32_opengl.c b/src/win32_opengl.c index 2c676c91..bf684094 100644 --- a/src/win32_opengl.c +++ b/src/win32_opengl.c @@ -171,15 +171,14 @@ static _GLFWfbconfig* getFBConfigs(_GLFWwindow* window, unsigned int* found) if (!available) { - _glfwSetError(GLFW_OPENGL_UNAVAILABLE, "Win32/WGL: No pixel formats found"); + _glfwSetError(GLFW_OPENGL_UNAVAILABLE, "WGL: No pixel formats found"); return NULL; } fbconfigs = (_GLFWfbconfig*) malloc(sizeof(_GLFWfbconfig) * available); if (!fbconfigs) { - _glfwSetError(GLFW_OUT_OF_MEMORY, - "Win32/WGL: Failed to allocate _GLFWfbconfig array"); + _glfwSetError(GLFW_OUT_OF_MEMORY, NULL); return NULL; } @@ -285,6 +284,9 @@ static _GLFWfbconfig* getFBConfigs(_GLFWwindow* window, unsigned int* found) if (*found == 0) { + _glfwSetError(GLFW_PLATFORM_ERROR, + "Win32/WGL: No usable pixel formats found"); + free(fbconfigs); return NULL; } @@ -310,15 +312,15 @@ static GLboolean createContext(_GLFWwindow* window, if (!DescribePixelFormat(window->WGL.DC, pixelFormat, sizeof(pfd), &pfd)) { - _glfwSetError(GLFW_OPENGL_UNAVAILABLE, - "Win32/WGL: Failed to retrieve PFD for selected pixel format"); + _glfwSetError(GLFW_PLATFORM_ERROR, + "Win32: Failed to retrieve PFD for selected pixel format"); return GL_FALSE; } if (!SetPixelFormat(window->WGL.DC, pixelFormat, &pfd)) { - _glfwSetError(GLFW_OPENGL_UNAVAILABLE, - "Win32/WGL: Failed to set selected pixel format"); + _glfwSetError(GLFW_PLATFORM_ERROR, + "Win32: Failed to set selected pixel format"); return GL_FALSE; } @@ -360,7 +362,7 @@ static GLboolean createContext(_GLFWwindow* window, if (!window->WGL.ARB_create_context_profile) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Win32/WGL: OpenGL profile requested but " + "WGL: OpenGL profile requested but " "WGL_ARB_create_context_profile is unavailable"); return GL_FALSE; } @@ -369,7 +371,7 @@ static GLboolean createContext(_GLFWwindow* window, !window->WGL.EXT_create_context_es2_profile) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Win32/WGL: OpenGL ES 2.x profile requested but " + "WGL: OpenGL ES 2.x profile requested but " "WGL_EXT_create_context_es2_profile is unavailable"); return GL_FALSE; } @@ -392,7 +394,7 @@ static GLboolean createContext(_GLFWwindow* window, if (!window->WGL.ARB_create_context_robustness) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Win32/WGL: An OpenGL robustness strategy was " + "WGL: An OpenGL robustness strategy was " "requested but WGL_ARB_create_context_robustness " "is unavailable"); return GL_FALSE; @@ -415,7 +417,7 @@ static GLboolean createContext(_GLFWwindow* window, if (!window->WGL.context) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Win32/WGL: Failed to create OpenGL context"); + "WGL: Failed to create OpenGL context"); return GL_FALSE; } } @@ -425,7 +427,7 @@ static GLboolean createContext(_GLFWwindow* window, if (!window->WGL.context) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to create OpenGL context"); + "WGL: Failed to create OpenGL context"); return GL_FALSE; } @@ -434,8 +436,8 @@ static GLboolean createContext(_GLFWwindow* window, if (!wglShareLists(share, window->WGL.context)) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to enable sharing with " - "specified OpenGL context"); + "WGL: Failed to enable sharing with specified " + "OpenGL context"); return GL_FALSE; } } @@ -466,7 +468,7 @@ int _glfwCreateContext(_GLFWwindow* window, if (!window->WGL.DC) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to retrieve DC for window"); + "Win32: Failed to retrieve DC for window"); return GL_FALSE; } @@ -478,16 +480,12 @@ int _glfwCreateContext(_GLFWwindow* window, fbconfigs = getFBConfigs(window, &fbcount); if (!fbconfigs) - { - _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: No usable pixel formats found"); return GL_FALSE; - } result = _glfwChooseFBConfig(fbconfig, fbconfigs, fbcount); if (!result) { - _glfwSetError(GLFW_PLATFORM_ERROR, + _glfwSetError(GLFW_FORMAT_UNAVAILABLE, "Win32/WGL: No pixel format matched the criteria"); free(fbconfigs); @@ -620,7 +618,7 @@ void _glfwPlatformCopyContext(_GLFWwindow* src, _GLFWwindow* dst, unsigned long if (!wglCopyContext(src->WGL.context, dst->WGL.context, mask)) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to copy OpenGL context attributes"); + "WGL: Failed to copy OpenGL context attributes"); } } diff --git a/src/win32_time.c b/src/win32_time.c index f3bd173a..1e1d57f4 100644 --- a/src/win32_time.c +++ b/src/win32_time.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Win32/WGL +// Platform: Win32 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/win32_window.c b/src/win32_window.c index 846c2639..8ff95351 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -822,7 +822,7 @@ static ATOM registerWindowClass(void) if (!classAtom) { _glfwSetError(GLFW_PLATFORM_ERROR, - "Win32/WGL: Failed to register window class"); + "Win32: Failed to register window class"); return 0; } @@ -895,7 +895,7 @@ static int createWindow(_GLFWwindow* window, if (!wideTitle) { _glfwSetError(GLFW_PLATFORM_ERROR, - "glfwCreateWindow: Failed to convert title to wide string"); + "Win32: Failed to convert title to wide string"); return GL_FALSE; } @@ -913,7 +913,7 @@ static int createWindow(_GLFWwindow* window, if (!window->Win32.handle) { - _glfwSetError(GLFW_PLATFORM_ERROR, "Win32/WGL: Failed to create window"); + _glfwSetError(GLFW_PLATFORM_ERROR, "Win32: Failed to create window"); return GL_FALSE; } @@ -1017,8 +1017,8 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, if (!window->WGL.ARB_create_context) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Win32/WGL: A forward compatible OpenGL context " - "requested but WGL_ARB_create_context is unavailable"); + "WGL: A forward compatible OpenGL context requested " + "but WGL_ARB_create_context is unavailable"); return GL_FALSE; } @@ -1030,7 +1030,7 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, if (!window->WGL.ARB_create_context_profile) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "Win32/WGL: OpenGL profile requested but " + "WGL: OpenGL profile requested but " "WGL_ARB_create_context_profile is unavailable"); return GL_FALSE; } @@ -1116,7 +1116,7 @@ void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title) if (!wideTitle) { _glfwSetError(GLFW_PLATFORM_ERROR, - "glfwSetWindowTitle: Failed to convert title to wide string"); + "Win32: Failed to convert title to wide string"); return; } diff --git a/src/window.c b/src/window.c index 1e169743..e59a1d25 100644 --- a/src/window.c +++ b/src/window.c @@ -6,6 +6,7 @@ //------------------------------------------------------------------------ // Copyright (c) 2002-2006 Marcus Geelnard // Copyright (c) 2006-2010 Camilla Berglund +// Copyright (c) 2012 Torsten Walluhn // // This software is provided 'as-is', without any express or implied // warranty. In no event will the authors be held liable for any damages @@ -32,7 +33,11 @@ #include #include +#ifdef __APPLE__ +#include +#else #include +#endif //======================================================================== @@ -79,6 +84,12 @@ void _glfwSetDefaultWindowHints(void) // The default is to allow window resizing _glfwLibrary.hints.resizable = GL_TRUE; + + // The default is 24 bits of depth, 8 bits of color + _glfwLibrary.hints.depthBits = 24; + _glfwLibrary.hints.redBits = 8; + _glfwLibrary.hints.greenBits = 8; + _glfwLibrary.hints.blueBits = 8; } @@ -262,7 +273,7 @@ GLFWAPI GLFWwindow glfwCreateWindow(int width, int height, if (mode != GLFW_WINDOWED && mode != GLFW_FULLSCREEN) { _glfwSetError(GLFW_INVALID_ENUM, - "glfwCreateWindow: Invalid enum for 'mode' parameter"); + "glfwCreateWindow: Invalid window mode"); return GL_FALSE; } @@ -287,8 +298,7 @@ GLFWAPI GLFWwindow glfwCreateWindow(int width, int height, window = (_GLFWwindow*) malloc(sizeof(_GLFWwindow)); if (!window) { - _glfwSetError(GLFW_OUT_OF_MEMORY, - "glfwCreateWindow: Failed to allocate window structure"); + _glfwSetError(GLFW_OUT_OF_MEMORY, NULL); return NULL; } diff --git a/src/x11_clipboard.c b/src/x11_clipboard.c index a833ed1f..5bc92d44 100644 --- a/src/x11_clipboard.c +++ b/src/x11_clipboard.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: X11/GLX +// Platform: X11 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -182,7 +182,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) if (_glfwLibrary.X11.selection.status == _GLFW_CONVERSION_FAILED) { _glfwSetError(GLFW_FORMAT_UNAVAILABLE, - "X11/GLX: Failed to convert selection to string"); + "X11: Failed to convert selection to string"); return NULL; } diff --git a/src/x11_fullscreen.c b/src/x11_fullscreen.c index cc0736af..64f543bf 100644 --- a/src/x11_fullscreen.c +++ b/src/x11_fullscreen.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: X11/GLX +// Platform: X11 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -441,7 +441,7 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found) if (visuals == NULL) { _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: Failed to retrieve the available visuals"); + "X11: Failed to retrieve the available visuals"); return 0; } diff --git a/src/x11_gamma.c b/src/x11_gamma.c index 44cd1139..148ae740 100644 --- a/src/x11_gamma.c +++ b/src/x11_gamma.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: X11/GLX +// Platform: X11 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -115,7 +115,7 @@ void _glfwPlatformGetGammaRamp(GLFWgammaramp* ramp) if (_glfwLibrary.originalRampSize != GLFW_GAMMA_RAMP_SIZE) { _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: Failed to get gamma ramp due to size " + "X11: Failed to get gamma ramp due to size " "incompatibility"); return; } @@ -166,7 +166,7 @@ void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp) if (_glfwLibrary.originalRampSize != GLFW_GAMMA_RAMP_SIZE) { _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: Failed to set gamma ramp due to size " + "X11: Failed to set gamma ramp due to size " "incompatibility"); return; } diff --git a/src/x11_init.c b/src/x11_init.c index 654e9fc9..e5bb2b26 100644 --- a/src/x11_init.c +++ b/src/x11_init.c @@ -491,7 +491,7 @@ static GLboolean initDisplay(void) _glfwLibrary.X11.display = XOpenDisplay(NULL); if (!_glfwLibrary.X11.display) { - _glfwSetError(GLFW_OPENGL_UNAVAILABLE, "X11/GLX: Failed to open X display"); + _glfwSetError(GLFW_OPENGL_UNAVAILABLE, "X11: Failed to open X display"); return GL_FALSE; } @@ -525,7 +525,7 @@ static GLboolean initDisplay(void) &_glfwLibrary.X11.RandR.minorVersion)) { _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: Failed to query RandR version"); + "X11: Failed to query RandR version"); return GL_FALSE; } } diff --git a/src/x11_input.c b/src/x11_input.c index 49961395..2ea8b8c4 100644 --- a/src/x11_input.c +++ b/src/x11_input.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: X11 (Unix) +// Platform: X11 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/x11_joystick.c b/src/x11_joystick.c index 9fc921fd..a5700c68 100644 --- a/src/x11_joystick.c +++ b/src/x11_joystick.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: X11/GLX +// Platform: X11 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -30,47 +30,139 @@ #include "internal.h" - -//======================================================================== -// Note: Only Linux joystick input is supported at the moment. Other -// systems will behave as if there are no joysticks connected. -//======================================================================== - - #ifdef _GLFW_USE_LINUX_JOYSTICKS +#include -//------------------------------------------------------------------------ -// Here are the Linux joystick driver v1.x interface definitions that we -// use (we do not want to rely on ): -//------------------------------------------------------------------------ - -#include +#include +#include #include #include + #include #include - -// Joystick event types -#define JS_EVENT_BUTTON 0x01 /* button pressed/released */ -#define JS_EVENT_AXIS 0x02 /* joystick moved */ -#define JS_EVENT_INIT 0x80 /* initial state of device */ - -// Joystick event structure -struct js_event { - unsigned int time; /* (u32) event timestamp in milliseconds */ - signed short value; /* (s16) value */ - unsigned char type; /* (u8) event type */ - unsigned char number; /* (u8) axis/button number */ -}; - -// Joystick IOCTL commands -#define JSIOCGVERSION _IOR('j', 0x01, int) /* get driver version (u32) */ -#define JSIOCGAXES _IOR('j', 0x11, char) /* get number of axes (u8) */ -#define JSIOCGBUTTONS _IOR('j', 0x12, char) /* get number of buttons (u8) */ - #endif // _GLFW_USE_LINUX_JOYSTICKS +//======================================================================== +// Attempt to open the specified joystick device +//======================================================================== + +static int openJoystickDevice(int joy, const char* path) +{ +#ifdef _GLFW_USE_LINUX_JOYSTICKS + char numAxes, numButtons; + int fd, version; + + fd = open(path, O_NONBLOCK); + if (fd == -1) + return GL_FALSE; + + _glfwLibrary.X11.joystick[joy].fd = fd; + + // Verify that the joystick driver version is at least 1.0 + ioctl(fd, JSIOCGVERSION, &version); + if (version < 0x010000) + { + // It's an old 0.x interface (we don't support it) + close(fd); + return GL_FALSE; + } + + ioctl(fd, JSIOCGAXES, &numAxes); + _glfwLibrary.X11.joystick[joy].numAxes = (int) numAxes; + + ioctl(fd, JSIOCGBUTTONS, &numButtons); + _glfwLibrary.X11.joystick[joy].numButtons = (int) numButtons; + + _glfwLibrary.X11.joystick[joy].axis = + (float*) malloc(sizeof(float) * numAxes); + if (_glfwLibrary.X11.joystick[joy].axis == NULL) + { + close(fd); + + _glfwSetError(GLFW_OUT_OF_MEMORY, NULL); + return GL_FALSE; + } + + _glfwLibrary.X11.joystick[joy].button = + (unsigned char*) malloc(sizeof(char) * numButtons); + if (_glfwLibrary.X11.joystick[joy].button == NULL) + { + free(_glfwLibrary.X11.joystick[joy].axis); + close(fd); + + _glfwSetError(GLFW_OUT_OF_MEMORY, NULL); + return GL_FALSE; + } + + _glfwLibrary.X11.joystick[joy].present = GL_TRUE; +#endif // _GLFW_USE_LINUX_JOYSTICKS + + return GL_TRUE; +} + + +//======================================================================== +// Polls for and processes events for all present joysticks +//======================================================================== + +static void pollJoystickEvents(void) +{ +#ifdef _GLFW_USE_LINUX_JOYSTICKS + int i; + ssize_t result; + struct js_event e; + + for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) + { + if (!_glfwLibrary.X11.joystick[i].present) + continue; + + // Read all queued events (non-blocking) + for (;;) + { + errno = 0; + result = read(_glfwLibrary.X11.joystick[i].fd, &e, sizeof(e)); + + if (errno == ENODEV) + _glfwLibrary.X11.joystick[i].present = GL_FALSE; + + if (result < sizeof(e)) + break; + + // We don't care if it's an init event or not + e.type &= ~JS_EVENT_INIT; + + switch (e.type) + { + case JS_EVENT_AXIS: + _glfwLibrary.X11.joystick[i].axis[e.number] = + (float) e.value / 32767.0f; + + // We need to change the sign for the Y axes, so that + // positive = up/forward, according to the GLFW spec. + if (e.number & 1) + { + _glfwLibrary.X11.joystick[i].axis[e.number] = + -_glfwLibrary.X11.joystick[i].axis[e.number]; + } + + break; + + case JS_EVENT_BUTTON: + _glfwLibrary.X11.joystick[i].button[e.number] = + e.value ? GLFW_PRESS : GLFW_RELEASE; + break; + + default: + break; + } + } + } +#endif // _GLFW_USE_LINUX_JOYSTICKS +} + + ////////////////////////////////////////////////////////////////////////// ////// GLFW internal API ////// ////////////////////////////////////////////////////////////////////////// @@ -82,97 +174,24 @@ struct js_event { void _glfwInitJoysticks(void) { #ifdef _GLFW_USE_LINUX_JOYSTICKS - int k, n, fd, joy_count; - const char* joy_base_name; - char joy_dev_name[20]; - int driver_version = 0x000800; - char ret_data; -#endif // _GLFW_USE_LINUX_JOYSTICKS - int i; - - // Start by saying that there are no sticks - for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) - _glfwJoy[i].Present = GL_FALSE; - -#ifdef _GLFW_USE_LINUX_JOYSTICKS - - // Try to open joysticks (nonblocking) - joy_count = 0; - for (k = 0; k <= 1 && joy_count <= GLFW_JOYSTICK_LAST; k++) + int i, j, joy = 0; + char path[20]; + const char* bases[] = { - // Pick joystick base name - switch (k) + "/dev/input/js", + "/dev/js" + }; + + for (i = 0; i < sizeof(bases) / sizeof(bases[0]); i++) + { + for (j = 0; j < 50; j++) { - case 0: - // USB joysticks - joy_base_name = "/dev/input/js"; + if (joy > GLFW_JOYSTICK_LAST) break; - case 1: - // "Legacy" joysticks - joy_base_name = "/dev/js"; - break; - default: - // This should never happen - continue; - } - // Try to open a few of these sticks - for (i = 0; i <= 50 && joy_count <= GLFW_JOYSTICK_LAST; i++) - { - sprintf(joy_dev_name, "%s%d", joy_base_name, i); - fd = open(joy_dev_name, O_NONBLOCK); - if (fd != -1) - { - // Remember fd - _glfwJoy[joy_count].fd = fd; - - // Check that the joystick driver version is 1.0+ - ioctl(fd, JSIOCGVERSION, &driver_version); - if (driver_version < 0x010000) - { - // It's an old 0.x interface (we don't support it) - close(fd); - continue; - } - - // Get number of joystick axes - ioctl(fd, JSIOCGAXES, &ret_data); - _glfwJoy[joy_count].NumAxes = (int) ret_data; - - // Get number of joystick buttons - ioctl(fd, JSIOCGBUTTONS, &ret_data); - _glfwJoy[joy_count].NumButtons = (int) ret_data; - - // Allocate memory for joystick state - _glfwJoy[joy_count].Axis = - (float*) malloc(sizeof(float) * - _glfwJoy[joy_count].NumAxes); - if (_glfwJoy[joy_count].Axis == NULL) - { - close(fd); - continue; - } - _glfwJoy[joy_count].Button = - (unsigned char*) malloc(sizeof(char) * - _glfwJoy[joy_count].NumButtons); - if (_glfwJoy[joy_count].Button == NULL) - { - free(_glfwJoy[joy_count].Axis); - close(fd); - continue; - } - - // Clear joystick state - for (n = 0; n < _glfwJoy[joy_count].NumAxes; n++) - _glfwJoy[joy_count].Axis[n] = 0.0f; - - for (n = 0; n < _glfwJoy[joy_count].NumButtons; n++) - _glfwJoy[joy_count].Button[n] = GLFW_RELEASE; - - // The joystick is supported and connected - _glfwJoy[joy_count].Present = GL_TRUE; - joy_count++; - } + sprintf(path, "%s%i", bases[i], j); + if (openJoystickDevice(joy, path)) + joy++; } } #endif // _GLFW_USE_LINUX_JOYSTICKS @@ -185,72 +204,18 @@ void _glfwInitJoysticks(void) void _glfwTerminateJoysticks(void) { - #ifdef _GLFW_USE_LINUX_JOYSTICKS - int i; - // Close any opened joysticks for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) { - if (_glfwJoy[i].Present) + if (_glfwLibrary.X11.joystick[i].present) { - close(_glfwJoy[i].fd); - free(_glfwJoy[i].Axis); - free(_glfwJoy[i].Button); + close(_glfwLibrary.X11.joystick[i].fd); + free(_glfwLibrary.X11.joystick[i].axis); + free(_glfwLibrary.X11.joystick[i].button); - _glfwJoy[i].Present = GL_FALSE; - } - } - -#endif // _GLFW_USE_LINUX_JOYSTICKS - -} - - -//======================================================================== -// Empty joystick event queue -//======================================================================== - -static void pollJoystickEvents(void) -{ -#ifdef _GLFW_USE_LINUX_JOYSTICKS - - struct js_event e; - int i; - - // Get joystick events for all GLFW joysticks - for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) - { - // Is the stick present? - if (_glfwJoy[i].Present) - { - // Read all queued events (non-blocking) - while (read(_glfwJoy[i].fd, &e, sizeof(struct js_event)) > 0) - { - // We don't care if it's an init event or not - e.type &= ~JS_EVENT_INIT; - - // Check event type - switch (e.type) - { - case JS_EVENT_AXIS: - _glfwJoy[i].Axis[e.number] = (float) e.value / 32767.0f; - // We need to change the sign for the Y axes, so that - // positive = up/forward, according to the GLFW spec. - if (e.number & 1) - _glfwJoy[i].Axis[e.number] = -_glfwJoy[i].Axis[e.number]; - break; - - case JS_EVENT_BUTTON: - _glfwJoy[i].Button[e.number] = - e.value ? GLFW_PRESS : GLFW_RELEASE; - break; - - default: - break; - } - } + _glfwLibrary.X11.joystick[i].present = GL_FALSE; } } #endif // _GLFW_USE_LINUX_JOYSTICKS @@ -267,11 +232,10 @@ static void pollJoystickEvents(void) int _glfwPlatformGetJoystickParam(int joy, int param) { - if (!_glfwJoy[joy].Present) - { - // TODO: Figure out if this is an error + pollJoystickEvents(); + + if (!_glfwLibrary.X11.joystick[joy].present) return 0; - } switch (param) { @@ -279,13 +243,13 @@ int _glfwPlatformGetJoystickParam(int joy, int param) return GL_TRUE; case GLFW_AXES: - return _glfwJoy[joy].NumAxes; + return _glfwLibrary.X11.joystick[joy].numAxes; case GLFW_BUTTONS: - return _glfwJoy[joy].NumButtons; + return _glfwLibrary.X11.joystick[joy].numButtons; default: - break; + _glfwSetError(GLFW_INVALID_ENUM, NULL); } return 0; @@ -296,28 +260,22 @@ int _glfwPlatformGetJoystickParam(int joy, int param) // Get joystick axis positions //======================================================================== -int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) +int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numAxes) { int i; - if (!_glfwJoy[joy].Present) - { - // TODO: Figure out if this is an error - return 0; - } - - // Update joystick state pollJoystickEvents(); - // Does the joystick support less axes than requested? - if (_glfwJoy[joy].NumAxes < numaxes) - numaxes = _glfwJoy[joy].NumAxes; + if (!_glfwLibrary.X11.joystick[joy].present) + return 0; - // Copy axis positions from internal state - for (i = 0; i < numaxes; i++) - pos[i] = _glfwJoy[joy].Axis[i]; + if (_glfwLibrary.X11.joystick[joy].numAxes < numAxes) + numAxes = _glfwLibrary.X11.joystick[joy].numAxes; - return numaxes; + for (i = 0; i < numAxes; i++) + axes[i] = _glfwLibrary.X11.joystick[joy].axis[i]; + + return numAxes; } @@ -326,27 +284,21 @@ int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) //======================================================================== int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, - int numbuttons) + int numButtons) { int i; - if (!_glfwJoy[joy].Present) - { - // TODO: Figure out if this is an error - return 0; - } - - // Update joystick state pollJoystickEvents(); - // Does the joystick support less buttons than requested? - if (_glfwJoy[joy].NumButtons < numbuttons) - numbuttons = _glfwJoy[joy].NumButtons; + if (!_glfwLibrary.X11.joystick[joy].present) + return 0; - // Copy button states from internal state - for (i = 0; i < numbuttons; i++) - buttons[i] = _glfwJoy[joy].Button[i]; + if (_glfwLibrary.X11.joystick[joy].numButtons < numButtons) + numButtons = _glfwLibrary.X11.joystick[joy].numButtons; - return numbuttons; + for (i = 0; i < numButtons; i++) + buttons[i] = _glfwLibrary.X11.joystick[joy].button[i]; + + return numButtons; } diff --git a/src/x11_keysym2unicode.c b/src/x11_keysym2unicode.c index 1f610fdd..c0d10c6e 100644 --- a/src/x11_keysym2unicode.c +++ b/src/x11_keysym2unicode.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: X11/GLX +// Platform: X11 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/x11_opengl.c b/src/x11_opengl.c index 2a5cc451..a7c3f61b 100644 --- a/src/x11_opengl.c +++ b/src/x11_opengl.c @@ -78,7 +78,7 @@ static _GLFWfbconfig* getFBConfigs(_GLFWwindow* window, unsigned int* found) if (!_glfwLibrary.GLX.SGIX_fbconfig) { _glfwSetError(GLFW_OPENGL_UNAVAILABLE, - "X11/GLX: GLXFBConfig support not found"); + "GLX: GLXFBConfig support not found"); return NULL; } } @@ -101,7 +101,7 @@ static _GLFWfbconfig* getFBConfigs(_GLFWwindow* window, unsigned int* found) if (!count) { _glfwSetError(GLFW_OPENGL_UNAVAILABLE, - "X11/GLX: No GLXFBConfigs returned"); + "GLX: No GLXFBConfigs returned"); return NULL; } } @@ -113,7 +113,7 @@ static _GLFWfbconfig* getFBConfigs(_GLFWwindow* window, unsigned int* found) if (!count) { _glfwSetError(GLFW_OPENGL_UNAVAILABLE, - "X11/GLX: No GLXFBConfigs returned"); + "GLX: No GLXFBConfigs returned"); return NULL; } } @@ -121,8 +121,7 @@ static _GLFWfbconfig* getFBConfigs(_GLFWwindow* window, unsigned int* found) result = (_GLFWfbconfig*) malloc(sizeof(_GLFWfbconfig) * count); if (!result) { - _glfwSetError(GLFW_OUT_OF_MEMORY, - "X11/GLX: Failed to allocate _GLFWfbconfig array"); + _glfwSetError(GLFW_OUT_OF_MEMORY, NULL); return NULL; } @@ -242,7 +241,7 @@ static int createContext(_GLFWwindow* window, if (fbconfig == NULL) { _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: Failed to retrieve the selected GLXFBConfig"); + "GLX: Failed to retrieve the selected GLXFBConfig"); return GL_FALSE; } } @@ -264,7 +263,7 @@ static int createContext(_GLFWwindow* window, XFree(fbconfig); _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: Failed to retrieve visual for GLXFBConfig"); + "GLX: Failed to retrieve visual for GLXFBConfig"); return GL_FALSE; } @@ -303,7 +302,7 @@ static int createContext(_GLFWwindow* window, if (!_glfwLibrary.GLX.ARB_create_context_profile) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "X11/GLX: An OpenGL profile requested but " + "GLX: An OpenGL profile requested but " "GLX_ARB_create_context_profile is unavailable"); return GL_FALSE; } @@ -312,7 +311,7 @@ static int createContext(_GLFWwindow* window, !_glfwLibrary.GLX.EXT_create_context_es2_profile) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "X11/GLX: OpenGL ES 2.x profile requested but " + "GLX: OpenGL ES 2.x profile requested but " "GLX_EXT_create_context_es2_profile is unavailable"); return GL_FALSE; } @@ -334,7 +333,7 @@ static int createContext(_GLFWwindow* window, if (!_glfwLibrary.GLX.ARB_create_context_robustness) { _glfwSetError(GLFW_VERSION_UNAVAILABLE, - "X11/GLX: An OpenGL robustness strategy was " + "GLX: An OpenGL robustness strategy was " "requested but GLX_ARB_create_context_robustness " "is unavailable"); return GL_FALSE; @@ -397,7 +396,7 @@ static int createContext(_GLFWwindow* window, // TODO: Handle all the various error codes here _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: Failed to create OpenGL context"); + "GLX: Failed to create OpenGL context"); return GL_FALSE; } @@ -437,7 +436,7 @@ int _glfwInitOpenGL(void) if (!_glfwLibrary.GLX.libGL) { - _glfwSetError(GLFW_PLATFORM_ERROR, "X11/GLX: Failed to find libGL"); + _glfwSetError(GLFW_PLATFORM_ERROR, "GLX: Failed to find libGL"); return GL_FALSE; } #endif @@ -445,7 +444,7 @@ int _glfwInitOpenGL(void) // Check if GLX is supported on this display if (!glXQueryExtension(_glfwLibrary.X11.display, NULL, NULL)) { - _glfwSetError(GLFW_OPENGL_UNAVAILABLE, "X11/GLX: GLX support not found"); + _glfwSetError(GLFW_OPENGL_UNAVAILABLE, "GLX: GLX support not found"); return GL_FALSE; } @@ -454,7 +453,7 @@ int _glfwInitOpenGL(void) &_glfwLibrary.GLX.minorVersion)) { _glfwSetError(GLFW_OPENGL_UNAVAILABLE, - "X11/GLX: Failed to query GLX version"); + "GLX: Failed to query GLX version"); return GL_FALSE; } @@ -565,17 +564,13 @@ int _glfwCreateContext(_GLFWwindow* window, fbconfigs = getFBConfigs(window, &fbcount); if (!fbconfigs) - { - _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: No usable GLXFBConfigs found"); return GL_FALSE; - } result = _glfwChooseFBConfig(fbconfig, fbconfigs, fbcount); if (!result) { _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: No GLXFBConfig matched the criteria"); + "GLX: No GLXFBConfig matched the criteria"); free(fbconfigs); return GL_FALSE; diff --git a/src/x11_platform.h b/src/x11_platform.h index c70c7d57..cde2542b 100644 --- a/src/x11_platform.h +++ b/src/x11_platform.h @@ -42,20 +42,21 @@ #include // This path may need to be changed if you build GLFW using your own setup -// We ship and use our own copy of glxext.h since GLFW uses fairly new +// GLFW comes with its own copy of glxext.h since it uses some fairly new // extensions and not all operating systems come with an up-to-date version #include "../support/GL/glxext.h" -// With XFree86, we can use the XF86VidMode extension +// The XF86VidMode extension provides mode setting and gamma control #if defined(_GLFW_HAS_XF86VIDMODE) #include #endif +// The XRandR extension provides mode setting and gamma control #if defined(_GLFW_HAS_XRANDR) #include #endif -// Do we have support for dlopen/dlsym? +// dlopen is used as a fallback function retrieval mechanism #if defined(_GLFW_HAS_DLOPEN) #include #endif @@ -65,7 +66,7 @@ #include #endif -// We support four different ways for getting addresses for GL/GLX +// GLFW supports four different ways for getting addresses for GL/GLX // extension functions: glXGetProcAddress, glXGetProcAddressARB, // glXGetProcAddressEXT, and dlsym #if defined(_GLFW_HAS_GLXGETPROCADDRESSARB) @@ -235,6 +236,15 @@ typedef struct _GLFWlibraryX11 int status; } selection; + struct { + int present; + int fd; + int numAxes; + int numButtons; + float* axis; + unsigned char* button; + } joystick[GLFW_JOYSTICK_LAST + 1]; + } _GLFWlibraryX11; @@ -271,19 +281,6 @@ typedef struct _GLFWlibraryGLX } _GLFWlibraryGLX; -//------------------------------------------------------------------------ -// Joystick information & state -//------------------------------------------------------------------------ -GLFWGLOBAL struct { - int Present; - int fd; - int NumAxes; - int NumButtons; - float* Axis; - unsigned char* Button; -} _glfwJoy[GLFW_JOYSTICK_LAST + 1]; - - //------------------------------------------------------------------------ // Platform-specific window structure //------------------------------------------------------------------------ diff --git a/src/x11_time.c b/src/x11_time.c index f1445233..bf3335d3 100644 --- a/src/x11_time.c +++ b/src/x11_time.c @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: X11/GLX +// Platform: X11 // API version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ diff --git a/src/x11_window.c b/src/x11_window.c index 0787882a..af5b35ab 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -43,21 +43,18 @@ #define Button6 6 #define Button7 7 + //======================================================================== // Translates an X Window key to internal coding //======================================================================== static int translateKey(int keycode) { - // Use the pre-filled LUT (see updateKeyCodeLUT() ). + // Use the pre-filled LUT (see updateKeyCodeLUT() in x11_init.c) if ((keycode >= 0) && (keycode < 256)) - { return _glfwLibrary.X11.keyCodeLUT[keycode]; - } else - { return -1; - } } @@ -90,6 +87,7 @@ static GLboolean createWindow(_GLFWwindow* window, // Every window needs a colormap // Create one based on the visual used by the current context + // TODO: Decouple this from context creation window->X11.colormap = XCreateColormap(_glfwLibrary.X11.display, _glfwLibrary.X11.root, @@ -109,33 +107,31 @@ static GLboolean createWindow(_GLFWwindow* window, if (wndconfig->mode == GLFW_WINDOWED) { - // The /only/ reason we are setting the background pixel here is - // that otherwise our window wont get any decorations on systems - // using Compiz on Intel hardware + // The /only/ reason for setting the background pixel here is that + // otherwise our window won't get any decorations on systems using + // certain versions of Compiz on Intel hardware wa.background_pixel = BlackPixel(_glfwLibrary.X11.display, _glfwLibrary.X11.screen); wamask |= CWBackPixel; } - window->X11.handle = XCreateWindow( - _glfwLibrary.X11.display, - _glfwLibrary.X11.root, - 0, 0, // Upper left corner of this window on root - window->width, window->height, - 0, // Border width - visual->depth, // Color depth - InputOutput, - visual->visual, - wamask, - &wa - ); + window->X11.handle = XCreateWindow(_glfwLibrary.X11.display, + _glfwLibrary.X11.root, + 0, 0, // Position + window->width, window->height, + 0, // Border width + visual->depth, // Color depth + InputOutput, + visual->visual, + wamask, + &wa); if (!window->X11.handle) { - // TODO: Handle all the various error codes here + // TODO: Handle all the various error codes here and translate them + // to GLFW errors - _glfwSetError(GLFW_PLATFORM_ERROR, - "X11/GLX: Failed to create window"); + _glfwSetError(GLFW_PLATFORM_ERROR, "X11: Failed to create window"); return GL_FALSE; } } @@ -147,8 +143,8 @@ static GLboolean createWindow(_GLFWwindow* window, // manager ignore the window completely (ICCCM, section 4) // The good thing is that this makes undecorated fullscreen windows // easy to do; the bad thing is that we have to do everything manually - // and some things (like iconify/restore) won't work at all, as they're - // usually performed by the window manager + // and some things (like iconify/restore) won't work at all, as those + // are tasks usually performed by the window manager XSetWindowAttributes attributes; attributes.override_redirect = True; @@ -165,7 +161,7 @@ static GLboolean createWindow(_GLFWwindow* window, "WM_DELETE_WINDOW", False); - // Declare the WM protocols we support + // Declare the WM protocols supported by GLFW { int count = 0; Atom protocols[2]; @@ -176,8 +172,8 @@ static GLboolean createWindow(_GLFWwindow* window, protocols[count++] = _glfwLibrary.X11.wmDeleteWindow; // The _NET_WM_PING EWMH protocol - // Tells the WM to ping our window and flag us as unresponsive if we - // don't reply within a few seconds + // Tells the WM to ping the GLFW window and flag the application as + // unresponsive if the WM doesn't get a reply within a few seconds if (_glfwLibrary.X11.wmPing != None) protocols[count++] = _glfwLibrary.X11.wmPing; @@ -194,7 +190,7 @@ static GLboolean createWindow(_GLFWwindow* window, if (!hints) { _glfwSetError(GLFW_OUT_OF_MEMORY, - "X11/GLX: Failed to allocate WM hints"); + "X11: Failed to allocate WM hints"); return GL_FALSE; } @@ -211,7 +207,7 @@ static GLboolean createWindow(_GLFWwindow* window, if (!hints) { _glfwSetError(GLFW_OUT_OF_MEMORY, - "X11/GLX: Failed to allocate size hints"); + "X11: Failed to allocate size hints"); return GL_FALSE; } @@ -283,7 +279,7 @@ static void captureCursor(_GLFWwindow* window) static void showCursor(_GLFWwindow* window) { - // Un-grab cursor (only in windowed mode: in fullscreen mode we still + // Un-grab cursor (in windowed mode only; in fullscreen mode we still // want the cursor grabbed in order to confine the cursor to the window // area) if (window->X11.cursorGrabbed) @@ -377,7 +373,7 @@ static void enterFullscreenMode(_GLFWwindow* window) } else if (window->X11.overrideRedirect) { - // In override-redirect mode, we have divorced ourselves from the + // In override-redirect mode we have divorced ourselves from the // window manager, so we need to do everything manually XRaiseWindow(_glfwLibrary.X11.display, window->X11.handle); @@ -405,7 +401,6 @@ static void leaveFullscreenMode(_GLFWwindow* window) { _glfwRestoreVideoMode(); - // Did we change the screen saver setting? if (_glfwLibrary.X11.saver.changed) { // Restore old screen saver settings @@ -482,17 +477,10 @@ static void processSingleEvent(void) // A keyboard key was pressed window = findWindow(event.xkey.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for KeyPress event\n"); return; - } - // Translate and report key press _glfwInputKey(window, translateKey(event.xkey.keycode), GLFW_PRESS); - - // Translate and report character input _glfwInputChar(window, translateChar(&event.xkey)); - break; } @@ -501,10 +489,7 @@ static void processSingleEvent(void) // A keyboard key was released window = findWindow(event.xkey.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for KeyRelease event\n"); return; - } // Do not report key releases for key repeats. For key repeats we // will get KeyRelease/KeyPress pairs with similar or identical @@ -532,9 +517,7 @@ static void processSingleEvent(void) } } - // Translate and report key release _glfwInputKey(window, translateKey(event.xkey.keycode), GLFW_RELEASE); - break; } @@ -543,10 +526,7 @@ static void processSingleEvent(void) // A mouse button was pressed or a scrolling event occurred window = findWindow(event.xbutton.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for ButtonPress event\n"); return; - } if (event.xbutton.button == Button1) _glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS); @@ -575,10 +555,7 @@ static void processSingleEvent(void) // A mouse button was released window = findWindow(event.xbutton.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for ButtonRelease event\n"); return; - } if (event.xbutton.button == Button1) { @@ -606,10 +583,7 @@ static void processSingleEvent(void) // The cursor entered the window window = findWindow(event.xcrossing.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for EnterNotify event\n"); return; - } if (window->cursorMode == GLFW_CURSOR_HIDDEN) hideCursor(window); @@ -623,10 +597,7 @@ static void processSingleEvent(void) // The cursor left the window window = findWindow(event.xcrossing.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for LeaveNotify event\n"); return; - } if (window->cursorMode == GLFW_CURSOR_HIDDEN) showCursor(window); @@ -640,15 +611,13 @@ static void processSingleEvent(void) // The cursor was moved window = findWindow(event.xmotion.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for MotionNotify event\n"); return; - } if (event.xmotion.x != window->X11.cursorPosX || event.xmotion.y != window->X11.cursorPosY) { - // The cursor was moved and we didn't do it + // The cursor was moved by something other than GLFW + int x, y; if (window->cursorMode == GLFW_CURSOR_CAPTURED) @@ -680,10 +649,7 @@ static void processSingleEvent(void) // The window configuration changed somehow window = findWindow(event.xconfigure.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for ConfigureNotify event\n"); return; - } _glfwInputWindowSize(window, event.xconfigure.width, @@ -701,10 +667,7 @@ static void processSingleEvent(void) // Custom client message, probably from the window manager window = findWindow(event.xclient.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for ClientMessage event\n"); return; - } if ((Atom) event.xclient.data.l[0] == _glfwLibrary.X11.wmDeleteWindow) { @@ -716,8 +679,8 @@ static void processSingleEvent(void) else if (_glfwLibrary.X11.wmPing != None && (Atom) event.xclient.data.l[0] == _glfwLibrary.X11.wmPing) { - // The window manager is pinging us to make sure we are still - // responding to events + // The window manager is pinging the application to ensure it's + // still responding to events event.xclient.window = _glfwLibrary.X11.root; XSendEvent(_glfwLibrary.X11.display, @@ -735,10 +698,7 @@ static void processSingleEvent(void) // The window was mapped window = findWindow(event.xmap.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for MapNotify event\n"); return; - } _glfwInputWindowIconify(window, GL_FALSE); break; @@ -749,10 +709,7 @@ static void processSingleEvent(void) // The window was unmapped window = findWindow(event.xmap.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for UnmapNotify event\n"); return; - } _glfwInputWindowIconify(window, GL_TRUE); break; @@ -763,10 +720,7 @@ static void processSingleEvent(void) // The window gained focus window = findWindow(event.xfocus.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for FocusIn event\n"); return; - } _glfwInputWindowFocus(window, GL_TRUE); @@ -781,10 +735,7 @@ static void processSingleEvent(void) // The window lost focus window = findWindow(event.xfocus.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for FocusOut event\n"); return; - } _glfwInputWindowFocus(window, GL_FALSE); @@ -799,10 +750,7 @@ static void processSingleEvent(void) // The window's contents was damaged window = findWindow(event.xexpose.window); if (window == NULL) - { - fprintf(stderr, "Cannot find GLFW window structure for Expose event\n"); return; - } _glfwInputWindowDamage(window); break; @@ -864,7 +812,6 @@ static void processSingleEvent(void) { case RRScreenChangeNotify: { - // Show XRandR that we really care XRRUpdateConfiguration(&event); _glfwRefreshMonitors(); break; @@ -1072,8 +1019,8 @@ void _glfwPlatformIconifyWindow(_GLFWwindow* window) { if (window->X11.overrideRedirect) { - // We can't iconify/restore override-redirect windows, as that's - // performed by the window manager + // Override-redirect windows cannot be iconified or restored, as those + // tasks are performed by the window manager return; } @@ -1091,8 +1038,8 @@ void _glfwPlatformRestoreWindow(_GLFWwindow* window) { if (window->X11.overrideRedirect) { - // We can't iconify/restore override-redirect windows, as that's - // performed by the window manager + // Override-redirect windows cannot be iconified or restored, as those + // tasks are performed by the window manager return; } @@ -1196,7 +1143,7 @@ void _glfwPlatformWaitEvents(void) void _glfwPlatformSetCursorPos(_GLFWwindow* window, int x, int y) { - // Store the new position so we can recognise it later + // Store the new position so it can be recognized later window->X11.cursorPosX = x; window->X11.cursorPosY = y; diff --git a/tests/events.c b/tests/events.c index 0bed007c..f1e71f9a 100644 --- a/tests/events.c +++ b/tests/events.c @@ -255,8 +255,11 @@ static void window_refresh_callback(GLFWwindow window) { printf("%08x at %0.3f: Window refresh\n", counter++, glfwGetTime()); - glClear(GL_COLOR_BUFFER_BIT); - glfwSwapBuffers(window); + if (glfwGetCurrentContext()) + { + glClear(GL_COLOR_BUFFER_BIT); + glfwSwapBuffers(window); + } } static void window_focus_callback(GLFWwindow window, int activated) diff --git a/tests/glfwinfo.c b/tests/glfwinfo.c index 90fd329f..11a84612 100644 --- a/tests/glfwinfo.c +++ b/tests/glfwinfo.c @@ -181,6 +181,28 @@ int main(int argc, char** argv) argc -= optind; argv += optind; + // Report GLFW version + + glfwGetVersion(&major, &minor, &revision); + + printf("GLFW header version: %u.%u.%u\n", + GLFW_VERSION_MAJOR, + GLFW_VERSION_MINOR, + GLFW_VERSION_REVISION); + + printf("GLFW library version: %u.%u.%u\n", major, minor, revision); + + if (major != GLFW_VERSION_MAJOR || + minor != GLFW_VERSION_MINOR || + revision != GLFW_VERSION_REVISION) + { + printf("*** WARNING: GLFW version mismatch! ***\n"); + } + + printf("GLFW library version string: \"%s\"\n", glfwGetVersionString()); + + // Initialize GLFW and create window + glfwSetErrorCallback(error_callback); if (!glfwInit()) @@ -216,26 +238,6 @@ int main(int argc, char** argv) glfwMakeContextCurrent(window); - // Report GLFW version - - glfwGetVersion(&major, &minor, &revision); - - printf("GLFW header version: %u.%u.%u\n", - GLFW_VERSION_MAJOR, - GLFW_VERSION_MINOR, - GLFW_VERSION_REVISION); - - printf("GLFW library version: %u.%u.%u\n", major, minor, revision); - - if (major != GLFW_VERSION_MAJOR || - minor != GLFW_VERSION_MINOR || - revision != GLFW_VERSION_REVISION) - { - printf("*** WARNING: GLFW version mismatch! ***\n"); - } - - printf("GLFW library version string: \"%s\"\n", glfwGetVersionString()); - // Report OpenGL version printf("OpenGL context version string: \"%s\"\n", glGetString(GL_VERSION)); diff --git a/tests/joysticks.c b/tests/joysticks.c index 488f98ad..40202ce7 100644 --- a/tests/joysticks.c +++ b/tests/joysticks.c @@ -137,7 +137,7 @@ static void refresh_joysticks(void) j->axes = realloc(j->axes, j->axis_count * sizeof(float)); } - glfwGetJoystickPos(GLFW_JOYSTICK_1 + i, j->axes, j->axis_count); + glfwGetJoystickAxes(GLFW_JOYSTICK_1 + i, j->axes, j->axis_count); button_count = glfwGetJoystickParam(GLFW_JOYSTICK_1 + i, GLFW_BUTTONS); if (button_count != j->button_count)