Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

solar system

c-plus-plus

//system.h
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/bitmap.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <cmath>
enum KEYS {KEY_LEFT, KEY_RIGHT, KEY_S};
const int screen_width = 1100, screen_height = 800, planet_count = 9, moon_count = 15;
ALLEGRO_BITMAP *MOONS[moon_count]={0,0,0,0,0,0,0,0,0,0,0,0,0};
double gl_speed = 1;
void load_bitmaps(ALLEGRO_BITMAP *moons[moon_count], ALLEGRO_BITMAP *planets[planet_count])
{
	planets[0] = al_load_bitmap("res\\mercury.png");
	planets[1] = al_load_bitmap("res\\venus.png");
	planets[2] = al_load_bitmap("res\\earth.png");
	planets[3] = al_load_bitmap("res\\mars.png");
	planets[4] = al_load_bitmap("res\\jupiter.png");
	planets[5] = al_load_bitmap("res\\saturn.png");
	planets[6] = al_load_bitmap("res\\uran.png");
	planets[7] = al_load_bitmap("res\\neptun.png");
	planets[8] = al_load_bitmap("res\\pluto.png");
	moons[0] = al_load_bitmap("res\\moon.png");
	moons[1] = al_load_bitmap("res\\mars_phobos.png");
	moons[2] = al_load_bitmap("res\\mars_deimos.png");
	moons[3] = al_load_bitmap("res\\jm1.png");
	moons[4] = al_load_bitmap("res\\jm2.png");
	moons[5] = al_load_bitmap("res\\jm3.png");
	moons[6] = al_load_bitmap("res\\jm4.png");
	moons[7] = al_load_bitmap("res\\um6.png");
	moons[8] = al_load_bitmap("res\\um5.png");
	moons[9] = al_load_bitmap("res\\um4.png");
	moons[10] = al_load_bitmap("res\\um3.png");
	moons[11] = al_load_bitmap("res\\um2.png");
	moons[12] = al_load_bitmap("res\\um1.png");
	moons[13] = al_load_bitmap("res\\neptun_triton.png");
	moons[14] = al_load_bitmap("res\\charon.png");
}
void initialize_allegro()
{
	al_init();
	al_init_image_addon();
	al_init_primitives_addon();
	al_install_keyboard();
	al_install_mouse();
	al_init_font_addon();
	al_init_ttf_addon();
}
inline double distance(int x1, int y1, int x2, int y2)
{
	return sqrt(double((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
}
void set_info(char name[50], char period[50], char speed[50], char moon_c[50], char osa[50], char osa2[50], int planet_id)
{
	switch(planet_id)
	{
	case -1:
		strcpy(name,"extremly hot object :)");
		strcpy(period,"---");
		strcpy(speed,"---");
		strcpy(moon_c,"---");
		strcpy(osa,"---");
		strcpy(osa2,"---");
		break;
	case 0:
		strcpy(name,"Mercury");
		strcpy(period,"115.88 days");
		strcpy(speed,"47.87 km/s");
		strcpy(moon_c,"0");
		strcpy(osa,"69 816 900 km");
		strcpy(osa2,"0.466697 AU");
		break;
	case 1:
		strcpy(name,"Venus");
		strcpy(period,"224.698 days");
		strcpy(speed,"35.02 km/s");
		strcpy(moon_c,"0");
		strcpy(osa,"108 939 000 km");
		strcpy(osa2,"0.728213 AU");
		break;
	case 2:
		strcpy(name,"Earth");
		strcpy(period,"365.256363 days");
		strcpy(speed,"29.78 km/s");
		strcpy(moon_c,"1");
		strcpy(osa,"152 098 232 km");
		strcpy(osa2,"1.01671388 AU");
		break;
	case 3:
		strcpy(name,"Mars");
		strcpy(period,"686.971 days");
		strcpy(speed,"24.077 km/s");
		strcpy(moon_c,"2");
		strcpy(osa,"249 209 300 km");
		strcpy(osa2,"1.665861 AU");
		break;
	case 4:
		strcpy(name,"Jupiter");
		strcpy(period,"4 332.59 days");
		strcpy(speed,"13.07 km/s");
		strcpy(moon_c,"65");
		strcpy(osa,"816 520 800 km");
		strcpy(osa2,"5.458104 AU");
		break;
	case 5:
		strcpy(name,"Saturn");
		strcpy(period,"10 759.22 days");
		strcpy(speed,"9.69 km/s");
		strcpy(moon_c,"62");
		strcpy(osa,"1 513 325 783 km");
		strcpy(osa2,"10.11595804 AU");
		break;
	case 6:
		strcpy(name,"Uranus");
		strcpy(period,"30 799.095 days");
		strcpy(speed,"6.81 km/s");
		strcpy(moon_c,"27");
		strcpy(osa,"3 004 419 704 km");
		strcpy(osa2,"20.08330526 AU");
		break;
	case 7:
		strcpy(name,"Neptune");
		strcpy(period,"60 190.03 days");
		strcpy(speed,"5.43 km/s");
		strcpy(moon_c,"13");
		strcpy(osa,"4 553 946 490 km");
		strcpy(osa2,"30.44125206 AU");
		break;
	case 8:
		strcpy(name,"Pluto");
		strcpy(period,"89 865.65 days");
		strcpy(speed,"4.7 km/s");
		strcpy(moon_c,"5");
		strcpy(osa,"7 311 000 000 km");
		strcpy(osa2,"48.871 AU");
		break;
	default:
		strcpy(name,"no object selected");
		strcpy(period,"---");
		strcpy(speed,"---");
		strcpy(moon_c,"---");
		strcpy(osa,"---");
		strcpy(osa2,"---");
		break;
	}
}
#
class planet{
	int distance;
	double speed, direction;
	ALLEGRO_BITMAP *image;
public:
	planet(int dist, double sp, ALLEGRO_BITMAP *im);
	void draw(int x, int y, bool lines);
	void step();
	int get_distance();
	double get_direction();
	int get_radius();
};
int planet::get_distance()
{
	return distance;
}
int planet::get_radius()
{
	return (al_get_bitmap_width(image)/2);
}
double planet::get_direction()
{
	return direction;
}
planet::planet(int dist, double sp, ALLEGRO_BITMAP *im)
:image(im), speed(sp), distance(dist)
{
	direction=rand()%360;
}
void planet::draw(int x, int y, bool lines)
{
	double _x, _y;
	_x=x+distance*cos((direction+1)/180.0*3.14);
	_y=y+distance*sin((direction+1)/180.0*3.14);
	if (lines)
		al_draw_circle(x,y,distance,al_map_rgb(0,100,100),1);
	al_draw_bitmap(image,_x-(al_get_bitmap_width(image)/2),_y-(al_get_bitmap_height(image)/2),0);
}
void planet::step()
{
	direction+=gl_speed*speed;
	if(direction>360)direction-=360;
}
int get_pressed_planet(int m_x, int m_y, planet planets[planet_count])
{
	for(int i = 0; i < planet_count; i++)
	{
		if (distance(m_x,m_y,
			screen_width/2+128+planets[i].get_distance()*cos((planets[i].get_direction())/180.0*3.14),
			screen_height/2+planets[i].get_distance()*sin((planets[i].get_direction())/180.0*3.14)
			) < (planets[i].get_radius()*1.5))
			return i;
	}
	if (distance(m_x, m_y, screen_width/2+128, screen_height/2) < 32)
		return -1;
	else
		return planet_count;
}
#
class moon{
	planet *pl;
	int distance;
	double speed, direction;
	ALLEGRO_BITMAP *image;
public:
	moon(int dist, double sp, int im, planet *_pl);
	void draw(int x, int y, bool lines);
	void step();
};
void moon::draw(int x, int y, bool lines)
{
	double _x, _y, _x2, _y2;
	_x=pl->get_distance()*cos((pl->get_direction()+1)/180.0*3.14);
	_y=pl->get_distance()*sin((pl->get_direction()+1)/180.0*3.14);
	_x2=distance*cos(direction/180.0*3.14);
	_y2=distance*sin(direction/180.0*3.14);
	if (lines)
		al_draw_circle(x+_x,y+_y,distance,al_map_rgb(0,100,100),1);
	al_draw_bitmap(image,x+_x+_x2-(al_get_bitmap_width(image)/2),y+_y+_y2-(al_get_bitmap_height(image)/2),0);
}
moon::moon(int dist, double sp, int im, planet *_pl)
:pl(_pl), 	speed(sp), 	distance(dist)
{
	image = MOONS[im];
	direction = rand()%360;
}
void moon::step()
{
	direction+=gl_speed*speed;
	if(direction>360)direction-=360;
}

//code.cpp
#include "system.h"
int main(int argc, char **argv)
{
	srand(time(NULL));
	ALLEGRO_DISPLAY *display = 0;
	ALLEGRO_EVENT_QUEUE *event_queue = 0;
	ALLEGRO_TIMER *timer = 0;
	ALLEGRO_BITMAP *PLANETS[planet_count]={0,0,0,0,0,0,0,0,0};
	ALLEGRO_BITMAP *BACK = 0, *SCREEN = 0;
	ALLEGRO_FONT *font = 0;
	int random;
	bool draw=0;
	bool show_back = 1;
	int mouse_x, mouse_y;
	bool KEY[3] = {0, 0, 0};
	bool show_lines = 0;
	char text[6][50]={"no object selected","---","---","---","---","---"};
#
	initialize_allegro();
#
	display = al_create_display(screen_width, screen_height);
	timer = al_create_timer(1.0 / 60);
	event_queue = al_create_event_queue();
	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_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_mouse_event_source());
	font = al_load_ttf_font("res\\computer_font.ttf", 20, 0);
#
	al_clear_to_color(al_map_rgb(0,0,0));
#
	ALLEGRO_BITMAP *SUN = al_load_bitmap("res\\sun.png");
	load_bitmaps(MOONS, PLANETS);
	BACK = al_load_bitmap("res\\back.png");
	SCREEN = al_load_bitmap("res\\screen.png");
#
	planet planets[planet_count]={
		planet(40,360/87.969,PLANETS[0]),
		planet(60,360/224.700,PLANETS[1]),
		planet(100,360/365.256,PLANETS[2]),
		planet(140,360/686.9601,PLANETS[3]),
		planet(200,360/4332.59,PLANETS[4]),
		planet(260,360/10759.22,PLANETS[5]),
		planet(320,360/30799.095,PLANETS[6]),
		planet(360,360/60190.03,PLANETS[7]),
		planet(390,360/89865.65,PLANETS[8])
	};
	moon moons[moon_count]={
		moon(18,360/28,0,&planets[2]),
		moon(15,360/0.3,1,&planets[3]),
		moon(20,360/1.26,2,&planets[3]),
		moon(48,360/16.689,3,&planets[4]),
		moon(42,360/7.154,4,&planets[4]),
		moon(36,360/3.552,5,&planets[4]),
		moon(30,360/1.769,6,&planets[4]),
		moon(16,360/0.7618,7,&planets[6]),
		moon(18,360/1.4135,8,&planets[6]),
		moon(20,360/2.5204,9,&planets[6]),
		moon(22,360/4.1442,10,&planets[6]),
		moon(24,360/8.7059,11,&planets[6]),
		moon(26,360/13.4632,12,&planets[6]),
		moon(20,360/5.8769,13,&planets[7]),
		moon(12,360/6.387,14,&planets[8])
	};

	al_flip_display();
	al_start_timer(timer);
	while(1)
	{
		ALLEGRO_EVENT ev;
		al_wait_for_event(event_queue, &ev);
		if(ev.type == ALLEGRO_EVENT_TIMER)
		{
			draw = 1;
		}
		else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			break;
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
		switch(ev.keyboard.keycode) {
			case ALLEGRO_KEY_L:
				show_lines = !show_lines; break;
			case ALLEGRO_KEY_LEFT: 
				KEY[KEY_LEFT] = 1; break;
			case ALLEGRO_KEY_RIGHT:
				KEY[KEY_RIGHT] = 1; break;
			case ALLEGRO_KEY_B:
				show_back = !show_back; break;
			case ALLEGRO_KEY_S:
				KEY[KEY_S] = 1;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
		switch(ev.keyboard.keycode) {
			case ALLEGRO_KEY_LEFT: 
				KEY[KEY_LEFT] = 0; break;
			case ALLEGRO_KEY_RIGHT:
				KEY[KEY_RIGHT] = 0; break;
			case ALLEGRO_KEY_S:
				KEY[KEY_S] = 0; break;
			case ALLEGRO_KEY_ESCAPE:
				goto END;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES || ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) {
			mouse_x = ev.mouse.x;
			mouse_y = ev.mouse.y;
		}
		else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
			set_info(text[0], text[1], text[2], text[3], text[4], text[5], get_pressed_planet(mouse_x, mouse_y, planets));
		}
		if(draw && al_is_event_queue_empty(event_queue))
		{
			if (KEY[KEY_LEFT] && !KEY[KEY_RIGHT] && !KEY[KEY_S])
				gl_speed = 0.02;
			else if (!KEY[KEY_LEFT] && KEY[KEY_RIGHT] && !KEY[KEY_S])
				gl_speed = 50;
			else if (!KEY[KEY_LEFT] && !KEY[KEY_RIGHT] && KEY[KEY_S])
				gl_speed = 0;
			else
				gl_speed = 1;
#
			draw = 0;
#
			if(show_back)
				al_draw_bitmap(BACK,0,0,0);
			else
				al_clear_to_color(al_map_rgb(0,0,0));
#
			al_draw_bitmap(SCREEN,0,0,0);
#
			if (rand()%20!=0)
			{
				al_draw_text(font, al_map_rgb(255,255,0), 20, 24, ALLEGRO_ALIGN_LEFT, "NAME:");
				al_draw_text(font, al_map_rgb(0,255,0), 20, 48, ALLEGRO_ALIGN_LEFT, text[0]);
				al_draw_text(font, al_map_rgb(255,255,0), 20, 72, ALLEGRO_ALIGN_LEFT, "ORBITAL PERIOD:");
				al_draw_text(font, al_map_rgb(0,255,0), 20, 96, ALLEGRO_ALIGN_LEFT, text[1]);
				al_draw_text(font, al_map_rgb(255,255,0), 20, 120, ALLEGRO_ALIGN_LEFT, "SPEED:");
				al_draw_text(font, al_map_rgb(0,255,0), 20, 144, ALLEGRO_ALIGN_LEFT, text[2]);
				al_draw_text(font, al_map_rgb(255,255,0), 20, 168, ALLEGRO_ALIGN_LEFT, "MOON COUNT:");
				al_draw_text(font, al_map_rgb(0,255,0), 20, 192, ALLEGRO_ALIGN_LEFT, text[3]);
				al_draw_text(font, al_map_rgb(255,255,0), 20, 216, ALLEGRO_ALIGN_LEFT, "APHELION:");
				al_draw_text(font, al_map_rgb(0,255,0), 20, 240, ALLEGRO_ALIGN_LEFT, text[4]);
				al_draw_text(font, al_map_rgb(0,255,0), 20, 264, ALLEGRO_ALIGN_LEFT, text[5]);
			}else
			{
				random=rand()%55+200;
				al_draw_text(font, al_map_rgb(random,random,0), 20, 24, ALLEGRO_ALIGN_LEFT, "NAME:");
				al_draw_text(font, al_map_rgb(0,random,0), 20, 48, ALLEGRO_ALIGN_LEFT, text[0]);
				al_draw_text(font, al_map_rgb(random,random,0), 20, 72, ALLEGRO_ALIGN_LEFT, "ORBITAL PERIOD:");
				al_draw_text(font, al_map_rgb(0,random,0), 20, 96, ALLEGRO_ALIGN_LEFT, text[1]);
				al_draw_text(font, al_map_rgb(random,random,0), 20, 120, ALLEGRO_ALIGN_LEFT, "SPEED:");
				al_draw_text(font, al_map_rgb(0,random,0), 20, 144, ALLEGRO_ALIGN_LEFT, text[2]);
				al_draw_text(font, al_map_rgb(random,random,0), 20, 168, ALLEGRO_ALIGN_LEFT, "MOON COUNT:");
				al_draw_text(font, al_map_rgb(0,random,0), 20, 192, ALLEGRO_ALIGN_LEFT, text[3]);
				al_draw_text(font, al_map_rgb(random,random,0), 20, 216, ALLEGRO_ALIGN_LEFT, "APHELION:");
				al_draw_text(font, al_map_rgb(0,random,0), 20, 240, ALLEGRO_ALIGN_LEFT, text[4]);
				al_draw_text(font, al_map_rgb(0,random,0), 20, 264, ALLEGRO_ALIGN_LEFT, text[5]);
			}
			
#
			for(int i = 0; i < moon_count; i++)
			{
				moons[i].step();
				moons[i].draw(screen_width/2+128,screen_height/2, show_lines);
			}
			for(int i = 0; i < planet_count; i++)
			{
				planets[i].step();
				planets[i].draw(screen_width/2+128,screen_height/2, show_lines);
			}
#
			al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
			al_draw_bitmap(SUN,screen_width/2-(al_get_bitmap_width(SUN)/2)+128,screen_height/2-(al_get_bitmap_height(SUN)/2),0);
			al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);

			al_flip_display();
		}
	}
END:
	for(int i = 0; i < planet_count; i++)
	{
		al_destroy_bitmap(PLANETS[i]);
	}
	for(int i = 0; i < moon_count; i++)
	{
		al_destroy_bitmap(MOONS[i]);
	}
	al_destroy_bitmap(SUN);
	al_destroy_timer(timer);
	al_destroy_display(display);
	al_destroy_event_queue(event_queue);
	return 0;
}

Neformátovaný

Přidáno: 12.5.2013
Expirace: Neuvedeno

Aktivity