SDL frame freezing

Hi,

I’ve created a game chich asks the user to type a word in a prompt and then it shows it in a SDL frame.
But there’s a problem, the frame freezes some seconds after it appears.
The program works fine in Ubuntu, and in Windows with C::B debug mode. (not debug compilation!!)

Any solution?
In advance, thanks.

My code: http://codepad.org/WkO2O5tn
Example of mots.txt:

Code:
abordage
balisage
charabia
deuxieme
ecarlate
francais
gratuite
horrible

Can you place the code as an attachment?

codepad is not receiving connections from where I am sitting.On Thu, Dec 16, 2010 at 8:52 PM, Dinduks <samy.dindane at gmail.com> wrote:

Hi,

I’ve created a game chich asks the user to type a word in a prompt and then
it shows it in a SDL frame.
But there’s a problem, the frame freezes some seconds after it appears.
The program works fine in Ubuntu, and in Windows with C::B debug mode. (not
debug compilation!!)

Any solution?
In advance, thanks.

My code: http://codepad.org/WkO2O5tn
Example of mots.txt:

Code:

abordage
balisage
charabia
deuxieme
ecarlate
francais
gratuite
horrible


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Can you place the code as an attachment?

codepad is not receiving connections from where I am sitting.> On Thu, Dec 16, 2010 at 8:52 PM, Dinduks <samy.dindane at gmail.com> wrote:

Hi,

I’ve created a game chich asks the user to type a word in a prompt and
then it shows it in a SDL frame.
But there’s a problem, the frame freezes some seconds after it appears.
The program works fine in Ubuntu, and in Windows with C::B debug mode.
(not debug compilation!!)

Any solution?
In advance, thanks.

My code: http://codepad.org/WkO2O5tn
Example of mots.txt:

Code:

abordage
balisage
charabia
deuxieme
ecarlate
francais
gratuite
horrible


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Here you go:

Code:
#include
#include
#include
#include
#include <time.h>
using namespace std;

int jouer(int argc, char**argv);
void clearScreen();
void attendreSaisie();

int main(int argc, char**argv){

int choix = 0;

while(1){

    clearScreen();

    cout << "\n === Motus ===" << endl;
    cout << "\n 1. Jouer" << endl;
    cout << " 2. Quitter" << endl;

    cout << "\n\n Choix: ";
    cin >> choix;

    switch(choix){
        case 1:
            jouer(argc, argv);
            break;
        case 2:
            return 0;
            break;
        default:
            return 0;
            break;
    }
}

}

//int?gration de la sdl
#include “SDL.h”
#include “SDL_ttf.h”

void drawRect(int x, int y, int w, int h, Uint32 color, SDL_Surface* ecran);
void drawBorders( SDL_Surface *ecran);

int jouer(int argc, char**argv){

//initialisation de la sdl
SDL_Surface *ecran = NULL, *lettre = NULL;
TTF_Font *font = NULL;

SDL_Init(SDL_INIT_VIDEO);
TTF_Init();

ecran = SDL_SetVideoMode(234, 261, 32, SDL_DOUBLEBUF);
SDL_WM_SetCaption("Motus", NULL);

//colori la surface en bleu
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 55, 122, 190));

//dessine les bords
drawBorders(ecran);

//mise ? jour de l'affichage
SDL_Flip(ecran);

//ouverture du fichier
fstream fichier("mots.txt");
string ligne = "", sMots = "";
int nbrMots = 0;
while(getline(fichier, ligne)){
    nbrMots++;
    sMots += ligne + " ";
}

//cr?ation du tableau qui va contenir les mots
string mots[nbrMots];

//insertion des mots dans le tableau
int counterPos = 0;
for( int i=0; i<nbrMots; i++ ){
    mots[i] = "";
    do{
        mots[i] += sMots[counterPos];
        counterPos++;
    }while(sMots[counterPos] != ' ');
    counterPos++;
}

//choisir un mot al?atoire
srand(time(NULL));
string motChoisi = mots[rand() % nbrMots-1];

clearScreen();

//demander la saisi du mot
cout << "\n 1er essai:" << endl;
cout << " Veuillez saisir un mot de huit lettres commencant par '";
cout << motChoisi[0] << motChoisi[1] << motChoisi[2];
cout << "': ";

