drawing quad seems to be rotated

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.
Post Reply
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

drawing quad seems to be rotated

Post by Shadowblitz16 »

can someone explain to me why this is happening?

its acting like I am using..
love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )
instead of..
love.graphics.draw( texture, quad, x, y, r, sx, sy, ox, oy, kx, ky )
where it thinks i am passing y as r


here is my code.

Code: Select all

local gfx = 
{
    image  = nil,
    imageW = 0,
    imageH = 0,
    quad   = nil,
    quadW  = 0,
    quadH  = 0  
}


local palette = 
{
    {000, 000, 000}, {032, 051, 123}, {126, 037, 083}, {000, 131, 049},
    {171, 082, 054}, {069, 069, 069}, {194, 195, 199}, {255, 241, 232},
    {255, 000, 077}, {255, 163, 000}, {255, 231, 039}, {000, 226, 050},
    {041, 173, 255}, {131, 118, 156}, {255, 119, 168}, {255, 204, 170}

}
local shader0 = 
[[
    extern vec4 oldColor;
    extern vec4 newColor;
    vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
        vec4 pixel = Texel(texture, texture_coords );//This is the current pixel color
        if(pixel == oldColor) return newColor * oldColor;
        return pixel * oldColor;
    }   
]]

function gfx:new(image)
    self.image  = love.graphics.newImage(image);
    self.imageW = self.image:getWidth()
    self.imageH = self.image:getHeight()
    self.quad   = love.graphics.newQuad(0,0,8,8,self.image:getDimensions())
    self.quadW  = 8
    self.quadH  = 8

    self.image:setFilter("nearest", "nearest" )
end

function gfx:pal(oldColor, newColor)
    local shader = love.graphics.newShader(shader0)
    shader:sendColor("oldColor", unpack(palette[oldColor]))
    shader:sendColor("newColor", unpack(palette[newColor]))
    love.graphics.setShader(shader)
end

function gfx:pal()
    love.graphics.setShader()
end

function gfx.clip(x, y, w, h)
    love.graphics.setScissor(x, y, w, h)
end

function gfx.clip()
    love.graphics.setScissor()
end

function gfx:tile(x,y,w,h,tile)

    local rows = math.floor(self.imageW / self.quadW) -- rows the tileset has

    for ly=0, h do
    for lx=0, w do

        local di = (tile + lx + (ly * rows)) -- draw index
        local dx = (di % rows) * self.quadW  -- draw x
        local dy = (di / rows) * self.quadH  -- draw y
        self.quad:setViewport(dx,dy,8,8)
        love.graphics.draw(self.image, self.quad, x+(lx*8), y+(ly*8))  
    end
    end
end

function gfx:line(x1,y1,x2,y2,color)
    love.graphics.setColor(palette[color])
    love.graphics.line(x1,y1,x2,y2)
    love.graphics.setColor()
end

function gfx:rect(x1,y1,x2,y2,color, fill)
    love.graphics.setColor(palette[color])
    love.graphics.rectangle(fill == true and "fill" or "line", x1, y1, x2, y2)
    love.graphics.setColor()
end

function gfx:circ(x,y,xr,yr,color, fill)
    love.graphics.setColor(palette[color])
    love.graphics.ellipse(fill == true and "fill" or "line", x, y, xr, yr)
    love.graphics.setColor()
end


return gfx

Code: Select all

local gfx = require("lib.gfx")

local canvas      = love.graphics.newCanvas(128, 128);
local canvasScale = 4

canvas:setFilter("nearest", "nearest")
gfx:new("gfx/bank0.png")

function love.draw()
    love.graphics.setCanvas(canvas)

    
    gfx:tile(0,0,2,2,0)


    love.graphics.setCanvas()
    love.graphics.draw(canvas, 0, 0, 0, 4, 4) 
end
Attachments
weird.png
weird.png (1.58 KiB) Viewed 2005 times
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: drawing quad seems to be rotated

Post by zorg »

At a first glance, i didn't see the issue you have, but i did notice something else:

Code: Select all

function gfx.clip(x, y, w, h)
    love.graphics.setScissor(x, y, w, h)
end

function gfx.clip()
    love.graphics.setScissor()
end
That doesn't do what you think it does; lua won't discriminate based on the provided parameter lists for a function with the same name, it will overwrite the function with the one defined last.

A simple fix is to check if all 4 parameters are nil, and if so, unset the scissor, else set it.

The same applies to gfx:pal, since that's also duplicated.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: drawing quad seems to be rotated

Post by Shadowblitz16 »

ok thankyou i didn't realize lua didn't have function overloading

although i am still having issues with my tiling being draw with a weird rotation/offset

EDIT: i fixed it.
I changed

Code: Select all

        local dx = (di % rows) * self.quadW  -- draw x
        local dy = (di / rows) * self.quadH  -- draw y
to

Code: Select all

        local dx = math.floor(di % rows) * self.quadW  -- draw x
        local dy = math.floor(di / rows) * self.quadH  -- draw y
Post Reply

Who is online

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