#1 

20-09-2009 22:00:55

nabouill
Membres
Avatar de nabouill
Date d'inscription:
Messages: 242
IP: 62.39.248.23
Courriel

Salut a tous

J'aimerais vous faire partager un bout de code donc je ne peu presque plus me passer.
Une petite fonction à intégrer au début de votre programme qui permet de configurer quelque paramètre d'affichage.
Et comme un dessin (enfin un screen pour nous) vaut souvent mieux qu'un discours :




C'est quand meme plus sympa que des messages dans une console.
Un des avantages, c'est qu'il sauvegarde a chaque fois les dernier paramètre d'affichage utilisé.smile

Bon, on va commencer par le fichier main.cpp

#include <irrlicht.h>

#include "menu.h"


using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace gui;


int main()
{
    bool runApps = false;
    int l_windows = 640;
    int h_windows = 480;
    bool fullscreen = false;
    bool shadows = false;
    bool vsync = false;
    E_DRIVER_TYPE driverType  = EDT_DIRECT3D9;

    //on envoie a notre fonction les parametre d'affichage, cette fonction revoie "true" si on clic sur start
    runApps = startMenu(&l_windows, &h_windows, &fullscreen, &shadows, &vsync, &driverType);


    if(runApps)
    {
        //on créer nore device avec les parametre que l'on a definie dans notre fonction
        IrrlichtDevice *device = createDevice(driverType, core::dimension2d<s32>(l_windows,h_windows),32,fullscreen,shadows,vsync);
        IVideoDriver* driver = device->getVideoDriver ();
        while (device->run())
        {
            driver->beginScene(true, true, video::SColor(0,200,200,200));

            driver->endScene ();
        }
    }
}

On créer un fichier "menu.h" :

#ifndef MENU_H
#define MENU_H

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;

bool startMenu(int *l_windows, int *h_windows, bool *fullscreen, bool *shadows, bool *vsync, E_DRIVER_TYPE *driverType);

#endif

Puis le fichier "menu.cpp" :

#include <irrlicht.h>
#include <stdio.h>
#include <string.h>

#include <string>
#include <iostream>


#include "menu.h"


using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace gui;


// Declare a structure to hold some context for the event receiver so that it
// has it available inside its OnEvent() method.
struct SAppContext
{
        IrrlichtDevice *device;
        IGUIListBox    *boxResolution;
        IGUIListBox    *boxRender;
};

// Define some values that we'll use to identify individual GUI controls.
enum
{
        GUI_ID_QUIT_BUTTON = 101,
        GUI_ID_START_BUTTON,
        GUI_ID_DRIVER_SELECT,
        GUI_ID_RESOLUTION_SELECT,
        GUI_ID_FULLSCREEN,
        GUI_ID_SHADOWS,
        GUI_ID_VSYNC,
};


class menuEventReceiver : public IEventReceiver
{
public:
        //constructeur
        menuEventReceiver(SAppContext & context) : Context(context)
        {
            selectRender = 0;
            selectResolution = 1;
            fullscreen = false;
            shadows = false;
            vsync = false;
            runApps = false;
        }

        s32 selectRender;
        s32 selectResolution;
        bool fullscreen;
        bool shadows;
        bool vsync;
        bool runApps;

