Exemple #1
0
// The start of the Application
int PathApp::start(const std::vector<std::string> &args)
{
	quit = false;

	// Set the window
	clan::DisplayWindowDescription desc;
	desc.set_title("ClanLib Path Example");
	desc.set_size(clan::Size(1024, 768), true);
	desc.set_allow_resize(true);

	clan::DisplayWindow window(desc);
	clan::Canvas canvas(window);
    clan::SlotContainer cc;

	// Connect the Window close event
	cc.connect(window.sig_window_close(), clan::bind_member(this, &PathApp::on_window_close));

	// Connect a keyboard handler to on_key_up()
	cc.connect(window.get_ic().get_keyboard().sig_key_up(), clan::bind_member(this, &PathApp::on_input_up));

	clan::Path rounded_rect_shape = clan::Path::rect(clan::Rectf(0.0f, 0.0f, clan::Sizef(256, 256)), clan::Sizef(64.0f, 64.0f));

	clan::Font test_font(canvas, "tahoma", 20);
	clan::Path complex_shape = clan::Path::circle(128.0f, 128.0f, 128.0f);
	clan::GlyphMetrics glyph_metrics;
	complex_shape += clan::Path::glyph(test_font, 'e', glyph_metrics).transform_self(clan::Mat3f::translate(58.0f, 198.0f) * clan::Mat3f::scale(10.0f, 10.0f));

	clan::Brush brush_solid = clan::Brush::solid_rgba8(50, 200, 150, 255);
	clan::Brush brush_image;
	brush_image.type = clan::BrushType::image;
	clan::Image image(canvas, "../../Display/Path/Resources/lobby_background2.png");
	brush_image.image = image;

	clan::Brush brush_linear;
	brush_linear.type = clan::BrushType::linear;
	brush_linear.start_point = clan::Pointf(0.0f, 0.0f);
	brush_linear.end_point = clan::Pointf(256.0f, 256.0f);
	brush_linear.stops.push_back(clan::BrushGradientStop(clan::Colorf::aliceblue, 0.0f));
	brush_linear.stops.push_back(clan::BrushGradientStop(clan::Colorf::orangered, 0.3f));
	brush_linear.stops.push_back(clan::BrushGradientStop(clan::Colorf::greenyellow, 0.7f));
	brush_linear.stops.push_back(clan::BrushGradientStop(clan::Colorf::aliceblue, 1.0f));

	clan::Brush brush_radial;
	brush_radial.type = clan::BrushType::radial;
	brush_radial.center_point = clan::Pointf(128.0f, 128.0f);
	brush_radial.radius_x = 128.0f;
	brush_radial.radius_y = 128.0f;
	brush_radial.stops.push_back(clan::BrushGradientStop(clan::Colorf::black, 0.0f));
	brush_radial.stops.push_back(clan::BrushGradientStop(clan::Colorf::white, 0.5f));
	brush_radial.stops.push_back(clan::BrushGradientStop(clan::Colorf::aliceblue, 1.0f));

	clan::Font fps_font(canvas, "tahoma", 20);
	clan::GameTime game_time;

	float angle = 0.0f;

	// Run until someone presses escape
	while (!quit)
	{
		game_time.update();

		angle += game_time.get_time_elapsed() * 32.0f;
		if (angle >= 360.0f) angle = -360.0f;
		clan::Mat4f rotation = clan::Mat4f::translate(128.0f, 128.0f, 0.0f) * clan::Mat4f::rotate(clan::Angle(angle, clan::angle_degrees), 0.0f, 0.0f, 1.0f) * clan::Mat4f::translate(-128.0f, -128.0f, 0.0f);

		canvas.clear(clan::Colorf(0.2f, 0.2f, 0.5f));

		canvas.set_transform(clan::Mat4f::translate(50.0f, 10.0f, 0.0f) * rotation);
		rounded_rect_shape.fill(canvas, brush_solid);
		canvas.set_transform(clan::Mat4f::translate(380.0f, 10.0f, 0.0f) * rotation);
		complex_shape.fill(canvas, brush_image);

		canvas.set_transform(clan::Mat4f::translate(50.0f, 300.0f, 0.0f) * rotation);
		rounded_rect_shape.fill(canvas, brush_linear);

		canvas.set_transform(clan::Mat4f::translate(380.0f, 300.0f, 0.0f) * rotation );
		rounded_rect_shape.fill(canvas, brush_radial);
		
		canvas.set_transform(clan::Mat4f::identity());
		std::string fps = clan::string_format("%1 fps", clan::StringHelp::float_to_text(game_time.get_updates_per_second(), 1));
		fps_font.draw_text(canvas, canvas.get_width() - 100, 30, fps);

		window.flip(0);

		// This call processes user input and other events
		clan::KeepAlive::process(0);
	}

	return 0;
}
Exemple #2
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;
}