A very simple camera lib

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: A very simple camera lib

Post by Roland_Yonaba »

Petunien wrote: @kikito
The MIT license allows it, but I'd prefer to ask for permission... may I change your library and implement new functions?
That's the purpose of MIT License. Just fork it and make whatever you want.
Just make sure to MIT License your modifications, though.
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: A very simple camera lib

Post by Petunien »

It's possible to share a game under a different license?

So, the library is MIT and the game not?

Or has it to be the same?
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: A very simple camera lib

Post by Roland_Yonaba »

Petunien wrote:It's possible to share a game under a different license?
So, the library is MIT and the game not?
Or has it to be the same?
No, I don't think so. IMHO.
A part of the work can be MIT Licensed, while the rest can be released on another license.
You will find a lot of projects released under ZLib, or Creative Commons, or GPL using MIT-Licensed third-parties.
I think that the purpose of the MIT-License here is just to make sure the copyright holder is acknowledged.

EDIT : You can have a look at this for additional informations.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: A very simple camera lib

Post by kikito »

You can use a MIT-licensed library in a game licensed by another lib. It depends on how the other lib works, of course (for example, you can not use it in a game that uses a lib that says "this lib can't be used in conjunction with any MIT-licensed library". Don't give me that look; if I remember correctly Microsoft used some kind of anti-opensource license which worked like that some years ago. Or maybe I dreamed about it)

The only thing you *must* do regarding the lib, at according to the MIT version I'm using, is including the text of the license file "somewhere public" in your game. It could be in the game's folder, or inside a file called external-licenses.txt with more licenses of other libraries inside it. Besides that, you can do pretty much whatever you want: use it in in a closed-source game, for example, is ok in general.

It would be also be nice if you included my name in your game, maybe in the credits or something. But that is not mandatory.

There is also one thing you *cannot* do, which is to sue me. If you are using that lib, you accepting that you will not sue me for any harm, trouble or any other reason occurred as a consequence of that action.
When I write def I mean function.
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: A very simple camera lib

Post by Petunien »

Thank you very much for your explanation. :)

I repeat my said, it's not that sure that my game will be released. ^^
But if so, you'll be mentioned in the credits.

Oh, in your last sentence, which action do you mean?
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: A very simple camera lib

Post by Nixola »

Using his lib, I think
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: A very simple camera lib

Post by kikito »

Yeah, the "action" meant "using the lib".
When I write def I mean function.
luaz
Citizen
Posts: 83
Joined: Sun Sep 16, 2012 2:55 pm

Re: A very simple camera lib

Post by luaz »

Could you give an example of how to implement this with Advanced Tile Loader?

Here's the original question, unrelated to this lib: viewtopic.php?f=4&t=11107
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! ;) I don't learn very well from references, etc....
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: A very simple camera lib

Post by kikito »

luaz wrote:Could you give an example of how to implement this with Advanced Tile Loader?
Sure. I'm going to assume that you already have something working - you can draw stuff, but you are not "scrolling" yet.

Step 1 is to download the lib into your game's folder. You can download it in the root folder if you want, or you can download it into a lib/ folder.

Step 2 is requiring the library. You will want to do this in the file that has the love.load function and the love.draw function.

Code: Select all

local camera = require 'camera' -- or require 'lib.camera' if you downloaded camera.lua to a lib/ folder
Step 3 is initializing the camera with the size of your world. This can be done inside love.load() like this:

Code: Select all

function love.load()
  ... -- your stuff
  camera.setBoundary(0,0, WORLD_WIDTH, WORLD_HEIGHT) -- Width and height are in pixels
end
Step 4 is optional. If you never change the screen dimensions from within the game, you don't need it. If you do change it, (for example, via a Grapical Options menu), you must inform camera like this:

Code: Select all

function changeScreenResoultion()
... -- your stuff
  camera.setBoundary(0,0, love.graphics.getWidth(), love.graphics.getHeight())
end
The library does this once at the beginning automatically, so if you don't change the screen size, you don't need this step.

Step 5 is making the camera look at the player. Usually you do this inside love.update(), after moving the player. Or in love.draw(), at the very beginning.

Code: Select all

... -- inside love.update(dt) after moving the player, or at the beginning of love.draw()
  camera.lookAt(player.x, player.y) -- you can choose any other thing for the camera to look at, player is just the most common one
Step 6 is modifying the code that draws the "part that you want to scroll" (this is usually the code that draws the tiles, the player and the monsters) so that it uses the camera lib. It is also worth mentioning that usually you want some drawing code to stay out of the scrolling - for example the game HUD (the score, the "hearts", etc).

So, if your drawing code looks like this:

Code: Select all

function love.draw()
  drawTiles()
  drawEnemies()
  drawPlayer()
  drawHUD()
end
You "wrap" the scrollable part inside camera.draw, like this:

Code: Select all

function love.draw()
  camera.draw(function()
    drawTiles()
    drawEnemies()
    drawPlayer()
  end)
  drawHUD() -- HUD is left out of camera because it doesn't scroll
end
That's pretty much it.

Bonus step: The camera.draw lib has 4 parameters - left, top, width and height. This is the rectangle that the camera is using. You can use that rectangle to limit the amount of tiles that you draw. The exact way to do this depends on how your data is structured, but the code should look similar to this:

Code: Select all

function love.draw()
  camera.draw(function(left, top, width, height)
    drawTilesInsideRectangle(left, top, width, height)
    drawEnemiesInsideRectangle(left, top, width, height)
    drawPlayerIfInsideRectangle(left, top, width, height)
  end)
  drawHUD() -- HUD is left out of camera because it doesn't scroll
end
I hope this helps. Regards, and good luck!
When I write def I mean function.
luaz
Citizen
Posts: 83
Joined: Sun Sep 16, 2012 2:55 pm

Re: A very simple camera lib

Post by luaz »

Thanks, it certainly does! This is much better than what I had previously. :)

Extra karma for you! ;)
If you're going to reply to my post, consider posting an (preferably working) example - 99.7% of time time, I already know how to implement the feature theoretically! ;) I don't learn very well from references, etc....
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 142 guests