hedgewars/uCaptions.pas
author sheepluva
Tue, 04 Feb 2014 16:35:31 +0100
changeset 10105 8c5fa1d15bd5
parent 10015 4feced261c68
child 10108 c68cf030eded
permissions -rw-r--r--
fixing the remaining PChar issues pas2c ran into to this point, now it stops because of being unable to deal with "set of char"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     1
(*
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     2
 * Hedgewars, a free turn based strategy game
9998
736015b847e3 update copyright to 2014
sheepluva
parents: 9080
diff changeset
     3
 * Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com>
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     4
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     8
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    12
 * GNU General Public License for more details.
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    13
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    14
 * You should have received a copy of the GNU General Public License
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    15
 * along with this program; if not, write to the Free Software
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    17
 *)
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 9998 9127
diff changeset
    18
5303
e06bf3954a83 this should fix captions on iOS (and android), though I can't check
Henek
parents: 4976
diff changeset
    19
{$INCLUDE "options.inc"}
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    20
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    21
unit uCaptions;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    22
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    23
interface
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    24
uses uTypes;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    25
10105
8c5fa1d15bd5 fixing the remaining PChar issues pas2c ran into to this point, now it stops because of being unable to deal with "set of char"
sheepluva
parents: 10015
diff changeset
    26
procedure AddCaption(caption: PChar; Color: Longword; Group: TCapGroup);
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    27
procedure DrawCaptions;
6394
f0a9042e7387 yay, finally osx (and likely windows) fullscreen switch works like on linux! ALL textures had to be destroyed and recreated only after the new window got created. In other news, the new window must be cleaned with glClear to skip a first frame of garbage and AddProgress is only called the first time.
koda
parents: 6383
diff changeset
    28
procedure ReloadCaptions(unload: boolean);
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    29
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    30
procedure initModule;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    31
procedure freeModule;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    32
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    33
implementation
4850
434cd1284204 code cleanup
koda
parents: 4808
diff changeset
    34
uses uTextures, uRenderUtils, uVariables, uRender;
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    35
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    36
type TCaptionStr = record
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6415
diff changeset
    37
    Tex: PTexture;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6415
diff changeset
    38
    EndTime: LongWord;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6415
diff changeset
    39
    Text: shortstring;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6415
diff changeset
    40
    Color: Longword
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6415
diff changeset
    41
    end;
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    42
var
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    43
    Captions: array[TCapGroup] of TCaptionStr;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    44
10105
8c5fa1d15bd5 fixing the remaining PChar issues pas2c ran into to this point, now it stops because of being unable to deal with "set of char"
sheepluva
parents: 10015
diff changeset
    45
procedure AddCaption(caption: PChar; Color: Longword; Group: TCapGroup);
8c5fa1d15bd5 fixing the remaining PChar issues pas2c ran into to this point, now it stops because of being unable to deal with "set of char"
sheepluva
parents: 10015
diff changeset
    46
var
8c5fa1d15bd5 fixing the remaining PChar issues pas2c ran into to this point, now it stops because of being unable to deal with "set of char"
sheepluva
parents: 10015
diff changeset
    47
    s: shortstring;
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    48
begin
10105
8c5fa1d15bd5 fixing the remaining PChar issues pas2c ran into to this point, now it stops because of being unable to deal with "set of char"
sheepluva
parents: 10015
diff changeset
    49
    s:= caption;
8027
e5ba3dd12531 make stats-only mode work headless. also skip a few things to save time/memory.
nemo
parents: 6999
diff changeset
    50
    if cOnlyStats then exit;
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 6999
diff changeset
    51
    if Length(s) = 0 then
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 6999
diff changeset
    52
        exit;
6379
ef4288298e57 reset chat lines too
nemo
parents: 6377
diff changeset
    53
    if Captions[Group].Text <> s then
6371
c6f73d8e87e2 Skip rerendering the caption if the text is unchanged. Scripts make heavy use of caption, often onGameTick
nemo
parents: 5303
diff changeset
    54
        begin
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    55
        FreeTexture(Captions[Group].Tex);
6371
c6f73d8e87e2 Skip rerendering the caption if the text is unchanged. Scripts make heavy use of caption, often onGameTick
nemo
parents: 5303
diff changeset
    56
        Captions[Group].Tex:= nil
c6f73d8e87e2 Skip rerendering the caption if the text is unchanged. Scripts make heavy use of caption, often onGameTick
nemo
parents: 5303
diff changeset
    57
        end;
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 9998 9127
diff changeset
    58
6374
b024e279587c oops. tex could expire, too...
nemo
parents: 6371
diff changeset
    59
    if Captions[Group].Tex = nil then
6371
c6f73d8e87e2 Skip rerendering the caption if the text is unchanged. Scripts make heavy use of caption, often onGameTick
nemo
parents: 5303
diff changeset
    60
        begin
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
    61
        Captions[Group].Color:= Color;
