I'm not sure the term for what SDL2 is doing, but I was reading that it "hijacks" main.
I have a project called CoolGame
and a project which I want to be reusable across many other games I make, called Engine
.
Here is my engine.cpp
#include "engine.h"
using namespace engine;
Engine::Engine()
{
window = NULL;
renderer = NULL;
}
Engine::Engine(const char* title)
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, 0);
}
void Engine::RenderThisFrame()
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
Some of you may see where I am going. I am trying to make a nice Game Engine wrapper for sdl2. Here is my main in coolgame
#include "engine.h"
#include <iostream>
#undef main
using namespace std;
using namespace engine;
int main(int argc, const char* argv[]) {
cout << "break main." << endl;
Engine e = Engine("title");
e.RenderThisFrame();
return 0;
}
This does not work. I also have tried #undef
in engine.h
and I get the same error as below.
LNK2019 unresolved external symbol _SDL_main referenced in function _main_utf8
When I take SDL.h
and put it directly in coolgame
it works as expected.
Is there anyway to stop SDL2
from hijacking main? Or is there any way to hoist that main hijack to the coolgame
project? If that makes any sense
For austerity here is
engine.h
#ifndef ENGINE_CODE
#define ENGINE_CODE
#include "SDL.h"
#undef main
namespace engine {
class Engine {
public:
Engine();
Engine(const char* title);
void RenderThisFrame();
private:
SDL_Window* window;
SDL_Renderer* renderer;
};
}
#endif
Edit
I am using Visual Studio 2019 for my IDE if that matters, I can provide C++ build tools version if requested.
Edit
I don't believe this question is exactly answered by another question. Here's why.
I have added SDL_MAIN_HANDLED
and removed the #undefine main
and it still didn't work.
I then moved all my engine code into the coolgame
project with SDL_MAIN_HANDLED
and main running as expected. Lo and behold it works, it has something to do with the projects being referenced. So this does not fix my original question. I am adding a Visual Studio tag.
I have added the engine folder to the include directories in
coolgame
and I have also added packages/.../sdl
to the include directories