B
Bhanu
Hi,
My code is this: (i will follow it up with the error)
image.h:
#include "window.h"
class Image
{
public:
Image(Win *w);
~Image();
int create(char **data);
void draw(Drawable drw, int x, int y);
private:
Win *w;
Pixmap pix;
Pixmap mask;
int width, height;
GC gc;
XGCValues xgcv;
};
image.cc:
#include <X11/Xlib.h>
#include "xpm.h"
#include "image.h"
#include "win.h"
Image::Image(Win *win)
{
w = win;
}
Image::~Image()
{
XFreePixmap(w->dpy, pix);
XFreePixmap(w->dpy, mask);
XFreeGC(w->dpy, gc);
}
int Image::create(char **data)
{
int status;
XpmAttributes attributes;
attributes.valuemask = XpmColormap | XpmCloseness;
attributes.colormap = w->screen_colormap;
attributes.closeness = 65535;
status = XpmCreatePixmapFromData(win->dpy, win->w, data, &pix, &mask,
&attributes);
if (status != XpmSuccess) return 0;
width = attributes.width;
height = attributes.height;
gc = XCreateGC(win->dpy, win->win, 0, NULL);
XSetFillStyle(win->display, gc, FillTiled);
XSetTile(win->display, gc, pix);
XSetClipMask(win->display, gc, mask);
return 1;
}
void Image::draw(Drawable drw, int x, int y)
{
xgcv.ts_y_origin = y;
xgcv.ts_x_origin = x;
xgcv.clip_y_origin = y;
xgcv.clip_x_origin = x;
XChangeGC(win->display, gc, GCClipXOrigin | GCClipYOrigin |
GCTileStipXOrigin | GCTileStipYOrigin, &xgcv);
XFillRectangle(win->display, drw, gc, x, y, width, height);
}
window.h:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <sys/time.h>
#define MAX_IMAGES 10
//this is to make sure that the number of images or the paratroopers in
the window is never more than 10
class Image;
class Win
{
friend class Image;/*so that the image class can access the private
variables of the window*/
public:
//this is the constructor
Win(int argc,char *argv[]);
private:
//here the members will be the objects that have to be shown on the
window i,e the images
Display *dpy;
GC gc, copygc;
Image *img[MAX_IMAGES];
Colormap screen_colormap;
Pixmap buffer;
int screen;
int depth;
int num_images;/*This will keep a track of the number of
paratroopers*/
int LoadImage(char **data);
int DrawImage(int x,int y);
//This is the window class.
};
window.cc
#include "bat.xpm"
#include "ball.xpm"
#include <stdio.h>
#include <stdlib.h>
#define NIL (0)
#include "image.h"
int main(int argc,char *argv[])
{
//This is the main function. Here we create a window object rest will
be taken care from there.
Win w(argc,argv);
return 0;
}
Win::Win(int argc,char *argv[])
{
// Open the display
dpy = XOpenDisplay(NIL);
if (dpy == NULL)
{
fprintf(stderr, "Cannot connect to X server %s\n", "simey:0");
exit(-1);
}
//Get the background color.
int blackColor=BlackPixel(dpy,DefaultScreen(dpy));
//Create a window
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
500, 500, 0, blackColor,
blackColor);
//Get the default screen and the depth
screen = DefaultScreen(dpy);
depth = DefaultDepth(dpy, screen);
//Select the inputs that our program has to receive.
XSelectInput(dpy,w,ButtonPressMask|ExposureMask);
//Create a graphics context
gc=XCreateGC(dpy,w,0,0);
//Map the window that is make it appear on the screen.
XMapWindow(dpy,w);
//Set the foreground to say green of the display that is much like
setting the foreground for this window
XColor green;
Status rc;
screen_colormap = DefaultColormap(dpy, DefaultScreen(dpy));
rc = XAllocNamedColor(dpy, screen_colormap, "green", &green, &green);
if (rc == 0) {
fprintf(stderr, "XAllocNamedColor - failed to allocated
'green' color.\n");
exit(1);
}
XSetForeground(dpy,gc,green.pixel);
buffer = XCreatePixmap(dpy, w, 500, 300, depth);
LoadImage(ball_xpm);
DrawImage(100,100);
XFlush(dpy);
XCloseDisplay(dpy);
}
int Win::LoadImage(char **data)
{
if (num_images>=10) return -1;
img[0]= new Image(this);
img[0]->create(data);
//num_images++;
//return (num_images-1);
//return 1;
}
int Win:rawImage(int x,int y)
{
img[0]->draw(buffer, x, y);
}
The problem:
Now the error that i get on compiling with using g++ window.cc -o
window.o -lX11 -lXpm is:
/tmp/cceikWLv.o(.text+0x59f): In function `Win::LoadImage(char**)':
: undefined reference to `Image::Image[in-charge](Win*)'
collect2: ld returned 1 exit status
Please help me out with this. What i want to do is just put an image on
a window. This i started out thinking would be a simple job but since
this is the first time i am programming in c++ (i am used to c more) i
am having tons of problems. I am sure i made many nasty mistakes in
this code but am unable to figure out. Please give your suggestions.
Thank you,
Bhanu
My code is this: (i will follow it up with the error)
image.h:
#include "window.h"
class Image
{
public:
Image(Win *w);
~Image();
int create(char **data);
void draw(Drawable drw, int x, int y);
private:
Win *w;
Pixmap pix;
Pixmap mask;
int width, height;
GC gc;
XGCValues xgcv;
};
image.cc:
#include <X11/Xlib.h>
#include "xpm.h"
#include "image.h"
#include "win.h"
Image::Image(Win *win)
{
w = win;
}
Image::~Image()
{
XFreePixmap(w->dpy, pix);
XFreePixmap(w->dpy, mask);
XFreeGC(w->dpy, gc);
}
int Image::create(char **data)
{
int status;
XpmAttributes attributes;
attributes.valuemask = XpmColormap | XpmCloseness;
attributes.colormap = w->screen_colormap;
attributes.closeness = 65535;
status = XpmCreatePixmapFromData(win->dpy, win->w, data, &pix, &mask,
&attributes);
if (status != XpmSuccess) return 0;
width = attributes.width;
height = attributes.height;
gc = XCreateGC(win->dpy, win->win, 0, NULL);
XSetFillStyle(win->display, gc, FillTiled);
XSetTile(win->display, gc, pix);
XSetClipMask(win->display, gc, mask);
return 1;
}
void Image::draw(Drawable drw, int x, int y)
{
xgcv.ts_y_origin = y;
xgcv.ts_x_origin = x;
xgcv.clip_y_origin = y;
xgcv.clip_x_origin = x;
XChangeGC(win->display, gc, GCClipXOrigin | GCClipYOrigin |
GCTileStipXOrigin | GCTileStipYOrigin, &xgcv);
XFillRectangle(win->display, drw, gc, x, y, width, height);
}
window.h:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <sys/time.h>
#define MAX_IMAGES 10
//this is to make sure that the number of images or the paratroopers in
the window is never more than 10
class Image;
class Win
{
friend class Image;/*so that the image class can access the private
variables of the window*/
public:
//this is the constructor
Win(int argc,char *argv[]);
private:
//here the members will be the objects that have to be shown on the
window i,e the images
Display *dpy;
GC gc, copygc;
Image *img[MAX_IMAGES];
Colormap screen_colormap;
Pixmap buffer;
int screen;
int depth;
int num_images;/*This will keep a track of the number of
paratroopers*/
int LoadImage(char **data);
int DrawImage(int x,int y);
//This is the window class.
};
window.cc
#include "bat.xpm"
#include "ball.xpm"
#include <stdio.h>
#include <stdlib.h>
#define NIL (0)
#include "image.h"
int main(int argc,char *argv[])
{
//This is the main function. Here we create a window object rest will
be taken care from there.
Win w(argc,argv);
return 0;
}
Win::Win(int argc,char *argv[])
{
// Open the display
dpy = XOpenDisplay(NIL);
if (dpy == NULL)
{
fprintf(stderr, "Cannot connect to X server %s\n", "simey:0");
exit(-1);
}
//Get the background color.
int blackColor=BlackPixel(dpy,DefaultScreen(dpy));
//Create a window
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
500, 500, 0, blackColor,
blackColor);
//Get the default screen and the depth
screen = DefaultScreen(dpy);
depth = DefaultDepth(dpy, screen);
//Select the inputs that our program has to receive.
XSelectInput(dpy,w,ButtonPressMask|ExposureMask);
//Create a graphics context
gc=XCreateGC(dpy,w,0,0);
//Map the window that is make it appear on the screen.
XMapWindow(dpy,w);
//Set the foreground to say green of the display that is much like
setting the foreground for this window
XColor green;
Status rc;
screen_colormap = DefaultColormap(dpy, DefaultScreen(dpy));
rc = XAllocNamedColor(dpy, screen_colormap, "green", &green, &green);
if (rc == 0) {
fprintf(stderr, "XAllocNamedColor - failed to allocated
'green' color.\n");
exit(1);
}
XSetForeground(dpy,gc,green.pixel);
buffer = XCreatePixmap(dpy, w, 500, 300, depth);
LoadImage(ball_xpm);
DrawImage(100,100);
XFlush(dpy);
XCloseDisplay(dpy);
}
int Win::LoadImage(char **data)
{
if (num_images>=10) return -1;
img[0]= new Image(this);
img[0]->create(data);
//num_images++;
//return (num_images-1);
//return 1;
}
int Win:rawImage(int x,int y)
{
img[0]->draw(buffer, x, y);
}
The problem:
Now the error that i get on compiling with using g++ window.cc -o
window.o -lX11 -lXpm is:
/tmp/cceikWLv.o(.text+0x59f): In function `Win::LoadImage(char**)':
: undefined reference to `Image::Image[in-charge](Win*)'
collect2: ld returned 1 exit status
Please help me out with this. What i want to do is just put an image on
a window. This i started out thinking would be a simple job but since
this is the first time i am programming in c++ (i am used to c more) i
am having tons of problems. I am sure i made many nasty mistakes in
this code but am unable to figure out. Please give your suggestions.
Thank you,
Bhanu