Üdv! A következő proggyt szeretném úgy kibővíteni hogy változtató legyen a rajzolószín. Ehhez szeretnék segítséget kérni, előre is köszi!
#include X11/Xlib.h
#include X11/Xutil.h
#include X11/Xos.h
#include X11/Xatom.h
#include stdlib.h
#include string.h
#include stdio.h
// Global variables
Display * display;
int screen_num;
static char * appname;
// main() function
int main( int argc, char * argv[] ) {
// Window variables
Window win;
int x, y;
unsigned int width, height;
unsigned int border_width;
char * window_name = "Hello, X Window System!";
char * icon_name = "HelloX";
// Display variables
char * display_name = NULL;
unsigned int display_width, display_height;
// Miscellaneous X variables
XSizeHints * size_hints;
XWMHints * wm_hints;
XClassHint * class_hints;
XTextProperty windowName, iconName;
XEvent report;
XFontStruct * font_info;
XGCValues values;
GC gc;
// Variables for color
long fgcolor, bgcolor;
int screen, pointx, pointy;
XGCValues gcval;
Colormap cmap;
XColor color, ignore;
char *colorname="grey";
appname = argv[0];
// Allocate memory for our structures
if ( !( size_hints = XAllocSizeHints() ) ||
!( wm_hints = XAllocWMHints() ) ||
!( class_hints = XAllocClassHint() ) ) {
fprintf(stderr, "%s: couldn't allocate memory.\n", appname);
exit(EXIT_FAILURE);
}
// Connect to X server
if ( (display = XOpenDisplay(display_name)) == NULL ) {
fprintf(stderr, "%s: couldn't connect to X server %s\n",
appname, display_name);
exit(EXIT_FAILURE);
}
// Get screen size from display structure macro
screen_num = DefaultScreen(display);
display_width = DisplayWidth(display, screen_num);
display_height = DisplayHeight(display, screen_num);
// Set initial window size and position, and create it
bgcolor = BlackPixel(display,screen_num);
fgcolor = WhitePixel(display,screen_num);
x = y = 0;
width = 640;
height = 480;
win = XCreateSimpleWindow(display, RootWindow(display, screen_num),
x, y, width, height, border_width,
fgcolor,
bgcolor);
cmap = DefaultColormap (display, screen_num);
// XAllocNamedColor(display, cmap, colorname, &color, &ignore);
// fgcolor = color.pixel;
// XStandardColormap *XAllocStandardColormap();
gcval.foreground = fgcolor;
gcval.background = bgcolor;
gc = XCreateGC(display,win,GCForeground|GCBackground,&gcval);
// Set hints for window manager before mapping window
if ( XStringListToTextProperty(&window_name, 1, &windowName) == 0 ) {
fprintf(stderr, "%s: structure allocation for windowName failed.\n",
appname);
exit(EXIT_FAILURE);
}
if ( XStringListToTextProperty(&icon_name, 1, &iconName) == 0 ) {
fprintf(stderr, "%s: structure allocation for iconName failed.\n",
appname);
exit(EXIT_FAILURE);
}
size_hints->flags = PPosition | PSize | PMinSize | PMaxSize;
size_hints->min_width = 640;
size_hints->min_height = 480;
size_hints->max_width = 640;
size_hints->max_height = 480;
wm_hints->flags = StateHint | InputHint;
wm_hints->initial_state = NormalState;
wm_hints->input = True;
class_hints->res_name = appname;
class_hints->res_class = "hellox";
XSetWMProperties(display, win, &windowName, &iconName, argv, argc,
size_hints, wm_hints, class_hints);
// XSetRGBColormaps(display, win, std_colormap, 0, prop1);
// Choose which events we want to handle
XSelectInput(display, win, ExposureMask | KeyPressMask |
ButtonPressMask | StructureNotifyMask);
// Load a font called "9x15"
if ( (font_info = XLoadQueryFont(display, "9x15")) == NULL ) {
fprintf(stderr, "%s: cannot open 9x15 font.\n", appname);
exit(EXIT_FAILURE);
}
// Create graphics context
gc = XCreateGC(display, win, 0, &values);
XSetFont(display, gc, font_info->fid);
XSetForeground(display, gc, fgcolor);
// Display Window
XMapWindow(display, win);
// Enter event loop
while ( 1 ) {
static char * message = "Hello, X Window System!";
static int length;
static int font_height;
static int msg_x, msg_y;
XNextEvent(display, &report);
switch ( report.type ) {
case Expose:
if ( report.xexpose.count != 0 ) break;
/* Output message centrally in window */
length = XTextWidth(font_info, message, strlen(message));
msg_x = (width - length) / 2;
font_height = font_info->ascent + font_info->descent;
msg_y = (height + font_height) / 2;
XDrawString(display, win, gc, msg_x, msg_y,
message, strlen(message));
break;
case ConfigureNotify:
/* Store new window width & height */
width = report.xconfigure.width;
height = report.xconfigure.height;
break;
case ButtonPress: /* Fall through */
XDrawPoint(display,win,gc, report.xbutton.x,report.xbutton.y);
break;
case KeyPress:
/* Clean up and exit */
XUnloadFont(display, font_info->fid);
XFreeGC(display, gc);
XCloseDisplay(display);
exit(EXIT_SUCCESS);
break;
default:;
}
}
return EXIT_SUCCESS; /* We shouldn't get here */
}
- 6890 megtekintés
Hozzászólások
segíteni nem tudok, de áttalicskáztam neked pastebinre.
- A hozzászóláshoz be kell jelentkezni
Köszönöm.
- A hozzászóláshoz be kell jelentkezni
nincs mit, így legalább néhány percig úgy érezhettem, mintha tudnék C-ben programozni xD
- A hozzászóláshoz be kell jelentkezni
Ezen erzes jogossagat, mar az elso kodsor megkerdojelezi :D
- A hozzászóláshoz be kell jelentkezni
Teljesen jogos, azonban a szabályos include szintaktika nem jelenik meg itt a fórumban mert html tagként értelmezodik, ezért bátorkodtam kivenni belőle. Amúgy a kód lefordul gcc-vel és ki is próbáltam, műxik.
- A hozzászóláshoz be kell jelentkezni
Egyreszt nem neked szolt, masreszt csak vicc volt, meg veletlenul sem kell komolyan venni ;) Egyebkent igy megy, de jobb ha a pastebin.com-ot hasznalod:
#include <X11/Xlib.h>
- A hozzászóláshoz be kell jelentkezni
OK :)
- A hozzászóláshoz be kell jelentkezni
Elore szolok, hogy nem neztem meg az egesz kodot, csak vakon felhasznaltam a mar meglevo valtozokat, de pl. igy megvaltoztathatod a rajzoloszint:
...
cmap = DefaultColormap (display, screen_num);
XParseColor(display, cmap, colorname, &color);
XAllocColor(display, cmap, &color);
fgcolor = color.pixel;
...
- A hozzászóláshoz be kell jelentkezni
Köszönöm szépen a segítséget, kipróbálom.
- A hozzászóláshoz be kell jelentkezni
Még 1 kérdésem lenne:
Itt megadtam a szürke színt:
char *colorname="grey";
ezt melyik utasítással tudom később rajzolásnál változtatni?
mondjuk az XDrawPoint() elé mit tegyek be?
case ButtonPress: /* Fall through */
gcval.foreground = fgcolor;
fgcolor = color.pixel;
XDrawPoint(display,win,gc, report.xbutton.x,report.xbutton.y);
break;
ez mondjuk így jó lenne?
- A hozzászóláshoz be kell jelentkezni
gcval.foreground = fgcolor;
fgcolor = color.pixel;
Ettol a ket sortol egeszen pontosan mit is varsz? ;) A szint ugyanugy tudod valtoztatni, ahogy azt a szurke eseteben is tetted, csak nyilvan meg kell hivnod az XSetForeground()-ot megint, mondjuk igy:
case ButtonPress: /* Fall through */
XAllocNamedColor(display, cmap, "yellow", &color, &ignore);
XSetForeground(display, gc, color.pixel);
XDrawPoint(display, win,gc, report.xbutton.x, report.xbutton.y);
break;
Itt az XAllocNamedColor()-t hasznaltam, mert ez mar az erdeti kododban is benne van, igaz kikommentezve ;) de itt ez ugyanaz, mintha az XParseColor()/XAllocColor() parost hivnad (ja, az altalam irt peldakban ugyan nem szerepel, de az legyen magatol ertetodo, hogy a visszateresi ertekeket nem art ellenorizni). Kerdes persze meg, hogyan akarod a beallithato szineket adminisztralni, de ez mar legyen a te dolgod. Egyebkent nem artana letisztazni a kodot, mert elegge "osszeollozottnak" tunik. Ez kulonben valami hazi? ;) Mert ha nem es a tema tenyleg erdekel (kell hozza egy kis mazochizmus), akkor javaslom az Xlib Programming Manual tanulmanyozasat.
- A hozzászóláshoz be kell jelentkezni
Igen ez még csak ilyen taknyolt-gányolt állatorvosi ló tanulási céllal. Éles helyzetben való alkalmazása még saját felelősségre sem ajánlott :P :)
Igen ha egy kicsit megokosodtam akkor elölről elkezdek egy másikat amivel egy-két alap grafikus feladatot meg tudok csinálni. (pixelek, síkidomok stb. kirajzolása, animálása, ilyesmi)
- A hozzászóláshoz be kell jelentkezni
Nem tudom, segít-e, vagy átnézed-e, mindenesetre itt egy program, amiben használok színeket.
----
India delenda est.
Hülye pelikán
- A hozzászóláshoz be kell jelentkezni
Köszi Neked is meg Igornak is a segítséget, minden sor kódot, ami előbbrevihet, szívesen fogadok, szeretném minél jobban megismerni ezt a témát.
- A hozzászóláshoz be kell jelentkezni