Can't load bitmap, ALLEGRO C++ CODEBLOCKS - c++

My problem is I can't load bitmap to allegro after compiling windows stops working. I tried different bitmaps with different color depths but it still doesn't work.
#include <allegro.h>
int main(int argc, char *argv[])
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0,0);
set_window_title("The Game");
BITMAP *bmp = create_bitmap(800,600);
clear_bitmap(bmp);
BITMAP *BG = load_bitmap("tlotlo.bmp",NULL);
while(!key[KEY_ESC])
{
blit(bmp, screen, 0,0,0,0, bmp->w, bmp->h);
blit(BG, bmp, 0,0,0,0, BG->w, BG->h);
}
destroy_bitmap(bmp);
destroy_bitmap(BG);
return 0;
}
END_OF_MAIN();

Use set_color_depth(desktop_color_depth())
You aren't checking return codes. If create_bitmap or load_bitmap fail, they will return NULL and you'll need to deal with that accordingly.
Loading a bitmap will fail if you aren't in the proper directory. You can test that out by using a full path to the bitmap. If a full path fixes the problem, then you should reconfigure your IDE to work properly.
It's possible that the BMP file is not supported by Allegro. Allegro 4's BMP loading code cannot load more "modern" versions of the format. If you're just getting started, you should really use Allegro 5 instead.

Related

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

Allegro 5 al_create_display(x, y) not working

I have set up my allegro 5.0.7 project in MSVC 2010 properly and the code executes. I am able to compile and run programs that will display an error dialog or something. However, whenever I run a program that draws a window, the window is not shown on my screen. I see it minimized with a broken file icon. The code runs with no errors, however. Here is an example of some code that gives me this problem. Thanks!
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
This code even exits after 10 seconds, as it should. The only problem is that the window is not drawn to the screen. It is only minimized, with a broken file icon. I have Windows 7 64-bit.
This is a known bug affecting certain configurations that may be fixed in a more recent version.
Use al_set_window_position() to move the window onscreen.

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.

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.