From 584fc662c1564b8245c5c1aa934ebd5cf35499f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ruben=20Mu=CC=88ller?= Date: Sun, 21 Aug 2011 18:15:55 +0200 Subject: [PATCH] shadow map macos fix --- src/Events.pbi | 13 ++-- src/Functions.pbi | 194 +--------------------------------------------- 2 files changed, 6 insertions(+), 201 deletions(-) diff --git a/src/Events.pbi b/src/Events.pbi index c64b178..681baf3 100644 --- a/src/Events.pbi +++ b/src/Events.pbi @@ -46,6 +46,11 @@ Procedure HandleEventRunningGame() *ActiveMenu.GameMenu = Menus(CurrentState) EndIf + CompilerSelect #PB_Compiler_OS + CompilerCase #PB_OS_Linux + GenerateShadowLayer() + CompilerEndSelect + ; Player movement If KeyboardReleased(#PB_Key_Left) And Player\X > 0 If TileIsType(#TILESET_TYPE_FLOOR, Player\X-1, Player\Y) Or TileIsType(#TILESET_TYPE_GRASS, Player\X-1, Player\Y) @@ -56,8 +61,6 @@ Procedure HandleEventRunningGame() If Player\X < (Cam\X + Cam\Padding) And Cam\X > 0 Cam\X - 1 EndIf - - GenerateShadowLayer() EndIf If KeyboardReleased(#PB_Key_Right) And Player\X < #MAP_WIDTH-1 @@ -69,8 +72,6 @@ Procedure HandleEventRunningGame() If Player\X > (Cam\X + Cam\Width - Cam\Padding) And (Cam\X + Cam\Width) < #MAP_WIDTH-1 Cam\X + 1 EndIf - - GenerateShadowLayer() EndIf If KeyboardReleased(#PB_Key_Up) And Player\Y > 0 @@ -82,8 +83,6 @@ Procedure HandleEventRunningGame() If Player\Y< (Cam\Y + Cam\Padding) And Cam\Y > 0 Cam\Y - 1 EndIf - - GenerateShadowLayer() EndIf If KeyboardReleased(#PB_Key_Down) And Player\Y < #MAP_HEIGHT-1 @@ -95,8 +94,6 @@ Procedure HandleEventRunningGame() If Player\Y > (Cam\Y + Cam\Height - Cam\Padding) And (Cam\Y + Cam\Height) < #MAP_HEIGHT-1 Cam\Y + 1 EndIf - - GenerateShadowLayer() EndIf EndProcedure ; IDE Options = PureBasic 4.51 (Linux - x64) diff --git a/src/Functions.pbi b/src/Functions.pbi index 5a5fce7..990c109 100644 --- a/src/Functions.pbi +++ b/src/Functions.pbi @@ -1,193 +1 @@ -; This file holds all the functions If it gets too big, -; we can split it in categories - -; ***************************************************************************** -; * Tileset -Procedure InitGraphics() - GFXLogo = LoadSprite(#PB_Any, "../data/gfx/logo.bmp") - GFXTileset = LoadSprite(#PB_Any, "../data/gfx/tileset01.bmp") -EndProcedure - -; TODO: Make this dynamic -Procedure InitTileset() - Define i.i, j.i, count.i - count = 0 - For i=0 To 5 - For j=0 To 5 - - Tileset(count)\ID = CopySprite(GFXTileset, #PB_Any) - ClipSprite(Tileset(count)\ID, #TILE_SIZE*j, #TILE_SIZE*i, #TILE_SIZE, #TILE_SIZE) - - Tileset(count)\Type = #TILESET_TYPE_FLOOR - Select count - Case 0 - Tileset(count)\Type = #TILESET_TYPE_PLAYER - Case 2 - Tileset(count)\Type = #TILESET_TYPE_WALL - Case 3 - Tileset(count)\Type = #TILESET_TYPE_SOLID - Case 4 - Tileset(count)\Type = #TILESET_TYPE_GRASS - EndSelect - - count+1 - Next - Next -EndProcedure - -Procedure LoadMap(Filename.s) - ; Extract the map data from the XML file - If LoadXML(0, Filename) And XMLStatus(0) = #PB_XML_Success - Define *Node.i = XMLNodeFromPath(MainXMLNode(0), "/map/layer/data") - Define MapData.s = GetXMLNodeText(*Node) - If CreateRegularExpression(0, "\d{1,2},?") - Dim Tiles.s(0) - Define NumFound.i = ExtractRegularExpression(0, MapData, Tiles()) - Define i.i, X.i, Y.i - X = 0 - Y = 0 - - CreateFile(0, "mapcheck.txt") ; File stuff for checking the map loader - For i=0 To NumFound-1 - TileMap(X, Y)\TileNumber = Val(RTrim(Tiles(i), ","))-1 - TileMap(X, Y)\X = X - TileMap(X, Y)\Y = Y - - WriteString(0, StrU(TileMap(X, Y)\TileNumber+1)+",") - - If X = #MAP_WIDTH-1 - X = 0 - Y+1 - WriteStringN(0, "") ; do new line - Else - X+1 - EndIf - Next - CloseFile(0) - EndIf - EndIf -EndProcedure - -; Shadowmap -Procedure CalculateShadow() - Define X.i, Y.i - Define MapX.i, MapY.i, Strength.i, Centercheck.i, CentercheckX.i, CentercheckY.i - - For X.i=-#SHADOW_RADIUS To #SHADOW_RADIUS*2 - For Y.i=-#SHADOW_RADIUS To #SHADOW_RADIUS*2 - MapX = Player\X + X - MapY = Player\Y + Y - - CentercheckX = X - If CentercheckX < 0 - CentercheckX * -1 - EndIf - - CentercheckY = Y - If CentercheckY < 0 - CentercheckY * -1 - EndIf - - If CentercheckX > CentercheckY - Centercheck = CentercheckX - Else - Centercheck = CentercheckY - EndIf - Centercheck = #SHADOW_RADIUS - Centercheck; - - Strength = (255 / #SHADOW_RADIUS) * (Centercheck) - - If MapX >= 0 And MapY >= 0 - If ShadowMap(MapX, MapY)\Strength < Strength - ShadowMap(MapX, MapY)\Strength = Strength - EndIf - EndIf - Next - Next -EndProcedure - -Procedure DrawShadowtile(strength.i, x.i, y.i) - Box(#TILE_SIZE*x, #TILE_SIZE*y, #TILE_SIZE, #TILE_SIZE, RGBA(Random(0), Random(0), Random(0), 255-strength)) -EndProcedure - -Procedure GenerateShadowLayer() - Define X.i ,Y.i - - ShadowOfDarkness = CreateImage(#PB_Any, 800, 600, 32|#PB_Image_Transparent) - StartDrawing(ImageOutput(ShadowOfDarkness)) - DrawingMode(#PB_2DDrawing_AlphaBlend) - For X.i=0 To Cam\Width - For Y.i=0 To Cam\Height - DrawShadowtile(ShadowMap(X+Cam\X, Y+Cam\Y)\Strength, X, Y) - Next - Next - StopDrawing() -EndProcedure - -Procedure DrawShadow() - StartDrawing(ScreenOutput()) - DrawImage(ImageID(ShadowOfDarkness), 0, 0) - StopDrawing() -EndProcedure - -; Map itself -Procedure DrawTile(number.i, x.i, y.i) - DisplaySprite(Tileset(number)\ID, #TILE_SIZE*x, #TILE_SIZE*y) -EndProcedure - -Procedure DrawMap() - Define X.i ,Y.i - - For X.i=0 To Cam\Width - For Y.i=0 To Cam\Height - DrawTile(TileMap(X+Cam\X, Y+Cam\Y)\TileNumber, X, Y) - Next - Next -EndProcedure - -Procedure TileIsType(type.i, X.i, Y.i) - Define istype.b - - istype = #False - - If Tileset(TileMap(X, Y)\TileNumber)\Type = type - istype = #True - EndIf - - ProcedureReturn istype -EndProcedure - -; The player -Procedure DrawPlayer() - DrawTile(Player\TileNumber, Player\X-Cam\X, Player\Y-Cam\Y) -EndProcedure - -; We need a HUD, hell yeah! -Procedure DrawHUD() -EndProcedure - -; and .. - - -; ***************************************************************************** -; * Menu - -Procedure Menu_GotoCurrent() - If *ActiveMenu\Entries()\Selected <> #True - FirstElement(*ActiveMenu\Entries()) - - ForEach *ActiveMenu\Entries() - If *ActiveMenu\Entries()\Selected = #True - Break - EndIf - Next - EndIf -EndProcedure - - -; IDE Options = PureBasic 4.51 (Linux - x64) -; CursorPosition = 116 -; Folding = --- -; EnableXP -; EnableCompileCount = 0 -; EnableBuildCount = 0 +; This file holds all the functions If it gets too big, ; we can split it in categories ; ***************************************************************************** ; * Tileset Procedure InitGraphics() GFXLogo = LoadSprite(#PB_Any, "../data/gfx/logo.bmp") GFXTileset = LoadSprite(#PB_Any, "../data/gfx/tileset01.bmp") EndProcedure ; TODO: Make this dynamic Procedure InitTileset() Define i.i, j.i, count.i count = 0 For i=0 To 5 For j=0 To 5 Tileset(count)\ID = CopySprite(GFXTileset, #PB_Any) ClipSprite(Tileset(count)\ID, #TILE_SIZE*j, #TILE_SIZE*i, #TILE_SIZE, #TILE_SIZE) Tileset(count)\Type = #TILESET_TYPE_FLOOR Select count Case 0 Tileset(count)\Type = #TILESET_TYPE_PLAYER Case 2 Tileset(count)\Type = #TILESET_TYPE_WALL Case 3 Tileset(count)\Type = #TILESET_TYPE_SOLID Case 4 Tileset(count)\Type = #TILESET_TYPE_GRASS EndSelect count+1 Next Next EndProcedure Procedure LoadMap(Filename.s) ; Extract the map data from the XML file If LoadXML(0, Filename) And XMLStatus(0) = #PB_XML_Success Define *Node.i = XMLNodeFromPath(MainXMLNode(0), "/map/layer/data") Define MapData.s = GetXMLNodeText(*Node) If CreateRegularExpression(0, "\d{1,2},?") Dim Tiles.s(0) Define NumFound.i = ExtractRegularExpression(0, MapData, Tiles()) Define i.i, X.i, Y.i X = 0 Y = 0 CreateFile(0, "mapcheck.txt") ; File stuff for checking the map loader For i=0 To NumFound-1 TileMap(X, Y)\TileNumber = Val(RTrim(Tiles(i), ","))-1 TileMap(X, Y)\X = X TileMap(X, Y)\Y = Y WriteString(0, StrU(TileMap(X, Y)\TileNumber+1)+",") If X = #MAP_WIDTH-1 X = 0 Y+1 WriteStringN(0, "") ; do new line Else X+1 EndIf Next CloseFile(0) EndIf EndIf EndProcedure ; Shadowmap Procedure CalculateShadow() Define X.i, Y.i Define MapX.i, MapY.i, Strength.i, Centercheck.i, CentercheckX.i, CentercheckY.i For X.i=-#SHADOW_RADIUS To #SHADOW_RADIUS*2 For Y.i=-#SHADOW_RADIUS To #SHADOW_RADIUS*2 MapX = Player\X + X MapY = Player\Y + Y CentercheckX = X If CentercheckX < 0 CentercheckX * -1 EndIf CentercheckY = Y If CentercheckY < 0 CentercheckY * -1 EndIf If CentercheckX > CentercheckY Centercheck = CentercheckX Else Centercheck = CentercheckY EndIf Centercheck = #SHADOW_RADIUS - Centercheck; Strength = (255 / #SHADOW_RADIUS) * (Centercheck) If MapX >= 0 And MapY >= 0 If ShadowMap(MapX, MapY)\Strength < Strength ShadowMap(MapX, MapY)\Strength = Strength EndIf EndIf Next Next EndProcedure Procedure DrawShadowtile(strength.i, x.i, y.i) Box(#TILE_SIZE*x, #TILE_SIZE*y, #TILE_SIZE, #TILE_SIZE, RGBA(0, 0, 0, 255-strength)) EndProcedure Procedure GenerateShadowLayer() Define X.i ,Y.i CompilerSelect #PB_Compiler_OS CompilerCase #PB_OS_Linux ShadowOfDarkness = CreateImage(#PB_Any, 800, 600, 32|#PB_Image_Transparent) StartDrawing(ImageOutput(ShadowOfDarkness)) CompilerDefault StartDrawing(ScreenOutput()) CompilerEndSelect DrawingMode(#PB_2DDrawing_AlphaBlend) For X.i=0 To Cam\Width For Y.i=0 To Cam\Height DrawShadowtile(ShadowMap(X+Cam\X, Y+Cam\Y)\Strength, X, Y) Next Next StopDrawing() EndProcedure Procedure DrawShadow() CompilerSelect #PB_Compiler_OS CompilerCase #PB_OS_Linux StartDrawing(ScreenOutput()) DrawImage(ImageID(ShadowOfDarkness), 0, 0) StopDrawing() CompilerDefault GenerateShadowLayer() CompilerEndSelect EndProcedure ; Map itself Procedure DrawTile(number.i, x.i, y.i) DisplaySprite(Tileset(number)\ID, #TILE_SIZE*x, #TILE_SIZE*y) EndProcedure Procedure DrawMap() Define X.i ,Y.i For X.i=0 To Cam\Width For Y.i=0 To Cam\Height DrawTile(TileMap(X+Cam\X, Y+Cam\Y)\TileNumber, X, Y) Next Next EndProcedure Procedure TileIsType(type.i, X.i, Y.i) Define istype.b istype = #False If Tileset(TileMap(X, Y)\TileNumber)\Type = type istype = #True EndIf ProcedureReturn istype EndProcedure ; The player Procedure DrawPlayer() DrawTile(Player\TileNumber, Player\X-Cam\X, Player\Y-Cam\Y) EndProcedure ; We need a HUD, hell yeah! Procedure DrawHUD() EndProcedure ; and .. ; ***************************************************************************** ; * Menu Procedure Menu_GotoCurrent() If *ActiveMenu\Entries()\Selected <> #True FirstElement(*ActiveMenu\Entries()) ForEach *ActiveMenu\Entries() If *ActiveMenu\Entries()\Selected = #True Break EndIf Next EndIf EndProcedure ; IDE Options = PureBasic 4.51 (Linux - x64) ; CursorPosition = 116 ; Folding = --- ; EnableXP ; EnableCompileCount = 0 ; EnableBuildCount = 0 \ No newline at end of file