Created
February 16, 2014 00:07
-
-
Save TannerRogalsky/9027221 to your computer and use it in GitHub Desktop.
segfaulting love2d with coroutines and user data
This file contains hidden or 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
function love.load(args) | |
love.physics.setMeter(60) | |
World = love.physics.newWorld(0, 10 * 16, true) | |
World:setCallbacks(function(fixture_a, fixture_b, contact) | |
print(fixture_a, fixture_b) | |
local data_a, data_b = fixture_a:getUserData(), fixture_b:getUserData() | |
print("I never run because of segfault.") | |
end) | |
coroutine_function = coroutine.create(function() | |
for i=1,3 do | |
local body = love.physics.newBody(World, love.graphics.getWidth() / 2, love.graphics.getHeight() - 100 + 30 * i, "static") | |
local shape = love.physics.newRectangleShape(love.graphics.getWidth(), 20) | |
local fixture = love.physics.newFixture(body, shape) | |
fixture:setUserData("test" .. i) | |
coroutine.yield() | |
end | |
end) | |
local body = love.physics.newBody(World, love.graphics.getWidth() / 2, 30, "dynamic") | |
local shape = love.physics.newRectangleShape(20, 20) | |
local fixture = love.physics.newFixture(body, shape) | |
end | |
function love.update(dt) | |
World:update(dt) | |
if coroutine.status(coroutine_function) ~= "dead" then | |
print(coroutine.resume(coroutine_function)) | |
end | |
end | |
function love.keypressed(key, unicode) | |
if key == "escape" then | |
love.event.push("quit") | |
end | |
end | |
function love.draw() | |
love.graphics.setColor(0, 255, 0) | |
for _, body in ipairs(World:getBodyList()) do | |
for _, fixture in ipairs(body:getFixtureList()) do | |
local shape = fixture:getShape() | |
love.graphics.polygon("fill", body:getWorldPoints(shape:getPoints())) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment