Sign in to follow this  

C++ Text Input

This topic is 2343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there! Brand new to the forum, but I have been using the tutorials here for a while.

I am building a game completely in C++ utilizing the Allegro libraries. I have had a rather successful run but I have run into a little roadblock I was hoping someone might be able to help me with.

I have created a client-server style game utilizing winsock and I am to the point where I have been building player data that is saved, due to the fact that multiple players will be logging in at once and since I want them to have their data persist in the world, I want to give each player their own unique login. I have set all of that up in the backend.

So getting them to enter it right now is a very ugly, blocky group of code that I have that basically works but I figured there must be something more simplistic out there, but not as simplistic as CIN >> ...

Think of this like when you log into a MMO like World of Warcraft, they have those two little text boxes were you can manipulate them with mouse clicks and such.

I have seen windows forms, but was unsure that would be an acceptable approach as I am not utilizing forms, my entire game is built displaying bitmaps and the like.

So, my question is this:

Does anyone know of anything, utilizing C++, that they can point me to that I might be able to use in regards to a clickable, manipulatable textbox akin to those that you could build with any standard windows form (Like in Access) or... can a textbox just be placed utilizing windows forms without overlaying everything else with some kind of windows-looking box?

Sorry if this is convoluted and I appreciate the help ahead of time!

Edited by Isxaari

Share this post


Link to post
Share on other sites
[quote name='Isxaari' timestamp='1310592907' post='4835011']
Hi there! Brand new to the forum, but I have been using the tutorials here for a while.

I am building a game completely in C++ utilizing the Allegro libraries. I have had a rather successful run but I have run into a little roadblock I was hoping someone might be able to help me with.

I have created a client-server style game utilizing winsock and I am to the point where I have been building player data that is saved, due to the fact that multiple players will be logging in at once and since I want them to have their data persist in the world, I want to give each player their own unique login. I have set all of that up in the backend.

So getting them to enter it right now is a very ugly, blocky group of code that I have that basically works but I figured there must be something more simplistic out there, but not as simplistic as CIN >> ...

Think of this like when you log into a MMO like World of Warcraft, they have those two little text boxes were you can manipulate them with mouse clicks and such.

I have seen windows forms, but was unsure that would be an acceptable approach as I am not utilizing forms, my entire game is built displaying bitmaps and the like.

So, my question is this:

Does anyone know of anything, utilizing C++, that they can point me to that I might be able to use in regards to a clickable, manipulatable textbox akin to those that you could build with any standard windows form (Like in Access) or... can a textbox just be placed utilizing windows forms without overlaying everything else with some kind of windows-looking box?

Sorry if this is convoluted and I appreciate the help ahead of time!

Thanks,

John
[/quote]

Allegro has some basic GUI support, check
http://www.allegro.cc/manual/4/api/gui-routines/d_edit_proc (to make textboxes)
http://www.allegro.cc/manual/4/api/gui-routines/ (for gui in general)

Share this post


Link to post
Share on other sites
This is my method but it is a little convoluted:

I make a windows resource dialog in ResEdit (code below) for login details

Then I create the dialog on the fly and grab the information from the form when it returns... this involves adding the dialog and having a second message loop so I am not sure how to interface it with allegro.
Also this will only work for windows :/

Login,Password,IP Are declared as global strings
Port is a global int

Called from inside your code you would call it like so:
[code]
if (DialogBox(Windows.hInstMain, MAKEINTRESOURCE(IDD_DIALOG1),hWndMain,DialogProc) == IDCANCEL)
{
return 0; // we hit cancel exit program
}
else
{

Network.Connect(IP, Port);
Network.SetLogin(Login, Password);

}[/code]

forward declaration for Dialog's message handler
[code]static BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam); //dialog message proc[/code]

And here is what I use for the dialogproc hander function
[code]
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
//grab our IP and port from our config file
SetDlgItemText(hwndDlg, IDC_IPADDRESS, "127.0.0.1");
SetDlgItemInt(hwndDlg, IDC_PORT, 7777, false);
}return(false); // focus was set

case WM_COMMAND:
{
WORD idControl = LOWORD(wParam); // control, identifier
HWND hwndCtl = (HWND) lParam; // handle of control

switch(idControl)
{
case IDOK: // OK button hit!
{
// grab the info for username and password
char username[13];
char password[13];
char IPAddress[25];
int port;


//username and password and IP
GetDlgItemText(hwndDlg, IDC_USERNAME, username, 12); //max of 12 characters
GetDlgItemText(hwndDlg, IDC_PASSWORD, password, 12); //max of 12 characters
GetDlgItemText(hwndDlg, IDC_IPADDRESS, IPAddress, 25);
//port
port = GetDlgItemInt(hwndDlg, IDC_PORT, NULL, false);

Login = username;
Password = password;
IP = IPAddress;
Port = port;
cout << username << " : " << password << " : " << IPAddress << ":" << port << endl;

EndDialog(hwndDlg, IDOK); // end this dialog box.
}return(true);

case IDCANCEL: // cancel button hit!
{
EndDialog(hwndDlg, IDCANCEL); // end this dialog box.
}return(true);
}

}break;
}//end switch(uMsg)
return(false); // did not process message
}


[/code]


include these with your source:
[code]#define IDD_DIALOG1 100
#define IDC_USERNAME 1006
#define IDC_PASSWORD 1007
#define IDC_IPADDRESS 1008
#define IDC_PORT 1009[/code]



login.rc
[code]// Generated by ResEdit 1.4.4.19
// Copyright (C) 2006-2008
// http://www.resedit.net

#include "login.h"
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>


//
// Dialog resources
//
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
IDD_DIALOG1 DIALOG 0, 0, 186, 95
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_FIXEDSYS | WS_VISIBLE | WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg 2"
{
DEFPUSHBUTTON "OK", IDOK, 129, 7, 50, 14, BS_DEFPUSHBUTTON
PUSHBUTTON "Cancel", IDCANCEL, 129, 24, 50, 14, BS_PUSHBUTTON
LTEXT "Username", IDC_STATIC, 5, 10, 33, 8, SS_LEFT
LTEXT "Password", IDC_STATIC, 5, 30, 32, 8, SS_LEFT
EDITTEXT IDC_USERNAME, 40, 10, 67, 14, ES_AUTOHSCROLL
EDITTEXT IDC_PASSWORD, 40, 30, 67, 14, ES_AUTOHSCROLL | ES_PASSWORD
EDITTEXT IDC_IPADDRESS, 20, 60, 88, 13, ES_AUTOHSCROLL
EDITTEXT IDC_PORT, 129, 60, 40, 14, ES_AUTOHSCROLL | ES_NUMBER
LTEXT "Server IP Address", IDC_STATIC, 35, 78, 59, 8, SS_LEFT
LTEXT "Port", IDC_STATIC, 139, 80, 14, 8, SS_LEFT
}[/code]

Ends up looking similar to this:
[attachment=4151:Dialog.png]

Share this post


Link to post
Share on other sites
SimonForsman,

Thank you very much. I would have never found that one by the name on my own. I have been wracking my brain for a while.

Much nicer than my big ugly block of code that I can now delete.

I really appreciate it.

Yewbie,

Thanks for your reply as well. If I have trouble with the bit from Allegro, I will totally push on to yours. I see a lot of form data in there which I am not using at all (Though I am using windows, so I could build around it. I am not too worried about multi-platform support yet! :D

John

Share this post


Link to post
Share on other sites
Okay, so in case anyone else is looking into this problem, I wanted to take a few moments to explain this one as it took me just a bit to figure out.


First of all, if you are using Allegro 4.something, you can find examples of the code under ..Allegro/examples/ and they will be in the two files "exgui.c" and "excustom.c"
While I did not look very hard through the files in higher versions, like 5, those two files do not exist (which is what the manual points to)

Anyway! I wanted to go over a little bit how this works because I was stumped for a bit until I figured it out and how it behaves. I am going to use the code that they supply as an example:


[code]

#include "allegro.h"

#define LEN 32
char the_string[(LEN + 1) * 6] = "Change Me!";

DIALOG the_dialog[] =
{
/* (dialog proc) (x) (y) (w) (h) (fg) (bg) (key) (flags) (d1) (d2) (dp) (dp2) (dp3) */
// { d_clear_proc, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, NULL, NULL, NULL },
{ d_edit_proc, 12, 82, 256, 48, 255, 0, 0, 0, LEN, 0, the_string, NULL, NULL },
// { d_check_proc, 12, 12, 161, 49, 255, 0, 't', 0, 0, 0, "&Toggle Me", NULL, NULL },
// { clock_proc, 242, 12, 64, 64, 255, 0, 0, 0, 0, 0, NULL, NULL, NULL },
// { change_font_proc, 12, 142, 141, 49, 255, 0, 'f', D_EXIT, 0, 0, "Change &Font", NULL, NULL },
// { d_button_proc, 162, 142, 141, 49, 255, 0, 0, D_EXIT, 0, 0, "Exit", NULL, NULL },
{ NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }
};

[/code]

This is the setup for it. While the example shows one of each of the buttons (You can see them built into the_dialog), I have notated out all the ones I was not using. According to the help files, the last one is terminating the array. (The one titled NULL)

Each of the ones I have commented out are actually other objects. Each of those values are positions, colors, and values to display.

This is where I had to do a bit of learning. Unfortunately their example really only works if you want the text box to mess everything else up allegro does. They advise utilizing "do_dialog( )" by itself in the example and this just did not work for me. What I learned is that when the text box is active, until it is finished (by the esc key) or it is manually shut down, it will persist and stop the program. I assume you could do some kind of multithreading for this if you really wanted to, but yeah. Too convoluted, instead they offer additional support through other functions that they do not warn you about until you peck around:

[code]
int main(void)
{
// Make sure you initialize allegro as well as your mouse.

DIALOG_PLAYER *dPlayer = init_dialog(the_dialog, -1);

if(!update_dialog(dPlayer))
{
// Do what you will with the input data
shutdown_dialog(dPlayer);
}
}
[/code]

This is in comparison to the way the examples display it as:

[code]
int main(void)
{
// Make sure you initialize allegro as well as your mouse.
do_dialog(dPlayer, -1);
}
[/code]

This of course works, to an extent and maybe if I was more savvy with these things I could figure out a way to utilize it, but without the shutdown_dialog() being called, I found that both mouse pointers (I have my own custom cursor) was being displayed and that the box itself would not disappear from the screen. Calling init_dialog and shutdown_dialog seemed to be the way to go.

You can add more objects on your screen by adding more lines to the "the_dialog" array.

Hope this helps someone else that might be looking for this at some point and thank you everyone again for your very fast help.

John

Edit: PS: I am not presuming to say Allegro, the examples, or the manuals are bad! Just that I did not understand them completely in my early beginnings of programming. I re-read my post and thought it might have sounded snobbish, was not meant to be! I would have gotten no where without the manual, examples and help from people here and examples through google searches.

Share this post


Link to post
Share on other sites

This topic is 2343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this