Thread: SDLKey to ASCII without unicode support?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    SDLKey to ASCII without unicode support?

    Hello,

    What's the best way to convert an SDLKey to it's corresponding ASCII value without unicode support? I don't want unicode support because I don't need it, so I'd rather not suffer with the overhead.

    Basically I'm writing a game console for my game, I catch the key presses (when the console is active) and print the keys,

    Code:
    int consoleKey(SDLKey sym)
    {
        char c;
        static size_t i = 0;
    
        if(sym == SDLK_RETURN)
        {
            strcpy(lines[0], current);
            memset(current, 0, sizeof(current));
            i = 0;
            return 0;
        } else if(sym == SDLK_BACKSPACE) {
            current[i] = '\0';
            if(i > 0)
                --i;
            return 0;
        }
    
        /* look up the key? Look-up table or something */
        
    
        /* add the key if it'll fit */
        if((i + 1) < sizeof(current))
        {
            current[i] = c;
            current[i + 1] = '\0';
            ++i;
        }
    
        return 0;
    }
    What's the best way to convert 'sym' into it's corresponding ASCII code? Perhaps

    Code:
    char alpha[] = "abcdefghijklmnopqrstuvwxyz";
    
    if(sym >= SDLK_a && sym <= SDLK_z)
        c = alpha[sym - SDLK_a];
    But the manual said "And at last, never ever ever write your own custom ASCII conversion table." http://www.libsdl.org/cgi/docwiki.cgi/SDLKey

    Or a look-up table? Since it's less code Sorry if I'm asking too many questions :s
    I've searched and searched, the SDL_keysym struct info http://docs.mandragor.org/files/Comm...sdlkeysym.html and the web

    [edit]
    Perhaps I should only enable Unicode support when my console gets focus?
    Note: I haven't really commited to any library for graphics etc, I was considering Fmod for sound, GLFW for framework and alike. Would that be a better choice than SDL?
    [/edit]
    Last edited by zacs7; 10-06-2007 at 04:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM