Author Topic: D3D9 Tutorial 3: Loading an image from file and drawing it  (Read 4018 times)

0 Members and 1 Guest are viewing this topic.

Offline powerfear

  • - Secret Permissions Level 1 -
  • Jr. Member
  • **
  • Posts: 58
  • Reputation 64
  • Gender: Male
    • View Profile
D3D9 Tutorial 3: Loading an image from file and drawing it
« on: March 06, 2010, 10:28:01 AM »
Loading an image from a file and drawing it

Code: C++
  1. #include <windows.h> //include windows header for some function like MessageBox, creating the window.
  2. #include "d3dx9.h" //include a directx header
  3. #include <stdio.h> //for the sprinf_s function used by DrawString
  4.  
  5. #pragma comment(lib, "d3dx9.lib") //link the library
  6. #pragma comment(lib, "d3d9.lib") //link the library
  7. #define Emsg(msg){MessageBox(NULL,msg,"Error",MB_OK|MB_ICONEXCLAMATION);} //macro for error pop-up
  8.  
  9. int WINDOW_WIDTH = 1024; //window width
  10. int WINDOW_HEIGHT = 768; //window height
  11. bool windowed = true; //window mode, change to false for full screen
  12.  
  13. IDirect3D9 *pD3D; //global variable that will be used by a couple of our function
  14. IDirect3DDevice9 *pDevice; //a device that will be used for msot of our function created inside *pD3D
  15. LPD3DXFONT pFont; //font variable to use with our create font function and to display text after
  16.  
  17. LPDIRECT3DTEXTURE9 imagetex; //textute our image will be loaded into
  18. LPD3DXSPRITE sprite; //sprite to display our image
  19. D3DXVECTOR3 imagepos; //vector for the position of the sprite
  20.  
  21. LRESULT CALLBACK wndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) //callback function for our GetMessage function on the while statement inside the main function
  22. {
  23.         switch(uMsg) //switch the MSG variable passed
  24.         {
  25.         case WM_CLOSE: //case it's a Window Close MSG
  26.                 PostQuitMessage(0); //Apply a Quit Message
  27.                 break;
  28.         }
  29.         return DefWindowProc(hWnd,uMsg,wParam,lParam); //pass the variable
  30. }
  31.  
  32. HRESULT initialize()
  33. {
  34.         if(FAILED(D3DXCreateTextureFromFile(pDevice,"test.png",&imagetex)))//first parameter is our device,second is the path to our image, third is a texture variable to load the image into
  35.         {
  36.                 Emsg("Failed to laod the image"); //error pop-up for debug purpose
  37.                 return E_FAIL; //return it failed
  38.         }
  39.         if(FAILED(D3DXCreateSprite(pDevice,&sprite)))//first parameter is our device, second is a empty sprite variable
  40.         {
  41.                 Emsg("Failed to create the sprite"); //error pop-up for debug purpose
  42.                 return E_FAIL; //return it failed
  43.         }
  44.         imagepos.x=8.0f; //coord x of our sprite
  45.         imagepos.y=18.0f; //coord y of out sprite
  46.         imagepos.z=0.0f; //coord z of out sprite
  47.         return S_OK;
  48. }
  49.  
  50. HRESULT render() //function where all Drawing/Render stuff go
  51. {
  52.         if(SUCCEEDED(pDevice->BeginScene())) //Call the BeginScene function with our Device and see if it succeeded, all drawing/render code go behind BeginScene and EndScene
  53.         {
  54.                 sprite->Begin(D3DXSPRITE_ALPHABLEND); //begin our sprite with alphablend so it support alpha, like png use alpha
  55.                 sprite->Draw(imagetex,NULL,NULL,&imagepos,0xFFFFFFFF); //Draw the sprite, first parameter is the image texture, 4th parameter is the position, last parameter is the color change leave to 0xFFFFFFFF for no change
  56.                 sprite->End(); //end the sprite
  57.                 pDevice->EndScene(); //Call the EndScene function with our Device
  58.                 return S_OK; //return it succeeded
  59.         }
  60.         return E_FAIL; //return it failed
  61. }
  62.  
  63. void CleanUp() //function to delete/release things to prevent memory leak
  64. {
  65.         if(pD3D) //check if pD3D ism't released
  66.                 pD3D->Release(); //release it
  67.         if(pDevice) //check if pDevice ism't released
  68.                 pDevice->Release(); //release it
  69.         if(sprite) //check if sprite ism't released
  70.                 sprite->Release(); //release it
  71.         if(imagetex) //check if imagetex ism't released
  72.                 imagetex->Release(); //release it
  73. }
  74.  
  75. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) //our main function the first function called of the program
  76. {
  77.         WNDCLASSEX wc; //window class won't go in detail not related to this tutorial really
  78.         wc.cbSize = sizeof(WNDCLASSEX);
  79.         wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  80.         wc.lpfnWndProc = (WNDPROC)wndProc;
  81.         wc.cbWndExtra = 0;
  82.         wc.cbClsExtra = 0;
  83.         wc.hInstance = hInstance;
  84.         wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  85.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  86.         wc.hbrBackground = NULL;
  87.         wc.lpszMenuName = NULL;
  88.         wc.lpszClassName = "D3DTEST"; //class name
  89.         wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
  90.  
  91.         RECT rect; //window rectangle
  92.         rect.top = (long)0;
  93.         rect.left = (long)0;
  94.         rect.right = WINDOW_WIDTH; //use our global WINDOW_WIDTH variable
  95.         rect.bottom = WINDOW_HEIGHT; //use our global WINDOW_HEIGHT variable
  96.  
  97.         if(!RegisterClassEx(&wc)) //register the window cs
  98.         {
  99.                 Emsg("Could not register window class"); //error pop-up for debug purpose
  100.                 return 1; //return error
  101.         }
  102.  
  103.         AdjustWindowRectEx(&rect,WS_OVERLAPPEDWINDOW,false,WS_EX_APPWINDOW | WS_EX_WINDOWEDGE); //set the window width and height with the rect above
  104.         //the D3DTEST must match the class name of the window class and D3D TEST is the window name
  105.         HWND hWindow = CreateWindowEx(NULL,"D3DTEST","D3D TEST",WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW,0,0,rect.right-rect.left,rect.bottom-rect.top,NULL,NULL,hInstance,NULL); //create the window
  106.  
  107.         if(!hWindow) //if the window failed to create
  108.         {
  109.                 DestroyWindow(hWindow); //destroy the window
  110.                 UnregisterClass("D3DTEST",hInstance); //unregister our window class
  111.                 Emsg("Failed to create the window"); //error pop-up for debug purpose
  112.                 return 1; //return error
  113.         }
  114.  
  115.         ShowWindow(hWindow,SW_SHOW); //show our window
  116.         UpdateWindow(hWindow); //update our window
  117.         SetForegroundWindow(hWindow); //set our window on top
  118.         SetFocus(hWindow); //set the focus on our window
  119.  
  120.         D3DPRESENT_PARAMETERS d3dpp; //the presentation parameters that will be used when we will create the device
  121.         ZeroMemory(&d3dpp,sizeof(d3dpp)); //to be sure d3dpp is empty
  122.         d3dpp.Windowed = windowed; //use our global windowed variable to tell if the program is windowed or not
  123.         d3dpp.hDeviceWindow = hWindow; //give the window handle of the window we created above
  124.         d3dpp.BackBufferCount = 1; //set it to only use 1 backbuffer
  125.         d3dpp.BackBufferWidth = WINDOW_WIDTH; //set the buffer to our window width
  126.         d3dpp.BackBufferHeight = WINDOW_HEIGHT; //set the buffer to out window height
  127.         d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; //the backbuffer format
  128.         d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //SwapEffect
  129.  
  130.         pD3D = Direct3DCreate9(D3D_SDK_VERSION); //Create the presentation parameters
  131.  
  132.         if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWindow,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pDevice))) //create the device and check if it failed
  133.         {
  134.                 Emsg("Failed to create device"); //error pop-up for debug purpose
  135.                 CleanUp(); //call your CleanUp() function to prevent memory leak
  136.                 return 1; //exit the program return 1 error
  137.         }
  138.  
  139.         if(FAILED(initialize())) //call the initialize() function and check if it failed
  140.         {
  141.                 Emsg("Failed to initialize"); //error pop-up for debug purpose
  142.                 CleanUp(); //call your CleanUp() function to prevent memory leak
  143.                 return 1; //exit the program return 1 error
  144.         }
  145.  
  146.         MSG msg; //declare a MSG local variable for the GetMessage of the while loop
  147.         while(GetMessage(&msg,NULL,0,0)) //GetMessage reffer to the wndProc() function
  148.         {
  149.                 pDevice->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0.0f); //Clear the screen with black, basically since we only draw text here it's like your background change the D3DCOLOR_XRGB(0,0,0) to change the color the 3 number additioned must make 255 maximum
  150.                 if(FAILED(render())) //call the function render() and verify if it worked
  151.                 {
  152.                         Emsg("Render failed"); //error pop-up for debug purpose
  153.                 }
  154.  
  155.                 pDevice->Present(NULL,NULL,NULL,NULL); //display your buffer on screen
  156.                 TranslateMessage(&msg); //translate the msg of the GetMessage of your while loop
  157.                 DispatchMessage(&msg); //dispath the msg of the GetMessage of your while loop
  158.         }
  159.         CleanUp(); //call your CleanUp() function to prevent memory leak
  160.         return 0; //return 0 no problem
  161. }
  162.  

note: if you test your program directly from your compiler you need to provide the full path to the image