Exemplo n.º 1
0
Arquivo: main.cpp Projeto: jnz/Lynx
int main(int argc, char** argv)
{
#ifdef _DEBUG
    // Visual Studio CRT memory leak detection
    // very useful, makes me sleep better :)
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
    fprintf(stderr, "%s version %i.%i\n", LYNX_TITLE, LYNX_MAJOR, LYNX_MINOR);
    srand((unsigned int)time(NULL));

    float dt;
    uint32_t time, oldtime;
    uint32_t fpstimer, fpscounter=0;

    // Load config file
    if(!CLynx::cfg.AddFile( "game.cfg" ))
        fprintf(stderr, "Failed to open config file.\n"); // bad, but not critical

    // Game Modules
    // Startup SDL OpenGL window
    if(!initSDLvideo(SCREEN_WIDTH, SCREEN_HEIGHT, BPP, FULLSCREEN))
    {
        assert(0);
        return -1;
    }

    // init menu
    menu_engine_callback_t callback;
    memset(&callback, 0, sizeof(callback));
    callback.menu_func_host = menu_func_host;
    callback.menu_func_join = menu_func_join;
    callback.menu_func_quit = menu_func_quit;
    if(!g_menu.Init(SCREEN_WIDTH, SCREEN_HEIGHT, callback))
    {
        fprintf(stderr, "Failed to load menu\n");
        return -1;
    }
    // Draw the menu once, before we continue
    g_menu.DisplayLoadingScreen();
    g_menu.DrawDefaultBackground();
    g_menu.Update(0.0f, 0);
    SDL_GL_SwapBuffers();
    // Ok, now we have something on the screen, now we can load the rest

    SDL_WM_SetCaption(WINDOW_TITLE, NULL);
    SDL_ShowCursor(SDL_DISABLE);
    SDL_WM_GrabInput(SDL_GRAB_ON);
    // we want key repeat. CLynxSys::GetKeyState remembers
    // if a key has been pressed by the user or by auto repeat.
    if(SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL) != 0)
        fprintf(stderr, "Failed to set key repeat\n");

    // Init sound mixer
    initSDLMixer(); // we don't care, if it won't init. No sound for you.

    // Now we are finished with loading, activate the main menu
    g_menu.DisplayMain();

    g_run = 1;
    oldtime = fpstimer = CLynxSys::GetTicks();
    while(g_run)
    {
        time = CLynxSys::GetTicks();
        dt = 0.001f * (float)(time-oldtime);
        if(dt > 0.150f)
        {
            fprintf(stderr, "Warning: Frame time too large: %f\n", dt);
            dt = 0.150f;
        }
        oldtime = time;
        fpscounter++;
        if(time - fpstimer > 1000.0f)
        {
            char title[128];
            sprintf(title, "Lynx (FPS: %i)", (int)fpscounter);
            SDL_WM_SetCaption(title, NULL);
            fpscounter = 0;
            fpstimer = time;
        }

        // Update Server
        if(g_worldsv)
        {
            g_worldsv->Update(dt, time);
            g_svgame->Update(dt, time);
            g_server->Update(dt, time);
        }

        // Update Client
        if(g_worldcl)
        {
            g_mixer->Update(dt, time);
            if(g_client->IsInGame())
                g_worldcl->Update(dt, time);
            g_client->Update(dt, time);
            g_renderer->Update(dt, time);

            if(!g_client->IsRunning())
            {
                fprintf(stderr, "Disconnected\n");
                g_menu.DisplayError("Disconnected from server");
                g_menu.MakeVisible();
                shutdown(&g_worldsv,
                         &g_server,
                         &g_svgame,
                         &g_worldcl,
                         &g_renderer,
                         &g_mixer,
                         &g_clgame,
                         &g_client);
            }
        }
        else
        {
            // We have no gameplay background, let's clear the
            // menu screen. Otherwise the game itself is our
            // background - neat!
            g_menu.DrawDefaultBackground();
            // but since we are not in a game, we don't need > 100 fps
            SDL_Delay(30);
        }

        // Update Menu
        g_menu.Update(dt, time);

        // Draw GL buffer
        SDL_GL_SwapBuffers();

        // Limit to 100 fps
        // so my notebook fan is quiet :-)
        const float dtrest = 1.0f/100.0f - dt;
        if(dtrest > 0.0f)
            SDL_Delay((uint32_t)(dtrest * 1000.0f));

        // Handle system events
        handleSDLevents();
    }

    shutdownSDLMixer();
    shutdown(&g_worldsv,
             &g_server,
             &g_svgame,
             &g_worldcl,
             &g_renderer,
             &g_mixer,
             &g_clgame,
             &g_client);

    return 0;
}
Exemplo n.º 2
0
int mainFenetre()
{
	unsigned int frame_max = SDL_GetTicks() + FRAME_RATE, temps = 0;
	Input * pInput = NULL; //structure contenant les informations relatives aux inputs clavier
	SDL_Texture * pTextureDisplay = NULL;	//Texture globale
	SDL_Rect camera = initRect(0, 0, 0, 0); // rect(x,y,w,h)
	Worms** wormsTab = NULL;
	char mapName[100];
	Jeu* jeu = NULL;

	//init SDL + fenetre + renderer
	if (initSWR())
	{
		//Initialisation des inputs
		pInput = initInput();
		if (pInput == NULL)
		{
			fprintf(logFile, "mainFenetre : FAILURE, initInput.\n");
			cleanUp(&pInput, &pTextureDisplay);
			return -1;
		}

		//InitSounds 
		if (!initSDLMixer()){
			fprintf(logFile, "initSDLMixer : FAILURE, init.\n");
			cleanUp(&pInput, &pTextureDisplay);
			return -1;
			
		}

		strcpy(mapName, cMAP);
		/*Initialisation SDL_TTF*/
		if (TTF_Init() == -1)
		{
			fprintf(logFile, "mainFenetre : FAILURE, initialisation de TTF_Init : %s.\n\n", TTF_GetError());
			cleanUp(&pInput, &pTextureDisplay);
			return -1;
		}

		if (mainMenu(pInput, mapName) < 0)
		{
			fprintf(logFile, "mainFenetre : FAILURE, mainMenu .\n\n");
			cleanUp(&pInput, &pTextureDisplay);
			return -1;
		}
		if (!pInput->quit)
		{
			if (mainInit() < 0)	//set le nombre d'équipe et le nombre de worms par équipe
			{
				fprintf(logFile, "mainInit : FAILURE.\n");
				cleanUp(&pInput, &pTextureDisplay);
				return -1;
			}

			/*Init game*/
			jeu = nouveauJeu(mapName);
			if (jeu == NULL)
			{
				fprintf(logFile, "nouveauJeu : FAILURE.\n");
				cleanUp(&pInput, &pTextureDisplay);
				return -1;
			}

			/*Init map*/
			if (initialisionTerrain(&jeu->pMapTerrain, "../assets/pictures/FondMap1.png", jeu->nomMap) < 0)
			{
				fprintf(logFile, "mainFenetre : FAILURE, initialisationTerrain.\n");
				cleanUp(&pInput, &pTextureDisplay);
				return -1;
			}

			/*Init global texture*/
			pTextureDisplay = my_createTextureFromSurface(jeu->pMapTerrain->globalMapSurface);
			if (pTextureDisplay == NULL)
			{
				fprintf(logFile, "mainFenetre : FAILURE, createGlobalTexture.\n");
				destroyMap(&jeu->pMapTerrain);
				cleanUp(&pInput, &pTextureDisplay);
				return -1;
			}

			/*Init sounds*/
			if (loadSounds(BipExplo, 0) < 0)
			{
				fprintf(logFile, "mainFenetre : FAILURE, loadSounds.\n");
				cleanUp(&pInput, &pTextureDisplay);
				return -1;
			}

			/*Init camera*/
			initCameras(jeu->pMapTerrain, &camera, NULL);

			/*Initialisation du tableau global de worms*/
			wormsTab = initWormsTab(jeu->equipes);
			if (wormsTab == NULL)
			{
				destroyMap(&jeu->pMapTerrain);
				cleanUp(&pInput, &pTextureDisplay);
				fprintf(logFile, "mainFenetre : FAILURE, allocating memory to the global array of worms pointer.\n\n");
				return -1;
			}

			/*Init Display*/
			initDisplay(jeu->pMapTerrain, pTextureDisplay);

			/*Initialisation des worms*/
			while (!KaamInitGame(wormsTab, jeu->pMapTerrain->collisionMapSurface))
				renderScreen(2, 0, jeu->pMapTerrain, 1, pTextureDisplay, &camera, NULL);
		}
		while (!(pInput->quit))
		{
			//Récupération des inputs
			getInput(pInput);

			//Gestion des inputs
			if (!gestInput(pInput, jeu->pMapTerrain, pTextureDisplay, &camera, wormsTab))
			{
				fprintf(logFile, "mainFenetre : FAILURE, gestInput.\n");
			}

			//Update de l'écran
			if (pInput->raffraichissement)
			{
				renderScreen(2, 0, jeu->pMapTerrain, 1, pTextureDisplay, &camera, NULL);
				pInput->raffraichissement = 0;
			}

			updateTeamLife(jeu->equipes);
			isGameEnd(jeu->equipes);

			//Gestion du frame Rate
			frameRate(frame_max);
			frame_max = SDL_GetTicks() + FRAME_RATE;
			if ((SDL_GetTicks() - temps) >= 1000)
			{
				temps = SDL_GetTicks();
				jeu->temps -= 1;
			}
		}


		endDisplay();
		cleanSounds();
		Mix_CloseAudio();
		fprintf(logFile, "||| END OF THE GAME |||\n");
		if (jeu != NULL)
			destroyMap(&jeu->pMapTerrain);
		destroyPolice();
		if (wormsTab != NULL)
			free(wormsTab);
		wormsTab = NULL;
	}
	cleanUp(&pInput, &pTextureDisplay);
	fprintf(logFile, "mainFenetre : SUCCESS.\n");
	if (jeu != NULL)
	{
		saveGame(jeu);
		destroyJeu(&jeu);
	}
	{
		time_t t1 = time(NULL);
		fprintf(logFile, "\n\nEnd of Session : %s", ctime(&t1));
		fclose(logFile);
	};
	return 0;
}