Advertisement

Making Breakout - Code Structure help requested

Started by September 11, 2017 01:08 PM
77 comments, last by Kylotan 7 years, 5 months ago
26 minutes ago, Kylotan said:

Chrono is part of the standard library. It's not called the STL, and hasn't been for many years.

I'm confused by this line, didn't I said the same thing? :D

quoting myself:

Quote

 part of the STL, you just need to #include <chrono>

isn't STL == standard template library == standard library?

Also the window title is holding the ms since last frame display, that's why I keep refreshing it

STL is the Standard Template Library. The "Standard Template Library" is an old name for part of the C++ standard library, specifically the containers, algorithms, iterators and functors. These days, we just talk about the standard library, which includes the former bits, plus tons of new stuff. Much of it is templates, but not all.

Sorry, must have missed the milliseconds in the window title. I suggest getting some other form of onscreen text debugging would be a useful thing to do. :)

Advertisement

I see, also forgot to add you a thanks in the previous reply for the FrameSkip explanation, fixed :P

Though standard library doesn't sound as cool as STL, that's unfortunate :/

 

I found a tutorial for making Breakout a while back. It uses OpenGL version 3.3. In my opinion it has a pretty good code structure, but it can be improved upon. It has a static singleton/global resource manager, which just simplifies the loading of assets. "Game Coding Complete" written by Mike McShaffry and David 'Rez' Graham has a better but far more complex way of loading/managing resources. I mentioned it because it contains good examples of how to structure you game, but they're a lot more complex than what you need for Breakout. Here's the link to the tutorial https://learnopengl.com/#!In-Practice/2D-Game/Breakout

I know that OpenGL tutorial :P At a certain point in time during the last year I was following it, I had to abandon it because it wasn't enough clear when he start making shaders and uses GLSL and stuff wasn't compiling on my end, and I had nowhere to ask, but with you guys help now things would probably be different! :)

Thanks for letting me know about Game Coding Complete, not scared about complexity don't worry, (I paused this project because I wanted to do some simple composition using templates for this project and follow

" rel="external">what he does in this video which I yet can't understand , so I am studying more C++ and templates right now, beforegoing back to his video multiple times and then resume this project :P ) so eventually I'll be picking up that book

That tutorial should help give you a basic understanding of how to do some of it, and the book i suggested should for most of it, if you can get your code to compile. The tutorial compiled for me. I used Glew 2.0 extension loader instead of GLAD. Also, OpenGL won't show anything in version 3.3 core profile or above without using shaders. Idk if you looked in the Getting Started section of the tutorial, but if not then try following through it to make sure you can get the triangle to display using shaders.

Advertisement

The getting started section is where I couldn't proceed if I remember correctly, this link -> https://learnopengl.com/#!Getting-started/Hello-Triangle this code about vertex shader:


#version 330 core
layout (location = 0) in vec3 aPos;

void main()
{
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

stuff just won't compile...

It is not clear also, am I supposed to copy-paste this inside main? or outside of main? or in a totally separated file? x_x

I think I tried all of those and just got red squiggly lines all over the place...

Its suppose to go in its own file, that gets compiled at run-time, not compile time. The way he stored it in the Hello-Triangle tutorial was actually in a const char*. Scroll down past the image of the triangle, and you'll see the full source for it in blue. Here's how its stored

const char *vertexShaderSource = "#version 330 core\n"
    "layout (location = 0) in vec3 aPos;\n"
    "void main()\n"
    "{\n"
    "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
    "}\0";

Normally this would be in its own file, so it can be modified and reloaded/re-compiled with a key press. He started putting it in its own file in the 5th tutorial:

https://learnopengl.com/#!Getting-started/Shaders

 

I see :D

Personally I think it was an important point to clarify when he first presented that new code, to not have begginners like me totally lost and throwing their hands into the air, so thank you for clarifying that :)

 

My code is not compiling, not sure why o_O

Let me explain the whole current situation.

I have this static constexpr member variable inside App class in App.h which rapresent how many times in a second we enter the Update function for the entire game:


class App
{
//GameUpdateTickPerSecond   suggested ratio 1/25 seconds = 40ms per udpate
static constexpr uint8_t GameUpdatesPerSec = 25;
  //...other stuff
};

then I have this using-declaration on top of Timer.h 


//use this tipe to declare the ticks per second of the update call
template <size_t N>
using UpdateTickDuration = duration<uint32_t, std::ratio<1, N>>;

it is templated, so that the call inside my App::GameLoop function looks like:


static constexpr UpdateTickDuration<GameUpdatesPerSec> GameUpdateTPS{ 1 };

which should resoult in UpdateTickDuration<25> which in it's expanded form it's simply:


std::chrono::duration<uint32_t, std::ratio<1, 25>>

so if I cast an UpdateTickDuration type (with a value of 1) to milliseconds, then I would get 40ms.

Now, my entire game loop looks like this:


void App::GameLoop()
{
	static constexpr UpdateTickDuration<GameUpdatesPerSec> GameUpdateTPS{ 1 };

	while (Game->IsRunning())
	{
		//UPDATE
		while (Timer->CurrentTime() > Timer->NextUpdateTime)
		{
			Game->Update();
			Timer->NextUpdateTime += GameUpdateTPS;
		}
		
		//DRAW
		float Interpolation = Timer->ComputeInterpolation(GameUpdateTPS);
		Game->Draw(Interpolation);

	}
}

So as you see, I am computing the Interpolation from within the GameTimer class, so my problem could also be there. That function looks like this:

Declaration in GameTimer.h:


template<size_t N>
float ComputeInterpolation(const UpdateTickDuration<N>& gameUpdateTPS);

Definition in GameTimer.h:


template<size_t N>
float GameTimer::ComputeInterpolation(const UpdateTickDuration<N>& gameUpdateTPS)
{
	auto TimeOffset_ns = CurrentTime() + gameUpdateTPS - NextUpdateTime;
	auto GameUpdateTPS_ns = duration_cast<nanoseconds>(gameUpdateTPS);

	return (float(TimeOffset_ns.count()) / float(GameUpdateTPS_ns.count()));
}

The error I am getting is this:


Errore LNK2019    riferimento al simbolo esterno "public: float __thiscall GameTimer::ComputeInterpolation<25>(class std::chrono::duration<unsigned int,struct std::ratio<1,25> > const &)" (??$ComputeInterpolation@$0BJ@@GameTimer@@QAEMABV?$duration@IU?$ratio@$00$0BJ@@std@@@chrono@std@@@Z) non risolto nella funzione "public: void __thiscall App::GameLoop(void)" (?GameLoop@App@@QAEXXZ)    Breakout    Z:\Asus\Documenti\Visual Studio 2017\Breakout\Breakout\App.obj    1    
 

This topic is closed to new replies.

Advertisement