Ejemplo n.º 1
0
int main(int argc, char **argv)
{
	srand(time(NULL));

	GARandomSeed();

	bool gameIsRunning = false;

	// SDL
	SDL_Window* window = nullptr;
	SDL_Event sdlEvent;
	SDL_Renderer* renderer = nullptr;

	TextureManager* textureManager = nullptr;
	MyB2DebugDraw* debugDraw = nullptr;
	MyB2ContactListener* contactListener = nullptr;

	// Fonts
	TTF_Font* font = nullptr;

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		return -1;
	}

	window = SDL_CreateWindow("GA Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
	if (!window)
	{
		return -1;
	}

	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

	if (!renderer)
	{
		return -1;
	}

	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);

	// Initialize SDL PNG loading
	int imgFlags = IMG_INIT_PNG;
	if (!(IMG_Init(imgFlags) & imgFlags))
	{
		printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
		return -1;
	}

	// Initialize SDL ttf
	if (TTF_Init() == -1)
	{
		printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
		return -1;
	}

	// Open ttf font
	font = TTF_OpenFont((dir_fonts + "OpenSans-Regular.ttf").c_str(), 12);

	textureManager = TextureManager::GetInstance();
	textureManager->Init(renderer, dir_assets);

	// Game variables
	//float dt = 1.0f / 10.0f;
	double dt_fixed = 1.0 / 60.0;
	float t = 0.0f;
	auto currentTime = std::chrono::system_clock::now();

	debugDraw = new MyB2DebugDraw(renderer);

	debugDraw->SetFlags(b2Draw::e_shapeBit
		//| b2Draw::e_aabbBit
		| b2Draw::e_jointBit
		| b2Draw::e_centerOfMassBit);

	contactListener = new MyB2ContactListener();

	// Box2D
	b2Vec2 gravity(0.0f, 0.0f);
	b2World* box2Dworld = new b2World(gravity);
	//box2Dworld->SetDebugDraw(debugDraw);
	box2Dworld->SetContactListener(contactListener);

	Game* game = new Game(window, renderer, WINDOW_WIDTH, WINDOW_HEIGHT, box2Dworld, textureManager, font);
	game->Init(dir_assets, dir_fonts, dir_textures, dt_fixed);
	gameIsRunning = true;

	vec2 curMouseClickPos;

	while (gameIsRunning)
	{
		while (SDL_PollEvent(&sdlEvent) != 0)
		{
			if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) 
			{
				SDL_GetMouseState(&curMouseClickPos.x, &curMouseClickPos.y);
			}

			if (sdlEvent.type == SDL_QUIT)
			{
				gameIsRunning = false;
			}
			else if (sdlEvent.type == SDL_KEYDOWN)
			{
				switch (sdlEvent.key.keysym.sym)
				{
				case SDLK_w:
					game->GetPlayerShip()->ActivateEventTrigger(Ship::THRUST_FORWARD, true);
					break;
				case SDLK_s:
					game->GetPlayerShip()->ActivateEventTrigger(Ship::THRUST_BACKWARD, true);
					break;
				case SDLK_a:
					game->GetPlayerShip()->ActivateEventTrigger(Ship::TORQUE_LEFT, true);
					break;
				case SDLK_d:
					game->GetPlayerShip()->ActivateEventTrigger(Ship::TORQUE_RIGHT, true);
					break;
				case SDLK_r:
					if (game->GetPlayerShip())
						game->GetPlayerShip()->Reset();
					break;
				case SDLK_e:
					game->GetPlayerShip()->ActivateEventTrigger(Ship::STRAFE_RIGHT, true);
					break;
				case SDLK_q:
					game->GetPlayerShip()->ActivateEventTrigger(Ship::STRAFE_LEFT, true);
					break;
				case SDLK_SPACE:
					game->GetPlayerShip()->ActivateEventTrigger(Ship::SHOOT, true);
					break;
				case SDLK_LSHIFT:
					game->GetPlayerShip()->ActivateEventTrigger(Ship::STABILIZE, true);
					break;
				case SDLK_ESCAPE:
					gameIsRunning = false;
					break;
				}
			}
			else if (sdlEvent.type == SDL_KEYUP)
			{
				switch (sdlEvent.key.keysym.sym)
				{
					case SDLK_w:
						game->GetPlayerShip()->ActivateEventTrigger(Ship::THRUST_FORWARD, false);
						break;
					case SDLK_s:
						game->GetPlayerShip()->ActivateEventTrigger(Ship::THRUST_BACKWARD, false);
						break;
					case SDLK_a:
						game->GetPlayerShip()->ActivateEventTrigger(Ship::TORQUE_LEFT, false);
						break;
					case SDLK_d:
						game->GetPlayerShip()->ActivateEventTrigger(Ship::TORQUE_RIGHT, false);
						break;
					case SDLK_e:
						game->GetPlayerShip()->ActivateEventTrigger(Ship::STRAFE_RIGHT, false);
						break;
					case SDLK_q:
						game->GetPlayerShip()->ActivateEventTrigger(Ship::STRAFE_LEFT, false);
						break;
					case SDLK_SPACE:
						game->GetPlayerShip()->ActivateEventTrigger(Ship::SHOOT, false);
						break;
					case SDLK_LSHIFT:
						game->GetPlayerShip()->ActivateEventTrigger(Ship::STABILIZE, false);
						break;
				}
			}
		}

		// Update game
		//game->Update(dt);

		// Draw game
		//game->Draw();

		// Present final canvas
		//SDL_RenderPresent(renderer);
	}

	if (game)
	{
		delete game;
		game = nullptr;
	}

	if (debugDraw)
	{
		delete debugDraw;
		debugDraw = nullptr;
	}

	if (contactListener)
	{
		delete contactListener;
		contactListener = nullptr;
	}

	TextureManager::Destroy();
	textureManager = nullptr;

	TTF_CloseFont(font);
	font = nullptr;

	SDL_DestroyWindow(window);
	window = nullptr;

	SDL_DestroyRenderer(renderer);
	renderer = nullptr;

	TTF_Quit();
	IMG_Quit();
	SDL_Quit();

	_CrtDumpMemoryLeaks();

    return 0;
}
Ejemplo n.º 2
0
bool Projekt::Init()
{
	CSplashWnd splash;
	splash.SetImage(Gdiplus::Image::FromFile(L"Data\\Textures\\test.jpg"));
	splash.Show();
	splash.SetProgressBarColor(0x00000000);
	splash.SetAutoProgress(0, 100, 50);

	if (!D3D11App::Init())
		return false;

	// Read game settings
	if (!Settings::GetInstance()->ReadFile("Data\\Settings.txt"))
		return false;

	// Initialize effects, input layouts and texture manager
	Effects::InitAll(mDirect3D->GetDevice());
	InputLayouts::InitAll(mDirect3D->GetDevice());
	mTextureMgr.Init(mDirect3D->GetDevice());
	RenderStates::InitAll(mDirect3D->GetDevice());

	// Initialize models
	GenericHandler::GetInstance()->Initialize(mDirect3D->GetDevice(), &mTextureMgr);
	Python->Initialize();


	//Create and initialize the GUI
	Gui->Init(mDirect3D->GetDevice());

	Network::GetInstance()->Initialize();
	Network::GetInstance()->Start();

	//init soundmodule
	this->soundModule = new SoundModule();
	this->soundModule->initialize(this->mhMainWnd, this->mDirectInput);

	
	// Create game
	mGame = new Game(mDirect3D->GetDevice(), mDirect3D->GetImmediateContext(), &mTextureMgr, soundModule);

	// Set if window is fullscreen or not
	D3D11App::SetFullscreen(Settings::GetInstance()->GetData().IsFullscreen);

	// Resize window after we've created our Game object
	D3D11App::SetResolution(Settings::GetInstance()->GetData().Width,
		Settings::GetInstance()->GetData().Height);

	// Create sky
	mSky = new Sky(mDirect3D->GetDevice(), L"Data\\Textures\\SkyBox_Space.dds", 5000.0f);

	// Create shadow map
	mShadowMap = new ShadowMap(mDirect3D->GetDevice(), 2048, 2048);

	// Create frustum culling
	mFrustumCulling = new FrustumCulling();

	//--------------------------------------------------------
	// Compute scene bounding box
	//--------------------------------------------------------
// 	XMFLOAT3 minPt(+MathHelper::infinity, +MathHelper::infinity, +MathHelper::infinity);
// 	XMFLOAT3 maxPt(-MathHelper::infinity, -MathHelper::infinity, -MathHelper::infinity);
// 
// 	// Get vertex positions from all models
// 	for (UINT i = 0; i < mGenericInstances.size(); ++i)
// 	{
// 		for (UINT j = 0; j < mGenericInstances[i].model->vertices.size(); ++j)
// 		{
// 			XMFLOAT3 vPos = mGenericInstances[i].model->vertices[j]->position;
// 
// 			minPt.x = MathHelper::getMin(minPt.x, vPos.x);
// 			minPt.y = MathHelper::getMin(minPt.x, vPos.x);
// 			minPt.z = MathHelper::getMin(minPt.x, vPos.x);
// 
// 			maxPt.x = MathHelper::getMax(maxPt.x, vPos.x);
// 			maxPt.y = MathHelper::getMax(maxPt.x, vPos.x);
// 			maxPt.z = MathHelper::getMax(maxPt.x, vPos.x);
// 		}
// 	}
// 
// 	// Sphere center is at half of these new dimensions
// 	mSceneBounds.Center = XMFLOAT3(	0.5f*(minPt.x + maxPt.x),
// 		0.5f*(minPt.y + maxPt.y),
// 		0.5f*(minPt.z + maxPt.z));
// 
// 	// Calculate the sphere radius
// 	XMFLOAT3 extent(0.5f*(maxPt.x - minPt.x),
// 		0.5f*(maxPt.y - minPt.y),
// 		0.5f*(maxPt.z - minPt.z));
// 
// 	mSceneBounds.Radius = sqrtf(extent.x*extent.x + extent.y*extent.y + extent.z*extent.z);
	splash.Hide();
	D3D11App::ShowWindow();
	OnResize();

	return true;
}