![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Xlib - Mapping Pixel Values to RGB Colors | thebin | High Level Programming | 1 | 03-21-2008 08:55 PM |
| kde blues | rinquisitor | UNIX Desktop for Dummies Questions & Answers | 1 | 05-11-2007 04:32 AM |
| ATI && XFree86 (Linux) | Esaia | UNIX Desktop for Dummies Questions & Answers | 1 | 12-24-2002 08:54 PM |
| Xlib: Invalid MIT-MAGIC-COOKIE-1 | errolg | UNIX for Dummies Questions & Answers | 1 | 07-25-2002 01:05 PM |
| NFS Blues | pcs7088 | UNIX for Dummies Questions & Answers | 2 | 07-16-2001 07:41 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
xlib blues (XFree86 - Linux)
My goal is to open a window in the X-server and plot a series of pixels. However, I find many examples I have tried to draw to a window fail and the window remains blank. Therefore, I am here requesting information. I do not know c, and am a beginner. Currently, I am using a mix-match of code, running against the gcc compiler finding errors and fixing them as I go along. I have experienced segmentation faults, but the following code produces a minimal amount of error.
Code:
/* window.c --This program opens a window on the display
* Use "gcc -o window window.c -L/usr/X11R6/lib -lX11
* to compile this code.
*/
#include <X11/X.h>
#include <X11/Xlib.h>
#include<stdio.h>
#define width 320
#define height 200
int main(void)
{
Display *display;
Window window, rootwindow;
int screen;
GC gc;
XImage *img=NULL;
int x;
int y;
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
gc = DefaultGC(display, screen);
rootwindow = RootWindow(display,screen);
window = XCreateSimpleWindow(display, rootwindow,
0, 0, width, height, 1, 0, 0);
XMapWindow(display, window);
XImage *xi;
unsigned long pixelvalue1;
xi = XCreateImage(display, 0, 8, ZPixmap,
0, malloc(width*height), width, height,
8, 0);
pixelvalue1 = 65535;
for (x = 0; x < width/2; x++)
{
for(y = 0; y < height/2; y++)
{
XPutPixel(xi, x, y, pixelvalue1);
}
}
printf("putting image\n");
XPutImage(display, window, gc, xi, 0, 0, 0, 0, width, height);
sleep (5);
return (0);
}
However, what actually happens is it opens a window with a black background, plots nothing, and I am left frustrated with little resources. I suspect the problem may lie in either XCreateImage, or XPutPixel commands. I suspect I have not got the right values for either command. Can anyone here offer a simple solution to my problem of wanting to plot pixels to a window? Finally - I would like to elabroate on my plans... I intend to further the pixel plotting using double buffering, for a simple animation routine I written in PHP graphics. As animation is not available in PHP, this is the reason for myself attempting c coding without any prior knowledge of the c language. However, I do have a basic grasp of BASIC (if, for, while etc), and constant/variable declaration (int, long, double etc). Please can someone enlighten me with a code snippet as to how I can accomplish my goal of plotting individual pixels to the screen. Much thanks, Alux. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Well...I'm
For some reason I cannot access any other color than blue. I would also like to apologise for posting a Linux question in the Unix forum. :/ However, my code has advanced and I would like to share the program I wanted to write. Code:
/*--------------------------------------------------*
*Open a window on the XFree86 system and draw to it.*
*Created by Robbie Dave after learning about Xlib.h *
*---------------------------------------------------*
Compile with: gcc -o dave dave.c -L/usr/X11R6/lib -lX11
*/
// Include the Xlib.h file so as to use X functions.
#include <X11/Xlib.h>
#define NIL (0) // A name for the void pointer
//This is the 'main' function in c programs.
int main(void) {
// Initialise the variables.
Window win;
int win_width = 640;
int win_height = 480;
int win_x;
int win_y;
int screen_num;
GC gc;
XImage *xi=NULL;
XColor system_color_1;
XColor exact_color;
Colormap davescol;
int x;
int y;
int c;
// Get a pointer (*display) to the X display.
Display *display = XOpenDisplay(NIL);
/*
Obtain the screen number using our *display pointer.
The display pointer does not need the asterisk (*).
*/
screen_num = DefaultScreen(display);
/*
Now we have the screen number and display pointer,
we get the GC (graphics context) of the X system.
*/
gc = DefaultGC(display, screen_num);
// I assume these are the window offsets set to 0.
win_x = win_y = 0;
// Create the window
win = XCreateSimpleWindow(display, RootWindow(display, screen_num), win_x, win_y, win_width, win_height, 0, BlackPixel(display, screen_num), BlackPixel(display, screen_num));
//Make the screen appear and flush events.
XMapWindow(display, win);
XFlush(display);
// Capture the screen for drawing.
xi = XGetImage(display, win, 0,0, win_width, win_height, AllPlanes, ZPixmap);
//Obtain a colormap to get colors.
davescol = DefaultColormap(display, screen_num);
// Enter a color into the colormap to use later.
Status rc = XAllocNamedColor(display, davescol, "red", &system_color_1, &exact_color);
//These two x, y for loops used to plot pixels to window.
for (x = 0; x < win_width; x++)
{
for(y = 0; y < win_height; y++)
{
//Plot a pixel.
XPutPixel(xi, x, y, exact_color.pixel);
/*Experiement with the x, y formula
for different animations.
*/
exact_color.pixel = ((x*x)+(y*y))*0.05;
if (exact_color.pixel > 65535)
{exact_color.pixel=65535;}
}
}
//This for loop is used for animation.
for(c = 8; c < 64; c++)
{
//Animate the colors
XAddPixel(xi, c);
//Try to control the animation.
XFlush(display);
XSync(display, 1);
/*
After plotting to every pixel in the window,
actually display what you've drawn.
*/
XPutImage(display, win, gc, xi, 0, 0, 0, 0, win_width, win_height);
}
//Clean up the memory a bit, but not much.
XDestroyImage(xi);
XCloseDisplay(display);
return (0);
}
EDIT: After reading yet another Xlib tutorial I discovered how to allocate colors. I still do not understand how to utilize these colors for above animation, but I am getting there... Ah vell... Last edited by Alux; 12-03-2005 at 12:41 AM. |
|||
| Google The UNIX and Linux Forums |