hedgewars/uFLRunQueue.pas
author unc0rr
Thu, 10 Dec 2015 00:33:45 +0300
branchqmlfrontend
changeset 11455 0c75fa9ce340
parent 11439 23912c93935a
child 11456 6e9b12864856
permissions -rw-r--r--
- Use queues instead of single buffer to communicate between threads - Fix build
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11439
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
     1
unit uFLRunQueue;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
     2
interface
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
     3
uses uFLTypes;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
     4
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
     5
procedure queueExecution(var config: TGameConfig);
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
     6
procedure passFlibEvent(p: pointer); cdecl;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
     7
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
     8
implementation
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
     9
uses uFLGameConfig, hwengine, uFLData, uFLUICallback;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    10
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    11
var runQueue: PGameConfig = nil;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    12
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    13
procedure nextRun;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    14
begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    15
    if runQueue <> nil then
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    16
    begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    17
        if runQueue^.gameType = gtPreview then
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    18
            sendUI(mtRenderingPreview, nil, 0);
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    19
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    20
        RunEngine(runQueue^.argumentsNumber, @runQueue^.argv);
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    21
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    22
        sendConfig(runQueue)
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    23
    end
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    24
end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    25
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    26
procedure cleanupConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    27
var t: PGameConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    28
begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    29
    t:= runQueue;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    30
    runQueue:= t^.nextConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    31
    dispose(t)
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    32
end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    33
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    34
procedure queueExecution(var config: TGameConfig);
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    35
var pConfig, t, tt: PGameConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    36
    i: Longword;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    37
begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    38
    new(pConfig);
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    39
    pConfig^:= config;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    40
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    41
    with pConfig^ do
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    42
    begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    43
        nextConfig:= nil;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    44
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    45
        for i:= 0 to Pred(MAXARGS) do
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    46
        begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    47
            if arguments[i][0] = #255 then
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    48
                arguments[i][255]:= #0
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    49
            else
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    50
                arguments[i][byte(arguments[i][0]) + 1]:= #0;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    51
            argv[i]:= @arguments[i][1]
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    52
        end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    53
    end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    54
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    55
    if runQueue = nil then
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    56
    begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    57
        runQueue:= pConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    58
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    59
        nextRun
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    60
    end else
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    61
    begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    62
        t:= runQueue;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    63
        while t^.nextConfig <> nil do 
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    64
        begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    65
            if (pConfig^.gameType = gtPreview) and (t^.nextConfig^.gameType = gtPreview) then
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    66
            begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    67
                tt:= t^.nextConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    68
                pConfig^.nextConfig:= tt^.nextConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    69
                t^.nextConfig:= pConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    70
                dispose(tt);
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    71
                exit // boo
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    72
            end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    73
            t:= t^.nextConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    74
        end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    75
        t^.nextConfig:= pConfig
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    76
    end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    77
end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    78
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    79
procedure passFlibEvent(p: pointer); cdecl;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    80
begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    81
    case TFLIBEvent(p^) of
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    82
        flibGameFinished: begin
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    83
                cleanupConfig;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    84
                nextRun
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    85
                end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    86
    end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    87
end;
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    88
23912c93935a - Implement engine runs queue
unc0rr
parents:
diff changeset
    89
end.