Sign in to follow this  
ostamo1

Trying to make an edit box sdl/opengl

Recommended Posts

i have made a class Ccontrol and derived from it CtextBox now i can get everything to display properely but i would like to draw text to the box when someone types on the keyboard. sometimes it works but prints double characters and sometimes it does not work at all. any help at all would be appreciated here is my function for getting the key presses
bool CtextBox::KeyPress(SDLKey p_key, SDLMod p_mod, Uint16 unicode)
{
	//if(!inside) return(false);
	if(p_key==0) return(false);
	switch(p_key)
	{
	case 0:
		{
			return(false);
		}break;
	case SDLK_BACKSPACE://backspace
		{
			if(text.size()>0)
				text.erase(text.size()-1);
			return(true);
		}break;
	case 9://tab
		{
			if(p_mod&KMOD_SHIFT)
			{
				
			}
			else
			{
				
			}
			return(true);
		}break;
	case SDLK_RETURN://enter SDLK_KP_ENTER
		{
			
			return(true);
		}break;
	default:
		{
			SetText(GetText()+(char)p_key);
			return(true);
		}
	}
	int textX = iGLengine->getTextWidth(label.data())/2;
	int textY = posY+ (height - iGLengine->getTextHeight(label.data()))/2;
	iGLengine->drawText(textX,textY,text.c_str());
}

this is how i pass the sdlkey events to my control
	while (SDL_PollEvent(&event))
		{
			
			keys= SDL_GetKeyState(NULL);
			txt_text->KeyPress( event.key.keysym.sym,
                                event.key.keysym.mod,
                                event.key.keysym.unicode );	
		}

Share this post


Link to post
Share on other sites
It looks as if younr input system is the main problem. Rather than going though that, I'll shamlessly point you towards a SDL input system I wrote a while back, it should work great for what you are doing. Here it is.

Here's a modified demo.cpp file that is geared towards what you would use:

#include "SDL.h"

#pragma comment( lib, "SDLmain.lib" )
#pragma comment( lib, "SDL.lib" )

#include <iostream>

// Include the input class header file
#include "Input.h"

// This would be the input class you have for your program. Note it is setup
// so you can actually have multiple classes and switch between them on demand
class BaseInput : public Input
{
public:
// Called on a key press event. If it returns true, the event is removed from the buffer
int OnKeyDown( const tKey& Key )
{
// A-Z then a-z
if( (Key.key >= 65 && Key.key <= 90) || (Key.key >= 97 && Key.key <= 122) )
{
if(!IsKeyDown(SDLK_LSHIFT) && !IsKeyDown(SDLK_RSHIFT))
std::cout << (char)Key.key << " pressed at " << Key.time << std::endl;
else
std::cout << (char)(Key.key - 32) << " pressed at " << Key.time << std::endl;
}
return true;
}

// Called on a key release event. If it returns true, the event is removed from the buffer
int OnKeyUp( const tKey& Key )
{
//std::cout << "Key[" << Key.name << "]released at " << Key.time << std::endl;
return true;
}
};

BaseInput InputClass;

int main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return -1;
}

atexit(SDL_Quit);

bool Done = 0;

if ( NULL == SDL_SetVideoMode( 640, 480, 32, SDL_DOUBLEBUF | SDL_HWSURFACE ) )
return -1;

SDL_Surface* Screen = SDL_GetVideoSurface();

SDL_Event Event;

while( Done == false )
{
while ( SDL_PollEvent(&Event) )
{
if ( Event.type == SDL_QUIT )
Done = 1;

// Here we first call the Poll function with the current event
// inside our polling loop
InputClass.Poll(Event);
}

// Then we just have to call the update function to take the data
// from the Poll event and update the class
InputClass.Update();

// If the user presses escape, we are done with the program
if( InputClass.IsKeyDown( SDLK_ESCAPE ) )
{
Done = 1;
}
}

SDL_FillRect( Screen, NULL, 0x000000 );
SDL_Flip( Screen );

return 0;
}



In your BaseInput class, where I handled the keydown, is where you could add your code to add the character, or process the key. In that example, if you run it, only a-z and A-Z are outputted. You can easily add more code to handle the backspace, return, etc.. very easily.

Just an idea to try if you want to. I know I like that input system [wink]. If you decide to use it and need help setting it up to do what you want it to do feel free to PM me.

Otherwise, take a look at that library in terms of what I did concept wise, and you can form your own input system. Good luck!

Share this post


Link to post
Share on other sites

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