X11 won't open a window - c++

I am trying to open an X11 window, print out one pixel, and then later add code to make Terminate() return true. But it won't make a window. Here is my code:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <iostream>
int x = 0;
int y = 0;
bool Termination() {
return true;
}
int main() {
Display *dspl = XOpenDisplay(NULL);
if (!dspl) return 1;
int screenNumber = DefaultScreen(dspl);
unsigned long white = WhitePixel(dspl, screenNumber);
unsigned long black = BlackPixel(dspl, screenNumber);
Window win = XCreateSimpleWindow(dspl, DefaultRootWindow(dspl), 50, 50, 1280, 720, 0, black, white);
XSetStandardProperties(dspl, win, "Lel", "Gaem", None, NULL, 0, NULL);
GC gc = XCreateGC(dspl, win, 0,0);
XSetBackground(dspl, gc, black);
XSetForeground(dspl, gc, white);
XClearWindow(dspl, win);
XMapRaised(dspl, win);
XDrawPoint(dspl, win, gc, x, y);
while (Termination())
{
}
XFreeGC(dspl, gc);
XDestroyWindow(dspl, win);
XCloseDisplay(dspl);
printf("Job's done!\n");
return 0;
}
What's keeping X11 from making my window appear?

add this before the while
XMapWindow(dspl, win);
XInternAtom(dspl, "WM_DELETE_WINDOW", False);

Related

i can't show text on the window using SDL2 TTF library

I make a SDL_ttf test code:
#include <SDL.h>
#include <stdbool.h>
#include <iostream>
#include "SDLwindow.h"
#include <SDL_ttf.h>
#include "GraphLib.h"
#undef main
using namespace std;
int main() {
bool running = 1;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
TTF_Init();
WindowSDL window1;
TTF_Font* font = TTF_OpenFont("./acme.ttf", 10);
SDL_Surface* textSurf = TTF_RenderText_Solid(font, "Hola mundo", {255,0,0});
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(window1.renderer, textSurf);
SDL_FreeSurface(textSurf);
SDL_Rect textRect;
textRect.x = 10;
textRect.y = 10;
textRect.w = 400;
textRect.h = 100;
//TTF_CloseFont(font);
window1.CreateWindow("Pix", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 600, SDL_WINDOW_BORDERLESS);
window1.CreateRenderer(window1.window, -1, 0);
window1.ChangeBackgroundColor(0xe0e0e0);
drawLine(window1, 3, 3, 40, 50, 0x0aaf88, 0);
drawLine(window1, 40, 50, 80, 3, 0x0aaf88, 0);
drawLine(window1, 80, 3, 3, 3, 0x0aaf88, 0);
SDL_RenderCopy(window1.renderer, textTexture, NULL, &textRect);
SDL_RenderPresent(window1.renderer);
while (running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = 0;
}
}
}
window1.Shutdown();
TTF_Quit();
return 0;
}
...and... boom it doesn't appear, heres my screen:
I tried to change the order of certain lines like TTF_Init(); and so on, the results were the same, it does not appear on the screen

Text overlay not working with Xorg modesetting driver

