Jump to content
  • Advertisement
Sign in to follow this  
DpakoH

[SOLVED] Gtkmm + cairomm

This topic is 3753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello all, i've been reading gtkmm tutorial and using this specific code : what i want to do is to create a window and draw in it a xy- coordinate system and plot some dots on it. but every time i use it, i want to draw different points , so i cannot statically assign the points and draw them in "on_expose_event" function. So please i need help from someone who is familiar with gtkmm , or point me some tutorials on this topic. i searched in google,and found pretty good tutorials on gtk+ , but gtkmm is kind of different and i cant just converse the code to gtkmm (maybe because i am poor in understanding documentation). Any help will be greatly appreciated. [Edited by - DpakoH on July 14, 2008 4:51:52 AM]

Share this post


Link to post
Share on other sites
Advertisement
I have played with gtkmm a little bit, but I don't understand your problem. Maybe you can post some code...

Share this post


Link to post
Share on other sites
well i want to have a function like :

void drawPoint(int x,int y){
// draw the point as a small circle
cr->save();
cr->arc(xc, yc, l.0, 0.0, 2.0 * M_PI); // full circle
cr->set_source_rgba(0.0, 0.0, 0.8, 0.6); // partially translucent
cr->fill_preserve();
cr->restore();
cr->stroke();
// where
// Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
// is set in initialization

}



and when this function draws a circle/point , then "on_expose_event" function will overdraw it. and i cannot put drawPoint in "on_expose_event" function because i dont know how many points i have to draw, and if i want to draw something else it is a problem again.

Share this post


Link to post
Share on other sites
You can keep a container with all the points or other drawable objects that you want to draw, and have on_expose_event call a function that goes through the container and asks every object in it to draw itself.

Share this post


Link to post
Share on other sites
sounds good solution, i have to check it out. thank you for help, and btw can u provide some sample code to orient myself better. it would be great !!
and again thanx for help


y.

Share this post


Link to post
Share on other sites
Starting from some sample gtkmm code and incorporating your drawing code, I came up with this:
#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <gtkmm/drawingarea.h>
#include <vector>

class Drawable {
public:
virtual void draw(Cairo::RefPtr<Cairo::Context> cr) = 0;
virtual ~Drawable() {
}
};

class Point : public Drawable {
double x, y;
public:
Point(double x, double y) : x(x), y(y) {
}

void draw(Cairo::RefPtr<Cairo::Context> cr) {
// draw the point as a small circle
cr->save();
cr->arc(x, y, 1.0, 0.0, 2.0 * M_PI); // full circle
cr->set_source_rgba(0.0, 0.0, 0.8, 0.6); // partially translucent
cr->fill_preserve();
cr->restore();
cr->stroke();
}
};

class MyArea : public Gtk::DrawingArea {
std::vector<Drawable *> drawables;
// It would be better to use a container of pointers from boost, but I
// want to keep this example easy to compile

public:
MyArea() {
add_events(Gdk::BUTTON_PRESS_MASK);
signal_button_press_event().connect(sigc::mem_fun(*this,&MyArea::on_button_pressed_event));
}

~MyArea() {
for(unsigned i = 0; i < drawables.size(); ++i)
delete drawables;
}

protected:
//Override default signal handler:
virtual bool on_expose_event(GdkEventExpose* event) {
// This is where we draw on the window
Glib::RefPtr<Gdk::Window> window = get_window();
if(window) {
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();

// clip to the area indicated by the expose event so that we only redraw
// the portion of the window that needs to be redrawn
cr->rectangle(event->area.x, event->area.y,
event->area.width, event->area.height);
cr->clip();

for(unsigned i = 0; i < drawables.size(); ++i)
drawables->draw(cr);
}

return true;
}

virtual bool on_button_pressed_event(GdkEventButton* event) {
if(event->type==GDK_BUTTON_PRESS) {
Point *point = new Point(event->x, event->y);
drawables.push_back(point);
queue_draw();
}

return true;
}
};

int main(int argc, char** argv) {
Gtk::Main kit(argc, argv);

Gtk::Window win;
win.set_title("Points!");

MyArea area;
win.add(area);
area.show();

kit.run(win);
}



Share this post


Link to post
Share on other sites
at first when you said "use container" i was thinking of some widget container , and that's why i was puzzled :) then i realized that you meant a list or vector for example . and i made it up. later i saw your source code , and it is pretty much with the same idea, slighty different implementation though. but nevertheless , THANK YOU. (again!)


y.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!