-1

In SDL programming in c++ (I write codes in Ubuntu Linux), for drawing a text on the screen, I made a function and the second argument of it, gets the text. The type of it, is char*. In the main function, what should I send to the above function for the second argument. For example in this code, I get error in compiling: (I want to draw the text (Player1 must play...) on the screen by using the function)

#include<iostream>
#include"SDL/SDL.h" 
#include<SDL/SDL_gfxPrimitives.h>
#include "SDL/SDL_ttf.h"
using namespace std;
void drawText(SDL_Surface* screen,char* strin1 ,int size,int x, int y,int fR, int fG,  int fB,int bR, int bG, int bB)
{
TTF_Font*font = TTF_OpenFont("ARIAL.TTF", size);
SDL_Color foregroundColor = { fR, fG, fB };
SDL_Color backgroundColor = { bR, bG, bB };
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, strin1,foregroundColor, backgroundColor);
SDL_Rect textLocation = { x, y, 0, 0 };
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
SDL_FreeSurface(textSurface);
TTF_CloseFont(font);
}
int main(){
SDL_Init( SDL_INIT_VIDEO);
TTF_Init();
SDL_Surface* screen = SDL_SetVideoMode(1200,800,32,0);
SDL_WM_SetCaption("Ping Pong", 0 );
SDL_Delay(500);
drawText(screen,"Player1 must play with ESCAPE & SPACE Keys and player2 must play with UP & DOWN Keys. . . Have Fun!!!",20,15,550,50,50,100,180,180,180);
return 0;
}

1 Answers1

0

What you're getting isn't an error, it is a warning. Compiler isn't forcing you to fix the code it doesn't like, just hinting that it could be problematic.

In C, it is valid to handle string constants as char*, but because you still aren't allowed to modify this constants this approach considered dangerous and therefore deprecated. As far as I remember, newer C++ standards forbids that and forces using string literals as constants only.

So while code in question may be formally correct (depending on language standard version), it is adviced to change parameter type in your function to const char*.

keltar
  • 17,711
  • 2
  • 37
  • 42