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!
Related
I made a short program to test out SDL2, though there are some things I don't understand how they work.
So I have created a window and a surface:
SDL_Window *window = nullptr;
SDL_Surface *windowSurface = nullptr;
Now I have this (the part I don't get):
window = SDL_CreateWindow("Window name", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windowSurface = SDL_GetWindowSurface(window);
So the first line: I use the SDL_createWindow() function to create a window called window I assume. The second line, I got no idea whats going on - explanation?
Finally I have this:
SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
followed by some clean up code to set the pointers back to nullptr and exit the program/destroy windows etc.
The code you have pasted does the following things: Creates a SDL window called "Window name", sets its horizontal and vertical positions to center, sets the window size to 640 x 480 and marks it as shown.
The second line acquires the SDL surface bind to this window.
What this means is: Create Window , actually sets up and openGL window and a GPU texture (the Surface, althou SDL2 has seperate class for Textures), to which it is going to draw. Modifying the surface acquired with GetWindowSurface will modify the pixel on the window you have just created.
Bliting is applying a array of pixel to a target texture, in the meaning : hey i got this image/prerendered frame etc.. and I want to apply it to this surface so i can show it. Blit it.
I hope this is helpful : >
You can find more information for SDL here
Official SDL wiki
LazyFoo
LazyFoo provides a full tutorial and explanations of everything for the old SDL, but a lot of the things are the same in SDL2
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.
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;
}
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;
}
I've tried to follow this tutorial on the basics of displaying an image with SDL. But, when I run the program, it returns a blank screen. The image is in the correct directories, but it doesn't display in the program. Am I doing something wrong? I'd really like SDL to work.
EDIT
Here is my code:
#include <SDL/SDL.h>
using namespace std;
int main(int argc, char *argv[])
{
SDL_Surface *hello;
SDL_Surface *screen;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
hello = SDL_LoadBMP("hello.bmp");
SDL_BlitSurface(hello, NULL, screen, NULL);
SDL_Flip(screen);
SDL_Delay(2000);
SDL_FreeSurface(hello);
SDL_Quit();
return 0;
}
Use SDL_GetError() to find out why SDL_LoadBMP() fails loading your bitmap.
Read this thread too
I thought I said I had fixed this months ago, seems as though I had not. I recompiled it again and it worked, very strange.
Instead of SDL_Flip(screen) try using SDL_UpdateRect(screen,0,0,0,0)
Make sure that hello.bmp is in your current directory, and that it's a readable and valid BMP file.
In the line: "hello = SDL_LoadBMP("hello.bmp");" , try "/hello.bmp" instead of "hello.bmp". I had the same problem but it seems that putting the slash ("/") before the file name helps in find and therefore, render the image, even if it isn't on the same directory as the program.
If you are on Windows the instead of "/hello.bmp", write "c://hello.bmp". The first one was for Mac. (:
EDIT: Forget it, I just realized it actually doesn't work, it worked for me since I had the file on my disk directory.