Public Group

# Using the message loop in another source file?

This topic is 2170 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So after learning a bit about DirectX, I understand that most of what goes on happens inside the message loop.

Is there any way to take everything inside the message loop and reference it to another file? I would rather not have all my game code crumpled up into one source file.

I tried creating a function, void Game(), which is called within the message loop. I declared it in a header file, and defined it in another source file that had the same #includes as the main source file. However, any of the creation, drawing, and present commands are unrecognized. I'm guessing it's referencing something within WinMain().

But the only immediate way I could see this being resolved is by including the main source file, which is a horrible idea. Either that, or inputting all the necessary functions as parameters.  (Which would either be tons of parameters, or a really large struct.)

But I see games and tutorials that are able to use a simple Game() function...

So if not, how can I do all my drawing somewhere BESIDES the main source? I would rather put the initialization as far behind me as possible.

##### Share on other sites

Hello there!

For some reason I don't really understand what you mean but I will try to answer your question...

What is you framework? How is everything working?

+There are many good articles about stuff like this: http://www.gamedev.net/page/resources/_/technical/game-programming/how-to-structure-a-game-r3113

for example... Just pick 2 and read them.

I assume you don't want yout whole game to be in one file and you want to split the things up. First it makes common sence the core game's main loop to be after the initialization function. I wouldn't spli them up. Though every other component would get its own file. I'll give an example on how to do this.

objects.h

#pragma once

class Entity
{
public:
virtual void update(float deltaTime) = 0;
virtaul void render(/* render context */) = 0;
};

class Player : public Entity
{
public:
void update(float deltaTime);
void render(/* render context */);
};

//... and so on...


objects.cpp

#include "objects.h"

void Player::update(float deltaTime)
{
if(key_pressed('W')) {
move_forward();
}
//... and everything else you would like to see in your player
}
void Player::render(/* context */)
{
if(player_visible()) {
context->render(get_player_vertex_data());
}
}


main.cpp

#include "objects.h"

void init()
{
}

std::list<Entity*> objs;

void loop()
{
while(1)
{
//handle events
while(there are messages)
{   }
for(auto i = objs.begin(); i != objs.end(); ++i)
{
(*i)->update(time);
(*i)->render(context);
}
}
}

void kill()
{
}

int main()
{
init();
loop();
kill();
return 0;
}


Everthing declared can be used, if it is defined somewhere in any of the .cpp files

• 11
• 32
• 12
• 15
• 15