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;
}