Advertisement
thecplusplusguy

Box2D tutorial 1 - main program with SDL rendering

Sep 2nd, 2012
3,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //The Box2D main program with 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. SDL_Surface* screen;
  16.  
  17. void putPixel(SDL_Surface* dest,int x,int y,int r,int g,int b)
  18. {
  19. //  std::cout << x << " " << y << std::endl;
  20.     if(x>=0 && x<dest->w && y>=0 && y<dest->h)
  21.         ((Uint32*)dest->pixels)[y*dest->pitch/4+x]=SDL_MapRGB(dest->format,r,g,b);
  22. }
  23.  
  24. void swapValue(int& a,int& b)
  25. {
  26.     int tmp=a;
  27.     a=b;
  28.     b=tmp;
  29. }
  30.  
  31. void drawLine(SDL_Surface* dest,int x0,int y0,int x1,int y1)
  32. {
  33.     int tmp;
  34.     bool step;
  35.    
  36.     step=abs(y1-y0)>abs(x1-x0);
  37.     if(step)
  38.     {
  39.         swapValue(x0,y0);
  40.         swapValue(x1,y1);
  41.     }
  42.    
  43.     if(x0>x1)
  44.     {
  45.         swapValue(x1,x0);
  46.         swapValue(y1,y0);
  47.     }
  48.     float error=0.0;
  49.     float y=y0;
  50.     float roundError=(float)abs(y1-y0)/(x1-x0);
  51.     int ystep=(y1>y0 ? 1 : -1);
  52.     for(int i=x0;i<x1;i++)
  53.     {
  54.         if(step)
  55.             putPixel(dest,y,i,255,255,255);
  56.         else
  57.             putPixel(dest,i,y,255,255,255);
  58.         error+=roundError;
  59.         if(error>=0.5)
  60.         {
  61.             y+=ystep;
  62.             error-=1;
  63.         }
  64.     }
  65. }
  66.  
  67. void rotateTranslate(b2Vec2& vector,const b2Vec2& center,float angle)
  68. {
  69.     b2Vec2 tmp;
  70.     tmp.x=vector.x*cos(angle)-vector.y*sin(angle);
  71.     tmp.y=vector.x*sin(angle)+vector.y*cos(angle);
  72.     vector=tmp+center;
  73. }
  74.  
  75.  
  76. b2Body* addRect(int x,int y,int w,int h,bool dyn=true)
  77. {
  78.     b2BodyDef bodydef;
  79.     bodydef.position.Set(x*P2M,y*P2M);
  80.     if(dyn)
  81.         bodydef.type=b2_dynamicBody;
  82.     b2Body* body=world->CreateBody(&bodydef);
  83.    
  84.     b2PolygonShape shape;
  85.     shape.SetAsBox(P2M*w/2,P2M*h/2);
  86.    
  87.     b2FixtureDef fixturedef;
  88.     fixturedef.shape=&shape;
  89.     fixturedef.density=1.0;
  90.     body->CreateFixture(&fixturedef);
  91.    
  92. }
  93.  
  94. void drawSquare(b2Vec2* points,b2Vec2 center,float angle)
  95. {
  96.     for(int i=0;i<4;i++)
  97.         drawLine(screen,points[i].x*M2P,points[i].y*M2P,points[(i+1)>3 ? 0 : (i+1)].x*M2P,points[(i+1)>3 ? 0 : (i+1)].y*M2P);
  98. }
  99.  
  100.  
  101. void init()
  102. {
  103.     world=new b2World(b2Vec2(0.0,9.81));
  104.     addRect(WIDTH/2,HEIGHT-50,WIDTH,30,false);
  105. }
  106.  
  107. void display()
  108. {
  109.     SDL_FillRect(screen,NULL,0);
  110.     b2Body* tmp=world->GetBodyList();
  111.     b2Vec2 points[4];
  112.     while(tmp)
  113.     {
  114.         for(int i=0;i<4;i++)
  115.         {
  116.             points[i]=((b2PolygonShape*)tmp->GetFixtureList()->GetShape())->GetVertex(i);
  117.             rotateTranslate(points[i],tmp->GetWorldCenter(),tmp->GetAngle());
  118.         }
  119.         drawSquare(points,tmp->GetWorldCenter(),tmp->GetAngle());
  120.         tmp=tmp->GetNext();
  121.     }
  122. }
  123.  
  124. int main(int argc,char** argv)
  125. {
  126.     SDL_Init(SDL_INIT_EVERYTHING);
  127.     screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  128.     Uint32 start;
  129.     SDL_Event event;
  130.     bool running=true;
  131.     init();
  132.     while(running)
  133.     {
  134.         start=SDL_GetTicks();
  135.         while(SDL_PollEvent(&event))
  136.         {
  137.             switch(event.type)
  138.             {
  139.                 case SDL_QUIT:
  140.                     running=false;
  141.                     break;
  142.                 case SDL_KEYDOWN:
  143.                     switch(event.key.keysym.sym)
  144.                     {
  145.                         case SDLK_ESCAPE:
  146.                             running=false;
  147.                             break;
  148.                     }
  149.                     break;
  150.                 case SDL_MOUSEBUTTONDOWN:
  151.                     addRect(event.button.x,event.button.y,20,20,true);
  152.                     break;
  153.                    
  154.             }
  155.         }
  156.         display();
  157.         world->Step(1.0/30.0,8,3);  //update
  158.         SDL_Flip(screen);
  159.         if(1000.0/30>SDL_GetTicks()-start)
  160.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  161.     }
  162.     SDL_Quit();
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement