X11 Lib Sinus Drawer

Hello fellas,

im making a program with the xlib to draw a sinus function but i cant make it work, could you have a look at my code?

http://pastebin.com/TwETyRzV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <X11/Xlib.h>
#include <unistd.h> //Sleep();
#include <math.h> //Sinus: sin();
#include <stdio.h> // getchar(); etc...

int main(){
	//Creates Window
	Display *display;
	Window window, rootwindow;
	int screen;

	display = XOpenDisplay(NULL);
	screen = DefaultScreen(display);
	rootwindow = RootWindow(display,screen);

	printf("Window Dimensions: ");
	int width,height;
	scanf("%d %d",&width,&height);
	
	//Sets foreground and background of the window
	GC gc;
	int screen_num = DefaultScreen(display);


	bg:
	printf("Foreground Color (white=0,black=1): ");
	int fcolor;
	scanf("%d",&fcolor);
	if(fcolor == 0){
		XSetForeground(display,gc,WhitePixel(display,screen_num));
		XSetBackground(display,gc,BlackPixel(display,screen_num));
		window = XCreateSimpleWindow(display, rootwindow,0, 0, width, height, 1,BlackPixel(display,screen_num),WhitePixel(display,screen_num));
	} else if(fcolor == 1){
		XSetForeground(display,gc,BlackPixel(display,screen_num));
		XSetBackground(display,gc,WhitePixel(display,screen_num));
		window = XCreateSimpleWindow(display, rootwindow,0, 0, width, height, 1,WhitePixel(display,screen_num),BlackPixel(display,screen_num));
	} else {
		printf("Error! Goto to fcolor\n");
		goto bg;
	}

	//window = XCreateSimpleWindow(display, rootwindow,0, 0, width, height, 1, 0, 0);
	XMapWindow(display, window);
	XFlush(display);
	XGCValues values;
	gc = XCreateGC(display,window,0,&values);

	//creates lines definitions

	int line_width = 2;
	int line_style = LineSolid;
	int cap_style = CapButt;
	int join_style = JoinBevel;

	XSetLineAttributes(display,gc,line_width,line_style,cap_style,join_style);
	XSetFillStyle(display,gc,FillSolid); //Fill style for GC

	//XSetForeground(display,gc,WhitePixel(display,screen_num));

	//End of creating window...

	double r,a;
	printf("Repeat: ");
	scanf("%d",&r);
	printf("Amount: ");
	scanf("%d",&a);

	for(double yi=0;yi<r;yi++){
		yi=sin(yi);
		yi=yi*a;
		for(double xi=0;xi<r;xi++){
			xi=sin(xi);
			xi=xi*a;
			XDrawPoint(display,window,gc,xi,yi);
		}
	}
	
	XDrawLine(display,window,gc,500,5,400,34); //This is a test, to see if the program also prints this line... but it doesn't

	getchar();
	sleep(5);
	XCloseDisplay(display);
	return 0;
}


Thank you
Last edited on
You don't appear to have created a graphics context.
http://math.msu.su/~vvb/2course/Borisenko/CppProjects/GWindow/xintro.html
Here is the "update" of my code..... but damn it doesnt do a thing.... and the code of before showed a window and this dont.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* include the X library headers */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>

/* include some silly stuff */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* here are our X variables */
Display *dis;
int screen;
Window win;
GC gc;

/* here are our X routines declared! */
void init_x();
void close_x();
void redraw();

main () {
	XEvent event;		/* the XEvent declaration !!! */
	KeySym key;		/* a dealie-bob to handle KeyPress Events */	
	char text[255];		/* a char buffer for KeyPress Events */

	init_x();

	double r,a;
	printf("Repeat: ");
	scanf("%d",&r);
	printf("Amount: ");
	scanf("%d",&a);

	for(double yi=0;yi<r;yi++){
		yi=sin(yi);
		yi=yi*a;
		for(double xi=0;xi<r;xi++){
			xi=sin(xi);
			xi=xi*a;
			XDrawPoint(dis,win,gc,xi,yi);
		}
	}

	getchar();
	sleep(5);
	close_x();
}

void init_x() {
/* get the colors black and white (see section for details) */        
	unsigned long black,white;

	dis=XOpenDisplay((char *)0);
   	screen=DefaultScreen(dis);
	black=BlackPixel(dis,screen),
	white=WhitePixel(dis, screen);
	printf("Window height, width: ");
	int height,width;
	scanf("%d %d",&height,&width);
   	win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,height, width, 5,black, white);
	XSetStandardProperties(dis,win,"x11lib","template",None,NULL,0,NULL);
	XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);
        gc=XCreateGC(dis, win, 0,0);
	XSetBackground(dis,gc,white);
	XSetForeground(dis,gc,black);
	XClearWindow(dis, win);
	XMapRaised(dis, win);
};

void close_x() {
	XFreeGC(dis, gc);
	XDestroyWindow(dis,win);
	XCloseDisplay(dis);
	exit(1);
};

void redraw() {
	XClearWindow(dis, win);
};
Hey, did you get this sorted OP? I have a very similar issue. I checked out Ivor Horton's great guide here http://simplenewz.com/2014-09-05/Mainstream/feed/1626 but no joy in finding an answer yet.
Topic archived. No new replies allowed.