I've been trying to have a bitmap image displayed on screen but I can't figure out why it's not displaying it for the life of me. Here's the code:
#include <iostream>
#include <SDL.h>
bool init(SDL_Window *window, SDL_Surface *surface) {
bool success {true};
if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
std::cout << "Could not initialize video: " << SDL_GetError() << "\n";
success = false;
}
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
if (window == NULL) {
std::cout << "Could not create window: " << SDL_GetError() << "\n";
success = false;
}
surface = SDL_GetWindowSurface(window);
return success;
}
bool loadMedia(SDL_Surface *image) {
bool success {true};
image = SDL_LoadBMP("C:\\Users\\Admin\\Documents\\Files\\Programming\\C++\\game\\Untitled.bmp");
if (image == NULL) {
std::cout << "Could not load image: " << SDL_GetError() << "\n";
success = false;
}
return success;
}
void close(SDL_Window *window, SDL_Surface *surface, SDL_Surface *image) {
SDL_FreeSurface(surface);
surface = nullptr;
SDL_FreeSurface(image);
image = nullptr;
SDL_DestroyWindow(window);
window = nullptr;
SDL_Quit();
}
int main(int argc, char *argv[]) {
bool quit {false};
SDL_Event event;
SDL_Window *window {nullptr};
SDL_Surface *surface {nullptr};
SDL_Surface *image {nullptr};
if (!init(window, surface)) {
std::cout << "Initialization failed\n";
return 1;
}
if (!loadMedia(image)) {
std::cout << "Loading media failed\n";
return 1;
}
SDL_BlitSurface(image, NULL, surface, NULL);
SDL_UpdateWindowSurface(window);
while (!quit) {
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
default: break;
}
}
close(window, surface, image);
return 0;
}
The file structure is like so:
-game/
-image.bmp
-main.cpp
-main.exe
-Makefile
Makefile is like so:
all : main.cpp
g++ main.cpp -IC:\SDL2\include\SDL2 -LC:\SDL2\lib -w -lmingw32 -lSDL2main -lSDL2 -o main
SDL version 2.0.14 64-bit
mingw 64-bit
Windows 10 64-bit
I don't even know how much more information is necessary.
I have tried using events, not using events, using an absolute path, making the variables global, different file formats, trying to display it using a renderer and a texture, putting everything in main, making the image the same size as the screen, using ImageMagick convert
as per the suggestion of an answer on some other thread on here, disabling the console window with the -Wl,-subsystem,windows
compiler flag, but nothing I did has worked. Any help here?
You are passing parameters to init()
- but the function receives a copy of those things. Then you change the copies passed into the function so they point somewhere else - but that doesn't change where the original pointers point. Then when you return to main()
you use the original pointers which are still pointing to null.
It's the same as writing this:
void f(int x) {
x=2; // change the copy of x passed to this function
}
int main() {
int x=1;
f(x);
printf("%d\n",x); // prints the original value: 1
}
When what you should do is pass pointers to the things you want to change and then inside the function change the contents of the parameters:
void f(int *x) {
*x=2; // change the contents of the pointer: the original x
}
int main() {
int x=1;
f(&x); // pass a pointer to x
printf("%d\n",x); // prints the changed value: 2
}
So, in your case, what you need to do is pass pointers to the things you want to change (a pointer to window and a pointer to surface) and then inside init()
change the contents of the parameters:
bool init(SDL_Window **window, SDL_Surface **surface) {
// ...
*window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
// ...
*surface = SDL_GetWindowSurface(window);
// ...
}
int main(int argc, char *argv[]) {
// ...
SDL_Window *window {nullptr};
SDL_Surface *surface {nullptr};
// ...
if (!init(&window, &surface)) {
std::cout << "Initialization failed\n";
return 1;
}
// ...
}