ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Greetings,
Sorry for writing a book here, but the book I'm reading shows how to draw to a screen using XDraw by creating a screen using the XOpenDisplay command. It works great. The book also shows how to capture Keyboard events using a screen created by SDL_INIT_VIDEO. I need to have both in one screen where I can use Xdraw to draw the Screen and capture the keyboard and mouse events on the same screen. Below is a piece of the code. If you let the program run for about a minute--or less--you'll see both windows. The SDL one always seems to appear quick. The XOpenDisplay window will appear, but it'll take about 30 seconds, depending on your CPU. You'll notice that the Escape key will only work if the SDL window is active. If you activate the XOpenDisplay window or any other window, the program will not exit with the escape key. I'll like to make the XOpenDisplay window accept the SDL events.
Can anyone give me some know-how to accomplish this?
Thanks,
AJA
Code:
/*
Compile Using:
gcc -lX11 -lSDL -L/usr/X11R6/lib -L/usr/include/SDL -I/usr/X11R6/include -I/usr/include/SDL -o go.exe video.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
#include <X11/Xlib.h>
#include <curses.h>
int running=1;
void HandleKeyboardEvent(SDL_KeyboardEvent *key);
int main()
{ // Variables used for Video Output //
int hx; int hy; int ix; int iy;
// VIDEO USING XOPENDISPLAY TO ACCEPT XDRAW PIXELS //
Display *display;
Window window;
Screen *screen;
int myscreen; int x; int y; int td;
char *display_str = (char *) getenv("DISPLAY");
GC gc;
XGCValues gcv;
display = XOpenDisplay(display_str);
screen = DefaultScreenOfDisplay(display);
myscreen = DefaultScreen(display);
window = XCreateSimpleWindow (
display, RootWindowOfScreen(screen),
0, 0,
200, 200,
1, BlackPixel(display, DefaultScreen(display)),
WhitePixel(display, DefaultScreen(display)));
XClearWindow(display, window);
XMapRaised(display, window);
gcv.foreground = BlackPixel(display, myscreen);
gc = XCreateGC(display, window, GCForeground, &gcv);
// VIDEO USING SDL_INIT_VIDEO TO CAPTURE KEYBOARD EVENTS //
SDL_Surface *SDLscreen;
SDL_Event event;
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{ printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
SDLscreen = SDL_SetVideoMode(200, 200, 0, SDL_ANYFORMAT);
if (SDLscreen == NULL)
{ printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
// BEGIN MAIN LOOP //
while (running) {
while(SDL_PollEvent(&event))
{
// DRAW H //
hx=34; hy=34;
for(y=(hy+5); y<(hy+7); y++){for(x=hx; x<(hx+10); x++){XDrawPoint(display, window, gc, x, y);}}
for(x=hx; x<(hx+2); x++){for(y=hy; y<(hy+12); y++){XDrawPoint(display, window, gc, x, y);}}
for(x=(hx+8); x<(hx+10); x++){for(y=hy; y<(hy+12); y++){XDrawPoint(display, window, gc, x, y);}}
// DRAW I //
ix=50; iy=34;
for(x=ix; x<(ix+2); x++){for(y=iy; y<(iy+12); y++){XDrawPoint(display, window, gc, x, y);}}
// LOOK FOR KEYBOARD KEYUP EVENT //
switch(event.type)
{ case SDL_KEYDOWN:
break;
case SDL_KEYUP:
HandleKeyboardEvent(&event.key);
break;
case SDL_QUIT:
running=0;
break;
}
refresh; wrefresh;
}
}
// EXIT PROGRAM //
XFreeGC(display, gc);
XCloseDisplay(display);
}
void HandleKeyboardEvent(SDL_KeyboardEvent *key)
{ switch(key->keysym.sym)
{ case SDLK_LEFT:
break;
case SDLK_RIGHT:
break;
case SDLK_UP:
break;
case SDLK_DOWN:
break;
case SDLK_ESCAPE:
running=0;
break;
default:
break;
}
}
Okay... Maybe combining these two is not possible. Can someone please show me how to draw to an SDL screen? I'd need the background white and foreground black and be able to draw pixels on the SDL screen.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.