Hangbot - Asimov Testgame

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Hangbot - Asimov Testgame

Post by Asimov » July 4th, 2013, 4:06 pm

Hi all,

Yeh I get a little confused with all this unicode stuff LOL. All I know is this. If it has a squiggly, Give it an L LOL.

Anyway I tried wchar_t and indeed I could send it to the messagebox without problems, but for some reason it is not getting the letter.

Strange if I add a break point I find that resText has the value of 'S' and for some reason firstChar does not show up at all when I hit the breakpoint. Which is wierd.

Anyway I know firstChar hasn't got the value of 'S' because I get a blank messagebox on the screen.

Code: Select all

 HGLOBAL hMem = LoadResource(hInstance, hRes);
   DWORD size = SizeofResource(hInstance, hRes);
   char* resText = (char*)LockResource(hMem);
   char* text = (char*)malloc(size + 1);
   memcpy(text, resText, size);
   text[size] = 0;
	
   void* lockedRes = LockResource(hMem);

// Now use this for the first char, for example:
//char firstChar = ((char*)lockedRes)[0];

wchar_t firstChar = ((char*)lockedRes)[0];
MessageBox(NULL, LPCWSTR(firstChar), TEXT("TEXT"), NULL);
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Hangbot - Asimov Testgame

Post by LuX » July 4th, 2013, 10:30 pm

If you read the test I made, which you can repeat, it clearly proves that it in fact is an 'S'.

It might not show up on the messagebox because a char != a long string. You might have to convert it via the printw_f function or someting called like that to store in a wchar_t array.
ʕ •ᴥ•ʔ

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Hangbot - Asimov Testgame

Post by Asimov » July 4th, 2013, 11:08 pm

Hi Lux,

Sorry I thought LPCWSTR(firstChar) was casting it to a wide string. I need to do some more experiment.

However am I right in thinking that ((char*)lockedRes)[0] is just a character array.

Hence if I did this
char test = ((char*)lockedRes)[1];

Should that give me the second character and so forth.

I suppose the only thing I gotta work out is how to get my text lines to read in as strings.
I am thinking along the llines of fscanf_s. I know that works with files, but I am not sure if it can be used to parse a char array or not.

PS I did a similar test.

Code: Select all

if (firstChar=='S')
{
MessageBox(NULL, L"found", TEXT("TEXT"), NULL);
}
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

Shaki
Posts: 104
Joined: June 13th, 2012, 12:20 am

Re: Hangbot - Asimov Testgame

Post by Shaki » July 4th, 2013, 11:19 pm

look at vscan and swprint and other string/widestring functions.

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Hangbot - Asimov Testgame

Post by Asimov » July 4th, 2013, 11:41 pm

Hi all,

Well I managed to read part of the array to a string by doing this.

Code: Select all

string str = string(((char*)lockedRes), 100);
Now all this did is read the first 100 characters of the char array into a string.

However I need more control.
The first line is only 5 characters.

So I have to read it in line by line.
So my first word in my string array should be "Shrek", and so forth.

Hi Shaki, I am looking into this.

If I was using a pFile to load from a file I could use
sscanf to decode it.

So something similar might work for a char array.

I think I almost have this cracked. I have to break up my char array into separate strings by maybe reading the return character eg \n
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Hangbot - Asimov Testgame

Post by Asimov » July 5th, 2013, 12:05 am

Hi all,

Well I managed to get the first line of the text by doing tihs.

Code: Select all

string test;
for (int g=0; g<500; g++)
{
	if(((char*)lockedRes)[g]=='\n')
	{
		test = string(((char*)lockedRes), g);
		break;
	}
}
Now this line string test = string(((char*)lockedRes), g); only works because it is checking for the return character and chopping off the start.

Then to get the next string I would have to start from after the return character and read in the next string.

I think I am on the verge of getting it to work now.

