LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2008, 10:25 PM   #1
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Rep: Reputation: 15
Combine SDL and XOpenDisplay windows.


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;
        }
}
 
Old 01-11-2008, 02:29 PM   #2
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
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.

Any help would be greatly appreciated.

Thanks,
aatwell
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sourcecompiling Problem...The sdl-config script installed by SDL could not be found. deepclutch Debian 1 12-15-2005 12:15 PM
Trying to program an SDL application but cannot find the SDL.h file:SuSE 9.2&KDevelop pujolasdf Linux - Newbie 4 03-13-2005 07:50 AM
sdl-perl: disappearing windows jrtayloriv Programming 1 02-02-2005 12:55 AM
XOpenDisplay() fails JohnCain Programming 1 01-26-2005 10:42 PM
Making Windows in SDL fatherg Programming 2 08-19-2004 01:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration