Help! SDL2 SDL_LoadBMP wont display image

So, I’m trying to load another bitmap onto the background image (using SDL_RenderCopy), but it just wont work. This is the code I’ve used, most of it was from the official SDL2 website itself aswell

for some strange reason it just doesn’t show the green square and shows just the white background bitmap that has “Hello World” on it.

I’m trying to load another bitmap using SDL_LoadBMP and it simply just won’t work.

#include <SDL2/SDL.h>
#include <stdio.h>

#define SHAPE_SIZE 16

int main( int argc, char *argv[] )
{
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *bitmapTex = NULL, *bitmapManTex;
SDL_Surface *bitmapSuface = NULL, *bitmapManSurface;

SDL_Rect srcrect;
SDL_Rect dstrect;

srcrect.x = 0;
srcrect.y = 0;
srcrect.w = SHAPE_SIZE;
srcrect.h = SHAPE_SIZE;

dstrect.x = 640/2 - SHAPE_SIZE / 2;
dstrect.x = 480/2 - SHAPE_SIZE / 2;
dstrect.w = SHAPE_SIZE;
dstrect.h = SHAPE_SIZE;


int posX = 100, posY = 100, width = 320, height = 240;

SDL_Init( SDL_INIT_VIDEO );

window = SDL_CreateWindow( "An SDL2 Window", posX, posY, width, height, 0 );

renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

bitmapSuface = SDL_LoadBMP("img/hello.bmp");
bitmapTex = SDL_CreateTextureFromSurface(renderer, bitmapSuface);
SDL_FreeSurface(bitmapSuface);

bitmapSuface = SDL_LoadBMP("img/test.bmp");
bitmapManTex = SDL_CreateTextureFromSurface(renderer, bitmapSuface);
SDL_FreeSurface(bitmapSuface);
	
while (1) 
{
	SDL_Event e;
	if (SDL_PollEvent(&e))
	{
		if (e.type == SDL_QUIT)
			break;
	}
	
//	const Uint8 *state = SDL_GetKeyboardState(NULL);
//	if (state[SDL_SCANCODE_RETURN])
//	{
//		printf("<RETURN> is pressed.\n");
//	}
//	if (state[SDL_SCANCODE_RIGHT] && state[SDL_SCANCODE_UP])
//	{
//		printf("Right and Up Keys Pressed.\n");
//	}

	SDL_RenderClear(renderer);		
	SDL_RenderCopy(renderer, bitmapTex, NULL, NULL);
	SDL_RenderCopy( renderer, bitmapManTex, &srcrect, &dstrect );	
	SDL_RenderPresent(renderer);
	SDL_Delay(500);
}


if (window == NULL)
{
	printf("Could not create window: %s\n", SDL_GetError());
	return 1;
}

SDL_DestroyTexture(bitmapManTex);
SDL_DestroyTexture(bitmapTex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);

SDL_Quit();
return 0;

}

Your code sample works for me.

It scales the hello.bmp image to the whole window. For test.bmp, it takes the top-left 16 by 16 pixels and renders these without scaling at the coordinates 312x232 in the window. Did you intend to do something different?

It’s also possible that one of the functions encountered an error. The code sample does not have any error checking. Did you strip it before posting here? If not, I highly recommend to add it and see if it actually finds the file.

Yeah I made sure if it checks if it found it and it found it, I just wanted the green cube to appear on the window but for some strange reason it doesn’t. Yeah I intended it for the green sprite to appear on the screen, but its not doing so. How would I scale that then?

Change the w and h members of the dstrect struct to something bigger. That directly defines the rectangle size that will be used for the drawing operation. The part of the image that was selected with the srcrect struct will be scaled to that size.

Something bigger like?

Hold on, I just spotted an issue. You don’t set dstrect.y:

dstrect.x = 640/2 - SHAPE_SIZE / 2;
dstrect.x = 480/2 - SHAPE_SIZE / 2;

Oops…man thanks a lot that really helped. Wow I can’t believe I didn’t notice that…