• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Randall Perkins

Text error in C

6 posts in this topic

im having a issue, i have a screen where users can input a name for there player and after 7 letters and some other combos and back slashs it will freeze the application and hang

 

this is the code from the control file which i think is where the error is

#include "controls.h"

    SDL_Event control_event;


    static void DeleteChar(char text[], unsigned int pos){
        strcpy(text+pos, text+pos+1);
        printf("Remove Char : %s \n", text);
    }
    static void InsertChar(char text[], unsigned int pos, char c) {
        char *buf;
        sprintf(buf, "%c%s", c, text+pos);
        strcpy(text+pos, buf);
        free(buf);
        printf("Insert Char : %s \n", text);
    }

    static void keydown_event(SDL_keysym* key) {

        switch(app_state) {

            case 1:
                switch(key->sym) {
                    case SDLK_ESCAPE:
                        app_exit = 0;  // exits application
                    break;
                    case SDLK_UP:
                        if(menu_selections > 1) { menu_selections--; }
                    break;
                    case SDLK_DOWN:
                        if(menu_selections < 3) { menu_selections++; }
                    break;
                    case SDLK_RETURN:

                        // actions for menu selection
                        switch(menu_selections) {
                            case 1:
                                // setup new game variables
                                SDL_Delay(500);
                                SDL_EnableUNICODE(SDL_ENABLE); // enables Unicode for text input
                                app_state = 4;
                            break;
                            case 2:
                                SDL_Delay(500);
                                app_state = 2;
                            break;
                            case 3:
                                SDL_Delay(500);
                                app_state = 3;
                            break;
                        }

                    break;
                    default:
                        break;
                }
            break;

            case 2:
                switch(key->sym) {
                    case SDLK_ESCAPE:
                        app_exit = 0; // exits application
                    break;
                    default:
                        break;
                }
            break;

            case 3:
                switch(key->sym) {
                    case SDLK_ESCAPE:
                        app_exit = 0; // exits application
                    break;
                    default:
                        break;
                }
            break;

            case 4:

                if(strlen(name_input) <= 16) {
                    if((control_event.key.keysym.unicode >= (Uint16) 'A') && (control_event.key.keysym.unicode <= (Uint16) 'Z') ) {
                        InsertChar(name_input, strlen(name_input), (char)control_event.key.keysym.unicode);
                        name_display = TTF_RenderText_Solid(menu_font, name_input, blackColor);
                        printf("uppercase render\n");
                    }else if( (control_event.key.keysym.unicode >= (Uint16) 'a') && (control_event.key.keysym.unicode <= (Uint16) 'z') ) {
                        InsertChar(name_input, strlen(name_input), (char)control_event.key.keysym.unicode);
                        name_display = TTF_RenderText_Solid(menu_font, name_input, blackColor);
                        printf("lowercase render\n");
                    }
                }

                switch(key->sym) {


                    case SDLK_BACKSPACE:
                        if(ngs == 1) { if(strlen(name_input) != 0) {
                            DeleteChar(name_input, strlen((const char*)name_input)-1);
                            name_display = TTF_RenderText_Solid(menu_font, name_input, blackColor);
                            printf("Backspace render\n");
                        } }
                    break;

                    case SDLK_ESCAPE:
                        app_exit = 0; // exits application
                    break;

                    case SDLK_LEFT:
                        if(ngs_race > 1) { ngs_race--; }
                    break;

                    case SDLK_RIGHT:
                        if(ngs_race < 3) { ngs_race++; }
                    break;

                    case SDLK_RETURN:
                        switch(ngs) {
                            case 1:

                            //    SDL_EnableUNICODE(SDL_DISABLE);
                            break;
                            case 2:

                            break;
                            case 3:

                            break;
                        }
                    break;

                    default:
                        break;
                }
            break;

        }

    }

    void handle_controls(){
        while(SDL_PollEvent(&control_event)) {
            switch(control_event.type) {
                case SDL_KEYDOWN:
                    keydown_event(&control_event.key.keysym);
                break;
                case SDL_QUIT:
                    app_exit = 0;  // exits application
                break;
            }
        }
    }

Edited by MrPhoenix
0

Share this post


Link to post
Share on other sites

You have to allocate memory for the pointer to point to. Look up malloc() and free() on how to allocate memory dynamically.

 

For example:

[code]int length = strlen(text); char *buf = malloc(length-pos+2); // +1 for null character, +1 for the inserved character, and -pos since we don't have to allocate the part of the string before pos. sprintf(buf, "%c%s", c, text+pos); strcpy(text+pos, buf); free(buf);[/code]

1

Share this post


Link to post
Share on other sites
Brother Bob thanks for the help on that but im still having the freezing issue ?
that what im trying to figure out
 
