|
Post by vovchik on Jan 22, 2014 13:11:50 GMT 1
Dear Alex and Joe, Very nice work! This is useful. With kind regards, vovchik
|
|
|
Post by bigbass on Jan 22, 2014 16:47:57 GMT 1
Hey guys I updated the last code snippet the link name is the same though it now shows all the terminal info in the title too of course doing this in xlib makes you see where gtk comes in on top and does a lot of grunt work then you see and respect how much work goes into HUG / GFX to make the coding part look like basic syntax surprisingly this is fun anyway this is good to see how things work it is helping me a lot to understand what is really happening with the widgets on the first layer what xlibs job is what gtk does and where HUG and GFX make the user experience uncluttered Alex I added a safety so if you make a function of this it wont get stuck in a loop if you press escape it will force a break quiting the program Joe
|
|
|
Post by alexfish on Jan 22, 2014 18:38:53 GMT 1
Hi joe , Yes we need it + also need the expose event for the window , if one is to Render to the Window then can only do When the Window is Displayed have do a Visual using Cairo , possible start of may be a Button.. could not get this into last demo 3.. since the events are continually been called so modded the version 2 . so possible try the bits in ver3 it be cliché since the win is continally rendering the context.. other than that , it the render works BR Alex PS :: 'BaCon TK' = BTK
'---simple-window2 + cairo - demonstrate creation of a simple window with cairo. PRAGMA LDFLAGS `pkg-config --cflags --libs x11 --libs cairo` PRAGMA INCLUDE X11/Xlib.h PRAGMA INCLUDE X11/Xutil.h PRAGMA INCLUDE cairo.h PRAGMA INCLUDE cairo-pdf.h PRAGMA INCLUDE cairo-ps.h PRAGMA INCLUDE cairo-xlib.h
'Cr$= "libcairo.so.2" 'IMPORT cairo_xlib_surface_create(long,long,long,int,int) FROM Cr$ TYPE long 'IMPORT cairo_xlib_surface_create(long,long,void*,int,int) FROM Cr$ TYPE long 'IMPORT cairo_create() FROM Cr$ TYPE long 'IMPORT cairo_rectangle(long,double,double,double,double) FROM Cr$ TYPE void 'IMPORT cairo_set_source_rgb(long,double,double,double) FROM Cr$ TYPE void 'IMPORT cairo_fill(long) FROM Cr$ TYPE void 'IMPORT cairo_move_to(long,double,double) FROM Cr$ TYPE void 'IMPORT cairo_show_text(long,char*) FROM Cr$ TYPE void 'IMPORT cairo_show_page(long) FROM Cr$ TYPE void 'IMPORT cairo_destroy(long) FROM Cr$ TYPE void PROTO DefaultScreen PROTO DisplayWidth PROTO DisplayHeight PROTO XCreateSimpleWindow PROTO RootWindow PROTO XMapWindow PROTO XSync PROTO XCloseDisplay PROTO XStoreName PROTO XSelectInput PROTO XNextEvent PROTO XEvent
DECLARE xevent TYPE XEvent 'XNextEvent(dpy, &e '@ Cairo bits PROTO cairo_create PROTO cairo_rectangle PROTO cairo_set_source_rgb PROTO cairo_fill PROTO cairo_move_to PROTO cairo_set_source_rgb PROTO cairo_show_text PROTO cairo_show_page PROTO cairo_destroy PROTO cairo_surface_destroy PROTO cairo_xlib_surface_create '---pointer to X Display structure DECLARE display TYPE Display* '--- number of screen to place the window on DECLARE screen_num TYPE int '---pointer to the newly created window DECLARE win TYPE Window '---height and width of the X display. DECLARE display_width, display_height TYPE unsigned int '---height and width for the new window. DECLARE width, height TYPE unsigned int '---location of the window's top-left corner. DECLARE win_x, win_y TYPE unsigned int '--width of window's border. DECLARE win_border_width DECLARE display_name TYPE char* SUB paint (cairo_surface_t *cs,double xpos,double ypos , double xwidth,double yheight)
LOCAL *c TYPE cairo_t c=cairo_create(cs) cairo_rectangle(c, xpos, ypos,xwidth, yheight) cairo_set_source_rgb(c,0.705, 0.196, 0.18) cairo_fill(c) cairo_move_to(c, xpos+20, ypos+yheight/2) cairo_set_source_rgb(c, 1.0, 1.0, 0.0) cairo_show_text(c, "Hello World! From BaCon") cairo_show_page(c) cairo_destroy(c) END SUB '---address of the X display. display_name = getenv("DISPLAY") display = XOpenDisplay(display_name) '--- get the geometry of the default screen for our display. screen_num = DefaultScreen(display) display_width = DisplayWidth(display, screen_num) display_height = DisplayHeight(display, screen_num) '---make the new window occupy 1/9 of the screen's size. width = (display_width / 3) height = (display_height / 3) '--- the window should be placed at the top-left corner of the screen. win_x = 0 win_y = 0 '--- the window's border shall be 2 pixels wide. win_border_width = 2 '--- create a simple window, as a direct child of the screen's '--- root window. Use the screen's white color as the background '--- color of the window. Place the new window's top-left corner '--- at the given 'x,y' coordinates. DECLARE *cs TYPE cairo_surface_t win = XCreateSimpleWindow(display, RootWindow(display, screen_num), \ win_x, win_y, width, height, win_border_width, \ BlackPixel(display, screen_num), \ WhitePixel(display, screen_num)) cs=cairo_xlib_surface_create(display, win, DefaultVisual(display, 0),width, height) XStoreName(display, win, "Hello From Bacon X11+ Cairo") 'TODO @ Need some event bits here
'--- make the window actually appear on the screen.
XMapWindow(display, win) '--- flush all pending requests to the X server, and wait until '--- they are processed by the X server. XSync(display, False) '@ Loop DOLOP= 1 WHILE DOLOP DO '@ NEED EVENT TRAP to exit this joe , else will have leaky paint(cs,20.0,20,200,50) WEND
cairo_surface_destroy(cs) '--- close the connection to the X server. XCloseDisplay(display)
Attachments:
|
|
|
Post by bigbass on Jan 22, 2014 19:27:41 GMT 1
Hey Alex That looks great! it is actually better you added it to the simple window so we can document how to add cairo P.S " BLT" BaCon's light toolkit A tasty sandwich too I will look at adding events tonight when I get home but here's where to look if you want to give it a try I tried to group the code together so it could be cut and pasted then made pretty later =====Add a name to the window======== code snippet ===================================== or '----user input--------- code snippet '---------------------- Joe I updated the code again to have a conditional break Simple-window3.bac '--- CONCAT these like in bacon to get it into the title name KEYCODE$ = "key code " & STR$(xk->keycode) & " " & STR$(xb->x_root) & ", "& STR$(xb->y_root) '--- this has a string the keycode a comma and the x,y position window_name = KEYCODE$ '---- translate the window name string into an X property. rc = XStringListToTextProperty(&window_name, 1, &window_name_property) '--- set the window name of our window. XSetWMName(display, win, &window_name_property) KEY = xk->keycode
|
|
|
Post by bigbass on Jan 23, 2014 16:42:51 GMT 1
Hey guys As much fun as it is to step forward sometimes taking a step backward is better I should have started with this example its still not to late keeping things simple so we can advance even faster I ported this code Window_creation X11 to BaCon it just needed a XFLush to exit cleanly Xlibhello.bacHey Alex I like this simple event trap for the demo better ! and this simple flow of INCLUDE, PROTO, DECLARE, main, loop ,flush ,closeMuch easier to plug in the code and modify things P.S I was surprised that I didn't need to PROTO strlen the BaCon compiler added it automatically Also I don't see by the examples I have seen how XCB is so called "easier" with BaCon we can get the structs using just Xlib Joe
|
|
|
Post by alexfish on Jan 23, 2014 19:40:20 GMT 1
Hi Joe
looking good
on the expose event just wondering if that is rendering repetitively in the loop EDI:: just tested appear not to be the case , but the 'xexpose.count' can be useful.. on the cairo demo using
IF (e.type == Expose && e.xexpose.count<1) THEN PRINT "ok" paint(cs,20.0,20,200,50) END IF
the render is only update once when the window is realised , so can see from the terminal "ok" once so wondering if try and apply same to Your demo.
IE
WHILE (1) XNextEvent(d, &e) IF (e.type == Expose && e.xexpose.count<1) THEN PRINT "Draw Now" XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10) XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg)) END IF IF (e.type == KeyPress) THEN BREAK ENDIF WEND
HTH Alex
|
|
|
Post by alexfish on Jan 24, 2014 2:48:07 GMT 1
Hi Joe
Thought about playing around with the event bits posted + see what some of . are
so done a X11 with Cairo + moving the Logo around the screen , important bit here is the 'XClearWindow' which will clear the screen ready for a re-draw
Also used ALIAS to look a bit like HUG ....
BR Alex
'---simple-window2 + cairo - demonstrate creation of a simple window with cairo. PRAGMA LDFLAGS `pkg-config --cflags --libs x11 --libs cairo` PRAGMA INCLUDE X11/Xlib.h PRAGMA INCLUDE X11/Xutil.h PRAGMA INCLUDE cairo.h PRAGMA INCLUDE cairo-pdf.h PRAGMA INCLUDE cairo-ps.h PRAGMA INCLUDE cairo-xlib.h
PROTO DefaultScreen PROTO DisplayWidth PROTO DisplayHeight PROTO XCreateSimpleWindow ALIAS WINDOW PROTO RootWindow PROTO XMapWindow PROTO XSync PROTO XCloseDisplay ALIAS QUIT PROTO XStoreName PROTO XSelectInput PROTO XNextEvent PROTO XEvent PROTO MapNotify PROTO XClearWindow PROTO XLookupKeysym PROTO XKeysymToString PROTO XFlush '@ Cairo bits PROTO cairo_create PROTO cairo_rectangle ALIAS SQUARE PROTO cairo_set_source_rgb PROTO cairo_fill PROTO cairo_move_to PROTO cairo_set_source_rgb PROTO cairo_show_text ALIAS MARK PROTO cairo_show_page ALIAS DRAW PROTO cairo_destroy PROTO cairo_surface_destroy PROTO cairo_xlib_surface_create ALIAS CANVAS PROTO cairo_set_font_size
' DECLARES DECLARE xevent TYPE XEvent DECLARE display TYPE Display* DECLARE screen_num TYPE int DECLARE win TYPE Window DECLARE display_width, display_height TYPE unsigned int DECLARE width, height TYPE unsigned int DECLARE win_x, win_y TYPE unsigned int DECLARE win_border_width DECLARE display_name TYPE char* DECLARE KEY TYPE int DECLARE *cs TYPE cairo_surface_t DECLARE e TYPE XEvent '@ bits for Display display_name = getenv("DISPLAY") display = XOpenDisplay(display_name) screen_num = DefaultScreen(display) display_width = DisplayWidth(display, screen_num) display_height = DisplayHeight(display, screen_num) width = (display_width / 3) height = (display_height / 3) win_x = 0 win_y = 0 win_border_width = 2
SUB LOGO(cairo_surface_t *cs,double xpos,double ypos , double xwidth,double yheight) LOCAL *c TYPE cairo_t c=cairo_create(cs) cairo_set_font_size(c,12.0)
SQUARE(c, xpos, ypos,xwidth, yheight) cairo_set_source_rgb(c,0.705, 0.196, 0.18) cairo_fill(c) cairo_move_to(c, xpos+20, ypos+yheight/2) cairo_set_source_rgb(c, 1.0, 1.0, 0.0) cairo_show_text(c, "Hello World! From BaCon") DRAW(c) cairo_destroy(c) END SUB
SUB Controls(cairo_surface_t *cs) LOCAL *c TYPE cairo_t c=cairo_create(cs) cairo_set_font_size(c,15.0) cairo_set_source_rgb(c,0.705, 0.196, 0.18) cairo_move_to(c, 20, 20) MARK(c, ":::::::KEYS ::::::") cairo_move_to(c, 20, 80) MARK(c, "UP ARROW KEY") cairo_move_to(c, 20, 130) MARK(c, "DOWN ARROW KEY") cairo_move_to(c, 20, 180) cairo_show_text(c, "LEFT ARROW KEY") cairo_move_to(c, 20, 230) MARK(c, "RIGHT ARROW KEY") cairo_move_to(c, 20, 280) MARK(c, "ESP KEY = QUITE") DRAW(c) cairo_destroy(c)
END SUB win= WINDOW(display, RootWindow(display, screen_num), \ win_x, win_y, width, height, win_border_width, \ BlackPixel(display, screen_num), \ WhitePixel(display, screen_num)) XSelectInput(display, win,StructureNotifyMask | ExposureMask | KeyPressMask) cs=CANVAS(display, win, DefaultVisual(display, 0),width, height) XStoreName(display, win, "X11+ Cairo Press Esc to exit") XMapWindow(display, win) XSync(display, False)
my_xpos =0.0 my_ypos=0.0
WHILE (1)
XNextEvent(display, &e) SELECT e.type CASE MapNotify PRINT "MapNotify" CASE Expose PRINT "Expose" Controls(cs) CASE ConfigureNotify PRINT "ConfigureNotify"
CASE KeyPress KEY=(XKeycodeToKeysym(display, e.xkey.keycode, 0)) PRINT "key : " ,KEY IF KEY = 65362 THEN DECR my_ypos , 10.0 IF KEY = 65364 THEN INCR my_ypos , 10.0 IF KEY = 65361 THEN DECR my_xpos , 10.0 IF KEY = 65363 THEN INCR my_xpos , 10.0 '@ Esc Key to quit IF KEY = 65307 THEN XFlush(display) BREAK END IF
'@ clear the screen and redraw the rect and text XClearWindow(display, win) LOGO(cs,my_xpos,my_ypos,200,50) END SELECT
WEND
PRINT "QUITING" '@ clear all XClearWindow(display, win) cairo_surface_destroy(cs) '--- close the connection to the X server. QUIT(display)
|
|
|
Post by bigbass on Jan 26, 2014 3:22:22 GMT 1
I had some problems before to get a working simple cairo example going on my box so this is a simple one to test with to make sure you have all headers getting read first before moving on to more complex apps P.S I sent you a PM Alex This takes a screenshot of your desktop and saves it in the same folder called test.png remember no gtk used here '---cairo-screenshot.bac - demonstrate embedding cairo. '--- dependencies are libcairo2-dev, libx11-dev PRAGMA LDFLAGS `pkg-config --cflags --libs x11 --libs cairo` PRAGMA INCLUDE X11/Xlib.h
PRAGMA INCLUDE /usr/include/cairo/cairo.h PRAGMA INCLUDE /usr/include/cairo/cairo-xlib.h
PROTO XOpenDisplay PROTO DefaultScreen PROTO DefaultRootWindow PROTO cairo_xlib_surface_create PROTO DefaultVisual PROTO DisplayWidth PROTO DisplayHeight PROTO cairo_surface_write_to_png PROTO cairo_surface_destroy
DECLARE disp TYPE Display* DECLARE root TYPE Window DECLARE surface TYPE cairo_surface_t* DECLARE scr TYPE int
disp = XOpenDisplay(NULL) scr = DefaultScreen(disp) root = DefaultRootWindow(disp)
surface = cairo_xlib_surface_create(disp, root, \ DefaultVisual(disp, scr),DisplayWidth(disp, scr), \ DisplayHeight(disp,scr)) cairo_surface_write_to_png(surface,"test.png") cairo_surface_destroy(surface)
|
|
|
Post by bigbass on Jan 26, 2014 4:23:08 GMT 1
Hey Alex!
Cool! I finally got your app to compile I really like what you did with this demo !
This a great advancement with cairo's drawing power
A big thanks
This is why I couldn't get it to compile
I did a lot of searching for a fix and here this works on mint just add and replace notice the function name changes and it gets one more argument so it doesn't just drop in cleanly
PROTO XkbKeycodeToKeysym
KEY=(XkbKeycodeToKeysym(display, e.xkey.keycode, 0,0))
Thanks again for your code sorry it took me so long to get cairo working on my box
Joe
|
|
|
Post by bigbass on Jan 26, 2014 9:02:15 GMT 1
Here is a simple drawing app with just Xlib (I hope Alex can simulate this with cairo it would look much better)
click left mouse move mouse click left mouse again to connect the line
quit by pressing any key
considering this is just xlib the code snippet is small
'----ported and modified xlines.c for BaCon by bigbass' '--- xlines.bac simple drawing app with left mouse clicks press any key to quit
PRAGMA LDFLAGS `pkg-config --cflags --libs x11` PRAGMA INCLUDE X11/Xlib.h
PROTO RootWindow PROTO DefaultScreen PROTO BlackPixel PROTO WhitePixel PROTO XCreateSimpleWindow PROTO XCreateGC PROTO XSelectInput PROTO XMapWindow PROTO XWindowEvent PROTO XClearWindow PROTO XDrawPoint PROTO XDrawLine PROTO XDestroyWindow
PROTO XFlush PROTO XCloseDisplay
DECLARE i,j TYPE int DECLARE display TYPE Display* DECLARE root, window TYPE Window DECLARE fgcolor, bgcolor TYPE long DECLARE screen, pointx, pointy TYPE int
eventmask = KeyPressMask|ExposureMask|ButtonPressMask
DECLARE event TYPE XEvent DECLARE gcval TYPE XGCValues DECLARE draw TYPE GC FirstPt = 1 display = XOpenDisplay(0) root = RootWindow(display,screen = DefaultScreen(display)) fgcolor = BlackPixel(display,screen) bgcolor = WhitePixel(display,screen)
window = XCreateSimpleWindow(display,root,0,0,200,200,2,fgcolor,bgcolor)
gcval.foreground = fgcolor gcval.background = bgcolor draw = XCreateGC(display,window,GCForeground|GCBackground,&gcval)
XSelectInput(display,window,eventmask) XMapWindow(display,window)
WHILE (1) XWindowEvent(display,window,eventmask,&event) SELECT event.type CASE Expose: XClearWindow(display,window) CASE ButtonPress: IF (FirstPt) THEN FirstPt=0 pointx = event.xbutton.x pointy = event.xbutton.y XDrawPoint(display,window,draw,pointx,pointy) ELSE FirstPt=1 XDrawLine(display,window,draw,pointx,pointy, \ event.xbutton.x, event.xbutton.y) END IF CASE KeyPress: XDestroyWindow(display, window) XFlush(display) XCloseDisplay(display) BREAK DEFAULT: END SELECT WEND
|
|
|
Post by bigbass on Jan 26, 2014 17:02:04 GMT 1
Here is the demo by Alex with cairo with the fix for mint Simple-window2-cairo.bacHey Alex I added a string function so we get the actual key press names Thanks again for this great demo with cairo some cool things can now be done Joe
|
|
|
Post by bigbass on Jan 26, 2014 17:02:59 GMT 1
removed a double post
|
|
|
Post by alexfish on Jan 26, 2014 18:39:39 GMT 1
Hi Joe
EDIT :: have set same up with Client Protcol for the WM 'window manager' . this allows clean EXIT from the user deleting the window This also possible best Exit Method , when not using Terminal , See Second Demo.
Have done the cairo routine to draw pixel and line , based on the orignal simple window demo, headers = same , but users can strip if want
also users will have to check the path of where the cairo .h files are on the system , look at above posts by Joe as regards the pixel have used rect/ but have set to 4x4 easy to see, user can use 1x1 . as note the line width 'cairo_set_line_width(cr,width) see how fine line can be using cairo.
BR Alex
'---simple-window + cairo - demonstrate creation of a simple drawing with cairo.
PRAGMA LDFLAGS `pkg-config --cflags --libs x11 --libs cairo` PRAGMA INCLUDE X11/Xlib.h PRAGMA INCLUDE X11/Xutil.h PRAGMA INCLUDE cairo.h PRAGMA INCLUDE cairo-pdf.h PRAGMA INCLUDE cairo-ps.h PRAGMA INCLUDE cairo-xlib.h
PROTO DefaultScreen PROTO DisplayWidth PROTO DisplayHeight PROTO XCreateSimpleWindow PROTO RootWindow PROTO XMapWindow PROTO XSync PROTO XCloseDisplay PROTO XStoreName PROTO XSelectInput PROTO XNextEvent PROTO XEvent PROTO MapNotify PROTO XClearWindow PROTO XLookupKeysym PROTO XkbKeycodeToKeysym PROTO XKeysymToString PROTO XFlush PROTO XSync PROTO XClearArea
PROTO cairo_xlib_surface_create PROTO cairo_xlib_surface_create_for_bitmap PROTO cairo_xlib_surface_set_size PROTO cairo_xlib_surface_get_display PROTO cairo_xlib_surface_get_screen PROTO cairo_xlib_surface_set_drawable PROTO cairo_xlib_surface_get_drawable PROTO cairo_xlib_surface_get_visual PROTO cairo_xlib_surface_get_width PROTO cairo_xlib_surface_get_heigh PROTO cairo_xlib_surface_get_depth
PROTO cairo_create PROTO cairo_rectangle PROTO cairo_set_source_rgb PROTO cairo_fill PROTO cairo_move_to PROTO cairo_set_source_rgb PROTO cairo_show_text PROTO cairo_show_page PROTO cairo_destroy PROTO cairo_surface_destroy PROTO cairo_set_font_size PROTO cairo_line_to PROTO cairo_save PROTO cairo_set_line_width PROTO cairo_stroke ' DECLARES DECLARE xevent TYPE XEvent DECLARE display TYPE Display* DECLARE screen_num TYPE int DECLARE win TYPE Window DECLARE win2 TYPE Window DECLARE display_width, display_height TYPE unsigned int DECLARE width, height TYPE unsigned int DECLARE win_x, win_y TYPE unsigned int DECLARE win_border_width DECLARE display_name TYPE char* DECLARE KEY TYPE int DECLARE *cs TYPE cairo_surface_t DECLARE e TYPE XEvent
display_name = getenv("DISPLAY") display = XOpenDisplay(display_name)
screen_num = DefaultScreen(display)
display_width = DisplayWidth(display, screen_num) display_height = DisplayHeight(display, screen_num) width = (display_width / 3) height = (display_height / 3) win_x = 0 win_y = 0 win_border_width = 2
SUB PIXEL(cairo_surface_t *cs,double xpos,double ypos)
LOCAL *c TYPE cairo_t c=cairo_create(cs) cairo_set_line_width(c,2.0) cairo_rectangle(c, xpos, ypos,4.0, 4.0) cairo_set_source_rgb(c,0.705, 0.196, 0.18) cairo_fill(c) cairo_show_page(c) cairo_destroy(c)
END SUB
SUB LINE(cairo_surface_t *cs,double xpos,double ypos,double xpos2,double ypos2)
LOCAL *c TYPE cairo_t c=cairo_create(cs) cairo_set_line_width(c,1.0) cairo_move_to(c, xpos, ypos) cairo_set_source_rgb(c,0.705, 0.196, 0.18) cairo_line_to(c,xpos2,ypos2) cairo_stroke(c) cairo_show_page(c) cairo_destroy(c)
END SUB
SUB Controls(cairo_surface_t *cs) LOCAL *c TYPE cairo_t c=cairo_create(cs) cairo_set_font_size(c,15.0) cairo_set_source_rgb(c,0.705, 0.196, 0.18) cairo_move_to(c, 20, 20) cairo_show_text(c, "Mouse Point And Click ____ ESP KEY = QUITE") cairo_show_page(c) cairo_destroy(c) END SUB
win= XCreateSimpleWindow(display, RootWindow(display, screen_num), \ win_x, win_y, width, height, win_border_width, \ BlackPixel(display, screen_num), \ WhitePixel(display, screen_num)) XSelectInput(display, win,StructureNotifyMask | ExposureMask | KeyPressMask|ButtonPressMask) cs=cairo_xlib_surface_create(display, win, DefaultVisual(display, 0),width, height) XStoreName(display, win, "X11+ Cairo Press Esc to exit") XMapWindow(display, win) FirstPt = 1
WHILE (1)
XNextEvent(display, &e) SELECT e.type CASE MapNotify PRINT "MapNotify" CASE Expose PRINT "Expose" Controls(cs) CASE KeyPress 'TODO XkbKeycodeToKeysym . Depend on xlib chose 'KEY=(XKeycodeToKeysym(display, e.xkey.keycode, 0)) KEY=(XkbKeycodeToKeysym(display, e.xkey.keycode, 0,0)) PRINT "key : " ,KEY '@ Esc Key to quit IF KEY = 65307 THEN XFlush(display) XClearWindow(display, win) cairo_surface_destroy(cs) XCloseDisplay(display) BREAK END IF
CASE ButtonPress: IF (FirstPt) THEN FirstPt=0 XClearWindow(display, win) pointx = e.xbutton.x pointy = e.xbutton.y PRINT "Point X : " , pointx PIXEL(cs,pointx,pointy) ELSE PRINT "Line" XClearWindow(display, win) FirstPt=1 LINE(cs,pointx,pointy, e.xbutton.x, e.xbutton.y) END IF
END SELECT
WEND
PRINT "BYE"
Clean way to Exit from window when delete ico is clicked
'---simple-window + cairo - demonstrate creation of a simple drawing with cairo.using WM messaging Protocols
PRAGMA LDFLAGS `pkg-config --cflags --libs x11 --libs cairo` PRAGMA INCLUDE X11/Xlib.h PRAGMA INCLUDE X11/Xutil.h PRAGMA INCLUDE cairo.h PRAGMA INCLUDE cairo-pdf.h PRAGMA INCLUDE cairo-ps.h PRAGMA INCLUDE cairo-xlib.h
PROTO DefaultScreen PROTO DisplayWidth PROTO DisplayHeight PROTO XCreateSimpleWindow PROTO RootWindow PROTO XMapWindow PROTO XSync PROTO XCloseDisplay PROTO XStoreName PROTO XSelectInput PROTO XNextEvent PROTO XEvent PROTO MapNotify PROTO XClearWindow PROTO XLookupKeysym PROTO XkbKeycodeToKeysym PROTO XKeysymToString PROTO XFlush PROTO XSync PROTO XClearArea '@ for delete event PROTO XInternAtom PROTO XSetWMProtocols PROTO Atom
REM PROTO cairo_xlib_surface_create PROTO cairo_xlib_surface_create_for_bitmap PROTO cairo_xlib_surface_set_size PROTO cairo_xlib_surface_get_display PROTO cairo_xlib_surface_get_screen PROTO cairo_xlib_surface_set_drawable PROTO cairo_xlib_surface_get_drawable PROTO cairo_xlib_surface_get_visual PROTO cairo_xlib_surface_get_width PROTO cairo_xlib_surface_get_heigh PROTO cairo_xlib_surface_get_depth
PROTO cairo_create PROTO cairo_rectangle PROTO cairo_set_source_rgb PROTO cairo_fill PROTO cairo_move_to PROTO cairo_set_source_rgb PROTO cairo_show_text PROTO cairo_show_page PROTO cairo_destroy PROTO cairo_surface_destroy PROTO cairo_set_font_size PROTO cairo_line_to PROTO cairo_save PROTO cairo_set_line_width PROTO cairo_stroke ' DECLARES DECLARE xevent TYPE XEvent DECLARE display TYPE Display* DECLARE screen_num TYPE int DECLARE win TYPE Window DECLARE win2 TYPE Window DECLARE display_width, display_height TYPE unsigned int DECLARE width, height TYPE unsigned int DECLARE win_x, win_y TYPE unsigned int DECLARE win_border_width DECLARE display_name TYPE char* DECLARE KEY TYPE int DECLARE *cs TYPE cairo_surface_t DECLARE e TYPE XEvent
display_name = getenv("DISPLAY") display = XOpenDisplay(display_name)
screen_num = DefaultScreen(display)
display_width = DisplayWidth(display, screen_num) display_height = DisplayHeight(display, screen_num) width = (display_width / 3) height = (display_height / 3) win_x = 0 win_y = 0 win_border_width = 2
SUB PIXEL(cairo_surface_t *cs,double xpos,double ypos)
LOCAL *c TYPE cairo_t c=cairo_create(cs) cairo_set_line_width(c,2.0) cairo_rectangle(c, xpos, ypos,4.0, 4.0) cairo_set_source_rgb(c,0.705, 0.196, 0.18) cairo_fill(c) cairo_show_page(c) cairo_destroy(c)
END SUB
SUB LINE(cairo_surface_t *cs,double xpos,double ypos,double xpos2,double ypos2)
LOCAL *c TYPE cairo_t c=cairo_create(cs) cairo_set_line_width(c,1.0) cairo_move_to(c, xpos, ypos) cairo_set_source_rgb(c,0.705, 0.196, 0.18) cairo_line_to(c,xpos2,ypos2) cairo_stroke(c) cairo_show_page(c) cairo_destroy(c)
END SUB
SUB Controls(cairo_surface_t *cs) LOCAL *c TYPE cairo_t c=cairo_create(cs) cairo_set_font_size(c,15.0) cairo_set_source_rgb(c,0.705, 0.196, 0.18) cairo_move_to(c, 20, 20) cairo_show_text(c, "Mouse Point And Click ____ ESP KEY = QUITE") cairo_show_page(c) cairo_destroy(c) END SUB
win= XCreateSimpleWindow(display, RootWindow(display, screen_num), \ win_x, win_y, width, height, win_border_width, \ BlackPixel(display, screen_num), \ WhitePixel(display, screen_num)) XSelectInput(display, win,StructureNotifyMask | ExposureMask | KeyPressMask|ButtonPressMask) cs=cairo_xlib_surface_create(display, win, DefaultVisual(display, 0),width, height) XStoreName(display, win, "X11+ Cairo Press Esc to exit") XMapWindow(display, win) FirstPt = 1 '@ set up client for WM delete event exit = this stops the ERR messages in Terminal DECLARE wm_delete_window TYPE Atom
wm_delete_window = XInternAtom (display, "WM_DELETE_WINDOW", False) XSetWMProtocols (display,win, &wm_delete_window, 1) WHILE (1)
XNextEvent(display, &e) SELECT e.type '@ when protcols are set the have to exit via the protocols if client data[0]=wm_delete_window CASE ClientMessage PRINT "Closing Down" IF ((Atom)e.xclient.data.l[0] == wm_delete_window) THEN PRINT "EXITING" XFlush(display) XClearWindow(display, win) cairo_surface_destroy(cs) XCloseDisplay(display) BREAK END IF CASE MapNotify PRINT "MapNotify" CASE Expose PRINT "Expose" Controls(cs) CASE ButtonPress: IF (FirstPt) THEN FirstPt=0 XClearWindow(display, win) pointx = e.xbutton.x pointy = e.xbutton.y PRINT "Point X : " , pointx PIXEL(cs,pointx,pointy) ELSE PRINT "Line" XClearWindow(display, win) FirstPt=1 LINE(cs,pointx,pointy, e.xbutton.x, e.xbutton.y) END IF
END SELECT
WEND
PRINT "BYE"
|
|
|
Post by alexfish on Jan 26, 2014 19:28:44 GMT 1
Hi Joe & all For those interested in GL. did a GL conversion on a gl dem.c , Here We Go '@ Simple GL dem User must have the apropriate DEV-libs installed , if have HUG working with gl '@ + tested the GL dems on main site then this should work. PRAGMA LDFLAGS `pkg-config --cflags --libs x11 --libs gl --libs glu ` PRAGMA INCLUDE stdio.h PRAGMA INCLUDE stdlib.h PRAGMA INCLUDE X11/X.h PRAGMA INCLUDE X11/Xlib.h PRAGMA INCLUDE GL/gl.h PRAGMA INCLUDE GL/glx.h PRAGMA INCLUDE GL/glu.h
'@ X11 PROTO XCreateWindow PROTO RootWindow PROTO XMapWindow PROTO XCloseDisplay PROTO XStoreName PROTO XNextEvent PROTO XEvent PROTO XVisualInfo PROTO XSetWindowAttributes PROTO XGetWindowAttributes PROTO XDestroyWindow PROTO Colormap
'@ GL PROTO GLXContex PROTO glClearColor PROTO glClear PROTO glMatrixMode PROTO glLoadIdentity PROTO glOrtho PROTO gluLookAt PROTO glBegin PROTO glColor3f PROTO glVertex3f PROTO glEnd PROTO glViewport PROTO glXCreateContext PROTO glXMakeCurrent PROTO glEnable PROTO glXSwapBuffers PROTO glXDestroyContext PROTO GLXContext
DECLARE gwa TYPE XWindowAttributes DECLARE xev TYPE XEvent DECLARE *dpy TYPE Display DECLARE root TYPE Window DECLARE *vi TYPE XVisualInfo DECLARE swa TYPE XSetWindowAttributes DECLARE win TYPE Window DECLARE glc TYPE GLXContext DECLARE att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None } TYPE GLint DECLARE cmap TYPE Colormap
SUB DrawAQuad() glClearColor(1.0, 1.0, 1.0, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-1., 1., -1., 1., 1., 20.)
glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.)
glBegin(GL_QUADS) glColor3f(1., 0., 0.) glVertex3f(-.75, -.75, 0.) glColor3f(0., 1., 0.) glVertex3f( .75, -.75, 0.) glColor3f(0., 0., 1.) glVertex3f( .75, .75, 0.) glColor3f(1., 1., 0.) glVertex3f(-.75, .75, 0.) glEnd() END SUB
DECLARE *dpy TYPE Display
dpy = XOpenDisplay(NULL) root = DefaultRootWindow(dpy)
vi = glXChooseVisual(dpy, 0, att)
IF (vi == NULL) THEN PRINT "appropriate visual found" END ELSE PRINT "Visual found" END IF
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone) swa.colormap = cmap swa.event_mask = ExposureMask | KeyPressMask
win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa)
XMapWindow(dpy, win) XStoreName(dpy, win, "GL SIMPLE APPLICATION") glc = glXCreateContext(dpy, vi, NULL, GL_TRUE) glXMakeCurrent(dpy, win, glc)
WHILE (1) XNextEvent(dpy, &xev) IF (xev.type == Expose) THEN XGetWindowAttributes(dpy, win, &gwa) glViewport(0, 0, gwa.width, gwa.height) DrawAQuad() glXSwapBuffers(dpy, win) ELIF (xev.type == ButtonPress ) THEN PRINT "button press" ELIF (xev.type == KeyPress) THEN glXMakeCurrent(dpy, None, NULL) glXDestroyContext(dpy, glc) XDestroyWindow(dpy, win) XCloseDisplay(dpy) END END IF WEND
Picy Attachments:
|
|
|
Post by bigbass on Jan 27, 2014 15:44:09 GMT 1
These functions allow programmers to write programs without knowing the details of the SYNTAX Here I created some new "ALIAS" to give it a Basic syntax look A tip for reading just ignore the first X this is just so we don't clobber built in BaCon commands When we have more example code this will be all organized and documented in the wiki but here is a start BaCon wiki '---open connection with the server XOPEN'---DefaultScreen XSCREEN'---Create a window XWINDOW'---Process Window Close Event (through an event handler) XEVENTQUIT'---select kind of events we are interested in XEVENTS
'---map (show) the window XSHOW'---The event loop goes here '---an example use WHILE/WEND '---destroy our window XQUIT'---close connection to server XCLOSE
|
|