SDL_BlitSurface() not displaying image? - c++

So I'm trying to display a simply image with the SDL library, but when I use the function SDL_BlitSurface() nothing happens, and all I get is a black screen. I should also note that I have the .bmp file, the source, and the executable file all in the same directory.
//SDL Header
#include "SDL/SDL.h"
int main(int argc, char* args[])
{
//Starts SDL
SDL_Init(SDL_INIT_EVERYTHING);
//SDL Surfaces are images that are going to be displayed.
SDL_Surface* Hello = NULL;
SDL_Surface* Screen = NULL;
//Sets the size of the window (Length, Height, Color(bits), Sets the Surface in Software Memory)
Screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
//Loads a .bmp image
Hello = SDL_LoadBMP("Hello.bmp");
//Applies the loaded image to the screen
SDL_BlitSurface(Hello, NULL, Screen, NULL);
//Update Screen
SDL_Flip(Screen);
//Pause
SDL_Delay(2000);
//Deletes the loaded image from memory
SDL_FreeSurface(Hello);
//Quits SDL
SDL_Quit();
return 0;
}

LoadBMP() is crap. Install SDL_image library
sudo apt-get install SDL_image SDL_image_dev
(not sure about the names of the packages. Just use aptitude or synaptic or whatever to find them)
and include it with
#include "SDL_image.h"
You load your image then with
SDL_Surface* Hello = IMG_Load("Hello.bmp");
if (!Hello){
printf("Ooops, something went wrong: %s\n", IMG_GetError());
exit(0);
}
Important: Note that you should always do an error check and print out the error.
if (!Hello) is the same as if (Hello == NULL)

have you tried blitting any other types of images? when I first started SDL I remember having issues with .bmp files. Try a .jpg or .png and get back to me whether your code works or not.

I had similar "Problems"; maybe it's a pre-Version, or version incompatible to your graphic-driver; let's figure out. SWSurface and Flip; as I remember, the Flip-functionality only works with double-buffered HW_Surface.
Screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
Please try Update instead of Flip.
SDL_Update(surface);
And nexttime :)
Hello = SDL_LoadBMP("Hello.bmp");
if(Hello != NULL) {
//Applies the loaded image to the screen
SDL_BlitSurface(Hello, NULL, Screen, NULL);
//Update Screen
...
//Deletes the loaded image from memory
SDL_FreeSurface(Hello);
}
because SDL_FreeSurface(NULL) will crash your programm.

Related

SDL will only draw a black screen when trying to print a picture C++

