Sign in to follow this  

Problem drawing my "Map"

Recommended Posts

Joshnathan    124
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));  

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;"carte.txt");
        cout<<"Erreur en dessinant la carte";
        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;
        case '0':
            Carte.ScreenBuffer[x + y * Carte.largeur].Char.AsciiChar = '#';
            Carte.ScreenBuffer[x + y * Carte.largeur].Attributes = FOREGROUND_GREEN | BACKGROUND_BLUE;
        case '1':
            Carte.ScreenBuffer[x + y * Carte.largeur].Char.AsciiChar = '#';
            Carte.ScreenBuffer[x + y * Carte.largeur].Attributes = FOREGROUND_RED | BACKGROUND_BLUE;
            cout<<"Pas possibe de charger la carte..";
            return false;
        if(x == Carte.largeur)
            x = 0;
    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
GameDev135    133
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

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