Allegro 5 game crashing

Started by
5 comments, last by cardinal 10 years, 5 months ago

Hello,

I'm coding some game, but I got into trouble. The game runs smoothly at the first run but when I close it and compile again I get this error:

"Run-Time Check Failure #3 - The variable 'degrees' is being used without being initialized.
If there is a handler for this exception, the program may be safely continued."

Heres my code:


#include<allegro5\allegro5.h>
#include<allegro5\allegro_native_dialog.h>
#include<allegro5\allegro_image.h>
#include <iostream>
#include <cmath>
	using namespace std;
 
#define ScreenWidth 800
#define ScreenHeight 600
#define PI 3.14159265


int main()
{

    ALLEGRO_DISPLAY *display;

    enum Direction { DOWN, LEFT, RIGHT, UP };
 
    const float FPS = 60.0;
    const float frameFPS = 15.0;

    if(!al_init())
        al_show_native_message_box(NULL, "Error", NULL, "Could not Initialize Allegro", NULL, NULL);
     
    display = al_create_display(ScreenWidth, ScreenHeight);
 
    if(!display)
        al_show_native_message_box(NULL, "Error", NULL, "Could not create Allegro Display", NULL, NULL);
 
    al_set_window_position(display, 200, 200);
	


	


    bool done = false, draw = true, active = false;
    float x = 360, y = 280, moveSpeed = 5;
    int dir = DOWN, sourceX = 32, sourceY = 0, mx, my; //mx , my - mouse position
	long double degrees;
 
	al_install_mouse();
    al_install_keyboard();
    al_init_image_addon();

	ALLEGRO_BITMAP *player = al_load_bitmap("plane.png");
	
	

 
    ALLEGRO_KEYBOARD_STATE keyState;
    ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
    ALLEGRO_TIMER *frameTimer = al_create_timer(1.0 / frameFPS);
 
    ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    al_register_event_source(event_queue, al_get_timer_event_source(frameTimer));
    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_mouse_event_source());
 
    al_start_timer(timer);
    al_start_timer(frameTimer);

	
	

    while(!done)
    {
		
		ALLEGRO_EVENT events;
        al_wait_for_event(event_queue, &events);
		al_get_keyboard_state(&keyState);

        if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            done = true;
        }
		else if(events.type == ALLEGRO_EVENT_MOUSE_AXES)
        {
			mx = events.mouse.x;
            my = events.mouse.y;
			if(mx > x && my > y) 
			{
				degrees = ((3*PI)/2) + atan2(mx-x,y-my);
				cout<< "Case 1: " << degrees << endl;
			}

			if(mx < x && my > y) 
			{
				degrees = (PI + atan2(y-my,x-mx));
				cout<< "Case : " << degrees << endl;
			}

			if(mx < x && my < y) 
			{
				degrees = (PI/2) + atan2(x-mx,my-y);
				cout<< "Case 3: " << degrees << endl;
			}

			if(mx > x && my < y) 
			{
				degrees =  (atan2(my-y,mx -x));
				cout<< "Case 4: " << degrees << endl;
			}


            
			cout << mx << endl << my << endl;
        }
        else if (events.type == ALLEGRO_EVENT_TIMER)
        {
            if(events.timer.source == timer)
            {
                active = true;
				if(al_key_down(&keyState, ALLEGRO_KEY_LEFT) && al_key_down(&keyState, ALLEGRO_KEY_DOWN))
				{
					x -= moveSpeed;
					y += moveSpeed;
				}
				else if(al_key_down(&keyState, ALLEGRO_KEY_RIGHT) && al_key_down(&keyState, ALLEGRO_KEY_DOWN))
				{
					x += moveSpeed;
					y += moveSpeed;
				}
				else if(al_key_down(&keyState, ALLEGRO_KEY_LEFT) && al_key_down(&keyState, ALLEGRO_KEY_UP))
				{
					x -= moveSpeed;
					y -= moveSpeed;
				}
				else if(al_key_down(&keyState, ALLEGRO_KEY_RIGHT) && al_key_down(&keyState, ALLEGRO_KEY_UP))
				{
					x += moveSpeed;
					y -= moveSpeed;
				}
                else if(al_key_down(&keyState, ALLEGRO_KEY_DOWN))
                {
                    y += moveSpeed;
                    dir = DOWN;
                }
                else if(al_key_down(&keyState, ALLEGRO_KEY_UP))
                {
                    y -= moveSpeed;
                    dir = UP;
                }
                else if(al_key_down(&keyState, ALLEGRO_KEY_RIGHT))
                {
                    x += moveSpeed;
                    dir = RIGHT;
                }
                else if(al_key_down(&keyState, ALLEGRO_KEY_LEFT))
                {
                    x -= moveSpeed;
                    dir = LEFT;
                }
			
                else
                    active = false;
			  
 
                
            }
			
 
            else if (events.timer.source == frameTimer)
            {
                if(active)
                    sourceX += al_get_bitmap_width(player) / 3;
                else
                    sourceX = 32;
 
                if(sourceX >= al_get_bitmap_width(player))
                    sourceX = 0;
 
                sourceY = dir;
            }
 
            draw = true;
        }
        if(draw)
        {
			al_draw_rotated_bitmap(player, 32 , 27 , x, y, degrees, 0);
			al_convert_mask_to_alpha(player, al_map_rgb( 255, 255, 255 ) );
            al_flip_display();
            al_clear_to_color(al_map_rgb(0, 0, 0));
        }
    }
    al_destroy_display(display);
    al_destroy_timer(timer);
	al_destroy_timer(frameTimer);
    al_destroy_bitmap(player);
    al_destroy_event_queue(event_queue);
	
    return 0;
}
Advertisement

Well, you can try initializing the variable with something, say, zero, or whatever you find more convenient.

long double degrees;

becomes

long double degrees = 0;

It'll probably solve your problem. But it is a weird report however. What compiler are you using?

There's a case where it goes through all possible initializations and ends up being used without being initialized.

Yes...what compiler (or runtime) are you using?...that's an odd error...
Im using Visual Studio 2012. Im confused is what you ask for?

what version of allegro are you using?

i have to say that initialize your variables before use them

when you can't see well like me, you can't test your applications and you can't read something

Github

5.0.10 as I remember.

Just got back from school, made some changes and tested it out. Seems the problem is solved, in case I will get errors, I will back here.

Solution: @dajaime degrees = 0;

Thank you everyone.

This isn't a weird error at all. Degrees was initially uninitialized. This isn't a visual studio or allegro problem. It's a coding problem.

It gets initialized in the conditional mouse handling code. There is no guarantee this code will be executed as far as the compiler is concerned.

It is then used here:

al_draw_rotated_bitmap(player, 32 , 27 , x, y, degrees, 0);

Without explicitly being initialized.

These are the types of cases that usually produce release mode only bugs: "I swear my game runs find in debug" because in debug mode memory is often filled with zeros, but in release configurations the memory is left uninitialized (for performance reasons) so your variables will hold whatever garbage was already in those sections of memory.

You should always properly initialize all variables.

I'm not sure what is doing the exception handling. But there is probably a warning in your compiler output indicating the same thing.

This topic is closed to new replies.

Advertisement