6371
c6f73d8e87e2 Skip rerendering the caption if the text is unchanged. Scripts make heavy use of caption, often onGameTick
nemo
parents: 5303
diff changeset
    62
        Captions[Group].Text:= s;
c6f73d8e87e2 Skip rerendering the caption if the text is unchanged. Scripts make heavy use of caption, often onGameTick
nemo
parents: 5303
diff changeset
    63
        Captions[Group].Tex:= RenderStringTex(s, Color, fntBig)
c6f73d8e87e2 Skip rerendering the caption if the text is unchanged. Scripts make heavy use of caption, often onGameTick
nemo
parents: 5303
diff changeset
    64
        end;
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    65
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    66
    case Group of
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    67
        capgrpGameState: Captions[Group].EndTime:= RealTicks + 2200
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    68
    else
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    69
        Captions[Group].EndTime:= RealTicks + 1400 + LongWord(Captions[Group].Tex^.w) * 3;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    70
    end;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    71
end;
6382
0e76c5cd4250 move the order of reloading texture to workaround buggy drivers
koda
parents: 6379
diff changeset
    72
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
    73
// For uStore texture recreation
6394
f0a9042e7387 yay, finally osx (and likely windows) fullscreen switch works like on linux! ALL textures had to be destroyed and recreated only after the new window got created. In other news, the new window must be cleaned with glClear to skip a first frame of garbage and AddProgress is only called the first time.
koda
parents: 6383
diff changeset
    74
procedure ReloadCaptions(unload: boolean);
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
    75
var Group: TCapGroup;
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
    76
begin
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
    77
for Group:= Low(TCapGroup) to High(TCapGroup) do
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6415
diff changeset
    78
    if unload then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6415
diff changeset
    79
        FreeTexture(Captions[Group].Tex)
6394
f0a9042e7387 yay, finally osx (and likely windows) fullscreen switch works like on linux! ALL textures had to be destroyed and recreated only after the new window got created. In other news, the new window must be cleaned with glClear to skip a first frame of garbage and AddProgress is only called the first time.
koda
parents: 6383
diff changeset
    80
    else if Captions[Group].Text <> '' then
6383
c34a8b98d78c increase land tex size to 512, which is the current minimum required just to load a hat. On my system max fps rose from 840 to 890 - about 6% change.
nemo
parents: 6382
diff changeset
    81
        Captions[Group].Tex:= RenderStringTex(Captions[Group].Text, Captions[Group].Color, fntBig)
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
    82
end;
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    83
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    84
procedure DrawCaptions;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    85
var
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    86
    grp: TCapGroup;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    87
    offset: LongInt;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    88
begin
6685
ef706fccfb0a moved other widgets under the USE_TOUCH_INTERFACE, added pause button (at least, graphically...)
koda
parents: 6580
diff changeset
    89
{$IFDEF USE_TOUCH_INTERFACE}
5303
e06bf3954a83 this should fix captions on iOS (and android), though I can't check
Henek
parents: 4976
diff changeset
    90
    offset:= 48;
4808
7c3e5b52344a remove obsolete TrainingFlags code
koda
parents: 4393
diff changeset
    91
{$ELSE}
7c3e5b52344a remove obsolete TrainingFlags code
koda
parents: 4393
diff changeset
    92
    offset:= 8;
7c3e5b52344a remove obsolete TrainingFlags code
koda
parents: 4393
diff changeset
    93
{$ENDIF}
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    94
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    95
    for grp:= Low(TCapGroup) to High(TCapGroup) do
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    96
        with Captions[grp] do
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
    97
            if Tex <> nil then
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
    98
                begin
6999
486db9d26e4b rename a few Draw* routines
koda
parents: 6700
diff changeset
    99
                DrawTextureCentered(0, offset, Tex);
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   100
                inc(offset, Tex^.h + 2);
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   101
                if EndTime <= RealTicks then
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
   102
                    begin
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   103
                    FreeTexture(Tex);
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   104
                    Tex:= nil;
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
   105
                    Text:= '';
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   106
                    EndTime:= 0
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
   107
                    end;
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   108
                end;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   109
end;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   110
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   111
procedure initModule;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   112
begin
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   113
    FillChar(Captions, sizeof(Captions), 0)
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   114
end;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   115
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   116
procedure freeModule;
6383
c34a8b98d78c increase land tex size to 512, which is the current minimum required just to load a hat. On my system max fps rose from 840 to 890 - about 6% change.
nemo
parents: 6382
diff changeset
   117
var group: TCapGroup;
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   118
begin
4901
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4850
diff changeset
   119
    for group:= Low(TCapGroup) to High(TCapGroup) do
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
   120
        begin
4901
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4850
diff changeset
   121
        FreeTexture(Captions[group].Tex);
6377
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
   122
        Captions[group].Tex:= nil
3ce19204b14b Since I'm storing the text string anyway, might as well recreate captions on resize as well
nemo
parents: 6374
diff changeset
   123
        end
4393
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   124
end;
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   125
883b979697e4 uCaptions
unc0rr
parents:
diff changeset
   126
end.