SDL Game Engine (SGE)

Pages: 12
I've been working on a game engine using SDL & OpenGL since yesterday and it's gotten to the first milestone (opening a window and handling events). I wanted to learn OpenGL and I thought a good way of doing that would be to write a game engine that uses it. I picked SDL because I've been kind of nostalgic about it recently (I don't know why). So my game engine is in C now.

What do you think? Does it look useful? Easy to use? etc. (please be honest).

The API is supposed to somewhat resemble OpenGL immediate mode, and I'm trying to make it possible to use it with a minimum of code.

Here's an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <SGE/SGE.h>

int main()
{
	sgeInit();
	sgeSetVideoMode(640, 480, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
	sgeSetEventMode(SGE_POLLING);
	int running = 1;
	while (running) {
		SDL_Event event;
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
			case SDL_QUIT:
				running = 0;
				break;
			}
		}
	}
	sgeQuit();
	return 0;
}

You might notice the line sgeSetEventMode(SGE_POLLING). I don't like libraries that force you to use callback functions for event handling, because I prefer to poll for events myself. But I think some people probably prefer callbacks. For that reason, my game engine lets you pick by setting the "event (handling) mode" to SGE_CALLBACKS or SGE_POLLING. Although it's a very small thing, I thought it was a cool idea.

I think I'm about 1% of the way into writing it, since currently it's just a (partial) wrapper around SDL (I'm intentionally not wrapping SDL entirely, that would be kind of pointless). I need to finish writing the lower level stuff so I can build the higher level parts on top.

Edit: I put it on GitHub: https://github.com/ChrisSwinchatt/SGE
Event callbacks & a screen refresh function are available now. It's still just an SDL wrapper but I'm very happy with what I've written so far.
Last edited on

I think I'm about 1% of the way into writing it, since currently it's just a (partial) wrapper around SDL


That's actually what I was just gonna say - that it looked like you were just writing a low level wrapper around SDL rather than writing a game engine.
I'm building it from the ground up, so for now it's basically just an SDL wrapper, but later it will have higher level features added.
Last edited on
First, my familiarity with SDL is nil and minimal with OpenGL.

Looks fairly straightforward, but there's not really too much difference in how event loops look from one library to another is there?

Not a big fan of the init() and quit(). Is there no way to wrap that up with some RAII? Oh yes. C. Never mind! I don't know why that bugs me so much. =)
cire wrote:
Looks fairly straightforward, but there's not really too much difference in how event loops look from one library to another is there?

Not if they all use polling, no. But this one lets you use polling or callbacks :) (although it uses polling to know when to call the callbacks...).

cire wrote:
Not a big fan of the init() and quit().

Neither am I, really, but one of the main things I wanted to avoid was having variables all over the place*. I think function calls look much cleaner.

* with the event polling it's kind of a necessity, but if you use callbacks instead, you don't have to have an SDL_Event object.

p.s. If I get to a 1.0 release then I'll probably do a binding for C++.
Last edited on
When it comes to Init and Quit, why not just provide a custom entry-point function? You could override main () like the SDL does (which isn't really a problem considering how YOU practically control the SDL in this library) or create a different entry point for the user to take advantage of, like in DarkGDK.
NGen wrote:
why not just provide a custom entry-point function?

What do you mean by that?

NGen wrote:
You could override main () like the SDL does

Does it? SDL has Init and Quit functions... :/
Last edited on
If you look at the headers, you'll find something along the lines of (I don't have SDL installed right now):

1
2
3
4
extern int main (int argc, char * argv []);
extern SDL_main (int argc, char * argv []);

#define main SDL_main 


... probably not EXACTLY the way it does it, but it definitely overloads it by default.

EDIT: Found it in SDL_main.h:

1
2
3
4
#define main	SDL_main

/** The prototype for the application's main() function */
extern C_LINKAGE int SDL_main(int argc, char *argv[]);
Last edited on
I had no idea it did that. But still, SDL uses Init and Quit functions, and for now, at least, I don't want to get rid of them. Maybe I will in a later version.
Now that I think about it, the Init might be useful for if you want to alter certain states of an application before handing it over to your engine.
Nah, engine my bootsky. That's a wrapper.

Like all game engines are wrappers for Direct3D or OpenGL, among other libraries. :P

-Albatross
I know. I know. I just had to do it. :)
Nah, engine my bootsky. That's a wrapper.

And why are any of you ppl in to programming?

It's hard, it's a tough industry, and the median average for skilled programmers 2+ years of work is only $54,000.00 annually.

Nobody can live with that kind of money. The least people should aim for is $68,000.00+.

1) All engines are just wrappers basically.
2) Because we like programming.
3) $54k a year is plenty to live off of unless you want or are planning on pricey cars and houses with tons of trips.
4)$14k more a year really wouldn't make that big a difference if $54k wasn't enough as you would be blowing it on something else.
Last edited on by closed account z6A9GNh0
NGen wrote:
Now that I think about it, the Init might be useful for if you want to alter certain states of an application before handing it over to your engine.

I'm actually planning to have the user handle the main loop themselves (otherwise, event polling doesn't work and they have to use callback functions).

I'm actually planning to have the user handle the main loop themselves


Great, I hate it when libs take that away from me (I'm looking at you, GLUT).
Yeah, me too. I think that libraries should do exactly what the user tells them to do, and nothing else.
closed account (S6k9GNh0)
If you're gonna write an API that's similar to SDL, base it on SDL2 pl0x. The issues with the original SDL API have been consulted and addressed. It feels a bit more intuitive and uses more modern concepts as well.
Last edited on
As far as the sgeInit() and sgeQuit() functions, why doesn't anyone use "atexit()"? Normally I'd just say throw both the initialization and exit into the DLL but if I remember correctly chrisname is a *Nix guy.
Because using (the way you propose it) atexit just hides the shutdown somewhere else, and makes it impossible to do a shutdown without killing the entire program.
I didn't say to privatize the sgeQuit() function, I just think that if you are going to override the entry point anyway then have "atexit()" register the shutdown function while you're at it to save the end user two redundant steps. You make a valid point about not being able to unload without closing the program though, I wasn't thinking of that. DLL's would be the way to go if they had an equal in *Nix.
I think you can do something similar to the DllMain thing under Linux, but it's compiler specific (on gcc, you use __attribute__((constructor/destructor))) and I don't know how widely supported it is. At the moment, having Init and Exit functions is the easiest way for me. Additionally, I'd like to get some actual functionality into the library before worrying about minor details :P

I'm going to have to do some function-combining at some point anyway, because at the moment it takes two function calls to fill the screen with a specific colour.
Pages: 12