[ create a new paste ] login | about

Link: http://codepad.org/82XV938G    [ raw code | fork ]

Ralith - D, pasted on May 9:
module fizziks.Fizziks;

import tango.core.Thread;
import tango.io.Stdout;
import tango.time.StopWatch;

import xf.hybrid.Hybrid;
import xf.hybrid.backend.GL;
import xf.hybrid.Event;
import xf.dog.Dog;
import xf.input.KeySym;

import blaze.all;

const STEPS_PER_FRAME = 10;

void main() {
	scope cfg = loadHybridConfig("./gui.cfg");
	scope renderer = new Renderer;
	auto whut = new Fizziks;
	
	gui.begin(cfg).retained;
		GLViewport(`main.glview`)
			.renderingHandler(&whut.draw)
			.addHandler(&whut.onClick)
			.addHandler(&whut.onMove)
			.addHandler(&whut.onKey);
	gui.immediate.end;

	StopWatch timer;
	bool running = true;
	while(running) {
		timer.start;
		gui.begin(cfg);
			if (gui().getProperty!(bool)("main.frame.closeClicked")) {
                running = false;
            }
		gui.end;
		gui.render(renderer);
		Thread.sleep(0.01);
		for(ubyte i = 0; i < STEPS_PER_FRAME; i++)
			whut.space.step(timer.stop/STEPS_PER_FRAME);
	}
	
}

class Fizziks {
	void draw(vec2i size, GL gl) {
		this.size = size;
		gl.LoadIdentity();
		gl.MatrixMode(GL_PROJECTION);
		gl.LoadIdentity();
		gl.gluOrtho2D(-size.x/2, size.x/2, -size.y/2, size.y/2);
		gl.MatrixMode(GL_MODELVIEW);
		gl.Disable(GL_DEPTH_TEST);
		gl.ShadeModel(GL_SMOOTH);
		gl.Enable(GL_BLEND);
		gl.Enable(GL_POINT_SMOOTH);
		gl.Enable(GL_LINE_SMOOTH);
		gl.Enable(GL_POLYGON_SMOOTH);
		gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		gl.Hint(GL_POINT_SMOOTH_HINT, GL_NICEST);
		gl.Hint(GL_LINE_SMOOTH_HINT, GL_NICEST);
		gl.Hint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
		gl.Hint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
		gl.LoadIdentity();

		gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		gl.LoadIdentity();
		
		gl.LineWidth(1);
		gl.Color3f(0, 1, 0);

		foreach (rb; bodies)
		{
			foreach (shape; rb.memberShapes)
			{
				/// Green
				gl.Color3f(0f, 1f, 0f);
				switch (shape.shapeType)
				{
				case 0: // Circle
					int segs = 15;
					Vector2D c = shape.getWorldCenter;
					double r = shape.getRadius;
					double coef = 2.0*(3.14159265358979323)/segs;

					gl.immediate(GL_LINE_STRIP, {
							for(int n = 0; n <= segs; n++){
								double rads = n*coef;
								gl.Vertex2f(r*cos(rads + rb.a) + c.x, r*sin(rads + rb.a) + c.y);
							}
							gl.Vertex2f(c.x,c.y);
						});
					break;
				case 2: // Polygon
					Vector2D[] verts = shape.getWorldVerts();
					gl.immediate(GL_LINE_LOOP, {
							foreach (Vector2D v; verts)
								gl.Vertex2d(v.x, v.y);
						});
					break;
				default:
					break;
				}

				gl.LoadIdentity();
				gl.Flush();
			}
		}

		// Draw static bodies
		/*foreach (GeometricShape shape; space.staticBody.memberShapes)
		{
			Vector2D[] verts = shape.getWorldVerts;
			gl.immediate(GL_LINE_LOOP, {
					foreach (Vector2D v; verts)
						gl.Vertex2d(v.x, v.y);
				});

			gl.LoadIdentity();
			gl.Flush();
			}*/

		// Shape being drawin
		if(inDraw) {
			gl.LineWidth(1);
			gl.Color3f(0.2, 0.5, 1);
			gl.immediate(GL_LINE_STRIP, {
					foreach(vert; drawing)
						gl.Vertex2f(vert.x, vert.y);
				});
							 
			gl.Color3f(1, 0.5, 0);
			gl.immediate(GL_LINES, {
					gl.Vertex2f(drawing[$-1].x, drawing[$-1].y);
					gl.Vertex2f(mouse.x, mouse.y);
				});
		}
	}

