Page 1 of 2

Problems Encountered While Developing a Game Engine

Posted: Sat Dec 04, 2010 1:35 pm
by lotios611
I have been working on a game engine and have come across a few problems.

Problem #1
In order to be able to use the regular main() function on windows with SDL, you need to use SDLmain.lib. The problem is, I want my engine to be as simple to use as possible, which means that I only want the user to have to link to my engine, and not both my engine and SDLmain.lib. Is there anyway to do this? I'm using VC++ 2008 Express edition, in case that makes any difference.

Problem #2
I would like it if the user does not have to return any values from their main function.

Instead of this:

Code: Select all

#include <cstdio>

int main()
{
        //Code
	return EXIT_SUCCESS;
}
I want the user's main.cpp to look like this:

Code: Select all

#include "Engine.h"

int main()
{
	Engine engine;
}
Is there any way to achieve this?

Re: Game Engine Troubles

Posted: Sat Dec 04, 2010 2:36 pm
by MadPumpkin
Are you saying that you just don't want it to require you to have SDL.dll with the program? Because if so, this is not legal. It discusses this as one of the terms of use on the SDL website.
If you would like you can just build your engine with many SDL specifics and include them in the engine files so that you don't have to in your main, you would still have to have SDL.dll though. But I recommend just doing it the original way.

Re: Game Engine Troubles

Posted: Sat Dec 04, 2010 3:07 pm
by X Abstract X
So what are you trying to accomplish? You simply don't want your users to have to return a value or add parameters to their main function? If so, I wrote this really ugly code that will help. I know it's not the ideal solution but it works heh. The obvious downfall as far as user friendliness is concerned is that they have to declare Engine globally and pass in their main function. Maybe someone else can come up with a nicer solution?

But really, why not just let the user define the main function? Surely they know enough to be able to do that? If you're linking against SDL and SDLMain in your Engine I'm pretty sure the user won't have to link to SDLMain.

GameMain.cpp

Code: Select all

#include "Engine.h"

Engine engine(&mainFunction);

void mainFunction() {
    bool gameRunning = true;

    while (gameRunning)
        engine.doSomethingCool();
}
EngineMain.cpp

Code: Select all

#include "Engine.h"

int main(int argc, char* argv[]) {
    if (Engine::newestInstance())
        Engine::newestInstance()->run();

    return 0;
}
Engine.h

Code: Select all

#ifndef Engine_h
#define Engine_h

void mainFunction();

class Engine {
private:
    void (*_mainFunction)();

    static Engine* _newestInstance;

public:
    Engine(void (*mainFunction)());
    ~Engine() {}

    void doSomethingCool();
    void run();

    static Engine* newestInstance() {
        return _newestInstance;
    }
};

#endif
Engine.cpp

Code: Select all

#include "Engine.h"
#include <iostream>

Engine* Engine::_newestInstance = 0;

Engine::Engine(void (*mainFunction)()) : _mainFunction(mainFunction) {
    _newestInstance = this;
}

void Engine::doSomethingCool()  {
    std::cout << "Hello World!\n";
}

void Engine::run() {
    (*_mainFunction)();
}

Re: Game Engine Troubles

Posted: Sat Dec 04, 2010 5:58 pm
by lotios611
I can live with the user having to return a value in main(), the main problem is that I link SDLmain.lib in my engine and everything compiles/links fine, but when I test out my engine, I get this error:
LINK : fatal error LNK1561: entry point must be defined
This is my main.cpp:

Code: Select all

#include <cstdio>
#include "SE_Engine.h"
#include "SE_Window.h"

int main(int argc, char *args[])
{
	SE::Engine engine;
	SE::Window window;
	return EXIT_SUCCESS;
}

Re: Game Engine Troubles

Posted: Sat Dec 04, 2010 6:11 pm
by Zer0XoL
lotios611 wrote:I can live with the user having to return a value in main(), the main problem is that I link SDLmain.lib in my engine and everything compiles/links fine, but when I test out my engine, I get this error:
LINK : fatal error LNK1561: entry point must be defined
This is my main.cpp:

Code: Select all

#include <cstdio>
#include "SE_Engine.h"
#include "SE_Window.h"

int main(int argc, char *args[])
{
	SE::Engine engine;
	SE::Window window;
	return EXIT_SUCCESS;
}
project properties > Linker > System > set SubSystem to Windows (or console, will make both the sdl window and a console window) :)

Re: Game Engine Troubles

Posted: Sat Dec 04, 2010 8:05 pm
by lotios611
Zer0XoL wrote:
lotios611 wrote:I can live with the user having to return a value in main(), the main problem is that I link SDLmain.lib in my engine and everything compiles/links fine, but when I test out my engine, I get this error:
LINK : fatal error LNK1561: entry point must be defined
This is my main.cpp:

Code: Select all

#include <cstdio>
#include "SE_Engine.h"
#include "SE_Window.h"

int main(int argc, char *args[])
{
	SE::Engine engine;
	SE::Window window;
	return EXIT_SUCCESS;
}
project properties > Linker > System > set SubSystem to Windows (or console, will make both the sdl window and a console window) :)
I've tried doing that, but when I compile my test game I still get the same error.

Re: Game Engine Troubles

Posted: Sat Dec 04, 2010 10:21 pm
by combatant936
lotios611 wrote:the main problem is that I link SDLmain.lib in my engine and everything compiles/links fine, but when I test out my engine, I get this error
...
It compiles and links fine until you "test out" the engine... Then it doesn't link right. What do you mean by testing it out? If you get a linker error when you "test out" the engine, but normally you don't, I can only assume that by this you mean you are building under release? I'm just guessing lol but if you are make sure under project properties that you changed what is says next to configuration at the top from debug to release and then add SDLmain.lib to the linker... I could be completely wrong though haha but this is just what I see could be happening.

Re: Game Engine Troubles

Posted: Sun Dec 05, 2010 7:14 am
by lotios611
combatant936 wrote:
lotios611 wrote:the main problem is that I link SDLmain.lib in my engine and everything compiles/links fine, but when I test out my engine, I get this error
...
It compiles and links fine until you "test out" the engine... Then it doesn't link right. What do you mean by testing it out? If you get a linker error when you "test out" the engine, but normally you don't, I can only assume that by this you mean you are building under release? I'm just guessing lol but if you are make sure under project properties that you changed what is says next to configuration at the top from debug to release and then add SDLmain.lib to the linker... I could be completely wrong though haha but this is just what I see could be happening.
My engine is compiled into a debug .lib file. I am writing a game using my engine, and in that game I get the error.

Re: Game Engine Troubles

Posted: Sun Dec 05, 2010 11:28 am
by combatant936
Oh okay good to know.

Re: Game Engine Troubles

Posted: Mon Dec 06, 2010 2:42 pm
by lotios611
I've uploaded a .rar of both my engine and my test game to rapidshare. The folder is 7.7mb, so it might be a little slow to download.

Re: Game Engine Troubles

Posted: Tue Dec 07, 2010 6:33 pm
by lotios611
Well, I got everything to compile and link fine, the only problem is that I have to add SplinterEngine.lib SDL.lib SDLmain.lib SDL_image.lib SDL_mixer.lib SDL_ttf.lib to my linker settings and I have to set the subsystem to windows in my test game. Is there any way to get around having to do that and just being able to link to SplinterEngine.lib? Splinter Engine is the name of my game engine by the way.

Re: Game Engine Troubles

Posted: Wed Dec 08, 2010 1:30 am
by Falco Girgis
lotios611 wrote:Well, I got everything to compile and link fine, the only problem is that I have to add SplinterEngine.lib SDL.lib SDLmain.lib SDL_image.lib SDL_mixer.lib SDL_ttf.lib to my linker settings and I have to set the subsystem to windows in my test game. Is there any way to get around having to do that and just being able to link to SplinterEngine.lib? Splinter Engine is the name of my game engine by the way.
Are you adding those libraries to your librarian's linker input for your engine? I swear that Visual Studio lets me statically link to just one .lib containing the others...

As for not wanting the application utilizing your engine to not have to return a value... tough shit. That's how exiting an application and returning to the operating system works. However, I'm pretty sure that want you want to do is this:

Code: Select all

int main() {
Engine engine;

return engine.exec();
So that your engine is the one returning to the operating system...

Re: Game Engine Troubles

Posted: Wed Dec 08, 2010 5:00 am
by lotios611
GyroVorbis wrote:
lotios611 wrote:Well, I got everything to compile and link fine, the only problem is that I have to add SplinterEngine.lib SDL.lib SDLmain.lib SDL_image.lib SDL_mixer.lib SDL_ttf.lib to my linker settings and I have to set the subsystem to windows in my test game. Is there any way to get around having to do that and just being able to link to SplinterEngine.lib? Splinter Engine is the name of my game engine by the way.
Are you adding those libraries to your librarian's linker input for your engine? I swear that Visual Studio lets me statically link to just one .lib containing the others...
Well, the problem is that I don't know. When I try to add the stuff to the linker settings when building a .lib, I can't find the linker settings, so I change the project to compile as an .exe, change the linker settings, and then change the project back to compiling as a .lib.

I've just tried going into the project file for my engine and manually editing the linker settings from there, but it still doesn't work.
I get these error messages in my test game now:
SplinterEngine.lib(SE_Engine.obj) : error LNK2019: unresolved external symbol _SDL_Init referenced in function "public: __thiscall SE::Engine::Engine(void)" (??0Engine@SE@@QAE@XZ)
SplinterEngine.lib(SE_Engine.obj) : error LNK2019: unresolved external symbol _SDL_Quit referenced in function "public: __thiscall SE::Engine::~Engine(void)" (??1Engine@SE@@QAE@XZ)
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
C:\Programming\C++\Splinter Games\Pickin' Sticks\Debug\Pickin' Sticks.exe : fatal error LNK1120: 3 unresolved externals
I've pretty much given up on problem #2.

Re: Game Engine Troubles

Posted: Wed Dec 08, 2010 6:12 pm
by Falco Girgis
lotios611 wrote:
GyroVorbis wrote:
lotios611 wrote:Well, I got everything to compile and link fine, the only problem is that I have to add SplinterEngine.lib SDL.lib SDLmain.lib SDL_image.lib SDL_mixer.lib SDL_ttf.lib to my linker settings and I have to set the subsystem to windows in my test game. Is there any way to get around having to do that and just being able to link to SplinterEngine.lib? Splinter Engine is the name of my game engine by the way.
Are you adding those libraries to your librarian's linker input for your engine? I swear that Visual Studio lets me statically link to just one .lib containing the others...
Well, the problem is that I don't know. When I try to add the stuff to the linker settings when building a .lib, I can't find the linker settings, so I change the project to compile as an .exe, change the linker settings, and then change the project back to compiling as a .lib.

I've just tried going into the project file for my engine and manually editing the linker settings from there, but it still doesn't work.
I get these error messages in my test game now:
SplinterEngine.lib(SE_Engine.obj) : error LNK2019: unresolved external symbol _SDL_Init referenced in function "public: __thiscall SE::Engine::Engine(void)" (??0Engine@SE@@QAE@XZ)
SplinterEngine.lib(SE_Engine.obj) : error LNK2019: unresolved external symbol _SDL_Quit referenced in function "public: __thiscall SE::Engine::~Engine(void)" (??1Engine@SE@@QAE@XZ)
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
C:\Programming\C++\Splinter Games\Pickin' Sticks\Debug\Pickin' Sticks.exe : fatal error LNK1120: 3 unresolved externals
Fuck only knows what's going on there... you aren't ever supposed to do that. I'm guessing you probably didn't also include the corresponding paths to these libraries? You need to find out how to link against additional libraries with your static library (under "librarian" settings).
I've pretty much given up on problem #2.
Why? The code snippet that I posted is literally THE way to handle that scenario...

Re: Game Engine Troubles

Posted: Thu Dec 09, 2010 1:52 pm
by lotios611
GyroVorbis wrote:
lotios611 wrote:
GyroVorbis wrote:
lotios611 wrote:Well, I got everything to compile and link fine, the only problem is that I have to add SplinterEngine.lib SDL.lib SDLmain.lib SDL_image.lib SDL_mixer.lib SDL_ttf.lib to my linker settings and I have to set the subsystem to windows in my test game. Is there any way to get around having to do that and just being able to link to SplinterEngine.lib? Splinter Engine is the name of my game engine by the way.
Are you adding those libraries to your librarian's linker input for your engine? I swear that Visual Studio lets me statically link to just one .lib containing the others...
Well, the problem is that I don't know. When I try to add the stuff to the linker settings when building a .lib, I can't find the linker settings, so I change the project to compile as an .exe, change the linker settings, and then change the project back to compiling as a .lib.

I've just tried going into the project file for my engine and manually editing the linker settings from there, but it still doesn't work.
I get these error messages in my test game now:
SplinterEngine.lib(SE_Engine.obj) : error LNK2019: unresolved external symbol _SDL_Init referenced in function "public: __thiscall SE::Engine::Engine(void)" (??0Engine@SE@@QAE@XZ)
SplinterEngine.lib(SE_Engine.obj) : error LNK2019: unresolved external symbol _SDL_Quit referenced in function "public: __thiscall SE::Engine::~Engine(void)" (??1Engine@SE@@QAE@XZ)
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
C:\Programming\C++\Splinter Games\Pickin' Sticks\Debug\Pickin' Sticks.exe : fatal error LNK1120: 3 unresolved externals
Fuck only knows what's going on there... you aren't ever supposed to do that. I'm guessing you probably didn't also include the corresponding paths to these libraries? You need to find out how to link against additional libraries with your static library (under "librarian" settings).
I tried doing that, and now I get these warnings when I compile my library:
SDL_image.lib(SDL_image.dll) : warning LNK4006: __NULL_IMPORT_DESCRIPTOR already defined in SDL.lib(SDL.dll); second definition ignored
SDL_image.lib(SDL_image.dll) : warning LNK4221: no public symbols found; archive member will be inaccessible
SDL_mixer.lib(SDL_mixer.dll) : warning LNK4006: __NULL_IMPORT_DESCRIPTOR already defined in SDL.lib(SDL.dll); second definition ignored
SDL_mixer.lib(SDL_mixer.dll) : warning LNK4221: no public symbols found; archive member will be inaccessible
SDL_ttf.lib(SDL_ttf.dll) : warning LNK4006: __NULL_IMPORT_DESCRIPTOR already defined in SDL.lib(SDL.dll); second definition ignored
SDL_ttf.lib(SDL_ttf.dll) : warning LNK4221: no public symbols found; archive member will be inaccessible
I've pretty much given up on problem #2.
Why? The code snippet that I posted is literally THE way to handle that scenario...
I realized that would be the way to handle it a while ago, and decided that I didn't want to design my engine like that.