hedgewars/uRenderUtils.pas
author alfadur
Tue, 02 Apr 2019 15:53:09 +0300
changeset 14764 731c8406bff0
parent 14757 8563cc40fc1e
child 14766 7cc768094d66
permissions -rw-r--r--
optimize atlas pruning
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
     1
(*
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
     2
 * Hedgewars, a free turn based strategy game
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10860
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
     4
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
     8
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    12
 * GNU General Public License for more details.
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    13
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    14
 * You should have received a copy of the GNU General Public License
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    15
 * along with this program; if not, write to the Free Software
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 10107
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    17
 *)
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    18
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    19
{$INCLUDE "options.inc"}
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    20
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    21
unit uRenderUtils;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    22
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    23
interface
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    24
uses SDLh, uTypes;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    25
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    26
procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean);
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    27
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    28
procedure copyRotatedSurface(src, dest: PSDL_Surface); // this is necessary since width/height are read only in SDL
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    29
procedure copyToXY(src, dest: PSDL_Surface; destX, destY: LongInt); inline;
7013
54db061b5710 Get rid of overloaded functions in uRenderUtils
unc0rr
parents: 6992
diff changeset
    30
procedure copyToXYFromRect(src, dest: PSDL_Surface; srcX, srcY, srcW, srcH, destX, destY: LongInt);
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    31
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    32
procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt); inline;
7013
54db061b5710 Get rid of overloaded functions in uRenderUtils
unc0rr
parents: 6992
diff changeset
    33
procedure DrawSpriteFrame2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt; frame: LongInt);
6620
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
    34
procedure DrawLine2Surf(dest: PSDL_Surface; x0,y0,x1,y1:LongInt; r,g,b: byte);
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    35
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean);
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    36
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    37
function  RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
7013
54db061b5710 Get rid of overloaded functions in uRenderUtils
unc0rr
parents: 6992
diff changeset
    38
function  RenderStringTexLim(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord): PTexture;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    39
function  RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    40
14757
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    41
function IsTooDarkToRead(TextColor: Longword): boolean; inline;
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    42
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    43
implementation
11821
d8844d4c67de always include uUtils _after_ SysUtils
sheepluva
parents: 11532
diff changeset
    44
uses uVariables, uConsts, uTextures, SysUtils, uUtils, uDebug;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    45
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    46
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    47
var r: TSDL_Rect;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    48
begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    49
    r:= rect^;
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
    50
    if Clear then
14655
b055360684bd Remove black pixels at corners of DrawRoundRect rectangles
Wuzzy <Wuzzy2@mail.ru>
parents: 13490
diff changeset
    51
        SDL_FillRect(Surface, @r, SDL_MapRGBA(Surface^.format, 0, 0, 0, 0));
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    52
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    53
    BorderColor:= SDL_MapRGB(Surface^.format, BorderColor shr 16, BorderColor shr 8, BorderColor and $FF);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    54
    FillColor:= SDL_MapRGB(Surface^.format, FillColor shr 16, FillColor shr 8, FillColor and $FF);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    55
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    56
    r.y:= rect^.y + cFontBorder div 2;
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    57
    r.h:= rect^.h - cFontBorder;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    58
    SDL_FillRect(Surface, @r, BorderColor);
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    59
    r.x:= rect^.x + cFontBorder div 2;
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    60
    r.w:= rect^.w - cFontBorder;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    61
    r.y:= rect^.y;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    62
    r.h:= rect^.h;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    63
    SDL_FillRect(Surface, @r, BorderColor);
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    64
    r.x:= rect^.x + cFontBorder;
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    65
    r.y:= rect^.y + cFontBorder div 2;
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    66
    r.w:= rect^.w - cFontBorder * 2;
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    67
    r.h:= rect^.h - cFontBorder;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    68
    SDL_FillRect(Surface, @r, FillColor);
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    69
    r.x:= rect^.x + cFontBorder div 2;
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    70
    r.y:= rect^.y + cFontBorder;
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    71
    r.w:= rect^.w - cFontBorder;
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
    72
    r.h:= rect^.h - cFontBorder * 2;
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
    73
    SDL_FillRect(Surface, @r, FillColor);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    74
