Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Buffering class for datafiles.

Credits go to Steve Terry for helping out!
This thread is locked; no one can reply to it. rss feed Print
Buffering class for datafiles.
julian_boolean
Member #8,201
January 2007

I seen this thread http://www.allegro.cc/forums/thread/590302 for a buffering class someone made and was wondering if there is any floating around that work with datafiles instead?

CGamesPlay
Member #2,559
July 2002
avatar

Thanks for searching the forum before you posted, but I am not quite sure what it is you want to accomplish.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

No problem? hehe

Well okay.. I got a function in each state of my game that looks something like this:

menu::menu()
{
  continue_button = new button((BITMAP*)gui[button_down].dat, (BITMAP*)gui[button_off].dat);

  exit_button = new button((BITMAP*)gui[button_down].dat, (BITMAP*)gui[button_off].dat);

void draw(BITMAP *buffer)
{
  continue_button->draw(buffer)
  exit_button->draw(buffer)
}

I want to try to find a way to remove this function all together and just put a buffer through the class?.. If that makes sense.

Steve Terry
Member #1,989
March 2002
avatar

I just hope you realize first off that the class you are referring to is severely broken and as far as I can tell does nothing than blit the image to location x, y and push the pointer into a vector which serves no purpose.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

julian_boolean
Member #8,201
January 2007

Nm about the whole thing, I'll find a way to do it myself since I'm having trouble explaining what I want.

The code I posted was just a very, very small example.. Not even my actual code.

(edit)

Oh you mean that guy's code?

Well I want to find a way to put all the objects I create into a vector and buffer them, without actually having to say yknow.. button_instance_name->draw(buffer)

Steve Terry
Member #1,989
March 2002
avatar

Yes that guys code. What do you want in a buffering class? If you mean the ability to switch between buffered modes (i.e. double, triple, page flipping) then there are plenty of examples in the forums. If on the other hand you want some kind of class which all widgets draw to you can create a simple class that creates a buffer and has a draw method which blits it to the screen. You can create a singleton instance of this class which means there is only one copy of it and each widget can get the instance and blit to it.

1class Display
2{
3 public:
4 BITMAP *backBuffer;
5 void Flip();
6 static Display *GetDisplay();
7 protected:
8 Display();
9 virtual ~Display();
10};
11 
12Display::Display()
13{
14 DEBUG("Display::Display()");
15 backBuffer = create_bitmap(SCREEN_W, SCREEN_H);
16 if(!backBuffer)
17 throw Exception("Error: Display - Unable to create backbuffer");
18}
19 
20Display::~Display()
21{
22 DEBUG("Display::~Display()");
23 if(backBuffer != NULL)
24 destroy_bitmap(backBuffer);
25 backBuffer = NULL;
26}
27 
28Display *Display::GetDisplay()
29{
30 static Display m_Display;
31 return &m_Display;
32}
33 
34void Display::Flip()
35{
36 masked_blit(mouse_sprite, backBuffer, 0, 0, mouse_x, mouse_y, mouse_sprite->w, mouse_sprite->h);
37 blit(backBuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
38}

In your class you simply do this:

1class Button
2{
3 private:
4 Display *display;
5 public:
6 void Draw();
7 void Button();
8 void ~Button();
9}
10 
11Button::Button()
12{
13 display = Display::GetDisplay();
14}
15 
16Button::Draw()
17{
18 blit(blah, display->backbuffer, 0, 0, 0, 0, blah->w, blah->h);
19}

Also you can have in the singleton display class a vector of pointers to base objects that have a draw method and upon flip you can simply iterate through those classes and have them draw to its buffer. That would work as well. A vector of bitmaps like the other guys class wouldn't work for what you want.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

julian_boolean
Member #8,201
January 2007

Ohhhhhh!! That looks good, going to fool around with that for a bit. Thanks a bunch!

Steve Terry
Member #1,989
March 2002
avatar

Alternative method which may be more of what you are looking for:

1class DrawObj
2{
3 public:
4 void Draw(BITMAP *buffer);
5}
6 
7class Button: public DrawObj
8{
9 // stuff here
10}
11 
12Button::Draw(BITMAP *buffer);
13{
14 // draw button to buffer
15}
16 
17 
18class Display
19{
20 private:
21 BITMAP *backBuffer;
22 std::vector<DrawObj *> draw_objs;
23 public:
24 Display();
25 virtual ~Display();
26 void Flip();
27 void Add(DrawObj *obj);
28};
29 
30Display::Display()
31{
32 DEBUG("Display::Display()");
33 backBuffer = create_bitmap(SCREEN_W, SCREEN_H);
34 if(!backBuffer)
35 throw Exception("Error: Display - Unable to create backbuffer");
36}
37 
38Display::~Display()
39{
40 DEBUG("Display::~Display()");
41 if(backBuffer != NULL)
42 destroy_bitmap(backBuffer);
43 backBuffer = NULL;
44 draw_objs.erase();
45}
46 
47void Display::Flip()
48{
49 for(i = 0; i < draw_objs.size(); ++i)
50 draw_objs<i>->Draw(backBuffer);
51 blit(backBuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
52}
53 
54void Display::Add(DrawObj *obj)
55{
56 draw_objs.push_back(obj);
57}

So you create somewhere a Display object and add your widgets to it, then in a main draw function you call display->flip(). Easy enough?

[edit]
A third alternative is to use the Display singleton and a new class that contains a vector to your objects which simply blits them all to your singleton Display, whichever method seems best for your needs.
[/edit]

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

ImLeftFooted
Member #3,935
October 2003
avatar

Damn, I was hoping this thread would be about adding a buffer for the datafile system to allow partial loading / unloading of datafiles.

I'm currently trying to figure out just that nightmare to lower my memory usage :(...

But being on topic, I have a class that just does this. It is not particularly clean as I wrote it years ago (and I had horrible style then). The class works very well though, if you would like it I can post its source maybe?

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

I'm currently trying to figure out just that nightmare to lower my memory usage :(...

load_datafile_object

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

julian_boolean
Member #8,201
January 2007

I would like to see it if you want to post it. Going to try out everyone's code/ideas to see what works best for me. Thanks! :)

Go to: