forked from SiENcE/lovemoai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoai_love2d_wrapper.lua
More file actions
213 lines (168 loc) · 5.31 KB
/
Copy pathmoai_love2d_wrapper.lua
File metadata and controls
213 lines (168 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
love = {}
love.graphics = {}
love.keyboard = {}
love.mouse = {}
love.audio = {}
MOAILove2D = {}
MOAILove2D.dir = ""
local __prop_instances = {}
local __gColor = { 1, 1, 1, 1 }
local __renderlist = {}
function MOAILove2D.convertFilename( filename )
return MOAILove2D.dir .. filename
end
function MOAILove2D.load( filename )
MOAILove2D.dir = filename;
require ( MOAILove2D.convertFilename( "conf" ) )
require ( MOAILove2D.convertFilename( "main" ) )
config = { title = "moai_love2d_wrapper_test" }
if love.conf then
love.conf( config )
end
SCREEN_UNITS_X = 800
SCREEN_UNITS_Y = 600
SCREEN_WIDTH = SCREEN_UNITS_X
SCREEN_HEIGHT = SCREEN_UNITS_Y
MOAISim.openWindow ( config.title, SCREEN_WIDTH, SCREEN_HEIGHT )
MOAISim.setStep ( 1 / 60 )
MOAISim.clearLoopFlags()
MOAISim.setLoopFlags ( MOAISim.SIM_LOOP_ALLOW_BOOST )
MOAISim.setBoostThreshold ( 0 )
viewport = MOAIViewport.new ()
viewport:setSize ( SCREEN_WIDTH, SCREEN_HEIGHT )
viewport:setScale ( SCREEN_UNITS_X, -SCREEN_UNITS_Y )
viewport:setOffset( -1, 1 )
layer = MOAILayer2D.new ()
layer:setViewport ( viewport )
MOAISim.pushRenderPass( layer )
textbox = MOAITextBox.new ()
textbox:setRect ( -150, -230, 150, 230 )
textbox:setYFlip ( true )
layer:insertProp ( textbox )
if MOAIUntzSystem then
MOAIUntzSystem.initialize()
end
love.load()
--==============================================================
-- game loop
--==============================================================
mainThread = MOAIThread.new ()
gameOver = false
mainThread:run (
function ()
local lastTime = MOAISim.getElapsedTime()
while not gameOver do
coroutine.yield ()
local curTime = MOAISim.getElapsedTime()
local dt = curTime - lastTime;
lastTime = curTime;
-- call Love2D callbacks
love.update( dt )
love.graphics:___renderinit()
love.draw()
love.graphics:___render()
-- love.keypressed(k)
end
end
)
end
local function clonePropInstance(prop)
local copy = MOAIProp2D.new()
copy:setDeck(prop.gfxQuad)
layer:insertProp(copy)
table.insert(__prop_instances[prop], copy)
return copy
end
function love.graphics.newImage(file)
local prop = MOAIProp2D.new ()
prop.texture = MOAITexture.new ()
prop.texture:load ( MOAILove2D.convertFilename( file ) )
local xtex, ytex = prop.texture:getSize ()
prop.gfxQuad = MOAIGfxQuad2D.new ()
prop.gfxQuad:setTexture ( prop.texture )
prop.gfxQuad:setRect ( 0, 0, xtex, ytex )
prop.gfxQuad:setUVRect( 0, 0, 1, 1 )
prop:setDeck(prop.gfxQuad )
layer:insertProp ( prop )
__prop_instances[prop] = {index=1; prop }
return prop
end
function love.graphics:___renderinit()
__renderlist = {}
end
function love.graphics:___render()
-- new frame; reset instance index
for _,prop in pairs(__prop_instances) do
prop.index = 1
end
for _,renderitem in ipairs(__renderlist) do
local prop, x, y, r, sx, sy, ox, oy = unpack(renderitem)
-- get an instance of this prop, creating a new one if all existing ones have been used
local instances = __prop_instances[prop]
if instances.index > #instances then
clonePropInstance(prop)
end
local renderprop = instances[instances.index]
instances.index = instances.index + 1
renderprop:setPiv( ox, oy );
renderprop:setLoc( x, y );
renderprop:setRot( math.deg(r) );
renderprop:setColor( unpack( __gColor ) )
renderprop:setBlendMode( MOAIProp2D.GL_SRC_ALPHA, MOAIProp2D.GL_ONE_MINUS_SRC_ALPHA )
end
end
function love.graphics.draw( prop, x, y, r, sx, sy, ox, oy )
table.insert(__renderlist, {prop, x, y, r or 0, sx or 1, sy or 1, ox or 0, oy or 0 })
end
local function color( r,g,b,a )
local function convert(c) return math.max( math.min( (c or 255 ) / 255, 1 ), 0 ) end
return convert(r), convert(g), convert(b), convert(a)
end
function love.graphics.setBackgroundColor( r, g, b )
MOAIGfxDevice.setClearColor( color( r,g,b ) )
end
function love.graphics.setColor( r,g,b,a)
__gColor = { color(r,g,b,a) }
end
-- FONT rendering
function love.graphics.newImageFont( fontimage, charcodes)
local font = MOAIFont.new ()
font:load ( fontimage, charcodes )
return font
end
function love.graphics.setFont( fontset )
textbox:setFont ( fontset )
textbox:setTextSize ( fontset:getScale ())
end
function love.graphics.print( text )
textbox:setString ( text )
end
function love.graphics.quad() end
function love.graphics.line() end
function love.graphics.setBlendMode() end
function love.graphics.circle() end
function love.graphics.setCaption() end
function love.graphics.rectangle() end
function love.graphics.newQuad() end
function love.graphics.drawq() end
function love.keyboard.isDown() end
function love.mouse.isDown() end
function love.mouse.getX() end
function love.mouse.getY() end
if MOAIUntzSystem then
function love.audio.play( loveSnd )
loveSnd.__untz:play()
end
function love.audio.newSource( file )
loveSnd = {}
loveSnd.__untz = MOAIUntzSound.new()
loveSnd.__untz:load( MOAILove2D.convertFilename( file ) )
loveSnd.__untz:setLooping(true)
return loveSnd
end
else
function love.audio.play() end
function love.audio.newSource() end
end
love.filesystem = {}
function love.filesystem.load() end