I want to create a function which accepts's key presses and displays them in a rectangular box (field).
I tried a logic which failed because a char cannot be converted to a string.
here's the logic.
char str*;
str = 'a' + event.keyboard.keycode - 1;
Here's my updated version but still not working. showing signs of memory leak, Please Help!
Instead of using keyboard.keycode try using keyboard.unichar
see: http://liballeg.org/a5docs/trunk/events.html
look under the ALLEGRO_EVENT_KEY_CHAR section
then you wont need 26 case blocks...
I want help with converting char to string also or a way to draw the unichar in al_draw_text(); function, if you can help please reply.
converting char to string
I would typically do that:
#include <sstream> #include <string> char c = 'p'; std::string result; std::stringstream ss; ss << c; result = ss.str();
To convert a char to a string you could do this:
int chr = 65; // "A"
char str[256];
str[0] = chr;
str[1] = NULL;
or
int chr = 65; // "A"
char str[256];
sprintf(str, "%c", chr);
There are probably better ways to do this though, I'm looking forward to being corrected!
*Mark Oates*.
I am using C so I can't use C++ strings!
and
thanks for help *Michel Weiss* but I actually thought of it earlier, it didn't work.
I can't convert str[256] to a char* and that's the real problem.
How to print different chars with exact distance replacement as in a string.
If there's a way to create an input field like some library for allegro5 or code for that, please share.
I implemented my own editable text field like this.
Its not pretty, but it works for me.
I needed a way to let the user edit the server name or IP address for netgame.
Sorry its not a simple example with only the editable text field part.
I had to include my function that shows the cursor position and my proc_controller
function that processes the event queue...etc
Its quite a lot of stuff, but maybe there's something in there you can use...
I am using C so I can't use C++ strings!
Ah, okie dokie. 👍
You said: I can't convert str[256] to a char* and that's the real problem.
Why can't you convert str[256] to a char* ?
Are you trying to get a string to pass to 'al_draw_text()' ?
Can you post some code that shows what you are trying to convert to what?
Or more specifically what you tried that didn't work?
Are you trying to build a string from char's returned from events?
That should be very simple to do.
this will work, I've done it many times.
char str[256] is an array of char
str is a pointer to the first element (just like any array)
Maybe I'm missing something and you're trying to do something else, let us know.
I want to create a function which accepts's key presses and displays them in a rectangular box (field).
I tried a logic which failed because a char cannot be converted to a string.
here's the logic.
char str*;
str = 'a' + event.keyboard.keycode - 1;
Here's my updated version but still not working. showing signs of memory leak, Please Help!
...
char* str1;
You're missing some fundamentals of C programming. First of all, 'str1' in your code there is just a pointer. It is NOT a string. It's a pointer of type char. That means it points to a char value. You have to allocate memory for the string somewhere. Second of all you didn't initialize the value of 'str1'. Which means it could hold ANY value.
I can't convert str[256] to a char* and that's the real problem.
Uh, why not? Did your compiler output an error? It shouldn't.
If there's a way to create an input field like some library for allegro5 or code for that, please share.
Here's an example of how to use an ALLEGRO_EVENT_KEY_CHAR event to create an input field :
https://github.com/EdgarReynaldo/EagleGUI/blob/master/TINS2017/src/Game.cpp#L380-L443
Edgar Reynaldo:-
Thank you for your advice and I was told that char* is a new way to initialize a string so that's why it happened.
You said and I quote "Uh, why not? Did your compiler output an error? It shouldn't.",
Yes my compiler throws an error:- C++11 doesn't support conversion from char to a string.
Thanx for your link:- https://github.com/EdgarReynaldo/EagleGUI/blob/master/TINS2017/src/Game.cpp#L380-L443, I will make utilization of your code in future but for now, I want to know what I am doing wrong.
Michael Weiss:-
Thank you for your suggestion but I already tried it and it successfully compiled but it produces a runtime error.
I am posting my compleat code so please read it, it's in attachments.
It just stops working when I click in my input_field() box.
For now, I replaced my code with your code.
Thank you for your advice and I was told that char* is a new way to initialize a string so that's why it happened.
You have to understand what is happening when you declare a char* like that.
This code :
char* str = "some_text_goes_here";
actually translates to :
static char str_array[20] = {'s','o','m','e','_','t','e','x','t','_', 'g','o','e','s','_','h','e','r','e','\0'}; char* str = &str_array[0];
The compiler declares a static string and gives you a pointer to it.
Yes my compiler throws an error:- C++11 doesn't support conversion from char to a string.
You said you're using C, not C++11. Make up your mind. It makes a difference.
Thank you for your suggestion but I already tried it and it successfully compiled but it produces a runtime error.
I am posting my compleat code so please read it, it's in attachments.
It just stops working when I click in my input_field() box.
You're not using events properly at all. You HAVE to check the event type before you access the event union or else you will get complete garbage.
Try reading the manual. http://liballeg.org/a5docs/trunk/events.html
I want to make a GUI input field function which I can use for c and c++ both.
If I use any GUI API for my game instead of trying to create my own, will it be the optimal solution, I mean will my game become large by adding additional library just for GUI purpose.
In C++, you can't assign the value of a const char* (a string in C++) to a char*. It's illegal because the memory isn't yours. You have to declare your own buffer.
char* str = "abc";/// Illegal in C++, deprecated in C /// "abc" is a static const char array /// and it is illegal to remove the const without a cast
You don't need a GUI library for a single widget.
Check the event type first, and then access the union.
This should give you an idea of how to monitor keyboard events.
Thanks for your help and now I am going to use your library for GUI - (EAGLE5).
again thanks for your advice.
What is SantaHack2016?
is it a game?
again thanks and especially because of EAGLE5.cbp. I love code blocks builds.
Eagle5 doesn't have an input widget yet. Eagle4 has integer, floating, and string entry widgets.
But you can write one very easily. I can help you write an input widget if you want.
Something that was hinted at, but not fully explained, is the difference between `char []` and `char *` and what they're used for.
An array, which is indicated by the square brackets `[]` allocates a sequence of bytes on the program's call stack. This is just like other local variables that you define, like int and char. It exists until the function returns, and is automatically released.
A char pointer can either point to a single character, or to an array of characters, or NULL which signifies that memory has not been allocated yet, or garbage if you forget to initialize it (never do this because it's an obscure crash waiting to happen). The character or characters that you point to can either be on the stack or on the heap.
The heap is like a pool of memory that you can allocate that survives even when a function returns. It is accessible from anywhere in the program as long as you know its address in memory. In C, you use the malloc family of functions to allocate memory on the heap, and in C++ you use the new and new[] operators. Anything that you allocate in this way must be cleaned up after using free() for malloc() style allocations and the delete or delete[] operators for new allocations. A memory leak is said to have occurred when you forget to do this.
In the snippet above I've tried to demonstrate the basic ways that character arrays and pointers are used. The thing that you were forgetting to do was allocate memory for the string. Whether you should do this on the stack or heap depends on how big it will be and whether you need it only temporarily or you need to be able to return it from a function. This is all about memory management. C and to an extent C++ put this burden on you do manage it. It's fun to do, but it's also hard to do perfectly. You definitely should try to understand these concepts now because it'll take a while to really get full control of this.
bamccaig: Thanks for this information, I can't express how glad I am!
Is there any site where I can find more programming knowledge like this?
Edgar Reynaldo: Thanks man, I really appreciate that but I already ripped off the logic from SDL's "TextInput" function.
Thanks for your help!