Jump to content
  • Advertisement
Sign in to follow this  
sSimontis

Compilation Errors with Graphics Demo

This topic is 5124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The past month or so, I have been working on a graphical demo for a project. Today I made a lot of changes and additions, and now I get a lot of errors. I have tried to find out what is causing these errors, but I have been unable to find any cause for the errors. My project is in four files: scifair.cpp
/*
  Name: scifair.cpp
  Copyright (c) 2004 Scott Simontis
  Author: Scott Simontis (ssimontis@spymac.com)
  Date: 01/11/04 17:33
  Description: Main source file for project.
*/
#include "tgaloader.h"
#include "scifair.h"
#include <gl/freeglut.h>

int main( int argc, char* argv[] )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA );
    glutInitWindowPosition( 0, 0 );
    glutInitWindowSize( 1024, 768 );
    glutCreateWindow( "Framerate demo" );
    glutReshapeFunc( ResizeScene );
    glutKeyboardFunc( ProcessInput );
    glutDisplayFunc( DrawScene );
    
    gluDeleteQuadric( quadratic );
    return 0;
}

scifair.h
/*
  Name: scifair.h
  Copyright (c) 2004 Scott Simontis
  Author: Scott Simontis (ssimontis@spymac.com)
  Date: 02/11/04 17:49
  Description: Defines some needed functions for the project's main file.
*/
#include <gl/gl.h>
#include <gl/glu.h>
#include <cstdlib>
#include <cmath>

TGAImage backgroundNear; //Background image with GL_NEAREST filtering
TGAImage backgroundLinear; //Background image with GL_LINEAR filtering
TGAImage reflectionNear; //Reflective image with GL_NEAREST filtering
TGAImage reflectionLinear; //Reflective image with GL_LINEAR filtering

GLUquadricObj* quadratic;

float points[45][45][3]; //Grid points of "wave" background
int waveSpeed = 0; //Counter that controls speed of wave
GLfloat temp; //Temporary variable for swap
int currentTex;
float quadX; //X Rotation for quadratic
float quadY; //Y ROtation for quadratic
int quadraticSelect = 0; //Selects the quadric to be used
int texture = 0; //Texture Filter

