Skip to content

Commit

Permalink
new entity system
Browse files Browse the repository at this point in the history
  • Loading branch information
akadjoker committed Feb 21, 2014
1 parent dc54c75 commit 3811af0
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 34 deletions.
Binary file modified bin/windows/neko/bin/glframework.exe
Binary file not shown.
Binary file modified bin/windows/neko/obj/ApplicationMain.n
Binary file not shown.
4 changes: 2 additions & 2 deletions src/com/djoker/glteste/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ override function begin()



//this.setScreen(new TesteBatch());
this.setScreen(new TesteBatch());
//this.setScreen(new TesteAtlas());
//this.setScreen(new TestneBatch());
//this.setScreen(new TesteCloud());
//this.setScreen(new TesteCloudTiles());
//this.setScreen(new TesteBatchTiles());
this.setScreen(new TesteTiles());
//this.setScreen(new TesteTiles());
//this.setScreen(new TestePrimitives());
//this.setScreen(new TesteBitmap());
//this.setScreen(new TesteDraTiles());
Expand Down
21 changes: 17 additions & 4 deletions src/com/djoker/glteste/TesteBatch.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.djoker.glteste;

import com.engine.game.Entity;
import com.engine.game.Screen;
import com.engine.render.SpriteBatch;
import com.engine.render.Texture;
Expand All @@ -20,15 +21,15 @@ class TesteBatch extends Screen
var batch:SpriteBatch;



var player:Entity;

var particles : Array<Particle>;


override public function show()
{

tex = new Texture("assets/texture.png");
tex = new Texture("assets/texture.png",true);
batch = new SpriteBatch(500);
particles = [];
for(i in 0...200)
Expand All @@ -42,22 +43,34 @@ class TesteBatch extends Screen
caption.defaultTextFormat = new TextFormat ("_sans", 12, 0xffff00);
caption.text = "Test 200 sprites with SpriteBatch ";
game.addChild(caption);

player = new Entity(200,200, tex);


}

override public function render(dt:Float)
{
//camera.Update();
batch.Begin();

//player.rotation += dt * 0.1;
player.skewX += dt * 0.1;



batch.Begin();

for(p in particles)
{
p.move(dt);

batch.drawImage(p);
}

batch.drawEntity(player);

batch.End();


}


Expand Down
45 changes: 45 additions & 0 deletions src/com/engine/game/Entity.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.engine.game;
import com.engine.render.Clip;
import com.engine.render.Texture;
import com.engine.render.BlendMode;

import flash.geom.Matrix;
import flash.geom.Point;


