loading images to the backbuffer / direct3d / c++

Abdulali
  • loading images to the backbuffer / direct3d / c++ Abdulali

    i think i just found the solution. 1) the problem is that backbuffer surface and source surface are of different formats - that is why exception was thrown. 2) the image path needed double slash "C\\......." ;)

    ================================= I have been googling for a while but still cant come up with a solution to the problem - D3DXGetImageInfoFromFile function just doesn't load any images.

    it compiles perfectly well, but throws an exception when it comes to the D3DXGetImageInfoFromFile function.

    Here is the complete listing:

    //================================================================================================================================// including stuff //=================//
    
    #include <windows.h>
    #include <string>
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <d3dx9tex.h>
    
    //================================================================================================================================// handling windows //================//
    
    HINSTANCE hInst;
    HWND wndHandle;
    bool stopEverything(false);
    
    //================================================================================================================================// handling directx needs //==========//
    
    LPDIRECT3D9                 pD3D;                       // the Direct3D object
    LPDIRECT3DDEVICE9           pd3dDevice;                 // the Direct3D device
    IDirect3DSurface9           *Surface = NULL;            // image surface
    IDirect3DSurface9           *BackBuffer = NULL;         // back buffer surface
    
    //================================================================================================================================// declaring functions //=============//
    
    bool initWindow(HINSTANCE hInstance);
    bool initDirect3D(void);
    void cleanUp (void);
    void render(void);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    //================================================================================================================================// code starts here //================//
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        if (!initWindow(hInstance)) return false;
    
        if (!initDirect3D())
        {
            return false;
        }
    
        MSG msg;
        ZeroMemory(&msg, sizeof(msg));
    
        while(msg.message!=WM_QUIT){
            if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
             {
                 TranslateMessage (&msg);
                 DispatchMessage (&msg);
             } else {
                 render();
             }
        }
    
        return static_cast<int>(msg.wParam);
    }
    
    bool initWindow(HINSTANCE hInstance )
    {
        WNDCLASSEX wcex;
    
        wcex.cbSize             = sizeof(WNDCLASSEX);
        wcex.style              = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc        = WndProc;
        wcex.cbClsExtra         = 0;
        wcex.cbWndExtra         = 0;
        wcex.hInstance          = hInstance;
        wcex.hIcon              = LoadIcon(0, IDI_APPLICATION);
        wcex.hCursor            = LoadCursor(0, IDC_ARROW);
        wcex.hbrBackground      = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
        wcex.lpszMenuName       = 0L;
        wcex.lpszClassName      = L"DirectXTemplate";
        wcex.hIconSm            = 0;
    
        RegisterClassEx(&wcex);
    
        wndHandle = CreateWindow(L"DirectXTemplate", L"DirectX Template", WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
    
        if (!wndHandle) return false;
    
        ShowWindow(wndHandle, SW_SHOW);
        UpdateWindow(wndHandle);
    
        return true;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
    
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
        }   
    }
    
    bool initDirect3D(void)
    {
        pD3D = NULL;
        pd3dDevice = NULL;
    
        // create the DirectX object
        if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) return false;
    
        std::wstring wsPath = L"C:\wood.bmp"; // path to the image
        D3DXIMAGE_INFO Info;
    
        if (FAILED(D3DXGetImageInfoFromFile(wsPath.c_str(), &Info)))
        {
            MessageBox(NULL, L"You're a terrible programmer!", L"ERROR", MB_OK);
            stopEverything = true;
        } else {
            pd3dDevice -> CreateOffscreenPlainSurface(Info.Width, Info.Height, Info.Format, D3DPOOL_SYSTEMMEM, &Surface, NULL);
            D3DXLoadSurfaceFromFile(Surface, NULL, NULL, wsPath.c_str(), NULL, D3DX_FILTER_NONE, 0, NULL);
            pd3dDevice -> GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &BackBuffer);
        }
    
        // fill the presentation parameters structure
        D3DPRESENT_PARAMETERS d3dpp;
        ZeroMemory(&d3dpp, sizeof(d3dpp));
    
        d3dpp.Windowed          = TRUE;
        d3dpp.SwapEffect        = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat  = D3DFMT_X8R8G8B8;
        d3dpp.BackBufferCount   = 1;
        d3dpp.BackBufferHeight  = 480;
        d3dpp.BackBufferWidth   = 640;
        d3dpp.hDeviceWindow     = wndHandle;
    
        // create a default DirectX device
        if (FAILED(pD3D -> CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice))) return false;
    
        return true;
    }
    
    void render(void)
    {
        // check to make sure you have a valid Direct3D device
        if (NULL == pd3dDevice) return;
    
        pd3dDevice -> Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);   // clear the back buffer to a blue color
    
        if (SUCCEEDED(pd3dDevice -> BeginScene()))
        {
            if (stopEverything == false)
            {
                pd3dDevice -> UpdateSurface(Surface, NULL, BackBuffer, NULL);
            }
    
            // end the scene
            pd3dDevice -> EndScene();
        }
    
        // present the back buffer contents to the display
        pd3dDevice -> Present(NULL, NULL, NULL, NULL);
    }
    
    void cleanUp (void)
    {
        // release the device and the Direct3D object
        if (pd3dDevice != NULL) pd3dDevice -> Release();
        if (pD3D != NULL) pD3D -> Release();
    }
    

  • Your D3D device needs to exist before you can load the image and create the surface.