end;
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    75
(*
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    76
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    77
begin
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    78
    WriteInRoundRect:= WriteInRoundRect(Surface, X, Y, Color, Font, s, 0);
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    79
end;*)
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    80
14757
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    81
function IsTooDarkToRead(TextColor: LongWord): boolean;
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    82
var clr: TSDL_Color;
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    83
begin
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    84
    clr.r:= (TextColor shr 16) and $FF;
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    85
    clr.g:= (TextColor shr 8) and $FF;
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    86
    clr.b:= TextColor and $FF;
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    87
    IsTooDarkToRead:= ((clr.r >= cInvertTextColorAt) or (clr.g >= cInvertTextColorAt) or (clr.b >= cInvertTextColorAt));
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    88
end;
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    89
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    90
function IsTooDarkToRead(TextColor: TSDL_COLOR): boolean;
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    91
begin
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    92
    IsTooDarkToRead:= (not ((TextColor.r >= cInvertTextColorAt) or (TextColor.g >= cInvertTextColorAt) or (TextColor.b >= cInvertTextColorAt)));
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    93
end;
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    94
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
    95
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring; maxLength: LongWord): TSDL_Rect;
10494
0eb97cf4c78e Fix warnings given by 32-bit fpc
unC0Rr
parents: 10142
diff changeset
    96
var w, h: Longword;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    97
    tmpsurf: PSDL_Surface;
14757
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
    98
    finalRect, textRect: TSDL_Rect;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    99
    clr: TSDL_Color;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   100
begin
10127
7f29a65aa1e4 It compiles \o/
unc0rr
parents: 10116
diff changeset
   101
    TTF_SizeUTF8(Fontz[Font].Handle, PChar(s), @w, @h);
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
   102
    if (maxLength > 0) and (w > maxLength * HDPIScaleFactor) then w := maxLength * HDPIScaleFactor;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   103
    finalRect.x:= X;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   104
    finalRect.y:= Y;
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
   105
    finalRect.w:= w + cFontBorder * 2 + cFontPadding * 2;
6982
8d41d22a291d breaking news, we don't support typed consts anymore
koda
parents: 6750
diff changeset
   106
    finalRect.h:= h + cFontBorder * 2;
6750
5eb087708fbd attractiveness tweak to the text clip
nemo
parents: 6749
diff changeset
   107
    textRect.x:= X;
5eb087708fbd attractiveness tweak to the text clip
nemo
parents: 6749
diff changeset
   108
    textRect.y:= Y;
5eb087708fbd attractiveness tweak to the text clip
nemo
parents: 6749
diff changeset
   109
    textRect.w:= w;
5eb087708fbd attractiveness tweak to the text clip
nemo
parents: 6749
diff changeset
   110
    textRect.h:= h;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   111
    clr.r:= (Color shr 16) and $FF;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   112
    clr.g:= (Color shr 8) and $FF;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   113
    clr.b:= Color and $FF;
14757
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
   114
    if (not IsTooDarkToRead(clr)) then
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
   115
        DrawRoundRect(@finalRect, cWhiteColor, cNearBlackColor, Surface, true)
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
   116
    else
8563cc40fc1e Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents: 14655
diff changeset
   117
        DrawRoundRect(@finalRect, cNearBlackColor, cWhiteColor, Surface, true);
10127
7f29a65aa1e4 It compiles \o/
unc0rr
parents: 10116
diff changeset
   118
    tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, PChar(s), clr);
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
   119
    finalRect.x:= X + cFontBorder + cFontPadding;
6982
8d41d22a291d breaking news, we don't support typed consts anymore
koda
parents: 6750
diff changeset
   120
    finalRect.y:= Y + cFontBorder;
11507
bd9a2f1b0080 SDLTry doesn't halt engine no more
unc0rr
parents: 11362
diff changeset
   121
    if SDLCheck(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true) then
bd9a2f1b0080 SDLTry doesn't halt engine no more
unc0rr
parents: 11362
diff changeset
   122
        exit;
6750
5eb087708fbd attractiveness tweak to the text clip
nemo
parents: 6749
diff changeset
   123
    SDL_UpperBlit(tmpsurf, @textRect, Surface, @finalRect);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   124
    SDL_FreeSurface(tmpsurf);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   125
    finalRect.x:= X;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   126
    finalRect.y:= Y;
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
   127
    finalRect.w:= w + cFontBorder * 2 + cFontPadding * 2;
6982
8d41d22a291d breaking news, we don't support typed consts anymore
koda
parents: 6750
diff changeset
   128
    finalRect.h:= h + cFontBorder * 2;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   129
    WriteInRoundRect:= finalRect;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   130
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   131
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   132
procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   133
var y, x, i, j: LongInt;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   134
    tmpPixel: Longword;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   135
    pixels: PLongWordArray;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   136
