Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7197131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:54:47+00:00 2026-05-28T20:54:47+00:00

I am currently learning DirectX so I am a starter and I am stuck

  • 0

I am currently learning DirectX so I am a starter and I am stuck at one code. I am studying from a book and I have written this code. It should draw a bitmap on the window but its giving me a blank screen. Moreover when I click esc button it gives an error but if I move or stretch the window before pressing esc, it doesnt give an error. Any help appreciated. I am using Visual Studio 2010 and C++. I have one assumption that the error might be at D3DXCreateSurfaceFromFile. Here is the code;

//Header files to include
#include <d3d9.h>
#include <time.h>
#include <d3dx9.h>

//Application title
#define APPTITLE L"Load_Bitmap"

//Screen Resolution
#define WIDTH 640
#define HEIGHT 480

//Forward Declarations
LRESULT WINAPI WinProc( HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass( HINSTANCE);
int GameInit(HWND);
void GameRun(HWND);
void GameEnd(HWND);

//Direct3d objects
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
LPDIRECT3DSURFACE9 backbuffer = NULL;
LPDIRECT3DSURFACE9  surface = NULL;

//Macros to read the keyboard asynchronously
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)


//Window Event Callback Function
LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
            GameEnd( hWnd);
            PostQuitMessage(0);
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam);
}

//Helper function to set up the window properties
ATOM MyRegisterClass( HINSTANCE hInstance)
{
    WNDCLASSEX wc;

    wc.cbSize = sizeof( WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WinProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = APPTITLE;
    wc.hIconSm = NULL;

    //Set up the window with the class info
    return RegisterClassEx(&wc);



}

//Entry point for a windows program
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstancem, LPSTR lpCmdLine, int nCmdShow)
{
    //Declare variables
    MSG msg;

    //Register the class
    MyRegisterClass( hInstance);

    //Initialize Application
    HWND hWnd;

    //Create new Window
    hWnd = CreateWindow( APPTITLE, APPTITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);

    if( !hWnd)
        return FALSE;

    //Display the Window
    ShowWindow( hWnd, nCmdShow);
    UpdateWindow( hWnd);

    //Initialize the Game
    if( !GameInit( hWnd))
        return FALSE;

    //Main Message Loop
    int done = 0;
    while(!done)
    {
        if(PeekMessage( &msg, hWnd, 0, 0, PM_REMOVE))
        {
            //Look for quit message
            if( msg.message == WM_QUIT)
                done = 1;

            //Decode and pass messages on to WndProc
            TranslateMessage( &msg);
            DispatchMessage( &msg);
        }
        else
            //Process game loop( else prevents running after window is closed)
            GameRun(hWnd);
    }

    return msg.wParam;
}

int GameInit( HWND hWnd)
{
    HRESULT result;

    //Initialize Direct3d
    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    if( d3d == NULL)
    {
        MessageBox( hWnd, L"Error initializing Direct3d", L"Error", MB_OK);
        return 0;
    }

    //Set Direct3D presentation parameters
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp));

    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferWidth = WIDTH;
    d3dpp.BackBufferHeight = HEIGHT;
    d3dpp.hDeviceWindow = hWnd;

    //Create Direct3D device
    d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

    if( d3ddev == NULL)
    {
        MessageBox( hWnd, L"Error creating Direct3d device", L"Error", MB_OK);
        return 0;
    }

    //Set Random number seed
    //srand( time(NULL));

    //Clear the backbuffer to black
    d3ddev->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0,0,0), 1.0f, 0);

    //Create pointer to the back buffer
    d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);

    //Create surface
    result = d3ddev->CreateOffscreenPlainSurface( 640, 480, D3DFMT_X8R8G8B8, 
                                                D3DPOOL_DEFAULT, &surface, NULL);

    if( result != D3D_OK)
        return 1;

    //load surface from file
    result = D3DXLoadSurfaceFromFile(
        surface, NULL, NULL, L"c.bmp", NULL, D3DX_DEFAULT, 0, NULL);

    //Make sure file was loaded okay
    if( result != D3D_OK)
        return 1;

    d3ddev->StretchRect( surface, NULL, backbuffer, NULL, D3DTEXF_NONE);

    //Return okay
    return 1;
}

void GameRun(HWND hWnd)
{

    //Make Sure the Direct3d device is valid
    if( d3ddev == NULL)
        return;

    //Start Rendering
    if( d3ddev->BeginScene())
    {
        //Create pointer to the back buffer
        d3ddev->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);

        //Draw surface to the backbuffer
        d3ddev->StretchRect( surface, NULL, backbuffer, NULL, D3DTEXF_NONE);
        //StopRendering
        d3ddev->EndScene();
    }

    //Display the back buffer on the screen
    d3ddev->Present( NULL, NULL, NULL, NULL);

    //Check for escape key( to exit program)
    if( KEY_DOWN(VK_ESCAPE))
        PostMessage(hWnd, WM_DESTROY, 0, 0);
}

void GameEnd(HWND hWnd)
{
    //free the surface
    if( surface != NULL)
        surface->Release();

    //Release the Direct3D device
    if( d3ddev != NULL)
        d3ddev->Release();

    if( d3d != NULL)
        d3d->Release();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:54:47+00:00Added an answer on May 28, 2026 at 8:54 pm

    Post WM_QUIT instead of WM_DESTROY when you check the escape key. As it stands now the message-loop will never quit since it depends on WM_QUIT being posted, and it will keep calling GameRun even after the surfaces are deleted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning algorithms from a book I have bought from Amazon but it
Currently learning Scheme/Racket and have problem running this piece of code. (if (or (<
I am currently learning regex but need this quite urgently. I have a set
I'm currently learning scala. Why this code doesn't work: class GenClass[T](var d : T)
Im currently learning c++ from a book called 'Ivor Hortons Beginning Visual c++ 2010'.
I'm currently learning after a book about how to convert xaml code into objects
im currently learning python (in the very begining), so I still have some doubts
im currently learning stacks in java and have a quick question. what will the
I'm currently learning Ruby and RoR and I stumbled across this declaration: link_to_remote(name, options
I am currently learning Perl. I have Perl hash that contains references to hashes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.