hedgewars/uRenderUtils.pas
author koda
Fri, 17 Feb 2012 18:23:36 +0100
changeset 6700 e04da46ee43c
parent 6620 b211d0b690de
child 6749 6bb0cea803bd
permissions -rw-r--r--
the most important commit of the year
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
6700
e04da46ee43c the most important commit of the year
koda
parents: 6620
diff changeset
     3
 * Copyright (c) 2004-2012 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
088d40d8aba2 Happy 2011 :)
koda
parents: 4611
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
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);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    27
procedure copyRotatedSurface(src, dest: PSDL_Surface); // this is necessary since width/height are read only in SDL
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    28
procedure copyToXY(src, dest: PSDL_Surface; destX, destY: 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
    29
procedure copyToXY(src, dest: PSDL_Surface; srcX, srcY, srcW, srcH, destX, destY: 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
    30
procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt; frame: LongInt = 0);
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
    31
procedure DrawLine2Surf(dest: PSDL_Surface; x0,y0,x1,y1:LongInt; r,g,b: byte);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    32
function  RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    33
function  RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    34
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    35
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    36
implementation
4404
6bae4e9461aa Remove some more circular dependencies
unc0rr
parents: 4403
diff changeset
    37
uses uUtils, uVariables, uConsts, uTextures, sysutils, uDebug;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    38
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    39
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    40
var r: TSDL_Rect;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    41
begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    42
    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
    43
    if Clear then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
    44
        SDL_FillRect(Surface, @r, 0);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    45
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    46
    BorderColor:= SDL_MapRGB(Surface^.format, BorderColor shr 16, BorderColor shr 8, BorderColor and $FF);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    47
    FillColor:= SDL_MapRGB(Surface^.format, FillColor shr 16, FillColor shr 8, FillColor and $FF);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    48
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    49
    r.y:= rect^.y + 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    50
    r.h:= rect^.h - 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    51
    SDL_FillRect(Surface, @r, BorderColor);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    52
    r.x:= rect^.x + 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    53
    r.w:= rect^.w - 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    54
    r.y:= rect^.y;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    55
    r.h:= rect^.h;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    56
    SDL_FillRect(Surface, @r, BorderColor);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    57
    r.x:= rect^.x + 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    58
    r.y:= rect^.y + 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    59
    r.w:= rect^.w - 4;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    60
    r.h:= rect^.h - 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    61
    SDL_FillRect(Surface, @r, FillColor);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    62
    r.x:= rect^.x + 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    63
    r.y:= rect^.y + 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    64
    r.w:= rect^.w - 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    65
    r.h:= rect^.h - 4;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    66
    SDL_FillRect(Surface, @r, FillColor)
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    67
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    68
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    69
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    70
var w, h: LongInt;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    71
    tmpsurf: PSDL_Surface;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    72
    clr: TSDL_Color;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    73
    finalRect: TSDL_Rect;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    74
begin
6286
835392304f81 and while we are giving SDLh.pas all this love, let's fix the signature of one SDL_ttf calls
koda
parents: 4976
diff changeset
    75
    TTF_SizeUTF8(Fontz[Font].Handle, Str2PChar(s), @w, @h);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    76
    finalRect.x:= X;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    77
    finalRect.y:= Y;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    78
    finalRect.w:= w + FontBorder * 2 + 4;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    79
    finalRect.h:= h + FontBorder * 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    80
    DrawRoundRect(@finalRect, cWhiteColor, endian(cNearBlackColorChannels.value), Surface, true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    81
    clr.r:= (Color shr 16) and $FF;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    82
    clr.g:= (Color shr 8) and $FF;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    83
    clr.b:= Color and $FF;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    84
    tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(s), clr);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    85
    finalRect.x:= X + FontBorder + 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    86
    finalRect.y:= Y + FontBorder;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    87
    SDLTry(tmpsurf <> nil, true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    88
    SDL_UpperBlit(tmpsurf, nil, Surface, @finalRect);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    89
    SDL_FreeSurface(tmpsurf);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    90
    finalRect.x:= X;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    91
    finalRect.y:= Y;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    92
    finalRect.w:= w + FontBorder * 2 + 4;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    93
    finalRect.h:= h + FontBorder * 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    94
    WriteInRoundRect:= finalRect;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    95
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    96
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    97
procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    98
var y, x, i, j: LongInt;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
    99
    tmpPixel: Longword;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   100
    pixels: PLongWordArray;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   101
begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   102
    TryDo(Surface^.format^.BytesPerPixel = 4, 'flipSurface failed, expecting 32 bit surface', true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   103
    pixels:= Surface^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   104
    if Vertical then
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   105
    for y := 0 to (Surface^.h div 2) - 1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   106
        for x := 0 to Surface^.w - 1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   107
            begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   108
            i:= y * Surface^.w + x;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   109
            j:= (Surface^.h - y - 1) * Surface^.w + x;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   110
            tmpPixel:= pixels^[i];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   111
            pixels^[i]:= pixels^[j];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   112
            pixels^[j]:= tmpPixel;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   113
            end
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   114
    else
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   115
    for x := 0 to (Surface^.w div 2) - 1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   116
        for y := 0 to Surface^.h -1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   117
            begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   118
            i:= y*Surface^.w + x;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   119
            j:= y*Surface^.w + (Surface^.w - x - 1);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   120
            tmpPixel:= pixels^[i];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   121
            pixels^[i]:= pixels^[j];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   122
            pixels^[j]:= tmpPixel;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   123
            end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   124
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   125
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   126
procedure copyToXY(src, dest: PSDL_Surface; destX, destY: 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
   127
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
   128
    copyToXY(src, dest, 0,0,src^.w, src^.h, destX, destY);
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
   129
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
   130
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
   131
procedure copyToXY(src, dest: PSDL_Surface; srcX, srcY, srcW, srcH, destX, destY: 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
   132
var i, j, maxDest, maxSrc, iX, iY: LongInt;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   133
    srcPixels, destPixels: PLongWordArray;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   134
    r0, g0, b0, a0, r1, g1, b1, a1: Byte;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   135
begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   136
    maxDest:= (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
   137
    maxSrc:= (src^.pitch div 4) * src^.h;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   138
    srcPixels:= src^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   139
    destPixels:= dest^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   140
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
   141
    for iX:= 0 to srcW - 1 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
   142
    for iY:= 0 to srcH - 1 do
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   143
        begin
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
   144
        i:= (destY + iY) * (dest^.pitch div 4) + (destX + iX);
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
   145
        j:= (srcY  + iY) * (src^.pitch  div 4) + (srcX  + iX);
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
   146
        if (i < maxDest) and (j < maxSrc) and (srcPixels^[j] and AMask <> 0) then
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   147
            begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   148
            SDL_GetRGBA(destPixels^[i], dest^.format, @r0, @g0, @b0, @a0);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   149
            SDL_GetRGBA(srcPixels^[j], src^.format, @r1, @g1, @b1, @a1);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   150
            r0:= (r0 * (255 - LongInt(a1)) + r1 * LongInt(a1)) div 255;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   151
            g0:= (g0 * (255 - LongInt(a1)) + g1 * LongInt(a1)) div 255;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   152
            b0:= (b0 * (255 - LongInt(a1)) + b1 * LongInt(a1)) div 255;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   153
            a0:= (a0 * (255 - LongInt(a1)) + a1 * LongInt(a1)) div 255;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   154
            destPixels^[i]:= SDL_MapRGBA(dest^.format, r0, g0, b0, a0);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   155
            end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   156
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   157
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   158
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
   159
procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y,frame: 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
   160
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
   161
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
   162
    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
   163
    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
   164
    col:= Frame div 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
   165
    
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
   166
    copyToXY(SpritesData[sprite].Surface, dest, 
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
   167
             col*SpritesData[sprite].Width, 
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
             row*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
   169
             SpritesData[sprite].Width, 
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
   170
             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
   171
             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
   172
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
   173
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
   174
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
   175
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
   176
    max: 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
   177
    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
   178
    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
   179
    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
   180
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
   181
    max:= (dest^.pitch div 4) * dest^.h;
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
   182
    yMax:= dest^.pitch div 4;
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
   183
    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
   184
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
   185
    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
   186
    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
   187
    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
   188
    if y0 < y1 then sy:= 1 else sy:= -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
   189
    err:= dx-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
   190
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
   191
    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
   192
        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
   193
        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
   194
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
   195
        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
   196
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
   197
        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
   198
        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
   199
            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
   200
            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
   201
            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
   202
            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
   203
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
   204
        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
   205
            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
   206
            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
   207
            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
   208
            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
   209
        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
   210
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
   211
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   212
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
   213
var y, x, i, j: LongInt;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   214
    srcPixels, destPixels: PLongWordArray;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   215
begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   216
    TryDo(src^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   217
    TryDo(dest^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   218
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   219
    srcPixels:= src^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   220
    destPixels:= dest^.pixels;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   221
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   222
    j:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   223
    for x := 0 to src^.w - 1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   224
        for y := 0 to src^.h - 1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   225
            begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   226
            i:= (src^.h - 1 - y) * (src^.pitch div 4) + x;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   227
            destPixels^[j]:= srcPixels^[i];
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   228
            inc(j)
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   229
            end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   230
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   231
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   232
function  RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   233
var w, h: LongInt;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   234
    finalSurface: PSDL_Surface;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   235
begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   236
    if length(s) = 0 then s:= ' ';
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   237
    font:= CheckCJKFont(s, font);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   238
    w:= 0; h:= 0; // avoid compiler hints
6286
835392304f81 and while we are giving SDLh.pas all this love, let's fix the signature of one SDL_ttf calls
koda
parents: 4976
diff changeset
   239
    TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), @w, @h);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   240
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   241
    finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + FontBorder * 2 + 4, h + FontBorder * 2,
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   242
            32, RMask, GMask, BMask, AMask);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   243
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   244
    TryDo(finalSurface <> nil, 'RenderString: fail to create surface', true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   245
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   246
    WriteInRoundRect(finalSurface, 0, 0, Color, font, s);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   247
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   248
    TryDo(SDL_SetColorKey(finalSurface, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   249
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   250
    RenderStringTex:= Surface2Tex(finalSurface, false);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   251
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   252
    SDL_FreeSurface(finalSurface);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   253
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   254
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   255
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   256
function RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   257
var textWidth, textHeight, x, y, w, h, i, j, pos, prevpos, line, numLines, edgeWidth, edgeHeight, cornerWidth, cornerHeight: LongInt;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   258
    finalSurface, tmpsurf, rotatedEdge: PSDL_Surface;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   259
    rect: TSDL_Rect;
4611
445d382cd401 A very very basic snow implementation
nemo
parents: 4404
diff changeset
   260
    chars: set of char = [#9,' ',';',':','?','!',','];
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   261
    substr: shortstring;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   262
    edge, corner, tail: TSPrite;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   263
begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   264
    case SpeechType of
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   265
        1: begin;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   266
        edge:= sprSpeechEdge;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   267
        corner:= sprSpeechCorner;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   268
        tail:= sprSpeechTail;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   269
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   270
        2: begin;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   271
        edge:= sprThoughtEdge;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   272
        corner:= sprThoughtCorner;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   273
        tail:= sprThoughtTail;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   274
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   275
        3: begin;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   276
        edge:= sprShoutEdge;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   277
        corner:= sprShoutCorner;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   278
        tail:= sprShoutTail;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   279
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   280
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   281
    edgeHeight:= SpritesData[edge].Height;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   282
    edgeWidth:= SpritesData[edge].Width;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   283
    cornerWidth:= SpritesData[corner].Width;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   284
    cornerHeight:= SpritesData[corner].Height;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   285
    // This one screws up WrapText
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   286
    //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
   287
    // This one does not
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   288
    //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
   289
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   290
    numLines:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   291
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   292
    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
   293
        s:= '...';
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   294
    font:= CheckCJKFont(s, font);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   295
    w:= 0; h:= 0; // avoid compiler hints
6286
835392304f81 and while we are giving SDLh.pas all this love, let's fix the signature of one SDL_ttf calls
koda
parents: 4976
diff changeset
   296
    TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(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
   297
    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
   298
        w:= 8;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   299
    j:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   300
    if (length(s) > 20) then
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   301
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   302
        w:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   303
        i:= round(Sqrt(length(s)) * 2);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   304
        s:= WrapText(s, #1, chars, i);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   305
        pos:= 1; prevpos:= 0; line:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   306
    // Find the longest line for the purposes of centring the text.  Font dependant.
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   307
        while pos <= length(s) do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   308
            begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   309
            if (s[pos] = #1) or (pos = length(s)) then
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   310
                begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   311
                inc(numlines);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   312
                if s[pos] <> #1 then inc(pos);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   313
                while s[prevpos+1] = ' ' do inc(prevpos);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   314
                substr:= copy(s, prevpos+1, pos-prevpos-1);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   315
                i:= 0; j:= 0;
6286
835392304f81 and while we are giving SDLh.pas all this love, let's fix the signature of one SDL_ttf calls
koda
parents: 4976
diff changeset
   316
                TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(substr), @i, @j);
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   317
                if i > w then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   318
                    w:= i;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   319
                prevpos:= pos;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   320
                end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   321
            inc(pos);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   322
            end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   323
        end
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   324
    else numLines := 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   325
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   326
    textWidth:=((w-(cornerWidth-edgeWidth)*2) div edgeWidth)*edgeWidth+edgeWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   327
    textHeight:=(((numlines * h + 2)-((cornerHeight-edgeWidth)*2)) div edgeWidth)*edgeWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   328
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   329
    textHeight:=max(textHeight,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   330
    //textWidth:=max(textWidth,SpritesData[tail].Width);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   331
    rect.x:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   332
    rect.y:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   333
    rect.w:= textWidth + (cornerWidth * 2);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   334
    rect.h:= textHeight + cornerHeight*2 - edgeHeight + SpritesData[tail].Height;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   335
    //s:= inttostr(w) + ' ' + inttostr(numlines) + ' ' + inttostr(rect.x) + ' '+inttostr(rect.y) + ' ' + inttostr(rect.w) + ' ' + inttostr(rect.h);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   336
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   337
    finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, rect.h, 32, RMask, GMask, BMask, AMask);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   338
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   339
    TryDo(finalSurface <> nil, 'RenderString: fail to create surface', true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   340
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   341
    //////////////////////////////// CORNERS ///////////////////////////////
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   342
    copyToXY(SpritesData[corner].Surface, finalSurface, 0, 0); /////////////////// NW
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   343
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   344
    flipSurface(SpritesData[corner].Surface, true); // store all 4 versions in memory to avoid repeated flips?
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   345
    x:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   346
    y:= textHeight + cornerHeight -1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   347
    copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SW
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   348
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   349
    flipSurface(SpritesData[corner].Surface, false);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   350
    x:= rect.w-cornerWidth-1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   351
    y:= textHeight + cornerHeight -1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   352
    copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SE
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   353
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   354
    flipSurface(SpritesData[corner].Surface, true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   355
    x:= rect.w-cornerWidth-1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   356
    y:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   357
    copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// NE
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   358
    flipSurface(SpritesData[corner].Surface, false); // restore original position
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   359
    //////////////////////////////// END CORNERS ///////////////////////////////
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   360
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   361
    //////////////////////////////// EDGES //////////////////////////////////////
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   362
    x:= cornerWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   363
    y:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   364
    while x < rect.w-cornerWidth-1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   365
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   366
        copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// top edge
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   367
        inc(x,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   368
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   369
    flipSurface(SpritesData[edge].Surface, true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   370
    x:= cornerWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   371
    y:= textHeight + cornerHeight*2 - edgeHeight-1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   372
    while x < rect.w-cornerWidth-1 do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   373
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   374
        copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// bottom edge
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   375
        inc(x,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   376
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   377
    flipSurface(SpritesData[edge].Surface, true); // restore original position
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   378
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   379
    rotatedEdge:= SDL_CreateRGBSurface(SDL_SWSURFACE, edgeHeight, edgeWidth, 32, RMask, GMask, BMask, AMask);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   380
    x:= rect.w - edgeHeight - 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   381
    y:= cornerHeight;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   382
    //// initially was going to rotate in place, but the SDL spec claims width/height are read only
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   383
    copyRotatedSurface(SpritesData[edge].Surface,rotatedEdge);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   384
    while y < textHeight + cornerHeight do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   385
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   386
        copyToXY(rotatedEdge, finalSurface, x, y);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   387
        inc(y,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   388
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   389
    flipSurface(rotatedEdge, false); // restore original position
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   390
    x:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   391
    y:= cornerHeight;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   392
    while y < textHeight + cornerHeight do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   393
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   394
        copyToXY(rotatedEdge, finalSurface, x, y);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   395
        inc(y,edgeWidth);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   396
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   397
    //////////////////////////////// END EDGES //////////////////////////////////////
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   398
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   399
    x:= cornerWidth;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   400
    y:= textHeight + cornerHeight * 2 - edgeHeight - 1;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   401
    copyToXY(SpritesData[tail].Surface, finalSurface, x, y);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   402
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   403
    rect.x:= edgeHeight;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   404
    rect.y:= edgeHeight;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   405
    rect.w:= rect.w - edgeHeight * 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   406
    rect.h:= textHeight + cornerHeight * 2 - edgeHeight * 2;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   407
    i:= rect.w;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   408
    j:= rect.h;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   409
    SDL_FillRect(finalSurface, @rect, cWhiteColor);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   410
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   411
    pos:= 1; prevpos:= 0; line:= 0;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   412
    while pos <= length(s) do
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   413
        begin
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   414
        if (s[pos] = #1) or (pos = length(s)) then
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   415
            begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   416
            if s[pos] <> #1 then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   417
                inc(pos);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   418
            while s[prevpos+1] = ' 'do
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   419
                inc(prevpos);
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   420
            substr:= copy(s, prevpos+1, pos-prevpos-1);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   421
            if Length(substr) <> 0 then
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   422
                begin
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   423
                tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(substr), cNearBlackColorChannels);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   424
                rect.x:= edgeHeight + 1 + ((i - w) div 2);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   425
                // trying to more evenly position the text, vertically
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   426
                rect.y:= edgeHeight + ((j-(numLines*h)) div 2) + line * h;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   427
                SDLTry(tmpsurf <> nil, true);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   428
                SDL_UpperBlit(tmpsurf, nil, finalSurface, @rect);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   429
                SDL_FreeSurface(tmpsurf);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   430
                inc(line);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   431
                prevpos:= pos;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   432
                end;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6286
diff changeset
   433
                end;
4380
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   434
        inc(pos);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   435
        end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   436
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   437
    RenderSpeechBubbleTex:= Surface2Tex(finalSurface, true);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   438
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   439
    SDL_FreeSurface(rotatedEdge);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   440
    SDL_FreeSurface(finalSurface);
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   441
end;
b78638b36b89 Introduce uRenderUtils
unC0Rr
parents:
diff changeset
   442
4611
445d382cd401 A very very basic snow implementation
nemo
parents: 4404
diff changeset
   443
end.