This is a piece of code which displays text and background rectangle When this piece of code is run with Intel as default XORG driver everything works fine both text and rectangle are being displayed,whereas when i switch to the Modesetting driver only the background rectangle is seen and text is not being displayed
#include <iostream>
#include<unistd.h>
#include <sstream>
#include <string>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xft/Xft.h>
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
int main()
{
Display *display = XOpenDisplay(NULL);
Screen *scn = DefaultScreenOfDisplay(display);
int screen_num = DefaultScreen(display);
int screen_width = DisplayWidth(display, screen_num);
int screen_height = DisplayHeight(display, screen_num);
int defaultScnDepth = DefaultDepthOfScreen(scn);
Visual *visual = DefaultVisualOfScreen(scn);
Window window=XCreateSimpleWindow(display,RootWindow(display,screen_num), 50, 50, 400, 400, 2 ,BlackPixel(display,screen_num),WhitePixel(display,screen_num));
//XFlush(display)
XMapWindow(display, window);
XShmSegmentInfo shmInfo;
XImage *xImage;
Pixmap backPixmap;
(xImage) = XShmCreateImage(display, visual, defaultScnDepth, ZPixmap, NULL, &shmInfo, screen_width, screen_height);
shmInfo.shmid = shmget(IPC_PRIVATE, (xImage)->bytes_per_line * (xImage)->height, IPC_CREAT | 0777);
shmInfo.shmaddr = (char *) shmat(shmInfo.shmid, 0, 0);
xImage->data = shmInfo.shmaddr;
shmInfo.readOnly = False;
XShmAttach(display, &shmInfo);
(backPixmap) = XShmCreatePixmap(display, window, (char *) (shmInfo.shmaddr), &shmInfo, (xImage)->width, (xImage)->height, (xImage)->depth);
XGCValues values;
GC gc = XCreateGC(display, backPixmap, 0, &values);
XSync(display, false);
Drawable drawable =backPixmap;
visual = DefaultVisual(display, DefaultScreen(display));
Colormap colormap = XCreateColormap(display, window, visual, AllocNone);
//gc = XCreateGC(display, drawable, 0, &values);
//XFlushGC(display, gc);
const char *text = "Hello";
XftDraw *xftDraw = NULL;
XRenderColor xrFGColor, xrBGColor;
XftColor xftFGColor, xftBGColor;
XftFont *font = NULL;
font = XftFontOpenName( display, DefaultScreen( display ), "morpheus-18" );
xftDraw = XftDrawCreate(display, drawable, visual, colormap);
int nextLineStartY, rectYRef;
bool firstIte;
unsigned int rectX, rectY, rectWidth, rectHeight;
nextLineStartY = 0; rectYRef = 0;
firstIte = true;
rectX = 0; rectY = 0; rectWidth = 0; rectHeight = 0;
std::istringstream strStream(text);
std::string line;
while(std::getline(strStream, line))
{
const char *lineText = line.c_str();
if(*lineText == '\0')
{
nextLineStartY += rectHeight + 1;
continue;
}
const char *text = lineText;
XGlyphInfo extents;
XftTextExtents8(display, font, (XftChar8 *)text, strlen(text), &extents);
unsigned int width = extents.width;
unsigned int height = extents.height;
int ascent = extents.y;
int lBearing = extents.x;
rectX = 50 - lBearing - 1;
rectY = 50 - ascent - 1;
rectWidth = width + 2 * 1;
rectHeight = height + 5 + 1;
if(firstIte)
{
rectYRef = rectY;
firstIte = false;
}
int diff = rectYRef - rectY;
rectY += nextLineStartY + diff;
nextLineStartY += rectHeight + 1;
if(1)
{
xrBGColor.red = 0x7fff;
xrBGColor.green= 0x7fff;
xrBGColor.blue = 0x7fff;
xrBGColor.alpha= 0xffff;
XftColorAllocValue(display, visual, colormap, &xrBGColor, &xftBGColor);
// Draw background fill rectangle
XftDrawRect(xftDraw, &xftBGColor, rectX, rectY, rectWidth, rectHeight);
XftColorFree(display, visual, colormap, &xftBGColor);
}
xrFGColor.red = 0xbfff;
xrFGColor.green = 0xbfff;
xrFGColor.blue = 0xbfff;
xrFGColor.alpha= 0xffff;
XftColorAllocValue(display, visual, colormap, &xrFGColor, &xftFGColor);
// Overlay Text
XftDrawString8(xftDraw, &xftFGColor, font, 50, 50, (XftChar8 *) text, strlen(text));
XColor xForeColor;
xForeColor.red = 0xafff;
xForeColor.green = 0xafff;
xForeColor.blue = 0xffff;
if(XAllocColor(display,colormap,&xForeColor))
XSetForeground(display,gc,xForeColor.pixel);
XftColorFree(display, visual, colormap, &xftFGColor);
XFreeColors(display, colormap, &(xForeColor.pixel), 1, 0);
}
XftDrawDestroy(xftDraw);
XShmPutImage(display, window, gc, xImage, 0, 0, 0, 0, 400, 400, false);
XSync(display, false);
getchar();
}
I tried out other drivers too, with the radeon drivers i see a X error that shared pixmaps are not supported while i don't see any such error for the modesetting driver.
Has this something to do with the shared pixmaps, if yes how should i make it work with the modesetting driver.
I have been stuck on this for a while now, any help would be appreciated.

Allegro Throws errors when trying to draw lines

For some reason allegro (C++ Game Engine), throws errors when i try to run... So first this code works fine ! :
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
int main(void)
{
int width = 640, height = 480;
ALLEGRO_DISPLAY *display = NULL;
if (!al_init())
return -1;
display = al_create_display(width, height);
if (!display)
return -1;
al_flip_display();
//al_draw_line(100, 100, width - 100, 100, al_map_rgb(255, 0, 0), 1);
al_rest(3);
al_destroy_display(display);
return 0;
}
But all the sudden when i try to uncomment that line above i get this "error".
More Closer Up:
As you can see above this is some type of assertion failling... Im Confused?! Help would be appreciated!
Before you can use the primitives add-on you need to initialize it by calling al_init_primitives_addon.
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
int main(void)
{
int width = 640, height = 480;
ALLEGRO_DISPLAY *display = NULL;
if (!al_init())
return -1;
display = al_create_display(width, height);
if (!display)
return -1;
al_init_primitives_addon();
if (!al_init_primitives_addon())
return -1;
al_draw_line(0, 50, 300, 100, al_map_rgb(255, 0, 4), 1.0f);
al_flip_display();
al_rest(13);
al_shutdown_primitives_addon();
al_destroy_display(display);
return 0;
}
The full documentation can be found here: https://www.allegro.cc/manual/5/al_init_primitives_addon.
Don't forget to call al_shutdown_primitives_addon when you're done.

SDL2: How to keep aspect ratio when resizing the window

