/******************************************************************************* * FUNCTION : part#include <random>icle_add * PURPOSE : add a particle to the dynamic particle linked list * INPUT : struct particle *head. Head of the particle list * OUTPUT : returns -1 on error, 0 on success * NOTES : Calls particle_init() *******************************************************************************/ int particle_add( struct particle **head ) { struct particle *new_particle = (struct particle*)malloc(sizeof(struct particle)); particle_init(new_particle); new_particle->next = *head; *head = new_particle; return 0; }
void particle_update(struct particle *p, float dt) { p->lifetime -= dt; if (p->lifetime <= 0) { particle_init(p); return; } if (p->visible) { p->x += p->vx * dt; p->y += p->vy * dt; } }
int main(int argc, char *argv[]) { srand((unsigned) time(NULL)); particle_init(); pso(); printf("gbest: %f\n", gbest); for (int i = 0; i < 10; i++) { printf("rand%d: %f\n", i, rand0_1()); } printf("rand: %f\n", rand0_1()); printf("rand: %f\n", rand0_1()); return 0; }
void particle_loop(int fd, int num_particles) { struct particle *particles; int i; particles = malloc(sizeof(struct particle) * num_particles); for (i=0; i<num_particles; i++) particle_init(&particles[i]); while (1) { float dt = get_time_step(); for (i=0; i<num_particles; i++) particle_update(&particles[i], dt); rwand_update(fd, particles, num_particles, 40); } free(particles); }
/** * Spawn a new particle. */ void particles_particle_spawn(struct particles *em, struct anim *anim, particle_think_t particle_think, float x, float y, float w, float h, float angle, float vx, float vy, float age_max) { /* Max particles reached? */ if(em->particles_count >= em->particles_max) { em->particles_max_counter++; return; } /* Get the next available particle. */ struct particle *p = particles_particle_next(em); if(p == NULL) { em->particles_max_counter++; return; } particle_init(p, anim, particle_think, x, y, w, h, angle, vx, vy, age_max); /* Add it to the list of alive particles. */ alist_append(em->particles, p); }
void init(void) { int i, j; SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); scaled = SDL_SetVideoMode(SCREENX*2, SCREENY*2, 32, SDL_SWSURFACE); s = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREENX, SCREENY, scaled->format->BitsPerPixel, scaled->format->Rmask, scaled->format->Gmask, scaled->format->Bmask, scaled->format->Amask ); titlesurf = IMG_Load("res/title.png"); srand(time(0)); seed = rand(); scale[0] = 0.025f; speed[0] = 0.005f; for(i = 1; i < ACTORS; i++){ scale[i] = scale[i-1]/8.0f; speed[i] = speed[i-1]/4.0f; } radius = 1.0f / scale[0]; sprite_init(); player = actor_new(egg-1); particle_init(); float pos[2] = {0.0f, 1.0f}; float vel[2] = {0.0f, 0.0f}; }
int main(void) { bool ret = true; plist *cells = NULL; window *win = NULL; /* initialize random number generator */ srand(time(NULL)); /* initialize opengl window */ win = window_init("brownian tree", WINDOW_W, WINDOW_H, 32); if (win == NULL) { fprintf(stderr, "FATAL: %s\n", strerror(errno)); ret = false; goto cleanup; } else { win->refresh = update_screen; } /* initialize opengl stuff */ glPointSize(POINTSIZE); glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); glfwSetWindowSizeCallback(win->enable2d); win->enable2d(WINDOW_W, WINDOW_H); /* initialize cells list */ cells = plist_init(particle_init()); for (int i = 0; i < FREE_CELLS_NUMBER; i++) { particle *pt = particle_init(); if (pt == NULL) { fprintf(stderr, "FATAL: %s\n", strerror(errno)); ret = false; goto cleanup; } pt->x = (GLfloat) randint((WINDOW_W - 1)); pt->y = (GLfloat) randint((WINDOW_H - 1)); if (!plist_push(cells, pt)) { fprintf(stderr, "FATAL: %s\n", strerror(errno)); ret = false; goto cleanup; } } /* initialize 2D grid */ for (int y = 0; y < WINDOW_H; y++) { for (int x = 0; x < WINDOW_W; x++) { grid[y][x] = CELL_EMPTY; } } /* initialize random freezed cells */ for (int i = 0; i < FREEZED_CELLS_NUMBER; i++) { int y = randint(WINDOW_H - 1); int x = (i * (WINDOW_W / FREEZED_CELLS_NUMBER)) + (FREEZED_CELLS_NUMBER / 2); grid[y][x] = CELL_FREEZED; } /* main loop */ do { win->clear(); win->refresh(win, &cells); move_cells(&cells); } while ( glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS && glfwGetWindowParam(GLFW_OPENED) ); goto cleanup; cleanup: /* free resources */ if (cells != NULL) plist_free(cells); if (win != NULL) { window_quit(); window_free(win); } /* exit error */ if (!ret) return EXIT_FAILURE; return EXIT_SUCCESS; }
/* Function: mdlStart ======================================================= * Abstract: * This function is called once at start of model execution. If you * have states that should be initialized once, this is the place * to do it. */ static void mdlStart(SimStruct *S) { particle_init(); }
int main(int argc, char *argv[]) { byte keypress; client_init(); if( argc > 1 ) /* Command line arguments */ handle_cl_args(argc, argv); srand( time(NULL) ); calc_lookup_tables(DEGREES); win_init(); entity_init(); if( (ent_img_loaded = (char *)malloc(num_entity_types)) == NULL ) { perror("Malloc"); ERR_QUIT("Error allocating ent_img_loaded array", 1); } else { memset(ent_img_loaded, 0, num_entity_types); } /* Load client preferences from file */ read_config_file( &client ); /* Set view dimensions, which are calculated by tile width & heights */ client.view_w = client.x_tiles * TILE_W; client.view_h = client.y_tiles * TILE_H; /* What the client WISHES their dimensions were (this may not be what it actually is, ie in demo's etc */ client.desired_w = client.view_w; client.desired_h = client.view_h; weapon_type_init(); particle_init(); menu_init(); cl_network_init(); client.state = MENU; if( client.demo == DEMO_PLAY ) { cl_network_connect(NULL, 42); client.state = GAME_LOAD; } for( ; ; ) { switch( client.state ) { case MENU: win_ungrab_pointer(); menu_driver(); break; case GAME_LOAD: win_set_properties(VERSION, client.view_w, client.view_h+STATUS_H); wait_till_expose(5); /* Finished loading, we are ready to play */ cl_change_map(client.map, client.gamemode); break; case GAME_RESUME: /* Resume game from menu */ win_set_properties(VERSION, client.view_w, client.view_h+STATUS_H); wait_till_expose(5); input_clear(); draw_status_bar(); cl_netmsg_send_ready(); /* Tell server new details */ cl_net_finish(); /* Send the NETMSG_READY to the server */ client.state = GAME_JOIN; break; case GAME_JOIN: win_set_cursor( client.mousemode!=MOUSEMODE_ROTATE ); win_set_cursor( 1 ); if( client.mousemode ) { win_grab_pointer(); } text_buf_clear(); particle_clear(); case GAME_PLAY: cl_net_update(); if( client.state == GAME_PLAY ) { /* Maybe changed in net_update */ draw_crosshair( my_entity ); if( client.map_target_active ) draw_map_target( my_entity ); text_buf_update(); if( client.netstats ) cl_net_stats(); win_update(); /* Keyboard Input */ client.dir = my_entity.dir; keypress = get_input(); cl_netmsg_send_cl_update(keypress, client.dir); } cl_net_finish(); /* Send things that need sending */ cap_fps(client.fps); break; case QUIT: game_close(); image_close(); /* Free all the images */ win_close(); write_config_file(&client); if( ent_img_loaded != NULL ) free(ent_img_loaded); exit(EXIT_SUCCESS); break; } } return EXIT_SUCCESS; }