Jump to content

  • Log In with Google      Sign In   
  • Create Account

MrFraggs

Member Since 02 Jan 2012
Offline Last Active Nov 25 2013 08:12 PM

Topics I've Started

Advice With Game Loop

09 July 2013 - 09:49 PM

Hey everyone, how goes it?

 

So i've been working on programming a game in C++/SDL2.0 ever since SDL 2.0 release candidate came out. I have spent about 2 days straight of doing nothing except trying to find and implement a game loop I thought would be best for me. Now, I don't mean best ever, I just mean deciding between fixed step and variable step, etc. Basically what I have now is a game loop that calls the Update() function about 60 times per second, while not capping the framerate. I have been thinking about also capping the frame rate to 60, but i'm not sure if that's necessary yet, as all I draw on the screen for now is a blue color. The Update() function will update all of the game logic when it starts to be implemented, while Render() draws everything. Ever since I switched back to C++ from XNA, i've been trying to basically emulate a XNA framework into C++, as that's where I found it to be the easiest. 

 

If it matters at all, i'm planning on making a 2D tile based game. So do you guys mind taking a look at my main game loop, and telling me if I should change it over to a different setup? Here is the code to just the main game loop:

 //Event structure
	SDL_Event e;

	const int UPDATES_PER_SECOND = 60;
	const int MS_PER_UPDATE = 1000 / UPDATES_PER_SECOND;
	const int MAX_FRAMESKIP = 5;
	
	Uint32 elapsedMS = 0;
	Uint32 startTime = SDL_GetTicks();
	Uint32 nextGameTick = SDL_GetTicks();
	int loops;

	int numUpdates = 0;
	int numFrames = 0;


	while(game->IsRunning())
	{
		while(SDL_PollEvent(&e)) 
		{
			//Exit the game if the user hits the 'X' button on the window.
			if(e.type == SDL_QUIT)
				game->SetIsRunning(false);
		}
		
		//Game update, render loop	
		loops = 0;
		while(SDL_GetTicks() > nextGameTick && loops < MAX_FRAMESKIP)
		{
			game->Update();
			nextGameTick += MS_PER_UPDATE;
			loops++;
			++numUpdates;
		}

		//Render the current frame
		game->Render();
		++numFrames;

		//Used to display FPS/UPS
		elapsedMS = SDL_GetTicks() - startTime;
		if(elapsedMS % 1000 == 0) {  //Only update once per second
			game->FPS = numFrames / (elapsedMS / 1000.0);
			game->UPS = numUpdates / (elapsedMS / 1000.0);
		}

	} // End main loop

So basically, here are my questions:

 

1) Is this an efficient game loop? By that I mean, will it update at about the same rate on all computers? It's designed to update the logic at 60 times per second no matter what the computer (usually), but the frame rate will change.

 

2) Is 60 updates per second too much? Too little?

 

3) How much should I look into capping the frame rate?

 

Let me know if you guys see anything you think I should change. I've been wracking my brain over this for 48 hours now, i'm ready to move on! :P Thanks guys!

 


MonoGame & C++

22 June 2013 - 07:27 PM

Hey everyone! Before you continue, no, this isn't a post about getting MonoGame to work with C++!

 

Now that everyone didn't run away, I have a question that i've been debating for a few days now. I've been programming for 4-5 years now, and the entire time I have had game programming in my sights. Only recently I have been able to start development on a full game, not a sample game you make from a book where the game runs through and ends. I mean a full game, like one you would buy. I've been working on it for a couple weeks using XNA 4.0, even though I have been aware that XNA is basically dead in Microsofts eyes. I was using this as a learning experience, and I did learn a lot.

 

Now here comes the question. I have two options here to continue, because I feel like as though I know i'm learning, continuing with XNA is counter productive if I were to want to deploy and sell my games (key word, IF. I know not everyone is going to be able to, or want to deploy their games to sell). I could port my game over to MonoGame, which I looked through and scoured the internet for info on. The problem here, is that since it's still in development, there is no content pipeline that you get with XNA, which was a huge part of it. I know MonoGame is basically the go-to thing for XNA devs as well.

 

My other option is to switch to C++. I did what every hobby dev does, and started my tenure of programming trying to learn C++, so I do have SOME experience. (Some = getting a sprite to move with DirectX after I learned the language itself of course). Another thing to keep in mind is that I do all of this by myself, and do not work in a team.

 

More thinking about the future, would it be worth it to port my game to MonoGame, continue developing it, and deal with all the tricky workarounds that MonoGame has as of now, (Version 3.0 or 3.1, can't remember which), or would it be better to just start learning C++ again, and get back into that for game development?  

 

Before everyone comes out with the, "no language is right for every situation, choose what works for you, etc", i'm not looking for advice on a personal level. I'm looking for advice at an industry level. Basically what i'm asking is, would it be worth it for a one man programming team to deal with the MonoGame stuff, or would it be worth it to make the switch back to C++? Taking the time to learn the language isn't a problem (I'm a third year student at college, I have some time before the real world!) What would be more advantageous in the long term is what I want to know. 

 

I'm only looking for opinions here, as I am aware of how many factors can affect a decision like this. I'm not looking for anyone to tell me how hard one is, or how easy another is, as i've had at least some experience in both. Not saying i'm a pro, far from it actually. Just stuck in this tough decision that I can't figure out which side to go with! If it helps, I only work in 2D. Not really working on any 3D games, at least in the foreseeable future. Thanks everyone!


DirectX And Game Programming Organization

17 June 2012 - 01:47 PM

Hey guys, i'm just starting some actual programming with DirectX, and I was hoping someone could go over my code so far and tell me how well it's organized, and how well I could fix it. Thanks! All the program does so far is display a sprite that moves itself across the screen.

Spoiler

Need help finding books

19 January 2012 - 02:24 PM

Hey everyone, I was hoping you all could help me.

I've been trying to find a good book to teach game programming, specifically 2D games. I've been looking through books and the closest one I found was Beginning Game Programming by Jonathan Harbour. The only thing about this book is I haven't had a source code compile without some kind of error. Even copy and paste doesn't take the errors out, and they are such complicated errors that it's beyond what I can do right now.

All the other books seem to focus on 3D gaming. I'm hoping you guys have some good resources on learning 2D game programming. I have a basic understanding of C++, but i'm no expert. If anyone can point me in the direction of a good book or two for 2D game programming, that would be greatly appreciated! I think I might continue reading Game Coding Complete, but I think that also is mostly 3D.

Where to go?

02 January 2012 - 06:37 PM

Hi there, I'm new to these forums but i've been looking around at posts without having made an account. I finally decided to and am looking for a little help.

I've been trying to learn C++ for a while now, with the hopes of making indie games (aren't we all?).
I think I have a basic grasp of the language, all self taught. I'm currently in my freshman year of college, majoring in Computer science.

I've been reading books trying to teach myself, as I haven't really learned any programming from school yet. I'm currently reading C++ Without Fear and Game Coding Complete. I'm almost done C++ without Fear, and just cracked into Game Coding Complete. I also have a few of the Game Programming Gems books on standby for after these.

I was wondering if there's any other books anyone would recommend to me, or if i'm even reading the right ones. I'm looking to make 2D games, mostly games that don't require 3DS Max or programs of a similar nature.

I've been thinking about starting Thinking In C++ by Bruce Eckel, but I haven't gotten around to it. So my question is, are there any other, possibly better books I should be reading or other resources I should get ahold of? Thanks!

PARTNERS