	EventHandling onClick(MouseButtonEvent e) {
		if(e.down)
			switch(e.button) {
			case 0:
				inDraw = true;
				drawing ~= winToGL(size, e.pos);
				break;
			case 2:
				// FIXME: Why are these two lines necessary?
				inDraw = false;
				drawing = null;
				vec2i pos = winToGL(size, e.pos);
				Vector2D[] shape = [Vector2D(-10, -10), Vector2D(-10, 10),
									Vector2D(10, 10), Vector2D(10, -10)];
				auto rb = new RigidBody(1, GeometricShape.momentForPoly(1, shape, Vector2D(pos.x, pos.y)));
				rb.p.setTo(0, 0);
				rb.setAngle(0);
				rb.addShape(new Polygon(shape, Vector2D(pos.x, pos.y), 0.5, .9, false));
				space.addRigidBody(rb);
				bodies ~= rb;
				break;
			default:
				break;
		}
		return EventHandling.Stop;
	}

	EventHandling onKey(KeyboardEvent e) {
		if(e.down)
			switch(e.keySym) {
			case KeySym.Return:
				if(!inDraw)
					break;
				Vector2D[] poly;
				foreach(vert; drawing)
					poly ~= Vector2D(vert.x, vert.y);
				auto rb = new RigidBody(drawing.length, GeometricShape.momentForPoly(drawing.length, poly, Vector2D(0, 0)));
				rb.p.setTo(0, 0);
				rb.setAngle(0);
				rb.addShape(new Polygon(poly, Vector2D(0, 0), 0, 1, true));
				space.addRigidBody(rb);
				bodies ~= rb;
			case KeySym.Escape:	// Cascade
				drawing = null;
				inDraw = false;
				break;
			default:
				break;
			}
		return EventHandling.Stop;
	}
			

	EventHandling onMove(MouseMoveEvent e) {
		mouse = winToGL(size, e.pos);
		return EventHandling.Stop;
	}

	this(vec2i dim = vec2i(800, 600)) {
		space = new Space(20, 1);

		Vector2D[] createRectangle(Number w, Number h)
		{
			Vector2D[] rect;
			rect ~= (Vector2D(-w / 2, -h / 2));
			rect ~= (Vector2D(-w / 2, h / 2));
			rect ~= (Vector2D(w / 2, h / 2));
			rect ~= (Vector2D(w / 2, -h / 2));
			return rect;
		}
	
		/*auto staticShape = new Polygon(createRectangle(600, 20), Vector2D(0, -200), 0.00, .9, false);
		space.addStaticShape(staticShape);*/
		auto floor = new Polygon(createRectangle(800, 10),
								 Vector2D(0, -303), .0, .9, false);
		space.addStaticShape(floor);
		space.masslessForce.setTo(0, -800);

		Vector2D[] shape = [Vector2D(-10, -10), Vector2D(-10, 10),
							Vector2D(10, 10), Vector2D(10, -10)];
		auto rb = new RigidBody(1, GeometricShape.momentForPoly(1, shape, Vector2D(0, 0)));
		rb.p.setTo(0, 0);
		rb.setAngle(0);
		rb.addShape(new Polygon(shape, Vector2D(0, 0), 0, 1, false));
		space.addRigidBody(rb);
		bodies ~= rb;
	}

	protected {
		bool inDraw = false;
		vec2i[] drawing;
		vec2i size;
		vec2i mouse;

		RigidBody[] bodies;
	}

	Space space;
}

vec2i winToGL(vec2i dim, vec2 win) {
	return vec2i(cast(int)win.x - dim.x/2, dim.y/2 - cast(int)win.y);
}


Create a new paste based on this one


Comments: