Createing Endless Texture Background

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
OdnsRvns
Prole
Posts: 16
Joined: Sun Sep 14, 2014 6:43 pm

Createing Endless Texture Background

Post by OdnsRvns »

Working on a game project mainly for fun/myself. I'm to a problem right now as a new programmer that I'm not sure how to solve. I have a background texture(2048 x 2048) of a star field that moves with the WASD keys. I want to reuse this texture over and over again to create the illusion of movement through space. Basically creating a never ending star field. Anyone have an easy idea on how to accomplish this task. I'm sure its simple I just for the life of me cant put together something that works.

Thanks
Attachments
odns.love
(2.86 MiB) Downloaded 209 times
User avatar
Kyrremann
Prole
Posts: 28
Joined: Fri Mar 01, 2013 2:17 pm
Location: Oslo, Norway
Contact:

Re: Createing Endless Texture Background

Post by Kyrremann »

Depending on how smooth you want to make it, you could detect when player is nearing the end of the background image, and then just draw another one.

But I'm not sure if it's such a good idea to use that big of an image, it's a lot to draw that the user will not see. As long as your not expecting the user to sit on 2048 x 2048 screens. Since it's just start you could draw them randomly with love.graphic.point() or small love.graphics.rectangle(). Just drawing them randomly at a slow rate will simulate a simple starry sky.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Createing Endless Texture Background

Post by Azhukar »

Fraction your image into chunks, draw chunks based on intersection with drawn area.
Attachments
tiledImage.love
(11.24 KiB) Downloaded 237 times
User avatar
OdnsRvns
Prole
Posts: 16
Joined: Sun Sep 14, 2014 6:43 pm

Re: Createing Endless Texture Background

Post by OdnsRvns »

Azhukar wrote:Fraction your image into chunks, draw chunks based on intersection with drawn area.
Thanks can you walk me through how this works. I'm a new programmer so this is something I could use in other projects.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Createing Endless Texture Background

Post by Azhukar »

OdnsRvns wrote:can you walk me through how this works.
It separates input imageData into a 2d table of smaller images created by copying parts of the original imageData. These small square images are then drawn based on the input of :draw(). This is done by first determining the top left and bottom right chunks intersecting the input drawn area rectangle (x,y,width,height) and afterwards drawing the relevant images from the chunk table. Note that drawn images are loaded as needed, that individual chunks are processed only once they are requested and then they are memorized, this is to smooth out the loading/parsing of possibly huge images.

This ensures you're not needlessly drawing giant images as only fractions of the image is drawn by each chunk, it only draws as many chunks as is needed to fill the drawn area. You can specify the chunk size at objects creation, in case of the example it's set to 200.

To use it, all you need to do is determine which part of your space is being drawn and input the coordinates to the draw function. The draw function must be called after your coordinate translations as if you were drawing a world object.
Attachments
newTiledImage.lua
(2.35 KiB) Downloaded 203 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Createing Endless Texture Background

Post by s-ol »

You can also use a screen-sized quad and set the wrap options to repeat.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
OdnsRvns
Prole
Posts: 16
Joined: Sun Sep 14, 2014 6:43 pm

Re: Createing Endless Texture Background

Post by OdnsRvns »

S0lll0s wrote:You can also use a screen-sized quad and set the wrap options to repeat.
How would I go about doing that.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Createing Endless Texture Background

Post by micha »

OdnsRvns wrote:How would I go about doing that.
I attached a working .love.
The code is this:

Code: Select all

function love.load()
	thisQuad = love.graphics.newQuad(0,0,400,400,32,32)
	thisImage = love.graphics.newImage('tile.png')
	thisImage:setWrap('repeat','repeat')
end

function love.draw()
	love.graphics.draw(thisImage,thisQuad,0,0)
end
Note, that the quad is larger than the image! This is the important part.
Attachments
repeat.love
(2.28 KiB) Downloaded 211 times
User avatar
OdnsRvns
Prole
Posts: 16
Joined: Sun Sep 14, 2014 6:43 pm

Re: Createing Endless Texture Background

Post by OdnsRvns »

Ok I understand making the quad and repeating the image across the open window. My issue is as the player moves, I am moving the quad under him. How do I add and remove textures from the quad as needed to create a seem less world. Check out the .love file i edited for a better description. I'm trying to create the illusion of an open world/space.

CODE

Code: Select all

function love.load()
	width = love.window.getWidth()
	height = love.window.getHeight()
	px, py = 0, 0
	playerImage = love.graphics.newImage('player.png')
	thisImage = love.graphics.newImage('tile.png')
	thisQuad = love.graphics.newQuad(px,py,width,height,32,32)
	thisImage:setWrap('repeat','repeat') 
end

function love.update( dt )
	if love.keyboard.isDown('a') then px = px + 1 end
	if love.keyboard.isDown('d') then px = px - 1 end
	if love.keyboard.isDown('w') then py = py + 1 end
	if love.keyboard.isDown('s') then py = py - 1 end
end

function love.draw()
	love.graphics.draw(thisImage,thisQuad,px,py)
	love.graphics.print(px .. 'x:y' .. py, 5, 5) 
	love.graphics.print(width .. ':' .. height, 5, 20)
	love.graphics.draw(playerImage,width/2,height/2)
end

Attachments
repeat_edited.zip
(6.99 KiB) Downloaded 158 times
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Createing Endless Texture Background

Post by Ref »

This is what micha is suggesting.
Can obviously improve math to make more generic.
Attachments
imageWrap.love
Simple example of micha's wrap
(46.32 KiB) Downloaded 245 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 152 guests