hedgewars/uTextures.pas
author unc0rr
Thu, 07 Nov 2013 21:06:30 +0400
branchsdl2transition
changeset 9682 aa2431ed87b2
parent 9655 e154ccca4dad
child 11342 ed5a6478e710
permissions -rw-r--r--
Provide more information to SDLTry
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     1
(*
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     2
 * Hedgewars, a free turn based strategy game
9080
9b42757d7e71 bump copyright year for Andrey entries
unc0rr
parents: 8027
diff changeset
     3
 * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     4
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     8
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    12
 * GNU General Public License for more details.
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    13
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    14
 * You should have received a copy of the GNU General Public License
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    15
 * along with this program; if not, write to the Free Software
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    17
 *)
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    18
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    19
{$INCLUDE "options.inc"}
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    20
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    21
unit uTextures;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    22
interface
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    23
uses SDLh, uTypes;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    24
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    25
function  NewTexture(width, height: Longword; buf: Pointer): PTexture;
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
    26
procedure Surface2GrayScale(surf: PSDL_Surface);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    27
function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    28
procedure FreeTexture(tex: PTexture);
9655
e154ccca4dad Tinted crosshair (without that cool white dot in the middle)
unc0rr
parents: 9080
diff changeset
    29
procedure FreeAndNilTexture(var tex: PTexture);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    30
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    31
procedure initModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    32
procedure freeModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    33
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    34
implementation
6394
f0a9042e7387 yay, finally osx (and likely windows) fullscreen switch works like on linux! ALL textures had to be destroyed and recreated only after the new window got created. In other news, the new window must be cleaned with glClear to skip a first frame of garbage and AddProgress is only called the first time.
koda
parents: 6390
diff changeset
    35
