Need this to play music - c++

Game dev class. Trying to understand what i did wrong here. Did I add the wrong thing, or do i have it at the wrong place.
my goal was to add music.
#include "allegro5/allegro.h"
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_native_dialog.h>
#include <stdio.h>
#include <string>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>
#define FPS 60
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define BLACK al_map_rgb(0, 0, 0)
#define WHITE al_map_rgb(255, 255, 255)
//Prototypes
bool initializeAllegro();
//Essential Allegro pointers
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *eventQueue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_FONT *arial14;
int main()
{
ALLEGRO_BITMAP *backgroundImage; //Bitmap for the background image(star field)
int backgroundImageWidth = 0, backgroundImageHeight = 0;
bool redrawRequired = true;
//Using std:string for name, so length is not a factor
std::string userName = "";
std::string prompt1 = "Enter your Player's name below";
std::string prompt2 = "(Letters & digits only. Backspace to erase.)";
std::string prompt3 = "Press Enter to accept...";
std::string prompt4 = "Press Escape to exit...";
char keyToAdd = ' ';
bool enteringUserName = true;
bool quit = false;
//Initialize allegro, etc
if (!initializeAllegro()) return -1;
//load the arial font
arial14 = al_load_font("arial-mt-black.ttf", 14, 0);
if (!arial14)
{
return -3;
}
//test running music
al_init_acodec_addon();
ALLEGRO_SAMPLE *song = al_load_sample("hideandseek.wav");
ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(song);
al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP);
al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer());
//Load the background picture
if (!(backgroundImage = al_load_bitmap("starbackground.bmp")))
{
return -5;
}
//Get dimensions of the background image
backgroundImageWidth = al_get_bitmap_width(backgroundImage);
backgroundImageHeight = al_get_bitmap_height(backgroundImage);
//Set the back buffer as the drawing bitmap for the display
al_set_target_bitmap(al_get_backbuffer(display));
//Initialize the event queue
eventQueue = al_create_event_queue();
if (!eventQueue)
{
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
//Register events as arriving from these sources: display, timer, keyboard
al_register_event_source(eventQueue, al_get_display_event_source(display));
al_register_event_source(eventQueue, al_get_timer_event_source(timer));
al_register_event_source(eventQueue, al_get_keyboard_event_source());
al_flip_display();
//play song
al_play_sample_instance(songInstance);
//Start up the timer. Don't do this until just before the game is to start!
al_start_timer(timer);
//This would be a loop solely for entering the user's name, not starting the game
//Allegro keycodes are laid out as follows. (Full set of key codes is included in keycodes.h)
//Key codes for capital letters are 1 - 26 for A - Z
//Digit values are 27 - 36 for the top digits (0 - 9), and 37 - 46 for the keypad
while (!quit)
{
ALLEGRO_EVENT evt;
//Wait for one of the allegro-defined events
al_wait_for_event(eventQueue, &evt);
//An event was generated. Check for all possible event types
switch (evt.type)
{
case ALLEGRO_EVENT_KEY_CHAR:
if (evt.keyboard.keycode >= 1 && evt.keyboard.keycode <= 26) //It's a capital letter
{
//Convert to ASCII capital
keyToAdd = *al_keycode_to_name(evt.keyboard.keycode);
userName += keyToAdd;
}
else if (evt.keyboard.keycode >= 27 && evt.keyboard.keycode <= 36)
{
//Convert to digit
keyToAdd = evt.keyboard.keycode + 21;
userName += keyToAdd;
}
//Handle digits on keypad
else if (evt.keyboard.keycode >= 37 && evt.keyboard.keycode <= 46)
{
//Convert to digit
keyToAdd = evt.keyboard.keycode + 11;
userName += keyToAdd;
}
else //Handle a few other keys
{
switch(evt.keyboard.keycode)
{
case ALLEGRO_KEY_BACKSPACE:
if (userName.length() > 0)
userName = userName.substr(0, userName.length() - 1);
break;
//Enter key stops username entry
case ALLEGRO_KEY_ENTER:
case ALLEGRO_KEY_PAD_ENTER:
enteringUserName = false;
break;
case ALLEGRO_KEY_ESCAPE:
quit = true;
break;
}
}
break;
case ALLEGRO_EVENT_DISPLAY_CLOSE:
quit = true;
break;
case ALLEGRO_EVENT_TIMER:
redrawRequired = true;
break;
}//END switch evt.type
//Rerender the entire scene
if (redrawRequired && al_is_event_queue_empty(eventQueue))
{
redrawRequired = false;
al_clear_to_color(BLACK); //Just in case
//Draw background
al_draw_scaled_bitmap(backgroundImage, 0, 0, backgroundImageWidth, backgroundImageHeight,
0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
//Prompt for name to be entered
al_draw_textf (arial14, WHITE, 0, 30, 0, "%s", prompt1.c_str());
al_draw_textf (arial14, WHITE, 0, 50, 0, "%s", prompt2.c_str());
if (userName.length() > 0)
{
if (enteringUserName)
{
al_draw_textf (arial14, WHITE, 0, 70, 0, "%s", userName.c_str());
al_draw_textf (arial14, WHITE, 0, 90, 0, "%s", prompt3.c_str());
}
else
{
al_draw_textf (arial14, WHITE, 0, 70, 0, "You entered your name as-->%s", userName.c_str());
}
}
al_draw_textf(arial14, WHITE, 0, 400, 0, "%s", prompt4.c_str());
al_flip_display();
}//END rendering the screen
}//END input Loop
//Release all dynamically allocated memory
al_destroy_bitmap(backgroundImage);
al_destroy_font(arial14);
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(eventQueue);
//destroy songs
al_destroy_sample(song);
al_destroy_sample_instance(songInstance);
return 0;
}//END main
//Take care of Allegro initialization tasks
//Return false if any fail.
bool initializeAllegro()
{
bool success = true;
//Init basic environment
if (!al_init())
{
al_show_native_message_box(NULL, "ERROR", "Allegro failed to initialize!", NULL, NULL, NULL);
success = false;
}
//Initialize keyboard input
if (!al_install_keyboard())
{
al_show_native_message_box(NULL, "ERROR", "Keyboard failed to initialize!", NULL, NULL, NULL);
success = false;
}
//Initialize timer
timer = al_create_timer(1.0 / FPS);
if (!timer)
{
al_show_native_message_box(NULL, "ERROR", "Timer failed to initialize!", NULL, NULL, NULL);
success = false;
}
//Initialize display
display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
if (!display)
{
al_show_native_message_box(NULL, "ERROR", "Display failed to initialize!", NULL, NULL, NULL);
success = false;
}
//Initialize the image mechanism
if (!al_init_image_addon())
{
al_show_native_message_box(NULL, "ERROR", "Image addon failed to initialize!", NULL, NULL, NULL);
success = false;
}
al_init_font_addon();
al_init_ttf_addon();
return success;
}//END initializeAllegro
Any wav file should work.
i get the error message -
First-chance exception at 0x0F196486
(allegro-5.0.10-monolith-md-debug.dll) in User Input Project.exe:
0xC0000005: Access violation reading location 0x00000000. Unhandled
exception at 0x0F196486 (allegro-5.0.10-monolith-md-debug.dll) in User
Input Project.exe: 0xC0000005: Access violation reading location
0x00000000.
The program '[9596] User Input Project.exe' has exited with code 0
(0x0).

I mentioned to step through things with a debugger in my comment, but the code smell here is this:
ALLEGRO_SAMPLE *song = al_load_sample("hideandseek.wav");
ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(song);
You should Definitely be checking for (song == NULL) before you call al_create_sample_instance()
If your path to the .WAV file is wrong or if the loading fails for any other reason, then al_load_sample() will return NULL, and you shouldn't be trying to do anything with song

Related

Allegro5 Problems with deleting a text and display a new one after pressing a button

So I wrote this code where if I press enter it should clear the page and then enter a new text that says "game starts". but it won't run at all and just stays the same, anyone knows how to fix this?
PS. Extra question, how do I create a delay after replacing the text for 5 seconds then clear the text again?
Thank!
#include <iostream>
#include<allegro5/allegro.h>
#include<allegro5/allegro_ttf.h>
#include<allegro5/allegro_font.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<Windows.h>
int main()
{
al_init();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_DISPLAY* display = al_create_display(640, 480);
ALLEGRO_FONT* font = al_load_ttf_font("YARDSALE.ttf", 30, 0);
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
al_install_keyboard();
al_install_mouse();
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
bool done = false;
while (!done) {
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(139, 0, 0), 320, 150, ALLEGRO_ALIGN_CENTER, "Press Enter to start");
al_draw_text(font, al_map_rgb(148, 0, 211), 320, 300, ALLEGRO_ALIGN_CENTER, "PRESS ESC TO QUIT");
al_flip_display();
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if (event.type == ALLEGRO_EVENT_KEY_UP)
{
switch (event.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_ENTER:
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(0, 255, 0), 300, 200, 0, "Game Starts");
al_flip_display;
break;
}
}
}
al_destroy_font(font);
al_destroy_display(display);
}
You simply missed "()" when you called 'al_flip_display'. It should be 'al_flip_display();'.
But even with this it will not work as expected, because "Game starts" will only appears for a moment. I suggest adding game states.
And I think at the end of the main function you should also destroy event queue.
#include<allegro5/allegro.h>
#include<allegro5/allegro_ttf.h>
#include<allegro5/allegro_font.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<Windows.h>
enum class GAME_STATE { CLICK, INTRO, MAIN };
int main()
{
al_init();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_DISPLAY* display = al_create_display(640, 480);
ALLEGRO_FONT* font = al_load_ttf_font("font_code_pro.ttf", 30, 0);
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
ALLEGRO_TIMER* timer = al_create_timer(1 / 60.0);
al_install_keyboard();
al_install_mouse();
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
al_register_event_source(queue, al_get_timer_event_source(timer));
bool done = false;
bool draw = false;
unsigned delay = 0;
GAME_STATE state = GAME_STATE::CLICK;
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if(event.type == ALLEGRO_EVENT_TIMER)
{
draw = true;
if(state == GAME_STATE::INTRO && !((++delay) % (60 * 5)))state = GAME_STATE::MAIN;
}
if(event.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(event.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_ENTER:
if(state == GAME_STATE::CLICK)
state = GAME_STATE::INTRO;
break;
}
}
if(draw)
{
draw = false;
switch(state)
{
case GAME_STATE::CLICK:
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(139, 0, 0), 320, 150, ALLEGRO_ALIGN_CENTER, "Press Enter to start");
al_draw_text(font, al_map_rgb(148, 0, 211), 320, 300, ALLEGRO_ALIGN_CENTER, "PRESS ESC TO QUIT");
al_flip_display();
break;
case GAME_STATE::INTRO:
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_text(font, al_map_rgb(0, 255, 0), 300, 200, 0, "Game Starts");
al_flip_display();
break;
case GAME_STATE::MAIN:
al_clear_to_color(al_map_rgb(255, 255, 255));
//
al_flip_display();
break;
default:
break;
}
}
}
al_destroy_font(font);
al_destroy_timer(timer);
al_destroy_event_queue(queue);
al_destroy_display(display);
}
I hope it helps! :)

