I'm relatively new to c++, so despite my experience in other c languages this may just be a simple mistake. Here's my code:
#include <iostream>
#include <SDL2/SDL.h>
SDL_Window *window;
SDL_Surface *windowSurface;
SDL_Surface *image[3];
SDL_Surface *currentImage;
int close();
SDL_Rect gRect(int, int, int, int); //for method described below
int main(int argc, char *argv[]){
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, SDL_WINDOW_SHOWN);
windowSurface = SDL_GetWindowSurface(window);
image[0] = SDL_LoadBMP("filepath/img.bmp");
image[1] = SDL_LoadBMP("filepath/img2.bmp");
image[2] = SDL_LoadBMP("filepath/img3.bmp");
currentImage = image[0];
bool done = false;
while(!done){
SDL_Rect rec;
rec.x = 100;
rec.y = 100;
rec.h = 100;
rec.w = 100;
SDL_BlitSurface(currentImage, NULL, windowSurface, rec);
SDL_UpdateWindowSurface(window);
}
return close();
}
int close(){
for(int i = 0; i < 3; i++){
SDL_FreeSurface(image[i]);
image[i] = nullptr;
}
currentImage = nullptr;
window = nullptr;
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Upon running this program I receive the error "No matching function for call to "SDL_UpperBlit"". Apparently this has been replaced with SDL_BlitSurface, which I am using in my program.
This error does not appear if I replace "rec" in the function call with "NULL", and my program compiles like normal, with the image filling the screen.
I tried using a function to create an SDL_Rect like so:
SDL_Rect gRect(int x, int y, int w, int h){
SDL_Rect ret;
ret.h = h;
ret.w = w;
ret.x = x;
ret.y = y;
return ret;
}
and changing the SDL_BlitSurface call to
SDL_BlitSurface(currentImage, NULL, windowSurface, gRect(100, 100, 100, 100));
This does not resolve the issue and the same error appears. If I could get any help with this it would be greatly appreciated! Thanks!
(Optional) PS: I also would appreciate some help with putting the "image[0]", "image[1]", and "image[2]" declarations in a for loop. I tried it but had some issues with types and concatenation so if anyone knows how I could do this let me know.
SDL_BlitSurface function declaration is
int SDL_BlitSurface(SDL_Surface* src,
const SDL_Rect* srcrect,
SDL_Surface* dst,
SDL_Rect* dstrect)
Your code fragment
SDL_Rect rec;
rec.x = 100;
rec.y = 100;
rec.h = 100;
rec.w = 100;
SDL_BlitSurface(currentImage, NULL, windowSurface, rec);
passes rec
as last argument, which have type SDL_Rect
, but SDL_Rect*
is expected. It should be e.g. SDL_BlitSurface(currentImage, NULL, windowSurface, &rec);
I would also advice against using close
as function name as it is known to cause problems (not as much true for C++ as it have name mangling, but still).