• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

how to make d3d9 work with a window which is created and run in other thread

5 posts in this topic

in the normal d3d application, when we resize the window or drag the window, it will block the main thread.

if it is a online-game, it will block the logic.

so i want to find a method to resolve the problem.

in my option, multi thread is a proper resolution,

so i create the window( CreateWindowEx ) in the special thread, and call PeekMessage in that thread, like this:

UINT GameWindow::Exec()

            HINSTANCE hInst = 0;

            hInst = (HINSTANCE)::GetModuleHandle(NULL);

            // register window
            WNDCLASSEX wc = {0};

            wc.cbSize        = sizeof(WNDCLASSEX);
            wc.style         = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
            wc.lpfnWndProc   = StaticClientWndProc;
            wc.cbClsExtra    = 0;
            wc.cbWndExtra    = 0;
            wc.hInstance     = hInst;
            wc.hIcon         = LoadIcon(0, IDI_WINLOGO);
            wc.hCursor       = LoadCursor(0, IDC_ARROW);
            //wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
            wc.hbrBackground = NULL;
            wc.lpszMenuName  = 0;
            wc.lpszClassName = L"xxxxxMainWindow";

            if( !RegisterClassExW(&wc) )
                LogError( _T("Can't Register Window Class:") _T("HikariMainWindow") );
                LogError( _T("Last Error:"), System::Error::GetErrorDesc() );

                return false;

            DWORD WindowStyle;

            WindowStyle = WS_OVERLAPPEDWINDOW;

            // Create Window
            m_WndID = CreateWindowExW(
                CW_USEDEFAULT, CW_USEDEFAULT, m_SizeX, m_SizeY,
                NULL );
            // ...... 	


            MSG msg = {0};

                while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
            }while( !IsRequestExit() );            

            return 0;

so the WndProc will called in this thread, in this function, i send all the parameters to main thread.

and in main thread, i open the message queue, process all the messages.

        LRESULT GameWindow::StaticClientWndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
            X_ASSERT( g_WindowsMessageDisptacher );

                HWND, hWnd, hwnd,
                UINT, message, msg,
                WPARAM, w, wParam,
                LPARAM, h, lParam

            return ::DefWindowProc( hwnd, msg, wParam, lParam );

  in the main thread.

        void GameWindow::ProcessGameWindowEvent( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
            switch( msg )
            case WM_DESTROY:
            case WM_CLOSE:
                m_bRequestExit = true;
            case WM_SIZE:
                if( g_GameEngine && wParam == SIZE_RESTORED )
                   // g_GameEngine->OnGameWindowResize( LOWORD(lParam), HIWORD(lParam) );


     it works well if i just drag the window, the main thread and its main loop is not blocked.


    the question is when i resize the window, there is a render error when i call 



   Direct3D9: (ERROR) :BitBlt or StretchBlt failed in Present


   if i ignore this error, it will work well in next frame.   


   so i want to know how to avoid this error when i create window in individual thread.


   is there any articles or topics?laugh.png


   my render work is also doing in the individual thread.rolleyes.gif


Share this post

Link to post
Share on other sites

According to the documentation, all D3D9 API calls should be made on the same thread where your window is created/updated unsure.png

if i create the window and process windows message in the render thread, it will block  rendering too, and then ,render thread will block the main thread, because main thread will wait render thread every frame.


Share this post

Link to post
Share on other sites
Since the problem is that the game logic is blocked, why don't you run that in a separate thread and let d3d happily work on the primary thread?

Share this post

Link to post
Share on other sites

As just mentioned, you have no choice but to keep DirectX and the window on the same thread.

You are tackling the problem the wrong way.

If the logic is being blocked, put the logic on a second thread.

And it’s not really a problem anyway.  If you use fixed-step updates the worst that can happen is you lag for a bit in the online game.  This is a classic situation.  Hell, I used to resize my window on purpose in Starsiege: Tribes to make people think I was lagging out.


So I really don’t see what the problem is.



L. Spiro


Share this post

Link to post
Share on other sites
Catch the window events WM_ENTERSIZEMOVE and WM_ENTERMENULOOP to detect when user is dragging the window

In that window event, create a new Timer (SetTimer) for 100ms

then, catch WM_TIMER message, and in there, call your game loop

Delete the timer (KillTimer) on WM_EXITSIZEMOVE or WM_EXITMENULOOP

This will solve your problem

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0