Why is an XImage's data pointer null when capturing using XShmGetImage?

I'm writing a program to rapidly capture images from one window, modify them, and output them to another window using Xlib with C++. I have this working via XGetImage:
#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
const int TEST_SIZE = 512;
int main()
{
// Open default display
Display *display = XOpenDisplay(nullptr);
int screen = DefaultScreen(display);
Window rootWin = RootWindow(display, screen);
GC graphicsContext = DefaultGC(display, screen);
// Create new window and subscribe to events
long blackPixel = BlackPixel(display, screen);
long whitePixel = WhitePixel(display, screen);
Window newWin = XCreateSimpleWindow(display, rootWin, 0, 0, TEST_SIZE, TEST_SIZE, 1, blackPixel, whitePixel);
XMapWindow(display, newWin);
XSelectInput(display, newWin, ExposureMask | KeyPressMask);
// Main event loop for new window
XImage *image;
XEvent event;
bool exposed = false;
bool killWindow = false;
while (!killWindow)
{
// Handle pending events
if (XPending(display) > 0)
{
XNextEvent(display, &event);
if (event.type == Expose)
{
exposed = true;
} else if (event.type == NoExpose)
{
exposed = false;
} else if (event.type == KeyPress)
{
killWindow = true;
}
}
// Capture the original image
image = XGetImage(display, rootWin, 0, 0, TEST_SIZE, TEST_SIZE, AllPlanes, ZPixmap);
// Modify the image
if (image->data != nullptr)
{
long pixel = 0;
for (int x = 0; x < image->width; x++)
{
for (int y = 0; y < image->height; y++)
{
// Invert the color of each pixel
pixel = XGetPixel(image, x, y);
XPutPixel(image, x, y, ~pixel);
}
}
}
// Output the modified image
if (exposed && killWindow == false)
{
XPutImage(display, newWin, graphicsContext, image, 0, 0, 0, 0, TEST_SIZE, TEST_SIZE);
}
XDestroyImage(image);
}
// Goodbye
XCloseDisplay(display);
}
It generates output like this: https://streamable.com/hovg9
I'm happy to have gotten this far, but from what I've read the performance isn't going to scale very well because it has to allocate space for a new image at every frame. In fact, without the call to XDestroyImage at the end of the loop this program fills up all 16GB of memory on my machine in a matter of seconds!
It seems the recommended approach here is to to set up a shared memory space where X can write the contents of each frame and my program can subsequently read and modify them without the need for any extra allocation. Since the call to XShmGetImage blocks and waits for IPC I believe this means I won't have to worry about any concurrency issues in the shared space.
I've attempted to implement the XShmGetImage approach with the following code:
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>
#include <X11/Xutil.h>
const int TEST_SIZE = 512;
int main()
{
// Open default display
Display *display = XOpenDisplay(nullptr);
int screen = DefaultScreen(display);
Window rootWin = RootWindow(display, screen);
GC graphicsContext = DefaultGC(display, screen);
// Create new window and subscribe to events
long blackPixel = BlackPixel(display, screen);
long whitePixel = WhitePixel(display, screen);
Window newWin = XCreateSimpleWindow(display, rootWin, 0, 0, TEST_SIZE, TEST_SIZE, 1, blackPixel, whitePixel);
XMapWindow(display, newWin);
XSelectInput(display, newWin, ExposureMask | KeyPressMask);
// Allocate shared memory for image capturing
Visual *visual = DefaultVisual(display, 0);
XShmSegmentInfo shminfo;
int depth = DefaultDepth(display, screen);
XImage *image = XShmCreateImage(display, visual, depth, ZPixmap, nullptr, &shminfo, TEST_SIZE, TEST_SIZE);
shminfo.shmid = shmget(IPC_PRIVATE, image->bytes_per_line * image->height, IPC_CREAT | 0666);
shmat(shminfo.shmid, nullptr, 0);
shminfo.shmaddr = image->data;
shminfo.readOnly = False;
XShmAttach(display, &shminfo);
// Main event loop for new window
XEvent event;
bool exposed = false;
bool killWindow = false;
while (!killWindow)
{
// Handle pending events
if (XPending(display) > 0)
{
XNextEvent(display, &event);
if (event.type == Expose)
{
exposed = true;
} else if (event.type == NoExpose)
{
exposed = false;
} else if (event.type == KeyPress)
{
killWindow = true;
}
}
// Capture the original image
XShmGetImage(display, rootWin, image, 0, 0, AllPlanes);
// Modify the image
if (image->data != nullptr) // NEVER TRUE. DATA IS ALWAYS NULL!
{
long pixel = 0;
for (int x = 0; x < image->width; x++)
{
for (int y = 0; y < image->height; y++)
{
// Invert the color of each pixel
pixel = XGetPixel(image, x, y);
XPutPixel(image, x, y, ~pixel);
}
}
}
// Output the modified image
if (exposed && killWindow == false)
{
XShmPutImage(display, newWin, graphicsContext, image, 0, 0, 0, 0, 512, 512, false);
}
}
// Goodbye
XFree(image);
XCloseDisplay(display);
}
Somehow, the images are still being captured and sent to the new window, but the data pointer within the XImage is always null. I can't modify any of the contents and any usage of the XGetPixel and XPutPixel macros will fail. Notice in this video that none of the colors are being inverted like before: https://streamable.com/dckyv
This doesn't make any sense to me. Clearly the data is still being transferred between the windows, but where is it in the XImage structure? How can I access it via code?
It turns out I wasn't reading the signature for shmat correctly. It doesn't have a void return type, it returns a void pointer. This needs to be assigned directly to the XImage's data pointer in order for the data pointer to do anything.
In the call to XShmPutImage the shared memory location is referenced internally rather than the XImage's data pointer which is why this was still working even without utilizing the return value from shmat.
Here's the working code, along with a few other additions which I made for error handling and teardown:
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>
#include <X11/Xutil.h>
const int TEST_SIZE = 512;
static int handleXError(Display *display, XErrorEvent *event)
{
printf("XErrorEvent triggered!\n");
printf("error_code: %d", event->error_code);
printf("minor_code: %d", event->minor_code);
printf("request_code: %d", event->request_code);
printf("resourceid: %lu", event->resourceid);
printf("serial: %d", event->error_code);
printf("type: %d", event->type);
return 0;
}
int main()
{
// Open default display
XSetErrorHandler(handleXError);
Display *display = XOpenDisplay(nullptr);
int screen = DefaultScreen(display);
Window rootWin = RootWindow(display, screen);
GC graphicsContext = DefaultGC(display, screen);
// Create new window and subscribe to events
long blackPixel = BlackPixel(display, screen);
long whitePixel = WhitePixel(display, screen);
Window newWin = XCreateSimpleWindow(display, rootWin, 0, 0, TEST_SIZE, TEST_SIZE, 1, blackPixel, whitePixel);
XMapWindow(display, newWin);
XSelectInput(display, newWin, ExposureMask | KeyPressMask);
// Allocate shared memory for image capturing
XShmSegmentInfo shminfo;
Visual *visual = DefaultVisual(display, screen);
int depth = DefaultDepth(display, screen);
XImage *image = XShmCreateImage(display, visual, depth, ZPixmap, nullptr, &shminfo, TEST_SIZE, TEST_SIZE);
shminfo.shmid = shmget(IPC_PRIVATE, image->bytes_per_line * image->height, IPC_CREAT | 0777);
image->data = (char*)shmat(shminfo.shmid, 0, 0);
shminfo.shmaddr = image->data;
shminfo.readOnly = False;
XShmAttach(display, &shminfo);
XSync(display, false);
shmctl(shminfo.shmid, IPC_RMID, 0);
// Main event loop for new window
XEvent event;
bool exposed = false;
bool killWindow = false;
while (!killWindow)
{
// Handle pending events
if (XPending(display) > 0)
{
XNextEvent(display, &event);
if (event.type == Expose)
{
exposed = true;
} else if (event.type == NoExpose)
{
exposed = false;
} else if (event.type == KeyPress)
{
killWindow = true;
}
}
// Capture the original image
XShmGetImage(display, rootWin, image, 0, 0, AllPlanes);
// Modify the image
if(image->data != nullptr) // NEVER TRUE. DATA IS ALWAYS NULL!
{
long pixel = 0;
for (int x = 0; x < image->width; x++)
{
for (int y = 0; y < image->height; y++)
{
// Invert the color of each pixel
pixel = XGetPixel(image, x, y);
XPutPixel(image, x, y, ~pixel);
}
}
}
// Output the modified image
if (exposed && killWindow == false)
{
XShmPutImage(display, newWin, graphicsContext, image, 0, 0, 0, 0, 512, 512, false);
}
}
// Goodbye
XShmDetach(display, &shminfo);
XDestroyImage(image);
shmdt(shminfo.shmaddr);
XCloseDisplay(display);
}

