Sign in to follow this  
  • entries
    455
  • comments
    639
  • views
    421669

Bonsai We have text!

Sign in to follow this  
_the_phantom_

71 views

I did this last night, didn't take that long once I took care of some stupid mistakes in the C++ code [grin]


std::string finalname = filenamebase + numberfix + "." + texturetype;
vs
std::string finalname = filenamebase + numberfix + texturetype;


*whistles*

Anyways;


The code;

require "engine"

function runDemo()

eng = bonsai.createGraphics()
print ("Created : " .. eng:__tostring())
eng:createWindow()
print "Window Created"
tex = eng:createTexture("test.jpg")

baseimg = {dirx = 0.1, diry = 0.1}
function baseimg:update()
self.x = self.x + self.dirx
self.y = self.y + self.diry

if self.x + self.width > 800
or self.x < 0
then self.dirx = -self.dirx end

if self.y + self.height > 600
or self.y < 0
then self.diry = -self.diry end
end
function baseimg:new(o)
o = o or {}
setmetatable(o,self)
self.__index = self
return o
end

img = baseimg:new({x = 0, y = 0, width = 200, height = 200, id = tex})
img2 = baseimg:new({x = 0, y = 100, width = 40, height = 10, id = tex})

font = eng:loadFont("scrolltext","png")
print "Font loaded..."

imgs = {img, img2}
while(eng:startFrame())
do
eng:renderQuads(imgs)
eng:renderText(0,100,"Testing",font,{r=0,g=1,b=1})

img:update()
img2:update()

eng:endFrame()
end
eng:deleteTexture(tex)
eng:destroyWindow()

end

runDemo()



The main changes being the code to load and render the text.

The loader loads the .bmf produced by Bitmap Font Generator and the textures which go with it. In theory it should be able to deal with fonts spread over multiple pages.

The text renderer takes the location to render, the string and the font and optionally a colour (currently a float between 0 and 1, I might change this to a 0-255 range instead) and renders the text using the supplied font. Again, this deals with multiple pages of fonts, in theory at least, however it's not what you'd call efficant. The main issue being the use of immediate mode and the constant changes of the texture between drawning each character. It might be worth, in the future, building an optimised VBO/VA of the data, maybe allowing the app to cache this data and just do a quick render. But, working under the principle of YAGNI I shant be doing that right now.

I'm infact pretty close to being able to do the graphical part of this game, I do need to sort a timer function out and then it's a matter of working out how to hook events together to get key presses.

I also spent some time thinking about sandboxing the scripts and doing things like making sure PhysFS is used for all file access; this involved a scheme of changing the enviroment for require and all manner of C Voodoo; then it dawned on me, there is no reason for this, in the launcher app I just restrict what I load at startup which will restrict the scripts to just what I allow. Oh well, only wasted about a day on that and it wasn't a real waste as I know how to make my own loader now so that all scripts will be loaded from PhysFS restricted locations.

Right, now I need to eat...
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now