begin
11532
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   137
    if checkFails(Surface^.format^.BytesPerPixel = 4, 'flipSurface failed, expecting 32 bit surface', true) then
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   138
        exit;
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   139
    SDL_LockSurface(Surface);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   140
    pixels:= Surface^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   141
    if Vertical then
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   142
    for y := 0 to (Surface^.h div 2) - 1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   143
        for x := 0 to Surface^.w - 1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   144
            begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   145
            i:= y * Surface^.w + x;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   146
            j:= (Surface^.h - y - 1) * Surface^.w + x;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   147
            tmpPixel:= pixels^[i];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   148
            pixels^[i]:= pixels^[j];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   149
            pixels^[j]:= tmpPixel;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   150
            end
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   151
    else
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   152
    for x := 0 to (Surface^.w div 2) - 1 do
11532
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   153
        for y := 0 to Surface^.h - 1 do
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   154
            begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   155
            i:= y*Surface^.w + x;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   156
            j:= y*Surface^.w + (Surface^.w - x - 1);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   157
            tmpPixel:= pixels^[i];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   158
            pixels^[i]:= pixels^[j];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   159
            pixels^[j]:= tmpPixel;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   160
            end;
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   161
    SDL_UnlockSurface(Surface);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   162
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   163
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
   164
procedure copyToXY(src, dest: PSDL_Surface; destX, destY: LongInt); inline;
6620
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   165
begin
12098
966a9739812f copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents: 11836
diff changeset
   166
    // copy from complete src
7013
54db061b5710 Get rid of overloaded functions in uRenderUtils
unc0rr
parents: 6992
diff changeset
   167
    copyToXYFromRect(src, dest, 0, 0, src^.w, src^.h, destX, destY);
6620
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   168
end;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   169
7013
54db061b5710 Get rid of overloaded functions in uRenderUtils
unc0rr
parents: 6992
diff changeset
   170
procedure copyToXYFromRect(src, dest: PSDL_Surface; srcX, srcY, srcW, srcH, destX, destY: LongInt);
12099
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   171
var spi, dpi, iX, iY, dX, dY, lX, lY, aT: LongInt;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   172
    srcPixels, destPixels: PLongWordArray;
12099
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   173
    rD, gD, bD, aD, rT, gT, bT: Byte;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   174
begin
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   175
    SDL_LockSurface(src);
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   176
    SDL_LockSurface(dest);
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   177
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   178
    srcPixels:= src^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   179
    destPixels:= dest^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   180
12098
966a9739812f copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents: 11836
diff changeset
   181
    // what's the offset between src and dest coords?
966a9739812f copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents: 11836
diff changeset
   182
    dX:= destX - srcX;
966a9739812f copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents: 11836
diff changeset
   183
    dY:= destY - srcY;
966a9739812f copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents: 11836
diff changeset
   184
966a9739812f copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents: 11836
diff changeset
   185
    // let's figure out where the rectangle we can actually copy ends
12101
2e70ef81e281 copyToXYFromRect: simplify my math (so that it actually, you know, works...)
sheepluva
parents: 12099
diff changeset
   186
    lX:= min(srcX + srcW, src^.w) - 1;
2e70ef81e281 copyToXYFromRect: simplify my math (so that it actually, you know, works...)
sheepluva
parents: 12099
diff changeset
   187
    if lX + dx >= dest^.w then lX:= dest^.w - dx - 1;
2e70ef81e281 copyToXYFromRect: simplify my math (so that it actually, you know, works...)
sheepluva
parents: 12099
diff changeset
   188
    lY:= min(srcY + srcH, src^.h) - 1;
2e70ef81e281 copyToXYFromRect: simplify my math (so that it actually, you know, works...)
sheepluva
parents: 12099
diff changeset
   189
    if lY + dy >= dest^.h then lY:= dest^.h - dy - 1;
12098
966a9739812f copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents: 11836
diff changeset
   190
966a9739812f copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents: 11836
diff changeset
   191
    for iX:= srcX to lX do
966a9739812f copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents: 11836
diff changeset
   192
    for iY:= srcY to lY do
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   193
        begin
12099
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   194
        // src pixel index
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   195
        spi:= iY * src^.w  + iX;
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   196
        // dest pixel index
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   197
        dpi:= (iY + dY) * dest^.w + (iX + dX);
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   198
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   199
        // get src alpha (and set it as target alpha for now)
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   200
        aT:= (srcPixels^[spi] and AMask) shr AShift;
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   201
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   202
        // src pixel opaque?
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   203
        if aT = 255 then
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   204
            begin
12099
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   205
            // just copy full pixel
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   206
            destPixels^[dpi]:= srcPixels^[spi];
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   207
            continue;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   208
            end;
12099
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   209
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   210
        // get dst alpha (without shift for now)
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   211
        aD:= (destPixels^[dpi] and AMask) shr AShift;
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   212
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   213
        // dest completely transparent?
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   214
        if aD = 0 then
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   215
            begin
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   216
            // just copy src pixel
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   217
            destPixels^[dpi]:= srcPixels^[spi];
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   218
            continue;
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   219
            end;
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   220
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   221
        // looks like some blending is necessary
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   222
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   223
        // set color of target RGB to src for now
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   224
        SDL_GetRGB(srcPixels^[spi],  src^.format,  @rT, @gT, @bT);
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   225
        SDL_GetRGB(destPixels^[dpi], dest^.format, @rD, @gD, @bD);
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   226
        // note: this is not how to correctly blend RGB, just sayin' (R,G,B are not linear...)
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   227
        rT:= (rD * (255 - aT) + rT * aT) div 255;
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   228
        gT:= (gD * (255 - aT) + gT * aT) div 255;
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   229
        bT:= (bD * (255 - aT) + bT * aT) div 255;
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   230
        aT:= aD + ((255 - LongInt(aD)) * aT div 255);
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   231
12102
51596d30a724 fix chat SDL surfaces being in wrong color format (didn't play well with copyToXY's new quick pixel copies)
sheepluva
parents: 12101
diff changeset
   232
        destPixels^[dpi]:= SDL_MapRGBA(dest^.format, rT, gT, bT, Byte(aT));
51596d30a724 fix chat SDL surfaces being in wrong color format (didn't play well with copyToXY's new quick pixel copies)
sheepluva
parents: 12101
diff changeset
   233
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   234
        end;
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   235
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   236
    SDL_UnlockSurface(src);
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   237
    SDL_UnlockSurface(dest);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   238
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   239
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
   240
procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt); inline;
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
   241
begin
12099
c1d83fc8e894 copyToXYFromRect: some tweaks
sheepluva
parents: 12098
diff changeset
   242
   DrawSpriteFrame2Surf(sprite, dest, x, y, 0);
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
   243
end;
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
   244
7013
54db061b5710 Get rid of overloaded functions in uRenderUtils
unc0rr
parents: 6992
diff changeset
   245
procedure DrawSpriteFrame2Surf(sprite: TSprite; dest: PSDL_Surface; x,y,frame: LongInt);
6620
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   246
var numFramesFirstCol, row, col: LongInt;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   247
begin
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   248
    numFramesFirstCol:= SpritesData[sprite].imageHeight div SpritesData[sprite].Height;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   249
    row:= Frame mod numFramesFirstCol;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   250
    col:= Frame div numFramesFirstCol;
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 9998 9127
diff changeset
   251
4feced261c68 partial merge of the webgl branch
koda
parents: 9998 9127
diff changeset
   252
    copyToXYFromRect(SpritesData[sprite].Surface, dest,
4feced261c68 partial merge of the webgl branch
koda
parents: 9998 9127
diff changeset
   253
             col*SpritesData[sprite].Width,
4feced261c68 partial merge of the webgl branch
koda
parents: 9998 9127
diff changeset
   254
             row*SpritesData[sprite].Height,
4feced261c68 partial merge of the webgl branch
koda
parents: 9998 9127
diff changeset
   255
             SpritesData[sprite].Width,
4feced261c68 partial merge of the webgl branch
koda
parents: 9998 9127
diff changeset
   256
             spritesData[sprite].Height,
6620
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   257
             x,y);
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   258
end;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   259
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   260
procedure DrawLine2Surf(dest: PSDL_Surface; x0, y0,x1,y1: LongInt; r,g,b: byte);
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   261
var
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   262
    dx,dy,err,e2,sx,sy: LongInt;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   263
    yMax: LongInt;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   264
    destPixels: PLongwordArray;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   265
begin
6992
b8f3d8991e92 marked a couple of functions as inline
koda
parents: 6986
diff changeset
   266
    //max:= (dest^.pitch div 4) * dest^.h;
6620
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   267
    yMax:= dest^.pitch div 4;
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   268
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   269
    SDL_LockSurface(dest);
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   270
6620
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   271
    destPixels:= dest^.pixels;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   272
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   273
    dx:= abs(x1-x0);
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   274
    dy:= abs(y1-y0);
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   275
    if x0 < x1 then sx:= 1 else sx:= -1;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   276
    if y0 < y1 then sy:= 1 else sy:= -1;
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 9998 9127
diff changeset
   277
    err:= dx-dy;
6620
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   278
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   279
    while(true) do
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   280
        begin
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   281
        destPixels^[(y0 * yMax) + x0]:= SDL_MapRGB(dest^.format, r,g,b); //But will it blend? no
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   282
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   283
        if (x0 = x1) and (y0 = y1) then break;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   284
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   285
        e2:= 2*err;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   286
        if e2 > -dy then
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   287
            begin
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   288
            err:= err - dy;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   289
            x0 := x0 + sx;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   290
            end;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   291
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   292
        if e2 < dx then
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   293
            begin
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   294
            err:= err + dx;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   295
            y0:=y0+sy
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   296
            end;
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   297
        end;
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   298
    SDL_UnlockSurface(dest);
6620
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   299
end;
b211d0b690de Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents: 6580
diff changeset
   300
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   301
procedure copyRotatedSurface(src, dest: PSDL_Surface); // this is necessary since width/height are read only in SDL, apparently
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   302
var y, x, i, j: LongInt;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   303
    srcPixels, destPixels: PLongWordArray;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   304
begin
11532
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   305
    checkFails(src^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true);
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   306
    checkFails(dest^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true);
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   307
    if not allOK then exit;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   308
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   309
    SDL_LockSurface(src);
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   310
    SDL_LockSurface(dest);
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   311
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   312
    srcPixels:= src^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   313
    destPixels:= dest^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   314
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   315
    j:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   316
    for x := 0 to src^.w - 1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   317
        for y := 0 to src^.h - 1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   318
            begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   319
            i:= (src^.h - 1 - y) * (src^.pitch div 4) + x;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   320
            destPixels^[j]:= srcPixels^[i];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   321
            inc(j)
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   322
            end;
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   323
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   324
    SDL_UnlockSurface(src);
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   325
    SDL_UnlockSurface(dest);
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   326
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   327
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   328
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
   329
function RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
   330
begin
7013
54db061b5710 Get rid of overloaded functions in uRenderUtils
unc0rr
parents: 6992
diff changeset
   331
    RenderStringTex:= RenderStringTexLim(s, Color, font, 0);
6986
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
   332
end;
409dd3851309 add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents: 6982
diff changeset
   333
7013
54db061b5710 Get rid of overloaded functions in uRenderUtils
unc0rr
parents: 6992
diff changeset
   334
function RenderStringTexLim(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord): PTexture;
10494
0eb97cf4c78e Fix warnings given by 32-bit fpc
unC0Rr
parents: 10142
diff changeset
   335
var w, h: Longword;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   336
    finalSurface: PSDL_Surface;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   337
begin
10139
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   338
    if cOnlyStats then
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   339
        begin
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   340
        RenderStringTexLim:= nil;
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   341
        end
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   342
    else
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   343
        begin
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   344
        if length(s) = 0 then s:= _S' ';
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   345
        font:= CheckCJKFont(s, font);
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   346
        w:= 0; h:= 0; // avoid compiler hints
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   347
        TTF_SizeUTF8(Fontz[font].Handle, PChar(s), @w, @h);
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
   348
        if (maxLength > 0) and (w > maxLength * HDPIScaleFactor) then w := maxLength * HDPIScaleFactor;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   349
11836
1d41f297b3d3 bit of a start on HDPI monitor support. Fonts.
nemo
parents: 11821
diff changeset
   350
        finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + cFontBorder*2 + cFontPadding*2, h + cFontBorder * 2,
10139
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   351
                32, RMask, GMask, BMask, AMask);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   352
11532
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   353
        if checkFails(finalSurface <> nil, 'RenderString: fail to create surface', true) then
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   354
            exit(nil);
10139
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   355
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   356
        WriteInRoundRect(finalSurface, 0, 0, Color, font, s, maxLength);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   357
12591
7bae1fab444b currently irrelevant sdl call adjustment
sheepluva
parents: 12102
diff changeset
   358
        checkFails(SDL_SetColorKey(finalSurface, SDL_TRUE, 0) = 0, errmsgTransparentSet, false);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   359
10139
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   360
        RenderStringTexLim:= Surface2Tex(finalSurface, false);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   361
10139
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   362
        SDL_FreeSurface(finalSurface);
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   363
        end;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   364
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   365
10689
692649e341fc change string types of speech bubbles fix to work with pas2c
sheepluva
parents: 10687
diff changeset
   366
