Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have to write a game in graphics.h for school, that's why I use this old header, but that's not important now.

The problem is that after I run this for about 10 seconds, it freezes. As in, stops working at all, and is only responsive to me closing it with the windows X button. I have tried debugging it and it returns SIGSEGV Segmentation fault, and when I open the stack call debugging window every time it crashes it's because of another place where the fault had occurred. I ran it three times, once it showed me that the problem was with main(), specifically cleardevice(), afterwards with drawButton() and then with something inside the menu().

After reading on wikipedia and on Stack Overflow about possible issues with the SIGSEGV signal I've reached the conclusion that is either because of a pointer or a char array, but I don't see where my pointers and char arrays might go bananas.

Also a few other things to consider:

I'm using code::blocks
I've tried using a string instead of a char array, if you want to suggest that please also tell me how I'd have to do it as well
I am perfectly aware I should have probably pasted a dumbed down version of my code, but this didn't happen in the past when it was shorter, so I assume it might also have to do with its complexity.

C++
#include <graphics.h>
#include <windows.h>
#include <winuser.h>
#include <string>

using namespace std;

POINT p;
POINT p1;
const int winx = 1200;
const int winy = 800;
int mainwin = initwindow(winx, winy, "Tower Blaster", 400, 100, false);
int gamestate = 0;

struct Button
{
    int x, y, wid, hig;
    char txt[100];
};

void drawButton(Button button)
{
    rectangle(button.x, button.y, button.x+button.wid, button.y+button.hig);
    outtextxy(button.x+button.wid/2, button.y+button.hig/2, button.txt);
}

bool buttonClick(Button button, POINT &p)
{
    if((p.x>=button.x) && (p.x<=button.x+button.wid) && (p.y>=button.y) && (p.y<=button.y+button.hig))
    {
        Button overlay = {button.x+10, button.y+10, button.wid-20, button.hig-20, " "};
        drawButton(overlay);
        if(GetAsyncKeyState(VK_LBUTTON)) return true;
    }
    else return false;
}

void game()
{

}

void rules()
{
    settextstyle(COMPLEX_FONT, HORIZ_DIR, 2);
    outtextxy(50, 50, "How to play:");
    settextstyle(COMPLEX_FONT, HORIZ_DIR, 1);
    outtextxy(50, 90, "When the game starts, you'll see two towers, yours and your enemy's.");
    outtextxy(50, 130, "The towers are in a certain order. Your job is to make every floor have a value smaller");
    outtextxy(50, 150, "than the one under it");
    outtextxy(50, 190, "You will achieve that by choosing between the floors presented to you in the middle of the");
    outtextxy(50, 210, "screen. You have two choices: the one on the left, discarded by the enemy, and the one on");
    outtextxy(50, 230, "the right which is a \"mystery\" block.");
    outtextxy(50, 270, "Whichever of you finishes putting the tower in order wins. Good luck!");
    Button exit = {50, 500, 100, 50, "Back to main menu"};
    drawButton(exit);
    if(buttonClick(exit, p)) gamestate = 0;
}

void gamemode()
{

}

void menu()
{
    Button start = {winx/2-100, winy/2-150, 200, 100, "Start"};
    drawButton(start);
    Button quit = {winx/2-100, winy/2, 200, 100, "Quit game"};
    drawButton(quit);
    Button rules = {winx/2+50, winy/2+150, 100, 50, "How to play"};
    drawButton(rules);
    if(buttonClick(quit, p)) {closegraph(mainwin); exit(0);}
    if(buttonClick(start, p)) gamestate = 1;
    if(buttonClick(rules, p)) gamestate = 2;

}


int main()
{
    int page = 0;
    settextstyle(COMPLEX_FONT, HORIZ_DIR, 1);
    while(1){
        //if(GetAsyncKeyState(0x52)) gamestate = 0;
        GetCursorPos(&p);
        ScreenToClient(GetForegroundWindow(), &p);
        char txt[200];

        sprintf(txt, "Position of mouse: %i %i \n", p.x, p.y);
        outtext(txt);
        if(GetAsyncKeyState(VK_LBUTTON)) outtext("Click!");
        switch(gamestate)
        {
            case 0: menu(); break;
            case 1: gamemode(); break;
            case 2: rules(); break;
        }
        swapbuffers();
        cleardevice();

    }
    return 0;
}


What I have tried:

1. Debugging the code with call stack, it always changes what the problem is and doesn't tell me anything
2. Googling the answer, didn't find anything for graphics.h
3.
Posted
Updated 13-Dec-20 7:53am

You appear to be trying to mix Windows graphical code with Console code. That will most likely cause problems. Either use only the features of the old DOS-style graphics library, or switch to proper Win32 and GDI or GDI+.
 
Share this answer
 
I would do two things : first, move the declaration of txt out of the loop. It does not need to be created in every iteration of the loop. Second, start commenting things off to see where the culprit is. Begin with something like this :
C++
int main()
{
//    int page = 0;
    char txt[200];
    settextstyle(COMPLEX_FONT, HORIZ_DIR, 1);
    while(1)
    {
        //if(GetAsyncKeyState(0x52)) gamestate = 0;
        GetCursorPos(&p);
        ScreenToClient(GetForegroundWindow(), &p);

        sprintf(txt, "Position of mouse: %i %i \n", p.x, p.y);
        outtext(txt);

        if( GetAsyncKeyState(VK_LBUTTON) )
            outtext("Click!");

//        switch(gamestate)
//        {
//            case 0: menu(); break;
//            case 1: gamemode(); break;
//            case 2: rules(); break;
//        }
//        swapbuffers();
//        cleardevice();
    }
    return 0;
}
and make sure that works for a few minutes at least. Then enable more and more of the other code until it starts failing again. If you do this gradually you should be able to conclude that the last thing you enabled causes the problem and focus your debugging efforts there.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900