An Error with SDL_CeateWindow appearing when program started - c

The code below initializes an SDL window. Because of bug I can't find SDL_CreateWindow() returns NULL.
int xwin_init(int w, int h)
{
int r;
r = SDL_Init(SDL_INIT_VIDEO);
assert(win == NULL);
win = SDL_CreateWindow("PRG Semester Project",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED, w, h,SDL_WINDOW_SHOWN);
SDL_SetWindowTitle(win, "PRG SEM");
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(icon_32x32_bits,32, 32, 24, 32*3, 0xff, 0xff00,0xff0000, 0x0000);
SDL_SetWindowIcon(win, surface);
SDL_FreeSurface(surface);
return r;
}
The error message looks like this:
prgsem: xwin_sdl.c:58: xwin_init: Assertion win != NULL' failed`

While I can't tell you what your problem is, at the very least it is worth checking for errors after initializing video and creating the window. You can use SDL_GetError to do this. A message about an error reported through the SDL library will be accessible through this function, and you can tell when to call it based on the return codes of other SDL functions such as SDL_Init in your example.

Related

SDL_GetError() returning an undocumented error on macOS

So I am developing a set of wrapping utilities for SDL2 but have come across a strange issue. Whenever I poll for errors using SDL_GetError at the end of a frame, if I am touching the trackpad I get the following strange error message: Unknown touch device id -1, cannot reset. This error in quotes only shows one result on google and it seems to be an issue with macOS though with only one occurrence it's hard to be sure.
Here's a minimum reproducible example:
#include <SDL2/SDL.h>
#include <stdio.h>
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 100, 100, SDL_WINDOW_SHOWN);
while(1) {
SDL_Event e;
while(SDL_PollEvent(&e))
switch(e.type) {
case SDL_QUIT: {
SDL_DestroyWindow(window);
SDL_Quit();
exit(0);
break;
}
}
const char* err = SDL_GetError();
if(err[0]) {
printf("%s", err);
exit(-1);
}
}
}
I understand that this is a strange issue(and a potentially unanswerable one) so I really appreciate any answers or suggestions.
I am using a MacBook Pro 13-inch Mid 2012 running macOS Catalina(10.15.6). Compiled using clang 12.0.0
EDIT: I have downloaded the SDL2 source code to look for the error and the root cause seems to be this function:
static int
SDL_GetTouchIndex(SDL_TouchID id)
{
int index;
SDL_Touch *touch;
for (index = 0; index < SDL_num_touch; ++index) {
touch = SDL_touchDevices[index];
if (touch->id == id) {
return index;
}
}
return -1;
}
I am in the process of looking for why exactly this error is triggered and will update this post for any future people who encounter this error as well as filing a bug report to the SDL dev team

cvShowImage makes the system to throw exceptions

I have a code in C language that uses the cvopen Library.
Here is the code:
#include <stdio.h>
#include <opencv2\highgui\highgui_c.h>
int main(void)
{
int i;
cvNamedWindow("Display window", CV_WINDOW_AUTOSIZE); //create a window
//create an image
IplImage* image = cvLoadImage("C:\\Users\\magshimim\\Desktop\\Mummy.png", 1);
if (!image)//The image is empty.
{
printf("could not open image\n");
}
else
{
cvShowImage("Display window", image);
cvWaitKey(0);
system("pause");
cvReleaseImage(&image);
}
getchar();
return 0;
}
In line 17 "cvShowImage("Display window", image);" the system throws exception that says:
Exception thrown at 0xAD76406A in Q4.exe: 0xC0000008: An invalid handle was specified
The cvopen pack is fine, and other function works. but this code (which works on other computers) just crushes every time.
How can i fix this?
cvShowImage is part of the old C-style naming convention in OpenCV. This old convention has been fully depreciated and is not compatible with OpenCV 3.0 and up.
Instead of cvShowImage try using imshow
imshow("Display Window", image);

GTK+ 3.0 and C programm

I'm a beginner programmer in C/C++. ;) Recently on the Internet I found information about GTK and so on. Also I found this site: (http://wingtk.sourceforge.net/ishan/sliders.html). I wanted to run this code, but my compiler says something like this in 231 line (in sliders.c): "GtkWidget has no member named 'parent'", I don't understand, what's wrong with this program. I tried to fix this, but I failed.
Piece of code:
void on_new_activate (GtkMenuItem *menuitem, gpointer user_data)
{
int rand, x;
gtk_statusbar_push (GTK_STATUSBAR(statusbar1), 0, "Welcome to sliders");
move_no=0;
if (GTK_IS_WIDGET(image) && GTK_IS_WIDGET(image->parent)) //**error here**
{
gtk_object_ref (GTK_OBJECT(image));
gtk_container_remove (GTK_CONTAINER(alignment1), image);
gtk_container_add (GTK_CONTAINER(alignment1), table1);
gtk_widget_show (table1);
}
for (x=0; x<=14; x++)
{
rand = abs(g_rand_int ( g_rand_new () )) % 15;
if (x!=rand)
swap_buttons (x, rand);
}
}
The parent container is no longer exposed as a member. Instead of accessing image->parent, call gtk_widget_get_parent(image).
In the long term, as pointed out by #andlabs, you are best off finding a tutorial that covers GTK+ version 3, which you are using.

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.

mysterious crash after load_bitmap from Allegro

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.

Resources