        virtual bool OnEvent(const SEvent& event)
        {
                if (event.EventType == EET_GUI_EVENT)
                {
                        s32 id = event.GUIEvent.Caller->getID();
                        IGUIEnvironment* env = Context.device->getGUIEnvironment();

                                        switch(id)
                                        {
                                            case GUI_ID_QUIT_BUTTON:
                                                    if(event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
                                                    {
                                                        Context.device->closeDevice();
                                                    }
                                                    return true;

                                            case GUI_ID_START_BUTTON:
                                                    if(event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
                                                    {
                                                        runApps = true;
                                                        Context.device->closeDevice();
                                                    }
                                                    return true;

                                            case GUI_ID_DRIVER_SELECT:
                                                    if (event.GUIEvent.EventType == gui::EGET_LISTBOX_CHANGED ||
                                                        event.GUIEvent.EventType == gui::EGET_LISTBOX_SELECTED_AGAIN)
                                                    {
                                                        selectRender = ((gui::IGUIListBox*)event.GUIEvent.Caller)->getSelected();

                                                    }
                                                    return true;

                                            case GUI_ID_RESOLUTION_SELECT:
                                                    if (event.GUIEvent.EventType == gui::EGET_LISTBOX_CHANGED ||
                                                        event.GUIEvent.EventType == gui::EGET_LISTBOX_SELECTED_AGAIN)
                                                    {
                                                        selectResolution = ((gui::IGUIListBox*)event.GUIEvent.Caller)->getSelected();

                                                    }
                                                    return true;

                                            case GUI_ID_FULLSCREEN:
                                                    if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
                                                        fullscreen = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
                                                    return true;

                                            case GUI_ID_SHADOWS:
                                                    if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
                                                        shadows = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
                                                    return true;

                                            case GUI_ID_VSYNC:
                                                    if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
                                                        vsync = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
                                                    return true;

                                            default:
                                                    return false;
                                        }


                }

                return false;
        }

private:
        SAppContext & Context;
};



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




bool startMenu(int *l_windows, int *h_windows, bool *fullscreen, bool *shadows,  bool *vsync, E_DRIVER_TYPE *driverType)
{
    s32 selectRender;
    s32 selectResolution;
    int t_fullscreen;
    int t_shadows;
    int t_vsync;
    FILE* configFile = fopen("config.ini", "r");
    if(configFile != NULL)
    {
        fscanf(configFile, "%d %d %d %d %d", &selectRender, &selectResolution, &t_fullscreen, &t_shadows, &t_vsync);
        fclose(configFile);
    }
    else
    {printf("\
ERREUR CHARGEMENT DU FICHIER DE CONFIG !!!!!\
\
");
    }

    if(t_fullscreen)
    *fullscreen = true;
    if(t_shadows)
    *shadows = true;
    if(t_vsync)
    *vsync = true;



    IrrlichtDevice *device = createDevice(video::EDT_BURNINGSVIDEO, core::dimension2d<s32>(300, 200), 16, false, false, false);
    IVideoDriver* driver = device->getVideoDriver();
    IGUIEnvironment* guienv = device->getGUIEnvironment();

    device->setWindowCaption(L"Select-parametre");

    // set new Skin
	IGUISkin* newskin = guienv->createSkin(gui::EGST_BURNING_SKIN);
	guienv->setSkin(newskin);
	newskin->drop();

	// load font
	IGUIFont* font = guienv->getFont("fonthaettenschweiler.bmp");
	if (font)
	{
		guienv->getSkin()->setFont(font);
	}

    IGUIListBox* boxRender = guienv->addListBox(core::rect<int>(10,10,100,100), 0, GUI_ID_DRIVER_SELECT);
    boxRender->addItem(L"DirectX 9");
	boxRender->addItem(L"OpenGL");
	boxRender->addItem(L"Irrlicht Software Renderer");
	boxRender->addItem(L"Burning's Video");



	IGUIListBox* boxResolution = guienv->addListBox(core::rect<int>(200,10,290,100), 0, GUI_ID_RESOLUTION_SELECT);
    boxResolution->addItem(L"640 X 480");
	boxResolution->addItem(L"800 X 600");
	boxResolution->addItem(L"1024 X 768");
	boxResolution->addItem(L"1280 X 1024");


    IGUIButton* exitButton = guienv->addButton(core::rect<int>(10,175,60,195), 0, GUI_ID_QUIT_BUTTON, L"Exit");
	IGUIButton* startButton = guienv->addButton(core::rect<int>(240,175,290,195), 0, GUI_ID_START_BUTTON, L"Start");

	guienv->addCheckBox(*fullscreen, core::rect<int>(10,100,90,120), 0, GUI_ID_FULLSCREEN, L"Fullscreen");
	guienv->addCheckBox(*shadows, core::rect<int>(10,120,125,140), 0, GUI_ID_SHADOWS, L"Realtime shadows");
	guienv->addCheckBox(*vsync, core::rect<int>(10,140,160,160), 0, GUI_ID_VSYNC, L"Vertical synchronisation");


    // Store the appropriate data in a context structure.
        SAppContext context;
        menuEventReceiver receiver(context);
        context.device = device;
        context.boxResolution = boxResolution;
        context.boxRender = boxRender;
        receiver.fullscreen = *fullscreen;
        receiver.shadows = *shadows;
        receiver.vsync = *vsync;


    device->setEventReceiver(&receiver);



    // initialisation des items avec les parametre charger depuis le fichier de config
    receiver.selectRender = selectRender;
    receiver.selectResolution = selectResolution;
    boxRender->setSelected(receiver.selectRender);
    boxResolution->setSelected(receiver.selectResolution);



	/// DEMAARAGE DU RENDU


	    while(device->run() && driver)
	    {
            if (device->isWindowActive())
            {

                    driver->beginScene(true, true, SColor(0,200,200,200));

                    guienv->drawAll();

                    driver->endScene();
            }

            selectRender = receiver.selectRender;
            selectResolution = receiver.selectResolution;

            switch(selectRender)
            {
                case 0:	*driverType = EDT_DIRECT3D9; break;
                case 1:	*driverType = EDT_OPENGL; break;
                case 2:	*driverType = EDT_SOFTWARE; break;
                case 3:	*driverType = EDT_BURNINGSVIDEO; break;
            }

            switch(selectResolution)
            {
                case 0:	*l_windows = 640; *h_windows = 480; break;
                case 1:	*l_windows = 800; *h_windows = 600; break;
                case 2:	*l_windows = 1024; *h_windows = 768; break;
                case 3:	*l_windows = 1280; *h_windows = 1024; break;
            }

            *fullscreen = receiver.fullscreen;
            *shadows = receiver.shadows;
            *vsync = receiver.vsync;
	    }



        // sauvegarde de la config dans le fichier config.ini
        configFile = fopen("config.ini", "w+");
        if(configFile != NULL)
        {
            fprintf(configFile, "%d %d %d %d %d", selectRender, selectResolution,
                                                            *fullscreen, *shadows, *vsync);
        }
        fclose(configFile);


        bool run = receiver.runApps;

        device->drop();

        return run;
}

Voilou. Je ne vous fait pas trop de commentaire sur ce code, il est pas très complexe.

Ceux qui le veulent peuvent télécharger le projet avec le code ici : Select_parametre_download

Hors ligne


#2 

21-09-2009 19:29:26

TUpac
Membres
Avatar de TUpac
Date d'inscription:
Messages: 387
IP: 88.168.3.38
Courriel

Merci pour le code nabouill c'est relativement utile big_smile d'autant que c'est facilement isérable dans les menus de nos ptits jeux qu'on aime wink


"Si vous ne partagez pas votre stabilité avec les pauvres, les pauvres partageront leur instabilité avec vous."

Hors ligne


Options Liens officiels Caractéristiques Statistiques Communauté
Préférences cookies
Corrections
irrlicht
irrklang
irredit
irrxml
Propulsé par Django
xhtml 1.0
css 2.1
884 membres
1440 sujets
11337 messages
Dernier membre inscrit: Saidov17
596 invités en ligne
membre en ligne: -
RSS Feed