How would I include Box2D in my app?

Hi,

Basically my app works like this:

Timer every 33 miliseconds -> Re-render

I want this to become

Timer -> Calculate physics -> Re-render

My rendering system looks like this:

main.cpp:

Code:
case WM_PAINT:
render(hwnd);
break;

case WM_TIMER:
//Force Windows to redraw the window
InvalidateRect(hwnd, NULL, TRUE);
break;


The "render" function is in a seperate source file render.cpp:

Code:
int render(HWND hwnd)
{
   //Variables
   PAINTSTRUCT psInfo;
   
   //Prepare painting
   HDC hdc = BeginPaint(hwnd, &psInfo);

   //Create surfaces
   if (doneloading == false)
   {
      bmBackground = CreateCompatibleDC(hdc);
      hbmOld = (HBITMAP)CreateCompatibleBitmap(hdc, 1000, 700);
      SelectObject ( bmBackground, hbmOld );
      
      doneloading = true;
   }

   //Draw background
   RECT rcBackground;
   rcBackground.left = 0;
   rcBackground.top = 0;
   rcBackground.right = 1000;
   rcBackground.bottom = 700;
   FillRect(bmBackground, &rcBackground, CreateSolidBrush(RGB(255, 255, 255)));

   //Draw physical objects
   

   //Flip buffers
   BitBlt(hdc, 0, 0, 1000, 700, bmBackground, 0, 0, SRCCOPY);

   //Stop painting
   EndPaint(hwnd, &psInfo);
   
   return 1;
}


Now, I've added a new file called physics.cpp. However, I already run into problems when creating the world. This is how it looks now:

Code:
//Box2D Physics engine
#include "Box2D.h"

//Variables
b2AABB worldAABB;

void pInitialize()
{
   //Create world
   worldAABB.lowerBound.Set(-500.0f, -350.0f);
   worldAABB.upperBound.Set(500.0f, 350.0f);
   
   b2Vec2 gravity(0.0f, -10.0f);
   bool doSleep = true;

   b2World world(worldAABB, gravity, doSleep);
}

void pStep()
{

}


This however poses the problem that the world stops existing after the initialize function is done. I don't see a way to globally define world, so that it actually keeps existing.
How would I do that? Or should I implent the physics in a whole other way anyway?
Quick mockup:
Code:
//Box2D Physics engine
#include "Box2D.h"

//Variables
b2AABB worldAABB;
b2World* g_physWorld=null; // Global pointer to a world object

void pInitialize()
{
   //Create world
   worldAABB.lowerBound.Set(-500.0f, -350.0f);
   worldAABB.upperBound.Set(500.0f, 350.0f);
   
   b2Vec2 gravity(0.0f, -10.0f);
   bool doSleep = true;

   // Use new to dynamically allocate the world, remember to delete it when shutting down!
   g_physWorld = new b2World(worldAABB, gravity, doSleep);
}

void pStep()
{
if(g_physWorld)
{
  g_physWorld->step(); // Pseudocode to step the world.
}
}

void pStopPhys()
{
  if(g_physWorld)
  {
    delete g_physWorld; // Or we've got a memory leak...
  }
}

Original thread: https://box2d.org/forum/viewtopic.php?t=1503