SDL & Direct3D - Event loop clogged up?

This is a discussion on SDL & Direct3D - Event loop clogged up? within the Game Programming forums, part of the General Programming Boards category; Well at least by using SDL I don't drown in that God-forbidden Win32 code. Now I can concentrate on learning ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    SDL & Direct3D - Event loop clogged up?

    Well at least by using SDL I don't drown in that God-forbidden Win32 code. Now I can concentrate on learning Direct3D nicely.

    Anyway I've got a problem. Everything compiles fine and the pretty triangle gets drawn, only the window doesn't respond to events. But it sort of does at the same time.

    I added some cout's so I can say "DEBUG: GOT QUIT MESSAGE" and what have you; when I press escape that's printed in the console window but the main window remains open.

    Code:
    #include "stdafx.h"
    using namespace std;
    
    struct D3DVertex
    {
        float x, y, z;
        DWORD dwColor;
    } *LPD3DVertex;
    
    IDirect3D9 *g_pD3D = NULL;
    IDirect3DDevice9 *g_pD3DDevice = NULL;
    IDirect3DVertexBuffer9 *pVertexBuffer = NULL;
    
    HRESULT Initialise(int nWidth, int nHeight);
    void Cleanup();
    void RenderFrame();
    
    int main(int argc, char *argv[])
    {
        SDL_Event sdleEvent;
    
        if (FAILED(Initialise(640, 480)))
        {
            cout << "Error initialising" << endl;
            Cleanup();
            return 1;
        }
    
        // Main loop
        while (1)
        {
            RenderFrame();
    
            while (SDL_PollEvent(&sdleEvent))
            {
                switch (sdleEvent.type)
                {
                case SDL_KEYUP:
                    if (sdleEvent.key.keysym.sym == SDLK_ESCAPE)
                    {
                        cout << "DEBUG: GOT ESCAPE MESSAGE\n";
                        break;
                    }
    
                case SDL_QUIT:
                    cout << "DEBUG: GOT QUIT MESSAGE\n";
                    break;
                }
            }
        }
    
        Cleanup();
        return 0;
    }
    
    // Initialise
    //
    // Returns: S_OK if initialisation ok, E_FAIL otherwise
    //
    HRESULT Initialise(int nWidth, int nHeight)
    {
        D3DPRESENT_PARAMETERS d3dpp;
        D3DXMATRIX d3dxm;
        VOID *pData = NULL;
        D3DVertex vTriangle[] = {
            {-2.0f, 1.0f,  10.0f, 0xffff0000},
            {-3.0f, -1.0f, 10.0f, 0xff00ff00},
            {-1.0f, -1.0f, 10.0f, 0xff0000ff}
        };
    
        // SDL Init
        if (SDL_Init(SDL_INIT_VIDEO) < 0 || !SDL_GetVideoInfo())
            return E_FAIL;
    
        SDL_SetVideoMode(nWidth, nHeight, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_RESIZABLE);
    
        // Direct3D Init
        g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
        if (g_pD3D == NULL) return E_FAIL;
    
        ZeroMemory(&d3dpp, sizeof(d3dpp));
        d3dpp.Windowed = true;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.EnableAutoDepthStencil = true;
        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
        d3dpp.hDeviceWindow = GetActiveWindow();
        d3dpp.BackBufferWidth = nWidth;
        d3dpp.BackBufferHeight = nHeight;
        d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
        d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
    
        if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetActiveWindow(),
                                        D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
        {
            return E_FAIL;
        }
    
        D3DXMatrixPerspectiveFovLH(&d3dxm, D3DX_PI / 4.0f, (float) nWidth / nHeight, 1, 1000);
        g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &d3dxm);
        g_pD3DDevice->SetRenderState(D3DRS_AMBIENT, RGB(255, 255, 255));
        g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, false);
        g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
        g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, true);
        g_pD3DDevice->SetFVF((D3DFVF_XYZ | D3DFVF_DIFFUSE));
    
        // Create vertex buffer
        g_pD3DDevice->CreateVertexBuffer(sizeof(vTriangle), D3DUSAGE_WRITEONLY,
                                        (D3DFVF_XYZ | D3DFVF_DIFFUSE),
                                        D3DPOOL_MANAGED, &pVertexBuffer, NULL);
    
        // Set pData to point to the vertex buffer and use it to copy
        // the vertex data into it
        pVertexBuffer->Lock(0, sizeof(pData), (void **) &pData, 0);
        memcpy(pData, vTriangle, sizeof(vTriangle));
        pVertexBuffer->Unlock();
    
        return S_OK;
    }
    
    // Cleanup
    //
    void Cleanup()
    {
        if (g_pD3DDevice)
        {
            g_pD3DDevice->Release();
            g_pD3DDevice = NULL;
        }
    
        if (g_pD3D)
        {
            g_pD3D->Release();
            g_pD3D = NULL;
        }
    }
    
    // RenderFrame
    //
    void RenderFrame()
    {
        g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                            D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
        g_pD3DDevice->BeginScene();
    
        g_pD3DDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(D3DVertex));
        g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
    
        g_pD3DDevice->EndScene();
        g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
    }
    Once I get it accepting SDL input I'll be building in DirectInput.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I realy don't recommend you try to use direct3d and SDL in the same program. SDL is intended to be used instead of lower level APIs like D3D or OpenGL.

    I don't know exacly what's going on behind the scenes to stop messages being processed properly but it's likely a conflict between the two, actually I'm suprised it works at all.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Quote Originally Posted by Quantum1024
    I realy don't recommend you try to use direct3d and SDL in the same program. SDL is intended to be used instead of lower level APIs like D3D or OpenGL.

    I don't know exacly what's going on behind the scenes to stop messages being processed properly but it's likely a conflict between the two, actually I'm suprised it works at all.
    SDL can also be used as a layer for writing opengl and d3d code without the hassle of creating windows or handling input and sound.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Brian
    SDL can also be used as a layer for writing opengl and d3d code without the hassle of creating windows or handling input and sound.
    I didn't know you could use it like that. I still don't think there's much point. There's not much to creating a window and a message loop and it's better to use DirectInput and DirectSound.

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Yeah I guess it is a little bit silly but I'm more comfortable with SDL and it's quicker to write what you need for windowing compared to using win32 API calls.

    Obviously I wouldn't use it for any larger project, just while I'm learning D3D.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by Quantum1024
    I realy don't recommend you try to use direct3d and SDL in the same program. SDL is intended to be used instead of lower level APIs like D3D or OpenGL.
    That's like saying you shouldn't use DirectX with .NET. If OpenGL wasn't meant to be used with SDL there wouldn't be the ability. On the other hand it isn't designed to be used with DirectX like that - no telling what might happen.

    Might want to try writing a wrapper around Win32 - like GLUT (DXUT?).
    Last edited by Frobozz; 04-11-2006 at 02:01 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Frobozz
    That's like saying you shouldn't use DirectX with .NET.
    That's not realy a fair comparison. A better one would be managed c++ with c++. [/quote]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM
  2. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 09:15 AM
  3. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 05:13 AM
  4. Replies: 1
    Last Post: 11-19-2001, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21