Jump to content

  • Log In with Google      Sign In   
  • Create Account


Posted 19 September 2012 - 07:33 PM



Posted 19 September 2012 - 01:22 PM

Hi guys!
I have a bitmap image with all ASCII characters in it (ArialBlack.bmp) and I want to generate a code through which I can generate characters in my screen by taking them from the bitmap file through the SDL_Rect, one after another by pressing keys on my keyboard. For example when I press a word like HELLO i want this word to be shown in my screen from characters that I cut from .bmp. I solved this problem but with many lines hard coded using switch statements but now I want to optimize my code and I find a method but I don't know how to use it or maybe I'm not lucky today Posted Image. Do you have any idea how to do this? I don't know how to correlate the key that I take from this method with each character in picture. If i use a for loop my program is running through that loop and show me only last letter, not the others.
char getAsciiKey(const SDL_Event &sdlEvent)
	char key = 0;
	if(sdlEvent.key.keysym.sym >= SDLK_a && sdlEvent.key.keysym.sym <= SDLK_z) {
		if(sdlEvent.key.keysym.mod & KMOD_SHIFT) {
			key = 'A' + char(sdlEvent.key.keysym.sym - SDLK_a);
		} else {
			key = 'a' + char(sdlEvent.key.keysym.sym - SDLK_a);
	else if(sdlEvent.key.keysym.sym >= SDLK_0 && sdlEvent.key.keysym.sym <= SDLK_9) {
		key = '0' + char(sdlEvent.key.keysym.sym - SDLK_0);
	return key;

and I am calling this method in:
SDL_Event event;

while( !quit ){
			while( SDL_PollEvent( &event ) ){
				switch( event.type ){
					/* Keyboard event */
					case SDL_KEYDOWN:
				   [b][color=#ff0000] getAsciiKey(event);[/color][/b]
					case SDL_KEYUP:
					/* SDL_QUIT event (window close) */
					case SDL_QUIT:
						quit = 1;

Any help or advice from you, would be grateful.
Thank you!