Пример #1
0
/** @brief	The game loop function
  * @param  p_structCommon : Struct with all program informations
  */
void playGame(structProgramInfo* p_structCommon)
{
	unsigned char l_cKey;
    char* l_sTopText;
    unsigned int l_iWatchdog;
	unsigned int l_iCursorX;
	unsigned int l_iCursorY;
    unsigned int l_iMovement;	/* store the wanted move, if impossible this variable */
    unsigned int l_iCurrentSocketIndex;		/* allow the program to go back */
    int l_iNewCoordinates;
    char l_bForceRedraw;

    l_bForceRedraw = TRUE;
	l_cKey = 0;
    l_iNewCoordinates = -1;
	l_iMovement = 0;
	l_iCursorX = 1;
	l_iCursorY = 1;
    p_structCommon->iLastXUsed = l_iCursorX;
    p_structCommon->iLastYUsed = l_iCursorY;
    l_iCurrentSocketIndex = 0;
	p_structCommon->iOffsetX = (p_structCommon->iCol / 2) - (p_structCommon->iSizeX / 2);
	p_structCommon->iOffsetY = (p_structCommon->iRow / 2) - (p_structCommon->iSizeY / 2);
    l_sTopText = (char*)malloc((p_structCommon->iCol + 1) * sizeof(char));
    if(l_sTopText == NULL) exit(-ENOMEM);

	p_structCommon->iCurrentUserColor = enumRouge; /* Main user, or server always red. If multiplayer and client, it receive another color suring connection  */
    p_structCommon->cUserMove = 0;

	/* Init the game, screen stuff etc... */
	gameInit(p_structCommon);

	do
	{
        snprintf(   l_sTopText,
                    p_structCommon->iCol,
                    "Your turn to play [ ] | Connected [ ] | Current port [%d] | Nickname %s",
                    p_structCommon->iTcpPort,
                    p_structCommon->sUserName);
        if(p_structCommon->bMyTurnToPlay == TRUE)
        {
            l_sTopText[19] = 'X';
        }
        else
        {
            l_sTopText[19] = ' ';
        }

        if(p_structCommon->bMutexInitialized == TRUE)
        {
            l_sTopText[35] = 'X';
        }
        else
        {
            l_sTopText[35] = ' ';
        }

        if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);}
        usleep(TIME_BETWEEN_TWO_REQUEST);
        initBar();
        topText(l_sTopText);

		/* Display wursor each time */
		displayCursor(l_iCursorX, l_iCursorY, p_structCommon->iOffsetX, p_structCommon->iOffsetY, FALSE, p_structCommon->cGrid);
        pointCounting(p_structCommon->cGrid, p_structCommon->iPoints, p_structCommon->iSizeX, p_structCommon->iSizeY);
        displayRanking(p_structCommon->iPoints, p_structCommon->iCol, p_structCommon->iRow);
        topText(l_sTopText);
		refresh();
        usleep(TIME_BETWEEN_TWO_REQUEST);
        if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);}

		l_cKey = getch();

        /* In order to allow an alone player to play alone */
        if(p_structCommon->bMutexInitialized == FALSE)
        {
            p_structCommon->bMyTurnToPlay = TRUE;
        }

		switch(l_cKey)
		{

			case 'D':
			{
				/* LEFT */
				l_iCursorX = (l_iCursorX < 1) ? p_structCommon->iSizeX - 1 : l_iCursorX - 1;
                p_structCommon->cUserMove = 'd';
				l_iMovement = DIRECTION_LEFT;
				break;
			}
			case 'C':
			{
				/* RIGHT */
				l_iCursorX = (l_iCursorX > p_structCommon->iSizeX - 2) ?
                    0 : l_iCursorX + 1;
                p_structCommon->cUserMove = 'c';
				l_iMovement = DIRECTION_RIGHT;
				break;
			}
			case 'A':
			{
				/* UP */
				l_iCursorY = (l_iCursorY < 1) ? p_structCommon->iSizeY - 1 : l_iCursorY - 1;
                p_structCommon->cUserMove = 'a';
				l_iMovement = DIRECTION_UP;
				break;
			}
			case 'B':
			{
				/* DOWN */
				l_iCursorY = (l_iCursorY > p_structCommon->iSizeY - 2) ?
                    0 : l_iCursorY + 1;
                p_structCommon->cUserMove = 'b';
				l_iMovement = DIRECTION_DOWN;
				break;
			}
            case ':':
            {
                /* Command mode */
                l_bForceRedraw = TRUE;
                logBar(p_structCommon, CLEAN_L2, "");
                if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);}
                logBar(p_structCommon, DISPLAY, "");
                if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);}

                /* Get command from the user, the command set by the user will be saved in p_structCommon->sUserCommand */
                userCommandGetter(p_structCommon);
                /* Analyse user command */
                l_iNewCoordinates = userCommandExecute(p_structCommon);
                if(l_iNewCoordinates > - 1)
                {
                    if(l_iNewCoordinates > - 1 && l_iNewCoordinates < (signed)p_structCommon->iSizeX)
                    {
                        /* belongs to the interval [0 ; p_structCommon->iSizeX[ ==> redefine X coordinate */
                        l_iCursorX = l_iNewCoordinates;
                        l_bForceRedraw = FALSE;
                    }
                    else if(l_iNewCoordinates >= (signed)p_structCommon->iSizeX &&
                            l_iNewCoordinates < (signed)p_structCommon->iSizeX + (signed)p_structCommon->iSizeY)
                    {
                        /* belongs to the interval [p_structCommon->iSizeX ; p_structCommon->iSizeX + p_structCommon->iSizeY[ ==> redefine Y coordinate */
                        l_iCursorY = l_iNewCoordinates - (p_structCommon->iSizeX);
                        l_bForceRedraw = FALSE;
                    }
                }

                if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);}
                displayCursor(l_iCursorX, l_iCursorY, p_structCommon->iOffsetX, p_structCommon->iOffsetY, l_bForceRedraw, p_structCommon->cGrid);
                refresh();

                /* Clean the screen */
                logBar(p_structCommon, DISPLAY, "");
                if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);}
                break;
            }
            case 'q':
            case 'Q':
            {
                p_structCommon->bNetworkDisconnectionRequiered = TRUE;

                /* Started threads have to down this flag -- bMutexInitialized means we have at least one thread started */
                while(p_structCommon->bNetworkDisconnectionRequiered == TRUE &&
                      p_structCommon->bMutexInitialized == TRUE)
                {
                    usleep(TIME_BETWEEN_TWO_REQUEST);
                    for(l_iCurrentSocketIndex = 0; l_iCurrentSocketIndex < MAX_CONNECTED_CLIENTS ; l_iCurrentSocketIndex++)
                    {
                        if(p_structCommon->iClientsSockets[l_iCurrentSocketIndex] != 0)
                        {
                            break;
                        }
                    }
                    if(l_iCurrentSocketIndex >= MAX_CONNECTED_CLIENTS - 1)
                    {
                        break;
                    }
                }   
                break;

                /* Normally, network.c have clean the mutex at the end of all connexions */
                if(p_structCommon->bMutexInitialized == TRUE)
                {
                    pthread_mutex_destroy(p_structCommon->pthreadMutex);
                    p_structCommon->bMutexInitialized = FALSE;
                }
            }
			case ' ':
			{
                if(p_structCommon->bMyTurnToPlay != TRUE)
                {
                    break;
                }

				/* When the user drop a rock */
                p_structCommon->cUserMove = 'r';
                p_structCommon->iLastXUsed = l_iCursorX;
                p_structCommon->iLastYUsed = l_iCursorY;

				/* Put the color information in the matrix */
				p_structCommon->cGrid[COLOR_MATRIX][l_iCursorY][l_iCursorX] =
					p_structCommon->iCurrentUserColor;

				/* Put the text information in the matrix */
				p_structCommon->cGrid[TEXT_MATRIX][l_iCursorY][l_iCursorX] =
					' ';

                /* declare this point to be synchronized with all clients */
                p_structCommon->cGrid[SYNC_MATRIX][l_iCursorY][l_iCursorX] = POINT_TO_SYNC;

                /* Draw the block of the current user (the other blocks are draw by
                    another function) */
                if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);}
				drawElement(l_iCursorX + p_structCommon->iOffsetX, l_iCursorY + p_structCommon->iOffsetY,
					p_structCommon->cGrid[TEXT_MATRIX][l_iCursorY][l_iCursorX],
					p_structCommon->iCurrentUserColor);
                if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);}

                /* Check neighborhood - If there is two contigous blocks of the player's
                   color that means there is maybee a loop */
                loopCompletion(l_iCursorX, l_iCursorY, p_structCommon->iCurrentUserColor, p_structCommon);

                /* Reset this player turn */
                p_structCommon->bMyTurnToPlay = FALSE;
                break;
			}

			default:
			{
				/* Else, do nothing */
				break;
			}
		}

		/* Check code, in order to forbid access to already reserved boxes */
		if((unsigned int)p_structCommon->cGrid[COLOR_MATRIX][l_iCursorY][l_iCursorX] != enumNoir)
		{
			/* So there is something here and it is not me */
			switch(l_iMovement)
			{
				case DIRECTION_UP:
				l_iCursorY++;
				break;
				case DIRECTION_DOWN:
				l_iCursorY--;
				break;
				case DIRECTION_LEFT:
				l_iCursorX++;
				break;
				case DIRECTION_RIGHT:
				l_iCursorX--;
				break;
				default:
				/* Who, error ! */
				perror("Unknown code");
				break;
			}

			/* If we go out of the screen by the upper or wester side put it in 0:0 and
			let the next block check if this block is empty or not */
			if(l_iCursorY >= p_structCommon->iSizeY || l_iCursorX >= p_structCommon->iSizeX)
			{
				l_iCursorX = 0;
				l_iCursorY = 0;
			}

			/* If with bad luck you go over the grid */
            l_iWatchdog = 0;
			while((unsigned int)p_structCommon->cGrid[COLOR_MATRIX][l_iCursorY][l_iCursorX] != enumNoir)
			{
				/* Find random coordinate with the simplest way */
				l_iCursorX = rand() % p_structCommon->iSizeX;
				l_iCursorY = rand() % p_structCommon->iSizeY;

                l_iWatchdog++;
                if(l_iWatchdog > (p_structCommon->iSizeX * p_structCommon->iSizeY) / 2)
                {
                    if(isTheGridFull(p_structCommon) == TRUE)
                    {
                        endOfTheGame(p_structCommon);
                        return;
                    }
                }
			}
		}
	}while((l_cKey != 'q') && (l_cKey != 'Q'));		/* until q/Q pressed */

    free(l_sTopText);

}
Пример #2
0
/*--------------------------------------------------------------------------*/
int sci_figure(char * fname, void* pvApiCtx)
{
    SciErr sciErr;
    int* piAddr = NULL;
    int iFig = 0;
    int iRhs = nbInputArgument(pvApiCtx);
    int iId = 0;
    int iPos = 0;
    int i = 0;
    int iAxes = 0;
    int iPropertyOffset = 0;
    BOOL bDoCreation = TRUE;
    BOOL bVisible = TRUE; // Create a visible figure by default
    BOOL bDockable = TRUE; // Create a dockable figure by default
    BOOL bDefaultAxes = TRUE; // Create an Axes by default
    double* figureSize = NULL;
    double* axesSize = NULL;
    double* position = NULL;
    double val[4];
    BOOL bMenuBar = TRUE;
    BOOL bToolBar = TRUE;
    BOOL bInfoBar = TRUE;
    BOOL bResize = TRUE;
    int iMenubarType = 1; // Create a 'figure' menubar by default
    int iToolbarType = 1; // Create a 'figure' toolbar by default
    double dblId = 0;
    BOOL status = FALSE;

    //figure(num) -> scf(num)
    //figure() -> scf()

    //figure(x, "...", ...)

    // figure()
    if (iRhs == 0) // Auto ID
    {
        iId = getValidDefaultFigureId();
        iFig = createNewFigureWithAxes();
        setGraphicObjectProperty(iFig, __GO_ID__, &iId, jni_int,  1);
        iAxes = setDefaultProperties(iFig, TRUE);
        initBar(iFig, bMenuBar, bToolBar, bInfoBar);
        createScalarHandle(pvApiCtx, iRhs + 1, getHandle(iFig));
        AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
        ReturnArguments(pvApiCtx);
        return 0;
    }

    if (iRhs == 1)
    {
        //figure(x);
        sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
            return 0;
        }

        if (isVarMatrixType(pvApiCtx, piAddr) == 0)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: An integer value expected.\n"), fname, 1);
            return 0;
        }

        if (getScalarDouble(pvApiCtx, piAddr, &dblId))
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }

        iId = (int)(dblId + 0.5); //avoid 1.999 -> 1

        //get current fig from id
        iFig = getFigureFromIndex(iId);
        if (iFig == 0) // Figure does not exists, create a new one
        {
            iFig = createNewFigureWithAxes();
            setGraphicObjectProperty(iFig, __GO_ID__, &iId, jni_int,  1);
            iAxes = setDefaultProperties(iFig, TRUE);
        }

        initBar(iFig, bMenuBar, bToolBar, bInfoBar);
        createScalarHandle(pvApiCtx, iRhs + 1, getHandle(iFig));
        AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
        ReturnArguments(pvApiCtx);
        return 0;
    }

    // Prepare property analysis
    if (iRhs % 2 == 0)
    {
        //get highest value of winsid to create the new windows @ + 1
        iId = getValidDefaultFigureId();
        iPos = 0;
    }
    else
    {
        iPos = 1;
        //figure(x, ...);
        sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
            return 0;
        }

        if (isVarMatrixType(pvApiCtx, piAddr) == 0)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: An integer value expected.\n"), fname, 1);
            return 0;
        }

        if (getScalarDouble(pvApiCtx, piAddr, &dblId))
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }

        iId = (int)(dblId + 0.5); //avoid 1.999 -> 1
        //get current fig from id
        iFig = getFigureFromIndex(iId);
        if (iFig != 0) // Figure already exists
        {
            bDoCreation = FALSE;
        }
    }

    if (bDoCreation)
    {
        int* piAddrProp = NULL;
        char* pstProName = NULL;
        int* piAddrData = NULL;
        for (i = iPos + 1 ; i <= iRhs ; i += 2)
        {
            //get property name
            sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddrProp);
            if (sciErr.iErr)
            {
                Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i);
                return 1;
            }

            if (getAllocatedSingleString(pvApiCtx, piAddrProp, &pstProName))
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i);
                return 1;
            }

            if (stricmp(pstProName, "dockable") != 0 &&
                    stricmp(pstProName, "toolbar") != 0 &&
                    stricmp(pstProName, "menubar") != 0 &&
                    stricmp(pstProName, "default_axes") != 0 &&
                    stricmp(pstProName, "visible") != 0 &&
                    stricmp(pstProName, "figure_size") != 0 &&
                    stricmp(pstProName, "axes_size") != 0 &&
                    stricmp(pstProName, "position") != 0 &&
                    stricmp(pstProName, "menubar_visible") != 0 &&
                    stricmp(pstProName, "toolbar_visible") != 0 &&
                    stricmp(pstProName, "resize") != 0 &&
                    stricmp(pstProName, "infobar_visible") != 0)
            {
                freeAllocatedSingleString(pstProName);
                continue;
            }

            //get address of value on stack
            sciErr = getVarAddressFromPosition(pvApiCtx, i + 1, &piAddrData);
            if (sciErr.iErr)
            {
                Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i + 1);
                freeAllocatedSingleString(pstProName);
                return 1;
            }

            //check property value to compatibility
            if (stricmp(pstProName, "dockable") == 0)
            {
                bDockable = getStackArgumentAsBoolean(pvApiCtx, piAddrData);
                if (bDockable == -1)
                {
                    Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "dockable", "on", "off");
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            else if (stricmp(pstProName, "toolbar") == 0)
            {
                char* pstVal = NULL;
                if (isStringType(pvApiCtx, piAddrData) == FALSE || isScalar(pvApiCtx, piAddrData) == FALSE)
                {
                    Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i);
                    freeAllocatedSingleString(pstProName);
                }

                if (getAllocatedSingleString(pvApiCtx, piAddrData, &pstVal))
                {
                    Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i + 1);
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }

                if (stricmp(pstVal, "none") == 0)
                {
                    iToolbarType = 0;
                }
                else if (stricmp(pstVal, "figure") == 0)
                {
                    iToolbarType = 1;
                }
                else
                {
                    Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "toolbar", "none", "figure");
                    freeAllocatedSingleString(pstProName);
                    freeAllocatedSingleString(pstVal);
                    return 1;
                }

                freeAllocatedSingleString(pstVal);
            }
            else if (stricmp(pstProName, "menubar") == 0)
            {
                char* pstVal = NULL;
                if (isStringType(pvApiCtx, piAddrData) == FALSE || isScalar(pvApiCtx, piAddrData) == FALSE)
                {
                    Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i + 1);
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }

                if (getAllocatedSingleString(pvApiCtx, piAddrData, &pstVal))
                {
                    Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i + 1);
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }

                if (stricmp(pstVal, "none") == 0)
                {
                    iMenubarType = 0;
                }
                else if (stricmp(pstVal, "figure") == 0)
                {
                    iMenubarType = 1;
                }
                else
                {
                    Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "menubar", "none", "figure");
                    freeAllocatedSingleString(pstProName);
                    freeAllocatedSingleString(pstVal);
                    return 1;
                }

                freeAllocatedSingleString(pstVal);
            }
            else if (stricmp(pstProName, "default_axes") == 0)
            {
                bDefaultAxes = getStackArgumentAsBoolean(pvApiCtx, piAddrData);
                if (bDefaultAxes == -1)
                {
                    Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "default_axes", "on", "off");
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            else if (stricmp(pstProName, "visible") == 0)
            {
                bVisible = getStackArgumentAsBoolean(pvApiCtx, piAddrData);
                if (bVisible == -1)
                {
                    Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "visible", "on", "off");
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            else if (stricmp(pstProName, "figure_size") == 0)
            {
                int iRows = 0;
                int iCols = 0;
                if (isDoubleType(pvApiCtx, piAddrData) == FALSE)
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A double vector expected.\n"), fname, i + 1);
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }

                getMatrixOfDouble(pvApiCtx, piAddrData, &iRows, &iCols, &figureSize);
                if (iRows * iCols != 2)
                {
                    Scierror(999, _("Wrong size for '%s' property: %d elements expected.\n"), "figure_size", 2);
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            else if (stricmp(pstProName, "axes_size") == 0)
            {
                int iRows = 0;
                int iCols = 0;
                if (isDoubleType(pvApiCtx, piAddrData) == FALSE)
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A double vector expected.\n"), fname, i + 1);
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }

                getMatrixOfDouble(pvApiCtx, piAddrData, &iRows, &iCols, &axesSize);
                if (iRows * iCols != 2)
                {
                    Scierror(999, _("Wrong size for '%s' property: %d elements expected.\n"), "axes_size", 2);
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            else if (stricmp(pstProName, "position") == 0)
            {
                int iRows = 0;
                int iCols = 0;
                double* pdbl = NULL;
                if (isDoubleType(pvApiCtx, piAddrData))
                {
                    getMatrixOfDouble(pvApiCtx, piAddrData, &iRows, &iCols, &pdbl);
                    if (iRows * iCols != 4)
                    {
                        Scierror(999, _("Wrong size for '%s' property: %d elements expected.\n"), "position", 4);
                        freeAllocatedSingleString(pstProName);
                        return 1;
                    }

                    position = pdbl;
                    axesSize = (pdbl + 2);
                }
                else if (isStringType(pvApiCtx, piAddrData) && isScalar(pvApiCtx, piAddrData))
                {
                    char* pstVal = NULL;
                    int iVal = 0;

                    if (getAllocatedSingleString(pvApiCtx, piAddrData, &pstVal))
                    {
                        Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i + 1);
                        freeAllocatedSingleString(pstProName);
                        return 1;
                    }

                    iVal = sscanf(pstVal, "%lf|%lf|%lf|%lf", &val[0], &val[1], &val[2], &val[3]);
                    freeAllocatedSingleString(pstVal);
                    if (iVal != 4)
                    {
                        Scierror(999, _("Wrong value for '%s' property: string or 1 x %d real row vector expected.\n"), "position", 4);
                        freeAllocatedSingleString(pstProName);
                        return 1;
                    }

                    position = val;
                    axesSize = (val + 2);
                }
                else
                {
                    Scierror(999, _("Wrong value for '%s' property: string or 1 x %d real row vector expected.\n"), "position", 4);
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            else if (stricmp(pstProName, "resize") == 0)
            {
                bResize = getStackArgumentAsBoolean(pvApiCtx, piAddrData);
                if (bResize == -1)
                {
                    Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "resize", "on", "off");
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            else if (stricmp(pstProName, "menubar_visible") == 0)
            {
                bMenuBar = getStackArgumentAsBoolean(pvApiCtx, piAddrData);
                if (bMenuBar == -1)
                {
                    Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "menubar_visible", "on", "off");
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            else if (stricmp(pstProName, "toolbar_visible") == 0)
            {
                bToolBar = getStackArgumentAsBoolean(pvApiCtx, piAddrData);
                if (bToolBar == -1)
                {
                    Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "toolbar_visible", "on", "off");
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            else if (stricmp(pstProName, "infobar_visible") == 0)
            {
                bInfoBar = getStackArgumentAsBoolean(pvApiCtx, piAddrData);
                if (bInfoBar == -1)
                {
                    Scierror(999, _("Wrong value for '%s' property: '%s' or '%s' expected."), "infobar_visible", "on", "off");
                    freeAllocatedSingleString(pstProName);
                    return 1;
                }
            }
            freeAllocatedSingleString(pstProName);
        }

        iFig = createFigure(bDockable, iMenubarType, iToolbarType, bDefaultAxes, bVisible);
        setGraphicObjectProperty(iFig, __GO_ID__, &iId, jni_int, 1);
        iAxes = setDefaultProperties(iFig, bDefaultAxes);
    }

    //set(iFig, iPos, iPos + 1)
    for (i = iPos + 1 ; i <= iRhs ; i += 2)
    {
        int isMatrixOfString = 0;
        int* piAddrProp = NULL;
        char* pstProName = NULL;
        int* piAddrData = NULL;
        int iRows = 0;
        int iCols = 0;
        void* _pvData = NULL;
        int iType = 0;

        //get property name
        sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddrProp);
        if (sciErr.iErr)
        {
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i);
            return 1;
        }

        if (getAllocatedSingleString(pvApiCtx, piAddrProp, &pstProName))
        {
            Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, i);
            return 1;
        }

        if (bDoCreation && (
                    stricmp(pstProName, "dockable") == 0 ||
                    stricmp(pstProName, "toolbar") == 0 ||
                    stricmp(pstProName, "menubar") == 0 ||
                    stricmp(pstProName, "default_axes") == 0 ||
                    stricmp(pstProName, "visible") == 0 ||
                    stricmp(pstProName, "figure_size") == 0 ||
                    stricmp(pstProName, "axes_size") == 0 ||
                    stricmp(pstProName, "position") == 0 ||
                    stricmp(pstProName, "resize") == 0 ||
                    stricmp(pstProName, "menubar_visible") == 0 ||
                    stricmp(pstProName, "toolbar_visible") == 0 ||
                    stricmp(pstProName, "infobar_visible") == 0))
        {
            // Already set creating new figure
            // but let the set_ function fail if figure already exists
            freeAllocatedSingleString(pstProName);
            continue;
        }

        //get address of value on stack
        sciErr = getVarAddressFromPosition(pvApiCtx, i + 1, &piAddrData);
        if (sciErr.iErr)
        {
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, i + 1);
            freeAllocatedSingleString(pstProName);
            return 1;
        }

        getVarType(pvApiCtx, piAddrData, &iType);

        if ((strcmp(pstProName, "user_data") == 0) || (stricmp(pstProName, "userdata") == 0))
        {
            /* in this case set_user_data_property
             * directly uses the  third position in the stack
             * to get the variable which is to be set in
             * the user_data property (any data type is allowed) S. Steer */
            _pvData = (void*)piAddrData;         /*position in the stack */
            iRows = -1;   /*unused */
            iCols = -1;   /*unused */
            iType = -1;
        }
        else
        {
            switch (iType)
            {
                case sci_matrix :
                    getMatrixOfDouble(pvApiCtx, piAddrData, &iRows, &iCols, (double**)&_pvData);
                    break;
                case sci_boolean :
                    getMatrixOfBoolean(pvApiCtx, piAddrData, &iRows, &iCols, (int**)&_pvData);
                    break;
                case sci_handles :
                    getMatrixOfHandle(pvApiCtx, piAddrData, &iRows, &iCols, (long long**)&_pvData);
                    break;
                case sci_strings :
                    if (   strcmp(pstProName, "tics_labels") != 0 && strcmp(pstProName, "auto_ticks") != 0 &&
                            strcmp(pstProName, "axes_visible") != 0 && strcmp(pstProName, "axes_reverse") != 0 &&
                            strcmp(pstProName, "text") != 0 && stricmp(pstProName, "string") != 0 &&
                            stricmp(pstProName, "tooltipstring") != 0) /* Added for uicontrols */
                    {
                        if (getAllocatedSingleString(pvApiCtx, piAddrData, (char**)&_pvData))
                        {
                            Scierror(999, _("%s: Wrong size for input argument #%d: A single string expected.\n"), fname, 3);
                            freeAllocatedSingleString(pstProName);
                            return 1;
                        }
                        iRows = (int)strlen((char*)_pvData);
                        iCols = 1;
                    }
                    else
                    {
                        isMatrixOfString = 1;
                        if (getAllocatedMatrixOfString(pvApiCtx, piAddrData, &iRows, &iCols, (char***)&_pvData))
                        {
                            Scierror(999, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 3);
                            freeAllocatedSingleString(pstProName);
                            return 1;
                        }
                    }
                    break;
                case sci_list :
                    iCols = 1;
                    getListItemNumber(pvApiCtx, piAddrData, &iRows);
                    _pvData = (void*)piAddrData;         /* In this case l3 is the list position in stack */
                    break;
                default :
                    _pvData = (void*)piAddrData;         /* In this case l3 is the list position in stack */
                    break;
            }
        }

        callSetProperty(pvApiCtx, iFig, _pvData, iType, iRows, iCols, pstProName);

        // If backgroundcolor is set :
        // * add it to colormap => performed by callSetProperty
        // * set background to index => performed by callSetProperty
        // * copy value into axes background property
        if (stricmp(pstProName, "backgroundcolor") == 0 && iAxes > 0)
        {
            int iBackground = 0;
            int *piBackground = &iBackground;

            getGraphicObjectProperty(iFig, __GO_BACKGROUND__, jni_int, (void **)&piBackground);
            setGraphicObjectProperty(iAxes, __GO_BACKGROUND__, piBackground, jni_int, 1);
        }

        freeAllocatedSingleString(pstProName);
        if (iType == sci_strings)
        {
            //free allacted data
            if (isMatrixOfString == 1)
            {
                freeAllocatedMatrixOfString(iRows, iCols, (char**)_pvData);
            }
            else
            {
                freeAllocatedSingleString((char*)_pvData);
            }
        }
    }

    if (position)
    {
        int pos[2];
        pos[0] = (int)position[0];
        pos[1] = (int)position[1];
        setGraphicObjectProperty(iFig, __GO_POSITION__, pos, jni_int_vector, 2);
    }

    //axes_size
    if (axesSize)
    {
        int axes[2];
        axes[0] = (int)axesSize[0];
        axes[1] = (int)axesSize[1];
        setGraphicObjectProperty(iFig, __GO_AXES_SIZE__, axes, jni_int_vector, 2);
    }
    else //no size, use default axes_size
    {
        int* piAxesSize = NULL;
        getGraphicObjectProperty(getFigureModel(), __GO_AXES_SIZE__, jni_int_vector, (void **)&piAxesSize);
        setGraphicObjectProperty(iFig, __GO_AXES_SIZE__, piAxesSize, jni_int_vector, 2);
        releaseGraphicObjectProperty(__GO_AXES_SIZE__, piAxesSize, jni_int_vector, 2);
    }

    initBar(iFig, bMenuBar, bToolBar, bInfoBar);

    if (axesSize == NULL && figureSize) //figure_size
    {
        int figure[2];
        figure[0] = (int)figureSize[0];
        figure[1] = (int)figureSize[1];
        setGraphicObjectProperty(iFig, __GO_SIZE__, figure, jni_int_vector, 2);
    }


    setGraphicObjectProperty(iFig, __GO_RESIZE__, (void*)&bResize, jni_bool, 1);

    //return new created fig
    createScalarHandle(pvApiCtx, iRhs + 1, getHandle(iFig));
    AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
    ReturnArguments(pvApiCtx);
    return 0;
}