Skip to content

Instantly share code, notes, and snippets.

@davidsan
Last active December 15, 2015 04:08
Show Gist options
  • Select an option

  • Save davidsan/5198997 to your computer and use it in GitHub Desktop.

Select an option

Save davidsan/5198997 to your computer and use it in GitHub Desktop.
LI 357 – TER Partiel du vendredi 25 mars 2011 Exercice 1
/************************************
LI 357 – TER
Partiel du vendredi 25 mars 2011
Exercice 1
************************************/
#include <X11/Xlib.h>
#include <X11/X.h>
#include <stdlib.h>
#include <stdio.h>
#define noire 0x000000
#define grise25 0xC0C0C0
#define grise50 0x7F7F7F
#define grise75 0x404040
#define blanche 0xFFFFFF
Display *dpy;
int ecran;
Window racine, principale;
XEvent evmt;
int largeur, hauteur;
int souris_est_dedans;
GC gc;
void installer (void);
/*********** debut event handling sig ***********/
void PourButtonPress(XButtonEvent * evmt);
void PourButtonRelease(XButtonEvent * evmt);
void PourExpose(XExposeEvent * evmt);
void PourEnterNotify(XEnterWindowEvent *evmt);
void PourLeaveNotify(XLeaveWindowEvent *evmt);
void algoPeindre(unsigned long color, unsigned long color2);
/*********** fin event handling sig ***********/
void remplirTriangle (Display *dpy, Window fen, unsigned long couleur, int x1, int y1, int x2, int y2, int x3, int y3);
void remplirRectangle (Display *dpy, Window fen, unsigned long couleur, int x, int y, int largeur, int hauteur);
void dessinerDiagonaux (Display *dpy, Window fen, unsigned long couleur);
int espaceEnX ();
int espaceEnY ();
int main (int argc, char *argv[]) {
installer();
/*********** debut event loop ***********/
while(1){
XNextEvent(dpy, &evmt);
switch(evmt.type){
case EnterNotify :
PourEnterNotify((XEnterWindowEvent *)(&evmt));
case LeaveNotify :
PourLeaveNotify((XLeaveWindowEvent *)(&evmt));
case ButtonPress :
PourButtonPress((XButtonEvent *)(&evmt));
break;
case ButtonRelease :
PourButtonRelease((XButtonEvent *)(&evmt));
break;
case Expose :
PourExpose((XExposeEvent *)(&evmt));
break;
default:;
}
}
/*********** fin event loop ***********/
}
void installer (void) {
dpy = XOpenDisplay(NULL);
racine = DefaultRootWindow(dpy);
ecran = DefaultScreen(dpy);
gc = DefaultGC(dpy, ecran);
souris_est_dedans = 0;
/*********** ?????????? ***********/
largeur = 150;
hauteur = 50;
int espace = hauteur / 10;
principale = XCreateSimpleWindow(dpy, racine, 0, 0, largeur, hauteur, 0, noire, blanche);
/*********** debut select event input mask ***********/
XSelectInput(dpy, principale, ButtonPressMask | ButtonReleaseMask | ExposureMask);
/*********** fin select event input mask ***********/
XStoreName(dpy, principale, "Exercice 1");
XMapWindow(dpy, principale);
}
void PourButtonPress(XButtonEvent * evmt){
algoPeindre(blanche, grise50);
}
void PourButtonRelease(XButtonEvent * evmt){
algoPeindre(grise25, grise75);
}
void PourExpose(XExposeEvent * evmt){
if(souris_est_dedans){
algoPeindre(blanche, grise50);
}else{
algoPeindre(grise25, grise75);
}
}
void PourEnterNotify(XEnterWindowEvent *evmt){
souris_est_dedans=1;
algoPeindre(blanche, grise50);
}
void PourLeaveNotify(XLeaveWindowEvent *evmt){
souris_est_dedans=0;
algoPeindre(grise25, grise75);
}
void algoPeindre(unsigned long color, unsigned long color2){
/* triangle noir du bas */
remplirTriangle(dpy, principale, noire, 0, hauteur, largeur, 0, largeur, hauteur);
/* triangle de couleur color du haut */
remplirTriangle(dpy, principale, color, 0, 0, 0, hauteur, largeur, 0);
/* diagonales */
dessinerDiagonaux(dpy, principale, noire);
/* rectangle */
remplirRectangle(dpy, principale, color2, 0+espaceEnX(), 0+espaceEnY(), largeur-espaceEnX()*2, hauteur-espaceEnY()*2);
}
/*********** fournis ***********/
void remplirTriangle (Display *dpy, Window fen, unsigned long couleur, int x1, int y1, int x2, int y2, int x3, int y3) {
XPoint *points = (XPoint *)malloc(sizeof(XPoint) * 3);
XSetForeground(dpy, gc, couleur);
points[0].x = x1; points[0].y = y1;
points[1].x = x2; points[1].y = y2;
points[2].x = x3; points[2].y = y3;
XFillPolygon(dpy, fen, gc, points, 3, Nonconvex, CoordModeOrigin);
}
void remplirRectangle (Display *dpy, Window fen, unsigned long couleur,int x, int y, int largeur, int hauteur) {
XSetForeground(dpy, gc, couleur);
XFillRectangle(dpy, fen, gc, x, y, largeur, hauteur);
}
void dessinerDiagonaux (Display *dpy, Window fen, unsigned long couleur) {
XSetForeground(dpy, gc, couleur);
XDrawLine(dpy, fen, gc, 0, 0, largeur, hauteur);
XDrawLine(dpy, fen, gc, largeur, 0, 0, hauteur);
}
int espaceEnX () {
return largeur / 10;
}
int espaceEnY () {
return espaceEnX() * hauteur / largeur;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment