Hi.
How do I use XDrawLine() on the SDL window in my program?
This is what we use in Sid Meier’s Alpha Centauri:
#include “SDL_video.h”
#include “SDL_syswm.h”
extern SDL_Surface *sdl_screen;
bool X11_line(int x_to, int y_to, int x_from, int y_from, int color, int thickness, int style)
{
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if ( SDL_GetWMInfo(&info) > 0 ) {
if ( info.subsystem == SDL_SYSWM_X11 ) {
Display *dpy;
Window win;
XWindowAttributes attributes;
GC gc;
int do_colorfree;
XColor xcolor;
XGCValues values;
unsigned long valuemask;
info.info.x11.lock_func();
dpy = info.info.x11.display;
win = info.info.x11.window;
/* Allocate the color we want in the window colormap */
XGetWindowAttributes(dpy, win, &attributes);
xcolor.pixel = color;
xcolor.red = sdl_screen->format->palette->colors[color].r << 8;
xcolor.green = sdl_screen->format->palette->colors[color].g << 8;
xcolor.blue = sdl_screen->format->palette->colors[color].b << 8;
xcolor.flags = (DoRed|DoGreen|DoBlue);
do_colorfree = XAllocColor(dpy, attributes.colormap, &xcolor);
/* Set the values for the graphic context */
valuemask = 0;
valuemask |= GCForeground;
values.foreground = xcolor.pixel;
valuemask |= GCLineWidth;
values.line_width = thickness;
valuemask |= GCLineStyle;
values.line_style = LineSolid;
gc = XCreateGC(dpy,RootWindow(dpy, DefaultScreen(dpy)),valuemask,&values);
/* Finally draw the line */
XDrawLine(dpy, win, gc, x_from, y_from, x_to, y_to);
XFreeGC(dpy, gc);
if ( do_colorfree ) {
XFreeColors(dpy, attributes.colormap, &xcolor.pixel, 1, 0);
}
info.info.x11.unlock_func();
return true;
}
}
return false;
}
However, I don’t recommend that you do this unless you absolutely have to.
This code won’t draw lines on the Linux console (or any other environment),
and breaks some Win32 X server implementations.
It would be better to find a general line implementation for the framebuffer.
See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software