A Little Help Required To Get Ghostbusters Finished...


Gadget

Member
Joined
May 16, 2006
Messages
247
Hi,

I have been so busy (and ill) over the last couple of months and I haven't had the time required to finish my port (actually a complete write as there wasn't any code to pull from MMF) of Ghostbusters for the GP2X.

I am after a bit of help:-

1) I am working on the account entry bit, where you enter a number that corresponds to your balance. How can I convert a long to char[]? I need to be able to operate on the individual chars afterwards to work out a checksum and merge with the account number. I have tried ToString() and casting but I seem to be going around in circles trying to work out why these won't work.

2) Does anyone have any example code for fast pixel hit detection between 2 sprites? I need to add this to the 'catch the ghost bit'. I realise how to do this, lock the surface and check the collision sprite looking for none background pixels in the collision rect but wondered if anyone had any SDL related code that I could quickly add in?

Many thanks, credits will be included on the title screen and in the credits for any help given.
 
Gadget posted on Dec 21 2006 at 09:36 AM said:
1) I am working on the account entry bit, where you enter a number that corresponds to your balance. How can I convert a long to char[]? I need to be able to operate on the individual chars afterwards to work out a checksum and merge with the account number. I have tried ToString() and casting but I seem to be going around in circles trying to work out why these won't work.

I think this should do it for you:

http://www.comeaucomputing.com/techtalk/#noitoa
 
Last edited by a moderator:
For converting numbers to text, I used

Code:
//Render the text (X wins)
	 char buffer[4];
	 sprintf(buffer, "%i", xwins);
	 SDL_Surface *message = TTF_RenderText_Solid(font, buffer, textColor );

Which is what saehn's tells you about, quite useful (and took some finding)

Hope it helps

Ben
---
 
Gadget posted on Dec 23 2006 at 08:08 AM said:
Thanks guys!!

Might be a little late by now .... but if you're coding in C++ and have the STL available you can use a C++ stringstream.

Code:
#include <iostream>
#include <sstream>

int main(void)
{

  std::stringstream my_ss;
  long i = 1000L;

  my_ss << i;
  std::cout << my_ss.str().c_str() << std::endl;

  return 0;
}

It might not be the fastest way of doing things in terms of execution time (C++ STL drawbacks) but if you're looking for something that will clean itself up memory wise (once your std::stringstream object goes out of scope) then it'll do the job - you could call strlen(my_ss.str().c_str()) to find the right number of characters to new[] or malloc() your char[] array appropriately instead of using horrid statically sized buffers.

Drawback of using statically sized buffers in code is that, for example, somebody manages to input a number of '10001' - the example above of 'char buffer[4]' would crash when you sprintf in to it ... unless you use :

snprintf(buffer,3,"%i",variable); (remember that with an array the final element in the array should be NULL otherwise you run in to some pretty difficult to trace memory allocation errors later on or random segfaults).

If you're after speed then use the snprintf method with statically sized buffer, but be aware that you could potentially cut off some of the number from the result.
 
Last edited by a moderator:
Back
Top