Page 1 of 3

Playmat - A Mode 7 Library

Posted: Tue Jan 03, 2017 8:08 am
by hatninja
Borrowing retrotail's fast shader code, i've decided to create a library that helps the mode 7 effect become much more accessible. The mode 7 plane is well documented, but sprites were a big difficulty. After hitting that wall myself, i've finally figured it out, so i hope playmat helps you out too!

Playmat
A strong utility for making mode 7 games from within love2d!

Features:
  • Fast to set up!
  • Cool helper functions.
  • Versatile sprite system.
  • Ability to run on slow computers.
Example Code:

Code: Select all

PM = require("playmat")

mapimg = love.graphics.newImage("map.png")
spriteimg = love.graphics.newImage("sprite.png")

cam = PM.newCamera() --Sets up a new camera, resolution is 800 x 600 by default.

cam:setPosition(100,50)
cam:setZoom(48)

function love.draw()
	PM.drawPlane(cam, mapimg) --The image drawn as a plane.
	
	PM.placeSprite(cam, spriteimg, 125,100, 0, 8,8)
	PM.renderSprites(cam) --And now the sprite is there too!
end
Changelog:

Code: Select all

v1.5 - Experimentally better
Sprites can now be colored!
Planes are stretchy now.
Planes now render directly using the shader.
Some major code improvements.

v1.2 - Minor
Fixed Sprite scaling problems

v1.1 - Perfection
Fixed non-square images not working right.
Added plane wrapping feature!

v1.0.3 - Hotfixes
The demo has also been updated to fix crashing for some.

v1.0 - Made public!
Find it here:
Github

Feedback would be greatly appreciated!

Demo controls:
wasd - Movement
q & e - Rotate
- & + - Zoom in and out
i & o - Change FOV
k & l - Change Offset

Re: Playmat - A Mode 7 Library

Posted: Tue Jan 03, 2017 9:26 am
by zorg
hatninja wrote:(...)
Didn't try it out (yet), but i did look at the code a bit;

Code: Select all

local size = ((1/distance)/cam.z*cam.o)*800 -- line 162
I'm assuming that magic number there wanted to be the width of the screen? Or is it more magic than that? :3

Re: Playmat - A Mode 7 Library

Posted: Tue Jan 03, 2017 10:19 am
by hatninja
It was an approximation, but i'm still surprised i didn't catch that! Sounds like it would be the case!

Re: Playmat - A Mode 7 Library

Posted: Tue Jan 03, 2017 11:52 am
by zorg
I remember myself wanting to calculate aperture, fov and z distance relations for my own camera implementation too, was quite suprised that the equation turned out simpler than i imagined... which i did want to share, if only for the fact that with some "basic" values, like 90° field-of-view and similarly default numbers, what it gave me back was exactly half of the value i was expecting... but yeah, sadly i can' find my code, (nor the equations i actually wrote out into one of my notebooks) so i can't be more clear than this. :monocle:

Re: Playmat - A Mode 7 Library

Posted: Wed Jan 04, 2017 5:08 am
by Jasoco
This is amazing.

Re: Playmat - A Mode 7 Library

Posted: Wed Jan 04, 2017 5:43 am
by pgimeno
I'm getting this:

Code: Select all

Error: playmat.lua:125: Shader uniform 'r' does not exist.
A common error is to define but not use the variable.
stack traceback:
	[C]: in function 'send'
	playmat.lua:125: in function 'drawPlane'
	main.lua:26: in function 'draw'
	[string "boot.lua"]:468: in function <[string "boot.lua"]:436>
	[C]: in function 'xpcall'
I've got that sometimes myself. An extern variable is optimized out and that causes an error. I removed the 'extern r = 0' and the shader:send('r'...) lines and then it worked.

Awesome work!

Re: Playmat - A Mode 7 Library

Posted: Wed Jan 04, 2017 6:01 am
by Jasoco
What version of Löve do you have? What GPU and drivers? It worked for me on 0.10.2.

Re: Playmat - A Mode 7 Library

Posted: Wed Jan 04, 2017 6:09 am
by Davidobot
Jasoco wrote:This is amazing.
Does this mean that our Raycasters can get a proper floor now? :awesome:

Re: Playmat - A Mode 7 Library

Posted: Wed Jan 04, 2017 8:18 am
by Jasoco
Davidobot wrote:
Jasoco wrote:This is amazing.
Does this mean that our Raycasters can get a proper floor now? :awesome:
I was wondering that myself. Maybe you should do some experimenting. It'd be figuring out the correct angles to use to make it line up properly.

Re: Playmat - A Mode 7 Library

Posted: Wed Jan 04, 2017 8:33 am
by yetneverdone
wow, very awesome. Possible to make doom-esque fps?