From a529d490d753242979843e1cdfa1816624415d68 Mon Sep 17 00:00:00 2001 From: sechshelme Date: Sun, 29 Jan 2023 18:03:54 +0100 Subject: [PATCH] crash --- .../24_-_Calculating_Frame_Rate/Project1.pas | 2 +- lazyfoo.net/26_-_Motion/Project1.pas | 6 +- lazyfoo.net/26_-_Motion/dot.pas | 5 +- .../27_-_Collision_Detection/Project1.lpi | 76 +++++++++ .../27_-_Collision_Detection/Project1.pas | 145 +++++++++++++++++ lazyfoo.net/27_-_Collision_Detection/dot.bmp | Bin 0 -> 1254 bytes lazyfoo.net/27_-_Collision_Detection/dot.pas | 145 +++++++++++++++++ .../27_-_Collision_Detection_2/Project1.lpi | 76 +++++++++ .../27_-_Collision_Detection_2/Project1.pas | 147 +++++++++++++++++ .../27_-_Collision_Detection_2/dot.bmp | Bin 0 -> 1254 bytes .../27_-_Collision_Detection_2/dot.pas | 152 ++++++++++++++++++ lazyfoo.net/units/ltexture.pas | 8 +- readme.md | 1 + 13 files changed, 751 insertions(+), 12 deletions(-) create mode 100644 lazyfoo.net/27_-_Collision_Detection/Project1.lpi create mode 100644 lazyfoo.net/27_-_Collision_Detection/Project1.pas create mode 100755 lazyfoo.net/27_-_Collision_Detection/dot.bmp create mode 100644 lazyfoo.net/27_-_Collision_Detection/dot.pas create mode 100644 lazyfoo.net/27_-_Collision_Detection_2/Project1.lpi create mode 100644 lazyfoo.net/27_-_Collision_Detection_2/Project1.pas create mode 100755 lazyfoo.net/27_-_Collision_Detection_2/dot.bmp create mode 100644 lazyfoo.net/27_-_Collision_Detection_2/dot.pas diff --git a/lazyfoo.net/24_-_Calculating_Frame_Rate/Project1.pas b/lazyfoo.net/24_-_Calculating_Frame_Rate/Project1.pas index 87be786..2b99b0f 100644 --- a/lazyfoo.net/24_-_Calculating_Frame_Rate/Project1.pas +++ b/lazyfoo.net/24_-_Calculating_Frame_Rate/Project1.pas @@ -134,7 +134,7 @@ end; timeText := ''; - WriteStr(timeText, 'Average Frames Per Second ', avgFPS:10:2); + WriteStr(timeText, 'Average Frames Per Second ', avgFPS: 10: 2); if not gFPSTextTexture.LoadFromRenderedText(gFont, timeText, textColor) then begin WriteLn('Unable to render FPS texture !'); end; diff --git a/lazyfoo.net/26_-_Motion/Project1.pas b/lazyfoo.net/26_-_Motion/Project1.pas index d49f7c8..d1aa81f 100644 --- a/lazyfoo.net/26_-_Motion/Project1.pas +++ b/lazyfoo.net/26_-_Motion/Project1.pas @@ -27,8 +27,6 @@ capTimer, fpsTimer: TLTimer; myDot: Tdot; - timeText: string; - avgFPS: single; frameTicks: uint32; function init: boolean; @@ -73,7 +71,7 @@ var sucess: boolean = True; begin - gDotTexture.LoadFromFile('dot.bmp'); + gDotTexture.LoadFromFile('dot.bmp', $FF, $FF, $FF); if gDotTexture = nil then begin WriteLn('Failed to load dot texture! SDL_ttf Error: '); sucess := False; @@ -125,7 +123,7 @@ myDot.move; - SDL_SetRenderDrawColor(gRenderer, $FF, $FF, $FF, $FF); + SDL_SetRenderDrawColor(gRenderer, $00, $9F, $00, $FF); SDL_RenderClear(gRenderer); myDot.render(gDotTexture); diff --git a/lazyfoo.net/26_-_Motion/dot.pas b/lazyfoo.net/26_-_Motion/dot.pas index 4658405..84d2846 100644 --- a/lazyfoo.net/26_-_Motion/dot.pas +++ b/lazyfoo.net/26_-_Motion/dot.pas @@ -13,14 +13,13 @@ interface Tdot = class(TObject) private widht, Height, mPosX, mPosY, mVelX, mVelY: integer; - public const DOT_WIDTH = 20; DOT_HEIGHT = 20; DOT_VEL = 10; constructor Create(Awidht, Aheigth: integer); - procedure HandleEvent(e: TSDL_Event); + procedure HandleEvent(var e: TSDL_Event); procedure move; procedure render(tex: TLTexture); end; @@ -39,7 +38,7 @@ constructor Tdot.Create(Awidht, Aheigth: integer); Height := Aheigth; end; -procedure Tdot.HandleEvent(e: TSDL_Event); +procedure Tdot.HandleEvent(var e: TSDL_Event); begin case e.type_ of SDL_KEYDOWN: begin diff --git a/lazyfoo.net/27_-_Collision_Detection/Project1.lpi b/lazyfoo.net/27_-_Collision_Detection/Project1.lpi new file mode 100644 index 0000000..54821d8 --- /dev/null +++ b/lazyfoo.net/27_-_Collision_Detection/Project1.lpi @@ -0,0 +1,76 @@ + + + + + + + + + + + + + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <BuildModes> + <Item Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + <UseFileFilters Value="True"/> + </PublishOptions> + <RunParams> + <FormatVersion Value="2"/> + </RunParams> + <RequiredPackages> + <Item> + <PackageName Value="SDL2_Package"/> + </Item> + </RequiredPackages> + <Units> + <Unit> + <Filename Value="Project1.pas"/> + <IsPartOfProject Value="True"/> + </Unit> + <Unit> + <Filename Value="ltexture.pas"/> + <IsPartOfProject Value="True"/> + <UnitName Value="LTexture"/> + </Unit> + <Unit> + <Filename Value="dot.pas"/> + <IsPartOfProject Value="True"/> + </Unit> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <Target> + <Filename Value="Project1"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <OtherUnitFiles Value="../units"/> + <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> + </SearchPaths> + <Linking> + <Debugging> + <DebugInfoType Value="dsDwarf3"/> + </Debugging> + </Linking> + </CompilerOptions> + <Debugging> + <Exceptions> + <Item> + <Name Value="EAbort"/> + </Item> + <Item> + <Name Value="ECodetoolError"/> + </Item> + <Item> + <Name Value="EFOpenError"/> + </Item> + </Exceptions> + </Debugging> +</CONFIG> diff --git a/lazyfoo.net/27_-_Collision_Detection/Project1.pas b/lazyfoo.net/27_-_Collision_Detection/Project1.pas new file mode 100644 index 0000000..f383dfa --- /dev/null +++ b/lazyfoo.net/27_-_Collision_Detection/Project1.pas @@ -0,0 +1,145 @@ +program Project1; + +// https://github.com/PascalGameDevelopment/SDL2-for-Pascal + +uses + sdl2, + sdl2_image, + ctypes, + LTexture, + LTimer, + dot; + +const + Screen_Widht = 640; + Screen_Height = 480; + Screen_FPS = 60; + Screen_Tick_Per_Frame = 1000 div Screen_FPS; + +var + gWindow: PSDL_Window; + gRenderer: PSDL_Renderer; + + quit: boolean = False; + e: TSDL_Event; + + gDotTexture: TLTexture; + capTimer, fpsTimer: TLTimer; + myDot: Tdot; + + frameTicks: uint32; + wall:TSDL_Rect = (x: 300; y: 40; w: 40; h: 400); + + function init: boolean; + var + sucess: boolean = True; + imgFlags: cint32 = 0; + begin + sucess := True; + if SDL_Init(SDL_INIT_VIDEO) < 0 then begin + WriteLn('SDL could not initialize! SDL_Error: ', SDL_GetError); + sucess := False; + end else begin + gWindow := SDL_CreateWindow('SDL Tuorial', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Screen_Widht, Screen_Height, SDL_WINDOW_SHOWN); + if gWindow = nil then begin + WriteLn('Window could not be created! SDL_Error: ', SDL_GetError); + sucess := False; + end else begin + gRenderer := SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); + if gRenderer = nil then begin + WriteLn('Renderer could not be created! SDL Error: ', SDL_GetError); + sucess := False; + end else begin + SDL_SetRenderDrawColor(gRenderer, $FF, $FF, $FF, $FF); + + if (IMG_Init(imgFlags) and imgFlags) <> 0 then begin + WriteLn('SDL_image could not initialize! SDL_image Error: ', IMG_GetError); + sucess := False; + end; + + end; + end; + end; + Result := sucess; + + gDotTexture := TLTexture.Create(gRenderer); + fpsTimer := TLTimer.Create; + capTimer := TLTimer.Create; + myDot := Tdot.Create(Screen_Widht, Screen_Height); + end; + + function loadMedia: boolean; + var + sucess: boolean = True; + begin + gDotTexture.LoadFromFile('dot.bmp',$FF,$FF,$FF); + if gDotTexture = nil then begin + WriteLn('Failed to load dot texture! SDL_ttf Error: '); + sucess := False; + end; + + Result := sucess; + end; + + procedure Close; + begin + gDotTexture.Free; + fpsTimer.Free; + capTimer.Free; + myDot.Free; + + SDL_DestroyRenderer(gRenderer); + SDL_DestroyWindow(gWindow); + gWindow := nil; + gRenderer := nil; + + IMG_Quit; + SDL_Quit; + end; + +begin + if not init then begin + WriteLn('Failed to initialize'); + end else begin + if not loadMedia then begin + WriteLn('Failed to load media'); + end else begin + fpsTimer.start; + while not quit do begin + while SDL_PollEvent(@e) <> 0 do begin + case e.type_ of + SDL_KEYDOWN: begin + case e.key.keysym.sym of + SDLK_ESCAPE: begin + quit := True; + end; + end; + end; + SDL_QUITEV: begin + quit := True; + end; + end; + myDot.HandleEvent(e); + end; + + myDot.move(wall); + + SDL_SetRenderDrawColor(gRenderer, $00, $80, $00, $FF); + SDL_RenderClear(gRenderer); + + SDL_SetRenderDrawColor(gRenderer, $80, $40, $00, $FF); + SDL_RenderFillRect(gRenderer, @wall); + + myDot.render(gDotTexture); + + SDL_RenderPresent(gRenderer); + + frameTicks := capTimer.getTicks; + if frameTicks < Screen_Tick_Per_Frame then begin + SDL_Delay(Screen_Tick_Per_Frame - frameTicks); + end; + end; + end; + end; + Close; +end. diff --git a/lazyfoo.net/27_-_Collision_Detection/dot.bmp b/lazyfoo.net/27_-_Collision_Detection/dot.bmp new file mode 100755 index 0000000000000000000000000000000000000000..bd0e7e23f7a0eee559468592e93293784e262eff GIT binary patch literal 1254 zcmZ?rea6B724+A~1BgYSn2|vOEWQCKe}oT$!2&?yKQ;i9M9|n&p^G6@V`HJKhVikf z#V!WZ0j04k!Yzud9k(j%GH|`vr3r~bv=dT=T^^_xyTqs{wL^gHpr$Z>$<q$eMY&#} X3B(5^vZcsu{3al)#Kp!m0jo*?PBZ^? literal 0 HcmV?d00001 diff --git a/lazyfoo.net/27_-_Collision_Detection/dot.pas b/lazyfoo.net/27_-_Collision_Detection/dot.pas new file mode 100644 index 0000000..8993cbd --- /dev/null +++ b/lazyfoo.net/27_-_Collision_Detection/dot.pas @@ -0,0 +1,145 @@ +unit dot; + +interface + +uses + sdl2, + sdl2_image, LTexture; + +type + + { Tdot } + + Tdot = class(TObject) + private + widht, Height, mPosX, mPosY, mVelX, mVelY: integer; + mCollider: TSDL_Rect; + public + const + DOT_WIDTH = 20; + DOT_HEIGHT = 20; + DOT_VEL = 10; + constructor Create(Awidht, Aheigth: integer); + procedure HandleEvent(var e: TSDL_Event); + procedure move(wall: TSDL_Rect); + procedure render(tex: TLTexture); + end; + +function checkCollision(var a, b: TSDL_Rect): boolean; + +implementation + +function checkCollision(var a, b: TSDL_Rect): boolean; +var + leftA, leftB, rightA, rightB, topA, topB, bottomA, bottomB: integer; +begin + leftA := A.x; + rightA := A.x + A.w; + topA := A.y; + bottomA := A.y + A.h; + + leftB := B.x; + rightB := B.x + B.w; + topB := B.y; + bottomB := B.y + B.h; + + Result := True; + if bottomA <= topB then begin + Result := False; + end; + if topA >= bottomB then begin + Result := False; + end; + if rightA <= leftB then begin + Result := False; + end; + if leftA >= rightB then begin + Result := False; + end; +end; + +{ Tdot } + +constructor Tdot.Create(Awidht, Aheigth: integer); +begin + mPosX := 0; + mPosY := 0; + mVelX := 0; + mVelY := 0; + widht := Awidht; + Height := Aheigth; + mCollider.w := DOT_WIDTH; + mCollider.h := DOT_HEIGHT; +end; + +procedure Tdot.HandleEvent(var e: TSDL_Event); +begin + case e.type_ of + SDL_KEYDOWN: begin + if e.key.repeat_ = 0 then begin + case e.key.keysym.sym of + SDLK_UP: begin + Dec(mVelY, DOT_VEL); + end; + SDLK_DOWN: begin + Inc(mVelY, DOT_VEL); + end; + SDLK_LEFT: begin + Dec(mVelX, DOT_VEL); + end; + SDLK_RIGHT: begin + Inc(mVelX, DOT_VEL); + end; + end; + end; + end; + SDL_KEYUP: begin + if e.key.repeat_ = 0 then begin + case e.key.keysym.sym of + SDLK_UP: begin + Inc(mVelY, DOT_VEL); + end; + SDLK_DOWN: begin + Dec(mVelY, DOT_VEL); + end; + SDLK_LEFT: begin + Inc(mVelX, DOT_VEL); + end; + SDLK_RIGHT: begin + Dec(mVelX, DOT_VEL); + end; + end; + end; + end; + end; +end; + +procedure Tdot.move(wall: TSDL_Rect); +var + oldX, oldY: integer; +begin + oldX := mPosX; + oldY := mPosY; + Inc(mPosX, mVelX); + mCollider.x := mPosX; + if (mPosX < 0) or (mPosX + DOT_WIDTH > widht) or checkCollision(mCollider, wall) then begin + mPosX := oldX; + // Dec(mPosX, mVelX); + mCollider.x := mPosX; + end; + + Inc(mPosY, mVelY); + mCollider.y := mPosY; + if (mPosY < 0) or (mPosY + DOT_HEIGHT > Height) or checkCollision(mCollider, wall) then begin + mPosY := oldY; + // Dec(mPosY, mVelY); + mCollider.y := mPosY; + end; +end; + +procedure Tdot.render(tex: TLTexture); +begin + tex.Render(mPosX, mPosY); +end; + +end. diff --git a/lazyfoo.net/27_-_Collision_Detection_2/Project1.lpi b/lazyfoo.net/27_-_Collision_Detection_2/Project1.lpi new file mode 100644 index 0000000..54821d8 --- /dev/null +++ b/lazyfoo.net/27_-_Collision_Detection_2/Project1.lpi @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="UTF-8"?> +<CONFIG> + <ProjectOptions> + <Version Value="12"/> + <General> + <Flags> + <MainUnitHasCreateFormStatements Value="False"/> + <MainUnitHasTitleStatement Value="False"/> + <MainUnitHasScaledStatement Value="False"/> + </Flags> + <SessionStorage Value="InProjectDir"/> + <Title Value="Project1"/> + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <BuildModes> + <Item Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + <UseFileFilters Value="True"/> + </PublishOptions> + <RunParams> + <FormatVersion Value="2"/> + </RunParams> + <RequiredPackages> + <Item> + <PackageName Value="SDL2_Package"/> + </Item> + </RequiredPackages> + <Units> + <Unit> + <Filename Value="Project1.pas"/> + <IsPartOfProject Value="True"/> + </Unit> + <Unit> + <Filename Value="ltexture.pas"/> + <IsPartOfProject Value="True"/> + <UnitName Value="LTexture"/> + </Unit> + <Unit> + <Filename Value="dot.pas"/> + <IsPartOfProject Value="True"/> + </Unit> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <Target> + <Filename Value="Project1"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <OtherUnitFiles Value="../units"/> + <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> + </SearchPaths> + <Linking> + <Debugging> + <DebugInfoType Value="dsDwarf3"/> + </Debugging> + </Linking> + </CompilerOptions> + <Debugging> + <Exceptions> + <Item> + <Name Value="EAbort"/> + </Item> + <Item> + <Name Value="ECodetoolError"/> + </Item> + <Item> + <Name Value="EFOpenError"/> + </Item> + </Exceptions> + </Debugging> +</CONFIG> diff --git a/lazyfoo.net/27_-_Collision_Detection_2/Project1.pas b/lazyfoo.net/27_-_Collision_Detection_2/Project1.pas new file mode 100644 index 0000000..d46e3bc --- /dev/null +++ b/lazyfoo.net/27_-_Collision_Detection_2/Project1.pas @@ -0,0 +1,147 @@ +program Project1; + +// https://github.com/PascalGameDevelopment/SDL2-for-Pascal + +uses + sdl2, + sdl2_image, + ctypes, + LTexture, + LTimer, + dot; + +const + Screen_Widht = 640; + Screen_Height = 480; + Screen_FPS = 60; + Screen_Tick_Per_Frame = 1000 div Screen_FPS; + +var + gWindow: PSDL_Window; + gRenderer: PSDL_Renderer; + + quit: boolean = False; + e: TSDL_Event; + + gDotTexture: TLTexture; + capTimer, fpsTimer: TLTimer; + myDot: Tdot; + + frameTicks: uint32; + wall:array [0..2]of TSDL_Rect = ((x: 300; y: 40; w: 40; h: 400),(x: 150; y: 40; w: 40; h: 400),(x: 450; y: 40; w: 40; h: 400)); + i: Integer; + + function init: boolean; + var + sucess: boolean = True; + imgFlags: cint32 = 0; + begin + sucess := True; + if SDL_Init(SDL_INIT_VIDEO) < 0 then begin + WriteLn('SDL could not initialize! SDL_Error: ', SDL_GetError); + sucess := False; + end else begin + gWindow := SDL_CreateWindow('SDL Tuorial', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Screen_Widht, Screen_Height, SDL_WINDOW_SHOWN); + if gWindow = nil then begin + WriteLn('Window could not be created! SDL_Error: ', SDL_GetError); + sucess := False; + end else begin + gRenderer := SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); + if gRenderer = nil then begin + WriteLn('Renderer could not be created! SDL Error: ', SDL_GetError); + sucess := False; + end else begin + SDL_SetRenderDrawColor(gRenderer, $FF, $FF, $FF, $FF); + + if (IMG_Init(imgFlags) and imgFlags) <> 0 then begin + WriteLn('SDL_image could not initialize! SDL_image Error: ', IMG_GetError); + sucess := False; + end; + + end; + end; + end; + Result := sucess; + + gDotTexture := TLTexture.Create(gRenderer); + fpsTimer := TLTimer.Create; + capTimer := TLTimer.Create; + myDot := Tdot.Create(Screen_Widht, Screen_Height); + end; + + function loadMedia: boolean; + var + sucess: boolean = True; + begin + gDotTexture.LoadFromFile('dot.bmp',$FF,$FF,$FF); + if gDotTexture = nil then begin + WriteLn('Failed to load dot texture! SDL_ttf Error: '); + sucess := False; + end; + + Result := sucess; + end; + + procedure Close; + begin + gDotTexture.Free; + fpsTimer.Free; + capTimer.Free; + myDot.Free; + + SDL_DestroyRenderer(gRenderer); + SDL_DestroyWindow(gWindow); + gWindow := nil; + gRenderer := nil; + + IMG_Quit; + SDL_Quit; + end; + +begin + if not init then begin + WriteLn('Failed to initialize'); + end else begin + if not loadMedia then begin + WriteLn('Failed to load media'); + end else begin + fpsTimer.start; + while not quit do begin + while SDL_PollEvent(@e) <> 0 do begin + case e.type_ of + SDL_KEYDOWN: begin + case e.key.keysym.sym of + SDLK_ESCAPE: begin + quit := True; + end; + end; + end; + SDL_QUITEV: begin + quit := True; + end; + end; + myDot.HandleEvent(e); + end; + + myDot.move(wall); + + SDL_SetRenderDrawColor(gRenderer, $00, $80, $00, $FF); + SDL_RenderClear(gRenderer); + + SDL_SetRenderDrawColor(gRenderer, $80, $40, $00, $FF); + + for i:=0 to Length(wall)-1 do SDL_RenderFillRect(gRenderer, @wall[i]); + + myDot.render(gDotTexture); + + SDL_RenderPresent(gRenderer); + + frameTicks := capTimer.getTicks; + if frameTicks < Screen_Tick_Per_Frame then begin + SDL_Delay(Screen_Tick_Per_Frame - frameTicks); + end; + end; + end; + end; + Close; +end. diff --git a/lazyfoo.net/27_-_Collision_Detection_2/dot.bmp b/lazyfoo.net/27_-_Collision_Detection_2/dot.bmp new file mode 100755 index 0000000000000000000000000000000000000000..bd0e7e23f7a0eee559468592e93293784e262eff GIT binary patch literal 1254 zcmZ?rea6B724+A~1BgYSn2|vOEWQCKe}oT$!2&?yKQ;i9M9|n&p^G6@V`HJKhVikf z#V!WZ0j04k!Yzud9k(j%GH|`vr3r~bv=dT=T^^_xyTqs{wL^gHpr$Z>$<q$eMY&#} X3B(5^vZcsu{3al)#Kp!m0jo*?PBZ^? literal 0 HcmV?d00001 diff --git a/lazyfoo.net/27_-_Collision_Detection_2/dot.pas b/lazyfoo.net/27_-_Collision_Detection_2/dot.pas new file mode 100644 index 0000000..72c6681 --- /dev/null +++ b/lazyfoo.net/27_-_Collision_Detection_2/dot.pas @@ -0,0 +1,152 @@ +unit dot; + +interface + +uses + sdl2, + sdl2_image, LTexture; + +type + + { Tdot } + + Tdot = class(TObject) + private + widht, Height, mVelX, mVelY: integer; + mPos: TSDL_Rect; + public + const + DOT_WIDTH = 20; + DOT_HEIGHT = 20; + DOT_VEL = 10; + constructor Create(Awidht, Aheigth: integer); + procedure HandleEvent(var e: TSDL_Event); + procedure move(wall: array of TSDL_Rect); + procedure render(tex: TLTexture); + end; + +function checkCollision(var a, b: TSDL_Rect): boolean; + +implementation + +function checkCollision(var a, b: TSDL_Rect): boolean; +var + leftA, leftB, rightA, rightB, topA, topB, bottomA, bottomB: integer; +begin + leftA := A.x; + rightA := A.x + A.w; + topA := A.y; + bottomA := A.y + A.h; + + leftB := B.x; + rightB := B.x + B.w; + topB := B.y; + bottomB := B.y + B.h; + + Result := True; + if bottomA <= topB then begin + Result := False; + end; + if topA >= bottomB then begin + Result := False; + end; + if rightA <= leftB then begin + Result := False; + end; + if leftA >= rightB then begin + Result := False; + end; +end; + +{ Tdot } + +constructor Tdot.Create(Awidht, Aheigth: integer); +begin + mPos.x := 0; + mPos.y := 0; + mVelX := 0; + mVelY := 0; + widht := Awidht; + Height := Aheigth; + mPos.w := DOT_WIDTH; + mPos.h := DOT_HEIGHT; +end; + +procedure Tdot.HandleEvent(var e: TSDL_Event); +begin + case e.type_ of + SDL_KEYDOWN: begin + if e.key.repeat_ = 0 then begin + case e.key.keysym.sym of + SDLK_UP: begin + Dec(mVelY, DOT_VEL); + end; + SDLK_DOWN: begin + Inc(mVelY, DOT_VEL); + end; + SDLK_LEFT: begin + Dec(mVelX, DOT_VEL); + end; + SDLK_RIGHT: begin + Inc(mVelX, DOT_VEL); + end; + end; + end; + end; + SDL_KEYUP: begin + if e.key.repeat_ = 0 then begin + case e.key.keysym.sym of + SDLK_UP: begin + Inc(mVelY, DOT_VEL); + end; + SDLK_DOWN: begin + Dec(mVelY, DOT_VEL); + end; + SDLK_LEFT: begin + Inc(mVelX, DOT_VEL); + end; + SDLK_RIGHT: begin + Dec(mVelX, DOT_VEL); + end; + end; + end; + end; + end; +end; + +procedure Tdot.move(wall: array of TSDL_Rect); +var + oldX, oldY, i: integer; +begin + oldX := mPos.x; + oldY := mPos.y; + + Inc(mPos.x, mVelX); + + if (mPos.x < 0) or (mPos.x + DOT_WIDTH > widht) then begin + mPos.x := oldX; + end; + for i := 0 to Length(wall) - 1 do begin + if checkCollision(mPos, wall[i]) then begin + mPos.x := oldX; + end; + end; + + Inc(mPos.y, mVelY); + if (mPos.y < 0) or (mPos.y + DOT_HEIGHT > Height) then begin + mPos.y := oldY; + end; + for i := 0 to Length(wall) - 1 do begin + if checkCollision(mPos, wall[i]) then begin + mPos.y := oldY; + end; + end; + +end; + +procedure Tdot.render(tex: TLTexture); +begin + tex.Render(mPos.x, mPos.y); +end; + +end. diff --git a/lazyfoo.net/units/ltexture.pas b/lazyfoo.net/units/ltexture.pas index 93db468..9efe288 100644 --- a/lazyfoo.net/units/ltexture.pas +++ b/lazyfoo.net/units/ltexture.pas @@ -23,7 +23,7 @@ TLTexture = class(TObject) property Height: integer read FHeight; constructor Create(ARenderer: PSDL_Renderer); destructor Destroy; override; - function LoadFromFile(path: string): boolean; + function LoadFromFile(path: string; ar: byte = $00; ag: byte = $FF; ab: byte = $FF): boolean; function LoadFromRenderedText(gFont: PTTF_Font; textureText: string; textColor: TSDL_Color): boolean; procedure FreeTexture; procedure SetColor(red, green, blue: byte); @@ -51,7 +51,7 @@ destructor TLTexture.Destroy; inherited Destroy; end; -function TLTexture.LoadFromFile(path: string): boolean; +function TLTexture.LoadFromFile(path: string; ar: byte; ag: byte; ab: byte): boolean; var loadedSurface: PSDL_Surface; newTexture: PSDL_Texture; @@ -60,8 +60,8 @@ function TLTexture.LoadFromFile(path: string): boolean; if loadedSurface = nil then begin WriteLn('Unable to load image ' + path + '! SDL_image Error: ', IMG_GetError()); end else begin - // SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface^.format, $00, $FF, $FF)); - SDL_SetColorKey(loadedSurface, 1, SDL_MapRGB(loadedSurface^.format, $00, $FF, $FF)); + // SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface^.format, $00, $FF, $FF)); + SDL_SetColorKey(loadedSurface, 1, SDL_MapRGB(loadedSurface^.format, ar, ag, ab)); newTexture := SDL_CreateTextureFromSurface(FRenderer, loadedSurface); if newTexture = nil then begin WriteLn('Unable to create texturefrom ', path, ' SDL Error: ', SDL_GetError); diff --git a/readme.md b/readme.md index 21e2704..933f7ab 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,6 @@ Folgende Packete müssen installiert sein: ``` sudo apt install libsdl2-dev +sudo apt install meson libfmt-dev libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-net-dev libsdl2-ttf-dev libyaml-cpp-dev zlib1g-dev ```