MODULE demo;

FROM tigr IMPORT TigrPtr,tigrWindow,tigrFree,tigrClosed, tigrClear, tigrUpdate, TPixelType,
                tigrLine, TK_ESCAPE, tigrKeyDown, tigrReadChar, tfont, tigrPrint,tigrFill,
                tigrCircle, tigrRect, tigrFillCircle,  tigrTime, tigrError, tigrClip, 
                tigrFillRect, tigrTextWidth, tigrTextHeight, tigrLoadImage, tigrReadFile,
                tigrBitmap, TIGR_2X, tigrSetPostFX, tigrMouse,tigrBlit, tigrBlitAlpha,
                 tigrEncodeUTF8, TK_RIGHT,tigrKeyHeld, TK_SPACE, TK_LEFT;

FROM helper IMPORT tigrRGB, tigrRGBA;
FROM DynamicStrings IMPORT String, InitString;
FROM InOut IMPORT Write, WriteLn, WriteCard,WriteString;
FROM Delay IMPORT Delay;


    


VAR
    standing            : BOOLEAN;
    remaining           : SHORTREAL;
    backdrop, screen    : TigrPtr;    
    squinkle            : TigrPtr; 
    greeting            : String;
    prevx, prevy, prev  : CARDINAL;
    chars               : ARRAY[0..16] OF CHAR;
    tempString          : String;
    n                   : CARDINAL;
    dt                  : SHORTREAL;
    x, y ,b             : CARDINAL;
    message, message1   : String;
    c                   : CARDINAL;
    playerx, playery,
    playerxs, playerys  : INTEGER;

    PROCEDURE update (dt : SHORTREAL);

    VAR
        oldx, oldy : INTEGER;

    BEGIN
        IF (remaining > 0.0) THEN 
            remaining := remaining - dt;
        END;
         (* Read the keyboard and move the player. *)
        WriteString("Position départ ->"); 
        WriteCard(playerxs, 6); WriteString("**");WriteCard(playerys, 6); WriteLn;
        IF (standing OR (tigrKeyDown(screen, TK_SPACE) >0)) THEN
            playerys := playerys - 200;
        END;
         IF ((tigrKeyHeld(screen, TK_LEFT) > 0) OR (tigrKeyHeld(screen, ORD('a')) > 0)) THEN
            WriteString("Touche gauche"); WriteLn;
            playerxs := playerxs - 10;
        END;
        IF ((tigrKeyHeld(screen, TK_RIGHT) > 0) OR (tigrKeyHeld(screen, ORD('d')) > 0)) THEN
        WriteString("Toiuche droite"); WriteLn;
            playerxs := playerxs + 10;
        END;
        WriteString("Nouvelle Position ->");
        WriteCard(playerxs, 6); WriteString("**");WriteCard(playerys, 6); WriteLn;
        Delay(1000);

        
    END update;

BEGIN
    standing    := TRUE;
    remaining   := 0.0;
    playerx     := 160;
    playery     := 200;
    playerxs    := 0;
    playerys    := 0;

    squinkle := tigrLoadImage("squinkle.png");
    IF ( squinkle = NIL) THEN
        tigrError(0, "Cannot load squinkle.png");
    END;
    
    (* Load some UTF-8 text. *)
    greeting := tigrReadFile("greeting.txt", 0);
    IF (greeting = NIL) THEN
        tigrError(0, "Cannot load greeting.txt");
    END;
    (* Make a window and an off-screen backdrop. *)
    screen := tigrWindow(320, 240, greeting, TIGR_2X);
    backdrop := tigrBitmap(screen^.w, screen^.h);

    (*  Fill in the background. *)
    tigrClear(backdrop, tigrRGB(80, 180, 255));
    tigrFill(backdrop, 0, 200, 320, 40, tigrRGB(60, 120, 60));
    tigrFill(backdrop, 0, 200, 320, 3, tigrRGB(0, 0, 0));
    tigrLine(backdrop, 0, 201, 320, 201, tigrRGB(255, 255, 255));

    (* Enable post fx *)
    tigrSetPostFX(screen, 1.0, 1.0, 1.0, 2.0);

    prevx := 0;
    prevy := 0;
    prev  := 0;

    (* Maintain a list of characters entered. *)
    FOR n := 0 TO 16 BY 1 DO
        chars[n] := "_";
    END;

    WHILE (NOT (tigrClosed(screen) > 0)) OR (tigrKeyDown(screen, TK_ESCAPE) > 0) DO 
        (* Update the game. *)
        dt := tigrTime();
        update(dt);

        (* Read the mouse and draw lines when pressed. *)
        
        tigrMouse(screen, x, y, b );
        IF (b = 1) THEN
            IF (prev > 0) THEN
                tigrLine(backdrop, prevx, prevy, x, y, tigrRGB(0, 0, 0));
            END;
            prevx := x;
            prevy := y;
            prev  := 1;
        ELSE 
            prev := 0;
        END;

        (* Composite the backdrop and sprite onto the screen. *)
        tigrBlit(screen, backdrop, 0, 0, 0, 0, backdrop^.w, backdrop^.h);
        tigrBlitAlpha(screen, squinkle, playerx - INTEGER(squinkle^.w / 2), playery - INTEGER(squinkle^.h), 0, 0, 
                    squinkle^.w, squinkle^.h, 1.0);

        message := InitString("A D + SPACE");
        tigrPrint(screen, tfont, 10, 10,  tigrRGBA(0C0H, 0D0H, 0FFH, 0C0H), greeting);
        tigrPrint(screen, tfont, 10, 222, tigrRGBA(0FFH, 0FFH, 0FFH, 0FFH), message);

        (* Grab any chars and add them to our buffer. *)
        LOOP
            c := tigrReadChar(screen);
            IF (c = 0) THEN
                EXIT;
            END;
            FOR n := 1 TO  16 DO
                chars[n - 1] := chars[n];
        END;
            chars[15] := CHR(c);
        END;
        Write(CHR(c));
        tigrUpdate(screen);
    END;

        (* (* Print out the character buffer too. *)
        FOR n := 0 TO 15 DO 
            tempString := tigrEncodeUTF8(p, chars[n]);
        END;
        *p = 0;
        
        tigrPrint(screen, tfont, 160, 222, tigrRGB(255, 255, 255), "Chars: %s", tmp); *)

    tigrFree(squinkle);
    tigrFree(backdrop);
    tigrFree(screen);
END demo.