C++ SDL, SDL_image issues - c++

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...

Related

Including SDL.h causes program to terminate immediately after building, and not run, but not give an error

This test program should create a blank window that stays open until you x-it-out. I copied it from SDL's documentation to make sure it is correct. It can be found here.
// Example program:
// Using SDL2 to create an application window
#include "SDL.h"
#include <stdio.h>
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 created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
//game loop, quitGame to quit
bool quitGame = false;
//var for checking events
SDL_Event event;
while(!quitGame) {
//Update particles
//Draw particles
//Check for events
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT)
quitGame = true;
}
}
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
It doesn't create a window and terminates immediately, but gives no errors.
I'm using Eclipse, mingw32, and the latest stable release of SDL2. SDL2's libraries and headers are within a file in my C drive. I am using a 64 bit system. I include the entire folder of SDL2's header files. The only library folder I have linked is the one within the 64 bit part of the SDL2 folder. The libraries I have linked are the ones suggested by HolyBlackCat, (in this order) mingw32, SDL2main, and SDL2. Any help is greatly appreciated. Thanks!

My SDL application not responding

I have a problem with SDL. When i run the following code it is not responding. I have .bmp in same folder as the executable file.Is problem in code or where.....?
#include "SDL/SDL.h"
int main( int argc, char* args[] )
{
SDL_Surface* robot = NULL;
SDL_Surface* screen = NULL;
SDL_Init( SDL_INIT_EVERYTHING );
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
robot = SDL_LoadBMP( "robot.bmp" );
SDL_BlitSurface( robot, NULL, screen, NULL );
SDL_Flip( screen );
SDL_Delay( 12*1000 );
SDL_FreeSurface( robot );
SDL_Quit();
return 0;
}
SDL_Delay( 12*1000 ); will halt the current thread for 12 seconds. The issue is that the thread which is halted is the "video" thread, the one in charge of displaying your image. After 12 secons the window will close and the program will exit.
In order to see the image and exit at will, you need to wait on a specific event of your choice like a keyboard event. See here how to make basic event loop with SDL.

SDL_BlitSurface() not displaying image?

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.

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.

C++ Semi-Transparent Window SDL

I wish to have a semi-transparent SDL background (nothing to do with sub-surfaces or images), such that instead of having a black background it is actually transparent, but the other things I draw are not. My current code is a slightly modified copy of Code::Blocks' SDL project, similar to how various applications have rounded borders or odd shapes besides rectangles.
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
int main ( int argc, char** argv )
{
putenv("SDL_VIDEO_WINDOW_POS");
putenv("SDL_VIDEO_CENTERED=1");
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_NOFRAME);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}
I think what you're trying to do is in fact a shaped window (parts of the window are transparent depending on a mask that you provide). It seems there's no way to do that with SDL 1.2, however there is a SDL_SetWindowShape function just for this in SDL 1.3 for which you can find a pre-release snapshot here but it's not even in beta yet so I suggest waiting until it's officialy released :)
this is a link to a pretty neat article about development of an older application for Mac OS 9, which did not have support for shaped windows, either.
It's actually a neat article in general about software development.
But the idea seems pretty smart, and I wonder if you might be able to get it working here, too. Instead of trying to make a transparent background, they actually take a screen-shot of the computer right where their window is going to go, and then use that screen shot for their background. When the user drags the window around on the screen, they continue to update the background with new screen-shots. I think this might be more complicated than you were hoping for, but it's certainly an interesting idea.