Partage
  • Partager sur Facebook
  • Partager sur Twitter

SDL_TTF et OpenGL

incompatible ?

Sujet résolu
    7 juin 2006 à 19:09:04

    Bonjour a tous,
    j'essay depuis quelque tant de faire marcher sdl_ttf et opengl en meme temps !
    Mon probleme est lorsque j'active SDL_OPENGL dans SDL_SetVideoMode, j'ai cette erreur :
    Fatal signal: Segmentation Fault (SDL Parachute Deployed)


    Il y aura une incompatibilité ???
    Merci de votre aide.
    • Partager sur Facebook
    • Partager sur Twitter
      7 juin 2006 à 19:27:19

      Non absolument pas car dans le futur je vous ferai utiliser SDL_TTF pour générer des textures de texte pour vos applis SDL.

      Je te montre l'init en question :


        SDL_Init(SDL_INIT_VIDEO);
          TTF_Init();
          atexit(stop);

          SDL_WM_SetCaption("SDL GL Application", NULL);
          SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_OPENGL);


      Et il n'y a aucun problème ici.
      • Partager sur Facebook
      • Partager sur Twitter
        7 juin 2006 à 19:34:29

        Tu a raison mais je ne comprend c'est lorsque je fais un SDL_FILLRECT ou un SDL_Flip.

        Mon code :
        #include <stdlib.h>
        #include <stdio.h>
        #include <SDL/SDL.h>
        #include <SDL/SDL_image.h>
        #include <SDL/SDL_ttf.h>


        int main(int argc, char *argv[])
        {
            SDL_Surface *ecran = NULL, *texte = NULL, *fond = NULL;
            SDL_Rect position;
            SDL_Event event;
            TTF_Font *police = NULL;
            SDL_Color couleurNoire = {100, 0, 0};
            int continuer = 1;

                 SDL_Init(SDL_INIT_VIDEO);
            TTF_Init();
            //atexit(stop);

            SDL_WM_SetCaption("SDL GL Application", NULL);
            SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);

            fond = IMG_Load("sky.jpg");

            /* Chargement de la police */
            police = TTF_OpenFont("Beurk.ttf", 65);
            /* Ecriture du texte dans la SDL_Surface "texte" en mode Blended (optimal) */
            texte = TTF_RenderText_Blended(police, "Salut les Zeros !", couleurNoire);
            while (continuer == 1)
            {
                SDL_WaitEvent(&event);
                switch(event.type)
                {
                    case SDL_QUIT:
                        continuer = 0;
                        break;
                }

                SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));

                position.x = 0;
                position.y = 0;
                SDL_BlitSurface(fond, NULL, ecran, &position); /* Blit du fond */

                position.x = 0;
                position.y = 0;
                SDL_BlitSurface(texte, NULL, ecran, &position); /* Blit du texte par-dessus */
                SDL_Flip(ecran);
            }


            TTF_CloseFont(police);
            TTF_Quit();

            SDL_FreeSurface(texte);
            SDL_Quit();


            return EXIT_SUCCESS;
        }


        Si je met en commentaire SDL_Flip et SDL_FillRect, ca marche.
        Ou est mon erreur ?
        • Partager sur Facebook
        • Partager sur Twitter
          7 juin 2006 à 19:40:58

          Oullala pas de blit encore moins de fillrect avec OpenGL.
          SDL n'est là que pour nous donner une fenetre et un gestionnaire d'event, il ne faut pas toucher à la surface qui nous est renvoyée.

          Voici un code exemple, à toi de comprendre et t'en inspirer :

          main.cpp
          #include <SDL/SDL.h>
          #include <GL/gl.h>
          #include <GL/glu.h>
          #include <SDL/SDL_ttf.h>
          #include <cstdlib>
          #include <cmath>

          #include "sdlglutils.h"

          #define FPS 50
          #define LARGEUR_FENETRE 640
          #define HAUTEUR_FENETRE 480

          void DrawGL();

          #define VITESSE_ROTATION_CAMERA 0.01
          #define VITESSE_ROTATION_PYRAMIDE 0.1
          #define VITESSE_ROTATION_CUBE 0.05
          double angle_camera = 0;
          double angle_pyramide = 0;
          double angle_cube = 180;
          double x_cube = 2;
          double hauteur = 3;

          GLuint sol;

          GLuint texte;
          unsigned int width_texte;
          unsigned int height_texte;

          void stop()
          {
              SDL_Quit();
              TTF_Quit();
          }

          int main(int argc, char *argv[])
          {
              SDL_Surface *ecran = NULL;
              SDL_Event event;
              const Uint32 time_per_frame = 1000/FPS;

              Uint32 last_time,current_time,elapsed_time; //for time animation
              Uint32 start_time,stop_time; //for frame limit

              SDL_Init(SDL_INIT_VIDEO);
              TTF_Init();
              atexit(stop);

              SDL_WM_SetCaption("SDL GL Application", NULL);
              SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_OPENGL);


              glEnable(GL_DEPTH_TEST);
              glEnable(GL_TEXTURE_2D);

              sol = loadTexture("tiles_ctf05r.jpg");
              glGenTextures(1, &texte);
              createTextureFromText(texte,"coucou les zéros",&width_texte,&height_texte);

              last_time = SDL_GetTicks();
              for (;;)
              {

                  start_time = SDL_GetTicks();


                  while(SDL_PollEvent(&event))
                  {
                      switch(event.type)
                      {
                          case SDL_QUIT:
                          exit(0);
                          break;
                          case SDL_KEYDOWN:
                          switch (event.key.keysym.sym)
                          {
                              case SDLK_p:
                              takeScreenshot("test.bmp");
                              break;
                          }
                      }
                  }

                  current_time = SDL_GetTicks();
                  elapsed_time = current_time - last_time;
                  last_time = current_time;

                  angle_camera += VITESSE_ROTATION_CAMERA*elapsed_time;
                  hauteur = 2+2*cos(2*angle_camera*M_PI/180);

                  DrawGL();

                  stop_time = SDL_GetTicks();
                  if ((stop_time - last_time) < time_per_frame)
                  {
                     SDL_Delay(time_per_frame - (stop_time - last_time));
                  }

              }

              return 0;
          }



          void DrawGL()
          {

              glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

              glMatrixMode( GL_PROJECTION );
              glLoadIdentity( );
              gluPerspective(70,(double)LARGEUR_FENETRE/HAUTEUR_FENETRE,1,1000);

              glMatrixMode( GL_MODELVIEW );
              glLoadIdentity( );

              gluLookAt(3,4,hauteur,0,0,0,0,0,1);

              glRotated(angle_camera,0,0,1);

              //LE SOL
              glBindTexture(GL_TEXTURE_2D, sol);
              glBegin(GL_QUADS);
              glColor3ub(255,0,0);
              glTexCoord2i(0,0);
              glVertex3i(-10,-10,-1);
              glColor3ub(0,255,0);
              glTexCoord2i(10,0);
              glVertex3i(10,-10,-1);
              glColor3ub(255,255,0);
              glTexCoord2i(10,10);
              glVertex3i(10,10,-1);
              glColor3ub(255,0,255);
              glTexCoord2i(0,10);
              glVertex3i(-10,10,-1);
              glEnd();


              glMatrixMode( GL_PROJECTION );
              glLoadIdentity( );
              gluOrtho2D(0,LARGEUR_FENETRE,0,HAUTEUR_FENETRE);
              glDisable(GL_DEPTH_TEST);

              glMatrixMode( GL_MODELVIEW );
              glLoadIdentity( );

              glEnable(GL_BLEND);
              glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

              glTranslated(LARGEUR_FENETRE/2-width_texte/2,HAUTEUR_FENETRE/2-height_texte/2,0);

              glColor4ub(255,255,255,255);
              glBindTexture(GL_TEXTURE_2D, texte);
              glBegin(GL_QUADS);
              glTexCoord2i(0,0); glVertex2i(0,0);
              glTexCoord2i(1,0); glVertex2i(width_texte,0);
              glTexCoord2i(1,1); glVertex2i(width_texte,height_texte);
              glTexCoord2i(0,1); glVertex2i(0,height_texte);
              glEnd();

              glEnable(GL_DEPTH_TEST);
              glDisable(GL_BLEND);


              glFlush();
              SDL_GL_SwapBuffers();
          }


          Rajout à sdlglutils.h
          void createTextureFromText(GLuint id_texture,const char * text,unsigned int * width,unsigned int * height);


          Rajout à sdlglutils.cpp
          void createTextureFromText(GLuint id_texture,const char * text,unsigned int * width,unsigned int * height)
          {
              SDL_Surface * texte_surface,*gl_surface,*gl_fliped_surface;
              TTF_Font *font;
              Uint32 rmask, gmask, bmask, amask;
              SDL_Color textColor = { 255, 255, 255 };
              font = TTF_OpenFont( "arial.ttf", 30 );
              texte_surface = TTF_RenderText_Solid(font,text,textColor);


          #if SDL_BYTEORDER == SDL_BIG_ENDIAN

              rmask = 0xff000000;
              gmask = 0x00ff0000;
              bmask = 0x0000ff00;
              amask = 0x000000ff;
          #else

              rmask = 0x000000ff;
              gmask = 0x0000ff00;
              bmask = 0x00ff0000;
              amask = 0xff000000;
          #endif

              *width = texte_surface->w;
              *height = texte_surface->h;
              unsigned int tmp_width = next_p2(texte_surface->w);
              unsigned int tmp_height = next_p2(texte_surface->h);

              SDL_Surface * good_text;

              good_text =  SDL_CreateRGBSurface(SDL_SWSURFACE, tmp_width,tmp_height, 32,
                                            rmask,gmask,bmask,amask);
              SDL_BlitSurface(texte_surface,NULL,good_text,NULL);

              *width = good_text->w;
              *height = good_text->h;

              SDL_PixelFormat format = *(good_text->format);
              format.BitsPerPixel = 32;
              format.BytesPerPixel = 4;
              format.Rmask = rmask;
              format.Gmask = gmask;
              format.Bmask = bmask;
              format.Amask = amask;

              gl_surface = SDL_ConvertSurface(good_text,&format,SDL_SWSURFACE);

              gl_fliped_surface = flipSurface(gl_surface);

              glBindTexture(GL_TEXTURE_2D,id_texture);

              glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w,
                               gl_fliped_surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE,
                               gl_fliped_surface->pixels);
              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
              glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);


              SDL_FreeSurface(good_text);
              SDL_FreeSurface(gl_surface);
              SDL_FreeSurface(gl_fliped_surface);
              SDL_FreeSurface(texte_surface);
              TTF_CloseFont( font );
          }


          Ce n'est pas forcément parfait mais ça te permet d'afficher du texte pour l'instant sans avoir plus d'explications.
          • Partager sur Facebook
          • Partager sur Twitter
            7 juin 2006 à 19:42:17

            Super, je vais bien trouver la :p !!!
            Merci beaucoup Kayl :) .
            • Partager sur Facebook
            • Partager sur Twitter

            SDL_TTF et OpenGL

            × 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