Skip to content

Commit

Permalink
Протестовано менеджер текстур
Browse files Browse the repository at this point in the history
  • Loading branch information
iso4free committed Sep 5, 2022
1 parent ad3681a commit 2af737d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 48 deletions.
118 changes: 85 additions & 33 deletions demo06/demo06.ctps
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<IsPartOfProject Value="True"/>
<EditorIndex Value="0"/>
<WindowIndex Value="0"/>
<CursorPos X="28" Y="16"/>
<UsageCount Value="27"/>
<CursorPos X="77" Y="17"/>
<UsageCount Value="29"/>
<Loaded Value="True"/>
</Unit>
<Unit>
Expand All @@ -20,106 +20,158 @@
<IsVisibleTab Value="True"/>
<EditorIndex Value="1"/>
<WindowIndex Value="0"/>
<CursorPos X="43" Y="108"/>
<UsageCount Value="14"/>
<TopLine Value="340"/>
<CursorPos X="51" Y="359"/>
<UsageCount Value="15"/>
<Loaded Value="True"/>
</Unit>
<Unit>
<Filename Value="/usr/local/codetyphon/fpcsrc/rtl/objpas/fgl.pp"/>
<FRMtype Value="0"/>
<EditorIndex Value="-1"/>
<WindowIndex Value="0"/>
<TopLine Value="283"/>
<CursorPos X="21" Y="292"/>
<UsageCount Value="9"/>
<TopLine Value="1679"/>
<CursorPos Y="1702"/>
<UsageCount Value="10"/>
</Unit>
<Unit>
<Filename Value="../SDL2-for-Pascal/units/sdlrenderer.inc"/>
<FRMtype Value="0"/>
<EditorIndex Value="2"/>
<EditorIndex Value="-1"/>
<WindowIndex Value="0"/>
<TopLine Value="873"/>
<CursorPos X="166" Y="889"/>
<TopLine Value="864"/>
<CursorPos X="144" Y="889"/>
<UsageCount Value="11"/>
<Loaded Value="True"/>
</Unit>
<Unit>
<Filename Value="../SDL2-for-Pascal/units/sdlrect.inc"/>
<FRMtype Value="0"/>
<EditorIndex Value="-1"/>
<WindowIndex Value="0"/>
<CursorPos X="3" Y="16"/>
<UsageCount Value="10"/>
</Unit>
</Units>
<JumpHistory HistoryIndex="18">
<JumpHistory HistoryIndex="29">
<Position>
<Filename Value="demo06.ppr"/>
<Caret Line="10" Column="43"/>
<Filename Value="ugoengine.pas"/>
<Caret Line="147" Column="82" TopLine="135"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="72" Column="33"/>
<Caret Line="132" Column="82" TopLine="110"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="42" Column="20" TopLine="24"/>
<Caret Line="135" Column="83" TopLine="110"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="56" Column="18" TopLine="41"/>
<Caret Line="153" Column="83" TopLine="119"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="226" TopLine="206"/>
<Caret Line="332" Column="21" TopLine="323"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="127" Column="21" TopLine="113"/>
<Caret Line="61" Column="19" TopLine="43"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="18" Column="100" TopLine="4"/>
<Caret Line="301" Column="21" TopLine="294"/>
</Position>
<Position>
<Filename Value="../SDL2-for-Pascal/units/sdlrenderer.inc"/>
<Caret Line="75" Column="42" TopLine="63"/>
<Filename Value="ugoengine.pas"/>
<Caret Line="248" Column="36" TopLine="231"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="55" Column="15" TopLine="35"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="244" Column="3" TopLine="240"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="52" Column="20" TopLine="35"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="219" Column="59" TopLine="213"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="55" Column="20" TopLine="30"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="156" Column="42" TopLine="143"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="24" Column="34" TopLine="11"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="165" Column="25" TopLine="156"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="19" Column="20" TopLine="2"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="115" Column="50" TopLine="97"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="65" Column="17" TopLine="31"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="20" Column="9" TopLine="4"/>
<Caret Line="338" Column="9" TopLine="331"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="98" Column="35" TopLine="96"/>
<Caret Line="53" Column="20" TopLine="41"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="47" Column="93" TopLine="25"/>
<Caret Line="206" Column="17" TopLine="184"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="146" Column="22" TopLine="134"/>
<Caret Line="67" Column="17" TopLine="31"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="240" TopLine="227"/>
<Caret Line="347" Column="21" TopLine="319"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="239" Column="98" TopLine="226"/>
<Caret Line="348" Column="16" TopLine="323"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="78" Column="69" TopLine="53"/>
<Caret Line="53" Column="19" TopLine="46"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="24" Column="24" TopLine="9"/>
<Caret Line="359" Column="47" TopLine="336"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="117" Column="20" TopLine="97"/>
<Caret Line="23" Column="15"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="24" Column="17" TopLine="6"/>
<Caret Line="147" Column="31" TopLine="141"/>
</Position>
<Position>
<Filename Value="ugoengine.pas"/>
<Caret Line="139" Column="41" TopLine="133"/>
<Caret Line="67" Column="18" TopLine="41"/>
</Position>
</JumpHistory>
<RunParams>
Expand Down
1 change: 1 addition & 0 deletions demo06/demo06.ppr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ begin
GoEngine.Width:=640;
GoEngine.Height:=480;
GoEngine.Caption:='Демо 06';
GoEngine.Texturemanager.Load('assets'+DirectorySeparator+'tux.png','Tux');
GoEngine.Run;
end;
end.
Expand Down
74 changes: 59 additions & 15 deletions demo06/ugoengine.pas
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ TGoTextureManager = class
FTextureMap : TGoTextureMap;
constructor Create;
public
function Load(aFileName : String; aID : String; aRenderer : PSDL_Renderer) : Boolean;
procedure Draw(aID : String; x,y,w,h : Integer; aRenderer : PSDL_Renderer; Flip : Integer = SDL_FLIP_NONE);
procedure DrawFrame(aID : String; x,y,w,h : Integer; CurrentRow, CurrentFrame : Integer; aRenderer : PSDL_Renderer; Flip : Integer = SDL_FLIP_NONE);
destructor Destroy;
function Load(aFileName : String; aID : String) : Boolean;
procedure Draw(aID : String; x,y,w,h : Integer; Flip : Integer = SDL_FLIP_NONE);
procedure DrawFrame(aID : String; x,y,w,h : Integer; CurrentRow, CurrentFrame : Integer; Flip : Integer = SDL_FLIP_NONE);
destructor Destroy; override;
end;


