#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include "objects.h"
#include <math.h>
//objects.h
struct RGB
{
int ID;
int x;
int y;
int speed;
int boundxmin;
int boundxmax;
int boundymin;
int boundymax;
int cot[8];
};
//objects.h end
//Gloabal
//constans
const int WIDTH = 640;
const int HEIGHT = 480;
const int FPS = 6;
//constans of conditions
const int rd[] = { 1/*RED*/,1/*GREEN*/,1/*BLUE*/ };
const int gn[] = { 1, 1, 1 };
const int be[] = { 1, 1, 1 };
// constans of elements
const int NUM_RED = 20;
const int NUM_GREEN = 20;
const int NUM_BLUE = 20;
//Red
const int rrad = 30; //radius of red
const int rspeed = 3;//speed of red
const int rxmin = 4; //boundx min and boundx max
const int rxmax = 15;
const int rymin = 4;//boundy min and boundy max
const int rymax = 15;
//Green
const int grad = 30; //radius of green
const int gspeed = 3;//speed of green
const int gxmin = 4;//boundx min and boundx max
const int gxmax = 15;
const int gymin = 4;//boundy min and boundy max
const int gymax = 15;
//Blue
const int brad = 30; //radius of blue
const int bspeed = 3;//speed of green
const int bxmin = 4;//boundx min and boundx max
const int bxmax = 15;
const int bymin = 4;//boundy min and boundy max
const int bymax = 15;
//procedures
void InitRed(RGB red[], int size);
void DrawRed(RGB red[], int size);
void RNear(RGB red[], int rSize, RGB green[], int gSize, RGB blue[], int bSize);
void MoveRtoR(RGB red[], int size);
void MoveRtoG(RGB red[], int rSize, RGB green[], int gSize);
void MoveRtoB(RGB red[], int rSize, RGB blue[], int bSize);
void InitGreen(RGB green[], int rSize, int gSize);
void DrawGreen(RGB green[], int size);
void GNear(RGB red[], int rSize, RGB green[], int gSize, RGB blue[], int bSize);
void MoveGtoG(RGB green[], int size);
void MoveGtoR(RGB green[], int gSize, RGB red[], int rSize);
void MoveGtoB(RGB green[], int gSize, RGB blue[], int bSize);
void InitBlue(RGB blue[], int rSize, int gSize, int bSize);
void DrawBlue(RGB blue[], int size);
void BNear(RGB red[], int rSize, RGB green[], int gSize, RGB blue[], int bSize);
void MoveBtoB(RGB blue[], int size);
void MoveBtoG(RGB blue[], int bSize, RGB green[], int gSize);
void MoveBtoR(RGB blue[], int bSize, RGB red[], int gSize);
//variables and arrays
int RRNear[NUM_RED];
int RGNear[NUM_RED];
int RBNear[NUM_RED];
int R[NUM_RED];
int GGNear[NUM_GREEN];
int GRNear[NUM_GREEN];
int GBNear[NUM_GREEN];
int G[NUM_GREEN];
int BBNear[NUM_BLUE];
int BRNear[NUM_BLUE];
int BGNear[NUM_BLUE];
int B[NUM_BLUE];
int main(void)
{
bool done = false;
bool redraw = true;
RGB red[20];
RGB green[20];
RGB blue[20];
for (int mole = 0; mole < NUM_RED; mole++)
{
RRNear[mole] = 0;
RBNear[mole] = 0;
RGNear[mole] = 0;
}
for (int mole = 0; mole < NUM_GREEN; mole++)
{
GGNear[mole] = 0;
GRNear[mole] = 0;
GBNear[mole] = 0;
}
for (int mole = 0; mole < NUM_BLUE; mole++)
{
BBNear[mole] = 0;
BRNear[mole] = 0;
BGNear[mole] = 0;
}
ALLEGRO_DISPLAY* display = NULL;
ALLEGRO_EVENT_QUEUE* event_queue = NULL;
ALLEGRO_TIMER* timer = NULL;
if (!al_init())
return -1;
display = al_create_display(WIDTH, HEIGHT);
if (!display)
return -1;
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);
InitRed(red, NUM_RED);
InitGreen(green, NUM_RED, NUM_GREEN);
InitBlue(blue, NUM_RED, NUM_GREEN, NUM_BLUE);
al_init_primitives_addon();
al_install_keyboard();
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_start_timer(timer);
while (!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER)
{
RNear(red, NUM_RED, blue, NUM_BLUE, green, NUM_GREEN);
GNear(red, NUM_RED, blue, NUM_BLUE, green, NUM_GREEN);
BNear(red, NUM_RED, blue, NUM_BLUE, green, NUM_GREEN);
MoveRtoR(red, NUM_RED);
MoveRtoG(red, NUM_RED, green, NUM_GREEN);
MoveRtoB(red, NUM_RED, blue, NUM_BLUE);
MoveGtoG(green, NUM_GREEN);
MoveGtoR(green, NUM_GREEN, red, NUM_RED);
MoveGtoB(green, NUM_GREEN, blue, NUM_BLUE);
MoveBtoB(blue, NUM_BLUE);
MoveBtoG(blue, NUM_BLUE, green, NUM_GREEN);
MoveBtoR(blue, NUM_BLUE, red, NUM_RED);
}
else if (ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch (ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
}
}
else if (ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch (ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
}
}
DrawRed(red, NUM_RED);
DrawGreen(green, NUM_GREEN);
DrawBlue(blue, NUM_BLUE);
al_clear_to_color(al_map_rgb(255, 255, 255));
al_flip_display();
}
al_destroy_display(display);
al_destroy_timer(timer);
al_destroy_event_queue(event_queue);
return 0;
}
//Procedures of Red
void InitRed(RGB red[], int size)
{
for (int i = 0; i < size; i++)
{
red[i].ID = i;
red[i].speed = rspeed;
red[i].boundxmin = rxmin;
red[i].boundymin = rxmax;
red[i].boundxmax = rymin;
red[i].boundymax = rymax;
for (int j = 0; j < 8; j++)
red[i].cot[j] = 0;
red[i].x = rand() % WIDTH;
red[i].y = rand() % HEIGHT;
}
}
void DrawRed(RGB red[], int size)
{
for (int i = 0; i < size; i++)
al_draw_filled_circle(red[i].x, red[i].y, rrad, al_map_rgb(128, 0, 0));
}
void RNear(RGB red[], int rSize, RGB green[], int gSize, RGB blue[], int bSize)
{
double re[NUM_RED];
double gr[NUM_RED];
double bl[NUM_RED];
for (int i = 0; i < rSize; i++)
{
re[i] = 0;
gr[i] = 0;
bl[i] = 0;
}
//Searsh Red nearly
if (rd[0])
{
for (int i = 0; i < rSize; i++)
for (int j = 0; j < rSize - 1; i++)
{
bool kot = false;
for (int k = 0; k < 8; k++)
if (red[i].cot[k] = red[j].ID)
kot = true;
if (!kot)
{
double a = sqrt(pow((red[j + 1].x - red[i].x), 2) + pow((red[j + 1].y - red[i].y), 2));
double b = sqrt(pow((red[j + 2].x - red[i].x), 2) + pow((red[j + 2].y - red[i].y), 2));
if (a > b)
{
RRNear[i] = j + 2;
re[i] = b;
}
else if (a < b)
{
RRNear[i] = j + 1;
re[i] = a;
}
else if (a = b)
{
RRNear[i] = j + 1;
re[i] = a;
}
}
else
break;
}
}
//Searsh Green nearly
if (rd[1])
{
for (int i = 0; i < rSize; i++)
for (int j = 0; j < gSize; i++)
{
bool kot = false;
for (int k = 0; k < 8; k++)
if (red[i].cot[k] = green[j].ID)
kot = true;
if (!kot)
{
double a = sqrt(pow((green[j].x - red[i].x), 2) + pow((green[j].y - red[i].y), 2));
double b = sqrt(pow((green[j + 1].x - red[i].x), 2) + pow((green[j + 1].y - red[i].y), 2));
if (a > b)
{
RGNear[i] = j + 2;
gr[i] = b;
}
else if (a < b)
{
RGNear[i] = j+1;
gr[i] = a;
}
else if (a = b)
{
RGNear[i] = j+1;
gr[i] = a;
}
}
}
}
//Searsh Blue nearly
if (rd[2])
{
for (int i = 0; i < rSize; i++)
for (int j = 0; j < bSize; i++)
{
bool kot = false;
for (int k = 0; k < 8; k++)
if (red[i].cot[k] = blue[i].ID)
kot = true;
if (!kot)
{
double a = sqrt(pow((blue[j].x - red[i].x), 2) + pow((blue[j].y - red[i].y), 2));
double b = sqrt(pow((blue[j + 1].x - red[i].x), 2) + pow((blue[j + 1].y - red[i].y), 2));
if (a > b)
{
RBNear[i] = j + 2;
bl[i] = b;
}
else if (a < b)
{
RBNear[i] = j+1;
bl[i] = a;
}
else if (a = b)
{
RBNear[i] = j+1;
bl[i] = a;
}
}
}
}
for (int i = 0; i < rSize; i++)
{
if ((re[i] >= gr[i]) && (re[i] >= bl[i]))
R[i] = re[i];
else if ((gr[i] >= re[i]) && (gr[i] >= bl[i]))
R[i] = gr[i];
else if ((bl[i] >= re[i]) && (bl[i] >= gr[i]))
R[i] = bl[i];
}
}