MOAIGfxQuadDeck2D issues

Discussion about using Moai SDK - post questions, issues, and ideas here.

Moderators: naturally, seebs

MOAIGfxQuadDeck2D issues

Postby Mantic » Sat Apr 27, 2013 3:37 pm

I'm having issues properly using MOAIGfxQuadDeck2D. I have one texture that includes multiple images on it, and I want to render different parts of the texture into different props. Here's what I understand:

setQuad defines a rectangle for a subimage on the texture (for a given index).
setRectangle defines how that quad will be rendered (scaled, aspect ratio different, etc).

Code: Select all
  1.  

  2.         local deck = MOAIGfxQuadDeck2D.new()

  3.         deck:setTexture('art/mainmenu.png')

  4.         deck:reserve(#buttons)

  5.        

  6.         for i,b in ipairs(buttons) do

  7.                 deck:setQuad(i, b.x,b.y, b.x+b.width,b.y, b.x+b.width,b.y+b.height, b.x,b.y+b.height)

  8.                 deck:setRect(i, -b.width,b.height, b.width*0.5,-b.height*0.5)

  9.         end

  10.  

  11.         local bplay = MOAIProp2D.new()

  12.         bplay:setDeck(deck)

  13.         bplay:setLoc(50,50)

  14.         bplay:setIndex(1)

  15.         self.layer._layer:insertProp(bplay)

  16.  



With this code, my entire image is being rendered, instead of just a subset.

I tried changing the texture, as per ezraanderson's suggestion http://getmoai.com/forums/ludumdare-26-anyone-t1801/?sid=4a107ad01eb1825276a793b6869b2e66#p9832, but that just made nothing render. At least with the code above i can render something on the screen.
Mantic
 
Posts: 5
Joined: Sat Apr 27, 2013 11:59 am

Re: MOAIGfxQuadDeck2D issues

Postby ezraanderson » Sun Apr 28, 2013 12:05 am

>
sorry man, maybe i shouldn't post if i don't have my code on hand, this is how i would do it. Not sure if it would work from a MOAIGfxQuadDeck2D.new()

Check moai samples
https://github.com/moai/moai-dev/blob/m ... D/main.lua


Code: Select all
  1.  

  2. --1. texture

  3.  

  4. --   local texture =   MOAIImageTexture.new() --1.3    

  5. --   local texture =   MOAITexture.new()       --1.4  

  6.  

  7. texture         =    MOAITexture.new()    

  8. texture:load( "test.png", MOAIImage.PREMULTIPLY_ALPHA   )  

  9.  

  10.  

  11. --1. deck

  12.  

  13. tileDeck = MOAITileDeck2D.new ()

  14. tileDeck:setTexture ( texture )

  15. tileDeck:setSize ( 2, 2 )  ---image is 128x128 4 images of 64x64

  16. tileDeck:setRect ( -64, -64, 64, 64 )

  17.  

  18.  

  19. --prop a

  20.  

  21. prop_A = MOAIProp2D.new ()

  22. prop_A:setDeck ( tileDeck )

  23. prop_A:setIndex(1) --change image

  24. prop_A:setLoc(0,100)    

  25. layer:insertProp ( prop_A )

  26.  

  27.  

  28. --prop b

  29.  

  30. prop_B = MOAIProp2D.new ()

  31. prop_B:setDeck ( tileDeck )

  32. prop_B:setLoc(150,100)

  33. prop_B:setIndex(2)   --change image

  34.  

  35.  

  36. layer:insertProp ( prop_B )

  37.  

  38.  

  39.  

  40.  

Dead Dark: A roguelike, zombie apocalypse survival game
www.dead-dark.com
User avatar
ezraanderson
 
Posts: 779
Joined: Wed Nov 21, 2012 2:24 pm
Location: Canada

Re: MOAIGfxQuadDeck2D issues

Postby Mantic » Mon Apr 29, 2013 9:11 am

MOAITileDeck2D seems to be the wrong answer. It assumes that each image is the same size, which in my case they aren't. I'm trying to map images of arbitrary sizes off of a sheet that would look something like this: http://goo.gl/wHBDM (random spritesheet taken off google), eg: it would have multiple images of different sizes.

This article (http://getmoai.com/wiki/index.php?title=GfxQuadListDeck2D) says:
MOAIGfxQuadDeck2D may be used to draw a list of textured quads selected, by index, from a set of such lists of quads. All quads share the same texture but may have different model and UV coordinates.


And from another site (http://highoncoding.com/Articles/795_Moai_Basics_Part_1.aspx):
MOAIGfxQuadDeck2D – An array of textured quads (from one texture). Use this for basic sprite sheets and texture atlases.


This sounds amazing, but why can't I get it to work? Also, can MOAIGfxQuadDeck2D support more than one prop?
Mantic
 
Posts: 5
Joined: Sat Apr 27, 2013 11:59 am

Re: MOAIGfxQuadDeck2D issues

Postby ezraanderson » Mon Apr 29, 2013 10:54 am

maybe try
http://www.codeandweb.com/texturepacker
and use muzzs snippet at
http://www.moaisnippets.info/fixed-text ... r-importer

Original Moai examples
https://github.com/moai/moai-dev/blob/m ... er/out.lua
https://github.com/moai/moai-dev/blob/m ... r/main.lua


If you can get it to work, you should have all the pieces of data and code needed to figure it out, also post the rest of your code, its hard to guess what you have set for these variables.
Code: Select all
  1.  

  2.          deck:setQuad(i, b.x,b.y, b.x+b.width,b.y, b.x+b.width,b.y+b.height, b.x,b.y+b.height)

  3.          deck:setRect(i, -b.width,b.height, b.width*0.5,-b.height*0.5)

  4.  

Dead Dark: A roguelike, zombie apocalypse survival game
www.dead-dark.com
User avatar
ezraanderson
 
Posts: 779
Joined: Wed Nov 21, 2012 2:24 pm
Location: Canada

Re: MOAIGfxQuadDeck2D issues

Postby Mantic » Mon Apr 29, 2013 9:49 pm

FINALLY.

So, here's something I forgot to mention... I have my project setup in non simulation mode.... :oops: eg:

Code: Select all
  1. viewport:setScale(ww, -wh)

  2. viewport:setOffset(-1,1)



Thus rendering 0,0 at the top left with X and Y increasing to the right and downward. (My preferred mode for menus.)

When it comes to calling :setUVQuad (or :setQuad), winding becomes super important.

Code: Select all
  1. -- For regular viewport settings:

  2. -- deck:setUVQuad( vertex1, vertex2, vertex3, vertex4)

  3. deck:setUVQuad(i, 0,0, 0.3,0, 0.3,0.3, 0,0.3)



When the non-sim mode (as I call it) is used on the viewport, then everything is flipped upside down. Therefor we need to reverse the winding in order to get the proper result:

Code: Select all
  1. -- "Non-sim" mode winding:

  2. -- deck:setUVQuad( vertex4, vertex3, vertex2, vertex1)

  3. deck:setUVQuad(i, 0,0.3, 0.3,0.3, 0.3,0, 0,0)



Unfortunately this doesn't seem to work with :setQuad, as the input is processed differently (being pixels). I was failing at first because I only had pixel data, not UV data, for the sub-images. Now with a little hack, it seems to work just fine:

Code: Select all
  1. local b = {x=0,y=0,width=100,height=120}

  2. local tw,th = texture:getSize()

  3. deck:setUVQuad(1, b.x/tw,(b.y+b.height)/th, (b.x+b.width)/tw,(b.y+b.height)/th, (b.x+b.width)/tw,b.y/th, b.x/tw,b.y/th)

  4.  

  5. -- luckily setRect still works normally:

  6. deck:setRect(1, 0,0, b.width,b.height )

  7.  

  8. local bplay = MOAIProp2D.new()

  9. bplay:setDeck(deck)

  10. bplay:setIndex(1)

  11. bplay:setLoc(50,50)

  12. layer:insertProp(bplay)



I'm sure there might be some way to reverse the winding with setQuad, but my measly 2 attempts didn't yield anything. Anyhow... Now onward to progress!
Mantic
 
Posts: 5
Joined: Sat Apr 27, 2013 11:59 am


Return to Moai SDK

Who is online

Users browsing this forum: No registered users and 2 guests

x