//////////////////////////////////ResizeScene()/////////////////////////////////
// Resizes the scene when the window's dimensions change. Makes sure the height
// is at least 1 to prevent a divide-by-zero error, resets the viewport, and
// then resets the projection matrix, calculates the aspect ratio, and resets
// the modelview matrix.
///////////////////////////////////////////////////////////////////////////////
GLvoid ResizeScene( GLsizei width, GLsizei height )
{
    if( 0 == height )
    {
        height = 1;
    }
    
    glViewport( 0, 0, width, height );
    
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    
    gluPerspective( 45.0f, (GLfloat)width / (GLfloat)height, 0.0f, 100.0f );
    
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

////////////////////////////////////InitGL/////////////////////////////////////
// Initalizes OpenGL, setting up shading, loading textures, enabling/disabling
// functions, etc. It then creates the points for the wave effect which is used
// with the background.
///////////////////////////////////////////////////////////////////////////////
int InitGL()
{
    glShadeModel( GL_SMOOTH );
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClearDepth( 1.0f );
    
    for( int i = 0; i < 45; i++ )
    {
        for( int j = 0; j < 45; j++ )
        {
            points[j][0] = float( ( i / 5.0f ) - 4.5f );
            points[j][1] = float( ( j / 5.0f ) - 4.5f );
            points[j][2] = float( sin( ( ( ( i / 5.0f ) * 40.0f ) / 360.0f ) * M_PI * 2.0f ) );
            //The above line uses the slightly confusing math to convert degrees to radians
        }
    }
    
    glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
    glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
            
    return true;
}

////////////////////////////////ProcessInput///////////////////////////////////
// Takes input from the keyboard and processes it.
///////////////////////////////////////////////////////////////////////////////
void ProcessInput( unsigned char key, int x, int y )
{
    if( 27 == key )
    {
        exit(1);
    }
}

////////////////////////////////LoadTextures///////////////////////////////////
// Loads the reflecting image and the background image.
///////////////////////////////////////////////////////////////////////////////
bool LoadTextures()
{
    if( !backgroundLinear.LoadTGA( "background.tga", 1 ) )
    {
        return false;
    }
    if( !backgroundNear.LoadTGA( "background.tga", 0 ) );
    {
        return false;
    }
    
    if( !reflectionLinear.LoadTGA( "reflection.tga", 1 ) );
    {
        return false;
    }
    if( !reflectionNear.LoadTGA( "relflection.tga", 0 ) );
    {
        return false;
    }
    return true;
}

//////////////////////////////////DrawFlag/////////////////////////////////////
// Draws the waving flag for the background. Draws quads and uses values calculated
// from them to get the texture points for the flag. The flag is then textured
// using these points. Select the texture before hand.
///////////////////////////////////////////////////////////////////////////////
int DrawFlag()
{
    glLoadIdentity();
    glTranslatef( 0.0f, 0.0f, -10.0f );
    
    float x, y, xb, yb; //Used when breaking the flag into tiny quads
    
    glBegin( GL_QUADS );
    for( int i = 0; i < 44; i++ )
    {
        for( int j = 0; j < 44; j++ )
        {
            x = float(i) / 44.0f;
            y = float(j) / 44.0f;
            xb = float(i + 1) / 44.0f;
            yb = float(j + 1) / 44.0f;
            
            glTexCoord2f( x, y );
            glVertex3f( points[j][0], points[j][1], points[j][2] );
            
            glTexCoord2f( x, yb );
            glVertex3f( points[j + 1][0], points[j + 1][1], points[j + 1][2] );
            
            glTexCoord2f( xb, yb );
            glVertex3f( points[i + 1][j + 1][0], points[i + 1][j + 1][1], points[i + 1][j + 1][2] );
            
            glTexCoord2f( xb, y );
            glVertex3f( points[i + 1][j][0], points[i + 1][j][1], points[i + 1][j][2] );
        }
    }
    glEnd();
    
    
    if( 2 == waveSpeed )
    {
        for( int i = 0; i < 45; i++ )
        {
            temp = points[0][2];
            for( int j = 0; j < 44; j++ )
            {
                points[j][2] = points[j + 1][2];
            }
            points[44][2] = temp;
        }
        waveSpeed = 0;
    }
    waveSpeed++;
    
    return true;
}

////////////////////////////////////DrawQuadric////////////////////////////////
// Draws one of three quadrics. The quadrics are sphere-mapped. Make sure to
// bind the proper texture before calling this function.
///////////////////////////////////////////////////////////////////////////////
int DrawQuadric()
{
    glLoadIdentity();
    glTranslatef( 0.0f, 0.0f, -5.0f );
    glEnable( GL_TEXTURE_GEN_S );
    glEnable( GL_TEXTURE_GEN_T );
    
    glPushMatrix();
    glRotatef( quadX, 1.0f, 0.0f, 0.0f );
    glRotatef( quadY, 0.0f, 1.0f, 0.0f );
    
    switch( quadraticSelect )
    {
        case 0:
       		glTranslatef( 0.0f, 0.0f, -1.5f );
       		gluCylinder( quadratic, 1.0f, 1.0f, 3.0f, 32, 32 );
       		quadraticSelect++;
       		break;
       		
  		case 1:
  		    gluSphere( quadratic, 1.3f, 32, 32 );
  		    quadraticSelect++;
  		    break;
  		    
        case 2:
            glTranslatef( 0.0f, 0.0f, -1.5f );
            gluCylinder( quadratic, 1.0f, 0.0f, 3.0f, 32, 32 );
            quadraticSelect = 0;
            break;
    };
    
    glPopMatrix();
    
    glDisable( GL_TEXTURE_GEN_S );
    glDisable( GL_TEXTURE_GEN_T );
    
    quadX += 0.5f;
    quadY += 0.7f;
    
}

/////////////////////////////////////DrawScene/////////////////////////////////
// Draws the scene. Simply wraps the function to draw the flag and the function
// to draw the quadric.
///////////////////////////////////////////////////////////////////////////////
void DrawScene()
{
    if ( !texture )
    {
        glBindTexture( GL_TEXTURE_2D, backgroundNear.texID_ );
    }
    else
    {
        glBindTexture( GL_TEXTURE_2D, backgroundLinear.texID_ );   
    }
    DrawFlag();
    
    if( !texture )
    {
        glBindTexture( GL_TEXTURE_2D, reflectionNear.texID_ );
    }
    else
    {
        glBindTexture( GL_TEXTURE_2D, reflectionLinear.texID_ );
    }
    DrawQuadric();
}

tgaloader.cpp
/*
  Name: tgaloader.cpp
  Copyright (c) 2004 Scott Simontis
  Author: Scott Simontis (ssimontis@spymac.com)
  Date: 01/11/04 17:41
  Description: A class that loads a targa image.
*/
#include "tgaloader.h"
#include <gl/gl.h>
#include <cstdlib>
#include <cstdio>

/////////////////////////////////////LoadTGA////////////////////////////////////
// Loads a targa image. This function first trys to open the file, and if it
// exists, checks its header to make sure the file is a 24- or a 32-bit
// UNCOMPRESSED TGA file. After this check is made, the next 6 bytes after the
// header are read to get the width, height, and bpp. The image width, height,
// bpp, and size are then determined, and the proper ammount of memory is
// allocated for the image. If the memory does not exist, the function exits.
// Because TGAs store data in BGR instead of RGB, we swap the first and third
// bits in a loop. Then we generate the texture, bind it, and select the filters.
// If the image is 24 bits, we set the type to GL_RGB, otherwise it is GL_RGBA.
// We then create the texture and exit. The variable texType is an unsigned int
// with a value of 0 or 1. 0 uses GL_NEAREST for creating the texture, 1
// uses GL_LINEAR.
////////////////////////////////////////////////////////////////////////////////
bool TGAImage::LoadTGA( const char* filename, GLuint texType )
{
    GLubyte tgaHeader[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    GLubyte imgHeader[12]; //The files header, to be compared with the above header
    GLubyte header[6]; //Width, height, and bpp info
    GLuint bpp;
    GLuint imageSize;
    GLuint swap;
    GLuint type = GL_RGBA;
    
    FILE* image = fopen( filename, "rb" );
    
    if( NULL == image ||
    	fread( imgHeader, 1, sizeof(imgHeader), image ) != sizeof(imgHeader) ||
    	memcmp( tgaHeader, imgHeader, sizeof(tgaHeader) ) != 0 ||
    	fread( header, 1, sizeof(header), image ) != sizeof(header) )
   	{
   	    if( NULL == image )
   	    {
   	        return false;
   	    }
        else
        {
            fclose( image );
            return false;
        }
    }
    
    TGAImage.width_ = header[1] * 256 + header[0];
    TGAImage.height_ = header[3] * 256 + header[2];
    
    if( TGAImage.width_ <= 0 ||
    	TGAImage.height_ <= 0 ||
    	( header[4] != 24 && header[4] != 32 ) )
   	{
   	    fclose( image );
   	    return false;
   	}
    
    TGAImage.bpp_ = header[4];
    bpp = TGAImage.bpp_ / 8; //Get the bytes per pixel from the bits per pixel
    imageSize = TGAImage.width_ * TGAImage.height_ * bpp;
    TGAImage.imageData_ = (GLubyte*)malloc( imageSize );
    
    if( NULL == TGAImage.imageData_  ||
    	fread( TGAImage.imageData_, 1, imageSize, image ) != imageSize )
   	{
   	    if( TGAImage.imageData_ != NULL )
   	    {
   	        free( texture->imageData_ );
   	    }
        fclose( image );
        return false;
    }
    
    for( int i = 0; i < int(imageSize); i+= bpp )
    {
        swap = texture->imageData_;
        TGAImage.imageData_ = TGAImage.imageData_[i + 2];
        TGAImage.imageData_[i + 2] = swap;
    }
    fclose( image );
    
    glGenTextures( 1, &texture[0].texID_ );
    glBindTexture( GL_TEXTURE_2D, (GLuint)texture[0].texID_ );
    
    if( 0 == texType )
    {
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    }
    if( 1 == texType )
    {
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    }
     
    
    if( 24 == TGAImage.bpp_ )
    {
        type = GL_RGB;
    }
    
    glTexImage2D( GL_TEXTURE_2D, 0, type, TGAImage.width_, TGAImage.height_, 0, type, GL_UNSIGNED_BYTE, TGAImage.imageData_ );
    
    return true;
}

tgaloader.h
/*
  Name: tgaloader.h
  Copyright (c) 2004 Scott Simontis
  Author: Scott Simontis (ssimontis@spymac.com)
  Date: 01/11/04 17:37
  Description: A class that loads a targa image.
*/
#ifndef TGALOADER_H
#define TGALOADER_H

#include <gl/gl.h>

class TGAImage
{
    public:
    bool LoadTGA( const char* filename, GLuint texType );
    GLuint texID_;
    GLubyte* imageData_;
    GLuint bpp_;
    GLuint width_;
    GLuint height_;
    
};

#endif

Here is the compile log:
Compiler: Default compiler
Building Makefile: "C:\Code\Makefile.win"
Executing  make...
mingw32-make.exe -f "C:\Code\Makefile.win" all
g++.exe -c tgaloader.cpp -o tgaloader.o -I"C:/Dev-Cpp/include/c++/3.3.1"  -I"C:/Dev-Cpp/include/c++/3.3.1/mingw32"  -I"C:/Dev-Cpp/include/c++/3.3.1/backward"  -I"C:/Dev-Cpp/lib/gcc-lib/mingw32/3.3.1/include"  -I"C:/Dev-Cpp/include"  -I"C:/Dev-Cpp/include/GL"  -I"C:/Dev-Cpp/include/SDL"  -I"C:/Dev-Cpp/include/allegro"  -I"C:/Dev-Cpp/allegro/include"  -I"C:/wx/include"  -I"C:/Code"   

tgaloader.cpp: In member function `bool TGAImage::LoadTGA(const char*, GLuint)':
tgaloader.cpp:41: error: `memcmp' undeclared (first use this function)
tgaloader.cpp:41: error: (Each undeclared identifier is reported only once for each function it appears in.)
tgaloader.cpp:55: error: expected primary-expression before '.' token
tgaloader.cpp:56: error: expected primary-expression before '.' token
tgaloader.cpp:58: error: expected primary-expression before '.' token
tgaloader.cpp:59: error: expected primary-expression before '.' token
tgaloader.cpp:66: error: expected primary-expression before '.' token
tgaloader.cpp:67: error: expected primary-expression before '.' token
tgaloader.cpp:68: error: expected primary-expression before '.' token
tgaloader.cpp:68: error: expected primary-expression before '.' token

tgaloader.cpp:69: error: expected primary-expression before '.' token
tgaloader.cpp:71: error: expected primary-expression before '.' token
tgaloader.cpp:72: error: expected primary-expression before '.' token
tgaloader.cpp:74: error: expected primary-expression before '.' token
tgaloader.cpp:76: error: `texture' undeclared (first use this function)
tgaloader.cpp:85: error: expected primary-expression before '.' token
tgaloader.cpp:85: error: expected primary-expression before '.' token
tgaloader.cpp:86: error: expected primary-expression before '.' token
tgaloader.cpp:105: error: expected primary-expression before '.' token
tgaloader.cpp:110: error: expected primary-expression before '.' token
tgaloader.cpp:110: error: expected primary-expression before '.' token
tgaloader.cpp:110: error: expected primary-expression before '.' token

mingw32-make.exe: *** [tgaloader.o] Error 1

Execution terminated

I thought memcmp was in stdlib, and I don't know how to fix the other errors. Sorry if the code is messy, this project has been a pain.

Share this post


Link to post
Share on other sites
Advertisement
First, get rid of the "TGAImage." before the TGAImage members in the member function. Those are incorrect. Next, memcmp is not in stdlib. It is in libc and is declared in string.h or memory.h

Share this post


Link to post
Share on other sites
Quote:

TGAImage.width_ = header[1] * 256 + header[0];


I don't think you can refer to a class like this unless it's static. You have to either instantiate it, or in this case you probably want to use the keyword this
as in:
 this.width_ = header[1]*256+header[0]; 


edit: or you can do as mastaba says and just leave it off as in:
 width_ = header[1]*256+header[0]; 

Share this post


Link to post
Share on other sites
Quote:
Today I made a lot of changes and additions, and now I get a lot of errors.


And hence, you learned about the importance of using source control (like CVS or Subversion), even when you're only working on your own code with yourself. With source control, you can check out an older, working copy of your code, and you can compare what you ACTUALLY changed (which is often different from what you THOUGHT you changed).

I recommend getting a copy of Subversion; it's free, and easy to install, and works reasonably well. Then, use it for ALL code you write. Make it a habit.

Then remember to back up your subversion repository to physical media once in a while, so your up-coming hard disk crash won't be a similar learning experience :-)

Share this post


Link to post
Share on other sites
Thank you all for your help. I'll go in and make those changes. I will also look into some source control apps. For now, I will copy the working code into another directory, but sometime this weekend I will try Subversion. And I do back up to physical media about once a month or so. Hard drive failures are nothing new to me!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I finally had the time to go back and fix the errors. It compiles correctly now, but I keep getting an error. When i try to run the program, it freezes up, and tells me "scifair.exe has encountered a problem and needs to close. We are sorry for the inconvenience." When I debug the program, I am told that "An Access Violation (Segmentation Fault) raised in your program." To be perfectly honest, I don't know anything about debugging. Right now I am using GDB in Dev-C++. I don't know where to go now that I know the problem. Here is the output for the debugger:

pre-prompt
(gdb)
prompt


post-prompt
Reading symbols from C:\Code\scifair.exe...done
frames-invalid
pre-prompt
(gdb)
prompt
post-prompt
pre-prompt
(gdb)
prompt
post-prompt


post-prompt
Reading symbols from C:\Code\scifair.exe...done
frames-invalid
pre-prompt
(gdb)
prompt
post-prompt
pre-prompt
(gdb)
prompt
post-prompt


post-prompt
Continuing
Starting
frames-invalid
signal
Program recieved signal
signal-name
SIGSEGV
signal-name-end
,
signal-string
Segmentation fault
signal-string-end
.
frame-begin 0 0x68b27f67
frame-address
0x68b27f67
frame-address-end
in
frame-function-name
_libmsvcrt_a_iname
frame-args
()
frame-end
stopped
pre-prompt
(gdb)
prompt


Does anyone know what this means and how I can fix this? Sorry for my lack of debugging knowledge. You can download the source at http://home.comcast.net/~ageofscott/scifair.zip, and the .exe at http://home.comcast.net/~ageofscott/scifair.exe. Thanks in advanced for any help.

Share this post


Link to post
Share on other sites
I have tried to debug, and stepped through the program. Everything goes fine until the line glutMainLoop(). The segmentation fault occurs right there. Does anyone know what might be causing this?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!