Skip to content

Instantly share code, notes, and snippets.

@ihaveamac
Last active December 16, 2015 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihaveamac/96e402b132ca3b6a21af to your computer and use it in GitHub Desktop.
Save ihaveamac/96e402b132ca3b6a21af to your computer and use it in GitHub Desktop.
it scrolls an image thing - using lpp-3ds - https://www.youtube.com/watch?v=d1AI4syhu1U
Screen.enable3D()
Graphics.init()
-- this should always be below 1
friction = 0.9
-- these are set while the page is moving
y_position = 0
y_velocity = 0
dragging = false
c_white = Color.new(255, 255, 255)
c_grey = Color.new(127, 127, 127)
c_blue = Color.new(0, 0, 255)
c_red = Color.new(255, 0, 0)
c_dark_red = Color.new(127, 0, 0)
c_black = Color.new(0, 0, 0)
c_black_tl = Color.new(0, 0, 0, 50)
function createPage(func)
local width, height = 320, 720
local img = Screen.createImage(width, height, c_black)
-- argument #3 to createImage seems to not work properly sometimes?
Screen.fillRect(1, width, 1, height, c_dark_red, img)
if func then
func(img, width, height)
end
local gimg = Graphics.convertFrom(img)
Screen.freeImage(img)
return gimg, width, height
end
function drawDebug()
Screen.debugPrint(5, 5, "y_position: "..y_position, c_white, BOTTOM_SCREEN)
Screen.debugPrint(5, 20, "y_velocity: "..y_velocity, c_white, BOTTOM_SCREEN)
Screen.debugPrint(5, 40, "friction: "..friction, c_white, BOTTOM_SCREEN)
Screen.debugPrint(5, 55, "DRight: +0.05 - DLeft: -0.05", c_white, BOTTOM_SCREEN)
end
page1, page1_width, page1_height = createPage(function(img, width, height)
Screen.debugPrint(5, 5, "Hello!", c_white, img)
Screen.fillRect(70, 100, 245, 480, c_grey, img)
Screen.debugPrint(20, 250, "Hello again!", c_white, img)
Screen.debugPrint(50, 265, "This isn't a waste", c_white, img)
Screen.fillRect(30, 60, 280, 320, c_white, img)
Screen.fillRect(70, 100, 280, 480, c_grey, img)
Screen.debugPrint(5, 550, "I should put sample text here", c_grey, img)
end)
frame = 0
while true do
frame = frame + 1
Screen.waitVblankStart()
Screen.refresh()
Screen.clear(TOP_SCREEN)
Screen.clear(BOTTOM_SCREEN)
lvl_3d = Screen.get3DLevel()
pad = Controls.read()
tx, ty = Controls.readTouch()
touching = Controls.check(pad, KEY_TOUCH)
showdebug = Controls.check(pad, KEY_L) or Controls.check(pad, KEY_R)
if touching and not dragging then
dragging = true
y_velocity = 0
otx, oty = tx, ty
end
if touching and dragging then
y_position = math.min(math.max(y_position + (ty - oty), -page1_height + 240), 0)
y_velocity = ty - oty
end
if not touching and dragging then
dragging = false
end
if not touching and not dragging and math.abs(y_velocity) > 0.1 then
--y_position = math.min(math.max(y_position + y_velocity, -240), 0)
y_position = math.min(math.max(y_position + y_velocity, -page1_height + 240), 0)
y_velocity = y_velocity * friction
end
if Controls.check(pad, KEY_DLEFT) and not Controls.check(oldpad, KEY_DLEFT) then
friction = math.min(friction + .05, .95)
end
if Controls.check(pad, KEY_DRIGHT) and not Controls.check(oldpad, KEY_DRIGHT) then
friction = math.max(friction - .05, .05)
end
if Controls.check(pad, KEY_START) then
Graphics.term()
System.exit()
end
oldpad = pad
otx, oty = tx, ty
Graphics.initBlend(BOTTOM_SCREEN)
Graphics.drawPartialImage(0, 0, 0, math.floor(-y_position + .5), 400, 240, page1)
Graphics.termBlend()
Graphics.initBlend(TOP_SCREEN, LEFT_EYE)
Graphics.drawPartialImage(40 - math.floor(10 * lvl_3d + .5), -math.min(math.max(math.floor(-y_position + .5), 0), 240) + 240, 0, math.max(math.floor(-y_position + .5) - 240, 0), 400, 240, page1)
Graphics.termBlend()
Graphics.initBlend(TOP_SCREEN, RIGHT_EYE)
Graphics.drawPartialImage(40 + math.floor(10 * lvl_3d + .5), -math.min(math.max(math.floor(-y_position + .5), 0), 240) + 240, 0, math.max(math.floor(-y_position + .5) - 240, 0), 400, 240, page1)
Graphics.termBlend()
if showdebug then
drawDebug()
end
Screen.flip()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment