GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


Flag by MaFiaBoY
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")

#include "BMPLoader.h"
#include <GL/glut.h>
#include <iostream>
float Pole_R = 1 , Pole_G = 0.843137 , Pole_B = 0 ;

float updown = 0; // initial point

float direction = 1 ;
float fragment = 0 ;
float sky_blue =1;

float star_color1 = 0 ;
float star_color2 = 0 ;
float star_color3 = sky_blue ;

void Sky_Color(int value)
{
    switch( value )
    {
        case 1: 
        
        if( sky_blue == 1 )
        {
            return ;
        }
        else
        {
            glClearColor(0,0,sky_blue,0);

            sky_blue += 0.1 ;

            star_color1 = 0 ;
            star_color2 = 0 ;
            star_color3 = sky_blue ;
        }

        glutPostRedisplay();
        glutTimerFunc( 10 , Sky_Color , 1 ) ;
    
        break ;
        



        case 2: 
        
        
        if( sky_blue <= 0.1 )
        {
            return ;
        }
        else
        {
            glClearColor(0,0,sky_blue,0);

            sky_blue -= 0.1 ;

            star_color1 = 1 ;
            star_color2 = 1 ;
            star_color3 = 1 ;
        }

        glutPostRedisplay();
        glutTimerFunc( 10 , Sky_Color , 2 ) ;

            
        break ;
    }

    
}

void DayNight_Func( int id )
{
    switch( id )
    {
        case 1 :
            glutTimerFunc(10,Sky_Color,1);
            break ;

        case 2 : 
            glutTimerFunc(10,Sky_Color,2);
            break ;
    }

    glutPostRedisplay() ;  // Needed For Refreshing , To Apply Changes
}

void WindSpeed_Func( int id )
{
    switch( id )
    {
        case 1 : break ;
        case 2 : break ;
        case 3 : break ;
    }

    glutPostRedisplay() ;  // Needed For Refreshing , To Apply Changes
}

void WindDirection_Func( int id )
{
    switch( id )
    {
        case 1 : break ;
        case 2 : break ;
    }

    glutPostRedisplay() ;  // Needed For Refreshing , To Apply Changes
}

bool check = true ;

void flag_anthem( int value )
{
    if ( updown != -350 && check )
    {
        updown = -350 ;

        check = false ;
    }

    if( updown >= 0 )
    {
        check = true ;

        return ;
    }
    else
    {
        updown += 0.5 ;
    }


    glutPostRedisplay() ;
    glutTimerFunc( 10 , flag_anthem , 1 ) ;
}

void Sound_Func( int id )
{
    switch( id )
    {
        case 1 :
            
            glutTimerFunc( 50 , flag_anthem , 1 ) ;

            sndPlaySound("c:\\a.wav", SND_ASYNC);

        break ;    
    }

}

void FlagPole_Func( int id )
{
    switch( id )
    {
        case 1 : Pole_R = 0 ; Pole_G = 0 ; Pole_B = 0 ; break ;
        case 2 : Pole_R = 0.89 ; Pole_G = 0.86 ; Pole_B = 0.89 ; break ;
        case 3 : Pole_R = 1 ; Pole_G = 0.843137 ; Pole_B = 0 ; break ;
    }

    glutPostRedisplay() ;  // Needed For Refreshing , To Apply Changes
}

void main_Func( int id )
{
    switch( id )
    {
        case 1 : exit( 0 ) ; break ;    // Exit Program
    }
}


void Move_Flag( int specialkey , int x , int y )
{
    switch( specialkey )
    {
        case GLUT_KEY_UP: 
            
            if( updown != 0 )
            {
                updown = updown + 5;
            }

            break ;

        case GLUT_KEY_DOWN: 
            
            if( updown > -350 )
            {
                updown = updown - 5 ; 
            }

            break ;
    }

    glutPostRedisplay() ;  // Needed For Refreshing , To Apply Changes
}

void draw(void)
{
    glClear(GL_COLOR_BUFFER_BIT);


    
    // Building
    glBegin( GL_QUADS );

        glColor3f( 0 , 0 , 0 );

        glVertex2f( 50 , 30 ) ;
        glVertex2f( 130 , 30 ) ;
        glVertex2f( 130 , 200 ) ;
        glVertex2f( 50 , 200 ) ;
    //buildin 2
        glVertex2f( 50 , 30 ) ;
        glVertex2f( 130 , 30 ) ;
        glVertex2f( 130 , 200 ) ;
        glVertex2f( 50 , 200 ) ;
        //buildin 3
        glVertex2f( 50 , 30 ) ;
        glVertex2f( 130 , 30 ) ;
        glVertex2f( 130 , 200 ) ;
        glVertex2f( 50 , 200 ) ;
        //buildin 4    
        glVertex2f( 50 , 30 ) ;
        glVertex2f( 130 , 30 ) ;
        glVertex2f( 130 , 200 ) ;
        glVertex2f( 50 , 200 ) ;
        //buildin 5    
        glVertex2f( 50 , 30 ) ;
        glVertex2f( 130 , 30 ) ;
        glVertex2f( 130 , 200 ) ;
        glVertex2f( 50 , 200 ) ;

    glEnd();

    glPopMatrix() ;


    glPushMatrix() ;

    // Lights
    glPointSize( 8 ) ;
    glBegin( GL_POINTS );

        glColor3f( 1 , 1 , 0 );

        glVertex2f( 70 , 170 ) ;
        glVertex2f( 110 , 170 ) ;
        glVertex2f( 70 , 150 ) ;
        glVertex2f( 110 , 150 ) ;
        glVertex2f( 70 , 130 ) ;
        glVertex2f( 110 , 130 ) ;
        glVertex2f( 70 , 110 ) ;
        glVertex2f( 110 , 110 ) ;
        glVertex2f( 70 , 90 ) ;
        glVertex2f( 110 , 90 ) ;

    glEnd();

    glPopMatrix() ;

    // Stars
    glPointSize( 1 );
    glBegin( GL_POINTS ) ;

        glColor3f( star_color1 , star_color2 , star_color3 );

        glVertex2f( 200 , 550 ) ;
        glVertex2f( 300 , 530 ) ;
        glVertex2f( 100 , 500 ) ;
        glVertex2f( 500 , 550 ) ;
        glVertex2f( 700 , 560 ) ;
        glVertex2f( 450 , 490 ) ;

        glVertex2f( 300 , 450 ) ;
        glVertex2f( 400 , 430 ) ;
        glVertex2f( 200 , 400 ) ;
        glVertex2f( 600 , 450 ) ;
        glVertex2f( 800 , 460 ) ;
        glVertex2f( 550 , 390 ) ;

    glEnd() ;

    glPopMatrix() ;
    
    // Flag Pole
    glBegin( GL_POLYGON );
        
        glColor3f( Pole_R , Pole_G , Pole_B ) ;
        glVertex2f( 395 , 90 ) ;
        glVertex2f( 405 , 90 ) ;
        glVertex2f( 400 , 570 ) ;
        glVertex2f( 400 , 570 ) ;

    glEnd();

    glPopMatrix();


    glPushMatrix();

    // Ground
    glBegin( GL_POLYGON ) ;
    
        glColor3f( 0 , 1 , 0 ) ;

        glVertex2f( 0 , 0 ) ;
        glVertex2f( 800 , 0 ) ;
        glVertex2f( 750 , 40 ) ;
        glVertex2f( 700 , 60 ) ;
        glVertex2f( 600 , 80 ) ;
        glVertex2f( 400 , 100 ) ;
        glVertex2f( 200 , 80 ) ;
        glVertex2f( 100 , 60 ) ;
        glVertex2f( 50 , 40 ) ;

    glEnd();

    glPopMatrix();
    
    glColor3f( 1 , 1 ,1 ) ;
    glPushMatrix();


        // Grass
    glBegin( GL_LINES );

        glColor3f( 0 , 0.3 , 0 );

        for( int grass_3 = 10 ; grass_3 < 795 ; grass_3 = grass_3 + 4 )
        {
            glVertex2f( grass_3 , 0 );
            glVertex2f( grass_3 , 10 ) ;
        }

        for( int grass_2 = 20 ; grass_2 < 785 ; grass_2 = grass_2 + 4 )
        {
            glVertex2f( grass_2 , 10 );
            glVertex2f( grass_2 , 20 ) ;
        }

        for( int grass_1 = 30 ; grass_1 < 775 ; grass_1 = grass_1 + 4 )
        {
            glVertex2f( grass_1 , 20 );
            glVertex2f( grass_1 , 30 ) ;
        }

        for( int grass0 = 40 ; grass0 < 765 ; grass0 = grass0 + 4 )
        {
            glVertex2f( grass0 , 30 );
            glVertex2f( grass0 , 40 ) ;
        }

        for( int grass1 = 50 ; grass1 < 750 ; grass1 = grass1 + 4 )
        {
            glVertex2f( grass1 , 40 );
            glVertex2f( grass1 , 50 ) ;
        }

        for( int grass2 = 80 ; grass2 < 720 ; grass2 = grass2 + 4 )
        {
            glVertex2f( grass2 , 50 );
            glVertex2f( grass2 , 60 ) ;
        }

        for( int grass3 = 101 ; grass3 < 707 ; grass3 = grass3 + 4 )
        {
            glVertex2f( grass3 , 60 );
            glVertex2f( grass3 , 70 ) ;
        }

        for( int grass4 = 150 ; grass4 < 650 ; grass4 = grass4 + 4 )
        {
            glVertex2f( grass4 , 70 );
            glVertex2f( grass4 , 80 ) ;
        }

        for( int grass5 = 200 ; grass5 < 600 ; grass5 = grass5 + 4 )
        {
            glVertex2f( grass5 , 80 );
            glVertex2f( grass5 , 90 ) ;
        }

        for( int grass6 = 301 ; grass6 < 499 ; grass6 = grass6 + 4 )
        {
            glVertex2f( grass6 , 90 );
            glVertex2f( grass6 , 100 ) ;
        }

    glEnd();

glBegin(GL_QUADS);
        glTexCoord2d(0,0);    glVertex2d(400,450 + updown);
        glTexCoord2d(1,0);    glVertex2d(550,450+ updown);
        glTexCoord2d(1,1);    glVertex2d(550,520+ updown);
        glTexCoord2d(0,1);    glVertex2d(400,520+ updown);
    glEnd();    

    glFlush();

    glutSwapBuffers() ; 
}

int main(int argc,char** argv)
{
    glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB);
    glutInit(&argc,argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("OpenGL Flag Project");
    
    glutSpecialFunc(Move_Flag);

    std::string fname;
    fname ="jordan1.bmp";
    BMPClass bmp;
    BMPLoad(fname,bmp);

    glEnable(GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D,0,3,bmp.width,bmp.height,0,GL_RGB,GL_UNSIGNED_BYTE,bmp.bytes);

    // Wind Speed Sub Menu

    int Menu_WindSpeed = glutCreateMenu( WindSpeed_Func );

        glutAddMenuEntry( "Windy" , 1 ) ;
        glutAddMenuEntry( "Breezy" , 2 ) ;
        glutAddMenuEntry( "Calm" , 3 ) ;


    // Wind Direction Sub Menu

    int Menu_WindDirection = glutCreateMenu( WindDirection_Func );

        glutAddMenuEntry( "East" , 1 ) ;
        glutAddMenuEntry( "West" , 2 ) ;

    int Menu_DayNight = glutCreateMenu (DayNight_Func);
        glutAddMenuEntry("Day" ,1);
        glutAddMenuEntry("Night",2);

    // Flag Pole Sub Menu

    int Menu_FlagPole = glutCreateMenu( FlagPole_Func );

        glutAddMenuEntry( "Black" , 1 ) ;
        glutAddMenuEntry( "Silver" , 2 ) ;
        glutAddMenuEntry( "Gold" , 3 ) ;

    int Sound = glutCreateMenu ( Sound_Func);
        glutAddMenuEntry( "Run", 1);

    // Main Menu

    glutCreateMenu( main_Func );

        glutAddSubMenu( "Wind Speed" , Menu_WindSpeed ) ;
        glutAddSubMenu( "Wind Direction" , Menu_WindDirection ) ;
        glutAddSubMenu( "Flag Pole" , Menu_FlagPole ) ;
        glutAddSubMenu( "Jordan Anthem", Sound);
        glutAddSubMenu("Now", Menu_DayNight);
        glutAddMenuEntry( "Quit" , 1 );

    glutAttachMenu( GLUT_RIGHT_BUTTON ) ;



    glClearColor( 0 , 0 , sky_blue, 0 ) ;

    glutDisplayFunc(draw);
    glutSpecialFunc(Move_Flag);
    
    gluOrtho2D(0,800,0,600);
    
    glutMainLoop();
    return 0;
}