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
Createing Endless Texture Background
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Createing Endless Texture Background
- Attachments
-
- odns.love
- (2.86 MiB) Downloaded 225 times
Re: Createing Endless Texture Background
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.
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.
Re: Createing Endless Texture Background
Fraction your image into chunks, draw chunks based on intersection with drawn area.
- Attachments
-
- tiledImage.love
- (11.24 KiB) Downloaded 257 times
Re: Createing Endless Texture Background
Thanks can you walk me through how this works. I'm a new programmer so this is something I could use in other projects.Azhukar wrote:Fraction your image into chunks, draw chunks based on intersection with drawn area.
Re: Createing Endless Texture Background
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.OdnsRvns wrote:can you walk me through how this works.
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 213 times
Re: Createing Endless Texture Background
You can also use a screen-sized quad and set the wrap options to repeat.
Re: Createing Endless Texture Background
How would I go about doing that.S0lll0s wrote:You can also use a screen-sized quad and set the wrap options to repeat.
Re: Createing Endless Texture Background
I attached a working .love.OdnsRvns wrote:How would I go about doing that.
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
- Attachments
-
- repeat.love
- (2.28 KiB) Downloaded 223 times
Check out my blog on gamedev
Re: Createing Endless Texture Background
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
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 176 times
Re: Createing Endless Texture Background
This is what micha is suggesting.
Can obviously improve math to make more generic.
Can obviously improve math to make more generic.
- Attachments
-
- imageWrap.love
- Simple example of micha's wrap
- (46.32 KiB) Downloaded 262 times
Who is online
Users browsing this forum: Ahrefs [Bot] and 8 guests