Jump to content

Put just some text on screen!


aldoz
 Share

Recommended Posts

Hi again guys, I am trying to put some text on my screen but a basic thing such this seems impossible for me... I can't get text and the only thing I get is NO texts on screen.

Tried a bunch of forum examples but nothing!

 

 

So, I put a sphere on the "terrain demo" and I made a script to let to push it pressing a key.

This work fine!

 

But in the same script I add a function to draw some text using App:loop but I am not sure.... I getting nill value error too..

 

here my code :

 

function Script:UpdatePhysics()

  if window:KeyDown(Key.F) then  
    self.entity:AddForce(50,0,0,true)
  end

end




function App:Loop()
    if (self.window:Closed() or self.window:KeyDown(Key.Escape)) then return false end

    self.context:SetColor(0,0,0)
    self.context:Clear()

    --Draw some centered text on the screen
    local text = "Leadwerks"

    local font = self.context:GetFont()
    local x = self.window:GetWidth()/2
    local y = self.window:GetHeight()/2
    x = x - font:GetTextWidth(text)/2
    y = y - font:GetHeight()/2
    self.context:SetBlendMode(Blend.Alpha)
    self.context:SetColor(1,1,1)
    self.context:DrawText(text,x,y)
    self.context:SetBlendMode(Blend.Solid)
    self.context:Sync()
    return true
end

Link to comment
Share on other sites

I can't run your example because it isn't a complete script, but try this:

self.context:SetColor(1,1,1,1)

 

It might be that your alpha color is set to zero.

 

Also, if you want to center the text use the context width and height, not the window. The window dimensions include the frame and titlebar, which you don't want.

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

mmm tried but nothing change.

 

I notice that, when I get out from demo, using esc key, I get "Script Error attempt to index field 'window' (a nil value)

 

If I delete the first script line so I get this error in the following code line : Script Error attempt to index field 'context' (a nil value)

 

Damn me when I lost time using Dark Basic :)

Link to comment
Share on other sites

Ok I get this working :

 

function App:Start()

    self.window = Window:Create()
    self.context = Context:Create(self.window)
    local font = Font:Load("Fonts/Arial.ttf",36)
    self.context:SetFont(font)
    font:Release()
    return true
end
function App:Loop()
    if (self.window:Closed() or self.window:KeyDown(Key.Escape)) then return false end

    self.context:SetColor(0,0,0)
    self.context:Clear()

    --Draw some centered text on the screen
    local text = "Leadwerks"

    local font = self.context:GetFont()
    local x = self.window:GetWidth()/2
    local y = self.window:GetHeight()/2
    x = x - font:GetTextWidth(text)/2
    y = y - font:GetHeight()/2
    self.context:SetBlendMode(Blend.Alpha)
    self.context:SetColor(1,1,1)
    self.context:DrawText(text,x,y)
    self.context:SetBlendMode(Blend.Solid)
    self.context:Sync()
    return true
end

 

Using this code inside the ball script, when I push esc key so I read the write!

  • Upvote 1
Link to comment
Share on other sites

Ok with the following code I can put a text on the center of the screen!

So, solved smile.png

 

function Script:Start()
self.font = Font:Load("Fonts/Ranchers-Regular.ttf",32)
if self.font==nil then
local context = Context:GetCurrent()
self.font = context:GetFont()
end
end

function Script:PostRender(context)
context:SetBlendMode(Blend.Alpha)
context:SetColor(1,0,0,1)
local text
local prevfont = context:GetFont()
prevfont:AddRef()
context:SetFont(self.font)
local fh=self.font:GetHeight()
local timestring

text="Level Complete"
context:DrawText(text,(context:GetWidth()-self.font:GetTextWidth(text))/2,(context:GetHeight()-fh)/2-fh*1.5)
end

  • Upvote 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...