uses GLunit, uUtils, uVariables, uConsts, uDebug, uConsole;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    36
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    37
var TextureList: PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    38
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    39
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    40
procedure SetTextureParameters(enableClamp: Boolean);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    41
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    42
    if enableClamp and ((cReducedQuality and rqClampLess) = 0) then
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
    43
        begin
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    44
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    45
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
    46
        end;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    47
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    48
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    49
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    50
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    51
procedure ResetVertexArrays(texture: PTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    52
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    53
with texture^ do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    54
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    55
    vb[0].X:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    56
    vb[0].Y:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    57
    vb[1].X:= w;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    58
    vb[1].Y:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    59
    vb[2].X:= w;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    60
    vb[2].Y:= h;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    61
    vb[3].X:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    62
    vb[3].Y:= h;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    63
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    64
    tb[0].X:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    65
    tb[0].Y:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    66
    tb[1].X:= rx;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    67
    tb[1].Y:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    68
    tb[2].X:= rx;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    69
    tb[2].Y:= ry;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    70
    tb[3].X:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    71
    tb[3].Y:= ry
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    72
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    73
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    74
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    75
function NewTexture(width, height: Longword; buf: Pointer): PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    76
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    77
new(NewTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    78
NewTexture^.PrevTexture:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    79
NewTexture^.NextTexture:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    80
NewTexture^.Scale:= 1;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    81
if TextureList <> nil then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    82
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    83
    TextureList^.PrevTexture:= NewTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    84
    NewTexture^.NextTexture:= TextureList
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    85
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    86
TextureList:= NewTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    87
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    88
NewTexture^.w:= width;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    89
NewTexture^.h:= height;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    90
NewTexture^.rx:= 1.0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    91
NewTexture^.ry:= 1.0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    92
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    93
ResetVertexArrays(NewTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    94
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    95
glGenTextures(1, @NewTexture^.id);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    96
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    97
glBindTexture(GL_TEXTURE_2D, NewTexture^.id);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    98
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    99
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   100
SetTextureParameters(true);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   101
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   102
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   103
procedure Surface2GrayScale(surf: PSDL_Surface);
6305
5f7480c2a08d Set default water colours in greyscale mode in case the theme does not define them, decrement piano weapon on use
nemo
parents: 6303
diff changeset
   104
var tw, x, y: Longword;
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   105
    fromP4: PLongWordArray;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   106
begin
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   107
fromP4:= Surf^.pixels;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   108
for y:= 0 to Pred(Surf^.h) do
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   109
    begin
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   110
    for x:= 0 to Pred(Surf^.w) do 
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   111
        begin
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   112
        tw:= fromP4^[x];
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   113
        tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED +  
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   114
              (tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + 
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   115
              (tw shr BShift and $FF) * RGB_LUMINANCE_BLUE);
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   116
        if tw > 255 then tw:= 255;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   117
        tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask);
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   118
        fromP4^[x]:= tw;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   119
        end;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   120
    fromP4:= @(fromP4^[Surf^.pitch div 4])
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   121
    end;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   122
end;
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents: 6394
diff changeset
   123
090269e528df - Improve parsing of prefix operators
unc0rr
parents: 6394
diff changeset
   124
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   125
function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   126
var tw, th, x, y: Longword;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   127
    tmpp: pointer;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   128
    fromP4, toP4: PLongWordArray;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   129
begin
8027
e5ba3dd12531 make stats-only mode work headless. also skip a few things to save time/memory.
nemo
parents: 7151
diff changeset
   130
if cOnlyStats then exit(nil);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   131
new(Surface2Tex);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   132
Surface2Tex^.PrevTexture:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   133
Surface2Tex^.NextTexture:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   134
if TextureList <> nil then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   135
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   136
    TextureList^.PrevTexture:= Surface2Tex;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   137
    Surface2Tex^.NextTexture:= TextureList
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   138
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   139
TextureList:= Surface2Tex;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   140
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   141
Surface2Tex^.w:= surf^.w;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   142
Surface2Tex^.h:= surf^.h;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   143
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   144
if (surf^.format^.BytesPerPixel <> 4) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   145
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   146
    TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   147
    Surface2Tex^.id:= 0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   148
    exit
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   149
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   150
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   151
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   152
glGenTextures(1, @Surface2Tex^.id);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   153
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   154
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   155
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   156
if SDL_MustLock(surf) then
9682
aa2431ed87b2 Provide more information to SDLTry
unc0rr
parents: 9655
diff changeset
   157
    SDLTry(SDL_LockSurface(surf) >= 0, 'Lock surface', true);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   158
5441
39962b855540 Add grayscale option for 3d, helps with colour clashing
nemo
parents: 4976
diff changeset
   159
fromP4:= Surf^.pixels;
39962b855540 Add grayscale option for 3d, helps with colour clashing
nemo
parents: 4976
diff changeset
   160
6982
8d41d22a291d breaking news, we don't support typed consts anymore
koda
parents: 6700
diff changeset
   161
if GrayScale then
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   162
    Surface2GrayScale(Surf);
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   163
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   164
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   165
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   166
    tw:= toPowerOf2(Surf^.w);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   167
    th:= toPowerOf2(Surf^.h);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   168
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   169
    Surface2Tex^.rx:= Surf^.w / tw;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   170
    Surface2Tex^.ry:= Surf^.h / th;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   171
7151
ec15d9e1a7e3 pas2c stuff
unc0rr
parents: 6982
diff changeset
   172
    tmpp:= GetMem(tw * th * surf^.format^.BytesPerPixel);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   173
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   174
    fromP4:= Surf^.pixels;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   175
    toP4:= tmpp;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   176
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   177
    for y:= 0 to Pred(Surf^.h) do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   178
        begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   179
        for x:= 0 to Pred(Surf^.w) do
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   180
            toP4^[x]:= fromP4^[x];
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   181
        for x:= Surf^.w to Pred(tw) do
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   182
            toP4^[x]:= 0;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   183
        toP4:= @(toP4^[tw]);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   184
        fromP4:= @(fromP4^[Surf^.pitch div 4])
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
    for y:= Surf^.h to Pred(th) do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   188
        begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   189
        for x:= 0 to Pred(tw) do
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   190
            toP4^[x]:= 0;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   191
        toP4:= @(toP4^[tw])
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   192
        end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   193
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   194
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   195
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   196
    FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   197
    end
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   198
else
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   199
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   200
    Surface2Tex^.rx:= 1.0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   201
    Surface2Tex^.ry:= 1.0;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   202
    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
   203
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   204
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   205
ResetVertexArrays(Surface2Tex);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   206
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   207
if SDL_MustLock(surf) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   208
    SDL_UnlockSurface(surf);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   209
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   210
SetTextureParameters(enableClamp);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   211
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   212
4901
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4403
diff changeset
   213
// deletes texture and frees the memory allocated for it.
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4403
diff changeset
   214
// if nil is passed nothing is done
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   215
procedure FreeTexture(tex: PTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   216
begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   217
if tex <> nil then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   218
    begin
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   219
    if tex^.NextTexture <> nil then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   220
        tex^.NextTexture^.PrevTexture:= tex^.PrevTexture;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   221
    if tex^.PrevTexture <> nil then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   222
        tex^.PrevTexture^.NextTexture:= tex^.NextTexture
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   223
    else
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   224
        TextureList:= tex^.NextTexture;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   225
    glDeleteTextures(1, @tex^.id);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   226
    Dispose(tex);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   227
    end
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   228
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   229
9655
e154ccca4dad Tinted crosshair (without that cool white dot in the middle)
unc0rr
parents: 9080
diff changeset
   230
procedure FreeAndNilTexture(var tex: PTexture);
e154ccca4dad Tinted crosshair (without that cool white dot in the middle)
unc0rr
parents: 9080
diff changeset
   231
begin
e154ccca4dad Tinted crosshair (without that cool white dot in the middle)
unc0rr
parents: 9080
diff changeset
   232
    FreeTexture(tex);
e154ccca4dad Tinted crosshair (without that cool white dot in the middle)
unc0rr
parents: 9080
diff changeset
   233
    tex:= nil
e154ccca4dad Tinted crosshair (without that cool white dot in the middle)
unc0rr
parents: 9080
diff changeset
   234
end;
e154ccca4dad Tinted crosshair (without that cool white dot in the middle)
unc0rr
parents: 9080
diff changeset
   235
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   236
procedure initModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   237
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   238
TextureList:= nil;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   239
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   240
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   241
procedure freeModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   242
begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   243
if TextureList <> nil then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   244
    WriteToConsole('FIXME FIXME FIXME. App shutdown without full cleanup of texture list; read game0.log and please report this problem');
6390
3807d4cad077 This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents: 6380
diff changeset
   245
    while TextureList <> nil do 
3807d4cad077 This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents: 6380
diff changeset
   246
        begin
3807d4cad077 This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents: 6380
diff changeset
   247
        AddFileLog('Texture not freed: width='+inttostr(LongInt(TextureList^.w))+' height='+inttostr(LongInt(TextureList^.h))+' priority='+inttostr(round(TextureList^.priority*1000)));
3807d4cad077 This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents: 6380
diff changeset
   248
        FreeTexture(TextureList);
3807d4cad077 This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents: 6380
diff changeset
   249
        end
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   250
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   251
4901
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4403
diff changeset
   252
end.