Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / [C++] Get the "graphics" to draw from another class
- By ? Date 2010-04-14 17:29
Hello,

I would like to create a class that contains and draws textures but I can't find the proper way to pass the "graphics" created in my Gosu::Window class to another class
I tried to use a variable "Gosu::Graphics &graphic;" but it does not work (seems I'm too noob to play with Gosu yet :-] ...)

the goal is to have somthing like this:

//creation
  imgData[1].reset(new Gosu::Image(graphics(), Gosu::sharedResourcePrefix() + L"/textures/toto.png", 0, 0, 256, 512, true));
  ...
//draw
  imgData[1]->draw(0, 0, z_Render_Game_Menu_Background_Lvl_00);

regards,
Gus
- By Gus Date 2010-04-14 17:32
(now I'm registered ^_^)
- By jlnr (dev) Date 2010-04-15 11:39
This looks perfectly correct to me, maybe you are doing this outside of a Gosu::Window subclass though. The "graphics()" call is actually a member function of Gosu::Window.
- By Gus Date 2010-04-15 21:53
That is what I thought first but I should be tired :-)

Just to test I wrote it quickly like that:

    class Image : public Gosu::Window
    {
        boost::scoped_ptr<Gosu::Image> image; // will be static list on a later dev
        int x,y;
   
    public:
   
        Image(int _x, int _y) : Window(screenWidth, screenHeight, false)
        {
             // will be a push in my list of textures
            image.reset(new Gosu::Image(graphics(), Gosu::sharedResourcePrefix() + L"/textures/toto.png", 0, 0, 256, 512, true));
            x = _x;
            y = _y;
        }
   
        void draw (int _zCoordt)
        {
            image->draw(x, y, _zCoordt);
        }
    };
   
    class MyWindow : public Gosu::Window, public Base
    {
        ...
        void draw ()
        {
            class Image img(200,200);
            img.draw(z_Render_Game_Menu_Background_Lvl_00);
        }
    };
   
Still I have a black screen.
I thought I could pass the graphics() parameter in the Image class constructor ... but couldn't manage it :-(

Doesn't this code create two rendering context? this would explain the boring black screen :-]

Regards,
Gus
- By jlnr (dev) Date 2010-04-16 17:55
Yes it does. You have to pass a reference to the main window you are using for your game to your image loading code, not create more windows :) (I think the Tutorial.cpp already does something like this by passing the Graphics& reference, it could also pass a Window& reference and call its .graphics() function.)
- By Gus Date 2010-04-19 10:41
my mistake!
I didn't saw the "player(graphics(), audio())" at the end of the GameWindows() contructor in your tutorial :$

works fine!
great job with Gosu :-]
Up Topic Gosu / Gosu Exchange / [C++] Get the "graphics" to draw from another class

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill