The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
I pinpointed segmentation fault - What is wrong?
bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
The code is listed below. The intention is to copy some text into a rectangle. I am trying to parallel a tutorial at:
http://www.aaroncox.net/tutorials/2dtutorials/sdl_text.pdf
I traced down the point where the segmentation fault occurs - It is marked by the line of asterisks.

Can anyone see why it is getting segmentation fault?

TIA Bill S.

Code:

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <string.h>


int main( int argc, char* args[] ){

SDL_Init(SDL_INIT_VIDEO);
SDL_DisplayMode vmode;
SDL_GetDisplayMode(0, 0, &vmode);
SDL_Window *win1 = NULL;
win1 = SDL_CreateWindow("", 0, 0, vmode.w, vmode.h, SDL_WINDOW_SHOWN);

SDL_Renderer* ren1 = SDL_CreateRenderer(win1, -1, 0);
SDL_SetRenderDrawColor(ren1, 255, 255, 255, 255);
SDL_RenderClear(ren1); // fill the scene with white

    SDL_Rect rect1;
    rect1.x = 50;
    rect1.y = 50;
    rect1.w = 200;
    rect1.h = 32;

SDL_Surface *surf1 =  NULL;
surf1 = SDL_CreateRGBSurface(0,200,32,32,0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);

SDL_RenderDrawRect(ren1, &rect1);
SDL_SetRenderDrawColor(ren1, 255, 0, 0, 255); // the rect color (solid red)
SDL_RenderFillRect(ren1, &rect1);

TTF_Init();
TTF_Font* font = TTF_OpenFont("ARIAL.TTF", 12);
SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Color backgroundColor = { 0, 0, 255 };
/**************************************    Segmentation Fault next line  **************************************/
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, "This is my text.", foregroundColor, backgroundColor);
SDL_Rect textLocation = { 0, 0, 200, 32 };
SDL_BlitSurface(textSurface, &rect1, surf1, &textLocation);
//SDL_BlitSurface(textSurface, NULL, surf1, &textLocation);
SDL_RenderPresent(ren1); // copy to screen

SDL_Delay(5000);
SDL_FreeSurface(textSurface);
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyWindow(win1);
SDL_DestroyRenderer(ren1);      
SDL_Quit();
  return 0;
}
MrTAToad


Joined: 13 Feb 2014
Posts: 205
Location: Chichester, England
Check to see if font == NULL. If it is, then the required font file isn't loaded (dont forget it wont pick it from the systems pool of fonts - you need to supply the ttf file yourself).
bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
MrTAToad wrote:
Check to see if font == NULL. If it is, then the required font file isn't loaded (dont forget it wont pick it from the systems pool of fonts - you need to supply the ttf file yourself).


Thank you. Yes, that was the problem. Where is the 'the systems pool of fonts'? Is that on my system's PATH? How can something like this be made portable to other computers?
MrTAToad


Joined: 13 Feb 2014
Posts: 205
Location: Chichester, England
Your systems pool of fonts could be anywhere. To make it portable, you need to download the arial font from somewhere and put in either with the programs executable, or for a Mac, in the Resource directory.