Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Something
Saturday, December 31st, 2011 at 8:19:08pm UTC 

  1. // Include the necessary libs.
  2. #pragma comment (lib, "d3d9.lib")
  3. #pragma comment (lib, "d3dx9.lib")
  4.  
  5. // Include the windows header file.
  6. #include <windows.h>
  7. #include <d3d9.h>
  8. #include <d3dx9.h>
  9. #include <iostream>
  10. #include <vector>
  11. #include <ctime>
  12. #include <sstream>
  13.  
  14. // Include definitions.
  15. #define SPRITE_WIDTH 48;
  16. #define SPRITE_HEIGHT 48;
  17. #define SCRN_WIDTH 1600;
  18. #define SCRN_HEIGHT 900;
  19.  
  20. using namespace std;
  21.  
  22. HINSTANCE hInst; // Global handle to hold the application instance.
  23. HWND wndHandle; // Global variable to hold the window handle.
  24.  
  25. // Direct 3D stuff
  26. LPD3DXFONT d3dFont;
  27. LPDIRECT3D9 pD3D;       // The Direct3D object.
  28. LPDIRECT3DDEVICE9 pd3dDevice;   // The Direct3D device.
  29.  
  30.  
  31. struct {
  32.         RECT srcRect;
  33.  
  34.         // Position
  35.         float X;
  36.         float Y;
  37.  
  38.         // Movement
  39.         float moveX;
  40.         float moveY;
  41.         short int dirX; // Non standard, this is to move the sprite in a given direction on the Y axis.
  42.         short int dirY; // Non standard, this is to move the sprite in a given direction on the X axis.
  43.  
  44.         // Animation
  45.         int numFrames;
  46.         int curFrame;
  47. } spriteStruct[3];
  48.  
  49. // Define some function prototypes.
  50. bool initWindow(HINSTANCE hInstance);
  51. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  52.  
  53. // Direct3D prototypes.
  54. bool initDirect3D(void);
  55. void render(void);
  56. IDirect3DSurface9* getSurfaceFromBitmap(std::string filename);
  57. bool initSprites(void);
  58.  
  59. // This is the WinMain function, it's the main() of a windows app.
  60. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  61. {
  62.         //srand((unsigned int)time(0));
  63.         // Initialize the window.
  64.         if (!initWindow(hInstance))
  65.                 return false;
  66.  
  67.         if (!initDirect3D())
  68.                 return false;
  69.  
  70.         if (!initSprites())
  71.                 return false;
  72.  
  73.         // The message loop.
  74.         MSG msg;
  75.         ZeroMemory(&msg, sizeof(msg));
  76.         while (msg.message != WM_QUIT)
  77.         {
  78.                 // Close the program if any key is pressed.
  79.                 if (msg.message == WM_KEYDOWN)
  80.                 {
  81.                         PostQuitMessage(0);
  82.                         return 0;
  83.                 }
  84.  
  85.                 // Windows related.
  86.                 if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  87.                 {
  88.                         TranslateMessage(&msg);
  89.                         DispatchMessage(&msg);
  90.                 }
  91.                 else
  92.                 {
  93.                         // This here, is the game loop. ~epic music~
  94.                         for (int i = 0; i <10; i++)
  95.                         {
  96.                                 spriteStruct[i].X  = 0;
  97.                                 // Move the sprite in the correct direction.
  98.                                 //if (spriteStruct[i].dirX == 1) spriteStruct[i].moveX += .5;
  99.                                 //else spriteStruct[i].moveX -= .5;
  100.  
  101.                                 //if (spriteStruct[i].dirY == 1) spriteStruct[i].moveY -= 1;
  102.                                 //else spriteStruct[i].moveY += 1;
  103.                                 //--OLD--
  104.                                 spriteStruct[i].moveX += .25;
  105.                                 spriteStruct[i].moveY += .25;
  106.  
  107.                                 spriteStruct[i].X += spriteStruct[i].moveX;
  108.                                 spriteStruct[i].Y += spriteStruct[i].moveY;
  109.  
  110.                                 // Not greater than 640.
  111.                                 if ((spriteStruct[i].X + 48) >= (1600 - 20))
  112.                                         spriteStruct[i].dirX *= -1;
  113.  
  114.                                 // Not greater than 480.
  115.                                 if ((spriteStruct[i].Y + 48) >= (900 - 20))
  116.                                         spriteStruct[i].dirY *= -1;
  117.  
  118.                                 // Check 0 now.
  119.                                 if (spriteStruct[i].X <= 20)
  120.                                         spriteStruct[i].dirX *= -1;
  121.                                 if (spriteStruct[i].Y <= 20)
  122.                                         spriteStruct[i].dirY *= -1;
  123.                         }
  124.                         render();
  125.                 }
  126.         }
  127.         return (int)msg.wParam;
  128. }
  129.  
  130. // bool initWindow(): registers the window class for the application and creates the window.
  131. bool initWindow(HINSTANCE hInstance)
  132. {
  133.         WNDCLASSEX wcex;
  134.  
  135.         // Fill in the WNDCLASSEX structure.
  136.         wcex.cbSize                    = sizeof(WNDCLASSEX); // The size of the structure.
  137.         wcex.style                        = CS_HREDRAW | CS_VREDRAW; // The class style.
  138.         wcex.lpfnWndProc                = (WNDPROC)WndProc; // The windows procedure callback/
  139.         wcex.cbClsExtra   = 0; // Extra bytes to allocate for this class.
  140.         wcex.cbWndExtra   = 0; // Extra bytes to allocate for this instance.
  141.         wcex.hInstance      = hInstance; // Handle to the application instance.
  142.         wcex.hIcon                        = 0; // Icon to associate with this application.
  143.         wcex.hCursor            = LoadCursor(NULL, IDC_ARROW); // The default cursor.
  144.         wcex.hbrBackground            = (HBRUSH)(COLOR_WINDOW+1); // The background color.
  145.         wcex.lpszMenuName              = NULL; // The resource name for the menu.
  146.         wcex.lpszClassName            = "DirectX Example"; // The class name being created.
  147.         wcex.hIconSm            = 0; // The handle to the small icon.
  148.         RegisterClassEx(&wcex);
  149.  
  150.         wndHandle = CreateWindow(
  151.                                         "DirectX Example",      // The window class to use.
  152.                                         "DirectX Example",      // The title bar text.
  153.                                         WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,  // The window style to use. *** WS_OVERLAPPEDWINDOW - Windowed; WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE - Full screen ~ Rune4234;
  154.                                         CW_USEDEFAULT,  // The default X coordinate.
  155.                                         CW_USEDEFAULT,  // The default Y coordinate.
  156.                                         640,    // The pixel width of the window.
  157.                                         480,    // The pixel height of the window.
  158.                                         NULL,   // The parent window. NULL for desktop.
  159.                                         NULL,   // The menu for the application. NULL for none.
  160.                                         hInstance,      // The handle to the application instance.
  161.                                         NULL)// No values passed to the window.
  162.  
  163.         // Check to make sure the handle created is valid.
  164.         if (!wndHandle)
  165.                 return false;
  166.  
  167.         // Display the window.
  168.         ShowWindow(wndHandle, SW_SHOW);
  169.         UpdateWindow(wndHandle);
  170.         return true;
  171. }
  172.  
  173. // The window procedure.
  174. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  175. {
  176.         // Check any available messages from the queue.
  177.         switch (message)
  178.         {
  179.         case WM_DESTROY:
  180.                 PostQuitMessage(0);
  181.                 break;
  182.         }
  183.  
  184.         // Always return the message to the default window procedure for further processing.
  185.         return DefWindowProc(hWnd, message, wParam, lParam);
  186. }
  187.  
  188. // The initDirect3D function.
  189. bool initDirect3D(void)
  190. {
  191.         pD3D = NULL;
  192.         pd3dDevice = NULL;
  193.         //d3dFont = NULL;
  194.  
  195.         // Create the DirectX object.
  196.         if (NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) return false;
  197.  
  198.         // Fill the presentation parameters structure.
  199.         D3DPRESENT_PARAMETERS d3dpp;
  200.         ZeroMemory(&d3dpp, sizeof(d3dpp));
  201.         d3dpp.Windowed      = FALSE; // *** TRUE - Windowed; FALSE - Fullscreen ~ Rune4234
  202.         d3dpp.SwapEffect                = D3DSWAPEFFECT_DISCARD;
  203.         d3dpp.BackBufferFormat  = D3DFMT_X8R8G8B8; // *** D3DFMT_UNKNOWN - Windowed; D3DFMT_X8R8G8B8 - Fullscreen ~ Rune4234
  204.         d3dpp.BackBufferCount   = 1;
  205.         d3dpp.BackBufferWidth   = 1600;
  206.         d3dpp.BackBufferHeight  = 900;
  207.         d3dpp.hDeviceWindow          = wndHandle;
  208.  
  209.         // Create the default DirectX device.
  210.         if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice)))
  211.                 return false;
  212.  
  213.         // Create the font object.
  214.         //if (FAILED(D3DXCreateFont(pd3dDevice, 48, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial", &d3dFont)))
  215.                 //return NULL;
  216.        
  217.         return true;
  218.  
  219. }
  220.  
  221. // Direct3D render function.
  222. void render(void)
  223. {
  224.         string spStr;
  225.         stringstream strX;
  226.         stringstream strY;
  227.  
  228.         // This will hold the back buffer.
  229.         IDirect3DSurface9* backbuffer = NULL;
  230.  
  231.         // Check to make sure you have a valid Direct3D device. 
  232.         if ((pd3dDevice == NULL)/* || (d3dFont == NULL)*/)
  233.                 return;
  234.  
  235.         // Set the back buffer to a blue color.
  236.         pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  237.  
  238.         // Get the back buffer.
  239.         pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
  240.  
  241.         for (int i = 0; i < 10; i++)
  242.         {
  243.                 // Increment the animation frame.
  244.                 if (spriteStruct[i].curFrame < spriteStruct[i].numFrames)
  245.                         spriteStruct[i].curFrame++;
  246.                 else
  247.                         spriteStruct[i].curFrame = 0;
  248.  
  249.                 // Set the source rectangle to the correct frame.
  250.                 spriteStruct[i].srcRect.left = spriteStruct[i].curFrame * 48;
  251.                 spriteStruct[i].srcRect.right = spriteStruct[i].srcRect.left + 48;
  252.                 RECT destRect;    // A temporary rectangle.
  253.                 RECT textRect;
  254.                 IDirect3DSurface9* spriteSurface;
  255.                
  256.                 spriteSurface = getSurfaceFromBitmap("text.bmp");
  257.                 if (spriteSurface == NULL) return;
  258.                
  259.                 destRect.left = (long)spriteStruct[i].X;
  260.                 destRect.top = (long)spriteStruct[i].Y;
  261.                 destRect.bottom = destRect.top + SPRITE_HEIGHT;
  262.                 destRect.right = destRect.left + SPRITE_WIDTH;
  263.  
  264.                 /*textRect.left = (long)spriteStruct[i].X;
  265.                 textRect.top = (long)spriteStruct[i].Y;
  266.                 textRect.bottom = textRect.top + SPRITE_HEIGHT;
  267.                 textRect.right = textRect.left + 100;*/
  268.  
  269.                 /*strX << spriteStruct[i].X;
  270.                 strY << spriteStruct[i].Y;
  271.                 spStr = "X: ";
  272.                 spStr += strX.out ;
  273.                 spStr += " Y: ";
  274.                 spStr += strY.out;*/
  275.  
  276.                 // Draw the sprite to the back buffer.
  277.                 pd3dDevice->StretchRect(spriteSurface, &spriteStruct[i].srcRect, backbuffer, &destRect, D3DTEXF_NONE);
  278.                 // Draw the coords of the sprite.
  279.                 //d3dFont->DrawText(NULL, spStr.c_str(), -1, &textRect, DT_LEFT|DT_NOCLIP, 0xFFFFFFFF);
  280.         }
  281.  
  282.         // Present the back buffer to the display.
  283.         pd3dDevice->Present(NULL, NULL, NULL, NULL);
  284. }
  285.  
  286. // Direct3D clean up function.
  287. void cleanUp(void)
  288. {
  289.         // Release the device and the Direct3D object.
  290.         if (pd3dDevice != NULL)
  291.                 pd3dDevice->Release();
  292.  
  293.         if (pD3D != NULL)
  294.                 pD3D->Release();
  295.         //if (d3dFont != NULL)
  296.         //      d3dFont->Relase();
  297. }
  298. IDirect3DSurface9* getSurfaceFromBitmap(string filename)
  299. {
  300.         HRESULT hResult;
  301.         IDirect3DSurface9* surface = NULL;
  302.         int lol = NULL;
  303.         D3DXIMAGE_INFO imgInfo;  // Holds information about the loaded bitmap.
  304.  
  305.         if (pd3dDevice == NULL) return NULL;
  306.         // Get the width and height info from this bitmap
  307.         hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imgInfo);
  308.         if (FAILED(hResult)) return NULL;
  309.  
  310.         int width = imgInfo.Width, height = imgInfo.Height;
  311.  
  312.         // Create an offscreen surface to hold the bitmap.
  313.         hResult = pd3dDevice->CreateOffscreenPlainSurface(width, height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
  314.         if (FAILED(hResult)) return NULL;
  315.  
  316.         // Load the bitmap into the surface that we created.
  317.         hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL);
  318.         if (FAILED(hResult)) return NULL;
  319.  
  320.         return surface;
  321. }
  322.  
  323. bool initSprites(void)
  324. {
  325.         srand((unsigned int)time(0));
  326.         // Loop through 10 sprite structures and initialize them.
  327.         for (int i = 0; i < 3; i++)
  328.         {
  329.                 // Set the location data.
  330.                 spriteStruct[i].srcRect.top               = 0;
  331.                 spriteStruct[i].srcRect.left        = i * SPRITE_WIDTH;
  332.                 spriteStruct[i].srcRect.right      = spriteStruct[i].srcRect.left + SPRITE_WIDTH;
  333.                 spriteStruct[i].srcRect.bottom    = SPRITE_HEIGHT;
  334.                 spriteStruct[i].X = (int)rand() % SCRN_WIDTH - SPRITE_WIDTH;
  335.                 spriteStruct[i].Y = (int)rand() % SCRN_HEIGHT - SPRITE_HEIGHT;
  336.                 spriteStruct[i].dirX = rand() % 2 + 1; // This isn't a standard parameter, I added this to make it move a random direction upon creation.
  337.                 spriteStruct[i].dirY = rand() % 2 + 1; // ^^^
  338.                
  339.                 // Set the animation data.
  340.                 spriteStruct[i].curFrame = 0;
  341.                 spriteStruct[i].numFrames = 3;
  342.         }
  343.         return true;
  344. }

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right
worth-right