function GetNextSpeechLine(s: ansistring; ldelim: char; var startFrom: LongInt; out substr: ansistring): boolean;
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   367
var p, l, m, r: Integer;
10691
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   368
    newl, skip: boolean;
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   369
    c         : char;
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   370
begin
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   371
    m:= Length(s);
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   372
10690
c3b73dc33294 grmlgrml
sheepluva
parents: 10689
diff changeset
   373
    substr:= '';
c3b73dc33294 grmlgrml
sheepluva
parents: 10689
diff changeset
   374
10689
692649e341fc change string types of speech bubbles fix to work with pas2c
sheepluva
parents: 10687
diff changeset
   375
    SetLengthA(substr, m);
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   376
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   377
    // number of chars read
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   378
    r:= 0;
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   379
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   380
    // number of chars to be written
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   381
    l:= 0;
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   382
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   383
    newl:= true;
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   384
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   385
    for p:= max(1, startFrom) to m do
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   386
        begin
10691
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   387
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   388
        inc(r);
10691
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   389
        // read char from source string
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   390
        c:= s[p];
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   391
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   392
        // strip empty lines, spaces and newlines on beginnings of line
10691
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   393
        skip:= ((newl or (p = m)) and ((c = ' ') or (c = ldelim)));
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   394
10691
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   395
        if (not skip) then
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   396
            begin
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   397
            newl:= (c = ldelim);
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   398
            // stop if we went past the end of the line
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   399
            if newl then
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   400
                break;
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   401
10691
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   402
            // copy current char to output substring
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   403
            inc(l);
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   404
            substr[l]:= c;
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   405
            end;
