• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Sprite fails to draw when calling my draw() method

4 posts in this topic

Hi All,


I am trying to draw a sprite to the screen. So far, when making small games I have loaded textures and drawn them in a very un-OO way and just used OO techniques for the game mechanics. Now, I wanted to load the textures in to the object and draw them from their own method too. I wrote what I thought would work but the sprite is not being drawn to the screen. The background get drawn fine and there are no compile or runtime errors. Can someone spot what I am doing wrong please?



#define HEADERS_H

#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <d3d9.h>
#include <d3dx9.h>
#include <string>

#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




[CODE]#include "Headers.h";

class Customer{


    D3DXVECTOR3 position;
    D3DXVECTOR3 center;


        D3DXCreateTextureFromFileEx(d3dDevice, "man.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT,
        D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &textureT);

        D3DXVECTOR3 position (10.0f, 10.0f, 0.0f);
        D3DXVECTOR3 center (0.0f, 0.0f, 0.0f);

    void Draw()
        d3dSprite->Draw(textureT, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));




* Title: DirectX 9.0 Standard Fullscreen Template
* Author: Chris Ramsey
* Created: November 2012
* This program is a standard template for a full screen
* directX 9.0 application.
* Any code from here can be used freely by
* anyone for any reason.

#include "Headers.h."
#include "Customer.h"

Customer customer1;

// DirectX 3D Vectors used for sprite positions

D3DXVECTOR3 center, backgroundP;

// Textures


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

    /************************************** 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


    // 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;  
    return DefWindowProc (hWnd,                         // Returns any messages the switch statement didn't pick up

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

    D3DXCreateSprite(d3dDevice, &d3dSprite);

    D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);

    D3DXCreateTextureFromFileEx(d3dDevice, "background2.png", D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT,
        D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, &backgroundT);

    D3DXVECTOR3 backgroundP(0,0,0);

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
    d3dSprite->Draw(backgroundT, NULL, &center, &backgroundP, D3DCOLOR_XRGB(255, 255, 255));
    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




Share this post

Link to post
Share on other sites

Drop a break point on the draw method and inspect the parameters (Seems like they should be fine, but wouldn't hurt to look).

Also, capture the return from the draw call. From MSDN: "If the method succeeds, the return value is S_OK. If the method fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DXERR_INVALIDDATA."

Unfortunately I'm not familiar enough with DX to be of much more assistance then that.


Share this post

Link to post
Share on other sites

Hi, thanks for the reply.


I have checkted and the draw call is definitely failing and the return code I am getting is 1 (I have not been able to find a way of telling whether it is a D3DERR_INVALIDCALL or D3DXERR_INVALIDDATA error.


One thing I did think, when drawing a sprite to the screen, we must put the call to the draw() method in between:



// ...  


Am I correct in thinking I can just put the call to the objects draw method in between these two and it will still work?


Share this post

Link to post
Share on other sites

I would recommend that you switch the erroneous return value through a block to see exactly what the error is.  (case D3DERR_INVALIDCALL, case D3DXERR_INVALIDDATA, default).  In most cases when you know the error the answer is just a google search away.  I would also suggest that ALL of the rendering methods be called from within your sprite's Draw method.  From what I can tell you have the main rendering loop calling 3+ methods on each sprite, you could simply be losing return values or passing invalid pointers around as you go and it might be easier to fix, debug and work with if all of the "rendering logic" for each sprite is contained within a single method such as your sprites draw method.


Share this post

Link to post
Share on other sites

I figured it! And rather obvious when you think about it too.


I used a switch statement as you suggested and found that I was getting a D3DERR_INVALIDCALL. Went back and looked at the way in which I was initialising my parameters. Soon realised I was creating my sprite object before I ran my initDirectX method. So, in turn I wasn't actually loading any textures or positions in the first place when I thought I was.


Anyway, thanks for the help. Took a while but finally got there!


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0