TopicGosu / 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 :-] ...)
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.
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 :-]
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.)