Page 3 of 4

Re: A very simple camera lib

Posted: Mon Aug 20, 2012 8:55 am
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.

Re: A very simple camera lib

Posted: Mon Aug 20, 2012 8:57 am
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?

Re: A very simple camera lib

Posted: Mon Aug 20, 2012 9:10 am
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.

Re: A very simple camera lib

Posted: Mon Aug 20, 2012 2:50 pm
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.

Re: A very simple camera lib

Posted: Mon Aug 20, 2012 5:46 pm
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?

Re: A very simple camera lib

Posted: Mon Aug 20, 2012 7:05 pm
by Nixola
Using his lib, I think

Re: A very simple camera lib

Posted: Mon Aug 20, 2012 9:34 pm
by kikito
Yeah, the "action" meant "using the lib".

Re: A very simple camera lib

Posted: Sat Sep 29, 2012 12:37 pm
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

Re: A very simple camera lib

Posted: Sat Sep 29, 2012 6:29 pm
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!

Re: A very simple camera lib

Posted: Sat Sep 29, 2012 7:56 pm
by luaz
Thanks, it certainly does! This is much better than what I had previously. :)

Extra karma for you! ;)