97f45f1374be change speechfix implementation (no "continue" anymore
sheepluva
parents: 10690
diff changeset
   406
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   407
        end;
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   408
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   409
    inc(startFrom, r);
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   410
10689
692649e341fc change string types of speech bubbles fix to work with pas2c
sheepluva
parents: 10687
diff changeset
   411
    SetLengthA(substr, l);
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   412
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   413
    GetNextSpeechLine:= (l > 0);
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   414
end;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   415
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   416
function RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   417
var textWidth, textHeight, x, y, w, h, i, j, pos, line, numLines, edgeWidth, edgeHeight, cornerWidth, cornerHeight: LongInt;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   418
    finalSurface, tmpsurf, rotatedEdge: PSDL_Surface;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   419
    rect: TSDL_Rect;
10127
7f29a65aa1e4 It compiles \o/
unc0rr
parents: 10116
diff changeset
   420
    {$IFNDEF PAS2C}
4611
445d382cd401 A very very basic snow implementation
nemo
parents: 4404
diff changeset
   421
    chars: set of char = [#9,' ',';',':','?','!',','];
10127
7f29a65aa1e4 It compiles \o/
unc0rr
parents: 10116
diff changeset
   422
    {$ENDIF}
10689
692649e341fc change string types of speech bubbles fix to work with pas2c
sheepluva
parents: 10687
diff changeset
   423
    substr: ansistring;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   424
    edge, corner, tail: TSPrite;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   425
begin
10139
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   426
    if cOnlyStats then exit(nil);
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   427
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   428
    case SpeechType of
10142
adb804cb2638 Reduce number of warnings in pas2c-generated code
unc0rr
parents: 10139
diff changeset
   429
        1: begin
10139
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   430
            edge:= sprSpeechEdge;
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   431
            corner:= sprSpeechCorner;
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   432
            tail:= sprSpeechTail;
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   433
            end;
10142
adb804cb2638 Reduce number of warnings in pas2c-generated code
unc0rr
parents: 10139
diff changeset
   434
        2: begin
10139
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   435
            edge:= sprThoughtEdge;
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   436
            corner:= sprThoughtCorner;
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   437
            tail:= sprThoughtTail;
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   438
            end;
10142
adb804cb2638 Reduce number of warnings in pas2c-generated code
unc0rr
parents: 10139
diff changeset
   439
        3: begin
10139
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   440
            edge:= sprShoutEdge;
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   441
            corner:= sprShoutCorner;
9046f69dae4c In stats-only mode:
unc0rr
parents: 10127
diff changeset
   442
            tail:= sprShoutTail;
10142
adb804cb2638 Reduce number of warnings in pas2c-generated code
unc0rr
parents: 10139
diff changeset
   443
            end
adb804cb2638 Reduce number of warnings in pas2c-generated code
unc0rr
parents: 10139
diff changeset
   444
        else
adb804cb2638 Reduce number of warnings in pas2c-generated code
unc0rr
parents: 10139
diff changeset
   445
            exit(nil)
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   446
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   447
    edgeHeight:= SpritesData[edge].Height;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   448
    edgeWidth:= SpritesData[edge].Width;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   449
    cornerWidth:= SpritesData[corner].Width;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   450
    cornerHeight:= SpritesData[corner].Height;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   451
    // This one screws up WrapText
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   452
    //s:= 'This is the song that never ends.  ''cause it goes on and on my friends. Some people, started singing it not knowing what it was. And they''ll just go on singing it forever just because... This is the song that never ends...';
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   453
    // This one does not
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   454
    //s:= 'This is the song that never ends.  cause it goes on and on my friends. Some people, started singing it not knowing what it was. And they will go on singing it forever just because... This is the song that never ends... ';
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   455
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   456
    numLines:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   457
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   458
    if length(s) = 0 then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   459
        s:= '...';
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   460
    font:= CheckCJKFont(s, font);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   461
    w:= 0; h:= 0; // avoid compiler hints
10127
7f29a65aa1e4 It compiles \o/
unc0rr
parents: 10116
diff changeset
   462
    TTF_SizeUTF8(Fontz[font].Handle, PChar(s), @w, @h);
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   463
    if w<8 then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   464
        w:= 8;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   465
    j:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   466
    if (length(s) > 20) then
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   467
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   468
        w:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   469
        i:= round(Sqrt(length(s)) * 2);
10127
7f29a65aa1e4 It compiles \o/
unc0rr
parents: 10116
diff changeset
   470
        {$IFNDEF PAS2C}
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   471
        s:= WrapText(s, #1, chars, i);
10127
7f29a65aa1e4 It compiles \o/
unc0rr
parents: 10116
diff changeset
   472
        {$ENDIF}
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   473
        pos:= 1; line:= 0;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   474
    // Find the longest line for the purposes of centring the text.  Font dependant.
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   475
        while GetNextSpeechLine(s, #1, pos, substr) do
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   476
            begin
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   477
            inc(numLines);
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   478
            i:= 0; j:= 0;
10689
692649e341fc change string types of speech bubbles fix to work with pas2c
sheepluva
parents: 10687
diff changeset
   479
            TTF_SizeUTF8(Fontz[font].Handle, PChar(substr), @i, @j);
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   480
            if i > w then
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   481
                w:= i;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   482
            end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   483
        end
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   484
    else numLines := 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   485
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   486
    if numLines < 1 then
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   487
        begin
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   488
        s:= '...';
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   489
        numLines:= 1;
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   490
        end;
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   491
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   492
    textWidth:=((w-(cornerWidth-edgeWidth)*2) div edgeWidth)*edgeWidth+edgeWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   493
    textHeight:=(((numlines * h + 2)-((cornerHeight-edgeWidth)*2)) div edgeWidth)*edgeWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   494
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   495
    textHeight:=max(textHeight,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   496
    //textWidth:=max(textWidth,SpritesData[tail].Width);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   497
    rect.x:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   498
    rect.y:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   499
    rect.w:= textWidth + (cornerWidth * 2);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   500
    rect.h:= textHeight + cornerHeight*2 - edgeHeight + SpritesData[tail].Height;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   501
    //s:= inttostr(w) + ' ' + inttostr(numlines) + ' ' + inttostr(rect.x) + ' '+inttostr(rect.y) + ' ' + inttostr(rect.w) + ' ' + inttostr(rect.h);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   502
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   503
    finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, rect.h, 32, RMask, GMask, BMask, AMask);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   504
11532
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   505
    if checkFails(finalSurface <> nil, 'RenderString: fail to create surface', true) then
bf86c6cb9341 Bye-bye TryDo
unc0rr
parents: 11507
diff changeset
   506
        exit(nil);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   507
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   508
    //////////////////////////////// CORNERS ///////////////////////////////
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   509
    copyToXY(SpritesData[corner].Surface, finalSurface, 0, 0); /////////////////// NW
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   510
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   511
    flipSurface(SpritesData[corner].Surface, true); // store all 4 versions in memory to avoid repeated flips?
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   512
    x:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   513
    y:= textHeight + cornerHeight -1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   514
    copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SW
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   515
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   516
    flipSurface(SpritesData[corner].Surface, false);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   517
    x:= rect.w-cornerWidth-1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   518
    y:= textHeight + cornerHeight -1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   519
    copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SE
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   520
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   521
    flipSurface(SpritesData[corner].Surface, true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   522
    x:= rect.w-cornerWidth-1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   523
    y:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   524
    copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// NE
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   525
    flipSurface(SpritesData[corner].Surface, false); // restore original position
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   526
    //////////////////////////////// END CORNERS ///////////////////////////////
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   527
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   528
    //////////////////////////////// EDGES //////////////////////////////////////
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   529
    x:= cornerWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   530
    y:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   531
    while x < rect.w-cornerWidth-1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   532
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   533
        copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// top edge
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   534
        inc(x,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   535
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   536
    flipSurface(SpritesData[edge].Surface, true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   537
    x:= cornerWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   538
    y:= textHeight + cornerHeight*2 - edgeHeight-1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   539
    while x < rect.w-cornerWidth-1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   540
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   541
        copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// bottom edge
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   542
        inc(x,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   543
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   544
    flipSurface(SpritesData[edge].Surface, true); // restore original position
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   545
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   546
    rotatedEdge:= SDL_CreateRGBSurface(SDL_SWSURFACE, edgeHeight, edgeWidth, 32, RMask, GMask, BMask, AMask);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   547
    x:= rect.w - edgeHeight - 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   548
    y:= cornerHeight;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   549
    //// initially was going to rotate in place, but the SDL spec claims width/height are read only
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   550
    copyRotatedSurface(SpritesData[edge].Surface,rotatedEdge);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   551
    while y < textHeight + cornerHeight do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   552
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   553
        copyToXY(rotatedEdge, finalSurface, x, y);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   554
        inc(y,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   555
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   556
    flipSurface(rotatedEdge, false); // restore original position
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   557
    x:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   558
    y:= cornerHeight;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   559
    while y < textHeight + cornerHeight do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   560
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   561
        copyToXY(rotatedEdge, finalSurface, x, y);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   562
        inc(y,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   563
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   564
    //////////////////////////////// END EDGES //////////////////////////////////////
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   565
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   566
    x:= cornerWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   567
    y:= textHeight + cornerHeight * 2 - edgeHeight - 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   568
    copyToXY(SpritesData[tail].Surface, finalSurface, x, y);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   569
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   570
    rect.x:= edgeHeight;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   571
    rect.y:= edgeHeight;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   572
    rect.w:= rect.w - edgeHeight * 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   573
    rect.h:= textHeight + cornerHeight * 2 - edgeHeight * 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   574
    i:= rect.w;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   575
    j:= rect.h;
13490
8935dcc0e130 Always use SDL_Map(A)RGB in SDL_FillRect for color
Wuzzy <Wuzzy2@mail.ru>
parents: 12591
diff changeset
   576
    SDL_FillRect(finalSurface, @rect, SDL_MapRGB(finalSurface^.format, cWhiteColor shr 16, cWhiteColor shr 8, cWhiteColor and $FF));
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   577
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   578
    pos:= 1; line:= 0;
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   579
    while GetNextSpeechLine(s, #1, pos, substr) do
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   580
        begin
10689
692649e341fc change string types of speech bubbles fix to work with pas2c
sheepluva
parents: 10687
diff changeset
   581
        tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, PChar(substr), cNearBlackColorChannels);
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   582
        rect.x:= edgeHeight + 1 + ((i - w) div 2);
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   583
        // trying to more evenly position the text, vertically
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   584
        rect.y:= edgeHeight + ((j-(numLines*h)) div 2) + line * h;
11507
bd9a2f1b0080 SDLTry doesn't halt engine no more
unc0rr
parents: 11362
diff changeset
   585
        if not SDLCheck(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true) then
bd9a2f1b0080 SDLTry doesn't halt engine no more
unc0rr
parents: 11362
diff changeset
   586
        begin
bd9a2f1b0080 SDLTry doesn't halt engine no more
unc0rr
parents: 11362
diff changeset
   587
            SDL_UpperBlit(tmpsurf, nil, finalSurface, @rect);
bd9a2f1b0080 SDLTry doesn't halt engine no more
unc0rr
parents: 11362
diff changeset
   588
            SDL_FreeSurface(tmpsurf);
bd9a2f1b0080 SDLTry doesn't halt engine no more
unc0rr
parents: 11362
diff changeset
   589
        end;
10687
2e921409b5b1 cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents: 10494
diff changeset
   590
        inc(line);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   591
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   592
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   593
    RenderSpeechBubbleTex:= Surface2Tex(finalSurface, true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   594
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   595
    SDL_FreeSurface(rotatedEdge);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   596
    SDL_FreeSurface(finalSurface);
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7546
diff changeset
   597
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   598
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   599
4611
445d382cd401 A very very basic snow implementation
nemo
parents: 4404
diff changeset
   600
end.