Blitz3Dsdk & Dev-C++

Blitz3D SDK Forums/Blitz3D SDK Programming/Blitz3Dsdk & Dev-C++

Can someone please tell me whats wrong with this code?

#include <windows.h>
#include <blitz3Dsdk.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

void DrawWorld(HWND hwnd);
BBEntity camera;
BBEntity light;
BBEntity cube;
bool BBReady = false;
DWORD wRenderTime=0;

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    bbSetBlitz3DHWND((int)hwnd);
    bbBeginBlitz3D();
    bbGraphics3D(GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CYFULLSCREEN), 0, GFX_WINDOWEDSCALED);
    BBReady = true;

    SetTimer(hwnd, 1, 30, NULL);

    camera = bbCreateCamera();
    light = bbCreateLight();
    cube = bbCreateCube();
    bbMoveEntity(camera, 0, 0, -4);
   
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    KillTimer(hwnd, 1);
    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_TIMER:
          InvalidateRect(hwnd, NULL, false);
          break;
        case WM_PAINT:
          DrawWorld(hwnd);         
          break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

void DrawWorld(HWND hwnd)
{
  DWORD t;
  
  if (!hwnd)
    return;
  if (!BBReady)
    return;
  t = GetTickCount();
  if (t - wRenderTime < 10)
    return;
  wRenderTime = t;

  RECT r;
  GetClientRect(hwnd, &r);

  bbCameraViewport(camera, 0, 0, r.right - r.left + 1, r.bottom - r.top + 1);
  bbTurnEntity( cube, .1, .2, .3 );
  bbUpdateWorld();
  bbRenderWorld();
  bbFlip();
}


Edit: Don't check the window creation code. It is generated by the Dev-C++ environment.

You haven't created the camera or the cube entities anywhere.

Check out bbCreateCamera(), bbCreateCube()

[edit]... the light also.

I just forgot to create them. But the problem still remains. When the program starts the window updates its display only if you move it and after a few seconds the application halts...

Can't test it because I don't have the SDK but could it be this?

if (t.wMilliseconds - wRenderTime < 10)
return;

You're using GetSystemTime and using the Millisecs field - range is 0 to 999 (it's the millisecs part of the current time and not the time expressed in millisecs).

Try using GetTickCount() - initialise wRenderTime with GetTickCount() and use ( GetTickCount() - wRenderTime ) as the test.

Besides which, do you really need a timing test, you're already setting the timer up to post a paint message at the interval you want.

Thanks Brendane, but I just checked your suggestions. The problem (application's halt) still remains...

Post your updated code Moraldi, I'll take another look.

I updated in my first post.
Thanks Brendane

You didn't initialise wRenderTime. (zero would be wrong, because your first test does this GetTickCount() - wRenderTime < 10

I can't test this for you, but to me it just looks like this only problem is the fact that the draw call is returning since you're never updating wRenderTime.

Either remove the whole test/return (your timer is being invoked every 30ms, so why do you need this?) or initialise wRenderTime to GetTickCount() just before starting the timer.

Good luck

Ok I found it!
You must make a WINAPI call BeginPaint before and an EndPaint call after DrawWorld function