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.

Dark GDK / Text extension, Image extension, Primitives Extension

Author
Message
Sephnroth
22
Years of Service
User Offline
Joined: 10th Oct 2002
Location: United Kingdom
Posted: 28th Jul 2005 20:33
Here are a few extensions I have written for DarkSDK to do some additional tasks. This is mainly simple stuff and tatters of them have been seen around the boards before but I figured it would be nice for me to put what I have in its entirity here to share Some still need a little more work perhaps, I will put them all in my DarkSDK Collection post in the code snippets when I am content with them.

For now simply open up the code tags and place the code into appropriate files (header and cpp provided)

First the text extension - I have to thank Cloggy for this, its the text portion of his d3dextras dll he wrote for dbp. He shared the source with me on msn and I set about porting it to DarkSDK.

Text.h:


Text.cpp:



You only need to InitText() once at the start of your program. Setup some fonts like you would create objects (you may want to make some constants for your font id's) and then simply call AAText() passing a font id to draw your text. You must call AAText() between StartText() and EndText() functions. Try and batch all your text in one go using only one Start/End block and you will get the best speed. You can use multiple blocks but the less you use the faster it will go - this is much faster than normal dbText(). You may use SetTextCol() to set the colour of the text at any time and you can set its alpha for transparent text! The header includes some constants for align codes which I recommend you use instead of passing just numbers to the AAText() function - makes it easier to read. Oh, and dont call any sdk commands which perform a drawing operation (dbBox() for example) inbetween StartText() and EndText() - it will only end in tears.

Primitives Extension - not many people may find a use for this one but I have. Adds a few extra objects which you can create. A ramp (you might want to call it a wedge), a paralellogram (like a ramp but has equal sized ends rather than one being a point) and also a 3d line which I cant take credit for because its from the Code Snippets forum, I simply ported it to darksdk.

ExtendedPrimitives.h:



ExtendedPrimitives.cpp:



Finally, ExtendedImage. This will allow you to perform drawing operations into darkbasic Image types - usually you would have to call your drawing commands like Box etc onto a bitmap and then use GetImage() to make a texture out of it, you can draw streight to the image with these. The image must, naturally, exist.

You can draw pixels, lines and circles. Its pretty fast (fast enough for the dynamic texture generation in Iro) as it uses breshams-based integer maths functions instead of floating point.

ExtendedImage.h:



ExtendedImage.cpp:



Despite my attempt at checking the format (as you can see) it still seems to have problems when the screen depth is set to 16 - not entirely sure why :/

Anyway! Hope they are usful to someone. I will add/improve as time goes by - if you make your own additions and improvments then let me know! Maybe we will one day end up with a big set of community made extensions

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 29th Jul 2005 02:05 Edited at: 3rd Dec 2005 13:25
Heres a little key-stroke handler I've been using recently

KeyEvent.hpp


KeyEvent.cpp


Simple usage:


Registered handlers are called on a newest-first type basis.

I use the boost smart pointer rather than the raw pointers I've shown here, but the effect is the same. When the usage count of the smart pointer is reduced to 1, I free the handler object.

I've also considered putting together a per-key type event handler together ... maybe sometime soon.

[edit]fix to code

For free Plug-ins and source code http://www.matrix1.demon.co.uk
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 9th Oct 2005 23:16
Sephnroth,
Excellent work, congratulations
It is a very useful code, I'll credit Cloggy and you in my program.

Ian,
I suppose that your code is for replacing dbInkey/dbScanCode. I get this error at the "Simple usage" code when compiling:

In this line:


It says:
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Oct 2005 19:42
Oops, typo! That line should be:



Yes, it is a simple replacement for dbKeyState, dbScanCode or dbInkey - all of those check the current state of the key, which can change between syncs. This code uses windows functionality to get the current state of the keyboard in a snapshot, and then calls the registered objects with the changes.

As an example, you might derive your own class from the KeyEvent class that allows you to control your player. As the code also passes the keypresses onto the most recently registered handler first, you could have a console that registers itself when opened and gets the first bite at all keypresses that take place.

For free Plug-ins and source code http://www.matrix1.demon.co.uk
miko
19
Years of Service
User Offline
Joined: 2nd Oct 2005
Location: Germany
Posted: 11th Oct 2005 21:22
Now this is what I call a very interesting thread.
Hopefully, I will be able to contribute one day, too.
Thank you all. Valuable code!!
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 12th Oct 2005 16:10
Hi,

Can you provide a simple usage for the Text extension please?

Cheers
Sephnroth
22
Years of Service
User Offline
Joined: 10th Oct 2002
Location: United Kingdom
Posted: 12th Oct 2005 18:40
Certainly

In your project make a new source file and a new header file, copy the respective code from the code blocks into them. Recommend you name them Text.cpp and Text.h.

OR

If you already have those files on your hd with the code copied in then add them to your project instead.

When your project contains Text.h and Text.cpp with the right code inside, goto your main.cpp and add at the top:

#include "Text.h"

Then just before your main game loop add:

InitText();

so in a typical empty DarkSDK application it would all look like:



Now you need to setup some fonts to use. The function you need and its parameters are:

void SetFont(int nFont, LPSTR pFont, int nFontSize, int nFontBold, int nFontItalic);

nFont is an integer number and its the font id, just like a darkbasic object ID. pFont is a string saying the name of the font you would like to use, like Arial. The font must of course be installed on the system running the program. nFontSize is the size of the text that will be printed using this font, standard font point size. nFontBold is wether the text should be bold or not, set it as 0 for normal or 1 for bold. nFontItalic works the same way but for italics.

To setup a font with an id of 1 that uses arial, a point size of 12 and is not bold or italic we add this:

SetFont(1, "Arial", 12, 0, 0);

Anywhere after InitText() and before the main game loop is a good place for it. If you need to setup alot of fonts for your game you could make a function called SetupFonts(); and just add all the SetFont lines to it and have it called after the InitText line.

Anyway, to eventually print some text to the screen you use AAText(), but you must call ALL AAText() lines between a StartText() and EndText() block. For Example, add this to your main loop:

StartText();
AAText(1, 10, 10, ALIGN_LEFT, "Hello World!");
EndText();

That would output the text "Hello World!" to position x: 10 y: 10 on the screen using font id 1 and the text will be left aligned. I made some simple constants for text alignment, valid ones are:

ALIGN_LEFT
ALIGN_RIGHT
ALIGN_CENTER

If you want to print more than one line its VERY advisiable to try and do it in the same start/end block as the rest of your text, for example you should do this:



and NOT this:



they both work but the last example is much slower. it wont be too bad with 2 start/end blocks like that, upto 4/5 maybe - but the more you use the slower you can get - the most effcient program will only use one (but i know thats sometimes impossiable).

This is the main reason this text extension is so much faster than darkbasics, it appears that every time you call dbText() its doing the equivilent of opening another start/end block.

HOWEVER, the thing that makes it hard to use only one start/end block is that you MUST NOT use ANY darksdk commands that draw to the screen whilst you are in the middle of a start/end block. example:



BIG NO NO. Try it and see why
(actually, i just tested that and it worked fine >.>;; but i know from experiance in iro its a bad idea when you have a full game running, the screen gets corrupted and it all gets messy, avoid it)

When doing things like making dialog boxes it does mean you have to think very carfully where to put the text, sometimes i found i had to use 2 blocks - but do your best

Anyway, thats pretty much it. you can use SetTextCol() to set the colour and transparancy value of text at any point (yes its fine in and out of start/end blocks). When you AAText() it uses the last set value with SetTextCol().

Any more questions just ask, sorry if this came out a bit long xD

full source:



miko
19
Years of Service
User Offline
Joined: 2nd Oct 2005
Location: Germany
Posted: 13th Oct 2005 20:29
Wel, here are my 2cents

Based on the text extension above, I made a class (CText). Fiddled a bit with the code, too (hoping that it would run a few ticks faster).

For a usage example, please see the demo app code below.

Okay, here we go:

This is the header


The code itself


A basic text app would look like this


Ah, in case you wonder: This is the GlobalHeader.h


Curious what you think about that.
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 14th Oct 2005 17:14
Thanks for the usage + info

Cheers,
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 14th Oct 2005 18:06
Actually, its more than thanks for the usage + info, this it bloomin' useful! Especially the alpha, good work
miko
19
Years of Service
User Offline
Joined: 2nd Oct 2005
Location: Germany
Posted: 1st Nov 2005 13:30
On a short note: When using fullscreen ("dbSetWindowOff") and directly accessing DX routines (e.g. for text output as shown above), I got resource errors on alt-tab out/in the game. Obviously, this is caused by a lost device and resources that would need a call to OnDeviceLost/OnDeviceReset. I could not figure out how (read: when) to do this, tho.
A workaround would be to *not* use dbSetWindowOff, but create a window with no borders and size of the current desktop. Like here:



This bounds the "fullscreen" game screen to the current desktop screen resolution, tho.
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 2nd Dec 2005 11:50
Ian,

I'm using your key-stroke handler, I works fine with single-key press but I have problems checking two or more keys simultaneously.

I use it like this for crtl+home:



Can you help me with this? Maybe this isn't the best way to use it
Also, is there a way to speed up a lot of checkings (like a switch statement for all keypress)?

Thanks in advance.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Dec 2005 13:40
What problems are you having? That code looks perfectly reasonable to me.

If you find that you are using many if's or select's then you are using the code in the wrong way. You should create a new class derived from the KeyEvent class and register it - that class will be called for each and every keystate change that is made.

For free Plug-ins and source code http://www.matrix1.demon.co.uk
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 3rd Dec 2005 17:37
I guess I need a little "initial" help.
This is my class, but how should I add new methods for any other checks (like crtl+home) or avoiding too many if's? I cannot even imagine right now




The code that I posted before:

is in the middle of main code, not inside any class.
And does not seem to work because once a key is down it does not send any more messages until it is up. So both keys should be pressed exactly at the same time.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Dec 2005 21:53
When you use the KeyIsDown function you are not tapping into the event system at all. You are checking the flags directly to see what the key status is. So I really don't know why your code isn't working, unless it is being affected by the surrounding code.

This is untested, but here is an event class that will detect Ctrl-Home for you:


Here is a simple class that I use for simple directional control:


Which I use like so:


I am thinking about a revamp for this code - my DBPro code for keyevents is far more flexible and advanced ATM. If you have any suggestions, let me know.

For free Plug-ins and source code http://www.matrix1.demon.co.uk
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 4th Dec 2005 11:14
All right, thanks a lot Ian.

As a suggestion, I always dreamt about using it like this (pseudocode):



So once the user press "right" you don't have to check "left" and many others.

Also in your simple class used for simple directional control, I assume that the class KeyDownEvent derives from KeyEvent

or is it right when I read:
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Dec 2005 13:17
You are right - the code I'm using has evolved a little from what is posted above, and I simply copied&pasted.

It's not possible to get exactly like your pseudocode, but I'll see what I can do.

For free Plug-ins and source code http://www.matrix1.demon.co.uk
Smithy
19
Years of Service
User Offline
Joined: 8th Dec 2004
Location: Switzerland
Posted: 12th Dec 2005 18:22 Edited at: 12th Dec 2005 18:31
//EDIT nevermind
Happy christmas DarkCoders!

//Awards: Best DM at NeverwinterConventionIII (NWCon3)
//Sys: Pentium IV 3200E/Prescott;800Mhz FSB;HT;WinXPPro;ATIR9700PRO;1024MB RAM(2x512MB"DualChanneled";VC++7.net;Delphi6;ADSL512;
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 18th Dec 2005 19:20
Hi,

Is there a prefered way to convert int/float/double/longlong etc. to work with AAText? Currently I've got my own function when non char/string converting that type (basically a number, e.g. convert double using _ecvt) to LPSTR and passing that in, might not be the best way, whaddya reckon?

Cheers
Sephnroth
22
Years of Service
User Offline
Joined: 10th Oct 2002
Location: United Kingdom
Posted: 18th Dec 2005 19:25
My way is probably not the most effcient or best by any means, but I tend to have one string global that I use for most of my output, say g_szData[512]; or g_szOutput[512];. then i'll just sprintf() any line im about to print into that string and then aatext it out - perfect variable conversion and the added bonus of formatting options

Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 19th Dec 2005 11:55
Ah yeah, that's one idea. Cheers
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 1st Jan 2006 16:03
Hi,

Just something quick - it looks like when you do SetFont, that the second font inherits the first font's bold, italic (and colour) setting?

E.g.

SetFont(1,"Verdana",10,1,0);
SetFont(2,"Courier New",10,0,1);

..then using those font numbers, both come out with italic on, I'd expect only font 2 to have italic - any ideas?

Cheers
Smithy
19
Years of Service
User Offline
Joined: 8th Dec 2004
Location: Switzerland
Posted: 6th Jan 2006 09:40 Edited at: 6th Jan 2006 09:59
@Lampton Worm

I don't know about the code of Sephnroth (Sorry, I had a quick look at the code but didnt found the problem), but it seems when you're using a class like the one provided by "miko" the problem doesnt occur.
(at least for me and my own class based upon the code of the DX SDK Tutorial Sample for Text which is the same code cloggy used for his dll as there are some variables still left that were used by the SDK sample code but not needed anymore in cloggys/Sep's code actually (like g_pFont2 and g_pStatsFont) )

//Awards: Best DM at NeverwinterConventionIII (NWCon3)
//Sys: Pentium IV 3200E/Prescott;800Mhz FSB;HT;WinXPPro;ATIR9700PRO;1024MB RAM(2x512MB"DualChanneled";VC++7.net;Delphi6;ADSL512;
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 7th Jan 2006 17:16
Ah yeah, thanks. It's because Miko's code uses text objects, which thus have true individual properties set up,

Cheers
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 7th Jan 2006 17:22
Also, while I'm here - I'm having trouble getting the text to display over sprites that are assigned and created dynamically during my main loop, any thoughts? Sprites end up on top. I experimented with sprite first/last and priority but no joy there..

Cheers
Smithy
19
Years of Service
User Offline
Joined: 8th Dec 2004
Location: Switzerland
Posted: 7th Jan 2006 23:10
Same problem here at the moment, though, I can live with it (aka there is other stuff I have to do before I can have a look at this problem)

Probably its because the text is not handled "inside" the SDK, instead its pure DX code.
( though, I hoped that CORE::dbDrawSpritesFirst/Last would work probably... but it seems this doesnt work either.)

//Awards: Best DM at NeverwinterConventionIII (NWCon3)
//Sys: Pentium IV 3200E/Prescott;800Mhz FSB;HT;WinXPPro;ATIR9700PRO;1024MB RAM(2x512MB"DualChanneled";VC++7.net;Delphi6;ADSL512;
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 8th Jan 2006 13:52
Hi,

I don't think DrawText can be given a priority, but someone may know otherwise.

Cheers
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 8th Jan 2006 15:58
Hi,

Quick solution to this... call dbDrawSpritesFirst() before your main loop, then don't use dbCLS() in your main loop - use dbSprite() to splat a background down instead (could just be a blank image).

Appears to work a treat for me, I've now got dynamically created sprites with alpha text over the top using Sephnroth's class, cool.

Cheers
Cloggy
20
Years of Service
User Offline
Joined: 31st Oct 2004
Location: Rayleigh, Essex
Posted: 9th Jan 2006 23:31
I've spent quite a bit of time looking at this, and it seems that you need to turn the backdrop off when using DrawSpritesFirst or the sprites don't even appear!

With DrawSpritesFirst and backdrop off Alpha text prints over the top without a problem.

As Smithy says it's to do with being pure DX code rather than DBPro.

I can't decide the order things are drawn, and this about as good as it gets.

BTW if anyone uses my 3DLine and 3DDot functions, I'll be releasing a new version soon that allows the use of multiple cameras. Although this is for DBP I'll post the source if anyone wants to use it.

Cheers,

Cloggy
Smithy
19
Years of Service
User Offline
Joined: 8th Dec 2004
Location: Switzerland
Posted: 10th Jan 2006 10:26
Lampton Worm:
Quote: "
Sprites end up on top. I experimented with sprite first/last and priority but no joy there..
"

Quote: "
Quick solution to this... call dbDrawSpritesFirst() before your main loop, then don't use dbCLS() in your main loop...
"


Ahh very good, so dbDrawSpriteFirst() will do the job.

Thanks for pointing it out LamptonWorm!

ps: Cloggy, I am sure I will need such code later so it would be appreciated if you could post the snippet after you converted it.


The DARK Force Shall Lead You...
*evil chuckle* Mwuahahaha...

//Awards: Best DM at NeverwinterConventionIII (NWCon3)
//Sys: Pentium IV 3200E/Prescott;800Mhz FSB;HT;WinXPPro;ATIR9700PRO;1024MB RAM(2x512MB"DualChanneled";VC++7.net;Delphi6;ADSL512;
Cloggy
20
Years of Service
User Offline
Joined: 31st Oct 2004
Location: Rayleigh, Essex
Posted: 11th Jan 2006 20:26
I have just posted the latest version of my d3dfunc dll here http://forum.thegamecreators.com/?m=forum_view&t=69221&b=5&p=0 if anyone wants to convert any of it to work with DarkSDK.

Cheers,

Cloggy
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 11th Jan 2006 20:51 Edited at: 11th Jan 2006 20:52
Great! Thanks a lot Cloggy.

By the way, I'm trying to make the text output by DrawAAText zenabled, so it doesn't appear like a "hud", and get occluded by other objects.

I think it can be done like this at the StartText function:



And then at the DrawAAText function, set any transforms to the device, as if it was transforming a regular 3D object.

However, I'm having some trouble making it work. If anybody can lend a hand, it would be great.
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 1st Mar 2006 13:41 Edited at: 9th Mar 2006 17:56
IanM, seems there is a problem in the key-stroke handler with the "F12" function key, number 123 in my keyboard. Pressing it raises this error:

"Unhandled exception at 0x7c911230 in Project.exe: User breakpoint."

Any idea about what's causing this?

Thanks in advance.

[EDIT]
Seems to be a SDK problem, since it happens to all the programs, so not related with key-stroke handler. Forget it.
The Back Alley Media
18
Years of Service
User Offline
Joined: 22nd Nov 2005
Location:
Posted: 2nd Mar 2006 19:23
This is a very interesting thread on extending Dark SDK. I would like to also stick in my 2 cents. I have been interested for a while in creating a .flc loader for the Dark SDK. I am tired of handling discrete frames for my sprite animations. I am familiar with the flic file format.

If anyone could point me in the right direction, I would be much obliged. Loading in the flic data isn't the problem, it is mapping it to a Dark SDK image that I can animate. As I understand it the following has to occur:

Load/Parse the .flc
Render each frame and assign it a dark sdk image number
Play each frame as a dark sdk image

The question I have is how do I take each frame that I render and hook it into a dark sdk image? Is this the most optimized way of doing this or is there a better way?

Anyone done this before?

Tnx
Mr Squishy
18
Years of Service
User Offline
Joined: 25th Feb 2006
Location:
Posted: 3rd Mar 2006 01:39
miko i went ahead and added the variable length string thingy to your TextOut function, so you can:
int input=1;
g_Text1.TextOut(0,0,ALIGN_LEFT,"input = %d",input);

here is the code if anyone wants it

the header


the implementation


Some days the cascading moments which constitute my experience dissolve to reveal a sense of clarity which transcends reasoning.
And then there are days like today.

Login to post a reply

Server time is: 2024-11-19 04:15:28
Your offset time is: 2024-11-19 04:15:28