Note that there are some explanatory texts on larger screens.

plurals
  1. POSprite object no longer renders when put in to a Vector
    primarykey
    data
    text
    <p>Creating a small 2D game, using Direct3D 9 and C++. I have come across a problem I have not seen before. The game is a shop based one so I have a customer class. I have created the object in the game and it renders and behaves how I want for the minute. I decided to put the object in to a Vector instead because I will, at some point, need a collection of them at any one time. My problem is, however, when I put the object in to a Vector, it no longer is visible on the screen!</p> <p>I get no errors. I have outputted all the return codes from the DirectX functions in to a test file and nothing failed. It ran fine, it just isn't visible on the screen! Nothing has changed other than putting it in to a Vector.</p> <p>I changed the Vector to a Vector of pointers because I was told that the Vector was simply creating the object, copying it and deleting the original. However, it would now seem that my Direct3D pointers become invalid using this method, pretty quickly. I think I know what is going wrong but not sure the best course of action. </p> <p>Here is the customer class:</p> <pre><code>#include "Headers.h" #include "Playercard.h" class Customer{ private: LPDIRECT3DTEXTURE9 texture, playerInfoCard; D3DXVECTOR3 position, direction, playerCardPosition; D3DXVECTOR3 center; LPD3DXFONT font; POINT curPos; HRESULT rc; //Playercard playerCard; int depthBuffer; int walkingAlternator; // Some information about the customer char name[10]; int age; double cash; int itemWanted; char productWanted[20]; bool happy; bool male; bool female; bool displayPlayerCard; RECT textBox; Playercard playercard; void assignNameAndGender() { srand((unsigned int)time(0)); int name_decider = rand() % 16 + 1; switch(name_decider) { case 1: strcpy_s(name, "John"); male = true; female = false; break; case 2: strcpy_s(name, "Chris"); male = true; female = false; break; case 3: strcpy_s(name, "Ben"); male = true; female = false; break; case 4: strcpy_s(name, "Jack"); male = true; female = false; break; case 5: strcpy_s(name, "Jamie"); male = true; female = false; break; case 6: strcpy_s(name, "Bill"); male = true; female = false; break; case 7: strcpy_s(name, "Liam"); male = true; female = false; break; case 8: strcpy_s(name, "Alex"); male = true; female = false; break; case 9: strcpy_s(name, "Nikki"); male = false; female = true; break; case 10: strcpy_s(name, "Alice"); male = false; female = true; break; case 11: strcpy_s(name, "Lucy"); male = false; female = true; break; case 12: strcpy_s(name, "Emily"); male = false; female = true; break; case 13: strcpy_s(name, "Laura"); male = false; female = true; break; case 14: strcpy_s(name, "Mary"); male = false; female = true; break; case 15: strcpy_s(name, "Katie"); male = false; female = true; break; case 16: strcpy_s(name, "Emma"); male = false; female = true; break; } } void assignAge() { srand((unsigned int)time(0)); age = rand() % 53 + 12; // An age between 12 and 65 } void assignCash() { if(age &lt; 16) cash = 5.00; if(age &gt;= 16 &amp;&amp; age &lt;= 18) cash = 15.00; if(age &gt;= 19 &amp;&amp; age &lt;= 21) cash = 20.00; if(age &gt;= 22 &amp;&amp; age &lt;= 25) cash = 25.00; if(age &gt;= 26 &amp;&amp; age &lt;= 30) cash = 30.00; if(age &gt; 31) { int randNum = rand() % 9 + 1; switch(randNum) { case 1: cash = 20.00; break; case 2: cash = 25.00; break; case 3: cash = 30.00; break; case 4: cash = 35.00; break; case 5: cash = 40.00; break; case 6: cash = 45.00; break; case 7: cash = 50.00; break; case 8: cash = 55.00; break; case 9: cash = 60.00; break; } } } public: Customer() { direction.x = (float)cos(0.558)*4; // 30 degree movement direction.y = (float)sin(0.558)*4; // 30 degree movement position.x = 500; position.y = 500; displayPlayerCard = false; // Only true when mouse clicked happy = true; // A new customer is always happy at first! walkingAlternator = 1; // Help control sprite movement strcpy_s(productWanted, "Xbox"); assignNameAndGender(); // Assign customer a name assignAge(); // Assign customer an age assignCash(); // Assign customer an amount of cash playercard.load(name, male, age, cash, productWanted); } ~Customer() { } void move() { if(KEY_DOWN(0x57) || KEY_DOWN(VK_UP)) { position.y -= direction.y; position.x += direction.x; walkingUp(); } else if(KEY_DOWN(0x41) || KEY_DOWN(VK_LEFT)) { position.y -= direction.y; position.x -= direction.x; walkingLeft(); } else if(KEY_DOWN(0x53) || KEY_DOWN(VK_DOWN)) { position.y += direction.y; position.x -= direction.x; walkingDown(); } else if(KEY_DOWN(0x44) || KEY_DOWN(VK_RIGHT)) { position.y += direction.y; position.x += direction.x; walkingRight(); } else { // Reset the sprite here so that the feet are together in resting position AND so that sprite is facing the way it was going when it stopped } } void loadGraphics() { D3DXCreateTextureFromFileEx(d3dDevice, "player_information_card.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;playerInfoCard); D3DXCreateTextureFromFileEx(d3dDevice, "Characters/female_shopper1.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;texture); D3DXVECTOR3 position(100.0f, 100.0f, 0.0f); D3DXVECTOR3 center(0.0f, 0.0f, 0.0f); D3DXCreateFont(d3dDevice, // the D3D Device 20, // 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 "Georgia", // use Facename Arial &amp;font); // the font object } void draw() { d3dSprite-&gt;Draw(texture, NULL, &amp;center, &amp;position, D3DCOLOR_ARGB(255, 255, 255, 255)); if(displayPlayerCard) { playerCardPosition = position; playerCardPosition.y -= 128; playerCardPosition.x += 32; SetRect(&amp;textBox, (int)playerCardPosition.x+5, (int)playerCardPosition.y+5, (int)playerCardPosition.x+256-5, (int)playerCardPosition.y+128-5); d3dSprite-&gt;Draw(playerInfoCard, NULL, &amp;center, &amp;playerCardPosition, D3DCOLOR_ARGB(255, 255, 255, 255)); font-&gt;DrawTextA(d3dSprite, playercard.plCard().c_str(), -1, &amp;textBox, DT_LEFT, D3DCOLOR_ARGB(255, 255, 255, 255)); } } void walkingDown() { if(walkingAlternator == 1 || walkingAlternator == 2 || walkingAlternator == 3 || walkingAlternator == 4 || walkingAlternator == 5 || walkingAlternator == 6 || walkingAlternator == 7 || walkingAlternator == 8 || walkingAlternator == 9 || walkingAlternator == 10) { D3DXCreateTextureFromFileEx(d3dDevice, "Characters/female_shopper2.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;texture); walkingAlternator++; } else if(walkingAlternator == 11 || walkingAlternator == 12 || walkingAlternator == 13 || walkingAlternator == 14 || walkingAlternator == 15 || walkingAlternator == 16 || walkingAlternator == 17 || walkingAlternator == 18 || walkingAlternator == 19 || walkingAlternator == 20) { D3DXCreateTextureFromFileEx(d3dDevice, "Characters/female_shopper3.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;texture); walkingAlternator++; } if(walkingAlternator &gt;= 20) { walkingAlternator = 1; } } void walkingUp() { if(walkingAlternator == 1 || walkingAlternator == 2 || walkingAlternator == 3 || walkingAlternator == 4 || walkingAlternator == 5 || walkingAlternator == 6 || walkingAlternator == 7 || walkingAlternator == 8 || walkingAlternator == 9 || walkingAlternator == 10) { D3DXCreateTextureFromFileEx(d3dDevice, "Characters/female_shopper1_back2.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;texture); walkingAlternator++; } else if(walkingAlternator == 11 || walkingAlternator == 12 || walkingAlternator == 13 || walkingAlternator == 14 || walkingAlternator == 15 || walkingAlternator == 16 || walkingAlternator == 17 || walkingAlternator == 18 || walkingAlternator == 19 || walkingAlternator == 20) { D3DXCreateTextureFromFileEx(d3dDevice, "Characters/female_shopper1_back1.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;texture); walkingAlternator++; } if(walkingAlternator &gt;= 20) { walkingAlternator = 1; } } void walkingLeft() { if(walkingAlternator == 1 || walkingAlternator == 2 || walkingAlternator == 3 || walkingAlternator == 4 || walkingAlternator == 5 || walkingAlternator == 6 || walkingAlternator == 7 || walkingAlternator == 8 || walkingAlternator == 9 || walkingAlternator == 10) { D3DXCreateTextureFromFileEx(d3dDevice, "Characters/female_shopper1_back3.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;texture); walkingAlternator++; } else if(walkingAlternator == 11 || walkingAlternator == 12 || walkingAlternator == 13 || walkingAlternator == 14 || walkingAlternator == 15 || walkingAlternator == 16 || walkingAlternator == 17 || walkingAlternator == 18 || walkingAlternator == 19 || walkingAlternator == 20) { D3DXCreateTextureFromFileEx(d3dDevice, "Characters/female_shopper1_back4.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;texture); walkingAlternator++; } if(walkingAlternator &gt;= 20) { walkingAlternator = 1; } } void walkingRight() { if(walkingAlternator == 1 || walkingAlternator == 2 || walkingAlternator == 3 || walkingAlternator == 4 || walkingAlternator == 5 || walkingAlternator == 6 || walkingAlternator == 7 || walkingAlternator == 8 || walkingAlternator == 9 || walkingAlternator == 10) { D3DXCreateTextureFromFileEx(d3dDevice, "Characters/female_shopper4.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;texture); walkingAlternator++; } else if(walkingAlternator == 11 || walkingAlternator == 12 || walkingAlternator == 13 || walkingAlternator == 14 || walkingAlternator == 15 || walkingAlternator == 16 || walkingAlternator == 17 || walkingAlternator == 18 || walkingAlternator == 19 || walkingAlternator == 20) { D3DXCreateTextureFromFileEx(d3dDevice, "Characters/female_shopper5.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &amp;texture); walkingAlternator++; } if(walkingAlternator &gt;= 20) { walkingAlternator = 1; } } void checkInteractivity() { GetCursorPos(&amp;curPos); if (GetKeyState(VK_LBUTTON) &amp; 0x80 &amp;&amp; curPos.x &gt;= position.x &amp;&amp; curPos.x &lt;= position.x+64 &amp;&amp; curPos.y &gt;= position.y &amp;&amp; curPos.y &lt;= position.y+128) { displayPlayerCard = true; } else { displayPlayerCard = false; } } D3DXVECTOR3 returnPosition() { return position; } D3DXVECTOR3 returnPosition(bool returnCentreOfSprite) { return position; } }; </code></pre> <p>Here is the code for the main</p> <pre><code>#include "Headers.h." #include "Customer.h" #include "Background.h" #include "Counter.h" #include "GameStations.h" #include "ConsoleCab.h" #include "ClothesStand.h" #include "GamePanel.h" std::vector&lt;Customer*&gt; customers; Background background; Counter counter; GameStations gameStations; ConsoleCab consoleCab; ClothesStand clothesStand; GamePanel gamePanel; void initDirectX(HWND hWnd); // Initializes Direct3D Graphics void render(); // Render graphics void cleanUp(); // Cleans everything up and releases memory LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hWnd; // The handle to the window function WNDCLASSEX window; // Pointer to window struct ZeroMemory(&amp;window, sizeof(WNDCLASSEX)); // Clears window class so we can use it window.cbSize = sizeof(WNDCLASSEX); // Size of window window.style = CS_HREDRAW | CS_VREDRAW; // Redraws the entire window if a movement or size adjustment changes the height of the client area. window.lpfnWndProc = WindowProc; // Pointer to the window procedure window.hInstance = hInstance; // Handle to current instance window.hCursor = LoadCursor(NULL, IDC_ARROW); // We'll stick with the normal cursor here window.lpszClassName = "Window"; // Gives the class a name RegisterClassEx(&amp;window); // Registers the window class hWnd = CreateWindowEx(NULL, "Window", // Name of the class "Space Game", // Title of the window WS_EX_TOPMOST | WS_POPUP, // Fullscreen 0, 0, // Position of window (0,0 for fullscreen) SCREEN_WIDTH, SCREEN_HEIGHT, // Screen resolution (Uses global declaration) NULL, // Parent window (None) NULL, // Menus (None) hInstance, // Application handle NULL); // Set to NULL as we aren't using more than 1 window ShowWindow(hWnd, nCmdShow); // Display window initDirectX(hWnd); // Initialises the directX graphics MSG msg = {0}; // msg holds the Windows events message queue customers.push_back(new Customer()); for (std::vector&lt;Customer*&gt;::iterator customerIT = customers.begin(); customerIT != customers.end(); customerIT++) { (*customerIT)-&gt;loadGraphics(); } // Object properties become invalid here. background.loadGraphics(); counter.loadGraphics(); gameStations.loadGraphics(); consoleCab.loadGraphics(); clothesStand.loadGraphics(); gamePanel.loadGraphics(); /************************************** Main game loop *************************************************/ while(TRUE) // Main game loop { if(PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE)) // If messages are waiting { TranslateMessage(&amp;msg); // Translates the keystroke messages into the correct format DispatchMessage(&amp;msg); // Sends message to the windowsProc function if(msg.message == WM_QUIT) // If the message was a quit message break; // Breaks out of main game loop } else { for (std::vector&lt;Customer*&gt;::iterator customerIT = customers.begin(); customerIT != customers.end(); customerIT++) { (*customerIT)-&gt;checkInteractivity(); (*customerIT)-&gt;move(); } gamePanel.hoverDetection(); render(); } } /*******************************************************************************************************/ // We are out of the main game loop (Due to a WM_QUIT signal) cleanUp(); // Do some housekeeping before quitting return msg.wParam; // Return the WM_QUIT message to Windows. End of program } LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) // Sorts through messages { case WM_DESTROY: // When window has been closed { PostQuitMessage(0); // Closes the application return 0; } break; } return DefWindowProc (hWnd, // Returns any messages the switch statement didn't pick up message, wParam, lParam); } void initDirectX(HWND hWnd) { d3d = Direct3DCreate9(D3D_SDK_VERSION); // Create DirectX9 interface D3DPRESENT_PARAMETERS d3dParameters; // Pointer to DirectX9 parameters ZeroMemory(&amp;d3dParameters, sizeof(d3dParameters)); // Clears the structure so we can use it d3dParameters.Windowed = FALSE; // Fullscreen d3dParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; // Get rid of old framess d3dParameters.hDeviceWindow = hWnd; // Sets the window to be used by Direct3D d3dParameters.BackBufferFormat = D3DFMT_X8R8G8B8; // Sets the back buffer format to 32-bit d3dParameters.BackBufferWidth = SCREEN_WIDTH; // Sets the width of the buffer d3dParameters.BackBufferHeight = SCREEN_HEIGHT; // Sets the height of the buffer d3d-&gt;CreateDevice(D3DADAPTER_DEFAULT, // Creates a device class D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &amp;d3dParameters, &amp;d3dDevice); D3DXCreateSprite(d3dDevice, &amp;d3dSprite); } void render() { d3dDevice-&gt;Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(10, 0, 32), 1.0f, 0); // Clears the screen to a dark blue d3dDevice-&gt;BeginScene(); // Begins Direct3D scene d3dSprite-&gt;Begin(D3DXSPRITE_ALPHABLEND); // Begin sprite drawing background.draw(); counter.draw(); gameStations.draw(); consoleCab.draw(); clothesStand.draw(); gamePanel.updateInformation(564.55, 800.56, 1034.54, "July"); for (std::vector&lt;Customer*&gt;::iterator customerIT = customers.begin(); customerIT != customers.end(); customerIT++) { (*customerIT)-&gt;draw(); } gamePanel.draw(); d3dSprite-&gt;End(); // End drawing d3dDevice-&gt;EndScene(); // Ends the Direct3D scene d3dDevice-&gt;Present(NULL, NULL, NULL, NULL); // Presents the Direct3D scene (Displays to screen) } void cleanUp() { d3dDevice-&gt;Release(); // Closes the Direct3D device and releases memory d3d-&gt;Release(); // Closes Direct3D and releases memory } </code></pre> <p>I apologise in advance if I have not explained something properly or missed something out. I am notoriously bad for trying to explain things! :/ Can anyone help me out? Beginning to think I should have stuck with XNA for this project and learnt about pointers and vectors a little more in depth later on... too late now though :(</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload