현재 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++의 배후에있는 이론에 대해 더 많이 알고있는 것 같습니다.하지만 실제로 im을 배우는 것 같아요.
문제점 :
사람이 아주 나쁜대로 내가 '새'와 '삭제'너무 여러 번 호출 할 수 없다고 말했다.
입자 생성은 타이머에 의해 제한됩니다. 입자 생성 속도를 제어 할 수 없으므로 만드는 방법을 모르거나 모르겠습니다.
누군가 나를 위해 만들어 줄 것으로 기대하지 않습니다. 이해할 수 있도록 뭔가를 읽을 수 있거나 다른 사람이 저에게 배우고 자하는 코드를 게시 할 수 있다면 큰 도움이 될 것입니다.
브릴리언트. 나는 이것을 실제로 구현했으나 그것을 할 수있는 방법을 완전히 알지 못했습니다. 감사합니다. 또한 실제로 진행되고있는 일을 저에게 줄 수 있습니까? 나는 다소 알고 있습니다. id가 그것을 잘 이해할 수는 있습니다. D – user2312034
타이머가 1/60로 설정되었으므로 매 초마다 60 ALLEGRO_TIMER_EVENTS가 나옵니다. 입자 생성을 포함하는 코드 블록이 해당 검사 내에 있으므로 초당 최대 60 개의 입자를 얻을 수 있습니다. 더 빠른 틱 속도를 가진 타이머를 사용하면 두 번째 솔루션에서와 같이 더 많은 파티클을 생성 할 수 있습니다. 첫 번째 해법은 초당 60 개의 입자를 변화시킴으로써 똑같은 일을합니다. Allegro 내의 타이밍에 대한 좋은 개요를 보려면 http://fixbyproximity.com/2d-game-development-course/ (4 부 구체적으로)를보십시오. – 131nary