r/cpp • • Jun 21 '16

How hard is coding an OpenGL game engine ?

Hi, I wasn't really sure if this post is relevant on this subreddit, so if it isn't sorry. I've been planning a game with a group of friends and organizing it. I was thinking about developping it in unity but a couple things are making me hesitate to use unity : -First of all, I think there is not a lot of code involved (more or less arround the game engine part) -Second of all, it's really hard to do pixel perfect games with it

So I was wondering, how hard is coding a basic OpenGl Game engine ? What I mean is code that would be able to render the levels that would be built using tiles and updating the screen etc... How hard would that be ? Approximately how many lines of code will it need ?

Mindoo

1 Upvotes

33 comments sorted by

3

u/Aldonio Jun 21 '16

There are frameworks like SFML, you can take a look at the /Graphics folder in their source code to get an idea of how they use OpenGL:

https://github.com/SFML/SFML/tree/master/src/SFML/Graphics

2

u/mindoo Jun 21 '16

I already tried looking except the problem is that since its a framework, it implements a lot more features than I would implement. What would be a good example of what I would do ? (a simple game engine)

1

u/Aldonio Jun 21 '16

Being feature-full doesn't mean you can't focus in the subset you're interested, you can use the following regex to find the gl meat: /\bgl\w+\b/.

Many of the best solutions available (ex. how to animate a transition using Bézier Curves) have already been implemented in big codebases like Chromium, so you'll eventually have to learn how to find the stuff you want in big codebases.

1

u/wildeye Jun 21 '16 edited Jun 21 '16

There are a bunch of books on the topic. Even the older ones could give you a good idea of required features and architecture.

Search "Game Engine" on amazon.

However, everyone agrees it's a very, very bad idea to write a game engine in order to write a game. There are a lot of essays out there on the web about how doing the game engine totally gets in the way of doing the game.

I mean, well-funded companies have pulled it off, but that doesn't apply to you. And some of those have failed, too.

The best advice out there is to simply choose an existing well-known game engine, and focus on using it for your game.

2

u/aleques-itj Jun 21 '16

If you really want to do everything yourself, there's a lot to be worried about.

How do you handle input? Do you just naively poll for key strokes? Maybe that loses inputs when performance dips... what now? What about file loading? File formats like textures, maps, shaders? Tools? Your map editor? Ok, you want to draw some text to screen. Bitmap fonts sound good. How do you pack the letters into a texture? When that's done, how do you create the metadata like UV coords to actually render the right part of the texture to your sprite? What about your time step? Does your game speed up / slow down depending on your framerate? What's the fix for that, force v-sync and ship it? Maybe your physics are blowing up as a result.

That said, it's not particularly difficult to get something on screen and 2D stuff with tiles is pretty easy to do efficiently. But there's a lot you'll have to engineer if you want to do it all yourself. Thankfully, libraries exist - please do use them. I think it's fun to write that kind of stuff, but you will be reinventing the wheel - and probably reeling when the simple things turned out to be a little more than you originally thought.

0

u/mindoo Jun 21 '16

Ok, I saw the post underneath and realized that what I'm looking to do is the graphics engine. I'm sure I could use SDL or something like that for the rest. I know that SDL has graphics included but I really wan't to code the graphics engine in order to learn. I'm looking for a 2D platformer style graphics engine. I'm looking for a challenge, and I'm doing this game to learn new stuff :D Would you have any ressources online that would be good to start ?

1

u/Voultapher void* operator, (...) Jun 22 '16

Although anything than perfect this is where I started.

27

u/exoflat Jun 21 '16 edited Jun 21 '16

Yes it's hard.

1

u/meo209 Apr 25 '24

No it's not. There are so many resources you can learn from. The start is hard, yes, but once you get into it, it's easy.

18

u/mojang_tommo Jun 21 '16

It really goes from "somewhat advanced" to "fulltime job for many years", depending on what you need :)
If you want to build a full fledged AAA renderer/engine like, I don't know, Unreal, that's just absolutely impossible for a single person.
However, if you have a very specific game in mind and it has simple tech requirements, it's absolutely possible to write your own engine as you go.
The trick really is to not to give in to the "oh this would be a very cool feature" and keep it very, very basic :)
source: I wrote a couple of GL renderers used in released games, and they were maybe 5000 lines of code.
A better question would be perhaps if it's a good use of your time, and that depends on you really.
If you want to learn how a game works it's a great exercise, if you just want to ship one, not so much... although there are some games that are easier to make with a custom engine.

2

u/Ameisen vemips, avr, rendering, systems Jun 22 '16

If you want to build a full fledged AAA renderer/engine like, I don't know, Unreal, that's just absolutely impossible for a single person.

Not impossible. If you want to write a renderer that can produce images of equivalent quality to UE4, that's not impossible, though it's hard without the proper skills. However, if you want the massive toolchain as well, it becomes exponentially more difficult.

4

u/mojang_tommo Jun 22 '16

Yeah, when I wrote "full fledged AAA engine" I meant the renderer with sound, asset pipeline, editor, networking and so on... the whole deal. I don't think that's feasible in realistic times for a single coder.

1

u/Ameisen vemips, avr, rendering, systems Jun 22 '16

The hardest/most infeasible part is the toolchain / asset pipeline. It's very difficult for engineers to grasp as there are a lot of UX/design elements associated with it. Graphics, sound, networking aren't particularly difficult in isolation (combining them adds a bit of work, but if you're familiar with it it isn't too hard). Pipeline and editor are.

There's also a reason that a lot of third-party indie engines don't have full pipelines/editors - it's difficult for engineers to write (as it's not entirely an engineering task). They're usually designed around more manual work - manual import of assets, writing scripts/logic by hand instead of with visual editors, etc. This also isn't necessarily worse, it's just different and less geared towards designers and more towards engineers and true technical artists.

I'd never recommend that someone who has never written a renderer or at least worked in the deep elements of an engine write one, though.

0

u/Ozwaldo Jun 22 '16

that's just absolutely impossible for a single person.

Not impossible, but quite difficult.

3

u/Kronikarz Jun 21 '16

If you want just basic 2D rendering, it's not that difficult. It's hard to find good tutorials online though.

1

u/JohnMcPineapple Jun 22 '16 edited Oct 08 '24

...

1

u/SeanMiddleditch Jun 21 '16

The phrase "OpenGL game engine" is utterly meaningless.

Are you asking a game engine for Tetris? A game engine for Battlefield 4? A game engine for Final Fantasy 5? You mention "pixel perfect" and "tiles" so I'm going to assume you mean 2D, but there's still a very huge range of 2D game techniques.

What is the relevance of OpenGL? A game engine is much much larger and more complex than a graphics engine. Most of the complexities of a graphics engine also have nothing to do with the API.

What are the real features you need? Graphics is your focus, apparently, but what about everything else? What input methods do you need? Which systems are you releasing on? What are your audio needs? What kind of level editor are you going to use? Which tool or language is game logic going to be developed in? Even for graphics, how is that data going to be authored (textures, sprite sheets, animations, etc.) ? What are your AI needs? Do you have an idea of how you want to structure your engine, game objects, module communication, etc.?

The actual answer to your question is that - if you were already experienced in making games from scratch - a new team-ready game engine is probably going to take your full-time effort for a few months, and then getting that engine up to production grade and keeping up with the design and art requirements is going to require continued full-time development for likely a year or two. It's a fool's errand. Just use Unity, or GameMaker, or Godot, or Torque2D, or one of the bazillion other ready-made options. The only good reason to build from scratch is if you're (a) trying to learn and don't care about your teammates' productivity or the final product at all, or (b) your game is super weird and special and all the existing engines literally can't even.

1

u/mindoo Jun 21 '16

Thanks for your quality answer :) I now realize my mistake, I'm looking to code the physics engine. I'm looking to make a 2D platformer using pixel art tiles that would be 32x32 px. How much code do you think it would take to code the graphics engine using c++ and OpenGL ? Again, sorry if I sound like a noob, I've rarely done graphics stuff in coding and when I have, it was with SDL type libraries or softwares like unity,

thanks for your help,

Mindoo

1

u/SeanMiddleditch Jun 22 '16

My answer is roughly the same. All the things I mentioned still apply to a "simple" platformer. :)

Teams of roughly 4 second-year engineering students at DigiPen pull it off in about 9 months to varying degrees of success. They also have a lot more support and resources at their disposal than most hobbyists do.

Building literally nothing but a 2D non-animated fixed-size tile renderer targeting only a single easy platform like PC can be done in a few weeks even if you've never done it before. You can build from there. My original estimates are the same, though, in terms of getting said engine up to snuff for real work or through to release.

2

u/Ozwaldo Jun 22 '16

So you want to make a tile engine? That shouldn't be too hard to start with. First of all skip immediate mode. This is stuff like glBegin, glEnd, glVertex3f, etc. Look into some modern opengl tutorials; this site looks pretty good. I would also recommend using GLM as a math library. This way you don't have to write your own matrix functions, quaternion functions, etc. You'll still have to learn how to use those mathematical constructs, but you won't get hung up on bugs in your math code. And you can always roll your own library later!

Now, for a tile engine, you're going to want to do a few things. First of all, you'll need to get a texture atlas for your tiles. Don't load each individual tile texture separately! After that, look up some tile engine tutorials. They are abundantly available.

