Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Di Jul 08, 2025 09:16

Foren-Übersicht » Programmierung » Einsteiger-Fragen
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 3 Beiträge ] 
Autor Nachricht
 Betreff des Beitrags: Schriftart mit SDL_TTF laden
BeitragVerfasst: Di Aug 08, 2006 11:43 
Offline
DGL Member
Benutzeravatar

Registriert: Do Sep 02, 2004 19:42
Beiträge: 4158
Programmiersprache: FreePascal, C++
Hi @ll

Entweder ich bin zu blöd oder sonstwas.
Ich versuche ne 2D Anwendung mit SDL und OpenGL zu machen.
Das Problem: Ich will Text einbauen. Also muss ich ja wohl eine ttf-Schriftart laden. Am besten mit der Zeile:
Code:
  1.  
  2. TextFont := TTF_OpenFont(PChar('I:\text.ttf'), 12);
  3.  


Allerdings bekomme ich immer nil zurück. Die TTF-Datei liegt da wo der Pfad hinzeigt. Ich habe auch schon ein / anstatt eines \ verwendet, aber es hilft nix.
Wo kann das Problem liegen?
SDL_TTF ist initialisiert (mit TTF_Init)

THX in Advance

Gruß Lord Horazont

_________________
If you find any deadlinks, please send me a notification – Wenn du tote Links findest, sende mir eine Benachrichtigung.
current projects: ManiacLab; aioxmpp
zombofant networkmy photostream
„Writing code is like writing poetry“ - source unknown


„Give a man a fish, and you feed him for a day. Teach a man to fish and you feed him for a lifetime. “ ~ A Chinese Proverb


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Di Aug 08, 2006 13:06 
Offline
Guitar Hero
Benutzeravatar

Registriert: Do Sep 25, 2003 15:56
Beiträge: 7810
Wohnort: Sachsen - ERZ / C
Programmiersprache: Java (, Pascal)
Ich hab hier nen alten C-Code rumliegen der bei mir funktionierte. Eventuell kannste den mal als Inspiration verwenden.