class Game {
public:
Game(int width, int height) {
gwidth = width;
gheight = height;
}
~Game() {
}
bool initGame() {
bool sucsess = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
sucsess = false;
}
//gTetrisSurface = SDL_LoadBMP("TetrisBg.bmp");
gWindow = SDL_CreateWindow("Petris V1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, gwidth, gheight, SDL_WINDOW_SHOWN);
gScreenSurface = SDL_GetWindowSurface(gWindow);
gMainBG = SDL_LoadBMP("test.bmp");
/**if (gMainBG == NULL) {
return false;
}**/
bool running = true;
//SDL_SetWindowFullscreen(gWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
while (running == true) {
SDL_BlitSurface(gMainBG, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface( gWindow );
}
return sucsess;
}
protected:
int gwidth, gheight;
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gTetrisSurface = NULL;
SDL_Surface* gPongSurface = NULL;
SDL_Surface* gMainBG = NULL;
};
The thought behind this, is that this should be one huge surface, containing 2 other surfaces for 2 other games. Problem is, it wont seem to draw the BMP that i try to draw on it. Because of this my development got kinda stuck. Anyone see a possible sulotion for this problem? Thanks!
EDIT: I made it so it flushes the events, BUT, i did a bit of debugging, the problem seems to be that the img file returns NULL, why? It should be loaded with a bmp... (No error messages at all..)
From the documentation of SDL2:
https://wiki.libsdl.org/SDL_GetWindowSurface
This surface will be invalidated if the window is resized. After resizing a window this function must be called again to return a valid surface.
You may not combine this with 3D or the rendering API on this window.
My advice is to avoid using the window surface directly.
Instead, you should use a renderer and copy your own main surface to your background texture.
The SDL2 migration guide explains clearly the right way to copy surfaces and textures on the screen, especially the paragraph "If your game wants to do both".
The whole problem was that the BMP file was in the wrong folder.
If anyone have the same problem, the picture should either be in the same directory as the src (when debugging) or the solution (if building).
Hope this might help someone in the future, either way, thanks to those who tried coming up with other possibilites.
(Using visual studio)

dyld: Library not loaded...reason image not found?

I'm new to c++ and XCode, I'm using sdl2 to create a window but when i compile it, it crashes giving me a thread.I have included opengl.h , stdio.h and SDL2.h. There are questions about
dlyd:library not loaded but their different.
Error Message:
dyld: Library not loaded: #rpath/SDL2.framework/Versions/A/SDL2 Referenced from:
/Users/shayanrazavi/Library/Developer/Xcode/DerivedData/c++_code-bbdyozxqxxdxosbxuyhcrqobxrkd/Build/Products/Debug/c++
code
Reason: image not found
This is the code I used i couldn't get int main to be inside the code block for some reason but anyway and I got this code from https://wiki.libsdl.org/SDL_CreateWindow.
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully made
if (window == NULL) {
// In the event that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// The window is open: enter program loop (see SDL_PollEvent)
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
I figured out why this was happening I was meant to put the framework in /Library/Frameworks folder before using it in XCode because when you download SDL it gives you a read me file and the read me file says to put it in that folder.
I should try reading all the text in read me files next time I guess. But if I try running this in XCode it will crash for some reason. (Makes sense because it says dyld: Library not loaded and we just put it in /Library/Frameworks)

C++ SDL, SDL_image issues

IDE: Code::blocks
Compiler: MinGW
I have SDL and SDL_image installed correctly (it doesn't give any errors when built).
Everything compiles fine but when I run it, the SDL window comes up, but the image never blits, the window. I use SDL_image so I can load PNG images (or so I'm hoping).
The code:
#include <cstdlib>
#include <iostream>
#include "SDL_image.h"
#include <SDL/SDL.h>
int main ( int argc, char** argv )
{
SDL_Surface* test = NULL;
SDL_Surface* screen = NULL;
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Name the window
SDL_WM_SetCaption( "Test-1", NULL );
//Set up screen
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
//Load image
test = IMG_Load("Test.png");
//Apply image to screen
SDL_BlitSurface( test, NULL, screen, NULL );
//Update Screen
SDL_Flip( screen );
//Pause
SDL_Delay( 2000 );
//Free the loaded image
SDL_FreeSurface( test );
//Quit SDL
SDL_Quit();
return 0;
}
Maybe you could try creating a window and then a surface and then blit the image onto the window through the surface? Then you can try with this function:
SDL_UpdateWindowSurface(*windowinstance*)
More information here: link (although this is loading a *.bmp file, but I'm sure the concept is the same). Also try the above mentioned answer also. Might be just that the image cannot be found...

Why does SDL program not show BMP picture?

I've got following code from Lazy:
#include <iostream>
#include "SDL/SDL.h"
using namespace std;
int main()
{
//Start SDL
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *hello = NULL;
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
hello = SDL_LoadBMP("hello.bmp");
SDL_BlitSurface(hello, NULL, screen, NULL);
SDL_Flip(screen);
SDL_Delay(3000);
SDL_FreeSurface(hello);
//Quit SDL
SDL_Quit();
return 0;
}
From time to time the picture is shown, but most of time it's just a black window (with slim string of this picture). And I've got the BMP file with name "hello.bmp" in the same directory.
PS.I've got ArchLinux.
Before displaying your image you should convert it to a format compatible to the video mode you selected.
Therefore you should implement something like this:
SDL_Surface *imagef;
imagef = SDL_DisplayFormat(image);
Before bliting your BMP and use imagef for all of your operations.

SDL loading my image messed up

I'm attemting to load an image that I exported from flash CS3 it's a very cute face but it loads very weird it loads on a blueish way this is the code for the two files:
//main.cpp
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "test.hpp"
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
// Activamos modo de video
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE | SDL_DOUBLEBUF);
image = IMG_Load("face.bmp");
dest.x = 200;
dest.y = 200;
//Main Loop
while(Abierto)
{
//We Draw
Draw();
//Events
while( SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
Abierto = false;
}
}
// We free the image
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
Now the other one the;
//test.hpp
DL_Surface *image = NULL, *screen = NULL;
SDL_Rect dest;
SDL_Event event;
bool Abierto = true;
float PlaneX = 300, PlaneY = 200;
float velX = 0.1, velY = 0.1;
void Draw()
{
Uint32 color;
// Black Background is created
color = SDL_MapRGB (screen -> format, 0, 0, 0);
SDL_FillRect (screen, NULL, color);
SDL_DisplayFormatAlpha(image);
SDL_BlitSurface(image, NULL, screen, &dest);
// Flip the working image buffer with the screen buffer
SDL_Flip (screen);
}
I need help with this please Im not that experienced on SDL stuff oh and if you want to take a closer look I uplaoded the project here.
Oh my bad I must add the image is 32 pixels with alpha according to flash exporting options
According to docs, SDL_DisplayFormatAlpha returns a new image and keeps the original intact.
So, try in the first part, when you load the image:
SDL_Surface *origImage = IMG_Load("face.bmp");
image = SDL_DisplayFormatAlpha(origImage);
SDL_FreeSurface(origImage)
As there is no need to call SDL_DisplayFormatAlpha each frame.
Then in the second part, just blit image, without calling SDL_DisplayFormatAlpha.
UPDATE
I've just checked your picture, and it looks like it is a weird bmp. I've seen that before: BMP format is such a mess that if you don't keep to the basics chances are that different programs will interpret the data differently.
In your case:
display face.bmp shows correctly.
gthumb face.bmp shows nothing.
eog face.bmp says "bogus header data".
I strongly recommend using PNG files for all your game cartoon-like pictures and JPG for all the photo-like ones.
So run
$ convert face.bmp face.png
And use the PNG file. I'll will work better and you will have a file 20% the size of the original.