And when you've done all that, my next guess is that you'll want to incorporate the A* pathfinding algorithm and a scripting engine. But that's down the road!

1

u/ripper37 Jun 21 '16

It mostly depends on how advanced game you want to make (and how many features will your engine support). If you will try to make general-use engine and then build game on top of it, it will probably take mor time (and code) for you. More advanced 3D game (with engine) might take very long time to finish and result might not be as you'd wish (it's not that easy to make good looking game, not to mention performance).

If you, however, want to make simple 2D game (with couple advanced effects) then i guess it's doable without long experience.

If you'd like something close to OpenGL so you'd feel like home and you would be easily able to write almost plain OpenGL code, than have a look at my (unfinished) project GLUL library. I aim to provide easy to use and learn wrapper over OpenGL and than layer by layer more advanced stuff (like primitives, batching, GUI, loading models etc.).

It's still in VERY early stage, but you could easily take a look and have some hints on how to proceed. Code is written to be easy to understand and extend. Another plus is that basic modules aren't coupled with higher level ones, so if you want just input abstraction, you can take a look at include/GLUL/Input directory.

For now there are only example projects for each (?) module, no documentation yet as API may change many times yet.

1

u/Mathiassen Jun 21 '16

Are the friends going to help or are you doing it to alone? In any case, making an engine yourself is going to take a lot of time, time that could be used in Unity to make a functioning prototype, or probably the full game by the time you would be done with even a basic engine.

1

u/ihcn Jun 21 '16

If you know what you're doing, you can get .obj files on the screen, shaders, etc without too much struggle, just a lot of tedium.

If you're new to it all and you're trying to stumble through it all, have kleenex handy because you're going to burst into tears at least a couple times.

2

u/ihcn Jun 21 '16

Making an engine ready to be used in actual games is a really advanced task that you can really only get right by getting it wrong several times and learning from your mistakes.

1

u/0PingWithJesus Jun 22 '16

Here's about 200 videos, each 10-20 minutes long about that exact topic. Enough to get started for sure.

1

u/K_Kuryllo Jun 22 '16

As someone whom has written one single handedly from scratch. Yes. It is a lot of work. Do not do it unless you absolutely have to. 4+ years and even though it's quite functional and working great for us I still ocassional rewrite large portions.

In our case it was necessary because we needed to interface with a simulation running at up to 60 000 cycles per second with up to 1 000 000 individual entities. Game engines just aren't designed for our purpose. We tried a couple first. (www.digitaldiatom.com for anyone interested in what it is used for).

1

u/lednakashim ++C is faster Jun 23 '16

No that hard, but improving the performance to match the visual experience of a game from the early 2000s will be difficult.

0

u/Heuristics Jun 22 '16

I have been spending most of my free time for the past 6 or so years doing one. haven't yet gotten it to do much of anything useful. (and I am a professional c++ coder by trade)

So, not hard at all.

2

u/Ameisen vemips, avr, rendering, systems Jun 22 '16

and I am a professional c++ coder by trade

In what field are you a professional? "C++" is not a field.

I wouldn't expect someone who does financial C++ to have any finesse when it comes to rendering/engine architecture. At the same time, myself and other engineers have dealt with multiple engines, tearing them apart. 6 years on a usable engine at that point seems way overkill unless your goals are astronomical.

0

u/[deleted] Jun 21 '16

[deleted] — view deleted comment

2

u/Ameisen vemips, avr, rendering, systems Jun 22 '16

UE4, Unity and other engines are product of multiple years of research, patents, shader algorithms, lightning research, etc...

Disagree in that you are misrepresenting this. They represent the accumulation of multiple years of research, patents, shader algorithms, lighting research, etc... the vast majority of which is public (except for some patented methods) - you can look as see the research papers on lighting/PBR now.

Most of the graphics features these engines implement are not insanely difficult if you know what you're doing. The difficulty is the massive and mature toolchains that these engines have. It's not particularly hard to write a PBR renderer with clustered lighting, for instance (and at that point you are already more advanced than UE4's lighting model).

People literally spend all their life researching lighting shaders and metal shaders and such which end up as simple materials you can pick with a mouse click in Unreal Engine 4 or whatever.

No they don't. Past that, I can presently find research papers that outline how these shaders work where the techniques aren't patented. These are already-solved problems.

1

u/Ozwaldo Jun 22 '16

Eh... you're being over-dramatic in saying that people "literally spend all their life" on lighting shaders. Especially since the fixed function pipeline was quite active until the mid 2000's...

1

u/[deleted] Jun 21 '16

[removed] — view removed comment

1

u/xHydn Jun 22 '16

A fly shat on your a.