Hi I dont understand why this int keeps corrupting ::Draw works fine but the variable m_EndRow ends up as a funny number. The class is supposed to print debug info to my screen. ::AddText adds a row of text which is then printed in DrawTable. Simple problem if you already know - but I don't!!

CText.h

#ifndef CText_h
#define CText_h

#include <d3dx9.h>

#define MAX_TEXT_ROWS 10

class CText
{
public: 
    CText(void);
    ~CText(void);

    void Create(void);
    int  AddText(char* cText);
    void Draw(void);
    void DrawTable(void);

private:

    LPD3DXFONT  m_DXFontMaster;   // pointer to the font object

    RECT        m_TextBoxMaster;        // rect to contain the text

    struct TEXTROW_TYPE 
    {
        char*       m_cText;    // the text to be displayed 
        LPD3DXFONT  m_DXFont;   // pointer to the font object
        RECT        m_TextBox;
    };

    TEXTROW_TYPE*   m_pTextRow[MAX_TEXT_ROWS]; // array of structs

    int             m_EndRow;
    //int               m_iMaxRows;

};// CText

#endif // CText_h

CText.cpp

#include "CText.h"
#include "CApplication.h"

// ********************************************************************* //
// Name: CText                                                           //
// Description: Constructor                                              //
// ********************************************************************* //
CText::CText(void)
{
    m_EndRow = 0;

}// CText

// ********************************************************************* //
// Name: Create                                                          //
// Description: Constructor                                              //
// ********************************************************************* //
void CText::Create(void)
{
    m_EndRow = 0;

    for (int i = 0; i <= MAX_TEXT_ROWS; i++) m_pTextRow[i] = new TEXTROW_TYPE;

    D3DXCreateFont(g_App.GetDevice(),           // the D3D Device
                   30,                          // font height of 30
                   0,                           // default font width
                   FW_NORMAL,                   // font weight
                   1,                           // not using MipLevels
                   true,                        // italic font
                   DEFAULT_CHARSET,             // default character set
                   OUT_DEFAULT_PRECIS,          // default OutputPrecision,
                   DEFAULT_QUALITY,             // default Quality
                   DEFAULT_PITCH | FF_DONTCARE, // default pitch and family
                   (LPCSTR)"Arial",             // use Facename Arial
                   //&m_pTextRow[m_EndRow]->m_DXFont);              // the font object
                   &m_DXFontMaster);                // the font object

    m_pTextRow[0]->m_DXFont = m_DXFontMaster;
}// Create

// ********************************************************************* //
// Name: Draw                                                            //
// Description: Display all the texts stored                             //
// ********************************************************************* //
int CText::AddText(char* cText)
{
    if (m_EndRow <= MAX_TEXT_ROWS)
    {
                                                 // create a RECT to contain the text
        SetRect(&m_pTextRow[m_EndRow]->m_TextBox, 0, 0, 640, 480); 
        m_pTextRow[m_EndRow]->m_cText = cText; // add text to the table
        m_pTextRow[m_EndRow]->m_DXFont = m_pTextRow[0]->m_DXFont;
        m_EndRow++;                          // increase to the next row
        return (1);                              // return success
    }
    else 
        return (0);

}// AddText

// ********************************************************************* //
// Name: Draw                                                            //
// Description: Display Hello World                                      //
// ********************************************************************* //
void CText::Draw(void)
{
    // create a RECT to contain the text
    SetRect(&m_TextBoxMaster, 0, 0, 640, 480); 

    // draw the Hello World text
    m_pTextRow[0]->m_DXFont->DrawTextA(NULL,
                             "Hello World...FINALLY!",
                             22,
                             &m_TextBoxMaster,
                             DT_CENTER | DT_VCENTER,
                             D3DCOLOR_ARGB(255, 255, 255, 255));
}// Draw

// ********************************************************************* //
// Name: DrawTable                                                       //
// Description: Display all the texts stored                             //
// ********************************************************************* //
void CText::DrawTable(void)
{
    if (m_EndRow <= MAX_TEXT_ROWS)
    {
        for (int i = 0; i <= MAX_TEXT_ROWS; i++)
            // draw the Hello World text
            m_pTextRow[i]->m_DXFont->DrawTextA(NULL,
                                    m_pTextRow[i]->m_cText,
                                    strlen(m_pTextRow[i]->m_cText),
                                    &m_pTextRow[i]->m_TextBox,
                                    DT_CENTER | DT_VCENTER,
                                    D3DCOLOR_ARGB(255, 255, 255, 255));
    }
};


// ********************************************************************* //
// Name: ~CText                                                          //
// Description: Destructor                                               //
// ********************************************************************* //
CText::~CText(void)
{
    if(m_pTextRow[0]->m_DXFont)
    {
        m_pTextRow[0]->m_DXFont->Release();
        m_pTextRow[0]->m_DXFont=NULL;
    }
}// ~CText

line 22: The loop is running once too many times which overflows the array. Should be
i<MAX_TEXT_ROWS and not i<=MAX_TEXT_ROWS. Same problem in other similar lines in your program. Arrays always start at 0, so the last valid member of an array is MAX_TEXT_ROWS-1.

Thanks that was something I hadn't thought of. Im gonna clean up this code now as its badly written.
My great thanks to you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.