Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4036626
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:14:47+00:00 2026-05-20T12:14:47+00:00

I am trying to write some boilerplate code for a game in C++. I

  • 0

I am trying to write some boilerplate code for a game in C++. I want to draw using both SDL and OpenGL. For example, I would like to have 2 versions of the functions “DrawPixel” written in both OpenGL and SDL. I can’t use both SDL and OpenGL for rendering at the same time, but I would like a common interface for the drawing so I don’t have to write twice as much when implementing the game.

I can write a base class with all the drawing functions I want in virtual, and then overload 2 versions of it for SDL and OpenGL. The problem is the performance, since I may end up calling DrawPixel million and millions of times every second, resolving the virtual calls will be expensive.

What other options do I have other than this OO approach? (or you could tell me that virtual isn’t slow)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:14:48+00:00Added an answer on May 20, 2026 at 12:14 pm

    Before I get into answering your question, have you considered making this a compile-time flag, e.g. #define — That would let you decide on either GL or SDL, without wasting space or requiring a virtual call. Definitely go for this solution if you can.

    Virtual functions are just an extra pointer lookup, calling a pointer-referenced function instead of a constant value. — and they have a big drawback that they cannot be inlined as the compiler doesn’t know where the function is. This is going to be the problem for any program which decides the functions at runtime.

    I’ll start with a warning: these may be micro-optimizations that aren’t worth worrying about. I would perhaps consider doing performance benchmark to see how important this is. For example, GL and SDL calls are already in a separate library, so each of them probably already require pointer lookups, among other things. An extra pointer is probably the least of your concerns.

    If you really can’t decide at compile-time, the only simple method IMHO that avoids virtual calls is to use C++ templates to do the job. In your case, I would make two separate classes with a function for each GL or SDL function you need, named the same, but do not make them virtual. Then, every function/class in your whole program which uses these functions would need to be declared as a template, either using SDL or GL. Finally, in your top-level main() function, have an if statement which runs one or the other version of your program depending on which version you want.

    This will lead to a perfectly efficient solution, even inlining all functions which can be inlined; however, at the cost of having two copies of your program and taking up more space. Also — because it results in a larger program, this may actually reduce performance.

    If you don’t want templates, one weird way that might also work would be to get the loader to use one of two different dynamically linked libraries. Both libraries would export the same functions, and then you would choose to load either one (e.g. by setting the LD_LIBRARY_PATH to one directory)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some LINQ To SQL code that would generate SQL like
I was trying to write some code that would check if an item has
I am trying to write some test code for my java application using Scalatest.
I'm new to using Eclipse. I'm trying to write some C++ code in it
Hello I'm trying to write some tool using code::blocks, wxWidgets and libxml2 on Windows
I'm trying to write some C# code to get to a specific folder in
I'm trying to write some C# code that calls a method from an unmanaged
I'm trying to write some code to find a specific XmlNode object based on
I'm trying to write some code to work with an htdigest password file. The
So, im trying to write some code that utilizes Nvidia's CUDA architecture. I noticed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.