Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Sprite object no longer renders when put in to a Vector


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 chris2307   Members   -  Reputation: 163

Like
0Likes
Like

Posted 03 February 2013 - 04:08 PM

Hi all, I created a player object and it worked fine. However, now I have put the player object in to a vector (as I want to add more of them later on) and it no longer renders to the screen. The object does exist because on feature of the player is that when you click on it, some player information is displayed above the sprite and this text information is still rendered when I click on the area the object is. I have tested the HRESULT codes coming back from draw() and D3DXCreateTextureFromFileEx() and both indicate success. Also, by clicking on the approximate area where the sprite is and moving the sprite around, I can tell that the sprite moves around as normal too. It just isn't displayed. Neither is this down to the render order - the sprite is rendered after everything in the program. Can anyone help with this?

 

Main

/**********************************************
*
* Title: Store Tycoon
* Author: Chris Ramsey
* Created: January 2013
*
*
*
***********************************************/

#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<Customer> customers;

Customer customer;
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(&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(&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(Customer());

    background.loadGraphics();
    counter.loadGraphics();
    gameStations.loadGraphics();
    consoleCab.loadGraphics();
    clothesStand.loadGraphics();
    gamePanel.loadGraphics();

    /************************************** Main game loop *************************************************/

    while(TRUE)                                         // Main game loop
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))     // If messages are waiting
        {
            TranslateMessage(&msg);                      // Translates the keystroke messages into the correct format
            DispatchMessage(&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<Customer>::iterator customerIT = customers.begin(); customerIT != customers.end(); customerIT++)
            {
                (*customerIT).checkInteractivity();
                (*customerIT).move();
            }

            //customer.checkInteractivity();
            //customer.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(&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->CreateDevice(D3DADAPTER_DEFAULT,                // Creates a device class
        D3DDEVTYPE_HAL,
        hWnd,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dParameters,
        &d3dDevice);

    D3DXCreateSprite(d3dDevice, &d3dSprite);
}

void render()
{
    d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(10, 0, 32), 1.0f, 0);   // Clears the screen to a dark blue
    d3dDevice->BeginScene();                                                         // Begins Direct3D scene
    d3dSprite->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<Customer>::iterator customerIT = customers.begin(); customerIT != customers.end(); customerIT++)
    {
        (*customerIT).draw();
    }

    gamePanel.draw();

    d3dSprite->End();                                                                // End drawing
    d3dDevice->EndScene();                                                           // Ends the Direct3D scene
    d3dDevice->Present(NULL, NULL, NULL, NULL);                                      // Presents the Direct3D scene (Displays to screen)
}

void cleanUp()
{
    d3dDevice->Release();   // Closes the Direct3D device and releases memory
    d3d->Release();         // Closes Direct3D and releases memory
}

 

Player Class

 

#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;

    RECT textBox;

    Playercard playercard;


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;

        playercard.load(name, male, age, cash, productWanted);
        loadGraphics();
    }


    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, &playerInfoCard);
        
/*        if(rc == D3D_OK)
        {
            diagFile.open("Diag.txt");
            diagFile.write("CreateTexture was okay", 100);
            diagFile.close();
        }*/

        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, &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
                   &font);                            // the font object    
    }

    void draw()
    {
        d3dSprite->Draw(texture, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
        
        if(displayPlayerCard)
        {
            playerCardPosition = position;
            playerCardPosition.y -= 128;
            playerCardPosition.x += 32;

            SetRect(&textBox, (int)playerCardPosition.x+5, (int)playerCardPosition.y+5, (int)playerCardPosition.x+256-5, (int)playerCardPosition.y+128-5);

            d3dSprite->Draw(playerInfoCard, NULL, &center, &playerCardPosition, D3DCOLOR_XRGB(255, 255, 255));
            font->DrawTextA(d3dSprite, playercard.plCard(), -1, &textBox, DT_LEFT, D3DCOLOR_XRGB(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, &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, &texture);
            walkingAlternator++;
        }

        if(walkingAlternator >= 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, &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, &texture);
            walkingAlternator++;
        }

        if(walkingAlternator >= 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, &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, &texture);
            walkingAlternator++;
        }

        if(walkingAlternator >= 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, &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, &texture);
            walkingAlternator++;
        }

        if(walkingAlternator >= 20)
        {
            walkingAlternator = 1;
        }
    }

    void checkInteractivity()
    {
        GetCursorPos(&curPos);
        if (GetKeyState(VK_LBUTTON) & 0x80 &&                        
            curPos.x >= position.x && curPos.x <= position.x+64 &&   
            curPos.y >= position.y && curPos.y <= position.y+128)    
        {
            displayPlayerCard = true;
        }
        else
        {
            displayPlayerCard = false;
        }
    }

    D3DXVECTOR3 returnPosition()
    {
        return position;
    }

    D3DXVECTOR3 returnPosition(bool returnCentreOfSprite)
    {
        return position;
    }

};

 

Thanks


Edited by chris2307, 03 February 2013 - 04:14 PM.


Sponsor:

#2 chris2307   Members   -  Reputation: 163

Like
0Likes
Like

Posted 03 February 2013 - 04:27 PM

Should probably mention that I have a headers file:
#ifndef HEADERS_H
#define HEADERS_H

#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <d3d9.h>
#include <d3dx9.h>
#include <string>
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <sstream>
#include <cmath>


#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

#define SCREEN_WIDTH  1024 
#define SCREEN_HEIGHT 768

LPDIRECT3D9 d3d;                 // Pointer to Direct3D interface
LPDIRECT3DDEVICE9 d3dDevice;     // Pointer to the device class
LPD3DXSPRITE d3dSprite;          // Pointer to Direct3D Sprite interface

std::ofstream diagFile;

#endif

Edited by chris2307, 03 February 2013 - 04:27 PM.


#3 chris2307   Members   -  Reputation: 163

Like
0Likes
Like

Posted 05 February 2013 - 11:11 AM

Anyone?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS