Drawing problem

This is a discussion on Drawing problem within the Game Programming forums, part of the General Programming Boards category; I'm having a problem with drawing the game. It's too slow because I draw the whole board every time so ...

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    Drawing problem

    I'm having a problem with drawing the game.
    It's too slow because I draw the whole board every time so I tried to draw the changes only but the background color changed every time I draw the changes.
    I think it happens because I don't clear the device.. but why? \=
    I would be happy if you could fix it for me and explain me why it happend.
    Thanks (:

    The code:
    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include <d3d9.h>
    #include <D3dx9core.h>
    #include <string.h>
    
    #pragma comment(lib, "d3d9.lib")
    #pragma comment(lib, "D3dx9.lib")
    
    #define SCREEN_WIDTH  1024
    #define SCREEN_HEIGHT 768
    #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) 
    
    // global declarations
    LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
    LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
    LPD3DXFONT dxfont;    // the pointer to the font object
    LPD3DXSPRITE d3dspt;    // the pointer to our Direct3D Sprite interface
    LPDIRECT3DTEXTURE9 sprite;    // the pointer to the sprite
    HWND hWnd; // the handle for the window, filled by a function
    
    // function prototypes
    void initD3D(HWND hWnd);    // sets up and initializes Direct3D
    void RenderFrame(void);    // renders a single frame
    void cleanD3D(void);    // closes Direct3D and releases memory
    void DrawLine(float x1, float y1, float x2, float y2, DWORD color);
    void PrintText(char* str, int size, int x, int y, DWORD color);
    void DrawImage(char* str, float x, float y);
    
    float x = SCREEN_WIDTH/2;
    const int bh = 15, bw = 5;
    bool bricks[bh][bw];
    
    // 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)
    {
    
        // this struct holds information for the window class
        WNDCLASSEX wc;
    
        // clear out the window class for use
        ZeroMemory(&wc, sizeof(WNDCLASSEX));
    
        // fill in the struct with the needed information
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WindowProc;
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
        wc.lpszClassName = "WindowClass1";
    
        // register the window class
        RegisterClassEx(&wc);
    
        // create the window and use the result as the handle
        hWnd = CreateWindowEx(NULL,
                              "WindowClass1",    // name of the window class
                              "Direct3D",   // title of the window
                              WS_EX_TOPMOST | WS_POPUP, // WS_OVERLAPPEDWINDOW for windowed mode // window style
                              0,    // x-position of the window
                              0,    // y-position of the window
                              SCREEN_WIDTH,    // width of the window
                              SCREEN_HEIGHT,    // height of the window
                              NULL,    // we have no parent window, NULL
                              NULL,    // we aren't using menus, NULL
                              hInstance,    // application handle
                              NULL);    // used with multiple windows, NULL
    
        // display the window on the screen
        ShowWindow(hWnd, nCmdShow);
    
        // set up and initialize Direct3D
        initD3D(hWnd);
    
    	D3DXCreateSprite(d3ddev, &d3dspt);    // create the Direct3D Sprite object
    
    	for(int i=0;i<bh;i++)
    		for(int j=0;j<bw;j++)
    			bricks[i][j] = true;
    
    	MSG msg;
    
    	while(TRUE)
        {
            while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    
    		RenderFrame();
    
    		if(msg.message == WM_QUIT)
                break;
    
    		if(KEY_DOWN(VK_ESCAPE))
    			PostMessage(hWnd, WM_DESTROY, 0, 0); 
    
    		if (KEY_DOWN(VK_LEFT) && x > 0)
    		{
    			x -= 5;
    			// Draw the changes only
    		}
    		if (KEY_DOWN(VK_RIGHT) && x < SCREEN_WIDTH - 125)
    		{
    			x += 5;
    			// Draw the changes only
    		}
    	}
    
        // 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)
    {
        // sort through and find what code to run for the message given
        switch(message)
        {
            // this message is read when the window is closed
            case WM_DESTROY:
                {
                    // close the application entirely
                    PostQuitMessage(0);
                    return 0;
                } break;
        }
    
        // Handle any messages the switch statement didn't
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
    
    // this function initializes and prepares Direct3D for use
    void initD3D(HWND hWnd)
    {
        d3d = Direct3DCreate9(D3D_SDK_VERSION);    // create the Direct3D interface
    
        D3DPRESENT_PARAMETERS d3dpp;    // create a struct to hold various device information
    
        ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use
        d3dpp.Windowed = FALSE;    // TRUE = WINDOWED
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames
        d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D
    	// for Full Screen Mode
    	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;    // set the back buffer format to 32-bit
    	d3dpp.BackBufferWidth = SCREEN_WIDTH;    // set the width of the buffer
        d3dpp.BackBufferHeight = SCREEN_HEIGHT;    // set the height of the buffer
    
        // create a device class using this information and information from the d3dpp stuct
        d3d->CreateDevice(D3DADAPTER_DEFAULT,
                          D3DDEVTYPE_HAL,
                          hWnd,
                          D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                          &d3dpp,
                          &d3ddev);
    }
    
    // this is the function used to render a single frame
    void RenderFrame(void)
    {
        // clear the window to a deep blue
        d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(216, 233, 236), 1.0f, 0);
    
        d3ddev->BeginScene();    // begins the 3D scene
    
        // do 3D rendering on the back buffer here
    	DrawLine(0,0,SCREEN_WIDTH,0,0xffffffff);
    	DrawLine(0,0,0,SCREEN_HEIGHT,0xffffffff);
    	DrawLine(0,SCREEN_HEIGHT-1,SCREEN_WIDTH-1,SCREEN_HEIGHT-1,0xffffffff);
    	DrawLine(SCREEN_WIDTH-1,0,SCREEN_WIDTH-1,SCREEN_HEIGHT-1,0xffffffff);
    	//PrintText("Captian Power By Resized:", 20, 395, 380, 0xffffffff);
    
    	d3dspt->Begin(NULL);    // begin sprite drawing
    
    	// perform sprite drawing here
    	DrawImage("D:\\gavrA'\\C, C++\\DirectX & WinApp's\\Game\\Bricks\\Debug\\Images\\obj.jpg", x, SCREEN_HEIGHT*9/10);
    
    	for(int i=0;i<bh;i++)
    		for(int j=0;j<bw;j++)
    			if(bricks[i][j])
    				DrawImage("D:\\gavrA'\\C, C++\\DirectX & WinApp's\\Game\\Bricks\\Debug\\Images\\brick.jpg", (float)i*50+150, (float)j*50+100);
    	d3dspt->End();    // end sprite drawing
    
        d3ddev->EndScene();    // ends the 3D scene
    
        d3ddev->Present(NULL, NULL, NULL, NULL);    // displays the created frame
    }
    
    // this is the function that cleans up Direct3D and COM
    void cleanD3D(void)
    {
    	sprite->Release();
        d3ddev->Release();    // close and release the 3D device
        d3d->Release();    // close and release Direct3D
    }
    
    void DrawLine(float x1, float y1, float x2, float y2, DWORD color)
    {
    		LPD3DXLINE line;
            D3DXCreateLine(d3ddev, &line);
            D3DXVECTOR2 lines[] = {D3DXVECTOR2(x1, y1), D3DXVECTOR2(x2, y2)};
            line->Begin();
            line->Draw(lines, 2, color);
            line->End();
            line->Release();
    }
    
    void PrintText(char* str, int size, int x, int y, DWORD color)
    {
    	static RECT textbox; SetRect(&textbox, x, y, SCREEN_WIDTH, SCREEN_HEIGHT);
    		    D3DXCreateFont(d3ddev,    // the D3D Device
                       size,    // font height of 30
                       0,    // default font width
                       FW_NORMAL,    // font weight
                       1,    // not using MipLevels
                       false,    // italic font
                       DEFAULT_CHARSET,    // default character set
                       OUT_DEFAULT_PRECIS,    // default OutputPrecision,
                       DEFAULT_QUALITY,    // default Quality
                       DEFAULT_PITCH | FF_DONTCARE,    // default pitch and family
                       "Arial",    // use Facename Arial
                       &dxfont);    // the font object
        dxfont->DrawTextA(NULL,
                          str,
                          strlen(str),
                          &textbox,
                          DT_LEFT | DT_TOP,
                          color);
    }
    
    void DrawImage(char* str, float x, float y)
    {
    	D3DXCreateTextureFromFile(d3ddev, str, &sprite);
    	D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);    // center at the upper-left corner
    	D3DXVECTOR3 position(x, y, 0.0f);    
    	d3dspt->Draw(sprite, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
    }
    Last edited by gavra; 06-03-2009 at 04:02 AM.
    gavra.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Try caching the texture resources away in memory instead of having to load it from disc and process each resource everytime you go to draw, which could be a lot.

  3. #3
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    I see your point, but I don't think it can make such a huge difference that I need.
    As I said, I think the cause is that I draw everything when I only need to make a small change.
    Thanks anyway.. I'll try it.

    more ideas?
    gavra.

  4. #4
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Thanks dude you were right [=
    the loading takes too much time.
    Thank you again (:
    gavra.

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    General rule of thumb when something is slow is to check the IO first, as it is generally the greatest bottleneck in an algorithm.

    Np, Gl.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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