string motSaisi = "";
//cr?ation du bg
SDL_Surface *bg = SDL_CreateRGBSurface(SDL_HWSURFACE, 27, 35, 32, 0, 0, 0, 0);

//position du bg et de la lettre
SDL_Rect pos, posLetter;
pos.x = 2, pos.y = 2;
posLetter.x = 0, posLetter.y = 0;

//initialisation de la police
font = TTF_OpenFont("tahoma.ttf", 20);
SDL_Color noir = {0, 0, 0};

//pour chaque mot saisi (7 fois max)
for( int i=0; i<7; i++ ){

    //si le joueur n'en est pas ? son premier essai on va lui demande de REsaisir
    if( i>0 ) {
        cout << i+1 << "eme essai: ";
        cout << "\n Veuillez resaisir un mot de huit lettres: ";
    }

    //on demande de saisir un mot
    cin >> motSaisi;

    //verification si le mot fait bien huit lettres
    while( motSaisi.length() != 8 ){
        cout << "\n Vous n'avez pas saisi un mot de huit lettres, veuillez reessayer: ";
        cin >> motSaisi;
    }

    cout << "\n ";

    //comparaison et affichage graphique
    for( int j=0; j<8; j++ ){
        //condition pour colorier le bg
        if( motSaisi[j] == motChoisi[j] ){
            SDL_FillRect(bg, NULL, SDL_MapRGB(ecran->format, 94, 187, 70));
            SDL_BlitSurface(bg, NULL, ecran, &pos);
        } else if( motSaisi[j] == motChoisi[j-1] || motSaisi[j] == motChoisi[j+1] ) {
            SDL_FillRect(bg, NULL, SDL_MapRGB(ecran->format, 243, 131, 35));
            SDL_BlitSurface(bg, NULL, ecran, &pos);
        }

        //affichage des lettres
        stringstream sstr;
        sstr << motSaisi[j];
        string outputString = sstr.str();

        //to upper case
        transform(outputString.begin(), outputString.end(),outputString.begin(), ::toupper);
        lettre = TTF_RenderText_Blended(font, outputString.c_str(), noir);
        //on donne une bonne position aux lettres
        posLetter.x = pos.x + 13 - lettre->w/2;
        posLetter.y = pos.y + 4;
        //on parse les lettres sur l'?cran
        SDL_BlitSurface(lettre, NULL, ecran, &posLetter);

        pos.x += 29;
    }

    //mise ? jour de l'affichage
    SDL_Flip(ecran);

    //mise ? jour de la position du bg
    pos.x = 2;
    pos.y += 37;

    //si le mot est trouv?
    if( motSaisi+(char)13 == motChoisi || motSaisi == motChoisi ){ //(char)13 retour chariot sous linux
        cout << "\n\n Correct! :D";
        cout << "\n\n\n ";
        attendreSaisie();
        SDL_FreeSurface(ecran);
        SDL_Quit();
        return 0;
    }

    //si le mot n'est pas trouv? au bout de 7 tentatives
    if( i==6 ){
        cout << "\n\n Vous avez echoue. :(" << endl;
        cout << " Le mot en question etait: " << motChoisi;
        cout << "\n\n\n ";
        attendreSaisie();
        SDL_FreeSurface(ecran);
        SDL_Quit();
        return 0;
    }

}
return 0;

}

void drawRect(int x, int y, int w, int h, Uint32 color, SDL_Surface* ecran) {
SDL_Rect rect = {x,y,w,h};
SDL_FillRect(ecran, &rect, color);
}

void drawBorders( SDL_Surface *ecran){
int y = 0;
for( int i=0; i<8; i++ ){
drawRect(0, y, 234, 2, SDL_MapRGB(ecran->format, 255, 255, 255), ecran);
y += 37;
}
int x = 0;
for( int j=0; j<9; j++ ){
drawRect(x, 0, 2, 261, SDL_MapRGB(ecran->format, 255, 255, 255), ecran);
x += 29;
}
}

void attendreSaisie(){
#ifdef WIN32
system(“pause”);
#else
//Code Linux ici
#endif
}

void clearScreen(){
#ifdef WIN32
system(“cls”);
#else
system(“clear”);
#endif
}

[/code]------------------------
My blog: www.dinduks.com
(http://profile.xfire.com/samyfom)

Bump!------------------------
My blog: www.dinduks.com
(http://profile.xfire.com/samyfom)