- // e_lab2.cc
- // Based on e_imgmapcube.cc Copyright (C) 2006-2007 Joe Linhoff, All Rights Reserved
- #include "config.h"
- #if BUILD_HUD // compile this app
- #include "qec.h"
- #define IMGPATH "../../../common/assets/img/"
- typedef struct {
- float lastupdate; // How long the engine has been running
- float lasttime; // How long it took to go between frames
- uns btnUp; // button count
- uns btnDown; // button count
- uns buttonone; // button count 1
- uns buttonzero; // button count 0
- int val;
- } GameHud;
- GameHud Game;
- // defines to make building the cube easier
- #define X 2
- #define Y 2
- #define Z 2
- GameHud *game=&Game;
- int drawHealth()
- {
- float val,w,h;
- // set 2D camera
- // (0,0,0) ... (1,0,0)
- // (0.5,0.5,0)
- // (0,1,0) ... (1,1,0)
- qefnSysCam2D();
- // compute bar width & height in cam coords
- w=(1.0/4.0)*game->val;
- h=1.0/20.0;
- uns j;
- // used for buttons
- GameHud *game=&Game; // game controller
- // adds health when pressing up key
- j=qeInpButton(QEINPBUTTON_UP);
- if((j&1)&&(j!=Game.btnUp)&&game->val < 1)
- { // up key
- Game.btnUp=j;
- game->val+= .1;
- } // up key
- // removes health when pressing down key
- j=qeInpButton(QEINPBUTTON_DOWN);
- if((j&1)&&(j!=Game.btnUp)&&game->val >=0 )
- { // up key
- Game.btnUp=j;
- game->val-= .1;
- } // up key
- // color & fill
- glColor3f(0,1,0);
- glPolygonMode(GL_FRONT,GL_FILL);
- // draw as quad, counter-clockwise
- glBegin(GL_QUADS);
- glVertex3f(1,0,0); // top right
- glVertex3f(1-w,0,0); // top left
- glVertex3f(1-w,h,0); // bot left
- glVertex3f(1,h,0); // bot right
- glEnd();
- return 0;
- };
- // 8 verts
- float cube_verts[]=
- {
- -X,Y,Z, // front left top
- -X,-Y,Z, // front left bottom
- X,-Y,Z, // front right bottom
- X,Y,Z, // front right top
- -X,Y,-Z, // back left top
- -X,-Y,-Z, // back left bottom
- X,-Y,-Z, // back right bottom
- X,Y,-Z, // back right top
- }; // cube_verts[]
- // 6 faces
- int cube_faces[]=
- {
- // number of vertices followed by the vert indices
- 4,1,5,6,2, // bottom
- 4,4,5,1,0, // left
- 4,7,6,5,4, // back
- 4,2,6,5,1, // floor
- 4,0,1,5,4, // left wall
- 4,4,5,6,7, // right wall
- }; // cube_faces[]
- // 24 UV coords for 6 faces, map all faces to the same part of the image
- float cube_fuvs[] =
- {
- //Big checker:
- 0.5,0.25, 0.5,0.746, 0.996,0.746, 0.996,0.25,
- //Green:
- 0.496,0.746, 0.496,0.25, 0,0.25, 0,0.746,
- //Yellow:
- 0.75,0, 0.75,0.246, 0.996,0.246, 0.996,0,
- //Small checker:
- 0.5,0, 0.5,0.246, 0.746,0.246, 0.746,0,
- //Red:
- 0.246,0, 0,0, 0,0.246, 0.246, 0.246,
- //Blue:
- 0.25,0.246, 0.496, 0.246, 0.496,0, 0.25,0,
- }; // cube_fuvs[]
- // cube geometry
- qeGeo2 cube_geo =
- {
- 8, // numv
- 6, // numf
- cube_verts, // verts
- NUL, // vnrms
- cube_faces, // faces
- cube_fuvs, // uvs
- }; // cube_geo
- int drawCube()
- {
- qeGeo2 *geo=&cube_geo; // pointer reference to geometry
- // axes & grid
- qefnDrawAxes(1);
- qefnDrawGrid(10,1);
- // setup texture mode to blend
- glPolygonMode(GL_FRONT,GL_FILL);
- glEnable(GL_TEXTURE_2D);
- glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
- // bind image with digits
- if(qeTexBind("lab2")<0)
- BRK();
- // set color & alpha (alpha is transparency)
- glColor4f(0,0,1,0.75);
- // draw
- qeDrawUVFaces(geo->verts,geo->faces,geo->uvs,geo->numf);
- // back to default state
- glDisable(GL_BLEND);
- glDisable(GL_TEXTURE_2D);
- return 0;
- } // gameDraw()
- // JFL 04 Oct 06
- int gameMainLoop(void)
- {
- drawCube();
- drawHealth();
- return 0;
- }
- void gameInit()
- {
- } // GameInit()
- int gameSetup()
- {
- // add image
- if(qeImgNew("lab2",0,IMGPATH"lab2.tga")<0)
- BRK();
- return 0;
- } // gameSetup()
- void gameFinal()
- {
- } // gameFinal()
- int qeMain(int argc,chr *argv[])
- {
- gameInit(); // initialize
- // print information
- qePrintf("%s / %s / %s\n",__FILE__,qeVersion(),glGetString(GL_VERSION));
- gameSetup(); // setup, load
- game->val=0.75; // 75% power
- // add a function to the engine's object loop
- if(!qeObjAddFnc(gameMainLoop))
- BRK();
- qeForever(); // turn control over to qe until the user closes the program
- gameFinal(); // finalize, free
- if(qefnCheckForCleanExit()<0)
- BRK(); // see qelog.txt
- return 0;
- } // qeMain()
- #endif // compile this app
- // EOF