Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# x and y in SDL

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

If you intended to correct an error in the post then please contact us.

## Recommended Posts

This all happened in the game programming forum, but I brought it here so maybe somebody will find this interesting.
Message by Tokikenshi Ack! Can someone tell me how to blit bitmaps and stuff with my own coordinates in SDL, cus this''s driving me nuts. My Reply Hi! (I''ve got my own gdnet forum now, so you can ask questons there ) Anyway what do you mean "with your own coordinates?" I''m working on the second tut right now. Maybe this piece of code will help you:
  // screen must be a global global variable void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2) { SDL_Rect dest; dest.x = x; dest.y = y; SDL_Rect dest2; dest2.x = x2; dest2.y = y2; dest2.w = w; dest2.h = h; SDL_BlitSurface(img, &dest2, screen, &dest); } void DrawIMG(SDL_Surface *img, int x, int y) { SDL_Rect dest; dest.x = x; dest.y = y; SDL_BlitSurface(img, NULL, screen, &dest); } // in main() or somewhere ... SDL_Surface *image; // Load a bitmap into image image = SDL_LoadBMP("image.bmp"); // Now to draw the image onto the screen, use DrawIMG(image, x, y); // where x and y are screen coordinates. // You can also use a more advanced function: DrawIMG(image, x, y, w, h, x2, y2); where x and y are the screen coords, w and h are the width and height of the image to draw. If the w and h are less than the image size, then the image is cropped if they are more, then the image is not repeated. x2 and y2 are the x and y of the image to display at the point x and y. For example: DrawIMG(image, 50, 60, 100, 100, 25, 55); 
With this statement we would blit the SDL_Surface *image onto the screen starting at the point 50x60. We would blit 100 pixels right and down. We would start the blitting (at point 50x60) with the pixel of the image at the point (25x55). On point ((50+100)x60) we would then blit the point ((25+100)x55) from the image. Hope this makes sence or cleared something up for you. If not, wait for the tut.
---
cone3d
http://cone3d.gamedev.net
Multitasking - screwing up several things at once.

#### Share this post

##### Share on other sites
Advertisement
Hey, thanks a million!

#### Share this post

##### Share on other sites
you''re welcome

---
cone3d
http://cone3d.gamedev.net
Multitasking - screwing up several things at once.

#### Share this post

##### Share on other sites
Ive noticed a mistake in the tutorials and your code, which is here:

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

The SDL_BlitSurface funtion performs a copy of the regions passed in the 2nd and 4th parameter of the function, from the surface passed in the 1st parameter to the 3rd. The ''dest'' rectangle is modified to reflect any changes in the blit area due to clipping.

You must also note that if you are passing a non-null value, like &dest that all four values must be set. As shown below, the SDL_Rect struct is declared like so:

typedef struct{
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;

It must contain the width and height that you want to copy onto the destination bitmap. These values can be obtained easily through the ''w'' and ''h'' integer fields in the SDL_Surface structure.

Therefore, it would be better to write the function like shown below. This may not always cause a problem but its possible to be the root of obscure problems if for example your operating system initialises local variables in memory areas which have been set to 0. Its better to specify directly like I have shown, rather than to depend on the random data already present in the structures to be larger than the size of your surface.

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = img->w;
dest.h = img->h;
SDL_BlitSurface(img, NULL, screen, &dest);
}

#### Share this post

##### Share on other sites
That may be true, BUT:
from the SDL docs:
(http://sdldoc.csn.ul.ie/sdlblitsurface.php)

SDL_BlitSurface
Name
SDL_BlitSurface -- This performs a fast blit from the source surface to the destination surface.

Synopsis
#include "SDL.h"
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);

Description
This performs a fast blit from the source surface to the destination surface.

The width and height in srcrect determine the size of the copied rectangle. Only the position is used in the dstrect (the width and height are ignored).

If srcrect is NULL, the entire surface is copied. If dstrect is NULL, then the destination position (upper left corner) is (0, 0).

The final blit rectangle is saved in dstrect after all clipping is performed (srcrect is not modified).

The blit function should not be called on a locked surface.

The results of blitting operations vary greatly depending on whether SDL_SRCAPLHA is set or not. See SDL_SetAlpha for an explaination of how this affects your results. Colorkeying and alpha attributes also interact with surface blitting, as the following pseudo-code should hopefully explain.

The SDL Docs CLEARLY state that the w and h of dstrect aren''t required. Even if I would give it them, then they would be IGNORED.

---
cone3d
http://cone3d.gamedev.net
Multitasking - screwing up several things at once.

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement
• ### Popular Tags

• Advertisement

• 10
• 9
• 48
• 12
• 10
• ### Forum Statistics

• Total Topics
631383
• Total Posts
2999685
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!