Here is a first working sample to run Love2D lua scripts with Moai SDK.
But there is one big problem and i don't have a solution yet!
To create an Object you have to create a texture using MOAIGfxQuad2D and afterwards you have use the texture to make an Object calling MOAIProp2D. Then you insert the object into the drawing layer.
In Moai SDK the draw operation is only an update operation. So it only updates the location of an existing object in the drawing layer.
So only one cloud is updated, just because we have only one cloud object.
Bad Idea 1:
Calling MOAIProp2D in the draw method is bad, just because everytime "love.graphics.draw" is called, an additional object is instantiated.
Idea 2
Maybe we have todo a list, to remember and check the instantiated object in the draw operation.
Any other ideas or solutions?
Moai file : "main.lua"
Code: Select all
print ( "hello, moai here is love!" )
require ("moai_love2d_wrapper")
require ("main1")
SCREEN_UNITS_X = 800
SCREEN_UNITS_Y = 600
SCREEN_WIDTH = SCREEN_UNITS_X
SCREEN_HEIGHT = SCREEN_UNITS_Y
MOAISim.openWindow ( "moai_love2d_wrapper_test", SCREEN_WIDTH, SCREEN_HEIGHT )
viewport = MOAIViewport.new ()
viewport:setScale ( SCREEN_UNITS_X, SCREEN_UNITS_Y )
viewport:setSize ( SCREEN_WIDTH, SCREEN_HEIGHT )
layer = MOAILayer2D.new ()
layer:setViewport ( viewport )
MOAISim.pushRenderPass ( layer )
love.load()
--==============================================================
-- game loop
--==============================================================
mainThread = MOAIThread.new ()
gameOver = false
mainThread:run (
function ()
local frames = 0
while not gameOver do
coroutine.yield ()
frames = frames + 1
-- call Love2D callbacks
love.update( MOAISim.framesToTime (frames) )
love.draw()
-- love.keypressed(k)
end
end
)
wrapper for love2d : "moai_love2d_wrapper.lua"
Code: Select all
love = {}
love.graphics = {}
function love.graphics.newImage(file)
local tex = MOAITexture.new ()
tex:load ( file )
local xtex, ytex = tex:getSize ()
local gfxQuad = MOAIGfxQuad2D.new ()
gfxQuad:setTexture ( tex )
gfxQuad:setRect ( xtex/2*-1, ytex/2*-1, xtex/2, ytex/2 )
-- return gfxQuad
local prop = MOAIProp2D.new ()
prop:setDeck ( gfxQuad )
prop:setLoc ( -1000, -1000 ) --hack to place the gfx ouside the screen
layer:insertProp ( prop )
return prop
end
function love.graphics.draw( prop, x, y, r, sx, sy, ox, oy )
prop:setLoc ( x-400, y-300 )
prop:forceUpdate ()
end
function love.graphics.setBackgroundColor() end
function love.graphics.setColor() end
function love.graphics.newImageFont() end
function love.graphics.quad() end
function love.graphics.setFont() end
function love.graphics.print() end
love.audio = {}
function love.audio.play() end
function love.audio.newSource() end
love.filesystem = {}
function love.filesystem.load() end
love2d sample "Passing Clouds Demo" : "main1.lua" <- take this from the love2d samples
Code: Select all
-------------------------------------------------
-- LOVE: Passing Clouds Demo
-- Website: http://love.sourceforge.net
-- Licence: ZLIB/libpng
-- Copyright (c) 2006-2009 LOVE Development Team
-------------------------------------------------
function love.load()
-- The amazing music.
music = love.audio.newSource("prondisk.xm")
-- The various images used.
body = love.graphics.newImage("body.png")
ear = love.graphics.newImage("ear.png")
face = love.graphics.newImage("face.png")
logo = love.graphics.newImage("love.png")
cloud = love.graphics.newImage("cloud_plain.png")
-- Set the background color to soothing pink.
love.graphics.setBackgroundColor(0xff, 0xf1, 0xf7)
-- Spawn some clouds.
for i=1,5 do
spawn_cloud(math.random(-100, 900), math.random(-100, 700), 80 + math.random(0, 50))
end
love.graphics.setColor(255, 255, 255, 200)
love.audio.play(music, 0)
end
function love.update(dt)
try_spawn_cloud(dt)
nekochan:update(dt)
-- Update clouds.
for k, c in ipairs(clouds) do
c.x = c.x + c.s * dt
end
end
function love.draw()
love.graphics.draw(logo, 400, 380, 0, 1, 1, 128, 64)
for k, c in ipairs(clouds) do
love.graphics.draw(cloud, c.x, c.y)
end
nekochan:render()
end
function love.keypressed(k)
if k == "r" then
love.filesystem.load("main.lua")()
end
end
nekochan = {
x = 400,
y = 250,
a = 0
}
function nekochan:update(dt)
self.a = self.a + 10 * dt
end
function nekochan:render()
love.graphics.draw(body, self.x, self.y, 0, 1, 1, 64, 64)
love.graphics.draw(face, self.x, self.y + math.sin(self.a/5) * 3, 0, 1, 1, 64, 64)
local r = 1 + math.sin(self.a*math.pi/20)
for i = 1,10 do
love.graphics.draw(ear, self.x, self.y, (i * math.pi*2/10) + self.a/10, 1, 1, 16, 64+10*r)
end
end
-- Holds the passing clouds.
clouds = {}
cloud_buffer = 0
cloud_interval = 1
-- Inserts a new cloud.
function try_spawn_cloud(dt)
cloud_buffer = cloud_buffer + dt
if cloud_buffer > cloud_interval then
cloud_buffer = 0
spawn_cloud(-512, math.random(-50, 500), 80 + math.random(0, 50))
end
end
function spawn_cloud(xpos, ypos, speed)
table.insert(clouds, { x = xpos, y = ypos, s = speed } )
end