Problem with 'Sint16 {aka short int}'?

Hello, I have downloaded from the internet, source alternative tibia client and I have many problem. I am a novice programmer in C++, help me if you can,
This is one of them:

C:\YATC\ui\..\enginesdl.h|52|warning: narrowing conversion of 'left' from 'int' to 'Sint16 {aka short int}' inside { } is ill-formed in C++11 [-Wnarrowing]|

1
2
3
4
void setClipping(int left, int top, int width, int height) {
     SDL_Rect r = {left, top, width, height};
     SDL_SetClipRect(m_screen, &r);
}


What's going on and how to fix it?
To compile i use Code Blocks.
If someone wants source to the client, it is here https://github.com/opentibia/yatc
The members of SDL_Rect are of type Sint16 and Uint16 and you are trying to initialize them with int values. This was perfectly fine before C++11 but unfortunately this is no longer valid. It's a narrowing conversion because the types Sint16 and Uint16 are smaller than int.

To make it work you could change the parameter types of setClipping, or you could cast the values to the correct types.
1
2
3
4
SDL_Rect r = {static_cast<Sint16>(left), 
              static_cast<Sint16>(top), 
              static_cast<Uint16>(width), 
              static_cast<Uint16>(height)};

Or you could assign each member individiually
1
2
3
4
5
SDL_Rect r;
r.x = left;
r.y = top;
r.w = width;
r.h = height;

Another possibility is to create a function that helps you create and initialize SDL_Rect objects.
1
2
3
4
5
6
inline SDL_Rect createRect(Sint16 x, Sint16 y, Uint16 w, Uint16 h)
{
	return SDL_Rect{x, y, w, h};
}

SDL_Rect r = createRect(left, top, width, height);
Last edited on
Note that the compiler is just giving you a warning so it will still be able to compile the code. If you just want to build and use the program without touching the code you can safely ignore this warning.
Thanks for fast reply.
Now I have somethink like this:
1
2
3
4
5
6
7
        void setClipping(int left, int top, int width, int height) {
            SDL_Rect r = {static_cast<Sint16>(left),
              static_cast<Sint16>(top),
              static_cast<Uint16>(width),
              static_cast<Uint16>(height)};
            SDL_SetClipRect(m_screen, &r);
        }


These are another errors:
 
C:\YATC\console.cpp|589|warning: no return statement in function returning non-void [-Wreturn-type]|

Code:
1
2
3
4
5
6
7
8
9
int Console::getHeight()
{
    // TODO (nfries88): use this to adjust the starting height for paintConsole and getConsoleEntryAt;
        //
    int height = 0;
    for (std::vector<ConsoleEntry>::reverse_iterator it=m_content.rbegin(); it!=m_content.rend(); it++) {
		height += (*it).getHeight();
	}
}

 
C:\YATC\creatureui.cpp|112|warning: unused variable 'animationTime' [-Wunused-variable]|

Code:
1
2
3
4
5
6
7
8
	    else if(m_obj->idleAnim){
		    // TODO (nfries88): all appearances that animate while idle.
		    //aframes = 0;
            uint32_t animationTime = (g_frameTime - m_startTime)/100;
            aframes = 0;/*(map_x % m_obj->xdiv + (map_y % m_obj->ydiv)*m_obj->xdiv +
					(animationTime % m_obj->animcount)*m_obj->xdiv*m_obj->ydiv)*
					spriteSize;*/
		}



1
2
3
4
5
C:\YATC\enginesdl.cpp|102|warning: narrowing conversion of '(int)x' from 'int' to 'Sint16 {aka short int}' inside { } is ill-formed in C++11 [-Wnarrowing]|
C:\YATC\enginesdl.cpp|102|warning: narrowing conversion of '(int)y' from 'int' to 'Sint16 {aka short int}' inside { } is ill-formed in C++11 [-Wnarrowing]|
C:\YATC\enginesdl.cpp|102|warning: narrowing conversion of '(int)width' from 'int' to 'Uint16 {aka short unsigned int}' inside { } is ill-formed in C++11 [-Wnarrowing]|
C:\YATC\enginesdl.cpp|102|warning: narrowing conversion of '(int)height' from 'int' to 'Uint16 {aka short unsigned int}' inside { } is ill-formed in C++11 [-Wnarrowing]|

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void EngineSDL::drawRectangle(float x, float y, float width, float height, oRGBA color)
{
	static const SDL_VideoInfo* vi = SDL_GetVideoInfo();
	SDL_Rect r={(int)x,(int)y,(int)width,(int)height};
	if(color.a != 255) {
	    // draw with alpha channel
	    // NOTE (nfries88): for some reason this always seems to exceed by 1, making the screen look like a grid when it's dark.
            // so I'm subtracting 1 from width and height.
        boxRGBA(m_screen, x, y, x + width - 1, y + height - 1, (uint8_t)color.r, (uint8_t)color.g, (uint8_t)color.b, (uint8_t)(color.a));
	} else {
	    // draw without alpha channel
	    SDL_FillRect(m_screen, &r, SDL_MapRGBA(vi->vfmt, (uint8_t)color.r, (uint8_t)color.g, (uint8_t)color.b, (uint8_t)color.a));
	}
}

How to fix this errors?
Thank you very much for your help with this.

Edit
If i ignore this warning, that my client will run :/
Last edited on
warning: no return statement in function returning non-void [-Wreturn-type]

The getHeight() function has return type int but you never return any value from inside the function. You probably want to return the variable height at the end of the function.


warning: unused variable 'animationTime' [-Wunused-variable]

This is nothing serious. The compiler is just telling you that the animationTime variable is not needed because it's never used. If you are not going use it you can safely remove this line.

warning: narrowing conversion of '(int)x' from 'int' to 'Sint16 {aka short int}' inside { } is ill-formed in C++11

This is the same problem as in your first post.
Topic archived. No new replies allowed.