int DemoSimple::run(CL_DisplayWindow &window) { quit = false; // Set the window window.set_title("LinearParticle Example - Simple "); // Connect the Window close event CL_Slot slot_quit = window.sig_window_close().connect(this, &DemoSimple::on_window_close); // Connect a keyboard handler to on_key_up() CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &DemoSimple::on_input_up); // Get the graphic context CL_GraphicContext gc = window.get_gc(); // initialize LinearParticle L_ParticleSystem::init(); // create surface to be used for particle and set the alignment CL_Sprite surface(gc,"Resources/light16p.png"); surface.set_alignment(origin_center); // create a sample of particle with life of 5000 L_Particle particle(&surface,5000); // create dropping effect with period of 16 L_DroppingEffect dropper(0,0,16); // add the particle to dropper effect dropper.add(&particle); // initialize particle effect dropper.initialize(); float x_pos = 320; float y_pos = 240; float x_vel = 3.0f; float y_vel = 3.0f; CL_Font font(gc, "tahoma", 16 ); FramerateCounter frameratecounter; // Run until someone presses escape while (!quit) { gc.clear(); x_pos += x_vel; y_pos += y_vel; if( x_pos > 640 || x_pos < 0 ) x_vel = -x_vel; if( y_pos > 480 || y_pos < 0 ) y_vel = -y_vel; dropper.set_position(x_pos, y_pos); dropper.trigger(); /* pass frame time to L_ParticleEffect::run(int) for time based system, a constant number would be a reference time unit for frame based system. */ dropper.run(16); // draw dropping effect L_DrawParticle(gc,dropper); frameratecounter.show_fps(gc, font); window.flip(0); // Set to "1" to lock to screen refresh rate frameratecounter.frame_shown(); CL_KeepAlive::process(0); } // deinitialize LinearParticle L_ParticleSystem::deinit(); return 0; }
// The start of the Application int App::start(const std::vector<CL_String> &args) { CL_DisplayWindowDescription win_desc; win_desc.set_allow_resize(true); win_desc.set_title("Vertex Buffer Object Example"); win_desc.set_depth_size(16); win_desc.set_size(CL_Size( 800, 600 ), false); CL_DisplayWindow window(win_desc); CL_Slot slot_quit = window.sig_window_close().connect(this, &App::on_window_close); CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); CL_GraphicContext gc = window.get_gc(); Shader shader(gc); // Prepare the display gc.set_map_mode(cl_user_projection); CL_PolygonRasterizer polygon_rasterizer; polygon_rasterizer.set_culled(true); polygon_rasterizer.set_face_cull_mode(cl_cull_back); polygon_rasterizer.set_front_face(cl_face_side_clockwise); gc.set_polygon_rasterizer(polygon_rasterizer); CL_BufferControl buffer_control; buffer_control.enable_depth_test(true); buffer_control.set_depth_compare_function(cl_comparefunc_lequal); buffer_control.enable_depth_write(true); gc.set_buffer_control(buffer_control); std::vector<CL_Vec3f> object_positions; std::vector<CL_Vec3f> object_normals; std::vector<CL_Vec4f> object_material_ambient; const int num_cubes = 20000; object_positions.reserve(num_cubes * 6 * 6); // 6 faces, and 6 vertices per face object_normals.reserve(num_cubes * 6 * 6); object_material_ambient.reserve(num_cubes * 6 * 6); for (int cnt=0; cnt < num_cubes; cnt++) { create_cube(object_positions, object_normals, object_material_ambient); } CL_VertexArrayBuffer vb_positions(gc, &object_positions[0], sizeof(CL_Vec3f) * object_positions.size()); CL_VertexArrayBuffer vb_normals(gc, &object_normals[0], sizeof(CL_Vec3f) * object_normals.size()); CL_VertexArrayBuffer vb_material_ambient(gc, &object_material_ambient[0], sizeof(CL_Vec4f) * object_material_ambient.size()); // ** Note, at this point "object_positions, object_normals and object_material_ambient" // ** can be destroyed. But for the purpose of this example, is it kept CL_Font fps_font(gc, "tahoma", 20); FramerateCounter frameratecounter; unsigned int time_last = CL_System::get_time(); float angle = 0.0f; is_vertex_buffer_on = true; while (!quit) { unsigned int time_now = CL_System::get_time(); float time_diff = (float) (time_now - time_last); time_last = time_now; gc.clear(CL_Colorf(0.0f, 0.0f, 0.0f, 1.0f)); gc.clear_depth(1.0f); gc.set_map_mode(cl_map_2d_upper_left); CL_String fps = cl_format("%1 fps", frameratecounter.get_framerate()); fps_font.draw_text(gc, gc.get_width() - 100, 30, fps); CL_String info = cl_format("%1 vertices", (int) object_positions.size()); fps_font.draw_text(gc, 30, 30, info); fps_font.draw_text(gc, 30, gc.get_height() - 8, "Press any key to toggle the Vertex Buffer option"); if (is_vertex_buffer_on) { fps_font.draw_text(gc, 200, 30, "Vertex Buffer = ON"); } else { fps_font.draw_text(gc, 200, 30, "Vertex Buffer = OFF"); } gc.set_map_mode(cl_user_projection); CL_Mat4f perp = CL_Mat4f::perspective(45.0f, ((float) gc.get_width()) / ((float) gc.get_height()), 0.1f, 100000.0f); gc.set_projection(perp); angle += time_diff / 20.0f; if (angle >= 360.0f) angle -= 360.0f; gc.push_modelview(); gc.set_modelview(CL_Mat4f::identity()); gc.mult_scale(1.0f,1.0f, -1.0f); // So +'ve Z goes into the screen gc.mult_translate(0.0f, 0.0f, 800.0f); gc.mult_rotate(CL_Angle(angle*2.0f, cl_degrees), 0.0f, 1.0f, 0.0f, false); gc.mult_rotate(CL_Angle(angle, cl_degrees), 1.0f, 0.0f, 0.0f, false); shader.Set(gc); shader.Use(gc); CL_PrimitivesArray prim_array(gc); if (is_vertex_buffer_on) { prim_array.set_attributes(0, vb_positions, 3, cl_type_float, (void *) 0); prim_array.set_attributes(1, vb_normals, 3, cl_type_float, (void *) 0); prim_array.set_attributes(2, vb_material_ambient, 4, cl_type_float, (void *) 0); } else { prim_array.set_attributes(0, &object_positions[0]); prim_array.set_attributes(1, &object_normals[0]); prim_array.set_attributes(2, &object_material_ambient[0]); } gc.draw_primitives(cl_triangles, object_positions.size(), prim_array); gc.pop_modelview(); gc.reset_program_object(); window.flip(0); frameratecounter.frame_shown(); CL_KeepAlive::process(); } return 0; }
int DemoShooting::run(clan::DisplayWindow &window) { clan::SlotContainer cc; window.set_title("LinearParticle Example - Shooting "); cc.connect(window.sig_window_close(), clan::bind_member(this, &DemoShooting::on_window_close)); clan::Canvas canvas(window); cc.connect(window.get_ic().get_keyboard().sig_key_up(), clan::bind_member(this, &DemoShooting::on_key_up)); // initialize LinearParticle L_ParticleSystem::init(); // create surface to be used for particle and set the alignment clan::Sprite surface(canvas,"Resources/star.png"); surface.set_alignment(clan::origin_center); motion_ctrl.set_1d_acceleration(-0.0003); L_Particle particle(&surface,3000); particle.set_color(L_Color(255,110,60,255)); particle.coloring2(L_Color(255,110,60,255),L_Color(0,200,100,255),0.6); particle.rotating2(L_2PI); particle.set_motion_controller(&motion_ctrl); L_Vector shooting_vector; shooting_vector.set2( 0.4, L_DEGREE_TO_RADIAN(-90) ); effect = new L_ShootingEffect(460,360,shooting_vector,16,4); effect->add(&particle); effect->set_width_interval(100); effect->set_angle_interval(L_DEGREE_TO_RADIAN(45)); effect->set_life_distortion(600); effect->set_size_distortion(0.4f); effect->initialize(); char str[32]; quit = false; show_menu = true; clan::Font font(canvas, "Arial", 16 ); FramerateCounter frameratecounter; clan::ubyte64 last_time = clan::System::get_time(); while(!quit) { canvas.clear(); clan::ubyte64 current_time = clan::System::get_time(); int time_run = current_time - last_time; last_time = current_time; /* the maximum time step is set to 50milisecs to avoid artifacts and errors caused by low frame rate to be less noticeable. */ while( time_run > 50 ) { run_a_step(50); time_run -= 50; } if( time_run > 0 ) run_a_step(time_run); L_DrawParticle(canvas, effect); if( show_menu ) { frameratecounter.show_fps(canvas, font); sprintf(str,"#Particle : %d", effect->get_particle_num()); font.draw_text(canvas,10,30,str); font.draw_text(canvas,10,425,"F1 : hide/show menu"); font.draw_text(canvas,10,440,"Space : trigger random sleep"); } window.flip(0); // Set to "1" to lock to screen refresh rate frameratecounter.frame_shown(); clan::KeepAlive::process(0); } delete effect; // deinitialize LinearParticle L_ParticleSystem::deinit(); return 0; }
int DemoMSmall::run(clan::DisplayWindow &window) { clan::SlotContainer cc; window.set_title("LinearParticle Example - MSmall "); cc.connect(window.sig_window_close(), clan::bind_member(this, &DemoMSmall::on_window_close)); clan::Canvas canvas(window); cc.connect(window.get_ic().get_keyboard().sig_key_up(), clan::bind_member(this, &DemoMSmall::on_key_up)); // initialize LinearParticle L_ParticleSystem::init(); // create surface to be used for particle and set the alignment clan::Sprite surface(canvas, "Resources/small.png"); surface.set_alignment(clan::origin_center); motion_ctrl.set_speed_limit(0.1); // set max speed of particle motion_ctrl.set_1d_acceleration(-0.0003); // set deceleration // create sample of particle L_Particle particle(&surface,2000); particle.set_color(L_Color(255,110,60,255)); particle.coloring2(L_Color(255,110,60,255),L_Color(0,200,100,255),0.6); particle.set_motion_controller(&motion_ctrl); // assign motion cotroller // create explosion effect effect = new L_ExplosionEffect(320,240,16,10,12,0.1); effect->add(&particle); // apply random life distortion for each emitted particle effect->set_life_distortion(700); effect->initialize(); char str[32]; quit = false; show_menu = true; clan::Font font(canvas, "Arial", 16 ); FramerateCounter frameratecounter; clan::ubyte64 last_time = clan::System::get_time(); clan::InputDevice &keyboard = window.get_ic().get_keyboard(); while(!quit) { canvas.clear(); static L_REAL x_pos = 320; static L_REAL y_pos = 240; L_REAL x_vel = 0; L_REAL y_vel = 0; if( keyboard.get_keycode(clan::keycode_up) ) y_vel = -0.2; else if( keyboard.get_keycode(clan::keycode_down)) y_vel = 0.2; if( keyboard.get_keycode(clan::keycode_left)) x_vel = -0.2; else if( keyboard.get_keycode(clan::keycode_right)) x_vel = 0.2; L_Vector vel; vel.set( x_vel, y_vel ); clan::ubyte64 current_time = clan::System::get_time(); int time_run = current_time - last_time; last_time = current_time; x_pos += x_vel*time_run; y_pos += y_vel*time_run; effect->trigger(); effect->set_velocity(vel); effect->run(time_run); effect->set_position(x_pos, y_pos); L_DrawParticle(canvas, effect); if( show_menu ) { frameratecounter.show_fps(canvas, font); sprintf(str,"#Particle : %d", effect->get_particle_num()); font.draw_text(canvas,10,30,str); font.draw_text(canvas,10,410,"F1 : hide/show menu"); font.draw_text(canvas,10,425,"Space : trigger random sleep"); font.draw_text(canvas,10,440,"Arrow keys : move the effect"); } window.flip(0); // Set to "1" to lock to screen refresh rate frameratecounter.frame_shown(); clan::KeepAlive::process(0); } delete effect; // deinitialize LinearParticle L_ParticleSystem::deinit(); return 0; }