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;
}