/**
* ...
* @author djoker
*/
class Entity extends GameObject
{
public var flipx:Bool;
public var flipy:Bool;
public var image:Texture;
public var blendMode:Int;
public var clip:Clip;
public var name:String;

public function new(x:Float,y:Float,image:Texture,?name:String='solid')
{
super();
flipx = false;
flipy = false;
this.image = image;
this.blendMode = BlendMode.NORMAL;
clip = new Clip(0, 0, image.width, image.height);
this.x = 100;
this.y = y;
this.name = name;







}



}
26 changes: 23 additions & 3 deletions src/com/engine/game/GameObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,27 @@ import flash.geom.Matrix;
*/
class GameObject extends Transform
{



public var children:List<Transform>;

public function new()
{
super();
children = new List<Transform>();
}
public function add(child:Transform)
{
this.children.add(child);
}
public function remove(child:Transform)
{
this.children.remove(child);
}

override public function dispose()
{
this.children = null;
this.mTransformationMatrix = null;
super.dispose();

}
}
3 changes: 1 addition & 2 deletions src/com/engine/game/Screen.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ package com.engine.game;
* ...
* @author djoker
*/
class Screen extends Transform
class Screen extends GameObject
{

public var game:Game = null;

public function show() { }
public function dispose() { }
public function render(dt:Float) { }
public function resize(width:Int, height:Int) {}

Expand Down
33 changes: 14 additions & 19 deletions src/com/engine/game/Transform.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class Transform
{
public var mTransformationMatrix:Matrix;
public var parent:Transform;
public var children:List<Transform>;
private var isDirty:Bool = false;


public var rotation:Float = 0;
public var scaleX:Float = 1;
Expand All @@ -22,8 +21,8 @@ class Transform
public var pivotY:Float = 0;
public var x:Float = 0;
public var y:Float = 0;
public var scrollFactorX:Float = 1;
public var scrollFactorY:Float = 1;
public var scrollFactorX:Float = 0;
public var scrollFactorY:Float = 0;



Expand All @@ -32,34 +31,26 @@ class Transform
{
parent = null;
mTransformationMatrix = new Matrix();
children = new List<Transform>();

}

public function add(child:Transform)
{
this.children.add(child);
}
public function remove(child:Transform)
{
this.children.remove(child);
}



public function getTransformationMatrix():Matrix
{

mTransformationMatrix.identity();


var cx:Float = Game.scrollX;
var cy:Float = Game.scrollY;
var cx:Float = Game.scrollX;
var cy:Float = Game.scrollY;
var sx:Float = x - cx * scrollFactorX;
var sy:Float = y - cy * scrollFactorY;


if (scaleX != 1.0 || scaleY != 1.0) mTransformationMatrix.scale(scaleX, scaleY);
if (skewX != 0.0 || skewY != 0.0) MatrixHelp.skew(mTransformationMatrix, skewX, skewY);
if (rotation != 0.0) mTransformationMatrix.rotate(rotation);
if (sx != 0.0 || sy != 0.0) mTransformationMatrix.translate(sx, sy);
if (sx != 0.0 || sy != 0.0) mTransformationMatrix.translate(sx, sy);

if (pivotX != 0.0 || pivotY != 0.0)
{
Expand All @@ -84,5 +75,9 @@ class Transform


}

public function dispose()
{
this.mTransformationMatrix = null;

}
}
2 changes: 2 additions & 0 deletions src/com/engine/render/Batch.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.engine.render;

import com.engine.game.GameObject;
import flash.geom.Matrix3D;

import openfl.display.OpenGLView;
Expand Down Expand Up @@ -189,6 +190,7 @@ private var invTexHeight:Float = 0;
}




public function drawImage(img:Image)
{
Expand Down
100 changes: 99 additions & 1 deletion src/com/engine/render/SpriteBatch.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.engine.render;

import flash.geom.Matrix3D;
import flash.geom.Matrix;

import openfl.display.OpenGLView;
import openfl.gl.GL;
Expand All @@ -13,6 +13,8 @@ import openfl.display.FPS;

import com.engine.render.BlendMode;
import com.engine.game.Game;
import com.engine.game.GameObject;
import com.engine.game.Entity;

/**
* ...
Expand Down Expand Up @@ -338,6 +340,102 @@ this.currentBatchSize++;


}

public function drawEntity(obj:Entity)
{

if(obj.image!= this.currentBaseTexture || this.currentBatchSize >= this.capacity)
{
switchTexture(obj.image);
}


if(obj.blendMode != this.currentBlendMode)
{
this.setBlendMode(obj.blendMode);
}






var u:Float = obj.clip.x * invTexWidth;
var u2:Float = ( obj.clip.x + obj.clip.width) * invTexWidth;
var v:Float = ( obj.clip.y + obj.clip.height) * invTexHeight;
var v2:Float = obj.clip.y * invTexHeight;

if (obj.flipx)
{
var tmp:Float = u;
u = u2;
u2 = tmp;
}

if (obj.flipy)
{
var tmp:Float = v;
v = v2;
v2 = tmp;
}



var index:Int = currentBatchSize * vertexStrideSize;

var TempX1:Float = 0;
var TempY1:Float = 0;
var TempX2:Float = obj.clip.width;
var TempY2:Float = obj.clip.height;





vertices[index + 0 * 9 + 0] = TempX1;
vertices[index + 0 * 9 + 1] = TempY1;
vertices[index+0*9+2] = 0;
vertices[index+0*9+3] = u;vertices[index+0*9+4] = v;
vertices[index+0*9+5] = 1;vertices[index+0*9+6] = 1;vertices[index+0*9+7] = 1;vertices[index+0*9+8] = 1;

vertices[index + 1 * 9 + 0] = TempX1;
vertices[index + 1 * 9 + 1] = TempY2;
vertices[index+1*9+2] = 0;
vertices[index+1*9+3] = u;vertices[index+1*9+4] = v2;
vertices[index+1*9+5] = 1;vertices[index+1*9+5] = 1; vertices[index+1*9+7] = 1; vertices[index+1*9+8] = 1;

vertices[index + 2 * 9 + 0] = TempX2;
vertices[index + 2 * 9 + 1] = TempX2;
vertices[index+2*9+2] = 0;
vertices[index+2*9+3] = u2;vertices[index+2*9+4] = v2;
vertices[index+2*9+5] = 1; vertices[index+2*9+6] = 1; vertices[index+2*9+7] = 1; vertices[index+2*9+8] = 1;

vertices[index + 3 * 9 + 0] = TempX2;
vertices[index + 3 * 9 + 1] = TempY1;
vertices[index+3*9+2] = 0;
vertices[index+3*9+3] = u2;vertices[index+3*9+4] = v;
vertices[index + 3 * 9 + 5] = 1; vertices[index + 3 * 9 + 6] = 1; vertices[index + 3 * 9 + 7] = 1; vertices[index + 3 * 9 + 8] = 1;

var matrix:Matrix = obj.getLocalToWorldMatrix();

for (i in 0...4)
{
var x:Float = vertices[index + i * 9 + 0];
var y:Float = vertices[index + i * 9 + 1];
vertices[index + i * 9 + 0] = matrix.a * x + matrix.c * y + matrix.tx;
vertices[index + i * 9 + 1] = matrix.d * y + matrix.b * x + matrix.ty;
}






this.currentBatchSize++;


}

public function Blt(texture:Texture, src:Clip,dst:Clip,flipX:Bool,flipY:Bool,blendMode:Int)
{
if(texture!= this.currentBaseTexture || this.currentBatchSize >= this.capacity)
Expand Down
Loading

0 comments on commit 3811af0

Please sign in to comment.