Advertisement
thecplusplusguy

Box2D tutorial 1 - main program with OpenGL rendering

Sep 2nd, 2012
4,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //The Box2D main program with OpenGL and SDL
  3. #include <iostream>
  4. #include <SDL/SDL.h>
  5. #include <SDL/SDL_image.h>
  6. #include <GL/gl.h>
  7. #include <GL/glu.h>
  8. #include <Box2D/Box2D.h>
  9.  
  10. const int WIDTH=640;
  11. const int HEIGHT=480;
  12. const float M2P=20;
  13. const float P2M=1/M2P;
  14. b2World* world;
  15.  
  16.  
  17.  
  18.  
  19. b2Body* addRect(int x,int y,int w,int h,bool dyn=true)
  20. {
  21.     b2BodyDef bodydef;
  22.     bodydef.position.Set(x*P2M,y*P2M);
  23.     if(dyn)
  24.         bodydef.type=b2_dynamicBody;
  25.     b2Body* body=world->CreateBody(&bodydef);
  26.    
  27.     b2PolygonShape shape;
  28.     shape.SetAsBox(P2M*w/2,P2M*h/2);
  29.    
  30.     b2FixtureDef fixturedef;
  31.     fixturedef.shape=&shape;
  32.     fixturedef.density=1.0;
  33.     body->CreateFixture(&fixturedef);
  34.    
  35. }
  36.  
  37. void drawSquare(b2Vec2* points,b2Vec2 center,float angle)
  38. {
  39.     glColor3f(1,1,1);
  40.     glPushMatrix();
  41.         glTranslatef(center.x*M2P,center.y*M2P,0);
  42.         glRotatef(angle*180.0/M_PI,0,0,1);
  43.         glBegin(GL_QUADS);
  44.             for(int i=0;i<4;i++)
  45.                 glVertex2f(points[i].x*M2P,points[i].y*M2P);
  46.         glEnd();
  47.     glPopMatrix();
  48. }
  49.  
  50. void init()
  51. {
  52.     glMatrixMode(GL_PROJECTION);
  53.         glOrtho(0,WIDTH,HEIGHT,0,-1,1);
  54.     glMatrixMode(GL_MODELVIEW);
  55.     glClearColor(0,0,0,1);
  56.     world=new b2World(b2Vec2(0.0,9.81));
  57.     addRect(WIDTH/2,HEIGHT-50,WIDTH,30,false);
  58. }
  59.  
  60. void display()
  61. {
  62.     glClear(GL_COLOR_BUFFER_BIT);
  63.     glLoadIdentity();
  64.     b2Body* tmp=world->GetBodyList();
  65.     b2Vec2 points[4];
  66.     while(tmp)
  67.     {
  68.         for(int i=0;i<4;i++)
  69.             points[i]=((b2PolygonShape*)tmp->GetFixtureList()->GetShape())->GetVertex(i);
  70.    
  71.         drawSquare(points,tmp->GetWorldCenter(),tmp->GetAngle());
  72.         tmp=tmp->GetNext();
  73.     }
  74. }
  75.  
  76. int main(int argc,char** argv)
  77. {
  78.     SDL_Init(SDL_INIT_EVERYTHING);
  79.     SDL_SetVideoMode(640,480,32,SDL_OPENGL);
  80.     Uint32 start;
  81.     SDL_Event event;
  82.     bool running=true;
  83.     init();
  84.     while(running)
  85.     {
  86.         start=SDL_GetTicks();
  87.         while(SDL_PollEvent(&event))
  88.         {
  89.             switch(event.type)
  90.             {
  91.                 case SDL_QUIT:
  92.                     running=false;
  93.                     break;
  94.                 case SDL_KEYDOWN:
  95.                     switch(event.key.keysym.sym)
  96.                     {
  97.                         case SDLK_ESCAPE:
  98.                             running=false;
  99.                             break;
  100.                     }
  101.                     break;
  102.                 case SDL_MOUSEBUTTONDOWN:
  103.                     addRect(event.button.x,event.button.y,20,20,true);
  104.                     break;
  105.                    
  106.             }
  107.         }
  108.         display();
  109.         world->Step(1.0/30.0,8,3);  //update
  110.         SDL_GL_SwapBuffers();
  111.         if(1000.0/30>SDL_GetTicks()-start)
  112.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  113.     }
  114.     SDL_Quit();
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement