Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 5.16 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // e_lab2.cc
  2.     // Based on e_imgmapcube.cc Copyright (C) 2006-2007 Joe Linhoff, All Rights Reserved
  3.      
  4.     #include "config.h"
  5.     #if BUILD_HUD // compile this app
  6.     #include "qec.h"
  7.      
  8.     #define IMGPATH "../../../common/assets/img/"
  9.  
  10.      
  11. typedef struct {
  12.    float lastupdate; // How long the engine has been running
  13.    float lasttime; // How long it took to go between frames
  14.    uns btnUp; // button count
  15.    uns btnDown; // button count
  16.    uns buttonone; // button count 1
  17.    uns buttonzero; // button count 0
  18.    int val;
  19. } GameHud;
  20.  
  21. GameHud Game;
  22.  
  23.     // defines to make building the cube easier
  24.     #define X 2
  25.     #define Y 2
  26.     #define Z 2
  27.  
  28.  
  29.  
  30.  
  31. GameHud *game=&Game;
  32.  
  33.  
  34. int drawHealth()
  35. {
  36.                 float val,w,h;
  37.  
  38.    // set 2D camera
  39.    //  (0,0,0)    ...    (1,0,0)
  40.    //         (0.5,0.5,0)
  41.    //  (0,1,0)    ...    (1,1,0)
  42.    qefnSysCam2D();
  43.  
  44.    // compute bar width & height in cam coords
  45.    w=(1.0/4.0)*game->val;
  46.    h=1.0/20.0;
  47.  
  48.  
  49.     uns j;
  50.    // used for buttons
  51.  
  52.    GameHud *game=&Game; // game controller
  53.  
  54.    // adds health when pressing up key
  55.    j=qeInpButton(QEINPBUTTON_UP);
  56.    if((j&1)&&(j!=Game.btnUp)&&game->val < 1)
  57.    { // up key
  58.       Game.btnUp=j;
  59.       game->val+= .1;
  60.    } // up key
  61.  
  62.  
  63.    // removes health when pressing down key
  64.    j=qeInpButton(QEINPBUTTON_DOWN);
  65.    if((j&1)&&(j!=Game.btnUp)&&game->val >=0 )
  66.    { // up key
  67.       Game.btnUp=j;
  68.       game->val-= .1;
  69.    } // up key
  70.  
  71.  
  72.  
  73.    // color & fill
  74.    glColor3f(0,1,0);
  75.    glPolygonMode(GL_FRONT,GL_FILL);
  76.  
  77.    // draw as quad, counter-clockwise
  78.    glBegin(GL_QUADS);
  79.    glVertex3f(1,0,0); // top right
  80.    glVertex3f(1-w,0,0); // top left
  81.    glVertex3f(1-w,h,0); // bot left
  82.    glVertex3f(1,h,0); // bot right
  83.    glEnd();
  84.  
  85.    
  86.    return 0;
  87.  
  88.  
  89. };
  90.      
  91.     // 8 verts
  92.     float cube_verts[]=
  93.     {
  94.        -X,Y,Z, // front left top
  95.        -X,-Y,Z, // front left bottom
  96.        X,-Y,Z, // front right bottom
  97.        X,Y,Z, // front right top
  98.        -X,Y,-Z, // back left top
  99.        -X,-Y,-Z, // back left bottom
  100.        X,-Y,-Z, // back right bottom
  101.        X,Y,-Z, // back right top
  102.     }; // cube_verts[]
  103.      
  104.     // 6 faces
  105.     int cube_faces[]=
  106.     {
  107.      // number of vertices followed by the vert indices
  108.      4,1,5,6,2, // bottom
  109.      4,4,5,1,0, // left
  110.      4,7,6,5,4, // back
  111.      4,2,6,5,1, // floor
  112.      4,0,1,5,4, // left wall
  113.      4,4,5,6,7, // right wall
  114.     }; // cube_faces[]
  115.      
  116.     // 24 UV coords for 6 faces, map all faces to the same part of the image
  117.     float cube_fuvs[] =
  118.     {
  119.        //Big checker:
  120.        0.5,0.25,    0.5,0.746,    0.996,0.746,   0.996,0.25,
  121.      
  122.        //Green:
  123.        0.496,0.746, 0.496,0.25,   0,0.25,        0,0.746,    
  124.      
  125.        //Yellow:
  126.        0.75,0,      0.75,0.246,   0.996,0.246,   0.996,0,
  127.      
  128.        //Small checker:
  129.        0.5,0,       0.5,0.246,    0.746,0.246,   0.746,0,
  130.      
  131.        //Red:
  132.        0.246,0,     0,0,          0,0.246,       0.246, 0.246,
  133.      
  134.        //Blue:
  135.        0.25,0.246,  0.496, 0.246, 0.496,0,       0.25,0,
  136.      
  137.      
  138.      }; // cube_fuvs[]
  139.      
  140.     // cube geometry
  141.     qeGeo2 cube_geo =
  142.     {
  143.        8, // numv
  144.        6, // numf
  145.        cube_verts, // verts
  146.        NUL, // vnrms
  147.        cube_faces, // faces
  148.        cube_fuvs, // uvs
  149.     }; // cube_geo
  150.      
  151.    
  152.     int drawCube()
  153.     {
  154.        qeGeo2 *geo=&cube_geo; // pointer reference to geometry
  155.      
  156.        // axes & grid
  157.        qefnDrawAxes(1);
  158.        qefnDrawGrid(10,1);
  159.      
  160.        // setup texture mode to blend
  161.        glPolygonMode(GL_FRONT,GL_FILL);
  162.        glEnable(GL_TEXTURE_2D);
  163.        glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
  164.      
  165.        // bind image with digits
  166.        if(qeTexBind("lab2")<0)
  167.           BRK();
  168.      
  169.        // set color & alpha (alpha is transparency)
  170.        glColor4f(0,0,1,0.75);
  171.      
  172.        // draw
  173.        qeDrawUVFaces(geo->verts,geo->faces,geo->uvs,geo->numf);
  174.      
  175.        // back to default state
  176.        glDisable(GL_BLEND);
  177.        glDisable(GL_TEXTURE_2D);
  178.        return 0;
  179.     } // gameDraw()
  180.      
  181.     // JFL 04 Oct 06
  182.     int gameMainLoop(void)
  183.  
  184.         {
  185.                 drawCube();
  186.                 drawHealth();
  187.                 return 0;
  188.         }
  189.  
  190.     void gameInit()
  191.     {
  192.     } // GameInit()
  193.      
  194.    
  195.     int gameSetup()
  196.     {
  197.        // add image
  198.        if(qeImgNew("lab2",0,IMGPATH"lab2.tga")<0)
  199.           BRK();
  200.        return 0;
  201.     } // gameSetup()
  202.      
  203.    
  204.     void gameFinal()
  205.     {
  206.     } // gameFinal()
  207.      
  208.    
  209.     int qeMain(int argc,chr *argv[])
  210.     {
  211.        gameInit(); // initialize
  212.      
  213.        // print information
  214.        qePrintf("%s / %s / %s\n",__FILE__,qeVersion(),glGetString(GL_VERSION));
  215.      
  216.        gameSetup(); // setup, load
  217.  
  218.            game->val=0.75; // 75% power
  219.  
  220.      
  221.        // add a function to the engine's object loop
  222.        if(!qeObjAddFnc(gameMainLoop))
  223.           BRK();
  224.      
  225.        qeForever(); // turn control over to qe until the user closes the program
  226.      
  227.        gameFinal(); // finalize, free
  228.      
  229.        if(qefnCheckForCleanExit()<0)
  230.           BRK(); // see qelog.txt
  231.      
  232.        return 0;
  233.     } // qeMain()
  234.      
  235.     #endif // compile this app
  236.     // EOF