text does not appear on screen

hi guys,

I'm having a problem with SDL_ttf I am trying to display text to the screen but to no avail,the ttf font is in the same folder as my project and I know it's in the correct folder as the program crashes when it's not so it's not the folder that's an issue,

TTF_init executes fine so as above it's not a problem with it been in a wrong folder,the name is spelled correctly also so I don't know why the text is appearing on the screen I tried searching online but found no clues as to why this is happening

any suggestions?

thanks

(PS this is just the original version,in the revised version I will be applying RAII so that resources are handled in the constructor and destroyed in the destructor)

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
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>

using namespace std;

const int SCREENHEIGHT = 600;
const int SCREENWIDTH = 600;
bool ballLeft,ballRight,ballUp,ballDown;
bool racketOneUp,racketOneDown;
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *font = NULL;
SDL_Event event;
SDL_Surface *ballSurface;
SDL_Surface *racketSurface;
SDL_Surface *fontSurface = NULL;
SDL_Texture *fontTexture;
SDL_Texture *ball;
SDL_Texture *racket;
SDL_Rect *ballRectDst;
SDL_Rect *ballRectSrc;
SDL_Rect *racketRectDst;
SDL_Rect *racketRectSrc;
SDL_Rect *fontRectDst;
SDL_Color color;


bool init()
{

    if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        return false;
    }
    if(IMG_Init(IMG_INIT_PNG) < 0)
    {
        return false;
    }
    if(TTF_Init() < 0){

        return false;
    }

    window = SDL_CreateWindow("PONG",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREENWIDTH,SCREENHEIGHT,SDL_WINDOW_RESIZABLE);

    if(!window)
    {

        cout << "could not create window" << endl;
        return false;
    }

    renderer = SDL_CreateRenderer(window,-1,0);

    if(!renderer)
    {

        cout << "could not create renderer" << endl;
        return false;
    }

    fontRectDst = new SDL_Rect;
    SDL_SetRenderDrawColor(renderer,255,255,255,255);
    ballSurface = IMG_Load("ball.png");
    racketSurface = IMG_Load("racket.png");
    font = TTF_OpenFont("PTN77F.ttf",100);

    SDL_Color nothercolor = {130, 170, 190};

    fontRectDst->h = 80;
    fontRectDst->w = 90;
    fontRectDst->x = 100;
    fontRectDst->y = 110;




    fontSurface = TTF_RenderText_Solid(font,"hello hello",nothercolor);


    ball = SDL_CreateTextureFromSurface(renderer,ballSurface);
    racket = SDL_CreateTextureFromSurface(renderer,racketSurface);
    ballRectSrc = new SDL_Rect;
    ballRectDst = new SDL_Rect;
    racketRectSrc = new SDL_Rect;
    racketRectDst = new SDL_Rect;

    ballRectDst->h = 80;
    ballRectDst->w = 80;
    ballRectDst->x = SCREENWIDTH / 2;
    ballRectDst->y = SCREENHEIGHT / 2;

    racketRectDst->h = 120;
    racketRectDst->w = 40;
    racketRectDst->x = 5;
    racketRectDst->y = 250;

    ballLeft = false;
    ballRight = true;
    ballUp = false;
    ballDown = false;

    racketOneDown = false;
    racketOneUp = false;

    return true;
}
Last edited on
**resolved


sorry guys I forgot to create a texture from the surface before I rendered it,everything works fine now.

thanks guys
Topic archived. No new replies allowed.