Thread: Directx 9 2D

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Directx 9 2D

    I'm trying to draw some 2D images in Microsoft Visual Studio 2005 c++ using DirectX. So far I'm doing ok, but I cant quite make the pixels on the images as clear as they are in the actual image files. I would like them to be the same pixel per pixel.


    I've been trying 2 different methods, one using the 3D environment and using a setup to look at the image as if it was on a 2D screen.

    And another where I just use absolute screen positioning.

    Both methods blur the images a little.

    Here is a picture showing what I get. I want it to look like the bottom line.
    http://img207.imageshack.us/img207/8553/44157057.png


    Here is the code I'm using
    Code:
    // test.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include "test.h"
    // include the basic windows header files and the Direct3D header file
    #include <windows.h>
    #include <windowsx.h>
    #include <d3d9.h>
    #include <d3dx9.h>
    
    // define the screen resolution
    #define SCREEN_WIDTH 800
    #define SCREEN_HEIGHT 600
    
    // include the Direct3D Library files
    #pragma comment (lib, "d3d9.lib")
    #pragma comment (lib, "d3dx9.lib")
    
    // global declarations
    LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
    LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
    LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;    // the pointer to the vertex buffer
    LPDIRECT3DVERTEXBUFFER9 v_buffer2 = NULL;    // the pointer to the vertex buffer
    LPDIRECT3DTEXTURE9      g_pTexture = NULL; // Our texture
    
    
    // function prototypes
    void initD3D(HWND hWnd);    // sets up and initializes Direct3D
    void render_frame(void);    // renders a single frame
    void cleanD3D(void);    // closes Direct3D and releases memory
    void init_graphics(void);    // 3D declarations
    
    
    //3D VERTEX STRUCT
    struct CUSTOMVERTEX {FLOAT X, Y, Z, tu, tv; DWORD COLOR;};
    #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_TEX1)
    
    //ABSOLUTE VERTEX STRUCT
    struct CUSTOMFLATVERTEX {FLOAT X, Y, Z, rhw, tu, tv ; DWORD COLOR;};
    #define CUSTOMFLATFVF (D3DFVF_XYZRHW  | D3DFVF_TEX1)
    
    
    
    // the WindowProc function prototype
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    
    // the entry point for any Windows program
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nCmdShow)
    {
        HWND hWnd;
        WNDCLASSEX wc;
    
        ZeroMemory(&wc, sizeof(WNDCLASSEX));
    
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WindowProc;
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.lpszClassName = L"WindowClass";
    
        RegisterClassEx(&wc);
    
        hWnd = CreateWindowEx(NULL, L"WindowClass", L"Our Direct3D Program",
                              WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
                              NULL, NULL, hInstance, NULL);
    
        ShowWindow(hWnd, nCmdShow);
    
        // set up and initialize Direct3D
        initD3D(hWnd);
    
        // enter the main loop:
    
        MSG msg;
    
        while(TRUE)
        {
            while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    
            if(msg.message == WM_QUIT)
                break;
    
            render_frame();
        }
    
        // clean up DirectX and COM
        cleanD3D();
    
        return msg.wParam;
    }
    
    
    // this is the main message handler for the program
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch(message)
        {
            case WM_DESTROY:
                {
                    PostQuitMessage(0);
                    return 0;
                } break;
        }
    
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
    
    
    // this function initializes and prepares Direct3D for use
    void initD3D(HWND hWnd)
    {
        d3d = Direct3DCreate9(D3D_SDK_VERSION);
    
        D3DPRESENT_PARAMETERS d3dpp;
    
        ZeroMemory(&d3dpp, sizeof(d3dpp));
        d3dpp.Windowed = TRUE;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.hDeviceWindow = hWnd;
        d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
        d3dpp.BackBufferWidth = SCREEN_WIDTH;
        d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    
    
    
        // create a device class using this information and the info from the d3dpp stuct
        d3d->CreateDevice(D3DADAPTER_DEFAULT,
                          D3DDEVTYPE_HAL,
                          hWnd,
                          D3DCREATE_HARDWARE_VERTEXPROCESSING,//D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DCREATE_MIXED_VERTEXPROCESSING
                          &d3dpp,
                          &d3ddev);
    
    
    
    	// Turn off culling
    	d3ddev->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
    
    	// Turn off D3D lighting
    	d3ddev->SetRenderState( D3DRS_LIGHTING, FALSE );
    
    	// Turn off the zbuffer
    	d3ddev->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);
    
    	//alpha blending enabled
    	d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
    
    	//source blend factor
    	d3ddev->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
    
    	//no looping texture
    	d3ddev->SetSamplerState(0,D3DSAMP_ADDRESSU,D3DTADDRESS_CLAMP);
    	d3ddev->SetSamplerState(0,D3DSAMP_ADDRESSV,D3DTADDRESS_CLAMP);
    
    
    
    
    
    	init_graphics();    // call the function to initialize the triangle
    
    }
    
    
    // this is the function used to render a single frame
    void render_frame(void)
    {
        d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    
        d3ddev->BeginScene();
    
        
    
     
    	///DRAW 3D IMAGE/////
        D3DXMATRIX matView;    // the view transform matrix
    
        D3DXMatrixLookAtLH(&matView,
                           &D3DXVECTOR3 (SCREEN_WIDTH/2, SCREEN_HEIGHT/2, SCREEN_HEIGHT/2),    // the camera position
                           &D3DXVECTOR3 (SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0.0f),    // the look-at position
                           &D3DXVECTOR3 (0.0f, -1.0f, 0.0f));    // the up direction
    
        d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView
    
        D3DXMATRIX matProjection;     // the projection transform matrix
    
        D3DXMatrixPerspectiveFovLH(&matProjection,
                                   D3DXToRadian(90),    // the horizontal field of view
                                   (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
                                   1.0f,    // the near view-plane
                                   SCREEN_HEIGHT/2+10);    // the far view-plane
    
        d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);    // set the projection
    	d3ddev->SetTexture( 0, g_pTexture );
    	// select which vertex format we are using
        d3ddev->SetFVF(CUSTOMFVF);
        // select the vertex buffer to display
        d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
    
        // copy the vertex buffer to the back buffer
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    
    
    
    
    
    
    	//DRAW ABSOLUTE IMAGE
    	d3ddev->SetTexture( 0, g_pTexture );
    	// select which vertex format we are using
        d3ddev->SetFVF(CUSTOMFLATFVF);
    	// select the vertex buffer to display
        d3ddev->SetStreamSource(0, v_buffer2, 0, sizeof(CUSTOMFLATVERTEX));
        // copy the vertex buffer to the back buffer
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    
        d3ddev->EndScene();
    
        d3ddev->Present(NULL, NULL, NULL, NULL);
    }
    
    
    // this is the function that cleans up Direct3D and COM
    void cleanD3D(void)
    {
        v_buffer->Release();    // close and release the vertex buffer
        d3ddev->Release();    // close and release the 3D device
        d3d->Release();    // close and release Direct3D
        g_pTexture->Release();
        v_buffer2->Release();
      
    }
    
    
    // this is the function that puts the 3D models into video RAM
    void init_graphics(void)
    {
    
    	//LOAD TEXTURE
    	D3DXIMAGE_INFO SrcInfo;      //Optional
    
    	//Use a magenta colourkey
    	D3DCOLOR colorkey = 0xFFFF00FF;
    
    	if (FAILED(D3DXCreateTextureFromFileEx (d3ddev, L"banana.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, 
            D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 
            colorkey, &SrcInfo, NULL, &g_pTexture)))
    	{
    		;
    	}
    
    
        // create the vertices using the CUSTOMVERTEX struct
         CUSTOMVERTEX vertices[] = 
        {	{ 50.0f, 50.0f+32.0f, 1.0f,			0.0f, 1.0f,	D3DCOLOR_XRGB(255, 0, 0), },
            { 50.0f, 50.0f, 1.0f,				0.0f, 0.0f,	D3DCOLOR_XRGB(255, 0, 0), },
            { 50.0f+32.0f, 50.0f+32.0f, 1.0f,	1.0f, 1.0f,	D3DCOLOR_XRGB(0, 0, 255), },     
            { 50.0f+32.0f, 50.0f, 1.0f,			1.0f, 0.0f,	D3DCOLOR_XRGB(0, 255, 0), },
        }; 
    
    	CUSTOMFLATVERTEX vertices2[] = 
    	{
    		{0.0f,	32.0f,	0.0f, 1.0f,			0.0f, 1.0f, D3DCOLOR_XRGB(255,255,255),},
    		{0.0f, 0.0f,	0.0f, 1.0f,			0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255),},
    		{32.0f,	32.0f,	0.0f, 1.0f,			1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255),},
    		{32.0f, 0.0f,	0.0f, 1.0f,			1.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255),},		
    	};
    
        // create a vertex buffer interface called v_buffer
        d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
                                   0,
                                   CUSTOMFVF,
                                   D3DPOOL_MANAGED,
                                   &v_buffer,
                                   NULL);
    
    	d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMFLATVERTEX),
                                   0,
                                   CUSTOMFLATFVF,
                                   D3DPOOL_MANAGED,
                                   &v_buffer2,
                                   NULL);
    
        VOID* pVoid;    // a void pointer
    
        // lock v_buffer and load the vertices into it
        v_buffer->Lock(0, 0, (void**)&pVoid, 0);
        memcpy(pVoid, vertices, sizeof(vertices));
        v_buffer->Unlock();
    
    	v_buffer2->Lock(0, 0, (void**)&pVoid, 0);
        memcpy(pVoid, vertices2, sizeof(vertices2));
        v_buffer2->Unlock();
    }
    Last edited by ybobjoe; 10-19-2009 at 02:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D in directX
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 01-25-2009, 11:23 AM
  2. text on 2d tiles in DirectX?
    By Quantum1024 in forum Game Programming
    Replies: 4
    Last Post: 04-26-2006, 11:40 PM
  3. 2D in DirectX 9
    By maxthecat in forum Game Programming
    Replies: 12
    Last Post: 01-08-2006, 09:17 PM
  4. OpenGL DirectX 2D
    By c++.prog.newbie in forum Game Programming
    Replies: 11
    Last Post: 03-01-2005, 01:54 PM
  5. DirectX 2D Frames Per Second
    By LuckY in forum Game Programming
    Replies: 9
    Last Post: 09-17-2004, 11:11 PM