Code:
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. #include <GL/gl.h>
  7. #include <GL/glu.h>
  8. #include <GL/glext.h>
  9.  
  10. #include <SDL.h>
  11. #include <SDL_ttf.h>
  12.  
  13. #ifdef _WIN32
  14. #undef main
  15. #endif
  16.  
  17. /* adjust these accordingly */
  18. char fontpath[] = "C:\\WINDOWS\\Fonts\\ARIBLK.TTF";
  19. int screenwidth = 640;
  20. int screenheight = 480;
  21.  
  22. /*int round(double x)
  23. {
  24.     return (int)(x + 0.5);
  25. }*/
  26.  
  27. int nextpoweroftwo(int x)
  28. {
  29.     double logbase2 = log(x) / log(2);
  30.     return round(pow(2,ceil(logbase2)));
  31. }
  32.  
  33. char *init_sdl(SDL_Surface** screen)
  34. {
  35.     if(SDL_Init(SDL_INIT_VIDEO))
  36.         return SDL_GetError();
  37.     atexit(SDL_Quit);
  38.    
  39.     *screen = SDL_SetVideoMode(screenwidth, screenheight, 0, SDL_OPENGL);
  40.    
  41.     SDL_WM_SetCaption("C-Junkie's SDLGL text example", 0);
  42.    
  43.     if(TTF_Init())
  44.         return TTF_GetError();
  45.     atexit(TTF_Quit);
  46.    
  47.     return 0;
  48. }
  49.  
  50. void SDL_GL_RenderText(char *text,
  51.                       TTF_Font *font,
  52.                       SDL_Color color,
  53.                       SDL_Rect *location)
  54. {
  55.     SDL_Surface *initial;
  56.     SDL_Surface *intermediary;
  57.     //SDL_Rect rect;
  58.     int w,h;
  59.     GLuint texture;
  60.    
  61.     /* Use SDL_TTF to render our text */
  62.     initial = TTF_RenderText_Solid(font, text, color);
  63.    
  64.     /* Convert the rendered text to a known format */
  65.     w = nextpoweroftwo(initial->w);
  66.     h = nextpoweroftwo(initial->h);
  67.    
  68.     intermediary = SDL_CreateRGBSurface(0, w, h, 32,
  69.             0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  70.  
  71.     SDL_BlitSurface(initial, 0, intermediary, 0);
  72.    
  73.     /* Tell GL about our new texture */
  74.     glGenTextures(1, &texture);
  75.     glBindTexture(GL_TEXTURE_2D, texture);
  76.     glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
  77.             GL_UNSIGNED_BYTE, intermediary->pixels );
  78.    
  79.     /* GL_NEAREST looks horrible, if scaled... */
  80.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  81.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  
  82.  
  83.     /* prepare to render our texture */
  84.     glEnable(GL_TEXTURE_2D);
  85.     glBindTexture(GL_TEXTURE_2D, texture);
  86.     glColor3f(1.0f, 1.0f, 1.0f);
  87.    
  88.     /* Draw a quad at location */
  89.     glBegin(GL_QUADS);
  90.         /* Recall that the origin is in the lower-left corner
  91.            That is why the TexCoords specify different corners
  92.            than the Vertex coors seem to. */
  93.         glTexCoord2f(0.0f, 1.0f);
  94.             glVertex2f(location->x    , location->y);
  95.         glTexCoord2f(1.0f, 1.0f);
  96.             glVertex2f(location->x + w, location->y);
  97.         glTexCoord2f(1.0f, 0.0f);
  98.             glVertex2f(location->x + w, location->y + h);
  99.         glTexCoord2f(0.0f, 0.0f);
  100.             glVertex2f(location->x    , location->y + h);
  101.     glEnd();
  102.    
  103.     /* Bad things happen if we delete the texture before it finishes */
  104.     glFinish();
  105.    
  106.     /* return the deltas in the unused w,h part of the rect */
  107.     location->w = initial->w;
  108.     location->h = initial->h;
  109.    
  110.     /* Clean up */
  111.     SDL_FreeSurface(initial);
  112.     SDL_FreeSurface(intermediary);
  113.     glDeleteTextures(1, &texture);
  114. }
  115.  
  116. void glEnable2D()
  117. {
  118.     int vPort[4];
  119.  
  120.     glGetIntegerv(GL_VIEWPORT, vPort);
  121.  
  122.     glMatrixMode(GL_PROJECTION);
  123.     glPushMatrix();
  124.     glLoadIdentity();
  125.  
  126.     glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
  127.     glMatrixMode(GL_MODELVIEW);
  128.     glPushMatrix();
  129.     glLoadIdentity();
  130. }
  131.  
  132. void glDisable2D()
  133. {
  134.     glMatrixMode(GL_PROJECTION);
  135.     glPopMatrix();  
  136.     glMatrixMode(GL_MODELVIEW);
  137.     glPopMatrix(); 
  138. }
  139.  
  140. void init_gl()
  141. {
  142.     /* Irrelevant stuff for this demo */
  143.     glShadeModel(GL_SMOOTH);
  144.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  145.     glClearDepth(1.0f);
  146.     glDepthFunc(GL_LEQUAL);
  147.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  148.    
  149.     /* Required if you want alpha-blended textures (for our fonts) */
  150.     glBlendFunc(GL_ONE, GL_ONE);
  151.     glEnable(GL_BLEND);
  152.    
  153.     /* Required setup stuff */
  154.     glViewport(0, 0, 800, 600);
  155.     glMatrixMode(GL_PROJECTION);
  156.     glLoadIdentity();
  157.     gluPerspective(45.0f, screenwidth / (float)screenheight, 0.1f, 50.0f);
  158.     glMatrixMode(GL_MODELVIEW);
  159.     glLoadIdentity();  
  160. }
  161.  
  162. int main()
  163. {
  164.     SDL_Surface *screen;
  165.     TTF_Font* font;
  166.     char *err;
  167.     SDL_Color color;
  168.     SDL_Rect position;
  169.     SDL_Event event;
  170.     int done;
  171.    
  172.     /* Do boring initialization */
  173.     if((err = init_sdl(&screen))) {
  174.         printf("Error while initializing: %s", err);
  175.         return 1;
  176.     }
  177.    
  178.     if(!(font = TTF_OpenFont(fontpath, 20))) {
  179.         printf("Error loading font: %s", TTF_GetError());
  180.         return 1;
  181.     }
  182.    
  183.     init_gl();
  184.  
  185.     done = 0;
  186.     while(!done) {
  187.         /* render a fun litte quad */
  188.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  189.         glLoadIdentity();
  190.         glTranslatef(0.0f, 0.0f, -5.0f);
  191.         glDisable(GL_TEXTURE_2D);
  192.        
  193.         glBegin(GL_QUADS);
  194.             glNormal3f(0.0f, 0.0f, 1.0f);
  195.             glColor3f(0.5f, 0.0f, 0.0f); glVertex3f(-1.5f, -1.0f,  1.0f);
  196.             glColor3f(0.0f, 0.5f, 0.0f); glVertex3f( 1.0f, -2.0f,  1.0f);
  197.             glColor3f(0.0f, 0.0f, 0.5f); glVertex3f( 1.5f,  1.0f,  1.0f);
  198.             glColor3f(0.5f, 0.0f, 0.0f); glVertex3f(-2.0f,  1.0f, -1.0f);
  199.         glEnd();
  200.        
  201.         /* Go in HUD-drawing mode */
  202.         glEnable2D();
  203.         glDisable(GL_DEPTH_TEST);
  204.        
  205.         /* Draw some text */
  206.         color.r = 255;
  207.         color.g = 128;
  208.         color.b = 255;
  209.         /** A quick note about position.
  210.          * Enable2D puts the origin in the lower-left corner of the
  211.          * screen, which is different from the normal coordinate
  212.          * space of most 2D api's. position, therefore,
  213.          * gives the X,Y coordinates of the lower-left corner of the
  214.          * rectangle **/
  215.         position.x = screenwidth / 3;
  216.         position.y = screenheight / 2;
  217.         SDL_GL_RenderText("Hello, World!", font, color, &position);
  218.         position.y -= position.h;
  219.         SDL_GL_RenderText("A line right underneath", font, color, &position);
  220.         position.y -= position.h;
  221.         SDL_GL_RenderText("Yay text rendering.", font, color, &position);
  222.  
  223.         /* Come out of HUD mode */
  224.         glEnable(GL_DEPTH_TEST);
  225.         glDisable2D();
  226.        
  227.         /* Show the screen */
  228.         SDL_GL_SwapBuffers( );
  229.        
  230.         /* Wait until you click the X */
  231.    
  232.         SDL_WaitEvent(&event);
  233.         switch(event.type) {
  234.         case SDL_QUIT:
  235.             done = 1;
  236.             break;
  237.         }
  238.     }
  239.    
  240.     /* Clean up (the atexit's take care of the rest) */
  241.     TTF_CloseFont(font);
  242.    
  243.     return 0;
  244. }
  245.  

_________________
Blog: kevin-fleischer.de und fbaingermany.com


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Di Aug 08, 2006 14:51 
Offline
DGL Member
Benutzeravatar

Registriert: Do Sep 02, 2004 19:42
Beiträge: 4158
Programmiersprache: FreePascal, C++
Ich werd bei gelegenheit mal das Wiki korrigieren.
Da steht drin, dass TTF_WasInit = 0 heißt, dass schon initalisiert wurde. Das Gegenteil ist aber der fall. Drauf gekommen bin ich durch TTF_GetError. Damit hab ich den Fehler gefunden.

Jetzt gehts.

Gruß Lord Horazont

_________________
If you find any deadlinks, please send me a notification – Wenn du tote Links findest, sende mir eine Benachrichtigung.
current projects: ManiacLab; aioxmpp
zombofant networkmy photostream
„Writing code is like writing poetry“ - source unknown


„Give a man a fish, and you feed him for a day. Teach a man to fish and you feed him for a lifetime. “ ~ A Chinese Proverb


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 3 Beiträge ] 
Foren-Übersicht » Programmierung » Einsteiger-Fragen


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.009s | 14 Queries | GZIP : On ]