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;
}