Tags
c++ directx
Related questions and answers
  • (pBackBuffer, NULL, &amp;rtv); pBackBuffer-&gt;Release(); // set the back buffer as the render target device-&gt;OMSetRenderTargets(1, &amp;rtv, dsv); D3D10_VIEWPORT viewport; // create a struct to hold...; bd.ByteWidth = sizeof(DWORD) * 36; bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; device-&gt;CreateBuffer(&amp;bd, NULL, &amp;iBuffer); // void... buffer ID3D10Texture2D* pDepthBuffer; device-&gt;CreateTexture2D(&amp;zbd, NULL, &amp;pDepthBuffer); // create the texture // create the depth buffer D3D10_DEPTH_STENCIL_VIEW_DESC dsvd; ZeroMemory

  • (); missile-&gt;Init(true, s-&gt;X, s-&gt;Y); bullets-&gt;push_back(missile); } } void CleanUp() { for(unsigned int index = 0; index &lt; bullets-&gt;size(); index... it returned: %i", rval); return 1; } allegro_message("Setting Graphics Mode...Press X To Begin Game. Default Missile count: %i", bullets-&gt;size()); set_color_depth(32); int ret...!"); return 0; } while(!done) { sceCtrlReadBufferPositive(&amp;pad, 1); if (pad.Buttons &amp; PSP_CTRL_START) { done = true; } else

  • --; cspace++;}} for(int s= 0; s &lt;sspace; s++) buff[buff.size()-1].push_back(' '); // cwall2 //if(cwall2!= 0){ // if(cwall2&gt;0... set_location(int X, int Y) { location[0] = X; location[1] = Y;}; bool Alive() {return alive;}; void SetLife(bool f) {alive= f;}; //////////////////////////////// Move //////////// bool move(int X, int Y) { location[0] += X; location[1] += Y; return true;}; };// end of sprite

  • new Object(); in the bottom part of the code sample. static BaseObject * CreateObjectFunc() { return new Object(); } How and where to delete the pointers? I wrote bool ReleaseClassType(). Despite...;CreateObjectFunc<Object>; return true; } // create new object by calling the pointer to the appropriate type function BaseObject* create(unsigned int nObjectIDParam) const..." #include &lt;vector> template &lt;typename BaseObject> class CGameObjectFactory { public: // cleanup and release registered object data types bool ReleaseClassTypes

  • in the shader code) for( int n = 0; n &lt; vShaderArgs.size(); n ++) glBindAttribLocation( m_nProgramId, n, vShaderArgs[n].sFieldName.c_str() ); // Create and bind to a vertex array object, which... ); glDisableClientState( GL_COLOR_ARRAY ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); } Back to the Vertex Array Object though. My code for creating the Vertex... for( int n = 0; n &lt; vShaderArgs.size(); n ++ ) { glEnableVertexAttribArray(n); glVertexAttribPointer( n, vShaderArgs[n].nFieldSize, GL_FLOAT, GL_FALSE

  • ; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; device-&gt;CreateBuffer(&amp;bd, NULL, &amp;pBuffer); void* pVoid; pBuffer-&gt;Map(D3D10_MAP_WRITE_DISCARD, 0, &amp;pVoid); //HERE memcpy(pVoid...(D3D10_PRIMITIVE_TOPOLOGY_LINELIST); UINT stride = sizeof(VERTEX); UINT offset = 0; device-&gt;IASetVertexBuffers(0, 1, &amp;pBuffer, &amp;stride, &amp;offset); screenPass-&gt;Apply(0... have marked with //THIS LINE, the code works fine.) when I run the code below the line breaks //HERE. Access violation reading location 0x00000000. seems like the create buffer line is not working

  • ::Vector3(0,1,0); p.d = 0; Ogre::MeshManager::getSingleton().createPlane( "FloorPlane", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, p, 200000, 200000, 20, 20, true, 1, 9000,9000,Ogre... *) (node)); // Add it to the physics world World-&gt;addRigidBody(RigidBody); Objects.push_back(RigidBody); m_pNumEntities++; // End Physics } I then have a method to create a cube and give it rigid body... every frame to handle input and render etc I call an UpdatePhysics method to update the physics simulation. void GameState::UpdatePhysics(unsigned int TDeltaTime) { World-&gt;stepSimulation(TDeltaTime

  • _Commands, m_MD2Header.num_glcommands * sizeof(int)); // Read all the data. for(int i = 0; i &lt; m_MD2Header.num_frames; ++i) { md2_frame_t* Frame = (md2_frame_t*)&amp;Buffer..._Commands; while(int i = *(Commands++)) { if(i &lt; 0) { glBegin(GL_TRIANGLE_FAN); i = -i; } else...; int* m_Normals; unsigned int m_TextureID; float m_Scale; void Interpolate(vec3_t* Vertices); }; #include &lt;helpers/md2.h&gt; #include &lt;iostream> #include &lt

  • ); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glLoadIdentity(); ...here is my image loading function... SDL_Surface *loadImage (const char *filename, bool alpha = true) { SDL_Surface... = SDL_DisplayFormat(zoomedImage); SDL_FreeSurface(zoomedImage); } return optimizedImage; } ...here is the code I actually use to create the GL texture... GLuint thisIsMyTexture; glGenTextures(1, &amp;thisIsMyTexture); temporarySurface = loadImage("theimagetomytexture.png"); glBindTexture(GL_TEXTURE_2D,thisIsMyTexture); glTexImage2D(GL_TEXTURE_2D, 0, 4, temporarySurface-&gt;w

Data information