Expand All @@ -48,6 +48,8 @@ TGOEngine = class
FFileName : TFileName; // ім'я файлу налаштувань
FSection : String; //назва секції налаштувань фреймворка

fr,fc : Integer;

constructor Create(Afile: Tfilename = 'goengine.ini'; Asection: String = 'ENGINE');
procedure SetCaption(AValue: String);
procedure SetHeight(AValue: Integer);
Expand Down Expand Up @@ -96,11 +98,11 @@ implementation

constructor TGoTextureManager.Create;
begin

inherited Create;
FTextureMap:=TGoTextureMap.Create;
end;

function TGoTextureManager.Load(aFileName: String; aID: String;
aRenderer: PSDL_Renderer): Boolean;
function TGoTextureManager.Load(aFileName: String; aID: String): Boolean;
var TmpSurface : PSDL_Surface;
TmpTexture : PSDL_Texture;
begin
Expand All @@ -110,24 +112,48 @@ implementation
WriteLN( SDL_GetError(),' Error loading '+aFileName);
Exit;
end;
TmpTexture:=SDL_CreateTextureFromSurface(aRenderer,TmpSurface);
TmpTexture:=SDL_CreateTextureFromSurface(GoEngine.FRenderer,TmpSurface);
SDL_FreeSurface(TmpSurface);
if TmpTexture<>nil then begin
FTextureMap[aID]:=TmpTexture;
Result:=True;
end;
end;

procedure TGoTextureManager.Draw(aID: String; x, y, w, h: Integer;
aRenderer: PSDL_Renderer; Flip: Integer);
procedure TGoTextureManager.Draw(aID: String; x, y, w, h: Integer;
Flip: Integer);
var srcRect, dstRect : TSDL_Rect;
p : TSDL_Point;
begin

