Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - + [Matrix1Utils] Writing Text to Images - Memblocks

Author
Message
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 15th Apr 2011 05:02 Edited at: 15th Apr 2011 05:03
Hello everyone! I've been working on this for a little while now(a week maybe?) and wanted to share this with the DBPro community that has helped me so much.

Basically, this is a set of 3 source files that contain approximately 37 functions to write text and numbers directly to an image through memblocks. The main function to call is "mem_writeString()" which needs the string, the image to write to, the position in the image (x and y coordinates), and the color you want the text to be.

Speed compared to other methods
DBP text to bitmap, get image(): 8306ms for 1000 iterations
D3DFunc text to bitmap,get image(): 8352ms for 1000 iterations
Memblock text: 747ms for 1000 iterations
*all speed results are an average of 3 different tests

Test code:


The source files
Mem_WriteAlpha.dba


Mem_WriteNum.dba


Mem_WriteString(includes two utility functions I use)


Enjoy! If you want to add your own features/use in a commercial project/use in a free project go ahead! However, if you turn this into a plugin I'd like to be notified at my listed email address.


Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 15th Apr 2011 23:31
Quote: "Speed compared to other methods
DBP text to bitmap, get image(): 8306ms for 1000 iterations
D3DFunc text to bitmap,get image(): 8352ms for 1000 iterations
Memblock text: 747ms for 1000 iterations
*all speed results are an average of 3 different tests"

That's verrrry far from an accurate benchmark. You also seem to have overlooked the fact that the get image function is super slow.

Also... WHY WOULD YOU CODE LIKE THAT?! D: .ttf files define fonts not .dbp files!


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 15th Apr 2011 23:57 Edited at: 16th Apr 2011 01:05
Quote: "That's verrrry far from an accurate benchmark. You also seem to have overlooked the fact that the get image function is super slow.
"

Do you have another way to print text to a separate image(not to the screen)?
The whole reason I made these series of functions is precisely because the get image function is super slow, and I wasn't aware of another way to get the same functionality so I made one.

Quote: "Also... WHY WOULD YOU CODE LIKE THAT?! D: .ttf files define fonts not .dbp files!"

I hard coded it for the speed increase and greater control.

If you have a better, faster way to do this I'd be happy to use it instead of this.


Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 18th Apr 2011 12:41 Edited at: 18th Apr 2011 12:44
How about this: Each time you change the font size, change the font color, or change the font, you render each character again. That would look something like:


There are two functions... asc and chr... one gets the ASCII character given a number, and the other gets a number given a character... I may have mixed them up here.

So anyways, then whenever you want to draw a character...


note that this method includes every character in the ASCII set. like the following: ~!@#$%^&*()_+-=/\[]{};:<>,. All of the characters in the ASCII set with any font, color, and size, can be rendered with significantly less code.


If pre-rendered text doesn't suit you, you could use the matrixutils "draw to image" command. If that still doesn't suit you... well... you can get a pointer to an image various ways. Or, you could try to figure out how to get a pointer to a bitmap (which would probably be faster).

My point is that repetitive code, and specifying data that's already specified elsewhere, is generally bad. Avoid it like the plague, and if you absolutely have to write repetitive code like this, write a script to do it.
pseudocode of a script to write the code you wrote:


[edit]
(where "echo" just writes the output to a text file. For easy use, set the outputs to command line and just copy the data from there)


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
Stormwire
13
Years of Service
User Offline
Joined: 3rd Sep 2010
Location:
Posted: 6th May 2011 08:00
What plugins do I need to get this to work? Two commands are not working D3D_Init and D3D_Font are the culprits. Thanks for the share!
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 7th May 2011 15:44
For the test program, you need the D3DFunc plugin by Cloggy. For the actual code, you only need the Matrix1Utils by IanM.


Login to post a reply

Server time is: 2024-04-12 23:14:06
Your offset time is: 2024-04-12 23:14:06