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 / Simple printing of a string.

Author
Message
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 2nd Aug 2010 23:19
This is one of those things that just ticks me off about some languages, and is likely why I haven't gone to C++ in all these years.

In DBPro:

text 10, 10, "FPS : " + str$(Screen FPS())

I've been trying to accomplish the same thing in C++ for an hour now, and keep running into the same issue.

Multiple google searches, and searches here really haven't resulted in anything that makes it simple to just print a few values on a line.

Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 2nd Aug 2010 23:42 Edited at: 2nd Aug 2010 23:43
You could either use sprintf or stringstream.

sprintf


stringstream


“C++ : Where friends have access to your private members.”
-Gavin Russell Baker
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 2nd Aug 2010 23:45
Thanks for that. Honestly I don't know why it has to be so stinkin' complicated.

I read in another thread that dbText has a memory leak issue, is there something else I need to do to avoid that when using your examples above?

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 2nd Aug 2010 23:59
its not dbText that does the memory leak, it's dbStr that most people use with dbText

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 3rd Aug 2010 00:03
Ah, ok. So no worries about using either of the examples.

Thanks. Still wish it were a little easier, but I guess it's not to bad.

_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 3rd Aug 2010 01:05 Edited at: 3rd Aug 2010 01:09
I use std::string for all my strings, and I think it's very easy to handle!

Just include the standart string header and namespace:



Then you can just do this:



or things like this:



Finally you can use strings in DGDK functions like so:



Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Dodga
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 3rd Aug 2010 01:36
C++ is easy once you get used to it, things are more difficult but you have WAY more control.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 3rd Aug 2010 04:39
It just seems silly to have to work so hard for something that should be so simple.

Thanks for the additional examples. It gives me some options, but also proves my point. There are to many ways to do the same thing in this language.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Aug 2010 06:57
Just a note regarding strings, you must free strings returned from GDK functions.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 3rd Aug 2010 19:57 Edited at: 3rd Aug 2010 19:58
So then in,

str = dbStr(dbScreenFPS());

str will have to be freed after you're done with it, but in,

dbText(10, 10, str);

it doesn't, because it's not being returned, it's just being passed?

[noob question]
How exactly does one free a string?
[/noob question]

Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 3rd Aug 2010 21:06
Quote: "str = dbStr(dbScreenFPS());

str will have to be freed after you're done with it"


Yes, you will have to free the string using the 'delete' operator.



The reason for this is that pointers literally point to an address in memory - nothing is copied here:
"str = dbStr(dbScreenFPS());"
So dbStr can't return a local (not dynamic) pointer, since all of its variables will be destructed when they go out of scope as dbStr returns. That's why dynamic memory has to be used, so the memory will remain after dbStr returns.

Pointers
Dynamic memory

“C++ : Where friends have access to your private members.”
-Gavin Russell Baker
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 3rd Aug 2010 21:30
I understand that C++ p's you off at the moment, but you have to remember it's not designed to be easy to get into. It's designed such that once you're used to it you can do anything you want with it. Right now you might think you'll just have to learn all this stuff by wrote, which is true, but once you start getting used to it and you find more and more uses for it, you'll appreciate it a lot more.

"everyone forgets a semi-colon sometimes." - Phaelax
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 3rd Aug 2010 21:34
That's what I'm hoping.

It's always frustrating at the beginning when learning a new language. I'll get over it.

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 3rd Aug 2010 21:37
Indeed, and it won't even take that long. Once you start using classes you'll see immediately what advantages they have over standard procedural programming. You'll learn that it becomes less about commands and more about the movement of information, and getting something to work right is the most satisfying feeling in the world!

"everyone forgets a semi-colon sometimes." - Phaelax
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 3rd Aug 2010 21:47 Edited at: 3rd Aug 2010 21:48
Right, for now I just need to get the structure of a program straight in my head. There are a lot of different containers to figure out and they have to be in certain places to be accessible to all the other parts of the program. It gets a little overwhelming until you've got it down.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 3rd Aug 2010 22:16
Quote: "It's designed such that once you're used to it you can do anything you want with it. Right now you might think you'll just have to learn all this stuff by wrote, which is true, but once you start getting used to it and you find more and more uses for it, you'll appreciate it a lot more."


this is absolutely right

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 3rd Aug 2010 22:32
Quote: "Right, for now I just need to get the structure of a program straight in my head. There are a lot of different containers to figure out and they have to be in certain places to be accessible to all the other parts of the program. It gets a little overwhelming until you've got it down."


The bare-bones containers you should get used to are std::strings and std::vectors. You already know how to use functions. Learn to use classes (which are just Types in DBP with the ability to have functions in them). Learn inheritance, which will lead you on to learn polymorphism (scary words? Yes. But they are amazingly simple and amazingly powerful). Learn to use pointers, which is extremely easy - it's just understanding why they exist in the first place that's difficult.

Once you have these 4 things down, you're sorted. Anything else you'll learn is just a utility.

"everyone forgets a semi-colon sometimes." - Phaelax
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 3rd Aug 2010 23:04 Edited at: 3rd Aug 2010 23:06
Thanks, I'll look at those next. I'm reading about Namespaces right now.

Beginning Visual C++ 2010 - by Ivor Horton (even though I'm using 2008..)

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 4th Aug 2010 13:35
This link is a good reference:
http://www.cppreference.com/wiki/start

[b]
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 4th Aug 2010 20:08
Thanks. I actually like that better than cplusplus.com.

Dar13
16
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 6th Aug 2010 14:14
I think that boost's lexical_cast would be an alternative to dbStr(). I use it to cast integers,floats,booleans to std::wstring and std::string all the time.

Using it would look like this:



Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 6th Aug 2010 17:11
Don't forget the necessary cast to char*, since dbText can't accept const char*:



“C++ : Where friends have access to your private members.”
-Gavin Russell Baker

Login to post a reply

Server time is: 2024-11-19 20:28:55
Your offset time is: 2024-11-19 20:28:55