0
我目前正在制作一个基本粒子系统,也使用Allegro 5库。C++粒子系统Allegro 5
这里是我想出了:
int main()
{
int mouseX = 0, mouseY = 0;
int randNum = 0;
std::vector <Particle *> Particles;
bool running = false, redraw = false, mouseHold = false;
int particleCount = 0;
al_init();
al_init_image_addon();
al_install_mouse();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_DISPLAY* display = al_create_display(800, 600);
ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
ALLEGRO_TIMER* myTimer = al_create_timer(1.0/60);
ALLEGRO_TIMER* pTimer = al_create_timer(1.0/120);
ALLEGRO_FONT* myFont = al_load_ttf_font("MyFont.ttf", 20, NULL);
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(myTimer));
al_register_event_source(event_queue, al_get_timer_event_source(pTimer));
al_register_event_source(event_queue, al_get_mouse_event_source());
running = true;
al_start_timer(myTimer);
while(running)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
running = false;
if(ev.type == ALLEGRO_EVENT_MOUSE_AXES)
{
mouseX = ev.mouse.x;
mouseY = ev.mouse.y;
}
if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
if(ev.mouse.button == 1)
mouseHold = true;
}
if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
{
if(ev.mouse.button == 1)
mouseHold = false;
}
if(ev.type == ALLEGRO_EVENT_TIMER)
{
randNum = (std::rand()+1 * ev.timer.count) % 50;
std::cout << randNum << std::endl;
if(mouseHold)
{
Particle* particle = new Particle(mouseX + randNum, mouseY + randNum);
Particles.push_back(particle);
}
particleCount = Particles.size();
for(auto i : Particles)
i->Update();
redraw = true;
}
for(auto iter = Particles.begin(); iter != Particles.end();)
{
if(!(*iter)->GetAlive())
{
delete (*iter);
iter = Particles.erase(iter);
}
else
iter++;
}
if(redraw && al_event_queue_is_empty(event_queue))
{
for(auto i : Particles)
i->Draw();
al_draw_textf(myFont, al_map_rgb(0,200,0), 0, 10, NULL, "Mouse X: %i", mouseX);
al_draw_textf(myFont, al_map_rgb(0,200,0), 0, 30, NULL, "Mouse Y: %i", mouseY);
al_draw_textf(myFont, al_map_rgb(0,200,0), 0, 60, NULL, "Particle Count: %i", particleCount);
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
redraw = false;
}
}
al_destroy_display(display);
al_destroy_event_queue(event_queue);
al_destroy_timer(myTimer);
for(auto i : Particles)
delete i;
Particles.clear();
return 0;
}
是,代码是相当糟糕。看来我更了解C++背后的理论,而不是实际执行它..但我猜是在学习。
的问题:
有人说我不能叫“新”和“删除”这么多次,因为这是非常糟糕的。
粒子的创造受到定时器的限制 - 我无法/不知道如何制作,所以我可以控制粒子创建的速度。
我不期待有人为我创作,如果我能阅读某些东西来帮助我理解或有人发布一些代码来学习/让我思考,那么这将非常有用。
辉煌。我实际上实现了这一点,但没有完全知道如何做到这一点大声笑,谢谢你,也可以只是给我一个真正发生了什么?我有点知道,但要充分理解id欣赏它:D – user2312034 2013-04-24 23:47:08
由于您的计时器设置为1/60运行,您将每秒获得60个ALLEGRO_TIMER_EVENTS。由于包含您的粒子创建的代码块在该检查中,因此每秒最多可以获得60个粒子。通过使用速度更快的计时器,您可以生成更多的粒子,如第二种解决方案所示。第一种解决方案将通过产生每秒60个可变速率的粒子来做同样的事情。看一看http://fixbyproximity.com/2d-game-development-course/(第4部分),以获得Allegro内部时间的高级概述。 – 131nary 2013-04-25 11:05:35