hedgewars/uTextures.pas
author nemo
Wed, 22 Dec 2010 10:09:39 -0500
changeset 4628 b28516c13c05
parent 4403 0dfe26f48ec1
child 4901 d1e2d82d9ccc
permissions -rw-r--r--
trying a graphic with more white and less translucency
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
     1
{$INCLUDE "options.inc"}
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
     2
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
     3
unit uTextures;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
     4
interface
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
     5
uses SDLh, uTypes;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
     6
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
     7
function  NewTexture(width, height: Longword; buf: Pointer): PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
     8
function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
     9
procedure FreeTexture(tex: PTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    10
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    11
procedure initModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    12
procedure freeModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    13
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    14
implementation
4403
unc0rr
parents: 4381
diff changeset
    15
uses GLunit, uUtils, uVariables, uConsts, uDebug;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    16
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    17
var TextureList: PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    18
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    19
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    20
procedure SetTextureParameters(enableClamp: Boolean);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    21
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    22
    if enableClamp and ((cReducedQuality and rqClampLess) = 0) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    23
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    24
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    25
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    26
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    27
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    28
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    29
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    30
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    31
procedure ResetVertexArrays(texture: PTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    32
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    33
with texture^ do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    34
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    35
    vb[0].X:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    36
    vb[0].Y:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    37
    vb[1].X:= w;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    38
    vb[1].Y:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    39
    vb[2].X:= w;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    40
    vb[2].Y:= h;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    41
    vb[3].X:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    42
    vb[3].Y:= h;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    43
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    44
    tb[0].X:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    45
    tb[0].Y:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    46
    tb[1].X:= rx;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    47
    tb[1].Y:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    48
    tb[2].X:= rx;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    49
    tb[2].Y:= ry;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    50
    tb[3].X:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    51
    tb[3].Y:= ry
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    52
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    53
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    54
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    55
function NewTexture(width, height: Longword; buf: Pointer): PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    56
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    57
new(NewTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    58
NewTexture^.PrevTexture:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    59
NewTexture^.NextTexture:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    60
NewTexture^.Scale:= 1;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    61
if TextureList <> nil then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    62
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    63
    TextureList^.PrevTexture:= NewTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    64
    NewTexture^.NextTexture:= TextureList
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    65
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    66
TextureList:= NewTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    67
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    68
NewTexture^.w:= width;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    69
NewTexture^.h:= height;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    70
NewTexture^.rx:= 1.0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    71
NewTexture^.ry:= 1.0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    72
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    73
ResetVertexArrays(NewTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    74
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    75
glGenTextures(1, @NewTexture^.id);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    76
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    77
glBindTexture(GL_TEXTURE_2D, NewTexture^.id);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    78
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    79
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    80
SetTextureParameters(true);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    81
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    82
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    83
function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    84
var tw, th, x, y: Longword;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    85
    tmpp: pointer;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    86
    fromP4, toP4: PLongWordArray;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    87
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    88
new(Surface2Tex);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    89
Surface2Tex^.PrevTexture:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    90
Surface2Tex^.NextTexture:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    91
if TextureList <> nil then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    92
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    93
    TextureList^.PrevTexture:= Surface2Tex;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    94
    Surface2Tex^.NextTexture:= TextureList
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    95
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    96
TextureList:= Surface2Tex;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    97
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    98
Surface2Tex^.w:= surf^.w;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    99
Surface2Tex^.h:= surf^.h;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   100
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   101
if (surf^.format^.BytesPerPixel <> 4) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   102
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   103
    TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   104
    Surface2Tex^.id:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   105
    exit
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   106
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   107
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   108
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   109
glGenTextures(1, @Surface2Tex^.id);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   110
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   111
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   112
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   113
if SDL_MustLock(surf) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   114
    SDLTry(SDL_LockSurface(surf) >= 0, true);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   115
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   116
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   117
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   118
    tw:= toPowerOf2(Surf^.w);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   119
    th:= toPowerOf2(Surf^.h);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   120
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   121
    Surface2Tex^.rx:= Surf^.w / tw;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   122
    Surface2Tex^.ry:= Surf^.h / th;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   123
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   124
    GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   125
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   126
    fromP4:= Surf^.pixels;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   127
    toP4:= tmpp;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   128
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   129
    for y:= 0 to Pred(Surf^.h) do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   130
        begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   131
        for x:= 0 to Pred(Surf^.w) do toP4^[x]:= fromP4^[x];
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   132
        for x:= Surf^.w to Pred(tw) do toP4^[x]:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   133
        toP4:= @(toP4^[tw]);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   134
        fromP4:= @(fromP4^[Surf^.pitch div 4])
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   135
        end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   136
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   137
    for y:= Surf^.h to Pred(th) do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   138
        begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   139
        for x:= 0 to Pred(tw) do toP4^[x]:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   140
        toP4:= @(toP4^[tw])
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   141
        end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   142
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   143
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   144
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   145
    FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   146
    end
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   147
else
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   148
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   149
    Surface2Tex^.rx:= 1.0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   150
    Surface2Tex^.ry:= 1.0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   151
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   152
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   153
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   154
ResetVertexArrays(Surface2Tex);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   155
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   156
if SDL_MustLock(surf) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   157
    SDL_UnlockSurface(surf);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   158
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   159
SetTextureParameters(enableClamp);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   160
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   161
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   162
procedure FreeTexture(tex: PTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   163
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   164
    if tex <> nil then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   165
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   166
        if tex^.NextTexture <> nil then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   167
            tex^.NextTexture^.PrevTexture:= tex^.PrevTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   168
        if tex^.PrevTexture <> nil then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   169
            tex^.PrevTexture^.NextTexture:= tex^.NextTexture
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   170
        else
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   171
            TextureList:= tex^.NextTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   172
        glDeleteTextures(1, @tex^.id);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   173
        Dispose(tex);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   174
    end
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   175
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   176
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   177
procedure initModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   178
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   179
TextureList:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   180
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   181
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   182
procedure freeModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   183
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   184
    while TextureList <> nil do FreeTexture(TextureList);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   185
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   186
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   187
end.