Open MarcoQin opened 4 years ago
Does this example work for you? https://stackoverflow.com/questions/34425799/use-root-x11-window-as-the-main-sdl2-window
It should just paint the screen one solid color.
No, it's not work, too. And the example result in the same error.
But X11 window is runnable, following snippet is working:
#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h> // I include this to test return values the lazy way
#include <unistd.h> // So we got the profile for 10 seconds
#define NIL (0) // A name for the void pointer
main()
{
Display *dpy = XOpenDisplay(NIL);
assert(dpy);
Window w = XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0,
CopyFromParent, CopyFromParent, CopyFromParent,
NIL, 0);
XMapWindow(dpy, w);
XFlush(dpy);
sleep(10);
}
Snipet that not work:
#include <SDL2/SDL.h>
#include <X11/Xlib.h>
#include <stdio.h>
// clang -lSDL2 -lX11 -I/usr/include/SDL2 -Weverything x11.c -o x11
int main(void)
{
Display *x11_d;
int x11_s;
Window x11_w;
SDL_Window *w;
SDL_Renderer *r;
x11_d = XOpenDisplay(NULL);
if(!x11_d) {
fprintf(stderr, "couldn't open display\n");
return 1;
}
/* x11_s = DefaultScreen(x11_d); */
/* x11_w = RootWindow(x11_d, x11_s); */
x11_w = XCreateWindow(x11_d, DefaultRootWindow(x11_d), 0, 0,
200, 100, 0,
CopyFromParent, CopyFromParent, CopyFromParent,
0, 0);
XMapWindow(x11_d, x11_w);
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "couldn't initialize SDL: %s\n", SDL_GetError());
return 1;
}
w = SDL_CreateWindowFrom((void *)x11_w);
/* XCloseDisplay(x11_d); */
if(!w) {
fprintf(stderr, "couldn't attach to the root X11 window: %s\n", SDL_GetError());
return 1;
}
printf("here haha\n");
r = SDL_CreateRenderer(w, -1, 0);
SDL_SetRenderDrawColor(r, 255, 0, 0, 255);
SDL_RenderClear(r);
SDL_RenderPresent(r);
SDL_Delay(5700);
SDL_Quit();
return 0;
}
This looks to be a similar error:
https://stackoverflow.com/questions/30185781/sdl-createwindowfrom-with-qt-widget-under-mac-os
Maybe the problem lies with how the window is being created. Since this is OSX specific I am not of much help
Anyway, thank you! I will try again when I have time.
Build command:
gcc -std=c99 -O2 -Wall -Wextra -Wpedantic main.c -I/usr/X11R6/include -L/usr/X11R6/lib -lSDL2 -lX11 -o paperview
Run command:
./paperview cyber 5
Output: