خرید بک لینک

Vote count: 1

Im working on a project with my friend and we have run into an issue with surfaces and windows in SDL.

Currently we are able to create a window and display a rectangle on that window and move it around. The next thing we want to do is take a image and display it on a rectangle and then move it around the screen.

We started with taking the SDL_window* and turning it into SDL_surface* though this would take the image and display it on the background of the window.

Is there a way to turn a rectangle we create into a surface and display the image on that rectangle?

I have also tried using textures and it distorts the image when I tried to move it and the whole image doesn’t move with the rectangle.

// this happens in the constructor

temp_image_sur = IMG_Load( image_location.c_str() );

if( temp_image_sur == NULL )
{
    std::cout << "Image could not be loaded" <<:endl exit="" code=""/>

// This is in the actual draw function.

display_surface = SDL_GetWindowSurface( display_window ); 
if(display_surface == NULL ) 
{
    printf(" null im exiting here %sn", SDL_GetError());
    exit(1);
}
image_surface = SDL_ConvertSurface( temp_image_sur, display_surface->format, 0 );

image_size = { this->location.x, this->location.y, this->size.width, this->size.height };

SDL_BlitSurface( image_surface, &image_size, display_surface, &image_size );

This is what we did for our first attempt, and the image was displaying on the base window. I believe I understand why it is displaying on the base window, it is because we are using that window as the surface, though I'm confused how do I make a user defined rectangle the surface?

We did try using SDL_CreateRGBSurface, though nothing is being displayed on the screen when we do this either.

display_surface = SDL_CreateRGBSurface(0, this->size.width, this->size.height, 1, this->color.red, this->color.green, this->color.blue, this->color.alpha);

Thanks guys! Please let me know if there is anymore information you need, this is my first time posting and I tried to put all the info that I could think of.

asked yesterday

1 Answer

Vote count: 0

Create a texture from your image surface by using SDL_CreateTextureFromSurface:

SDL_Texture* image_surface = SDL_CreateTextureFromSurface(renderer, temp_image_sur);

(remember to free it with SDL_DestroyTexture)

then use SDL_RenderCopy to draw it:

SDL_RenderCopy(renderer, image_texture, nullptr, &image_rect);

where image_rect is a SDL_Rect and the destination rectangle you want to draw your image to, for example:

SDL_rect image_rect = {10, 10, 200, 200};

To move your image simply change image_rect.x and/or image_rect.y

answered yesterday

برچسب: نویسنده: استخدام کار تاريخ: شنبه 19 فروردين 1396 ساعت: 3:52

صفحه بندی