SDL text to screen

I would like to render text to the screen. The renderText function gives me a bad access error. I feel like its conflicting with my render object...any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

#include <iostream>
#include "Window.h"
#include <SDL2_ttf/SDL_ttf.h>


using namespace std;




void gameLoop();
void update();
int main()
{
    gameLoop();
    
    return 0;
}

void gameLoop()
{
 
    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();
    Window win;

    
    SDL_Renderer *render = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
    
    SDL_Surface *paddleSurf;
    
    paddleSurf = SDL_CreateRGBSurface(0, 1, 1, 32, 0, 0, 0, 0);
    SDL_FillRect(paddleSurf, NULL, SDL_MapRGB(paddleSurf->format, 255, 0, 0));
    
    SDL_Texture* paddleTex = SDL_CreateTextureFromSurface(render, paddleSurf);
    
    SDL_Event event;
    
  
    
    SDL_Rect paddle;
    
    paddle.x = 10;
    paddle.y = 400;
    
    //paddle dimensions
    paddle.w = 100;
    paddle.h = 20;
    
    
    
    SDL_Surface *ballSurf = SDL_CreateRGBSurface(0, 1, 1, 32, 0, 0, 0, 0);
    SDL_FillRect(ballSurf, NULL, SDL_MapRGB(ballSurf->format, 0xFF, 0xFF, 0xFF));
    SDL_Texture *ballTex = SDL_CreateTextureFromSurface(render, ballSurf);
    
    SDL_Surface *bgSurf = SDL_CreateRGBSurface(0, 1, 1, 32, 0, 0, 0, 0);
    SDL_FillRect(bgSurf, NULL, SDL_MapRGB(ballSurf->format, 0, 0, 255));
    SDL_Texture *bgTex  = SDL_CreateTextureFromSurface(render, bgSurf);
    
    SDL_Rect ball;
    
    ball.x = 640/2;
    ball.y = 480/2;
    
    //ball dimenions
    ball.w = 10;
    ball.h = 10;
    
    


    double deltaTime = 0.0f;
    double currentTime = 0;
    double lastTime = 0;
    
    
    double ballVel = 300;
    double ballVely = 300;
    double paddleVel = 600;
    bool wallhit = false;
    
    const Uint8 *state = SDL_GetKeyboardState(NULL);
    
    
    
    TTF_Font * font = TTF_OpenFont("/Macintosh_HD/Library/Fonts/Arial.TTF", 12);
    SDL_Color foregroundColor = {255,255,255};
    SDL_Color backgroundColor = {0,0,0};
    
    SDL_Surface *textSurface = TTF_RenderText_Solid(font, "TEXT", foregroundColor);
    while (event.type != SDL_QUIT) {
        SDL_PollEvent(&event);
        
        
        currentTime = SDL_GetTicks();
        deltaTime = (currentTime - lastTime)/1000;
        lastTime = currentTime;
     
        
        ball.x += ballVel * deltaTime;
        ball.y += ballVely * deltaTime;
        
        if (ball.x > 640 || ball.x < 0) {
            ballVel *= -1;
            wallhit = true;
        }
        if (ball.y > 480 || ball.y < 0) {
            ballVely *= -1;
            wallhit = false;
        }
        
        if (state[SDL_SCANCODE_LEFT] && paddle.x > 0) {
            paddle.x-=paddleVel*deltaTime;
        }
        else if(state[SDL_SCANCODE_RIGHT] && paddle.x < 535)
        {
            paddle.x+=paddleVel*deltaTime;
        }
        
        if (state[SDL_SCANCODE_A] && paddle.x > 0) {
            paddle.x-=paddleVel*deltaTime;
        }
        else if(state[SDL_SCANCODE_D] && paddle.x < 535)
        {
            paddle.x+=paddleVel*deltaTime;
        }
        

        if (ball.x > paddle.x && ball.x < paddle.x+100 &&
            ball.y > paddle.y-10 &&  ball.y < paddle.y+10)  {
            ballVely *= -1;
        }
        cout << "  " << endl;
        SDL_SetRenderDrawColor(render, 25, 255, 10, 0);
        SDL_RenderClear(render);
       
        if (wallhit == true) {
            SDL_RenderCopy(render, bgTex, NULL, NULL);
        }
        SDL_RenderCopy(render, ballTex, NULL, &ball);
        SDL_RenderCopy(render, paddleTex, NULL, &paddle);
        

        SDL_RenderPresent(render);

    }
}




Been a long time since I used TTF with SDL but what makes you think it is conflicting?
Always make sure the font exists before you try and use it. (Your path could be wrong).

1
2
3
4
5
 if(!font)
  // Print Error

if(!(textSurface = TTF_RenderText_Solid(font, "TEXT", foregroundColor);
    //cout << TTF_GetError() << endl; 

Make sure TTF_Init() isn't returning -1.

Is it crashing the first time you call TTF_RenderText_Solid? Does it work when you comment that out?
Basically get rid of all of the obvious cases first.
Last edited on
Topic archived. No new replies allowed.