Jump to content
  • Advertisement
Sign in to follow this  
Joshnathan

Problem drawing my "Map"

This topic is 4971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, here I am again (what is c++ hard lol). I have spent about 2 hours already trying to figure out what I did wrong in my function to draw my map. here is the function:
void DessinerCarte()
{  
    Carte.rect.Left = 5;
	Carte.rect.Top = 5;
	Carte.rect.Right = 5 + (Carte.largeur - 1);
	Carte.rect.Bottom = 5 + (Carte.hauteur - 1);
	Carte.handle_output = GetStdHandle(STD_OUTPUT_HANDLE);
	Carte.handle_input = GetStdHandle(STD_INPUT_HANDLE);
    COORD BufferSize = {Carte.largeur, Carte.hauteur};
    COORD BufferCoord = {0, 0};
    WriteConsoleOutput(Carte.handle_output, Carte.ScreenBuffer, BufferSize, BufferCoord, &(Carte.rect));  
    Sleep(1000);
} 

here you have the struct:
struct CARTE
{
HANDLE handle_input;
HANDLE handle_output;
DWORD events;
INPUT_RECORD input_record;
    int largeur;
    int hauteur;
    CHAR_INFO *ScreenBuffer;
    SMALL_RECT rect;
};

I also have another function that "loads" the map, which works fine.
bool ChargerCarte()    
{
    ifstream file;
    file.open("carte.txt");
    if(!file)
    {
        cout<<"Erreur en dessinant la carte";
        Sleep(1000);
        return false;
    }    
    file >> Carte.largeur;
    file >> Carte.hauteur;
    Carte.ScreenBuffer = new CHAR_INFO[Carte.largeur * Carte.hauteur];
    int x = 0;
    int y = 0;
    char temp;
    while(y != Carte.hauteur)
    {
        
    file >> temp;
    switch(temp)
    {
        case '0':
            Carte.ScreenBuffer[x + y * Carte.largeur].Char.AsciiChar = '#';
            Carte.ScreenBuffer[x + y * Carte.largeur].Attributes = FOREGROUND_GREEN | BACKGROUND_BLUE;
            break;
        case '1':
            Carte.ScreenBuffer[x + y * Carte.largeur].Char.AsciiChar = '#';
            Carte.ScreenBuffer[x + y * Carte.largeur].Attributes = FOREGROUND_RED | BACKGROUND_BLUE;
            break;
        default:
            file.close();
            cout<<"Pas possibe de charger la carte..";
            return false;
            break;
    }  
    x++;
        if(x == Carte.largeur)
            {
            x = 0;
            y++;
            }
    }       
    file.close();   
    return true;
}

the problem is that the moment I call the "DessinerCarte()", I wait 1 second (I guess that is the effect of the Sleep(1000)) and then the program shuts down :(. does anyone know where my problem is? I think it should be in "DessinerCarte()", but I am not sure. Thanks all for taking your time to try and answer this one ^^ Joshua

Share this post


Link to post
Share on other sites
Advertisement
Hmm....well if it actually successfully Sleeps for 1000 milliseconds like you say, then odds are that the problem is not with DessinerCarte(), though it does occur to me that when you send the parameter carte.ScreenBuffer to the WriteConsoleOutput function that it is possible it has not been initialized--though the WriteConsoleOutput() function may take care of this anyway......

But normally when a program crashs abruptly like this it means that you have dereferenced a null pointer somewhere. Since you say you just started learning C++ I'll try to explain what happens.

Any time you have a pointer (which you do in your struct), which is indicated by the * after the type.

So lets say we have:
int *p;
The variable p stores a memory location, which in turn is where an integer is stored.

If we just had :
int i;
Then i simply stores an integer.

If you want to make p point to a specific item, you could say:
p = &i; -- The & means "address of" --so in this case p = the address of integer i;

If you wanted to change the value that p points to, then you have to DEREFERENCE p. In c++, you do this by putting a * before it.

So if we have....

int i;
int *p; //declare a pointer p
p = &i; // set p = to the address of i
(*p) = 5; // the parenthesis are for emphasis not needed here


Now the integer i will equal 5.....

Anyway, another thing p can point to is NULL. This basically meams it points to nothing....Any time that you dereference a NULL pointer, the program will crash. Think of it as p points to nothing. By dereferencing it, you are trying to modify the value of nothing.

This is why a lot of times that people use pointers, they put in a safety check before using the star:

if (p != NULL) {
*p = whatever;
}



So somewhere in your program, there probably is a pointer which is being dereferenced despite pointing to NULL.

I hope my explanation is correct and makes sense.....Someone with more c++ experience maybe can correct me if i made some mistake.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!