Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème d'affichage d'image avec BlitSurface

    21 février 2020 à 21:59:31

    Bonjour,

    Comme l'indique le titre j'ai un soucis avec un SDL_BlitSurface :

    char charge_image(SDL_Surface*screen,char name[1000])
    {
        SDL_Surface*temp=SDL_LoadBMP(name);
        SDL_Rect TEMP={screen->w/2-temp->w/2,screen->h/2-temp->h/2,temp->w,temp->h};
        SDL_BlitSurface(temp,NULL,screen,&TEMP);
        SDL_FreeSurface(temp);
        printf("\n%s",name);
        return 0;
    }
    
    charge_image(surface[1],tab_image[i].path);

    Si l'appel de la fonction est mis dans la fonction principale, l'image sera affichée. Par contre, si l'appel de cette fonction est mis dans une fonction secondaire (donc autre que dans le main), l'image ne s'affiche pas...

    Est-ce un problème d'adresse ?

    • Partager sur Facebook
    • Partager sur Twitter
      21 février 2020 à 23:05:04

      Hello,

      Avec si peu de code, difficile d'avoir un avis. Mais fais-tu bien quelque part un SDL_Flip() ? Sans lui, pas de mise à jour de l'affichage.

      -
      Edité par edgarjacobs 21 février 2020 à 23:05:38

      • Partager sur Facebook
      • Partager sur Twitter

      On écrit "j'ai tort", pas "tord" qui est le verbe "tordre" à la 3ème personne de l'indicatif présent

        22 février 2020 à 17:58:40

        Il y a bien un SDL_Flip dans le main, peut-être en faut-il un aussi dans cette fonction secondaire ?

        EDIT : Attention : il est assez copieux :

        Le main :

        #include <stdio.h>
        #include <stdlib.h>
        #include <stdbool.h>
        #include <windows.h>
        #include <inttypes.h>
        #include <io.h>
        #include <SDL/SDL.h>
        #include <SDL/SDL_ttf.h>
        #include <SDL/SDL_image.h>
        #include <SDL/SDL_rotozoom.h>
        #include <SDL/SDL_framerate.h>
        #include <SDL/SDL_gfxPrimitives.h>
        #include "function.h"
        #include "barre_des_taches.h"
        #include "saisie_des_touches.h"
        #include "creation_image.h"
        #include "test.h"
        #include "boutons.h"
        
        int main ( int argc, char** argv )
        {
            if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
            {
                printf( "Unable to init SDL: %s\n", SDL_GetError() );
                return 1;
            }
            if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1 )
            {
                fprintf(stderr,"Erreur lors de l'initialisation de la SDL\n");
                return -1;
            }
            Detect detect1;
            TTF_Init();
            if(TTF_Init()==-1)
            {
                fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
                exit(EXIT_FAILURE);
            }
        
            IMG_Init(IMG_INIT_PNG);
            Uint8 *key = SDL_GetKeyState( NULL );
        
            //atexit(SDL_Quit);
        
            const SDL_VideoInfo*resolution=NULL;
            resolution=SDL_GetVideoInfo();
            int W=resolution->current_w;
            int H=resolution->current_h;
            putenv("SDL_VIDEO_WINDOW_POS=0,26");
            SDL_Surface* screen = SDL_SetVideoMode(W, H-50, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
            if ( !screen )
            {
                printf("Unable to set 640x480 video: %s\n", SDL_GetError());
                return 1;
            }
            SDL_WM_SetCaption("Editeur 2D",NULL);
            SDL_Surface *texte=NULL;
            SDL_Surface *texte2=NULL;
            SDL_Surface *texte3=NULL;
            SDL_Surface *texte4=NULL;
            SDL_Surface *texte5=NULL;
            TTF_Font *Texte=NULL;
            Texte=TTF_OpenFont("police.ttf",20);
            SDL_Color noir={0,0,0};
            SDL_Color blanc={255,255,255};
        
            bool done = false;
            char chaine[max_length]="";
            char chaine2[max_length]="";
            char chaine3[max_length]="";
            char chaine4[max_length]="";
            char chaine5[max_length]="";
            char chaine6[max_length]="";
            char char_name_image[max_length]="";
            char char_name_file[max_length]="";
            detect1.value=0;
            detect1.tag=0;
            detect1.check=malloc(nb_window*sizeof(int));
            detect1.check[0]=0;
            detect1.check[1]=0;
            detect1.check[2]=0;
            SDL_Rect curseur={0,0,1,1};
            SDL_Surface *window=NULL,*window2=NULL,*roue_chromatique=NULL,*barre=NULL;
            SDL_Surface**bouton_fermer=malloc(sizeof(SDL_Surface)*nb_window);////////////////
            window=SDL_LoadBMP("images/fenetre.bmp");
            window2=SDL_LoadBMP("images/fenetre2.bmp");
            roue_chromatique=IMG_Load("images/roue_chromatique.bmp");
            barre=SDL_LoadBMP("images/barre.bmp");
            for(int i=0;i<nb_window;i++)
            {
                bouton_fermer[i]=IMG_Load("images/bouton_fermer.png");
            }
        
            SDL_Rect fenetre={screen->w/2-500/2,screen->h/2-200/2,500,200};
            SDL_Rect fenetre2={screen->w/2-window2->w/2,screen->h/2-200/2,500,200};
        
            TTF_Font *ttf_texte=NULL;
            ttf_texte=TTF_OpenFont("police.ttf",20);
        
            SDL_Color gris_clair={235,235,235};
            SDL_Surface *Donner_nom_projet=NULL;
            Donner_nom_projet=TTF_RenderText_Shaded(ttf_texte,"Donner le nom du projet",noir,gris_clair);
            SDL_Surface *Donner_nom_tileset=NULL;
            Donner_nom_tileset=TTF_RenderText_Shaded(ttf_texte,"Donner le nom du tileset",noir,gris_clair);
            SDL_Surface *Donner_w_tileset=NULL;
            Donner_w_tileset=TTF_RenderText_Shaded(ttf_texte,"Donner le nombre de tiles en largeur",noir,gris_clair);
            SDL_Surface *Donner_h_tileset=NULL;
            Donner_h_tileset=TTF_RenderText_Shaded(ttf_texte,"Donner le nombre de tiles en hauteur",noir,gris_clair);
            SDL_Surface *Donner_w_tile=NULL;
            Donner_w_tile=TTF_RenderText_Shaded(ttf_texte,"Donner la largeur des tiles",noir,gris_clair);
            SDL_Surface *Donner_h_tile=NULL;
            Donner_h_tile=TTF_RenderText_Shaded(ttf_texte,"Donner la hauteur des tiles",noir,gris_clair);
            SDL_Surface *nom_a_charger=NULL;
            nom_a_charger=TTF_RenderText_Shaded(ttf_texte,"Donner le nom du projet à ouvrir",noir,gris_clair);
            SDL_Surface *tileset=NULL;
            SDL_Surface *largeur_tilemap=NULL;
            largeur_tilemap=TTF_RenderText_Shaded(ttf_texte,"Donner le nombre de tiles en largeur",noir,gris_clair);
            SDL_Surface *hauteur_tilemap=NULL;
            hauteur_tilemap=TTF_RenderText_Shaded(ttf_texte,"Donner le nombre de tiles en hauteur",noir,gris_clair);
            SDL_Surface *Donner_nom_image=NULL;
            Donner_nom_image=TTF_RenderText_Shaded(ttf_texte,"Donner le nom de l'image",noir,gris_clair);
            SDL_Surface *Donner_w_image=NULL;
            Donner_w_image=TTF_RenderText_Shaded(ttf_texte,"Donner la largeur de l'image",noir,gris_clair);
            SDL_Surface *Donner_h_image=NULL;
            Donner_h_image=TTF_RenderText_Shaded(ttf_texte,"Donner la hauteur de l'image",noir,gris_clair);
            SDL_Surface *Donner_nom_dossier=NULL;
            Donner_nom_dossier=TTF_RenderText_Shaded(ttf_texte,"Donner le nom du dossier où enregistrer l'image",noir,gris_clair);
        
            SDL_Rect BARRE_EDITION={fenetre.x+fenetre.w/2-200/2,fenetre.y+fenetre.h/2-20/2,200,20};
            SDL_Rect CHAINE={BARRE_EDITION.x,BARRE_EDITION.y,100,20};
            SDL_Rect NOM_PROJET={BARRE_EDITION.x,BARRE_EDITION.y-BARRE_EDITION.h,Donner_nom_projet->w,Donner_nom_projet->h};
            SDL_Rect BARRE_EDITION2={fenetre.x+fenetre.w/2-200/2,fenetre.y+40+fenetre.h/2-20/2,200,20};
            SDL_Rect CHAINE2={BARRE_EDITION2.x,BARRE_EDITION2.y,100,20};
            SDL_Rect NOM_PROJET2={BARRE_EDITION2.x,BARRE_EDITION2.y-BARRE_EDITION2.h,Donner_w_tileset->w,Donner_w_tileset->h}; // texte
            SDL_Rect BARRE_EDITION3={fenetre.x+fenetre.w/2-200/2,fenetre.y+80+fenetre.h/2-20/2,200,20};
            SDL_Rect CHAINE3={BARRE_EDITION3.x,BARRE_EDITION3.y,100,20};
            SDL_Rect NOM_PROJET3={BARRE_EDITION3.x,BARRE_EDITION3.y-BARRE_EDITION3.h,Donner_h_tileset->w,Donner_h_tileset->h};
            SDL_Rect BARRE_EDITION4={fenetre.x+fenetre.w/2-200/2,fenetre.y+120+fenetre.h/2-20/2,200,20};
            SDL_Rect CHAINE4={BARRE_EDITION4.x,BARRE_EDITION4.y,100,20};
            SDL_Rect NOM_PROJET4={BARRE_EDITION4.x,BARRE_EDITION4.y-BARRE_EDITION4.h,Donner_w_tile->w,Donner_w_tile->h};
            SDL_Rect BARRE_EDITION5={fenetre.x+fenetre.w/2-200/2,fenetre.y+160+fenetre.h/2-20/2,200,20};
            SDL_Rect CHAINE5={BARRE_EDITION5.x,BARRE_EDITION5.y,100,20};
            SDL_Rect NOM_PROJET5={BARRE_EDITION5.x,BARRE_EDITION5.y-BARRE_EDITION5.h,Donner_h_tile->w,Donner_h_tile->h};
            SDL_Rect BARRE={810,505,40,20};
            SDL_Rect VISU_BARRE={0,0,40,20};
        
            SDL_SetColorKey(roue_chromatique,SDL_SRCCOLORKEY,SDL_MapRGB(roue_chromatique->format,255,255,255));
            int cnt=-1;
            int cnt2=-1;
            int cnt3=-1;
            int cnt4=-1;
            int cnt5=-1;
            int champ1_selected=1;
            int champ2_selected=0;
            int champ3_selected=0;
            int champ4_selected=0;
            int champ5_selected=0;
            int cnt_ani=0;
            int nbcolonnes=0;
            int tiles_hauteur=0;
            int nblignes=0;
            int tiles_largeur=0;
            int w_image=0;
            int h_image=0;
            /*unsigned int ** tableau2d = malloc(nbcolonnes*tiles_hauteur*sizeof(int));
            for(int i=0;i<nbcolonnes*tiles_hauteur;i++)
            {
                tableau2d[i] = malloc(nblignes*tiles_largeur*sizeof(int));
            }*/
            /*Image *sprite1;
            sprite1=(int)malloc(10*sizeof(int));
            sprite1[0].x=22;
            sprite1[0].y=10;
            sprite1[0].color=SDL_MapRGB(screen->format,0,255,255);
            printf("\nx:%d",sprite1[0].x);
            printf("\ny:%d",sprite1[0].y);
            printf("\ncolor:%lu",(unsigned long)sprite1[0].color);*/
        
            SDL_Rect ROUE_CHROMATIQUE={screen->w/2-roue_chromatique->w/2,screen->h/2-roue_chromatique->h/2,roue_chromatique->w,roue_chromatique->h};
        
            Image pipette;
        
            //SDL_Surface*rotation=NULL;
            Cercle cercle1;
            SDL_Rect*BOUTON_FERMER=(SDL_Rect*)malloc(nb_window*sizeof(SDL_Rect));
        
            BOUTON_FERMER[0].x=fenetre.x+fenetre.w-bouton_fermer[0]->w/2-3;
            BOUTON_FERMER[0].y=fenetre.y+3;
            BOUTON_FERMER[0].w=bouton_fermer[0]->w;
            BOUTON_FERMER[0].h=bouton_fermer[0]->h;
        
            SDL_Rect*ANIM_FERMER=(SDL_Rect*)malloc(nb_window*sizeof(SDL_Rect));
        
            for(int i=0;i<nb_window;i++)
            {
                ANIM_FERMER[i].x=0;
                ANIM_FERMER[i].y=0;
                ANIM_FERMER[i].w=bouton_fermer[i]->w/2;
                ANIM_FERMER[i].h=bouton_fermer[i]->h;
            }
        
            SDL_Surface *color_window=NULL;
            color_window=SDL_LoadBMP("images/fenetre_couleur.bmp");
            SDL_Rect COLOR={screen->w-color_window->w-1,38,color_window->w,color_window->h};
        
            BOUTON_FERMER[1].x=COLOR.x+COLOR.w-bouton_fermer[1]->w/2-3;
            BOUTON_FERMER[1].y=COLOR.y+3;
            BOUTON_FERMER[1].w=bouton_fermer[1]->w;
            BOUTON_FERMER[1].h=bouton_fermer[1]->h;
        
            pipette.color=SDL_MapRGB(screen->format,255,255,255);
        
            BOUTON_FERMER[2].x=fenetre.x+fenetre.w-bouton_fermer[0]->w/2-3;
            BOUTON_FERMER[2].y=fenetre.y+3;
            BOUTON_FERMER[2].w=bouton_fermer[2]->w;
            BOUTON_FERMER[2].h=bouton_fermer[2]->h;
        
            SDL_Surface*cadre_couleur=SDL_LoadBMP("images/cadre_couleur.bmp");
            SDL_Rect CADRE_COULEUR={COLOR.x+COLOR.w-40,99,cadre_couleur->w,cadre_couleur->h};
        
            int nb_image=0;
        
            load_image *tab_image=malloc(1*sizeof(load_image));
        
            while (!done)
            {
        
                ROUE_CHROMATIQUE.x=COLOR.x+3;
                ROUE_CHROMATIQUE.y=COLOR.y+3;
                cercle1.x=ROUE_CHROMATIQUE.x+ROUE_CHROMATIQUE.w/2;
                cercle1.y=ROUE_CHROMATIQUE.y+ROUE_CHROMATIQUE.h/2;
                cercle1.rayon=ROUE_CHROMATIQUE.w/2;
                SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 200, 200, 200));
                freopen("CON", "w", stdout);
                freopen("CON", "r", stdin);
                freopen("CON", "w", stderr);
                SDL_Event event;
                SDL_PollEvent(&event);
        
                switch (event.type)
                {
                case SDL_QUIT:
                    done = true;
                    break;
        
                case SDL_MOUSEBUTTONDOWN:
                    if (event.button.button == SDL_BUTTON_LEFT)
                        detect1.value = 1;
                        break;
                case SDL_MOUSEBUTTONUP:
                    if (event.button.button == SDL_BUTTON_LEFT)
                        detect1.value = 0;
                        break;
        
                case SDL_MOUSEMOTION:
                    curseur.x=event.motion.x;
                    curseur.y=event.motion.y;
                    break;
        
                }
        
                SDL_Surface *Tableau_de_surface[5]={window,screen,texte,Donner_nom_projet,bouton_fermer[0]};
                SDL_Rect Tableau_de_rect[7]={fenetre,BARRE_EDITION,CHAINE,NOM_PROJET,curseur,BOUTON_FERMER[0],ANIM_FERMER[0]};
                Bouton1(&detect1,key,chaine,Texte,Tableau_de_surface,Tableau_de_rect);
                SDL_Surface *Tableau_de_surface2[15]={window2,screen,texte,texte2,texte3,texte4,Donner_nom_image,Donner_w_image,Donner_h_image,Donner_nom_dossier,color_window,bouton_fermer[1],roue_chromatique,cadre_couleur,barre};
                SDL_Rect Tableau_de_rect2[20]={fenetre2,VISU_BARRE,curseur,BARRE_EDITION,BARRE_EDITION2,BARRE_EDITION3,BARRE_EDITION4,CHAINE,CHAINE2,CHAINE3,CHAINE4,NOM_PROJET,NOM_PROJET2,NOM_PROJET3,NOM_PROJET4,COLOR,BOUTON_FERMER[1],ANIM_FERMER[1],ROUE_CHROMATIQUE,CADRE_COULEUR};
                Bouton2(nb_image,tab_image,&detect1,&pipette,event,&champ1_selected,&champ2_selected,&champ3_selected,&champ4_selected,key,chaine,chaine2,chaine3,chaine4,Texte,cercle1,Tableau_de_surface2,Tableau_de_rect2,&BARRE);
                SDL_Surface *Tableau_de_surface3[14]={window2,screen,texte,texte2,texte3,texte4,texte5,Donner_nom_tileset,Donner_w_tileset,Donner_h_tileset,Donner_w_tile,Donner_h_tile,bouton_fermer[2],barre};
                SDL_Rect Tableau_de_rect3[20]={fenetre2,VISU_BARRE,curseur,BARRE_EDITION,BARRE_EDITION2,BARRE_EDITION3,BARRE_EDITION4,BARRE_EDITION5,CHAINE,CHAINE2,CHAINE3,CHAINE4,CHAINE5,NOM_PROJET,NOM_PROJET2,NOM_PROJET3,NOM_PROJET4,NOM_PROJET5,BOUTON_FERMER[2],ANIM_FERMER[2]};
                Bouton3(&detect1,&champ1_selected,&champ2_selected,&champ3_selected,&champ4_selected,&champ5_selected,key,Texte,chaine,chaine2,chaine3,chaine4,chaine5,&BARRE,Tableau_de_surface3,Tableau_de_rect3);
                if(detect1.check[3]==1)
                {
                    if(cnt<max_length)
                    {
                        if(touches(key,chaine6))
                        {
                            cnt+=1;
                            printf("\n%s",chaine6);
                        }
                    }
                    if(key[SDLK_BACKSPACE])
                    {
                        strcpy(chaine6,"");
                        cnt=0;
                    }
                    SDL_BlitSurface(window,NULL,screen,&fenetre);
                    if(cnt<max_length)
                    {
                        if(touches(key,chaine))
                        {
                            cnt+=1;
                        }
                    }
                    texte=TTF_RenderText_Shaded(Texte,chaine6,noir,blanc);
                    SDL_FillRect(screen,&BARRE_EDITION,SDL_MapRGB(screen->format,255,255,255));
                    SDL_BlitSurface(texte,NULL,screen,&CHAINE);
                    SDL_BlitSurface(nom_a_charger,NULL,screen,&NOM_PROJET);
                    char cmd2[110] = "projects/";
                    char cmd3[110] = "/save.txt";
                    strcat(cmd2,chaine6);
                    strcat(cmd2,cmd3);
                    if(Collision(&curseur,&BOUTON_FERMER[0]))
                    {
                        ANIM_FERMER[0].x=40;
                        if(detect1.value==1)
                        {
                            detect1.check[3]=0;
                            strcpy(chaine6,"");
                            cnt=0;
                        }
                    }
                    else
                    {
                        ANIM_FERMER[0].x=0;
                    }
                    SDL_BlitSurface(bouton_fermer[0],&ANIM_FERMER[0],screen,&BOUTON_FERMER[0]);
                    if(key[SDLK_RETURN])
                    {
                        detect1.check[3]=0;
        
                        /*fichier = fopen(cmd2, "r");
                        if(fichier != NULL)
                        {
                            //printf("\nSuccess");
                            for(i4=0;i4<NB_BLOCS_HAUTEUR;i4++){
                                for(j4=0;j4<NB_BLOCS_LARGEUR;j4++){
                                    fscanf(fichier,"%d ",&table1[i4][j4]);
                                }
                            }
                        }
                        if(fichier == NULL)
                        {
                            //printf("\nFail");
                        }
                        fclose(fichier);*/
                        strcpy(cmd2,"");
                        strcpy(cmd3,"");
                        strcpy(chaine6,"");
                        cnt=0;
                    }
                }
                if(detect1.check[4]==1)
                {
                    SDL_BlitSurface(window,NULL,screen,&fenetre);
                    if(cnt<max_length)
                    {
                        if(touches(key,chaine))
                        {
                            cnt+=1;
                        }
                    }
                    texte=TTF_RenderText_Shaded(Texte,chaine,noir,blanc);
                    SDL_FillRect(screen,&BARRE_EDITION,SDL_MapRGB(screen->format,255,255,255));
                    SDL_BlitSurface(texte,NULL,screen,&CHAINE);
                    SDL_BlitSurface(largeur_tilemap,NULL,screen,&NOM_PROJET);
                    texte2=TTF_RenderText_Shaded(Texte,chaine2,noir,blanc);
                    SDL_FillRect(screen,&BARRE_EDITION2,SDL_MapRGB(screen->format,255,255,255));
                    SDL_BlitSurface(texte2,NULL,screen,&CHAINE2);
                    SDL_BlitSurface(hauteur_tilemap,NULL,screen,&NOM_PROJET2);
                    char cmd1[255]="cd projects & md ";
                    if(Collision(&curseur,&BOUTON_FERMER[0]))
                    {
                        ANIM_FERMER[0].x=40;
                        if(detect1.value==1)
                        {
                            detect1.check[4]=0;
                            strcpy(cmd1,"");
                            strcpy(chaine,"");
                            cnt=0;
                        }
                    }
                    else
                    {
                        ANIM_FERMER[0].x=0;
                    }
                    SDL_BlitSurface(bouton_fermer[0],&ANIM_FERMER[0],screen,&BOUTON_FERMER[0]);
                    if(key[SDLK_BACKSPACE])
                    {
                        strcpy(chaine,"");
                        cnt=0;
                    }
                    if(key[SDLK_RETURN])
                    {
                        detect1.check[4]=0;
                        strcat(cmd1,chaine);
                        system(cmd1);
                        strcpy(cmd1,"");
                        strcpy(chaine,"");
                        cnt=0;
                    }
                }
                barre_des_taches(screen,curseur,&detect1);
        
                SDL_Flip(screen);
        
            }
            fclose(stdin);
            fclose(stderr);
            fclose(stdout);
        
            TTF_Quit();
            free(detect1.check);
            SDL_FreeSurface(screen);
            SDL_FreeSurface(texte);
            SDL_FreeSurface(texte2);
            SDL_FreeSurface(texte3);
            SDL_FreeSurface(texte4);
            SDL_FreeSurface(texte5);
            SDL_FreeSurface(window);
            SDL_FreeSurface(window2);
            SDL_FreeSurface(roue_chromatique);
            SDL_FreeSurface(barre);
            SDL_FreeSurface(Donner_nom_projet);
            SDL_FreeSurface(Donner_nom_tileset);
            SDL_FreeSurface(Donner_w_tileset);
            SDL_FreeSurface(Donner_h_tileset);
            SDL_FreeSurface(Donner_w_tile);
            SDL_FreeSurface(Donner_h_tile);
            SDL_FreeSurface(nom_a_charger);
            SDL_FreeSurface(largeur_tilemap);
            SDL_FreeSurface(hauteur_tilemap);
            SDL_FreeSurface(Donner_nom_image);
            SDL_FreeSurface(Donner_w_image);
            SDL_FreeSurface(Donner_h_image);
            SDL_FreeSurface(Donner_nom_dossier);
            SDL_FreeSurface(color_window);
            SDL_FreeSurface(cadre_couleur);
            SDL_Quit();
        
            printf("Exited cleanly\n");
            return 0;
        }
        


        La fonction Bouton2 dans laquelle l'appel de la fonction qui nous intéresses a lieu :

        void Champ(int a,int b,int c,int d,int e,int *champ1,int *champ2,int *champ3,int *champ4,SDL_Rect *rect)
        {
            *champ1=a;
            *champ2=b;
            *champ3=c;
            *champ4=d;
            rect->y=e;
        }
        
        void Bouton2(int nb_image,load_image *tab_image,Detect* detect,Image* pipette,SDL_Event event,int *champ1_selected,int *champ2_selected,int *champ3_selected,int *champ4_selected,Uint8 *key,char chaine[max_length],char chaine2[max_length],char chaine3[max_length],char chaine4[max_length],TTF_Font *Texte,Cercle cercle,SDL_Surface* surface[15],SDL_Rect rect[20],SDL_Rect *rect2)
        {
            int cnt_ani=0;
            int cnt=-1;
            int cnt2=-1;
            int cnt3=-1;
            int cnt4=-1;
            Texte=TTF_OpenFont("police.ttf",20);
            SDL_Color noir={0,0,0};
            SDL_Color blanc={255,255,255};
            char char_name_image[max_length]="";
            char char_name_file[max_length]="";
            int w_image=0;
            int h_image=0;
            if(detect->check[1]==1)
            {
                pipette->x=event.motion.x;
                pipette->y=event.motion.y;
                SDL_BlitSurface(surface[0],NULL,surface[1],&rect[0]);
                if(cnt_ani>0 && cnt_ani<=50)
                {
                    rect[1].y=20;
                }
                if(cnt_ani>50 && cnt_ani<=100)
                {
                    rect[1].y=0;
                }
                if(cnt_ani>100)
                {
                    cnt_ani=0;
                }
                cnt_ani++;
                if(Collision(&rect[2],&rect[3])&&detect->value==1)
                {
                    Champ(1,0,0,0,505,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                }
                if(Collision(&rect[2],&rect[4])&&detect->value==1)
                {
                    Champ(0,1,0,0,545,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                }
                if(Collision(&rect[2],&rect[5])&&detect->value==1)
                {
                    Champ(0,0,1,0,585,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                }
                if(Collision(&rect[2],&rect[6])&&detect->value==1)
                {
                    Champ(0,0,0,1,625,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                }
                if(*champ1_selected==1)
                {
                    if(cnt<max_length)
                    {
                        if(touches(key,chaine))
                        {
                            cnt+=1;
                        }
                    }
                    if(key[SDLK_BACKSPACE])
                    {
                        strcpy(chaine,"");
                        cnt=0;
                    }
                }
                if(*champ2_selected==1)
                {
                    if(cnt2<max_length)
                    {
                        if(touches(key,chaine2))
                        {
                            cnt2+=1;
                        }
                    }
                    if(key[SDLK_BACKSPACE])
                    {
                        strcpy(chaine2,"");
                        cnt2=0;
                    }
                }
                if(*champ3_selected==1)
                {
                    if(cnt3<max_length)
                    {
                        if(touches(key,chaine3))
                        {
                            cnt3+=1;
                        }
                    }
                    if(key[SDLK_BACKSPACE])
                    {
                        strcpy(chaine3,"");
                        cnt3=0;
                    }
                }
                if(*champ4_selected==1)
                {
                    if(cnt4<max_length)
                    {
                        if(touches(key,chaine4))
                        {
                            cnt4+=1;
                        }
                    }
                    if(key[SDLK_BACKSPACE])
                    {
                        strcpy(chaine4,"");
                        cnt4=0;
                    }
                }
                surface[2]=TTF_RenderText_Shaded(Texte,chaine,noir,blanc);
                SDL_FillRect(surface[1],&rect[3],SDL_MapRGB(surface[1]->format,255,255,255));
                SDL_BlitSurface(surface[2],NULL,surface[1],&rect[7]);
                SDL_BlitSurface(surface[6],NULL,surface[1],&rect[11]);
        
                surface[3]=TTF_RenderText_Shaded(Texte,chaine2,noir,blanc);
                SDL_FillRect(surface[1],&rect[4],SDL_MapRGB(surface[1]->format,255,255,255));
                SDL_BlitSurface(surface[3],NULL,surface[1],&rect[8]);
                SDL_BlitSurface(surface[7],NULL,surface[1],&rect[12]);
        
                surface[4]=TTF_RenderText_Shaded(Texte,chaine3,noir,blanc);
                SDL_FillRect(surface[1],&rect[5],SDL_MapRGB(surface[1]->format,255,255,255));
                SDL_BlitSurface(surface[4],NULL,surface[1],&rect[9]);
                SDL_BlitSurface(surface[8],NULL,surface[1],&rect[13]);
        
                surface[5]=TTF_RenderText_Shaded(Texte,chaine4,noir,blanc);
                SDL_FillRect(surface[1],&rect[6],SDL_MapRGB(surface[1]->format,255,255,255));
                SDL_BlitSurface(surface[5],NULL,surface[1],&rect[10]);
                SDL_BlitSurface(surface[9],NULL,surface[1],&rect[14]);
        
                SDL_BlitSurface(surface[10],NULL,surface[1],&rect[15]);
                if(Collision(&rect[2],&rect[16]))
                {
                    rect[17].x=40;
                    if(detect->value==1)
                    {
                        detect->check[1]=0;
                        cnt=0;
                    }
                }
                else
                {
                    rect[17].x=0;
                }
                SDL_BlitSurface(surface[11],&rect[17],surface[1],&rect[16]);
                //surface13=rotozoomSurface(surface14,0,0.3,1);
                SDL_BlitSurface(surface[12],NULL,surface[1],&rect[18]);
                //circleRGBA(surface[1],rect25->x+rect25->w/2,rect25->y+rect25->h/2,109,0,0,0,255);
                SDL_BlitSurface(surface[13],NULL,surface[1],&rect[19]);
                if((CollisionCercle(event.motion.x,event.motion.y,&cercle))&&(detect->value==1))
                {
                    pipette->color=getpixel(surface[1],pipette->x,pipette->y);
                    SDL_LockSurface(surface[10]);
                    putpixel(surface[10],2,2,pipette->color);
                    SDL_UnlockSurface(surface[10]);
                    SDL_SaveBMP(surface[10],"test.bmp");
                }
                else
                {
                    //printf("\n%s",Accent("non détecté !"));
                }
        
                for(int i=0;i<8;i++)
                {
                    for(int j=0;j<8;j++)
                    {
                        putpixel(surface[1],rect[15].x+rect[15].w-31-j,101+i,(unsigned long)pipette->color);
                    }
                }
                SDL_BlitSurface(surface[14],&rect[1],surface[1],rect2);
                if(key[SDLK_RETURN])
                {
                    strcpy(char_name_image,"");
                    strcpy(char_name_file,"");
                    strcat(char_name_image,chaine);
                    strcat(char_name_file,chaine4);
                    w_image=atoi(chaine2);
                    h_image=atoi(chaine3);
                    //printf("\nnom_image:%s",char_name_image);
                    //printf("\nw_image:%d",w_image);
                    //printf("\nh_image:%d",h_image);
                    tab_image[nb_image]=create_image(surface[1],w_image,h_image,char_name_image,char_name_file,tab_image[nb_image]);
                    nb_image+=1;
                    tab_image=(load_image*)realloc(tab_image,nb_image*sizeof(load_image));
                    //create_image(surface[1],w_image,h_image,char_name_image,char_name_file);
                    detect->check[1]=0;
                    strcpy(chaine,"");
                    strcpy(chaine2,"");
                    strcpy(chaine3,"");
                    strcpy(chaine4,"");
                    cnt=0;
                    cnt2=0;
                    cnt3=0;
                    cnt4=0;
                    Champ(1,0,0,0,505,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                    key[SDLK_RETURN]=0;
                }
            }
            for(int i=0;i<nb_image;i++)
            {
                //printf("\ntab_image[%d].check : %d",i,tab_image[i].check);
                if(tab_image[i].check==1)
                {
                    //printf("\n%d",tab_image[i].check);
                    //printf("\n%s\n",tab_image[i].path);
                    //printf("\nchemin : %s",tab_image[i].path);
                    charge_image(surface[1],tab_image[i].path);
                    //printf("\nchemin : %s",tab_image[i].path);
                }
            }
            TTF_CloseFont(Texte);
        }

        Et la fonction qui pose problème :

        char charge_image(SDL_Surface*screen,char name[1000])
        {
            SDL_Surface*temp=SDL_LoadBMP(name);
            SDL_Rect TEMP={screen->w/2-temp->w/2,screen->h/2-temp->h/2,temp->w,temp->h};
            SDL_BlitSurface(temp,NULL,screen,&TEMP);
            SDL_FreeSurface(temp);
            printf("\n%s",name);
            return 0;
        }

        EDIT 2 : @Warren79, je penses en effet que tu ais raison, c'est surement le chemin d’accès à l'image qui doit être perdu car l'image elle est bien créée, donc le chemin existe encore avant la fonction qui doit gérer son chargement, puis son affichage.

        -
        Edité par ReunanBeauvois 22 février 2020 à 19:33:06

        • Partager sur Facebook
        • Partager sur Twitter
          22 février 2020 à 18:00:43

          Et si tu postais tout le code ?

          RE: tu as modifié ton post précédent, je ne peux qu'éditer le mien:

          edgarjacobs écrit maintenant:

          Désolé, je ne veux pas parcourir ce code.

          Découpe tout ça en petites fonctions, utilise des tableaux, mais là c'est réellement indigeste (pour moi en tout cas).



          -
          Edité par edgarjacobs 22 février 2020 à 18:26:51

          • Partager sur Facebook
          • Partager sur Twitter

          On écrit "j'ai tort", pas "tord" qui est le verbe "tordre" à la 3ème personne de l'indicatif présent

            22 février 2020 à 18:52:09

            Bonjour,

            Je pense avoir saisi le problème auquel tu fais face. Dans le code ci-dessous:

            void function(int valueA, int *valueB)
            {
                valueA += 2;
                (*valueB) += 2;
            }

            A la sortie de la fonction function() valueA retrouvera sa valeur avant la fonction, tandis que valueB aura sa valeur impactée à la sortie de la fonction (il gagne 2 en plus sur sa valeur d'int).

            Dans le code ci-dessous:

            void firstFunction()
            {
                 Type *ptrA;
                 Type *ptrB;
                secondFunction(ptrA, &ptrB);
            }
            
            void secondFunction(Type *typeAPtr, Type **typeBPtr)
            {
                typeAPtr = malloc(etc...);
                *typeBPtr = malloc(etc...);
            
                //A la fin de cette fonction la nouvelle valeur de typeAPtr
                //est perdue alors que typePtrB gardera sa valeur allouée (qui peut être égale à NULL donc à vérifier 
            }

            Tu as sûrement appris que lors d'un passage d'argument par valeur (même type entre type passé et type en paramètre) la variable copiée pour le contexte de la fonction est détruite à la fin de la fonction appelée et... c'est pareil pour les pointeurs; si ta fonction qui alloue un SDL_Surface , prend un simple pointeur sur SDL_Surface et que tu passe un SDL_Surface à cette fonction alors la valeur si elle change sera détruite à la fin de la fonction. Donc quand tu veux allouer une valeur à un pointeur à l'interieur d'une fonction et que la modification soit impactée au sortir de la fonction, passe ton pointeur par adresse par exemple:

            void allocateMemory(int ***ptr)
            {
               *ptr = malloc(8 * sizeof(int*);
               for( size_t i{0} ; i < 4 ; ++i )
               {
                  (*ptr)[i] = malloc( sizeof(int) );
               }
               //Ici, on alloue une matrice de 4* 8 integers
            }
            
            int main()
            {
                int **ptr;
                allocateMemory(&ptr);
                return 0;
            }

            J'espère que je me suis pas trompé dans le dernier exemple :-°



            -
            Edité par Warren79 22 février 2020 à 18:54:13

            • Partager sur Facebook
            • Partager sur Twitter

            Mon site web de jeux SDL2 entre autres : https://www.ant01.fr

              26 février 2020 à 11:57:09

              C'est surtout un problème d'adresse de l'image à charger, le chemin n'est pas pris en compte et à chaque fois que je tente quelque chose, le programme plante.

              C'est le deuxième paramètre de cette fonction qui me pose problème :

              char charge_image(SDL_Surface*screen,char name[1000])
              {
                  SDL_Surface*temp=SDL_LoadBMP(name);
                  SDL_Rect TEMP={screen->w/2-temp->w/2,screen->h/2-temp->h/2,temp->w,temp->h};
                  SDL_BlitSurface(temp,NULL,screen,&TEMP);
                  SDL_FreeSurface(temp);
                  //printf("\n%s",*name);
                  return 0;
              }

              Ce deuxième paramètre est définit selon la structure suivante :

              typedef struct
              {
                  char path[1000]; <- ce paramètre
                  int check;
              }load_image;

              Fonction qui est utilisée à la fin de cette fonction :

              void Champ(int a,int b,int c,int d,int e,int *champ1,int *champ2,int *champ3,int *champ4,SDL_Rect *rect)
              {
                  *champ1=a;
                  *champ2=b;
                  *champ3=c;
                  *champ4=d;
                  rect->y=e;
              }
              
              void Bouton2(int nb_image,load_image *tab_image,Detect* detect,Image* pipette,SDL_Event event,int *champ1_selected,int *champ2_selected,int *champ3_selected,int *champ4_selected,Uint8 *key,char chaine[max_length],char chaine2[max_length],char chaine3[max_length],char chaine4[max_length],TTF_Font *Texte,Cercle cercle,SDL_Surface* surface[15],SDL_Rect rect[20],SDL_Rect *rect2)
              {
                  int cnt_ani=0;
                  int cnt=-1;
                  int cnt2=-1;
                  int cnt3=-1;
                  int cnt4=-1;
                  Texte=TTF_OpenFont("police.ttf",20);
                  SDL_Color noir={0,0,0};
                  SDL_Color blanc={255,255,255};
                  char char_name_image[max_length]="";
                  char char_name_file[max_length]="";
                  int w_image=0;
                  int h_image=0;
                  if(detect->check[1]==1)
                  {
                      pipette->x=event.motion.x;
                      pipette->y=event.motion.y;
                      SDL_BlitSurface(surface[0],NULL,surface[1],&rect[0]);
                      if(cnt_ani>0 && cnt_ani<=50)
                      {
                          rect[1].y=20;
                      }
                      if(cnt_ani>50 && cnt_ani<=100)
                      {
                          rect[1].y=0;
                      }
                      if(cnt_ani>100)
                      {
                          cnt_ani=0;
                      }
                      cnt_ani++;
                      if(Collision(&rect[2],&rect[3])&&detect->value==1)
                      {
                          Champ(1,0,0,0,505,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                      }
                      if(Collision(&rect[2],&rect[4])&&detect->value==1)
                      {
                          Champ(0,1,0,0,545,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                      }
                      if(Collision(&rect[2],&rect[5])&&detect->value==1)
                      {
                          Champ(0,0,1,0,585,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                      }
                      if(Collision(&rect[2],&rect[6])&&detect->value==1)
                      {
                          Champ(0,0,0,1,625,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                      }
                      if(*champ1_selected==1)
                      {
                          if(cnt<max_length)
                          {
                              if(touches(key,chaine))
                              {
                                  cnt+=1;
                              }
                          }
                          if(key[SDLK_BACKSPACE])
                          {
                              strcpy(chaine,"");
                              cnt=0;
                          }
                      }
                      if(*champ2_selected==1)
                      {
                          if(cnt2<max_length)
                          {
                              if(touches(key,chaine2))
                              {
                                  cnt2+=1;
                              }
                          }
                          if(key[SDLK_BACKSPACE])
                          {
                              strcpy(chaine2,"");
                              cnt2=0;
                          }
                      }
                      if(*champ3_selected==1)
                      {
                          if(cnt3<max_length)
                          {
                              if(touches(key,chaine3))
                              {
                                  cnt3+=1;
                              }
                          }
                          if(key[SDLK_BACKSPACE])
                          {
                              strcpy(chaine3,"");
                              cnt3=0;
                          }
                      }
                      if(*champ4_selected==1)
                      {
                          if(cnt4<max_length)
                          {
                              if(touches(key,chaine4))
                              {
                                  cnt4+=1;
                              }
                          }
                          if(key[SDLK_BACKSPACE])
                          {
                              strcpy(chaine4,"");
                              cnt4=0;
                          }
                      }
                      surface[2]=TTF_RenderText_Shaded(Texte,chaine,noir,blanc);
                      SDL_FillRect(surface[1],&rect[3],SDL_MapRGB(surface[1]->format,255,255,255));
                      SDL_BlitSurface(surface[2],NULL,surface[1],&rect[7]);
                      SDL_BlitSurface(surface[6],NULL,surface[1],&rect[11]);
              
                      surface[3]=TTF_RenderText_Shaded(Texte,chaine2,noir,blanc);
                      SDL_FillRect(surface[1],&rect[4],SDL_MapRGB(surface[1]->format,255,255,255));
                      SDL_BlitSurface(surface[3],NULL,surface[1],&rect[8]);
                      SDL_BlitSurface(surface[7],NULL,surface[1],&rect[12]);
              
                      surface[4]=TTF_RenderText_Shaded(Texte,chaine3,noir,blanc);
                      SDL_FillRect(surface[1],&rect[5],SDL_MapRGB(surface[1]->format,255,255,255));
                      SDL_BlitSurface(surface[4],NULL,surface[1],&rect[9]);
                      SDL_BlitSurface(surface[8],NULL,surface[1],&rect[13]);
              
                      surface[5]=TTF_RenderText_Shaded(Texte,chaine4,noir,blanc);
                      SDL_FillRect(surface[1],&rect[6],SDL_MapRGB(surface[1]->format,255,255,255));
                      SDL_BlitSurface(surface[5],NULL,surface[1],&rect[10]);
                      SDL_BlitSurface(surface[9],NULL,surface[1],&rect[14]);
              
                      SDL_BlitSurface(surface[10],NULL,surface[1],&rect[15]);
                      if(Collision(&rect[2],&rect[16]))
                      {
                          rect[17].x=40;
                          if(detect->value==1)
                          {
                              detect->check[1]=0;
                              cnt=0;
                          }
                      }
                      else
                      {
                          rect[17].x=0;
                      }
                      SDL_BlitSurface(surface[11],&rect[17],surface[1],&rect[16]);
                      //surface13=rotozoomSurface(surface14,0,0.3,1);
                      SDL_BlitSurface(surface[12],NULL,surface[1],&rect[18]);
                      //circleRGBA(surface[1],rect25->x+rect25->w/2,rect25->y+rect25->h/2,109,0,0,0,255);
                      SDL_BlitSurface(surface[13],NULL,surface[1],&rect[19]);
                      if((CollisionCercle(event.motion.x,event.motion.y,&cercle))&&(detect->value==1))
                      {
                          pipette->color=getpixel(surface[1],pipette->x,pipette->y);
                          SDL_LockSurface(surface[10]);
                          putpixel(surface[10],2,2,pipette->color);
                          SDL_UnlockSurface(surface[10]);
                          SDL_SaveBMP(surface[10],"test.bmp");
                      }
                      else
                      {
                          //printf("\n%s",Accent("non détecté !"));
                      }
              
                      for(int i=0;i<8;i++)
                      {
                          for(int j=0;j<8;j++)
                          {
                              putpixel(surface[1],rect[15].x+rect[15].w-31-j,101+i,(unsigned long)pipette->color);
                          }
                      }
                      SDL_BlitSurface(surface[14],&rect[1],surface[1],rect2);
                      if(key[SDLK_RETURN])
                      {
                          strcpy(char_name_image,"");
                          strcpy(char_name_file,"");
                          strcat(char_name_image,chaine);
                          strcat(char_name_file,chaine4);
                          w_image=atoi(chaine2);
                          h_image=atoi(chaine3);
                          //printf("\nnom_image:%s",char_name_image);
                          //printf("\nw_image:%d",w_image);
                          //printf("\nh_image:%d",h_image);
                          tab_image[nb_image]=create_image(surface[1],w_image,h_image,char_name_image,char_name_file,tab_image[nb_image]);
                          nb_image+=1;
                          tab_image=(load_image*)realloc(tab_image,nb_image*sizeof(load_image));
                          //create_image(surface[1],w_image,h_image,char_name_image,char_name_file);
                          detect->check[1]=0;
                          strcpy(chaine,"");
                          strcpy(chaine2,"");
                          strcpy(chaine3,"");
                          strcpy(chaine4,"");
                          cnt=0;
                          cnt2=0;
                          cnt3=0;
                          cnt4=0;
                          Champ(1,0,0,0,505,champ1_selected,champ2_selected,champ3_selected,champ4_selected,rect2);
                          key[SDLK_RETURN]=0;
                      }
                  }
                  for(int i=0;i<nb_image;i++)
                  {
                      //printf("\ntab_image[%d].check : %d",i,tab_image[i].check);
                      if(tab_image[i].check==1)
                      {
                          //printf("\n%d",tab_image[i].check);
                          //printf("\n%s\n",tab_image[i].path);
                          //printf("\nchemin : %s",tab_image[i].path);
                          charge_image(surface[1],tab_image[i].path);
                          //printf("\nchemin : %s",tab_image[i].path);
                      }
                  }
                  TTF_CloseFont(Texte);
              }

              Quoi que je fasse, ça ne marche pas, l'image qui a été créée par cette fonction ne s'affiche pas.

              EDIT : En faisant des tests par rapport à la fonction charge_image, j'ai pu vérifier si le chemin était bien retenu, apparemment oui, donc c'est peut-être la surface ?

              -
              Edité par ReunanBeauvois 26 février 2020 à 12:28:44

              • Partager sur Facebook
              • Partager sur Twitter
                26 février 2020 à 12:51:28

                Bonjour,

                Tu as la macro 'SDL_GetError()' à disposition , que tu peux utiliser avec fprintf avec '%s' (par exemple pour un fichier de log) qui renvoie la dernière erreur, s'il y en a une. Sinon à part ça, tu ne vérifies pas le retour de la fonction SDL_LoadBMP() dans ta fonction 'charge_image()'.

                -
                Edité par Warren79 26 février 2020 à 19:36:56

                • Partager sur Facebook
                • Partager sur Twitter

                Mon site web de jeux SDL2 entre autres : https://www.ant01.fr

                  27 février 2020 à 16:12:29

                  Pour ceux qui se posent la question, je n'ai toujours pas trouvé. J'ai le code façon brute mais j'aimerais bien avoir mon main être clair et pas faire plus de 10000 lignes de codes.

                  Là actuellement je ne sais pas où est l'erreur, si c'est la surface, si c'est le rectangle ou si c'est le chemin tellement que ça me prend la tête.

                  Pour le SDL_LoadBMP, le programme ne plante pas car le chemin est bien retenu et utilisé, puisque cette fonction interrompt le programme si le chemin est faux (ce qui n'est pas le cas ici), c'est la surface qui est responsable de ce bug et je ne sais pas comment réparer ça.

                  Tout les codes ont déjà été donnés, je ne sais pas quoi faire d'autre.

                  • Partager sur Facebook
                  • Partager sur Twitter
                    27 février 2020 à 16:32:01

                    Je suppose que comme tous les "débutants", tu as codé tout le machin sans faire de test en cours de route.

                    On commence par un code simple, qui initalise sdl et l'écran, on véririe si c'est ok.

                    On code ensuite le chargement de l'image, on l'affiche pour vérifier si c'est ok.

                    Etc.

                    ReunanBeauvois a écrit:

                    Tout les codes ont déjà été donnés, je ne sais pas quoi faire d'autre.

                    Ben non.

                    Où se trouve le code qui remplit tab_image[....].path ? Comment est déclaré (et / ou défini) tab_image ? As-tu appelé tes fonctions dans le bon ordre ? (oui, c'est con comme question, mais ça arrive). Tu dis que le programme est interrompu si l'image ne se charge pas, mais cette partie du code est-elle corecte ? (nous, on ne sait pas).

                    -
                    Edité par edgarjacobs 27 février 2020 à 16:38:46

                    • Partager sur Facebook
                    • Partager sur Twitter

                    On écrit "j'ai tort", pas "tord" qui est le verbe "tordre" à la 3ème personne de l'indicatif présent

                    Problème d'affichage d'image avec BlitSurface

                    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                    • Editeur
                    • Markdown