Created
March 8, 2014 08:16
-
-
Save Vavius/9427201 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MOAISim.openWindow ( "test", 1536, 2048 ) | |
viewport = MOAIViewport.new () | |
viewport:setSize ( 1536, 2048 ) | |
viewport:setScale ( 1536, 2048 ) | |
layerMesh = MOAILayer.new () | |
layerMesh:setViewport ( viewport ) | |
layerMesh:setSortMode ( MOAILayer.SORT_NONE ) | |
layerProp = MOAILayer.new () | |
layerProp:setViewport ( viewport ) | |
layerProp:setSortMode ( MOAILayer.SORT_NONE ) | |
renderTable = { layerMesh } | |
MOAIGfxDevice.getFrameBuffer():setRenderTable(renderTable) | |
local function initBuffers(numSprites) | |
local vertexFormat = MOAIVertexFormat.new () | |
vertexFormat:declareCoord ( 1, MOAIVertexFormat.GL_FLOAT, 3 ) | |
vertexFormat:declareUV ( 2, MOAIVertexFormat.GL_FLOAT, 2 ) | |
vertexFormat:declareColor ( 3, MOAIVertexFormat.GL_UNSIGNED_BYTE ) | |
vbo = MOAIVertexBuffer.new () | |
vbo:setFormat ( vertexFormat ) | |
vbo:reserveVerts ( 4 * numSprites ) | |
ibo = MOAIIndexBuffer.new () | |
ibo:reserve ( 6 * numSprites ) | |
curSprite = 0 | |
end | |
local function addSprite(x, y) | |
vbo:writeFloat ( x - 32, y + 32, 0 ) | |
vbo:writeFloat ( 0, 0 ) | |
vbo:writeColor32 ( 1, 1, 1, 1 ) | |
vbo:writeFloat ( x + 32, y + 32, 0 ) | |
vbo:writeFloat ( 1, 0 ) | |
vbo:writeColor32 ( 1, 1, 1, 1 ) | |
vbo:writeFloat ( x + 32, y - 32, 0 ) | |
vbo:writeFloat ( 1, 1 ) | |
vbo:writeColor32 ( 1, 1, 1, 1 ) | |
vbo:writeFloat ( x - 32, y - 32, 0 ) | |
vbo:writeFloat ( 0, 1 ) | |
vbo:writeColor32 ( 1, 1, 1, 1 ) | |
ibo:setIndex ( curSprite * 6 + 1, curSprite * 4 + 1 ) | |
ibo:setIndex ( curSprite * 6 + 2, curSprite * 4 + 4 ) | |
ibo:setIndex ( curSprite * 6 + 3, curSprite * 4 + 3 ) | |
ibo:setIndex ( curSprite * 6 + 4, curSprite * 4 + 1 ) | |
ibo:setIndex ( curSprite * 6 + 5, curSprite * 4 + 3 ) | |
ibo:setIndex ( curSprite * 6 + 6, curSprite * 4 + 2 ) | |
curSprite = curSprite + 1 | |
end | |
gfxQuad = MOAIGfxQuad2D.new () | |
gfxQuad:setTexture ( "moai.png" ) | |
gfxQuad:setRect ( -32, -32, 32, 32 ) | |
gfxQuad:setUVRect ( 0, 0, 1, 1 ) | |
local function makeProp(x, y) | |
local prop = MOAIProp2D.new () | |
prop:setDeck ( gfxQuad ) | |
prop:setLoc ( x, y ) | |
layerProp:insertProp ( prop ) | |
end | |
local n = 6000 | |
initBuffers(n) | |
for i = 1, n do | |
x = math.random(-768, 768) | |
y = math.random(-1024, 1024) | |
addSprite(x, y) | |
makeProp(x, y) | |
end | |
vbo:bless() | |
mesh = MOAIMesh.new () | |
mesh:setTexture ( "moai.png" ) | |
mesh:setVertexBuffer ( vbo ) | |
mesh:setIndexBuffer ( ibo ) | |
mesh:setPrimType ( MOAIMesh.GL_TRIANGLES ) | |
prop = MOAIProp.new () | |
prop:setDeck ( mesh ) | |
layerMesh:insertProp ( prop ) | |
local function switch() | |
renderTable[1] = renderTable[1] == layerMesh and layerProp or layerMesh | |
end | |
local onTouch = function(eventType, idx, x, y, tapCount) | |
if eventType == MOAITouchSensor.TOUCH_UP then | |
switch() | |
end | |
end | |
local onClick = function(down) | |
if not down then | |
switch() | |
end | |
end | |
if MOAIInputMgr.device.touch then | |
MOAIInputMgr.device.touch:setCallback(onTouch) | |
else | |
MOAIInputMgr.device.mouseLeft:setCallback(onClick) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment