Allegro bitmap commands return black screen - c++

I am a beginner at allegro and c++. I am trying to use the bitmap commands. I used this simple program to test it:
#include <allegro.h>
BITMAP *red;
int main(){
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
red = load_bitmap( "frago.png", NULL);
acquire_screen();
blit(red, screen, 0, 0, 0, 0, 480, 360);
release_screen();
readkey();
destroy_bitmap(red);
return 0;
}
END_OF_MAIN();
The file "frago.png" in question is located on my desktop and is a big red rectangle. The color is supported in color depth 32. I am using Xcode 4 on a Mac. Can someone help me?

Allegro library cannot read .png files by default. You must use some other libraries/addons (libpng, zlib, loadpng). loadpng is bundled with Allegro from version 4.3.10, but you need libpng and zlib installed in your compiler.
You must use register_png_file_type() before load_bitmap().
The loadpng addon of Allegro 4.4 is included in its source code:
https://alleg.svn.sourceforge.net/svnroot/alleg/allegro/branches/4.4/addons/loadpng/
If the PNG is 8bpp image, remember to load its color palette:
PALETTE palette;
BITMAP* red = load_bitmap("frago.png", palette);
select_palette(palette);
blit(red, screen, 0, 0, 0, 0, red->w, red->h);
unselect_palette();
Anyway I think Allegro should convert your image to 32bpp automatically, try using set_color_conversion before load_bitmap() just in case:
set_color_conversion(COLORCONV_TOTAL);
Finally you could try to use load_png() function directly (replace load_bitmap with load_png).

If the program is not running in the same folder as the image, it will not find the image.
For example, if the program is running in c:\temp\MyProgram\, the image should be located in this same folder.
Also, some IDEs allow you to specify the folder that the program will run when running or debugging from the IDE, you can set this path to your desktop or copy the image to the program folder.
Another option is to specify the full image path in the load_bitmap call, but this is the worst solution in my opinion, because the program will only works when the image is exactly in this location.
Also I suggest adding a check for null:
red = load_bitmap("frago.png", NULL);
if(red == NULL)
{
printf("Cannot load frago.png\n");
return 0;
}

Related

Drawing antialiased primitives to bitmap Allegro 5

I'm working on a little project using C++ and Allegro 5, my question is
Is there a way to draw antialiased primitives to a bitmap using Allegro 5?
I mean I'm using this function
void draw_to_gameBuffer(ALLEGRO_BITMAP *&gameBuffer, ALLEGRO_DISPLAY *&display)
{
static float x = 0;
al_set_target_bitmap(gameBuffer);
al_draw_filled_rectangle(0,0, 350, 622, al_map_rgb(130, 80, 120));
al_draw_filled_circle(x, 200, 100, al_map_rgb(12, 138, 129));
al_draw_filled_triangle(0, 0, 100, 0, 50, 100, al_map_rgb(12, 138, 129));
x += 2.7;
if(x > 350 + 100)
x = -250;
al_set_target_backbuffer(display);
}
to draw a cicle and a triangle (testing purposes) over a target bitmap as shown, on the project display options I have
al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 4, ALLEGRO_SUGGEST);
al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);
to enable antialiasing, the problem is that all primitives rendered on the gameBuffer have jaggies but the primitives rendered outside the gameBuffer are perfectly smooth, how can I solve that? Or is there a way to do what I'm trying to do and get smooth primitives drawn on the gameBuffer?
It seems that Allegro 5 has some experimental features that can be enabled defining this macro (before defining any allegro header):
#define ALLEGRO_UNSTABLE
With this macro enabled we are able to create a bitmap to draw anything we want and do whatever we want with the bitmap, yes we can do this without enabling the ALLEGRO_UNSTABLE macro but there's this new procedure called:
al_set_new_bitmap_samples(int numberOfSamplesDesired);
that defines how many samples we want in a newly created bitmap, "the downside" of this implementation is that it's only going to work with OpenGl so... we need to force OpenGl (Windows only) to see this new feature working and, how do we put this together and draw antialiased primitives over bitmaps?
first of all, we need to define the ALLEGRO_UNSTABLE macro
#define ALLEGRO_UNSTABLE
then we need to add some allegro headers (the ones we need)
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
after this we need to define a bitmap
ALLEGRO_BITMAP *buffer = nullptr;
now we enable OpenGl and the flags we want and the display options
al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE | ALLEGRO_OPENGL);
al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 2, ALLEGRO_SUGGEST); //antialias stuff
al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); //antialias stuff
to create our bitmap we first call the experimental procedure then create our bitmap
al_set_new_bitmap_samples(4);
buffer = al_create_bitmap(bufferWidth, bufferHeight);
4 samples as an example (lol), to draw to the bitmap we use a procedure like this
void draw_to_buffer(ALLEGRO_BITMAP *&buffer, ALLEGRO_DISPLAY *&display)
{
al_set_target_bitmap(buffer);
al_clear_to_color(al_map_rgb(0, 0, 0));
//anything you want to draw to the bitmap
al_set_target_backbuffer(display);
}
now we just need to draw our bitmap the way we usually draw bitmaps on screen
al_draw_bitmap(buffer, 0, 0, 0);
and that's pretty much it, if you have issues or questions with this implementation, yo can look at my post in the Allegro forums where I got the help of many lovely allegro users that taught me a lot without knowing.
my post in the Allegro forum

Why do allegro crash when blitting? What does these debug reports mean?

I was programming in c++ using the allegro library in code::blocks. I declared a bitmap, loaded the bitmap and blit the image to test if the bitmap is being drawn into the screen. I compiled this program and the compiler found no errors. When I run the program, the bitmap is not drawn to the screen and allegro crashed. I changed the color depth and made sure that I included things in the code that won't make allegro crash but still no avail. I then run the debug to see if there's something wrong with this program. I get this message:
sigsegv segmentation fault
#0 10004DDD colorconv_blit_24_to_15() (C:\Windows\SysWOW64\alleg42.dll:??)
#1 0028FE3C ?? () (??:??)
#2 1006C3E0 get_uformat() (C:\Windows\SysWOW64\alleg42.dll:??)
What does this mean and how can I fix it? Are there any solution to this?
Here's my code:
$
#include <allegro.h>
using namespace std;
int main()
{
allegro_init();
install_keyboard();
set_color_depth(8);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
BITMAP *link=NULL;
link=load_bitmap("cave.bmp",NULL);
blit(link,screen,0,0,0,0,25,177);
readkey();
destroy_bitmap(link);
allegro_exit();
return 0;
}
END_OF_MAIN()
SOLVED!!!
I figured out why allegro crashed. I used Gimp to edit my bitmap before loading it on allegro in code::blocks. For everyone loading their bitmap using the old allegro library, do not use Gimp. Use any other image editing software like paint.net.
UPDATE:
I experimented my bitmap with gimp and it turns out that when I export my image as bitmap, another pop out came showing me the compatibility option and the advanced option. For everyone that uses the allegro library, ignore the top part. You can use Gimp but when you get that pop out after you click "export," click on the plus sign on the left of the compatibility option and that option will drop down and click that check that says "do not write color space information" and then click export. That should work and load fine on allegro.

Allegro Crashes When Loading Bitmap

I've looked around and their are similar questions but the answers haven't fixed my problem.
So, the problem is - If i try to load/draw bmp's in allegro it crashes, they are in the same directory as my project, they are named correctly, i believe the problem lies within the bmp itself. If i use the bmp's from the tutorial im using they work fine, however if i try to create my own, allegro crashes on startup, is there some sort of preset bmp i can use to create my sprites? I've gone over my code multiple times but if i missed something please point it out :)
My Code:
#include <allegro.h>
int main(int argc, char *argv[])
{
// Startup Stuff
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
// Bitmap Stuff
BITMAP *pic = NULL;
pic = load_bitmap("enemy.bmp", NULL);
blit(pic, screen, 0,0,0,0,200,150);//Draw the whole bitmap to the screen at (0,0)
readkey(); // Wait for Key Press.
destroy_bitmap(pic);
return 0;
}
END_OF_MAIN();
Tutorial Im Reading
Thanks in advance
The problem was the color depth, the bmp's i made were 16 bit, changing them to 24 bit fixed the problem!

When loading a .BMP image using SDL on visual studios 2012, it only shows a frame

I'm trying to make a game on visual studio 2012. I'm using SDL and I've set everything up correctly due to this tutorial: http://sdltutorials.com/sdl-tutorial-basics
I did look for other solutions on google: (1) place the image files in the project file. And, (2) place the images with the .exe program. A window does show but its black with no images. Both of these solutions failed. I'm on the edge of giving up on using visual studio 2012 for anything now. As for the code, I got the sources at the link I posted above. Thanks :)
EDIT:
When I build the project, its a success.
Im also using SDL-devel-1.2.15/SDL version 1.2.15.
My os system is windows 8.1 if that helps.
Here's the code for a short version of this example:
#include "SDL.h"
int main( int argc, char* args[] )
{
//The images
SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Set up screen
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
//Load image
hello = SDL_LoadBMP( "hello.bmp" );
//Apply image to screen
SDL_BlitSurface( hello, NULL, screen, NULL );
//Update Screen
SDL_Flip( screen );
//Pause
SDL_Delay( 2000 );
//Free the loaded image
SDL_FreeSurface( hello );
//Quit SDL
SDL_Quit();
return 0;
}
The image shows. It was the code example from the website I posted. Can anyone find out what's wrong with his code(not the code I posted here. The code examples I shared in the website)?
Can anyone find out what's wrong with his code(not the code I posted here.
The code in tutorial (Win32_Sources.zip) you linked never calls SDL_Flip So even if you DO draw something on screen, you'll never see it.
Another problem is that this dude hasn't read SDL documentation, and attempts to free framebuffer (allocated with SDL_SetVideoMode) with SDL_FreeSurface. According to documentation, you shouldn't do that, because framebuffer is released by SDL_Quit.
(opinion)In addition to that I think his coding style is rather very poor - the has that "let's make a class for everything"(i.e. he loves OOP too much) problem, that some newbies have. Making separate *.cpp for a single function doesn't look like a good idea to me (it is 10..15 lines of code, for ****'s sake), I don't quite agree with his choice of CodeBlocks for project's IDE/build system, the code also kinda reminds me of MFC or Microsoft coding examples (where classes are named CSomething, etc). Overall, the guy looks like another newbie me so I don't think learning from him is a good idea.(/opinion)
I'd recommend to read SDL documentation instead of following tutorials (like this one). SDL is rather simple by itself, and its documentation contains short/simple examples which are normally written in C, which means there will be no OOP madness as in this guy's example.
For a build system or IDE on windows platform I'd suggest to use Visual Studio express (instead of codeblocks), however you should also learn some cross-platform build tool like CMake or qMake (part of Qt 4 or Qt 5). Learning those tools will make your life easier should you ever decide to transition to linux or something similar.
For a realtively good/clean C++ coding style, you should take a look at Qt 4/5 source code, tutorials and examples. Qt will be also useful shall you ever need GUI toolkit. Also consider trying OpenGL. Pure SDL is fine if you want to do MS-Dos style demos or software renderer, but OpenGL can do certain things faster.
You might consider upgrading to SDL2 and using a different set of tutorials like these that don't look too bad: http://twinklebeardev.blogspot.com/p/sdl-20-tutorial-index.html
If not you can use the same GetCurrentDirectory code to figure out what directory it is expecting the file to be in.
Here's a simple SDL2 example that loads and displays a bitmap. You can put breakpoints in the error checking code so you can see what's wrong if it fails. Make sure you're building a console style program so the errors have somewhere to print.
#include <Windows.h>
#include <iostream>
#include <string>
#include "SDL.h"
#pragma comment(lib, "sdl2.lib")
#pragma comment(lib, "sdl2main.lib")
int main(int argc, char* args[])
{
const std::string bmpFilename("test.bmp");
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
if(!window)
{
std::cerr << "Could not create window!\n";
return 0;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(!renderer)
{
std::cerr << "Could not create renderer!\n";
return 0;
}
SDL_Surface* bmp = SDL_LoadBMP(bmpFilename.c_str());
if(!bmp)
{
char workingDirectory[_MAX_PATH];
GetCurrentDirectory(sizeof(workingDirectory), workingDirectory);
std::string fullPath(std::string(workingDirectory) + "\\" + bmpFilename);
std::cerr << "Could not load " << fullPath << "!\n";
return 0;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, bmp);
if(!texture)
{
std::cerr << "Could not create texture!\n";
return 0;
}
SDL_FreeSurface(bmp);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(2000);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

Emscripten call to SDL_Init freezes Browser Textinput

I am currently crosscompiling a Sprite Engine under mingw.
Therefore i have 2 Questions.
The behavior of SDL is Emulated by Emscripten through the WebGL Layer.
i don't even have to link the SDL libraries when compiling with emcc.
Question is:
If i initalize my App Like this:
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) == -1)return -1;
SDL_Surface *screen= SDL_SetVideoMode(640, 480, 24, SDL_SWSURFACE);
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 0, 0));
SDL_Flip(screen);
then i am NOT able to put text into a textfield of the Browser, but i am getting the SDL_Events.
All other Browser Inputs like checkboxes or selectboxes are working.
If initialize my App like this (Emscripten works also without SDL_Init!):
SDL_Surface *screen= SDL_SetVideoMode(640, 480, 24, SDL_HWSURFACE);
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 0, 0));
SDL_Flip(screen);
then i am able to put tet into the browser textfield but i am not getting SDL_Events.
Is there a workaround to get the Browser Text Input Fields and SDL_Events working?
Question
This line of code compiled on my WIN32 System fills the screen blue
SDL_FillRect(screen,NULL, SDL_MapRGB(screen->format, 255, 0, 0));
the same line compiled with Emscripten fills the screen red.
Is there a way to switch the SDL colors in Emscripten or in the SDL headers?
From your native code, add this before the SDL_CreateWindow call:
SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#canvas");
More info here: https://wiki.libsdl.org/SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT
Emscripten, by default, captures all user events to the page. This makes sense for a fullscreen game for example. In your use case, you probably want to modify Emscripten's SDL_Init to not listen to key events, or change its receiveEvent return value.
Had the same problem. For me, doing this fixed it:
Module['doNotCaptureKeyboard'] = true;