I am trying to create a SDL window which keeps its aspect ratio when resize event happens. If user widens the window, the height is increased and vice versa. I catch the SDL_WINDOWEVENT_RESIZED event, calculate new width or height which maintains the aspect ratio and then call SDL_SetWindowSize() with calculated values.
The problem is that calling the SDL_SetWindowSize() function inside the event polling loop does nothing on the screen. SDL does update the window size variables (calling SDL_GetWindowSize() in my main loop returns the updated window dimensions). However, the actual window is not updated.
The only way I can get this to work is to call constantly SDL_SetWindowSize() in the main loop, but I think that is the wrong way of doing things. The code below illustrates my problem. Is there a better and cleaner way to get this to work?
I am using SDL 2.0.3 and 64-bit Ubuntu Linux with GNOME desktop.
#include <SDL2/SDL.h>
static const float ASPECT_RATIO = 16.f/9.f;
SDL_Window* window;
SDL_Renderer* renderer;
uint32_t windowID;
SDL_Rect screen;
bool done = false;
bool resizeDone = false;
void handle_events()
{
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_WINDOWEVENT:
if(e.window.windowID == windowID) {
switch(e.window.event) {
case SDL_WINDOWEVENT_RESIZED: {
int width = e.window.data1;
int height = e.window.data2;
float aspectRatio = (float)width/(float)height;
if(aspectRatio != ASPECT_RATIO) {
if(aspectRatio > ASPECT_RATIO) {
height = (1.f / ASPECT_RATIO) * width;
}
else {
width = ASPECT_RATIO * height;
}
printf("Setting window size to %d, %d, aspect ratio: %f\n",
width, height, (float)width/(float)height);
}
screen.w = width;
screen.h = height;
SDL_SetWindowSize(window, width, height); // <-- does not work
resizeDone = true;
break;
}
}
}
break;
case SDL_QUIT:
done = true;
break;
default:
break;
}
}
}
void run() {
while(!done) {
//SDL_SetWindowSize(window, screen.w, screen.h); // <-- works
handle_events();
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
if(resizeDone) {
int w, h;
SDL_GetWindowSize(window, &w, &h);
printf("SDL_GetWindowSize: %d, %d\n", w, h);
resizeDone = false;
}
}
}
int main(int, char**) {
SDL_Init(SDL_INIT_VIDEO);
uint32_t window_flags = SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE;
window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
windowID = SDL_GetWindowID(window);
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
run();
SDL_Quit();
return 0;
}
Some window managers seems to ignore resize requests made while WM itself resizes window (e.g. while mouse button held). On contrary, SDL_GetWindowSize returns cached values, which in that specific case sometimes happens to be wrong.
I see no platform-independent way to achieve that, other than constantly calling SDL_SetWindowSize on each frame, just in case. It could be achieved using platform-specific APIs, though (like SDL_GetWindowSysWMInfo and then using Xlib).
On macOS, I have solved it like this:
cocoa.m:
#import <Cocoa/Cocoa.h>
void SetWindowRatio(void *window) {
NSWindow *win = (__bridge NSWindow*) window;
win.aspectRatio = NSMakeSize( 1280, 720 );
}
main.cpp:
#include <SDL.h>
#include <SDL_syswm.h>
extern "C" void SetWindowRatio(void *window);
// and later..
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(sdl.window, &wmInfo);
SetWindowRatio(wmInfo.info.cocoa.window);
Perhaps something similar could be done on Linux, only access different part of wmInfo.info. and call the native function?

how do you make a background in allegro with c++?

i'm fairly new at programming with allegro, and i wanna change the background color of my programs from something more pleasant than black haha :) can some one help please?
and just for a reference of what im doing
#include <allegro.h>
BITMAP* buffer;
BITMAP* bmp;
int cursor_x = 20;
int cursor_y = 20;
int getMouseInfo(){
if(mouse_b & 1){
cursor_x = mouse_x;
cursor_y = mouse_y;
return 1;
}
return 0;
}
void updateScreen(){
show_mouse(NULL);
circlefill ( buffer, cursor_x, cursor_y, 60, makecol( 0, 255 , 0));
draw_sprite( screen, buffer, 0, 0);
}
int main(){
allegro_init();
install_mouse();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
rectfill (
buffer = create_bitmap( 640, 480);
show_mouse(screen);
while( !key[KEY_ESC])
{
int switcher=1;
while(getMouseInfo())
{
updateScreen();
if(getMouseInfo()==0) switcher=0;
}
if(switcher==0) show_mouse(screen);
}
return 0;
}
END_OF_MAIN();
To create backgroud bitmap try this:
/* Make a bitmap in RAM. */
BITMAP *bmp = create_bitmap(SCR_X, SCR_Y);
then try this to clear bmp to some different color:
/* Clear the screen to red. */
clear_to_color(bmp, makecol(255, 0, 0));
or this to load bitmap from file:
bmp = load_bitmap("image.pcx", palette);
Then you just need to blit this bitmap with your screen - like this:
/* Blit bmp on the screen. */
blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
Draw a screen-sized rectangle that is the color you want the background to be. Or just use clear_bitmap to clear the screen.
#include <iostream>
using namespace std;
int main()
{
cout<<" In the world were gamers play MINECRAFT, where they creating everithing they can emagine ...\n";
cin.get();
return 0;
}