#include "controls.h"

    SDL_Event control_event;


    static void DeleteChar(char text[], unsigned int pos){
        strcpy(text+pos, text+pos+1);
        printf("Remove Char : %s \n", text);
    }
    static void InsertChar(char text[], unsigned int pos, char c) {
        int length = strlen(text);
        char *buff = malloc(length-pos+2);
        sprintf(buff, "%c%s", c, text+pos);
        strcpy(text+pos, buff);
        free(buff);
        printf("Insert Char : %s \n", text);

    }

    static void keydown_event(SDL_keysym* key) {

        switch(app_state) {

            case 1:
                switch(key->sym) {
                    case SDLK_ESCAPE:
                        app_exit = 0;  // exits application
                    break;
                    case SDLK_UP:
                        if(menu_selections > 1) { menu_selections--; }
                    break;
                    case SDLK_DOWN:
                        if(menu_selections < 3) { menu_selections++; }
                    break;
                    case SDLK_RETURN:

                        // actions for menu selection
                        switch(menu_selections) {
                            case 1:
                                // setup new game variables
                                SDL_Delay(500);
                                SDL_EnableUNICODE(SDL_ENABLE); // enables Unicode for text input
                                app_state = 4;
                            break;
                            case 2:
                                SDL_Delay(500);
                                app_state = 2;
                            break;
                            case 3:
                                SDL_Delay(500);
                                app_state = 3;
                            break;
                        }

                    break;
                    default:
                        break;
                }
            break;

            case 2:
                switch(key->sym) {
                    case SDLK_ESCAPE:
                        app_exit = 0; // exits application
                    break;
                    default:
                        break;
                }
            break;

            case 3:
                switch(key->sym) {
                    case SDLK_ESCAPE:
                        app_exit = 0; // exits application
                    break;
                    default:
                        break;
                }
            break;

            case 4:

                if(strlen(name_input) <= 16) {
                    if((control_event.key.keysym.unicode >= (Uint16) 'A') && (control_event.key.keysym.unicode <= (Uint16) 'Z') ) {
                        InsertChar(name_input, strlen(name_input), (char)control_event.key.keysym.unicode);
                        name_display = TTF_RenderText_Solid(menu_font, name_input, blackColor);
                        printf("uppercase render\n");
                    }else if( (control_event.key.keysym.unicode >= (Uint16) 'a') && (control_event.key.keysym.unicode <= (Uint16) 'z') ) {
                        InsertChar(name_input, strlen(name_input), (char)control_event.key.keysym.unicode);
                        name_display = TTF_RenderText_Solid(menu_font, name_input, blackColor);
                        printf("lowercase render\n");
                    }
                }

                switch(key->sym) {


                    case SDLK_BACKSPACE:
                        if(ngs == 1) { if(strlen(name_input) != 0) {
                            DeleteChar(name_input, strlen((const char*)name_input)-1);
                            name_display = TTF_RenderText_Solid(menu_font, name_input, blackColor);
                            printf("Backspace render\n");
                        } }
                    break;

                    case SDLK_ESCAPE:
                        app_exit = 0; // exits application
                    break;

                    case SDLK_LEFT:
                        if(ngs_race > 1) { ngs_race--; }
                    break;

                    case SDLK_RIGHT:
                        if(ngs_race < 3) { ngs_race++; }
                    break;

                    case SDLK_RETURN:
                        switch(ngs) {
                            case 1:

                            //    SDL_EnableUNICODE(SDL_DISABLE);
                            break;
                            case 2:

                            break;
                            case 3:

                            break;
                        }
                    break;

                    default:
                        break;
                }
            break;

        }

    }

    void handle_controls(){
        while(SDL_PollEvent(&control_event)) {
            switch(control_event.type) {
                case SDL_KEYDOWN:
                    keydown_event(&control_event.key.keysym);
                break;
                case SDL_QUIT:
                    app_exit = 0;  // exits application
                break;
            }
        }
    }



and the stdout
Dark Galaxy V0.1 BETA UN-RELEASED
Author - Randall Perkins 1/18/13 

Starting graphics engine
Remove Char : Defaul 
Backspace render
Remove Char : Defau 
Backspace render
Remove Char : Defa 
Backspace render
Remove Char : Def 
Backspace render
Remove Char : De 
Backspace render
Remove Char : D 
Backspace render
Remove Char :  
Backspace render
Insert Char : r 
lowercase render
Insert Char : ra 
lowercase render
Insert Char : ran 
lowercase render
Insert Char : rand 
lowercase render
Insert Char : randa 
lowercase render
Insert Char : randal 
lowercase render
Insert Char : randall 
lowercase render
Insert Char : randallp (THE POINT THE PROGRAM STOPS WHEN THE 'P' IS PRESSED)  <-----------------
lowercase render
Exiting Graphics engine
Edited by MrPhoenix
0

Share this post


Link to post
Share on other sites
rip-off Thank you so much dude, i been stuck on this issue for hours now and your method fixed it biggrin.png
all i can say gamedev is a great community with people like you and i hope ill be able to help other people out like you

have a great day and once more thank you so much!
1

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  
Followers 0