Obviously the number 500 will be changed to the value of the EOF eventually.
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Hangbot - Asimov Testgame

Post by Asimov » July 5th, 2013, 12:27 am

Hi all,

Well the code is almost complete to read the char to strings. Yes it needs tidying, and I bet there is a better way I haven't worked out yet, but so far this works.

It works by creating a buffer, which copys the main char array. Every time the it finds a return eg '/n' it copys the buffer to a string array called test, and resets the buffer. Of course the buffer has to be big enough to hold the entire word, but so far it works.

Code: Select all

string test[40];
char buffer[100];
int buffercount=0;
int testcount=0;
for (int g=0; g<500; g++)
{
	//if(((char*)lockedRes)[g]=='\n') {MessageBox(NULL, L"Return found", TEXT("TEXT"), NULL);}

	buffer[buffercount]=((char*)lockedRes)[g];
	buffercount++;
	if(((char*)lockedRes)[g]=='\n')
	{		
		test[testcount] = string(buffer,buffercount);
		buffercount=0;
		testcount++;
	}
}
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

Shaki
Posts: 104
Joined: June 13th, 2012, 12:20 am

Re: Hangbot - Asimov Testgame

Post by Shaki » July 5th, 2013, 12:46 am

Code: Select all

std::vector<string> str;
Is a dynamic string array that can help, also if you're copying char arrays to strings, try using strcpy.
Sprintf also works.

Code: Select all

sprintf(stringToSendTo,"%s\n",stringToReadFrom);

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Hangbot - Asimov Testgame

Post by LuisR14 » July 5th, 2013, 10:06 am

Asimov wrote: If I was using a pFile to load from a file I could use
sscanf to decode it.
haha, you misspelled to the correct function you should use :P
sscanf is the function similar to fscanf that scans a string array instead :)
but i think you should use swscanf instead o.o
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Hangbot - Asimov Testgame

Post by Asimov » July 5th, 2013, 1:14 pm

Hi Shaki,
Is a dynamic string array that can help, also if you're copying char arrays to strings, try using strcpy.
Sprintf also works.
In the example I posted I am using a standard string array, just for test purposes. I wll be using a vector array when I iron out the bugs LOL, as there is no limit to the size of a vector array, apart from the hard drive size, and memory heh heh.

I have come across strcpy before, but I am not sure how it helps me here, as my routine does work. I know strcpy is quite fast, but can it copy sections of a char to a string.

The way my routine works at the moment is that the 'char buffer' copying from the main char array, and then transferring the contents of the buffer, resetting the buffere and then carrying on to the next string. I will google strcpy to see if it can do the same job, as the code I posted in my last post.

I know that the code isn't perfect, but it was a fast and ready to show that it is working.

I very often code like this, and then go back over it to see how I can improve it.

Thanks for the suggestions.

PS I just looked up strcpy and that is used with char strings. whereas I need to convert to std::strings. so I don't think I can use it here.


PPS to LUX: Hey I changed the code from this

Code: Select all

HRSRC hRes = FindResource(hInstance, MAKEINTRESOURCE(IDR_MOVIES), L"TXT"); // <- make sure it's the type you specify it to be...
	if (hRes == 0)
       { 
			MessageBox(NULL, LPWSTR(L"Text Not Loaded"), TEXT("TEXT"), NULL);
        }

   HGLOBAL hMem = LoadResource(hInstance, hRes);
   DWORD size = SizeofResource(hInstance, hRes);
to this, and it still works. So I don't need to pass hInstance to it at all.

Code: Select all

HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_MOVIES), L"TXT"); // <- make sure it's the type you specify it to be...
	if (hRes == 0)
       { 
			MessageBox(NULL, LPWSTR(L"Text Not Loaded"), TEXT("TEXT"), NULL);
        }

   HGLOBAL hMem = LoadResource(NULL, hRes);
   DWORD size = SizeofResource(NULL, hRes);
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

Post Reply