somnock
somnock

Reputation: 147

Performance of drawing function

I'm creating small engine/framework for making games. It's kind of simple question but I haven't found an answer so I'd like to consult you.

There are some classes derived from Graphic with the Draw(...) function. Draw() will be called dozens or maybe even hundreds of times (if there are many sprites to render). Draw() is too big to be inline (10-20 lines).

  1. I'd like it to be virtual (sometimes but rather rarely I'll be using polymorphism*). I think it shouldn't affect greatly on performance but do you think it should be virtual?
  2. Should it have some arguments (2-6) describing position to render (etc)? I don't have an idea if passing many arguments will make it slower. Maybe every drawable object should have its own position/area data?

The most important question is 2.

*ex. with animations

Upvotes: 1

Views: 107

Answers (2)

Ajay
Ajay

Reputation: 18431

First you should choose in-memory, double-buffer drawing using BitBlt, StretchBlt functions.

  • If the base class declares virtual as pure-virtual, isn't a performance problem at all.
  • Yes, passing multiple arguments can make it slow - put them into structure, and pass reference/pointer of that structure; or use a shared area (private variables in class).
  • Yes, for redrawing (re-painting) would need all data related to object - that's obvious!

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308206

Any object which needs to be drawn will be affecting a large number of pixels; it is likely that drawing itself will take much more time than calling the drawing function, so the calling overhead will be negligible. Don't worry about it until profiling shows it to be a bottleneck.

Upvotes: 2

Related Questions