Advertisement

cannot convert from 'Uint32' to 'SDL_Color'

Started by December 28, 2004 02:52 PM
4 comments, last by Drew_Benton 20 years, 2 months ago
When I try and do this... SDL_Color white = SDL_MapRGB(screen->format, 255, 255, 255); SDL_Surface *texttest = TTF_RenderText_Solid(font,"Hello World!", white); I get "cannot convert from 'Uint32' to 'SDL_Color'", what can I do about this?
SDL_MapRGB is the wrong function.

(Uint32 SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b))

You want this:

typedef struct{
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 unused;
} SDL_Color;

Just declare one and set the 3 values accordingly.
Advertisement
Quote:

SDL_MapRGB
Name
SDL_MapRGB -- Map a RGB color value to a pixel format.
Synopsis

#include "SDL.h"
Uint32 SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b);


Description
Maps the RGB color value to the specified pixel format and returns the pixel value as a 32-bit int.

If the format has a palette (8-bit) the index of the closest matching color in the palette will be returned.

If the specified pixel format has an alpha component it will be returned as all 1 bits (fully opaque).

Return Value
A pixel value best approximating the given RGB color value for a given pixel format. If the pixel format bpp (color depth) is less than 32-bpp then the unused upper bits of the return value can safely be ignored (e.g., with a 16-bpp format the return value can be assigned to a Uint16, and similarly a Uint8 for an 8-bpp format).



Quote:

SDL_Color
Name
SDL_Color -- Format independent color description
Structure Definition
typedef struct{
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 unused;
} SDL_Color;
Structure Data

r Red intensity
g Green intensity
b Blue intensity
unused Unused


Description
SDL_Color describes a color in a format independent way. You can convert a SDL_Color to a pixel value for a certain pixel format using SDL_MapRGB.



If you want to convert a Uint32 into a SDL_Color, you must mask the values with the correct value to get the color.
SDL_Color translate_color(Uint32 int_color)                                 //Change from an "int color" to an SDL_Color{    #if SDL_BYTEORDER == SDL_BIG_ENDIAN        SDL_Color color={(int_color & 0x00ff0000)/0x10000,(int_color &0x0000ff00)/0x100,(int_color & 0x000000ff),0};    #else        SDL_Color color={(int_color & 0x000000ff),(int_color &0x0000ff00)/0x100,(int_color & 0x00ff0000)/0x10000,0};        #endif    return color;}

I got that off of a user contributed SDL docs page.

Now for your code -

Uint32 tmp = SDL_MapRGB(screen->format, 255, 255, 255);SDL_Color white = translate_color(tmp);SDL_Surface *texttest = TTF_RenderText_Solid(font,"Hello World!", white);


Give that a try.

- Drew
Quote:
Original post by Kylotan
SDL_MapRGB is the wrong function.

(Uint32 SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b))

You want this:

typedef struct{
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 unused;
} SDL_Color;

Just declare one and set the 3 values accordingly.


This will work too - but it was posted when I was typing mine [smile].
Thx all, but I have another problem, how can I convert my integers to one c-style string for the rendering of the text. I'm using VC7 and I found ToChar and ToString but It giving me 'ToChar/String': identifier not found, even with argument-dependent lookup. How can I use these to help me?
Quote:
Original post by Yamian
Thx all, but I have another problem, how can I convert my integers to one c-style string for the rendering of the text. I'm using VC7 and I found ToChar and ToString but It giving me 'ToChar/String': identifier not found, even with argument-dependent lookup. How can I use these to help me?


Here is one way:

char tmp[256];
memset(tmp,0,256);
sprintf("String: %s Integer: %i Float: %f","test",25,3.14f);

This allows you to format a string. Take a look at this page for all the different %characters you can use.

Second way:
char tmp[256];
memset(tmp,0,256);
int var1 = 25;
itoa(var1,tmp,10);

itoa converts an integer into a string, the last param is the base - which we use base 10.

I would suggest using the sprintf! I hope this helps you some.

-Drew

This topic is closed to new replies.

Advertisement