Thread: Why Does My Program Crash?

  1. #1
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55

    Question Why Does My Program Crash?

    I finally got working on my snake game again. Right now, i've only gotten it to work to the point where you can move one piece of the snake around. Now i'm trying to get it to seem like a whole snake is moving. The way I'm doing it is that I'm deleting the back end of the snake and adding another one at the head. This probably isn't the best way to do it, so if you could tell me what would be a better way, then that would be good. But right now, the game is crashing right as it starts. Here's the code:

    Code:
    //Includes
    #include <allegro.h>
    #include<iostream>
    #include<string>
    #include <stdlib.h>
    #include <stdlib.h> 
    #include <cstdlib>
    #include <time.h>
    #include <vector>
    #include <complex>
    #include <math.h>
    #include <cmath>
    
    using namespace std;
    
    //Graphics
    BITMAP *snakeimg = NULL;
    BITMAP *buffer = NULL;
    
    //Sounds
    
    //Variables
    float x,y;
    float dir = 45;
    float dx = sin(dir);
    float dy = cos(dir);
    #define PI 3.14159265
    int maxsize = 3;
    
    
    //Structs
    struct snake {
    	int x;
    	int y;
    };
    
    struct fruit {
    	int x;
    	int y;
    };
    
    //Vectors
    vector<snake> snakes(0);
    
    //Functions
    void UpdateSnake();
    void TestKeys();
    
    int main(int argc, char argv[1])
    {
    	
    	allegro_init();
    	install_keyboard();
    	set_color_depth(24);
    	set_gfx_mode(GFX_AUTODETECT_WINDOWED,400,400,0,0);
    
    	//BITMAPS
    	snakeimg = load_bitmap("Snake.bmp",NULL);
    	buffer = create_bitmap(400,400);
    
    	while(!key[KEY_ESC])
    	{
    	clear_bitmap(buffer);
    	TestKeys();
    	UpdateSnake();
    	draw_sprite(screen,buffer,0,0);
    	}
    
    	allegro_exit();
    	return 0;
    
    	//Cleanup
    	for(int i = 0;i < snakes.size(); i++);
    	{
    		snakes.erase(snakes.begin()+i);
    	}
    	destroy_bitmap(snakeimg);
    	destroy_bitmap(buffer);
    }
    
    	
    END_OF_MAIN();
    
    void UpdateSnake()
    {
    	dx = sin(dir*PI/180);
    	dy = cos(dir*PI/180);
    	x = x + dx/5;
    	y = y + dy/5;
    
    	snake w;
    	w.x = x;
    	w.y = y;
    	snakes.push_back(w);
    
    	if(snakes.size() > maxsize)
    	{
    		snakes.erase(snakes.begin()+snakes.size());
    	}
    
    	for(int i = 0; i < snakes.size(); i++)
    	{
    		draw_sprite(buffer,snakeimg,snakes[i].x,snakes[i].y);
    	}
    }
    
    void TestKeys()
    {
    	if(key[KEY_LEFT])
    	{
    		dir = dir + .4; 
    	}
    	else if(key[KEY_RIGHT])
    	{
    		dir = dir - .4;
    	}
    }

  2. #2
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    you need to include windows.h and use a callback procedure for the key messages.

    in other words learn some win32 programming first....

    www.winprog.org is GREAT for beginners www.functionx.com is also ok and http://code.glowdot.com is AWESOME for advanced win32, need good C++ skills though
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  3. #3
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    need help? PM ME
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  4. #4
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    But it worked fine without including window.h before. I havn't added anything that I havn't done before without it.

  5. #5
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    oh, my bad your using allegro. sorry to bursty your bubble allegro is the easy awy out and can get pretty glitchy i stand buy what i said ditch allegro and go with good ol' win32 and pure C++ also why do you include cmath and math.h u shud only want math.h cmath is old version of math.h
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  6. #6
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    and dont go usin any of that MFC crap lol

    edit: these are just suggestions, its your choice really just tryin to help out
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  7. #7
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    I guess I don't know what win32 is... I didn't know you could do graphics with it. But for this project I'm going to stick with allegro, unless it gets reaaally buggy. I found that the problem is in this block :
    Code:
    if(snakes.size() > maxsize)
    {
       snakes.erase(snakes.begin()+snakes.size());
    }

  8. #8
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    great btw win32 isnt consoles its like windows and such.like internet explorer its win32 programmed win32 is the art of creating windows resources button edits etc.

    make a windows project and compile this:

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, WPARAM wParam, LPARAM lParam)
    {
        MessageBox("my Message Box", "Title of my message box", MB_OK | MB_ICONERROR);
    }
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  9. #9
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    make sure to add #include <windows.h> at the top
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  10. #10
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    IT gives me the error unexpected end of file while looking for precompiled header directive.

  11. #11
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    i hate typing callbacks ill give u alot of code to compile mmk *gawd* make sure you make a windows project

    Code:
    // basic window creation learn basic win32 @ www.winprog.org
    #include <windows.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
        UpdateWindow(hwnd);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    make sure you have a WINDOWS PROJECT

    Note: this may seem like alot of code... it is. but if u wanna add edit boxes and other stuff like that you need to add very little code. (Win32 Template made by Dev C++)
    Last edited by C+noob; 09-29-2005 at 04:11 PM.
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Grantyt3
    IT gives me the error unexpected end of file while looking for precompiled header directive.
    if you are using VC++ 6.0 or .NET compiler then either turn precompiled headers off or add #include "stdafx.h" at the top of the *.cpp file. You HAVE to turn them off for *.c files.

  13. #13
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    true but the error was my fault.

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, WPARAM wParam, LPARAM lParam)

    WPARAM and LPARAM dont belong in there i was thinking callbacks replaace tht with:

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    C+noob: stop babbling with these off-topic win32 comments. It's retarded. You're comment about Allegro was entirely unfounded and your just spitting out BS and making a fool of yourself. As to the original question:

    Code:
    	return 0;
    
    	//Cleanup
    	for(int i = 0;i < snakes.size(); i++);
    	{
    		snakes.erase(snakes.begin()+i);
    	}
    	destroy_bitmap(snakeimg);
    	destroy_bitmap(buffer);
    I don't believe that cleanup is ever reached. You return 0 before you get there.
    The real problem, however, as you had mentioned

    Code:
    snakes.erase(snakes.begin()+snakes.size());
    I think is that, snakes.begin() returns a pointer to the first element of the vector, which you add snakes.size() to (which returns 1, 2, 3, whatev) and it crashes.

  15. #15
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    no need to flame and yea allegro is a piece of ........
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 08:17 PM
  2. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 04:06 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM