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 / string to char*

Author
Message
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 07:26 Edited at: 19th Jun 2008 07:28
I am trying to make a function that creates a char* file name from an int and a char*. Example:



fileFormat is a char* that points to ".bmp". The function creates the string "1.bmp" if num is 1. I thought that
would do the trick, but it doesn't. It returns a const char*. How do I make str into a standard char*, not a constant?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Jun 2008 07:52
By casting it? i.e. (char*)whatever.c_str();

But it's better to copy the string into a new buffer for many reasons, using something like:



Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 08:09 Edited at: 19th Jun 2008 08:11
It compiled fine using the method you suggested, but it causes a runtime error. It says the application has requested the runtime to terminate it in an unusual way. What could it be?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Jun 2008 08:34
Look at the debugger and see? It should work fine, how are you using it?

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 08:46
I've never gotten the GDK to work with debugging.

But, as far as usage goes: I'm running a loop to check for the existence of files. The plan was to make loading the image files simple. It checks for "1.bmp"'s existence. If it exist, it then loads the image under ID 1, and moves on to "2.bmp". When it finds that one of the images doesn't exist, it stops looking. I have 1.bmp, 2.bmp, and 3.bmp in the directory. Here is the code to the loop:



I thought it was its usage in a loop. So, I replaced the above with this:



It still gave the error. I really don't know what the problem is. Should I just give up using this method and load them manually? I was hoping to make it dynamic in hopes of it being more usable by others helping with the project.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 08:57
I think I'll just read the file names from a text file. That would be less hassle and probably a better idea. Thanks for your time.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Jun 2008 09:04
I see, I thought your first snippet was just an example, i.e. not tested. On its own it won't work because you don't declare any buffer, only an address to a char. Change the line 'char* inToString;' to 'static char inToString[16];' and it will work, it's static because otherwise the buffer will get deallocated as soon as the function returns(thus returning an address to nothing of use), optionally you could pass the address of a buffer to the function or dynamically allocate a buffer in the function, but making it static is simple and doesn't require any extra garbage collection.

But as you're using sprintf you might as well just concatenate the whole string using that, with something like:



Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 09:13 Edited at: 19th Jun 2008 09:14
I actually figured it out with the help of Lilith's answer about this kind of thing from a few days ago. She suggested strcat, something I feel stupid for not using. But, it made it work correctly. I changed the code to this:



That method works. I feel so stupid for not thinking of strcat and a dynamic char*. Thank you for your time and for suggesting the dynamic allocation. I hate asking dumb questions, but I'm just very new to this.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 09:16
Also, I just read what you said about garbage collection. Should I change it to static, or, for practice, make an array of the addresses used and a loop to delete them all after loading the files?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Jun 2008 09:27
It's up to you, remember that in your last code example you only allocate a buffer of 10 chars, which includes the zero at the end of the string so you're limiting your file paths to 9 characters, this likely won't be sufficient for all paths.

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 09:28
Thanks for the tip about sprintf. I didn't note that usage. It worked great and allowed me to remove another include "#include <string>". Thanks again.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 09:31 Edited at: 19th Jun 2008 09:32
Quote: "this likely won't be sufficient for all paths"


The paths are still automatically generated in that for loop. By giving it 10 char's for a path name, that is allowing the number of images to reach 99,999 ( "99999.bmp" ). I don't expect to use more than 50 sprites or so.

But about the garbage collection: should I start an array of char*'s to keep track of the dynamic char arrays to clean after the for loop? I think it would be good practice. I guess I'm asking would that work?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Jun 2008 09:37
I'm not sure I follow, aren't you just loading as many images starting from 1,2,3 etc? If so then you don't need to store the paths anywhere, just the amount you load? As the filenames should be quite obvious then. In which case you just call your function, check if the image path exists, load it then 'delete [] path;' after this whether or not it loads.

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 09:40
Apparently, I don't think very well at 1:30 in the morning. Thank you. That is a much better idea. I just need to get more sleep. I didn't mean to take your time like this. Thank you, though.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 09:51 Edited at: 19th Jun 2008 09:53
Thanks to your help, it works beautifully. Kudos.

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Jun 2008 13:03
You could cut a lot of that out and remove the dynamic allocation, so it becomes:



And if your current version you need to use 'delete [] file_name' as you've allocated an array.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 19th Jun 2008 21:51
You can only use IDs 1 - 65355 with DBP, DBC, Dark GDK... anything that TGC has for programmers.

If you can do any models for FW, reply to the FleetWars thread.

Click here!
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 19th Jun 2008 21:53
@dark coder

Correct, once again. I do really stupid things that late.

@zuka

True. I forgot about that. So, yeah. 10 is more than enough.

Login to post a reply

Server time is: 2024-09-29 23:34:03
Your offset time is: 2024-09-29 23:34:03