# (SDL)Blit a surface to the screen using floating point variables?

This topic is 2901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The tile pretty much spells it out, but I want to blit an SDL_Surface to the screen with the X and Y values as floating point values rather than integer values. I've been learning SDL all day, but this is the one thing I can't really seem to get. I'm so used to XNA. Anyway, any help is appreciated, I've searched all I can for an answer, I just can't figure it out. Currently I have a load_image() that, go figure, loads an image, but with the extension image library for SDL, so I can render PNG files and etc. As such:

 /**************************** Be sure to include the proper header files in your main source file! *****************************/ SDL_Surface *load_image( std::string filename ) { //The image that is loaded SDL_Surface* loadedImage = NULL; //The optimized image that will be used SDL_Surface* optimizedImage = NULL; //Load the image using SDL_image loadedImage = IMG_Load( filename.c_str() ); //If the image loaded if( loadedImage != NULL ) { //Create an optimized image optimizedImage = SDL_DisplayFormatAlpha( loadedImage ); //Free the old image SDL_FreeSurface( loadedImage ); } //Return the optimized image return optimizedImage; } 

Don't mind the opening comment I have in here, I created a base sort of template for me with some header files to use and reuse for every new SDL project I have, so I don't have to worry about rewriting the same code again and again. Anyway, it's also blitted to the screen using this function:

 /**************************** Be sure to include the proper header files in your main source file! *****************************/ void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination ) { //Rectangle to hold the offsets SDL_Rect offset; //Get offsets offset.x = x; offset.y = y; //Blit the surface SDL_BlitSurface( source, NULL, destination, &offset ); } 

These are the two main parts right now, other than when I initialize SDL I have an 800 x 600 fullscreen window.

Any help is appreciated!

##### Share on other sites
Pixel coordinates are integer values. You can use floating point variables for your logic but when you blit the surface you have to use integers. You can round the floating point values to nearest integer or just chop off the decimal part as it happens when you just assign a floating point number to an integer variable. I'm not sure what's the best way.

##### Share on other sites

Pixel coordinates are integer values. You can use floating point variables for your logic but when you blit the surface you have to use integers. You can round the floating point values to nearest integer or just chop off the decimal part as it happens when you just assign a floating point number to an integer variable. I'm not sure what's the best way.

Ah, well I'm so used to C# XNA that I forgot that you can do that. My bad. Thank you!

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 11
• 9
• 11
• 15