Advertisement
Guest User

main.cpp

a guest
Apr 11th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. #define GL_CLAMP_TO_EDGE 0x812F
  2. #include "engine.h"
  3. #include "cw3Load.h"
  4. #include "cw2Load.h"
  5. //-----------------
  6. using namespace std;
  7. int Width = 1280, Height = 720;
  8. int Wi1,He1;
  9. int FPS = 0,Frames = 0;
  10. DWORD LastFPS = 0;
  11. GLuint LoadTexture(char *TexName);
  12. //GLuint ModTex1; Tex Name!
  13. cw2Load Img;      
  14. GLuint Texture;
  15. float angle;
  16.  
  17. enum key_state {Release, Press} keyarr[127];
  18. //---------------------
  19. float PlayerPosX, PlayerPosY;
  20. //---------------------
  21. #pragma region KeyStates+-
  22. void key(unsigned char key, int x, int y)
  23. {
  24. if(key == 'w')keyarr[int('w')] = Press;
  25. if(key == 'a')keyarr[int('a')] = Press;
  26. if(key == 32)keyarr[int(32)] = Press;
  27. if(key == 's')keyarr[int('s')] = Press;
  28. if(key == 'd')keyarr[int('d')] = Press;
  29. if(key == 'z')keyarr[int('z')] = Press;
  30. if(key == 'x')keyarr[int('x')] = Press;
  31.  
  32. }
  33. void keyup(unsigned char key, int x, int y)
  34. {
  35. if(key == 'w')keyarr['w'] = Release;
  36. if(key == 'a')keyarr['a'] = Release;
  37. if(key == 32)keyarr[32] = Release;
  38. if(key == 's')keyarr[int('s')] = Release;
  39. if(key == 'd')keyarr[int('d')] = Release;
  40. if(key == 'z')keyarr[int('z')] = Release;
  41. if(key == 'x')keyarr[int('x')] = Release;
  42. }
  43.  
  44. #pragma endregion
  45. void Draw2DSt(void)
  46. {
  47. //For 2D sprites ,textures....
  48. glViewport(0,0,Width,Height);
  49. glMatrixMode(GL_PROJECTION);
  50. glPushMatrix();
  51. glLoadIdentity();
  52. gluOrtho2D(0,Wi1,0,He1);
  53. glScalef(1,-1,1);
  54. glTranslatef(0,-He1,0);
  55. glMatrixMode(GL_MODELVIEW);
  56. }
  57. void Draw2DEnd(void)
  58. {
  59.     //For 3D obj's
  60. glViewport(0,0,(GLfloat)Width,(GLfloat)Height);
  61. glMatrixMode(GL_PROJECTION);
  62. glEnable (GL_DEPTH_TEST);
  63. glPopMatrix();
  64. glMatrixMode(GL_MODELVIEW);
  65. }
  66.  
  67.  
  68. void Draw()
  69. {
  70.  
  71.     if( keyarr['w'] )
  72.     {
  73.        
  74.     }
  75.    
  76. }
  77.  
  78. #pragma region DrawFPSC
  79. void DrawFPSCT()
  80. {
  81.     glRasterPos2f(1135,16);
  82.     char stringS[300];
  83.     sprintf_s(stringS, "FPS: %d", FPS);
  84.  
  85.     for (const char* ch=stringS; *ch != '\0'; ch++)
  86.     {
  87.         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,*ch);
  88.     }
  89.     if(GetTickCount() -LastFPS >=1000)
  90.     {
  91.         LastFPS = GetTickCount();
  92.         FPS = Frames;
  93.         Frames = 0;
  94.    
  95.     }
  96.     Frames++;
  97. }
  98. #pragma endregion
  99.  
  100. void idle()
  101. {
  102.     glutPostRedisplay();
  103. }
  104. void DrawGUI( )
  105. {  
  106.     DrawFPSCT();
  107. }
  108.  
  109. void RenderAll(void)
  110. {
  111.    
  112.     Draw();
  113.     glClearColor(0.6f,0.6f,0.6f,1);
  114.  
  115.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  116.     glLoadIdentity();
  117.  
  118.     Draw2DSt();
  119.  
  120.     glEnable(GL_BLEND);
  121.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  122.     glEnable(GL_TEXTURE_2D);
  123.     //glBindTexture (GL_TEXTURE_2D,Texture Name);
  124.     //2D sprites
  125. //  glBegin(GL_QUADS);
  126. //  glTexCoord2f(0,1); glVertex2f(3466+0, 460+0);
  127. //  glTexCoord2f(1,1); glVertex2f(3466+362, 460+0);
  128.  //   glTexCoord2f(1,0); glVertex2f(3466+362, 460+362);
  129.   //  glTexCoord2f(0,0); glVertex2f(3466+0, 460+362);
  130. //  glEnd();
  131.    
  132.     glDisable(GL_TEXTURE_2D);
  133.     glDisable(GL_DEPTH_TEST);
  134.  
  135.     DrawGUI();
  136.      Draw2DEnd();
  137.     //3D
  138.     glPushMatrix();
  139.     angle++;
  140.  
  141.     glEnable(GL_BLEND);
  142.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  143.    
  144.     glTranslatef(0,0,-50);//-100
  145.     glRotatef(angle,0,1,0);
  146.     //----------------------- OBJ* readers ar failu.
  147.     RaDOBJ("ak47.obj");
  148.     //-----------------------
  149.     glFlush();
  150.     glPopMatrix();
  151.     glutSwapBuffers();
  152.  
  153.  
  154. }
  155.  
  156. void Res(int w,int h)
  157. {
  158.    
  159.     glMatrixMode(GL_PROJECTION);                       
  160.     glLoadIdentity();  
  161.     gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,1000.0f);
  162.     Wi1 = w;
  163.     He1 = h;
  164.     glMatrixMode(GL_MODELVIEW);            
  165. }
  166.  
  167.  
  168. int main(int argc,char* argv[])
  169. {
  170.  
  171.     glutInit(&argc, argv);
  172.      glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
  173.     glutInitWindowSize(Width,Height);
  174.     glutCreateWindow("3D model test");
  175.     glutKeyboardFunc(key);
  176.     glutKeyboardUpFunc(keyup);
  177.     glutIdleFunc(RenderAll);
  178.     glutDisplayFunc(RenderAll);
  179.     glutPostRedisplay();
  180.     glutReshapeFunc(Res);
  181.    
  182.    
  183.     //GUI_1 = LoadTexture("data/textures/GUI_1.tga");
  184.     glutMainLoop();
  185.     //glDeleteTextures(1,&GUI_1);
  186.  
  187.    
  188.     return 0;
  189.    
  190. }
  191.  
  192. GLuint LoadTexture(char *TexName)
  193.   {
  194.       if(Img.Load(TexName)!=IMG_APC)
  195.       return -1;
  196.  
  197.    
  198.    glGenTextures(1,&Texture);
  199.    glBindTexture(GL_TEXTURE_2D,Texture);
  200.     if(Img.GetBPP()==32)
  201.      glTexImage2D(GL_TEXTURE_2D,0,4,Img.GetWidth(),Img.GetHeight(),0,
  202.      GL_RGBA,GL_UNSIGNED_BYTE,Img.GetImg());
  203.     else
  204.      return -1;
  205.  
  206.    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  207.    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  208.    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
  209.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
  210.  
  211.    return Texture;
  212.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement