mysterious crash after load_bitmap from Allegro - c

I am new to Allegro. We have to use it in our study.
I have a problem with my code, which should load a bitmap and print it.
#include <allegro.h>
int main( void )
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
BITMAP *Bild;
if( (Bild=load_bitmap("Spielfeld_Rand.bmp", NULL) ) == NULL )
{
allegro_message( "Error" );
return 1;
}
while( !key[KEY_ESC])
{
draw_sprite(screen, Bild, 0,0);
}
destroy_bitmap(Bild);
return 0;
}
END_OF_MAIN()
The Code chrashes. I do not see any error message, my screen turns black and i can't do anything. I also tried to enter the full path of the picture, but it wont help.
But if i remove the if arount the load_bitmap, the program aborts and return to the sceen.
Can anyone help me with this mysterious crash?
Thanks alot.

set_gfx_mode will change your screen resolution to 640x480 and show a black screen.
The manual says not to use allegro_message in graphics mode. It is probably been called and is locking up the program.
In text mode, allegro_message will put up a dialog box with "Error" in it. The program then won't exit until the ok is selected.
You should also call allegro_exit before exiting or your screen will be left at 640x480 resolution.

Related

Console Screen Hides after implementing a API function from (windows.h)in C

I am new to C language but having experience in higher level language such as java, python.....etc.
I am trying to resize the console window in Windows OS. Using the "SetConsoleWindowInfo and SetConsoleScreenBufferSize" function from "windows.h" header file.
But while I try to run that code the console window hides in the taskbar and nothing is happening. While I try to run the code in vsCode the integrated terminal prints nothing.
Here is my code
void setConsole(short consolePos_X, short consolePos_Y, short consoleWidth, short consoleHeight){
HANDLE wConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT consoleWinSize = {consolePos_X, consolePos_Y, consolePos_X+consoleWidth, consolePos_Y+consoleHeight};
SetConsoleWindowInfo(wConsoleHandle, TRUE, &consoleWinSize);
COORD buffSize = {100, 50};
SetConsoleScreenBufferSize(wConsoleHandle, buffSize);
SetConsoleTitle("Snake Game");
I think the problem is with setting buffersize but I am not sure.
Hope I will get Solution.
Thanks in Advance...!
Change your code as follow:
HANDLE wConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if (wConsoleHandle == INVALID_HANDLE_VALUE) {
fprintf(stderr, "GetStdHandle failed with error %d\n", GetLastError());
return;
}
There should be no error here, unless your application has no console allocated.
And also change as follow:
if (!SetConsoleWindowInfo(wConsoleHandle, TRUE, &consoleWinSize)) {
fprintf(stderr, "SetConsoleWindowInfo failed with error %d\n", GetLastError());
return;
}
It is likely that the error is 87 (Invalid parameter). Check the values, especially the consoleWinSize. If it is, then look at this and this.
Hey I Found The Solution.
The problem is that the "ConsoleScreenBufferSize" should not be less than the "ConsoleWindowSize" (More info here). But I have used a "BufferSize" which is less than the "ConsoleSize". So the problem aries.
Solution for this problem is just to use the "BufferSize" Greater than or equal to the "ConsoleSize".
As follows:
COORD coord = {consoleWidth, consoleHeight};
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), &coord);
And this just works fine.
If you have anydoubts fell free to comment here.
I will try to answer in my free time
Hope this is useful..!

SDL window close because of SDL_Flip with an image surface array

I am in the beginning of a game of brick breaker type and I'm stuck in the SDL_Flip step. My CodeBlocks compiler says nothing and the console doesn't crash, but yet the SDL window shutdown and the console process returned code 3. When I ran the debugger it says:
SDL_Flip()
Display(Bricks=0x28f69c, screen=0x0)
and the Display type error was said located at the line of my SDL_Flip(screen);
Here's a glimpse of my code. My Brick_Coordinates and Brick_Surface struct are already initialize (my coordinates for Brick_Coordinates and NULL for Brick_Surface) by another function before that one:
void Display(BrickStruct Bricks[12][10],SDL_Surface *screen)
{
int i=0,j=0;
for(j=0;j<10;j++)
{
if( (j+1)%2==0 ) // If we are on even lines, display only 11 bricks
{
for(i=0;i<11;i++)
{
Bricks[i][j].Brick_Surface = IMG_Load("BrickTest1.png");
SDL_BlitSurface(Bricks[i][j].Brick_Surface, NULL, screen, &Bricks[i][j].Brick_Coordinates);
SDL_Flip(screen);
}
}
else // If we are on odd lines, display the 12 bricks
{
for(i=0;i<12;i++)
{
}
}
}
}
My Structure looks like this:
typedef struct BrickStruct
{
int type;
SDL_Rect Brick_Coordinates;
SDL_Surface *Brick_Surface;
}BrickStruct;
In my main, my code is like this:
SDL_Surface *screen= NULL;
BrickStruct Bricks[12][10]; // I create my 2D array of struct named Bricks
Display(Bricks,screen);
I've already tested with a fprintf the values of my coordinates initialized. These are good. And apparently my SDL_Blit is working. But The Flip isn't. My screen surface is big enough for all my images (480x540 and my images are 40x20). I was wondering if that problem has to do with an impossibility for Blit to place an image on top of another but the Flip doesn't even work when I try with only one image (without my loops).
Can somebody please have the kindness to indicate me where is located my problem ?
Thanks in advance
There reason was that that you didn't save screen into the global variable.
You probably had a line in your SDL_Initialisation similar to this:
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
This creates a new local variable called screen. Since you wanted to save this into the global one, you should change it to:
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
According to your debugger and your example code your screen structure is null. So your call to SDL_BlitSurface will fail. The reason it probably works for you when you do your Display call inside your Initialize is that you've just initialized your screen and used it right after.
You need to store the surface you are writing to and use it again when you're blitting.
Also, as others have recommended, you should take a look at a tutorial for SDL and perhaps some more C tutorials to reinforce some concepts.

Capture image from webcam and display it - OpenCV - Eclipse - Windows

I'm new to OpenCV and I want to display what my webcam sees with OpenCV. I'm using the C Coding Language.
I've tried with this code:
#include <stdio.h>
#include <cv.h> // Include the OpenCV library
#include <highgui.h> // Include interfaces for video capturing
int main()
{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
CvCapture* capture =cvCreateCameraCapture(-1);
if (!capture){
printf("Error. Cannot capture.");
}
else{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
while (1){
IplImage* frame = cvQueryFrame(capture);
if(!frame){
printf("Error. Cannot get the frame.");
break;
}
cvShowImage("Window",frame);
}
cvReleaseCapture(&capture);
cvDestroyWindow("Window");
}
return 0;
}
My webcam's light turns on, but the result is a completely grey window, with no image.
Can you help me?
You need to add
cvWaitKey(30);
to the end of while-loop.
cvWaitKey(x) / cv::waitKey(x) does two things:
It waits for x milliseconds for a key press. If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns -1.
It handles any windowing events, such as creating windows with cvNamedWindow(), or showing images with cvShowImage().
A common mistake for opencv newcomers is to call cvShowImage() in a loop through video frames, without following up each draw with cvWaitKey(30). In this case, nothing appears on screen, because highgui is never given time to process the draw requests from cvShowImage().
See What does OpenCV's cvWaitKey( ) function do? for more info.

GCC/SDL2 Hangs Indefinitely and Stops Responding

Hi guys Ive been at this issue for a few days now and cant find the answer. After a successful build of SDL2 my projects hang and dont respond. They dont accept input and hang indefinitely unless I close them using the command prompt window. Even the 'X' on the application window doesnt respond. As far as I can tell this issue seems to be related to the window itself as the program can draw to the renderer. Please help.
I am using Windows 7, MinGW32, Eclipse Europa and SDL2
See below for example of the problem....
Internal Builder is used for build
gcc -O0 -g3 -Wall -c -fmessage-length=0 -osrc\CTestProject.o ..\src\CTestProject.c
gcc -oCTestProject.exe src\CTestProject.o -lmingw32 -lSDL2main -lSDL2
Build complete for project CTestProject
Time consumed: 562 ms.
Here is the example program
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
int main(int argc, char* args[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Event event;
SDL_Window* sdlWindow = SDL_CreateWindow("test",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,300,300,SDL_WINDOW_OPENGL);
SDL_Renderer* sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
while(event.type != SDL_QUIT){
SDL_RenderClear(sdlRenderer);
SDL_SetRenderDrawColor(sdlRenderer, 150, 0, 0, 255);
SDL_RenderPresent(sdlRenderer);
}
SDL_DestroyRenderer(sdlRenderer);
SDL_DestroyWindow(sdlWindow);
SDL_Quit();
return EXIT_SUCCESS;
}
while(event.type != SDL_QUIT)
{
SDL_RenderClear(sdlRenderer);
SDL_SetRenderDrawColor(sdlRenderer, 150, 0, 0, 255);
SDL_RenderPresent(sdlRenderer);
}
You are comparing a variable that doesn't change in your while loop. You need to update it in every iteration of loop using SDL_PollEvent(&event) Something like this:
bool quit = false;
SDL_Event event;
// Loop while user hasn't quit
while ( !quit )
{
// Check all new event to see if a SDL_QUIT event has come in...
while (SDL_PollEvent(&event) )
{
// SDL_QUIT event has come in, quit.
if ( event.type == SDL_QUIT )
{
quit = true;
}
}
SDL_RenderClear(sdlRenderer);
SDL_SetRenderDrawColor(sdlRenderer, 150, 0, 0, 255);
SDL_RenderPresent(sdlRenderer);
}
Also I don't think you need SDL2_main anymore. At least I don't use it in my code.
Nor do you need #include <stdio.h> and #include <stdlib.h> in your specific example.
Tutorials
SDL2 is fairly new, so there aren't that much tutorials around. The only ones I know is TwinklebearDev.But in most cases SDL1.3 and SDL2 are quite similar. So in most cases you can use SDL1.3 code with SDL_Texture, SDL_Renderer and SDL_Window.You can have a look here for more information on porting from 1.3 to 2.0. For SDL1.3 I have used LazyFoo's tutorials.

cvCreateFileCapture strange error

i'm trying to create a simple Opencv program in C that creates a file capture from a .avi, and it plays it in a window highlighting faces. I'm running a self-compiled version of Opencv (i already tried the same with a jpeg image and it works).
Building goes well, no errors, no warning, but when i launch it this the console output this:
Unknown parameter encountered: "server role"
Ignoring unknown parameter "server role"
And the program simply stops
Previously it was complaining for a missing /home/#user/.smb/smb.conf file, so i tried installing samba ( even though i've still no idea what does samba have to do in all this )
here is my code:
main(){
printf("Ciao!");
cvNamedWindow("window", CV_WINDOW_AUTOSIZE);
cvWaitKey(0);
printf("ok");
CvCapture* capture = cvCreateFileCapture("monsters.avi");
CvHaarClassifierCascade* cascade = load_object_detector("haarcascade_frontalface_alt.xml");
CvMemStorage* storage = cvCreateMemStorage(0);
//List of the faces
CvSeq* faces;
while (0<10) {
CvArr* image = cvQueryFrame(capture);
double scale = 1;
faces = cvHaarDetectObjects(image,cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(1,1), cvSize(300,300));
int i;
for(i = 0; i < faces->total; i++ )
{
CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i );
cvRectangle( image,
cvPoint(face_rect.x*scale,face_rect.y*scale),
cvPoint((face_rect.x+face_rect.width)*scale,(face_rect.y+face_rect.height)*scale),
CV_RGB(255,0,0) , 3, 8, 0);
}
cvReleaseMemStorage( &storage );
cvShowImage("window", image);
}
cvWaitKey(0);
printf("Ciao!");
}
I thank you for your answer, i switched to C++ for my trials. Now i did this:
int main(){
namedWindow("Video", CV_WINDOW_FREERATIO);
VideoCapture cap("sintel.mp4");
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
for(;;){
Mat frame;
cap>>frame;
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("Video", edges);
//cvWaitKey(0);
}
return(0);
}
Now it succesfully load the video and query a frame, evry time i press a key it obviously query another frame and everything works fine, but if i comment the waitkey() the program simply hangs for a bit and crashes if i try to close the window, i'm starting to think there is a problem with codecs or something like that...
There are so many potential problems in the code, most of them related to not coding defensively.
What is cvWaitKey(0); doing after cvNamedWindow()? It's unecessary, remove it!
What happens if the capture was unsucessful? Code defensively:
CvCapture* capture = cvCreateFileCapture("monsters.avi");
if (!capture)
{
// File not found, handle error and possibly quit the application
}
and you should use this technique for every pointer that you receive from OpenCV, ok?
One of the major problems, is that you allocate memory for CvMemStorage before the loop, but inside the loop you release it, which means that after the first loop iteration there will be no longer a valid CvMemStorage* storage, and that's a HUGE problem.
Either move the allocation procedure to the beginning of the loop, so on every iteration memory is allocated/deallocated, or move the cvReleaseMemStorage( &storage ); call out of the loop.
Now it works fine, i changed cvWaitKey() with this
if(waitKey(30) >= 0) break;
I don't understand exactly why but now everything works as it should :)

Resources