XOpenDisplay failing

I'm writing a simple window class which is failing to return a display. Here's the short version:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
class WindowImpl
{
public:
WindowImpl()
{
open = true;
}
WindowImpl(float width, float height)
{
if(!create(width, height))
{
fprintf(stderr, "Could not open display\n");
exit(1);
}
open = true;
}
~WindowImpl()
{
XCloseDisplay(display);
};
bool create(float width, float height)
{
display = XOpenDisplay(NULL);
if(display == NULL)
return false;
int displayID = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, displayID), 10, 10, width, height, 1, BlackPixel(display, displayID), WhitePixel(display, displayID));
XMapWindow(display, window);
return true;
}
bool isOpen()
{
return open;
}
void close()
{
open == false;
}
private:
Display* display;
Window window;
bool open;
};
int main()
{
WindowImpl myWindow(1920, 1080);
char* cmd;
while(myWindow.isOpen())
{
if(gets(cmd) == "close")
myWindow.close();
}
return 0;
}
WindowImpl::create fails, XOpenDisplay is returning NULL but I'm not sure why. Hopefully someone could shed some light on the problem here.
Edit: Changing WindowImpl::create to return true and false instead of 0 and 1 causes it to go through but the window still doesn't open;
For clarification:
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
Display* display;
Window window;
XEvent event;
char* message = "Hello";
int screenSize;
display = XOpenDisplay(NULL);
if(display == NULL)
{
fprintf(stderr, "Cannot open display\n");
exit(1);
}
screenSize = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, screenSize), 10, 10, 1920, 1080, 1, BlackPixel(display, screenSize), WhitePixel(display, screenSize));
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
KeySym keysym = XK_Escape;
KeyCode keycode = XKeysymToKeycode(display, keysym);
while(true)
{
XNextEvent(display, &event);
if(event.type == KeyPress && event.xkey.keycode == keycode)
break;
}
XCloseDisplay(display);
return 0;
}
Compiles and runs just fine.
When you create a windows, you need to let the X server to handle output buffer. Normally this is done when you enter into the window event loop, i.e. calling any function that has EVENT in its name.
Once you are not dealing with events in your code, another way to flush the output buffer is calling XFlush, as said in its manual:
The XFlush function flushes the output buffer. Most client applications need not use this function because the output buffer is automatically flushed as needed by calls to XPending, XNextEvent, and XWindowEvent. Events generated by the server may be enqueued into the library's event queue.
The XSync function flushes the output buffer and then waits until all requests have been received and processed by the X server.
So, to solve your issue, I suggest to put this line of code:
window = XCreateSimpleWindow(display, RootWindow(display, displayID), 10, 10, width, height, 1, BlackPixel(display, displayID), WhitePixel(display, displayID));
XMapWindow(display, window);
XFlush(display); // <------------
return true;

Xlib: Closing window always causes fatal IO error?

I'm not sure why this happens, but any window I create using Xlib in C++ gives outputs an error to the terminal when I try to close is using the X button. I can close it programmatically with no errors, it's just the X button that does it.
The error is the following:
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
after 483 requests (483 known processed) with 0 events remaining.
The number of requests is different every time, but there's always 0 events remaining. Why does this happen? The cause doesn't seem to be my code, since it does this no matter what and sends no close events to the queue. I've tried intercepting the Atom WM_WINDOW_DELETE, and it doesn't run over the expected code when I close the window.
Edit: Added event loop code.
while(XPending(display)) {
XNextEvent(display, &event);
pthread_mutex_unlock(&mutex);
if(event.type == Expose) {
XWindowAttributes getWindowAttributes;
pthread_mutex_lock(&mutex);
XGetWindowAttributes(display, window, &getWindowAttributes);
if(state.currentState == STATE_NORMAL) {
state.normX = getWindowAttributes.x;
state.normY = getWindowAttributes.y;
state.normWidth = getWindowAttributes.width;
state.normHeight = getWindowAttributes.height;
}
pthread_mutex_unlock(&mutex);
glViewport(0, 0, getWindowAttributes.width, getWindowAttributes.height);
} else if(event.type == KeyPress) {
return false;
} else if(event.type == ClientMessage) {
std::cout<<"X Button pressed"<<std::endl; //Never run when X-ing window
if(event.xclient.message_type == XInternAtom(display, "WM_DELETE_WINDOW", True)) {
return false;
}
} else if(event.type == ButtonPress) {
if(state.currentState != STATE_FULLSCREEN) {
fullscreen();
} else {
normalize();
}
} else if(!handleEvent(event)){
return false;
}
pthread_mutex_lock(&mutex);
}
In addition to WM_WINDOW_DELETE you need to listen for and handle the ClientMessage event.
Modifying the example from Rosetta Code to illustrate:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
Display *d;
Window w;
XEvent e;
const char *msg = "Hello, World!";
int s;
d = XOpenDisplay(NULL);
if (d == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1, BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);
// I support the WM_DELETE_WINDOW protocol
Atom WM_DELETE_WINDOW = XInternAtom(d, "WM_DELETE_WINDOW", False);
XSetWMProtocols(d, w, &WM_DELETE_WINDOW, 1);
while (1) {
XNextEvent(d, &e);
if (e.type == Expose) {
XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
}
else if (e.type == KeyPress)
break;
else if (e.type == ClientMessage)
// TODO Should check here for other client message types -
// however as the only protocol registered above is WM_DELETE_WINDOW
// it is safe for this small example.
break;
}
XCloseDisplay(d);
return 0;
}