srcRect.x := 0;
srcRect.y := 0;
srcRect.w := w;
dstRect.w := w;
srcRect.h := h;
dstRect.h := h;
dstRect.x := x;
dstRect.y := y;
p.x:=0;
p.y:=0;
SDL_RenderCopyEx(GoEngine.FRenderer,FTextureMap[aId],@srcRect,@dstRect,0.0,@p,Flip);
end;

procedure TGoTextureManager.DrawFrame(aID: String; x, y, w, h: Integer;
CurrentRow, CurrentFrame: Integer; aRenderer: PSDL_Renderer; Flip: Integer);
procedure TGoTextureManager.DrawFrame(aID: String; x, y, w, h: Integer;
CurrentRow, CurrentFrame: Integer; Flip: Integer);
var srcRect, dstRect : TSDL_Rect;
p : TSDL_Point;
begin

srcRect.x := w*CurrentFrame;
srcRect.y := h*CurrentRow;
srcRect.w := w;
dstRect.w := w;
srcRect.h := h;
dstRect.h := h;
dstRect.x := x;
dstRect.y := y;
p.x:=0;
p.y:=0;
SDL_RenderCopyEx(GoEngine.FRenderer,FTextureMap[aId],@srcRect,@dstRect,0.0,@p,Flip);
end;

destructor TGoTextureManager.Destroy;
Expand All @@ -138,6 +164,8 @@ implementation
SDL_DestroyTexture(FTextureMap.Data[i]);
FTextureMap.Delete(i);
end;
FreeAndNil(FTextureMap);
inherited Destroy;
end;

{ TGOEngine }
Expand Down Expand Up @@ -172,6 +200,10 @@ implementation
FErrorInfo:=SDL_GetError;
FError:=true;
WriteLn(FErrorInfo);
end else begin
FTexturemanager:= TGoTextureManager.Create;
fr:=0;
fc:=0;
end;
//виділення пам'яті для структури обробки подій
New(FEvent);
Expand Down Expand Up @@ -219,6 +251,7 @@ procedure TGOEngine.Setfullscreen(Avalue: Boolean);
if Ffullscreen then SDL_SetWindowFullscreen(FWindow,SDL_WINDOW_FULLSCREEN)
else SDL_SetWindowFullscreen(FWindow,0);
SDL_UpdateWindowSurface(FWindow);
SDL_SetWindowTitle(FWindow,PChar(UTF8String(FCaption)));
end;

procedure TGOEngine.Sethardwareacceleration(Avalue: Boolean);
Expand Down Expand Up @@ -270,6 +303,8 @@ function TGOEngine.SaveSettings: Boolean;
//зберігаємо налаштування в файл
if not SaveSettings then WriteLN('Не вдалось записати налаштування!');
//прибрати за собою - в оберненому порядку створення

FreeAndNil(FTexturemanager);
Dispose(FEvent);
SDL_DestroyRenderer(FRenderer);
SDL_DestroyWindow(FWindow);
Expand All @@ -278,7 +313,7 @@ function TGOEngine.SaveSettings: Boolean;
inherited Destroy;
end;

procedure TGOEngine.DoEvents;
procedure TGOEngine.DoEvents;
begin
if SDL_PollEvent(FEvent)=1 then begin
case FEvent^.type_ of
Expand All @@ -305,6 +340,12 @@ function TGOEngine.SaveSettings: Boolean;
procedure TGOEngine.GameLogic;
begin
//тут буде прораховуватись ігрова логіка
if fc=7 then begin
fc:=0;
Inc(fr);
if fr>9 then fr:=0;
end else inc(fc);
SDL_Delay(75);
end;

procedure TGOEngine.Draw;
Expand All @@ -314,6 +355,9 @@ function TGOEngine.SaveSettings: Boolean;
//очистити вікно
SDL_RenderClear(FRenderer);

//temporary draw
Texturemanager.DrawFrame('Tux',0,0,32,32,fr,fc);

//показати вікно на екран
SDL_RenderPresent(FRenderer);
end;
Expand Down

0 comments on commit 2af737d

Please sign in to comment.