function drawCard(target, card)
	if card.mustbedrawn then
		local x=card.position.x
		local y=card.position.y
		local value=card.value
		local color=card.color
		local v_or_h=nil
		
		if card.selected then
			y=y-CARDSPREF["selectedShift"]
		end
		
		if card.orientation=="verticalmain" or card.orientation=="verticalsub" then
			v_or_h=cardImages.vertical
		else
			v_or_h=cardImages.horizontal
		end
		
		
		if card.visible==false then --face down
			if card.orientation=="verticalmain" then --in the hand of the player
				target:blit(x, y, cardImages.vertical["big_back"], true)
			else 
				target:blit(x, y, v_or_h["back"], true)
			end
		else --face up
			local images=nil
			if card.greyed then
				images=v_or_h.greyed
			else
				images=v_or_h
			end
			
			if color~="trump" then
				target:blit(x, y, images.underlay[color], true)
			end
			target:blit(x, y, images[value], true)
			if color~="trump" then
				target:blit(x, y, images.overlay[color], true)
			end
			images=nil
		end
		
		x=nil
		y=nil
		value=nil
		color=nil
		v_or_h=nil
	end
	return target
end

--TODO: uniformize with every other drawing functions, do smth better
function drawSplashScreen(timeinmvblanks)
	--local splashImage=Image.load("images/splash.png")
	screen:blit(0, 0, splashImage, false)
	screen.waitVblankStart()
	screen.flip()
	--a second time so all 2 images (screen and his buffer) are splashed
	screen:blit(0, 0, splashImage, false)
	--splashImage=nil
	screen.flip()
	if timeinvblanks==nil then
		timeinvblanks=GAMEPREF["splashScreenTime"]
	end
	screen.waitVblankStart(timeinvblanks)
	return splashImage
end

--TODO: this will be too much, optimize!
function drawPlayers(target, lazy)
	if lazy==nil then
		lazy=true
	end
	local playerPropertyPrefix=nil
	local i=0
	for i=1,4 do	
		playerPropertyPrefix="player"..capitalize(players[i].position)
		
		if not lazy then
			--avatar
			x=GAMEPREF[playerPropertyPrefix.."AvatarX"]
			y=GAMEPREF[playerPropertyPrefix.."AvatarY"]
			target:blit(x, y, players[i].avatar,  true)
			
			--name
			x=GAMEPREF[playerPropertyPrefix.."NameX"]
			y=GAMEPREF[playerPropertyPrefix.."NameY"]
			--printBicolor(target, x, y, players[i].name, players[i]["colorOut"], players[i]["colorIn"])
			local colorOut=nil
			if CARPETPREF["namesBicolor"] then
				colorOut=CARPETPREF["namesColorOut"]
			end
			printU(target, x, y, players[i].name, players[i]["colorIn"], true, CARPETPREF["namesFontName"], CARPETPREF["namesFontW"], CARPETPREF["namesFontH"], CARPETPREF["namesFontSpacing"], colorOut)
			--target:blit(x-25, y-25, players[i].nameImage, false)
		
		
		else--lazy:
		
			--player number icon
			x=GAMEPREF[playerPropertyPrefix.."NumIconX"]
			y=GAMEPREF[playerPropertyPrefix.."NumIconY"]
			target:blit(x, y, iconImages["playersNum"]["p"..i],  true)
		
			--state icon
			x=GAMEPREF[playerPropertyPrefix.."StateIconX"]
			y=GAMEPREF[playerPropertyPrefix.."StateIconY"]
			target:blit(x, y, iconImages["states"][players[i].state],  true)
			
			--rank
			x=GAMEPREF[playerPropertyPrefix.."RankIconX"]
			y=GAMEPREF[playerPropertyPrefix.."RankIconY"]
			local pending=true
			if players[i].state=="finished" or players[i].state=="not_playing" then
				pending=false
			end
			--printRank(target, x, y, players[i].rank, justBecame)
			drawRank(target, x, y, players[i].rank, pending)
	
			--cards
			local j=0
			for j=1,tableLength(players[i].hand) do
				drawCard(target, players[i].hand[j])
			end
		end
	end
	
	return target
end

function drawIngameCursor(target)
	if ingameCursor["available"] then
		target:blit(ingameCursor.position.x, ingameCursor.position.y, ingameCursor.image, true)
	end
	return target
end

function drawCarpet(target)
	target:blit(0,0,background,false)
	fdebug("drawn background")
	return target
end


function printRank(target, x, y, rank, justBecame)
	local prefix=""
	if justBecame then
		prefix=">"
	end
	printBicolor(target, x, y, prefix..status, colors[status.." Out"], colors[status.." In"])
end

function drawRank(target, x, y, rank, pending)
	if pending then --rank is not sure because he's playing and hasn't finished
		target:blit(x, y, iconImages["ranks"]["greyed"][rank], true)
	else
		--fdebug("drawrank: iconImages[\"ranks\"][\""..rank.."\"]")
		target:blit(x, y, iconImages["ranks"][rank], true)
	end
end

function drawCardsOnTheTable(target)
	local i=0
	for i=1,tableLength(tableDeck) do
		drawCard(target, tableDeck[i])
	end
end

function drawSpeechBubble(target, position)
	--local cap_pos=capitalize(position)
	local bub_x=BUBBLESPREF[position.."X"]
	local bub_y=BUBBLESPREF[position.."Y"]
	local t_x=BUBBLESPREF[position.."X"]+BUBBLESPREF[position.."TextShiftX"]
	local t_y=BUBBLESPREF[position.."Y"]+BUBBLESPREF[position.."TextShiftY"]
	local col=BUBBLESPREF["fontColor"]
	local font=BUBBLESPREF["fontName"]
	local w=BUBBLESPREF["fontW"]
	local h=BUBBLESPREF["fontH"]
	local space=BUBBLESPREF["lineSpacing"]
	
	local bubble=speechBubbles[position]
	
	local num=string.find(bubble.text, "\n")
	if num==nil then --one line text, move the text
		t_y=t_y+math.floor(BUBBLESPREF["lineSpacing"]/2)
	end
	
	if speechBubbleIsVisible(position) then
		target:blit(bub_x, bub_y, bubbleImages[position], true)
		--printWithCR(target, t_x, t_y, bubble.text, black, 10)
		printU(target, t_x, t_y, bubble.text, col, true, font, w, h, space)
	end
end

function drawSpeechBubbles(target)
	drawSpeechBubble(target, "bottom")
	drawSpeechBubble(target, "top")
	drawSpeechBubble(target, "left")
	drawSpeechBubble(target, "right")
end
