- Details
- Category: Game Development
- By Stuart Mathews
- Parent Category: Code
- Hits: 3663
I had a good weekend. I spent most of it reading the new Game Coding Complete – 4th edition book that I got. I headed down to the local post office collection depot after the gym and was happy to get 2 books(the one has gone AWOL but I’m not too worried about that particular one right now.) So basically I got the Alan Thorn book on how to design and implement a game engine (not started reading yet) and the GGC4 book which I did start reading.
So far I’ve learnt a lot about game development, the topics include using shared pointers, resource caching, basic game design philosophy, which in this case is reminiscent of the observer/publisher/subscriber pattern. Basically you have 3 components who all talk to a each other by way of a central component(the game logic component). The other components are the application layer and the game views. The game views are the manifestation (or eyes) of what the game logic component determines is happening in the game world. The other is the OS specific layer, which is the application layer.
The game logic code basically gets stimulus from the other layers and uses that stimulus(events) to modify the game world. So if the game view says move left, the game logic sets the car to move left – that sort of thing. Its actually pretty cool because the game logic creates and manipulates what the world is and what its state is(what its doing) and it does this by determining how physics, incoming movement events would manipulate what would happen in a simulation. Actually that's what the game logic does, it runs a simulation of the game world and the game views just interpret the game world’s simulation, mostly by drawing it or reacting to it. So the whole events being send around mechanism is very reminiscent of OOP and well these books bang on about C++ unsurprisingly. I can see the benefit however and I’m still wondering if I should use C instead. Because as great as the code is that does stuff, its all basically C which has been(in my opinion) abstracted into classes – which annoys me because as much as I like working with objects, they make things complex(though they are reusable). I’m not going to be writing a game engine every year so I wonder if i really need to spend time following their examples in C++ and rather just re-write them in C – for the fun of it.
Like for example, they tend to use a common VUpdate() function on all things that can be updated in one timer tick, and they then put that as a virtual function in a base class for other updatable objects moving forward. Fine. They also use interfaces and the factory pattern to decrease the impact of change in the code base. Fine too. They use C++ shared_ptr too to easily allocate memory and forget about it(effectively leaving de-allocation to itself) – also fine. They use a process manager like what linux does in the kernel. Fine. Can all this be done in C – you betcha. They also talk about using your own memory allocator, which is a new thing for me but you can do that in C too. I guess its not about if it can be done in C or C++ – it can, the question is would you if you’re a pro dev in the industry. I think the question is probably not due to the time it take to write C and re-write data structures and algorithms. There is no STL in C. There is no interfaces in C. You’d need to massage the factory pattern in. I’m enjoying the speed at which C++ makes code progress but not at the complexity and abstraction it provides – others will argue that the abstracts is awesome. It might be.
GCC4 talks about DirectX11. I was learning SDL and although I’m learning DirectX lingo in this book, I like the SDL route so I’ll continue just learning the concepts and then apply them to SDL work. The other book which I’ve not read uses SDL. That's kinda good. Its version 1.3 though and I’m all up for V2.
I also did some university work/assignment. This is all about classic software engineering principles. So I’m talking about drawing conceptual diagrams and putting associations and multiplicities between classes, collecting requirements and interpreting them. I quite enjoy this but find it quite boring. I like object and class drawings but hate the time it takes to do. I wonder if that's why no one seems to do them anymore. I got some good results but also which was good. The tutor’s comments included that I should stick to the wording of the course – I’m damned if I’m going to do that. That’s just a waste of time honestly - I’d have to scrutinize the text books and activities just to phrase the thing just the right way when my words mean the same thing! I don't care if its easier for him to mark or not, right is right and that's really the point - I’ll use the way that's the simplest for me while being factually correct also. You can’t mandate how to say things in the real world, people just go and say them and if right, that's it. Their is no point, in my mind, spending the extra strain and pain(because its really painful) trying to fit into everyone else's vision because its easy for them. In the real world, when someone explains something to you, they use whatever vocabulary that makes sense to them, and you interpret that for factual correctness. You dont stop the architect in mid-sentence and say, “don't use that word, I prefer this one”, He’ll just get frustrated with you and say “man its the same thing, do you get what I’m saying?” and in most cases you will have. You can stop every one from changing how they explain things. I wish I could because it would make be understand things easier because I would be familiar with terms I know but the world doesn’t work like that – you have to listen and interpret. That’s how the real world works. No one will say the same thing the same way as everyone else.
I also watched some videos this weekend, basically that was Man of Steel – Superman(I enjoyed this), About a boy and 40 days and 40 nights, both which were OK. Its nice to watch videos on weekends – no other time.
I also watched the tail-end of the England-France game in the 6 nations. I enjoy watching rugby. I’ve had times when I’ve thought that I could take up rugby again but I dont think so, its just not worth getting injured for – especially if its no your life’s passion and its not mine – it was drummed right out of me in high school.
- Details
- Category: Game Development
- By Stuart Mathews
- Parent Category: Code
- Hits: 5201
I’ve started thinking about writing my game engine in pure C because I think as OOP as C++ is, it makes things rather longer and more complex – mostly because it adds more code by default and more abstraction when its not needed. The issue I have is that by default you’ve got it and it goes on and on and on from there until you have a large codebase with lots of stuff in it and you forget what the design you used is all about because you’ve made a class for just about everything, then you’re jumping around the codebase trying to piece it all together – seemed like a good thing to do at the time!
SDL for instance is written in C and a lot of people interface their C++ code with it. I downloaded SDL version 2 and started seeing how it works. First of all its incredibly easy to obtain and setup, I was impressed. I had visions of it being a mammoth task something akin to setting up Windows Driver SDK or worse. I set it up in visual studio 2015 and it was just a matter of downloading the library and linking against it. So in Visual Studio, a quick couple of teaks to the linker and include directories is all you need.
A lot of books on 3D graphic rendering and game engine are written in C++. Perhaps it will soon become apparent to me why this is. Is it just convention, much like Java is to students learning OOP, c++ is to game coding? I have seen game engines written in C so I’ve not really put off from going against the grain? Doom was written in C and if C is good enout for ID Software, C is good enough for me. boom.
Anyway, I got a simple SDL window up and running, a primitive game loop and could paint things onto the screen. When I say things, its just a static picture within the confines of a window. I know, advanced stuff huh? I got to know some SDL concepts such as SDL_Surface, SDL_Window, SDL_Renderer, SDL_Texture and how to initialize the SDL system. I also learned, mostly from lazyfoo’s tutorials online, how to incorporate some SDL extensions to load .png files and interact with the keyboard. The thing about all of this is that I’ve been itching to put my game loop to the test. I wrote or rather took advice from a professional-grade game loop I read in “Core Techniques and Algorithms in Game Programming” which explained pretty much all the elements that make up a game loop, from updating the players status, updating the world, rendering, animation, AI and everything it seems into a good structure. I’ve replicated that structure with placeholders for all those tasks. I wanted to fill some of them in. SDL is my “cheat” to see what i could put where. And so far, I’ve seen that I can fill in some code in the game loop using SDLs controller/input code and perhaps some very rudimentary initializing of the graphics in the game loop:
So today I took my game loop that I’d been working on and incorporated much of the above code(just to get a SDLWindow up and running and showing a picture) into that. This is good because I’ve moved from Visual Studio and a dependency on Windows()I was using timegetTime() Win32 API function for determining the framerate of my gameloop) to running all of this under Mingw, incorporating both my C library(stulibc), glib(yay!) and SDL into one autotools project. Their is a slight bit of hackery around using hard coded paths for stulib’s include and lib files along with the same for using SDLs Image extension. Also it seems i cannot use the main stulib.h header file at the moment so I’ve resorted to using specific header files for specific functions – like safettchecking.h for CHK_ExitIf() function, which I use to initialise/check some SDL functions.
And because I’ve factored the SDL code into my original game loop in C, I’ve started to fill in some of the pieces that were missing in the game loop, namely the sense_player_input() function which now boasts a rudimentary event loop query to determine what the user is doing at the keyboard. Currently it just prints out “up”, “down”, “left” and “right” when you hit these keys on the keyboard, while processing the game loop.
The other great thing about all this is that under mingw, I’ve effectively made large inroads into being able to develop in both Windows and Linux and hence be cross-platform – specifically I’ve got a POSIX environment. For instance, I’ve been able to replace the Win32 time function timegetTime() with glib’s own version, g_get_real_time() so this makes it work on any of the two platforms. Also being SDL, I’ll have again appeased my cross-platform ambitions by its very existence – its a cross platform library. When I need to make the leap (and it probably is a leap) to 3D graphics rendering, SDL will thankfully be able to use Direct3D under windows and OpenGL under Linux.
Granted I’ve only touched the surface of all of this, writing all this in C makes me happy. I wonder if I’ll be forced to move to C++? I suspect not, but perhaps its possible to use a hybrid approach? I would certainly think that incorporating a scripting language for the AI might introduce OOP at the point that really its needed. I’m not too sure OOP is really needed for low-level game engine stuff.
So now I’ve got the best of both worlds, I can run it potentially in Linux and Windows. I’ve linked it against stulibc so I have access to my functions, I’ve linked it to glib so I have access to some other cross platform functions such as advanced data structures and functions and we’re using SDL in a autotools configuration so it should be fairly easy to do this all again under Linux. Woohoo!
I’ve ordered the following books:
Game Coding Complete: This is probably the best book I can see on what I’m undertaking to learn. I looks like the kind of book, “Core Techniques and Algorithms in Game Programming” is – eye-opening.
Game Engine Design and implementation: I’m not expecting miracles from this book, I am from the one above but this is a fallback that might turn into a gem!
3d Games:Volume 1: Real-Time Rendering and Software Technology Vol 1: Real-time Rendering and Software Technology: This is just because I’m new to 3D and need some basics This is old but so are the basics.
And the anti-climax we’ve all been waiting for:
It’s a red box. I know, but not just any red box – oh no! Its a red box that moves when you press keyboard buttons. It also has two blue lines – there is a genius at work here, watch this space!
That is all…for now ![]()
- Details
- Category: Game Development
- By Stuart Mathews
- Parent Category: Code
- Hits: 6468
I’ve been looking into a simple thing and its proven to me that while a simple thing, its pretty complicated to get your head around it. Does that make it not simple? Its a game loop because I’m interested in game design at the moment and game engines are pretty interesting.
Its the concept of performing one routine at a fixed rate frequency like 20 times a second and in the time between perform another routine as many times as possible. Basically its a game loop:
int main(int argc, char **argv)
{
gboolean bGameDone = FALSE;
gboolean bNetworkGame = FALSE;
gboolean bCanRender = TRUE;
tickCountAtLastCall = ticks();
while(!bGameDone) {
newTime = ticks();
frameTicks = 0;
numLoops = 0;
long ticksSince = newTime - tickCountAtLastCall;
while((ticksSince) > TICK_TIME && numLoops < MAX_LOOPS ) {
GameTickRun(); // logic/update
// tickCountAtLastCall is now been +TICK_TIME more since the last time. update it
tickCountAtLastCall += TICK_TIME;
frameTicks += TICK_TIME; numLoops++;
ticksSince = newTime - tickCountAtLastCall;
}
IndependantTickRun(frameTicks); // handle player input, general housekeeping
if(!bNetworkGame && (ticksSince > TICK_TIME))
tickCountAtLastCall = newTime - TICK_TIME;
if(bCanRender) {
float percentOutsideFrame = (ticksSince/TICK_TIME)*100;
GameDrawWithInterpolation(percentOutsideFrame);
}
}
return 0;
}
I’m convinced that because I spent an entire evening trying to understand how this works, that it is some kind of black magic crafted by a magic person.
It works pretty well. In this instance the GameTickRun() is run 20 times a second – ie every 50 milliseconds. And when its not being called, its calling the IndependnatTickRun() and GameDrawWithInterpolation() as many times as possible thereby in this instance, maximizing the rendering but ensuring that the gameplay is consistent irrespective of how fast the CPU is – 20 times a second is 20 times a second in this case. This allows the game to happen at a predictive pace but rendering happens as many times as the CPU can run. Very clever.
More Articles …
Page 8 of 9