I want to make splash screen and now I have two problems?

1: I want to have a splash screen but I only have a window?so,how to do with sth like parm
2: I've used a while(!done) to draw the window so how to break out with a function or sth else
here is my code and much thx to you
g++ -o m_splash m_splash.cpp -lX11 -lImlib2
#include <stdio.h>
#include <X11/Xlib.h>
#include <Imlib2.h>
#include <unistd.h>
int main()
{
Imlib_Image m_img;
Display *m_dpy;
Pixmap m_pix;
Window m_root;
Screen *scn;
int m_width, m_height;
const char *filename = "/home/ang/so_zt/w.png";
m_img = imlib_load_image(filename);
if(!m_img)
{
printf("%s\n","init m_img faild");
}
imlib_context_set_image(m_img);
m_width = imlib_image_get_width();
m_height = imlib_image_get_height();
m_dpy = XOpenDisplay(NULL);
if(!m_dpy)
{
printf("%s\n","open display failed");
}
scn = DefaultScreenOfDisplay(m_dpy);
int s = DefaultScreen(m_dpy);
m_root = XCreateSimpleWindow(m_dpy, RootWindow(m_dpy,s),10,10,m_width,m_height,0,
BlackPixel(m_dpy, s), WhitePixel(m_dpy, s));
m_pix = XCreatePixmap(m_dpy, m_root, m_width, m_height, DefaultDepthOfScreen(scn));
imlib_context_set_display(m_dpy);
imlib_context_set_visual(DefaultVisualOfScreen(scn));
imlib_context_set_colormap(DefaultColormapOfScreen(scn));
imlib_context_set_drawable(m_pix);
imlib_render_image_on_drawable(0,0);
XSetWindowBackgroundPixmap(m_dpy, m_root, m_pix);
XClearWindow(m_dpy, m_root);
Atom wmDeleteMessage = XInternAtom(m_dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(m_dpy, m_root, &wmDeleteMessage, 1);
XSelectInput(m_dpy, m_root, ExposureMask | KeyPressMask | StructureNotifyMask);
XMapWindow(m_dpy, m_root);
bool done = false;
while (!done)
{
XEvent m_ev;
XNextEvent(m_dpy, &m_ev);
/* draw or redraw the window */
if (m_ev.type == Expose)
{
XFillRectangle(m_dpy, m_root, DefaultGC(m_dpy, DefaultScreen(m_dpy)), 20, 20, 10, 10);
}
/* exit on key press */
//usleep(1000000);
//done = true;
switch(m_ev.type)
{
case KeyPress:
XDestroyWindow(m_dpy, m_root);
break;
case DestroyNotify:
done = true;
break;
case ClientMessage:
if (m_ev.xclient.data.l[0] == wmDeleteMessage)
{
done = true;
}
break;
}
}
//XFreePixmap(m_dpy, m_pix);
//imlib_free_image();
//XCloseDisplay(m_dpy);
}
To make it a splash screen, use extended window manager hints.
#include <X11/Xatom.h>
Atom type = XInternAtom(m_dpy, "_NET_WM_WINDOW_TYPE", False);
Atom value = XInternAtom(m_dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
XChangeProperty(m_dpy, m_root, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
The window then appears without decorations and stays until clicked.
When clicked you get an UnmapNotify event, so this what you should use to set done.
To avoid having to get events, add
XFlush(m_dpy);
after mapping the window to display it and
XUnmapWindow(m_dpy, m_root);
when you want to get rid of it.
In this example the program just sleeps for 5 seconds before continuing:
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <Imlib2.h>
#include <unistd.h>
int main()
{
Imlib_Image m_img;
Display *m_dpy;
Pixmap m_pix;
Window m_root;
Screen *scn;
int m_width, m_height;
const char *filename = "w.png";
m_img = imlib_load_image(filename);
if(!m_img)
{
printf("%s\n","init m_img faild");
}
imlib_context_set_image(m_img);
m_width = imlib_image_get_width();
m_height = imlib_image_get_height();
m_dpy = XOpenDisplay(NULL);
if(!m_dpy)
{
printf("%s\n","open display failed");
}
scn = DefaultScreenOfDisplay(m_dpy);
int s = DefaultScreen(m_dpy);
m_root = XCreateSimpleWindow(m_dpy, RootWindow(m_dpy,s),10,10,m_width,m_height,0,
BlackPixel(m_dpy, s), WhitePixel(m_dpy, s));
m_pix = XCreatePixmap(m_dpy, m_root, m_width, m_height, DefaultDepthOfScreen(scn));
Atom type = XInternAtom(m_dpy, "_NET_WM_WINDOW_TYPE", False);
Atom value = XInternAtom(m_dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
XChangeProperty(m_dpy, m_root, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
imlib_context_set_display(m_dpy);
imlib_context_set_visual(DefaultVisualOfScreen(scn));
imlib_context_set_colormap(DefaultColormapOfScreen(scn));
imlib_context_set_drawable(m_pix);
imlib_render_image_on_drawable(0,0);
XSetWindowBackgroundPixmap(m_dpy, m_root, m_pix);
XClearWindow(m_dpy, m_root);
Atom wmDeleteMessage = XInternAtom(m_dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(m_dpy, m_root, &wmDeleteMessage, 1);
XMapWindow(m_dpy, m_root);
XFlush(m_dpy);
sleep(5);
XUnmapWindow(m_dpy, m_root);
}
and this program can have a splash with hello world alse I do not know how to jump out of the while(!done) so much thx to U
g++ -o test_chr test_chr.cpp -lX11
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
Display *d;
Window w;
XEvent e;
const char *msg = "Hello, World!";
int s;
bool done = false;
/* open connection with the server */
d = XOpenDisplay(NULL);
if (d == NULL)
{
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(d);
/* create window */
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 480, 320, 0,BlackPixel(d, s), WhitePixel(d, s));
Atom type = XInternAtom(d,"_NET_WM_WINDOW_TYPE", False);
Atom value = XInternAtom(d,"_NET_WM_WINDOW_TYPE_SPLASH", False);
XChangeProperty(d, w, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
/* register interest in the delete window message */
Atom wmDeleteMessage = XInternAtom(d, "WM_DELETE_WINDOW", False);
XSetWMProtocols(d, w, &wmDeleteMessage, 1);
/* select kind of events we are interested in */
XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask);
/* map (show) the window */
XMapWindow(d, w);
/* event loop */
while (!done)
{
XNextEvent(d, &e);
/* draw or redraw the window */
if (e.type == Expose)
{
XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
}
/* exit on key press */
switch(e.type)
{
case KeyPress:
XDestroyWindow(d, w);
break;
case DestroyNotify:
done = true;
break;
case ClientMessage:
if (e.xclient.data.l[0] == wmDeleteMessage)
{
done = true;
}
break;
}
}
/* close connection to server */
XCloseDisplay(d);
return 0;
}