From 035b26d9dbf30d754d476dd6dfbe0faf06bd6a7b Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 23 Aug 2016 09:48:35 +0200 Subject: [PATCH 1/8] Cache cleaning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit premier test de nettoyage du cache mémoire pour éviter que l'application grignote de plus en plus de mémoire --- PBMap.pb | 215 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 150 insertions(+), 65 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 5795e5f..52ef232 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -26,7 +26,6 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos Global Verbose = #False - ;-Proxy ON/OFF Global Proxy = #False Declare InitPBMap() Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) @@ -102,7 +101,7 @@ Module PBMap Structure ImgMemCach nImage.i - Usage.i + Location.Location EndStructure Structure TileMemCach @@ -115,37 +114,43 @@ Module PBMap CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) EndStructure + Structure Option + WheelMouseRelative.i + EndStructure + ;-PBMap Structure Structure PBMap Gadget.i ; Canvas Gadget Id Font.i ; Font to uses when write on the map TargetLocation.Location ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point - ; + ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - ; + ; Position.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map - ; + ; ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/ ZoomMin.i ; Min Zoom supported by server ZoomMax.i ; Max Zoom supported by server Zoom.i ; Current zoom TileSize.i ; Tile size downloaded on the server ex : 256 - ; + ; HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache - ; - Moving.i + ; + Moving.i ; Dirty.i ; To signal that drawing need a refresh - ; - MainDrawingThread.i - List TilesThreads.TileThread() - ; + ; + MainDrawingThread.i ; + List TilesThreads.TileThread() ; + ; List track.Location() ; To display a GPX track List Marker.Marker() ; To diplay marker - EditMarkerIndex.l + EditMarkerIndex.l ; + ; + Options.option ; EndStructure Global PBMap.PBMap, Null.i @@ -163,7 +168,7 @@ Module PBMap EndIf EndProcedure - ;- *** CURL specific *** + ;- *** CURL specific ; (program has To be compiled in console format for curl debug infos) IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl @@ -239,30 +244,33 @@ Module PBMap PBMap\Drawing\Semaphore = CreateSemaphore() PBMap\EditMarkerIndex = -1 ;<- You must initialize with No Marker selected PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) - ;- Proxy details + ;-Options + PBMap\Options\WheelMouseRelative = #True + ;-Preferences ;Use this to create and customize your preferences file for the first time - ; Result = CreatePreferences(GetHomeDirectory() + "PBMap.prefs") - ; If Proxy - ; PreferenceGroup("PROXY") - ; WritePreferenceString("ProxyURL", "myproxy.fr") - ; WritePreferenceString("ProxyPort", "myproxyport") - ; WritePreferenceString("ProxyUser", "myproxyname") - ; EndIf - ; If Result - ; ClosePreferences() - ; EndIf - Result = OpenPreferences(GetHomeDirectory() + "PBMap.prefs") + ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") + ; ;Or this to modify + ; ;OpenPreferences(GetHomeDirectory() + "PBMap.prefs") + ; ;Or this + ; ;RunProgram("notepad.exe", GetHomeDirectory() + "PBMap.prefs", GetHomeDirectory()) + ; PreferenceGroup("PROXY") + ; WritePreferenceInteger("Proxy", #True) + ; WritePreferenceString("ProxyURL", "myproxy.fr") + ; WritePreferenceString("ProxyPort", "myproxyport") + ; WritePreferenceString("ProxyUser", "myproxyname") + ; WritePreferenceString("ProxyPass", "myproxypass") ;TODO !Warning! !not encoded! + ; ClosePreferences() + OpenPreferences(GetHomeDirectory() + "PBMap.prefs") + PreferenceGroup("PROXY") + Proxy = ReadPreferenceInteger("Proxy", #False) If Proxy - PreferenceGroup("PROXY") Global ProxyURL$ = ReadPreferenceString("ProxyURL", "") ;InputRequester("ProxyServer", "Do you use a Proxy Server? Then enter the full url:", "") Global ProxyPort$ = ReadPreferenceString("ProxyPort", "") ;InputRequester("ProxyPort" , "Do you use a specific port? Then enter it", "") Global ProxyUser$ = ReadPreferenceString("ProxyUser", "") ;InputRequester("ProxyUser" , "Do you use a user name? Then enter it", "") - Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") + Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") ;TODO EndIf - If Result - ClosePreferences() - EndIf - curl_global_init(#CURL_GLOBAL_WIN32); + ClosePreferences() + curl_global_init(#CURL_GLOBAL_WIN32) ;- Main drawing thread launching PBMap\MainDrawingThread = CreateThread(@DrawingThread(), @PBMap\Drawing) If PBMap\MainDrawingThread = 0 @@ -310,10 +318,10 @@ Module PBMap Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) If Gadget = #PB_Any - PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height) + PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows Else PBMap\Gadget = Gadget - CanvasGadget(PBMap\Gadget, X, Y, Width, Height) + CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) EndIf EndProcedure @@ -371,7 +379,7 @@ Module PBMap x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) ; convert from degrees To radians latRad = PBMap\TargetLocation\Latitude*#PI/180; - ; get y value + ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) @@ -470,6 +478,7 @@ Module PBMap Protected nImage.i = -1 Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" + Protected Tile.position ;Adding the image to the cache if possible AddMapElement(PBMap\MemCache\Images(), key) nImage = GetTileFromHDD(CacheFile) @@ -478,6 +487,9 @@ Module PBMap EndIf If nImage <> -1 PBMap\MemCache\Images(key)\nImage = nImage + Tile\x=*Tile\PBMapTileX + Tile\y=*Tile\PBMapTiley + XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") MyDebug("With the following key : " + key) Else @@ -571,20 +583,39 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next + ;-****Clean Mem Cache + ForEach PBMap\MemCache\Images() + ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Protected MaxNbTile.l + If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) + MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Else + MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize + EndIf + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + Protected Limit.d=Scale*(MaxNbTile)*1.5 + Protected Distance.d=HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) + Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) + If Distance>Limit + Debug "delete" + DeleteMapElement(PBMap\MemCache\Images()) + EndIf + Next + EndProcedure - Procedure Pointer(x.i, y.i, color.l = #Red) - color=RGBA(255, 0, 0, 255) + Procedure Pointer(x.i, y.i, color.l = 0) VectorSourceColor(color) MovePathCursor(x, y) - AddPathLine(-8,-16,#PB_Path_Relative) - AddPathCircle(8,0,8,180,0,#PB_Path_Relative) - AddPathLine(-8,16,#PB_Path_Relative) + AddPathLine(-8, -16, #PB_Path_Relative) + AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) + AddPathLine(-8, 16, #PB_Path_Relative) ;FillPath(#PB_Path_Preserve) ;ClipPath(#PB_Path_Preserve) - AddPathCircle(0,-16,5,0,360,#PB_Path_Relative) + AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) VectorSourceColor(color) - FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) + FillPath(#PB_Path_Preserve):VectorSourceColor(color);RGBA(0, 0, 0, 255)) + StrokePath(1) EndProcedure Procedure TrackPointer(x.i, y.i,dist.l) @@ -627,7 +658,7 @@ Module PBMap StrokePath(10, #PB_Path_RoundEnd|#PB_Path_RoundCorner) ;Draw Distance ForEach PBMap\track() - ;-Test Distance + ;Distance test If ListIndex(PBMap\track())=0 Location\Latitude=PBMap\track()\Latitude Location\Longitude=PBMap\track()\Longitude @@ -707,8 +738,20 @@ Module PBMap If PBMap\CallBackMainPointer > 0 CallFunctionFast(PBMap\CallBackMainPointer, Drawing\CenterX, Drawing\CenterY) Else - Pointer(Drawing\CenterX, Drawing\CenterY, #Red) + Pointer(Drawing\CenterX, Drawing\CenterY, RGBA($FF, 0, 0, $FF)) EndIf + ;TODO Add Option and function to display Scale on Map + ;Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + ;VectorFont(FontID(PBMap\Font), 30) + ;VectorSourceColor(RGBA(0, 0, 0, 80)) + ;MovePathCursor(50,50) + ;DrawVectorText(StrD(Scale)) + + ;- Display How Many Image in Cache + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while @@ -818,7 +861,43 @@ Module PBMap If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf - EndProcedure + EndProcedure + + ;Zoom on x, y position relative to the canvas gadget + Procedure SetZoomOnPosition(x, y, zoom) + Protected MouseX.d, MouseY.d + Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d + ;Fast and dirty code + OldPx = PBMap\Position\x : OldPy = PBMap\Position\y + OldMx = OldPx + GadgetWidth(PBMap\Gadget) / 2 - x + OldMy = OldPy + GadgetHeight(PBMap\Gadget) / 2 - y + PBMap\Zoom = PBMap\Zoom + zoom + If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf + If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf + ;Centered Zoom + LockMutex(PBMap\Drawing\Mutex) + LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) + ;Convert X, Y in tile.decimal into real pixels + PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize + PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize + MouseX = PBMap\Position\x + GadgetWidth(PBMap\Gadget) / 2 - x + MouseY = PBMap\Position\y + GadgetHeight(PBMap\Gadget) / 2 - y + ;Cross-multiply to get the new center + PBMap\Position\x = (OldPx * MouseX) / OldMx + PBMap\Position\y = (OldPy * MouseY) / OldMy + ;PBMap tile position in tile.decimal + PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize + PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize + PBMap\Drawing\PassNb = 1 + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + UnlockMutex(PBMap\Drawing\Mutex) + ;Start drawing + SignalSemaphore(PBMap\Drawing\Semaphore) + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf + EndProcedure Procedure SetCallBackLocation(CallBackLocation.i) PBMap\CallBackLocation = CallBackLocation @@ -852,12 +931,11 @@ Module PBMap ProcedureReturn Value EndProcedure - + Procedure Event(Event.l) Protected Gadget.i Protected MouseX.i, MouseY.i Protected Marker.Position - Protected *Drawing.DrawingParameters If IsGadget(PBMap\Gadget) And GadgetType(PBMap\Gadget) = #PB_GadgetType_Canvas Select Event Case #PB_Event_Gadget ;{ @@ -866,7 +944,13 @@ Module PBMap Case PBMap\Gadget Select EventType() Case #PB_EventType_MouseWheel - SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta),#PB_Relative) + If PBMap\Options\WheelMouseRelative + ;Relative zoom (centered on the mouse) + SetZoomOnPosition(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta)) + Else + ;Absolute zoom (centered on the center of the map) + SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) + EndIf Case #PB_EventType_LeftButtonDown ;Check if we select a marker MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) @@ -884,11 +968,12 @@ Module PBMap PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) Case #PB_EventType_MouseMove + ;If a move has been initiated by a left click If PBMap\MoveStartingPoint\x <> - 1 MouseX = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\MoveStartingPoint\x MouseY = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - PBMap\MoveStartingPoint\y PBMap\Moving = #True - ;move Marker + ;If it's marker move If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) LatLon2XY(@PBMap\Marker()\Location, @Marker) @@ -899,38 +984,39 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY - ;-*** Fill parameters and signal the drawing thread + ;Fill parameters and signal the drawing thread LockMutex(PBMap\Drawing\Mutex) ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf UnlockMutex(PBMap\Drawing\Mutex) EndIf ;Start drawing SignalSemaphore(PBMap\Drawing\Semaphore) - ;- *** + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) EndIf Case #PB_EventType_LeftButtonUp PBMap\Moving = #False PBMap\MoveStartingPoint\x = - 1 + ;Stop marker move If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 - Else ;Move Map + Else + ;Stop map move LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) UnlockMutex(PBMap\Drawing\Mutex) - EndIf + EndIf EndSelect EndSelect EndSelect @@ -1019,7 +1105,7 @@ CompilerIf #PB_Compiler_IsMainFile LoadFont(0, "Wingdings", 12) LoadFont(1, "Arial", 12, #PB_Font_Bold) - + TextGadget(#Text_1, 530, 50, 60, 15, "Movements") ButtonGadget(#Gdt_Left, 550, 100, 30, 30, Chr($E7)) : SetGadgetFont(#Gdt_Left, FontID(0)) ButtonGadget(#Gdt_Right, 610, 100, 30, 30, Chr($E8)) : SetGadgetFont(#Gdt_Right, FontID(0)) @@ -1067,10 +1153,10 @@ CompilerIf #PB_Compiler_IsMainFile Case #Button_5 PBMap::SetZoom( - 1) Case #Gdt_LoadGpx - PBMap::LoadGpxFile(OpenFileRequester("Choisissez un fichier à charger", "", "*.gpx", 0)) - PBMap::ZoomToArea() ; <-To center the view, and to viex all the track + PBMap::LoadGpxFile(OpenFileRequester("Choose a file to load", "", "*.gpx", 0)) + PBMap::ZoomToArea() ; <-To center the view, and zoom on the tracks Case #Gdt_AddMarker - PBMap:: AddMarker(ValD(GetGadgetText(#String_0)),ValD(GetGadgetText(#String_1)),RGBA(Random(255),Random(255),Random(255),255)) + PBMap:: AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255),255)) EndSelect Case #PB_Event_SizeWindow ResizeAll() @@ -1080,10 +1166,9 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf CompilerEndIf - -; IDE Options = PureBasic 5.50 (Windows - x86) -; CursorPosition = 994 -; FirstLine = 969 +; IDE Options = PureBasic 5.50 (Windows - x64) +; CursorPosition = 1167 +; FirstLine = 1123 ; Folding = --------- ; EnableThread ; EnableXP From 4d3c3ffffcc71ea875aa32e40ee1203af503da62 Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 23 Aug 2016 09:51:27 +0200 Subject: [PATCH 2/8] Revert "Cache cleaning" This reverts commit 035b26d9dbf30d754d476dd6dfbe0faf06bd6a7b. --- PBMap.pb | 215 +++++++++++++++++-------------------------------------- 1 file changed, 65 insertions(+), 150 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 52ef232..5795e5f 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -26,6 +26,7 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos Global Verbose = #False + ;-Proxy ON/OFF Global Proxy = #False Declare InitPBMap() Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) @@ -101,7 +102,7 @@ Module PBMap Structure ImgMemCach nImage.i - Location.Location + Usage.i EndStructure Structure TileMemCach @@ -114,43 +115,37 @@ Module PBMap CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) EndStructure - Structure Option - WheelMouseRelative.i - EndStructure - ;-PBMap Structure Structure PBMap Gadget.i ; Canvas Gadget Id Font.i ; Font to uses when write on the map TargetLocation.Location ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point - ; + ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - ; + ; Position.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map - ; + ; ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/ ZoomMin.i ; Min Zoom supported by server ZoomMax.i ; Max Zoom supported by server Zoom.i ; Current zoom TileSize.i ; Tile size downloaded on the server ex : 256 - ; + ; HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache - ; - Moving.i ; + ; + Moving.i Dirty.i ; To signal that drawing need a refresh - ; - MainDrawingThread.i ; - List TilesThreads.TileThread() ; - ; + ; + MainDrawingThread.i + List TilesThreads.TileThread() + ; List track.Location() ; To display a GPX track List Marker.Marker() ; To diplay marker - EditMarkerIndex.l ; - ; - Options.option ; + EditMarkerIndex.l EndStructure Global PBMap.PBMap, Null.i @@ -168,7 +163,7 @@ Module PBMap EndIf EndProcedure - ;- *** CURL specific + ;- *** CURL specific *** ; (program has To be compiled in console format for curl debug infos) IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl @@ -244,33 +239,30 @@ Module PBMap PBMap\Drawing\Semaphore = CreateSemaphore() PBMap\EditMarkerIndex = -1 ;<- You must initialize with No Marker selected PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) - ;-Options - PBMap\Options\WheelMouseRelative = #True - ;-Preferences + ;- Proxy details ;Use this to create and customize your preferences file for the first time - ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") - ; ;Or this to modify - ; ;OpenPreferences(GetHomeDirectory() + "PBMap.prefs") - ; ;Or this - ; ;RunProgram("notepad.exe", GetHomeDirectory() + "PBMap.prefs", GetHomeDirectory()) - ; PreferenceGroup("PROXY") - ; WritePreferenceInteger("Proxy", #True) - ; WritePreferenceString("ProxyURL", "myproxy.fr") - ; WritePreferenceString("ProxyPort", "myproxyport") - ; WritePreferenceString("ProxyUser", "myproxyname") - ; WritePreferenceString("ProxyPass", "myproxypass") ;TODO !Warning! !not encoded! - ; ClosePreferences() - OpenPreferences(GetHomeDirectory() + "PBMap.prefs") - PreferenceGroup("PROXY") - Proxy = ReadPreferenceInteger("Proxy", #False) + ; Result = CreatePreferences(GetHomeDirectory() + "PBMap.prefs") + ; If Proxy + ; PreferenceGroup("PROXY") + ; WritePreferenceString("ProxyURL", "myproxy.fr") + ; WritePreferenceString("ProxyPort", "myproxyport") + ; WritePreferenceString("ProxyUser", "myproxyname") + ; EndIf + ; If Result + ; ClosePreferences() + ; EndIf + Result = OpenPreferences(GetHomeDirectory() + "PBMap.prefs") If Proxy + PreferenceGroup("PROXY") Global ProxyURL$ = ReadPreferenceString("ProxyURL", "") ;InputRequester("ProxyServer", "Do you use a Proxy Server? Then enter the full url:", "") Global ProxyPort$ = ReadPreferenceString("ProxyPort", "") ;InputRequester("ProxyPort" , "Do you use a specific port? Then enter it", "") Global ProxyUser$ = ReadPreferenceString("ProxyUser", "") ;InputRequester("ProxyUser" , "Do you use a user name? Then enter it", "") - Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") ;TODO + Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") EndIf - ClosePreferences() - curl_global_init(#CURL_GLOBAL_WIN32) + If Result + ClosePreferences() + EndIf + curl_global_init(#CURL_GLOBAL_WIN32); ;- Main drawing thread launching PBMap\MainDrawingThread = CreateThread(@DrawingThread(), @PBMap\Drawing) If PBMap\MainDrawingThread = 0 @@ -318,10 +310,10 @@ Module PBMap Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) If Gadget = #PB_Any - PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows + PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height) Else PBMap\Gadget = Gadget - CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) + CanvasGadget(PBMap\Gadget, X, Y, Width, Height) EndIf EndProcedure @@ -379,7 +371,7 @@ Module PBMap x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) ; convert from degrees To radians latRad = PBMap\TargetLocation\Latitude*#PI/180; - ; get y value + ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) @@ -478,7 +470,6 @@ Module PBMap Protected nImage.i = -1 Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" - Protected Tile.position ;Adding the image to the cache if possible AddMapElement(PBMap\MemCache\Images(), key) nImage = GetTileFromHDD(CacheFile) @@ -487,9 +478,6 @@ Module PBMap EndIf If nImage <> -1 PBMap\MemCache\Images(key)\nImage = nImage - Tile\x=*Tile\PBMapTileX - Tile\y=*Tile\PBMapTiley - XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") MyDebug("With the following key : " + key) Else @@ -583,39 +571,20 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next - ;-****Clean Mem Cache - ForEach PBMap\MemCache\Images() - ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Protected MaxNbTile.l - If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) - MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Else - MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize - EndIf - Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - Protected Limit.d=Scale*(MaxNbTile)*1.5 - Protected Distance.d=HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) - Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) - If Distance>Limit - Debug "delete" - DeleteMapElement(PBMap\MemCache\Images()) - EndIf - Next - EndProcedure - Procedure Pointer(x.i, y.i, color.l = 0) + Procedure Pointer(x.i, y.i, color.l = #Red) + color=RGBA(255, 0, 0, 255) VectorSourceColor(color) MovePathCursor(x, y) - AddPathLine(-8, -16, #PB_Path_Relative) - AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) - AddPathLine(-8, 16, #PB_Path_Relative) + AddPathLine(-8,-16,#PB_Path_Relative) + AddPathCircle(8,0,8,180,0,#PB_Path_Relative) + AddPathLine(-8,16,#PB_Path_Relative) ;FillPath(#PB_Path_Preserve) ;ClipPath(#PB_Path_Preserve) - AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) + AddPathCircle(0,-16,5,0,360,#PB_Path_Relative) VectorSourceColor(color) - FillPath(#PB_Path_Preserve):VectorSourceColor(color);RGBA(0, 0, 0, 255)) - StrokePath(1) + FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) EndProcedure Procedure TrackPointer(x.i, y.i,dist.l) @@ -658,7 +627,7 @@ Module PBMap StrokePath(10, #PB_Path_RoundEnd|#PB_Path_RoundCorner) ;Draw Distance ForEach PBMap\track() - ;Distance test + ;-Test Distance If ListIndex(PBMap\track())=0 Location\Latitude=PBMap\track()\Latitude Location\Longitude=PBMap\track()\Longitude @@ -738,20 +707,8 @@ Module PBMap If PBMap\CallBackMainPointer > 0 CallFunctionFast(PBMap\CallBackMainPointer, Drawing\CenterX, Drawing\CenterY) Else - Pointer(Drawing\CenterX, Drawing\CenterY, RGBA($FF, 0, 0, $FF)) + Pointer(Drawing\CenterX, Drawing\CenterY, #Red) EndIf - ;TODO Add Option and function to display Scale on Map - ;Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - ;VectorFont(FontID(PBMap\Font), 30) - ;VectorSourceColor(RGBA(0, 0, 0, 80)) - ;MovePathCursor(50,50) - ;DrawVectorText(StrD(Scale)) - - ;- Display How Many Image in Cache - VectorFont(FontID(PBMap\Font), 30) - VectorSourceColor(RGBA(0, 0, 0, 80)) - MovePathCursor(50,50) - DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while @@ -861,43 +818,7 @@ Module PBMap If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf - EndProcedure - - ;Zoom on x, y position relative to the canvas gadget - Procedure SetZoomOnPosition(x, y, zoom) - Protected MouseX.d, MouseY.d - Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d - ;Fast and dirty code - OldPx = PBMap\Position\x : OldPy = PBMap\Position\y - OldMx = OldPx + GadgetWidth(PBMap\Gadget) / 2 - x - OldMy = OldPy + GadgetHeight(PBMap\Gadget) / 2 - y - PBMap\Zoom = PBMap\Zoom + zoom - If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf - If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - ;Centered Zoom - LockMutex(PBMap\Drawing\Mutex) - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) - ;Convert X, Y in tile.decimal into real pixels - PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize - PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize - MouseX = PBMap\Position\x + GadgetWidth(PBMap\Gadget) / 2 - x - MouseY = PBMap\Position\y + GadgetHeight(PBMap\Gadget) / 2 - y - ;Cross-multiply to get the new center - PBMap\Position\x = (OldPx * MouseX) / OldMx - PBMap\Position\y = (OldPy * MouseY) / OldMy - ;PBMap tile position in tile.decimal - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize - PBMap\Drawing\PassNb = 1 - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - UnlockMutex(PBMap\Drawing\Mutex) - ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf - EndProcedure + EndProcedure Procedure SetCallBackLocation(CallBackLocation.i) PBMap\CallBackLocation = CallBackLocation @@ -931,11 +852,12 @@ Module PBMap ProcedureReturn Value EndProcedure - + Procedure Event(Event.l) Protected Gadget.i Protected MouseX.i, MouseY.i Protected Marker.Position + Protected *Drawing.DrawingParameters If IsGadget(PBMap\Gadget) And GadgetType(PBMap\Gadget) = #PB_GadgetType_Canvas Select Event Case #PB_Event_Gadget ;{ @@ -944,13 +866,7 @@ Module PBMap Case PBMap\Gadget Select EventType() Case #PB_EventType_MouseWheel - If PBMap\Options\WheelMouseRelative - ;Relative zoom (centered on the mouse) - SetZoomOnPosition(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta)) - Else - ;Absolute zoom (centered on the center of the map) - SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) - EndIf + SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta),#PB_Relative) Case #PB_EventType_LeftButtonDown ;Check if we select a marker MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) @@ -968,12 +884,11 @@ Module PBMap PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) Case #PB_EventType_MouseMove - ;If a move has been initiated by a left click If PBMap\MoveStartingPoint\x <> - 1 MouseX = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\MoveStartingPoint\x MouseY = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - PBMap\MoveStartingPoint\y PBMap\Moving = #True - ;If it's marker move + ;move Marker If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) LatLon2XY(@PBMap\Marker()\Location, @Marker) @@ -984,39 +899,38 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY - ;Fill parameters and signal the drawing thread + ;-*** Fill parameters and signal the drawing thread LockMutex(PBMap\Drawing\Mutex) ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf UnlockMutex(PBMap\Drawing\Mutex) EndIf ;Start drawing SignalSemaphore(PBMap\Drawing\Semaphore) - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf + ;- *** PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) EndIf Case #PB_EventType_LeftButtonUp PBMap\Moving = #False PBMap\MoveStartingPoint\x = - 1 - ;Stop marker move If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 - Else - ;Stop map move + Else ;Move Map LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) UnlockMutex(PBMap\Drawing\Mutex) - EndIf + EndIf EndSelect EndSelect EndSelect @@ -1105,7 +1019,7 @@ CompilerIf #PB_Compiler_IsMainFile LoadFont(0, "Wingdings", 12) LoadFont(1, "Arial", 12, #PB_Font_Bold) - + TextGadget(#Text_1, 530, 50, 60, 15, "Movements") ButtonGadget(#Gdt_Left, 550, 100, 30, 30, Chr($E7)) : SetGadgetFont(#Gdt_Left, FontID(0)) ButtonGadget(#Gdt_Right, 610, 100, 30, 30, Chr($E8)) : SetGadgetFont(#Gdt_Right, FontID(0)) @@ -1153,10 +1067,10 @@ CompilerIf #PB_Compiler_IsMainFile Case #Button_5 PBMap::SetZoom( - 1) Case #Gdt_LoadGpx - PBMap::LoadGpxFile(OpenFileRequester("Choose a file to load", "", "*.gpx", 0)) - PBMap::ZoomToArea() ; <-To center the view, and zoom on the tracks + PBMap::LoadGpxFile(OpenFileRequester("Choisissez un fichier à charger", "", "*.gpx", 0)) + PBMap::ZoomToArea() ; <-To center the view, and to viex all the track Case #Gdt_AddMarker - PBMap:: AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255),255)) + PBMap:: AddMarker(ValD(GetGadgetText(#String_0)),ValD(GetGadgetText(#String_1)),RGBA(Random(255),Random(255),Random(255),255)) EndSelect Case #PB_Event_SizeWindow ResizeAll() @@ -1166,9 +1080,10 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf CompilerEndIf -; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1167 -; FirstLine = 1123 + +; IDE Options = PureBasic 5.50 (Windows - x86) +; CursorPosition = 994 +; FirstLine = 969 ; Folding = --------- ; EnableThread ; EnableXP From 654a17fec66da8c82f181b423c366bcb63c5fd2d Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 23 Aug 2016 09:52:26 +0200 Subject: [PATCH 3/8] Cache cleaning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit premier test de nettoyage du cache mémoire pour éviter que l'application grignote de plus en plus de mémoire --- PBMap.pb | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index f6f955a..9985f29 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -101,7 +101,7 @@ Module PBMap Structure ImgMemCach nImage.i - Usage.i + Location.Location EndStructure Structure TileMemCach @@ -478,6 +478,7 @@ Module PBMap Protected nImage.i = -1 Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" + Protected Tile.position ;Adding the image to the cache if possible AddMapElement(PBMap\MemCache\Images(), key) nImage = GetTileFromHDD(CacheFile) @@ -486,6 +487,9 @@ Module PBMap EndIf If nImage <> -1 PBMap\MemCache\Images(key)\nImage = nImage + Tile\x=*Tile\PBMapTileX + Tile\y=*Tile\PBMapTiley + XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") MyDebug("With the following key : " + key) Else @@ -579,6 +583,25 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next + ;-****Clean Mem Cache + ForEach PBMap\MemCache\Images() + ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Protected MaxNbTile.l + If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) + MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Else + MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize + EndIf + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + Protected Limit.d=Scale*(MaxNbTile)*1.5 + Protected Distance.d=HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) + Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) + If Distance>Limit + Debug "delete" + DeleteMapElement(PBMap\MemCache\Images()) + EndIf + Next + EndProcedure Procedure Pointer(x.i, y.i, color.l = 0) @@ -717,6 +740,18 @@ Module PBMap Else Pointer(Drawing\CenterX, Drawing\CenterY, RGBA($FF, 0, 0, $FF)) EndIf + ;TODO Add Option and function to display Scale on Map + ;Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + ;VectorFont(FontID(PBMap\Font), 30) + ;VectorSourceColor(RGBA(0, 0, 0, 80)) + ;MovePathCursor(50,50) + ;DrawVectorText(StrD(Scale)) + + ;- Display How Many Image in Cache + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while @@ -1131,11 +1166,10 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf CompilerEndIf - -; IDE Options = PureBasic 5.42 LTS (Windows - x86) -; CursorPosition = 1120 -; FirstLine = 1077 +; IDE Options = PureBasic 5.50 (Windows - x64) +; CursorPosition = 1132 +; FirstLine = 1123 ; Folding = --------- -; EnableUnicode ; EnableThread -; EnableXP \ No newline at end of file +; EnableXP +; EnableUnicode \ No newline at end of file From c679cfbd7330cd050005f14492fec28e8a00bbd4 Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 24 Aug 2016 18:00:08 +0200 Subject: [PATCH 4/8] WIP typhoon cache cleaning --- PBMap.pb | 76 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 9985f29..dd6f07f 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -102,6 +102,7 @@ Module PBMap Structure ImgMemCach nImage.i Location.Location + Mutex.i EndStructure Structure TileMemCach @@ -481,23 +482,26 @@ Module PBMap Protected Tile.position ;Adding the image to the cache if possible AddMapElement(PBMap\MemCache\Images(), key) + PBMap\MemCache\Images(key)\Mutex = CreateMutex() + LockMutex(PBMap\MemCache\Images(key)\Mutex) nImage = GetTileFromHDD(CacheFile) If nImage = -1 nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) + If nImage = -1 + MyDebug("Error GetImageThread procedure, image not loaded - " + key) + *Tile\nImage = -1 + ProcedureReturn + EndIf EndIf - If nImage <> -1 - PBMap\MemCache\Images(key)\nImage = nImage - Tile\x=*Tile\PBMapTileX - Tile\y=*Tile\PBMapTiley - XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) - MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") - MyDebug("With the following key : " + key) - Else - MyDebug("Error GetImageThread procedure, image not loaded - " + key) - nImage = -1 - EndIf + PBMap\MemCache\Images(key)\nImage = nImage + Tile\x=*Tile\PBMapTileX + Tile\y=*Tile\PBMapTiley + XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) + MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") + MyDebug("With the following key : " + key) ;Define this tile image nb *Tile\nImage = nImage + UnlockMutex(PBMap\MemCache\Images(key)\Mutex) EndProcedure Procedure DrawTile(*Tile.Tile) @@ -583,23 +587,30 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next - ;-****Clean Mem Cache + ;-**** Clean Mem Cache + ;TODO in development, by now there's many cache problem as the loading thread could be perturbed + ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Protected MaxNbTile.l + If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) + MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Else + MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize + EndIf + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + Protected Limit.d=Scale*(MaxNbTile)*1.5 + Debug "Cache cleaning" ForEach PBMap\MemCache\Images() - ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Protected MaxNbTile.l - If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) - MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Else - MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize - EndIf - Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - Protected Limit.d=Scale*(MaxNbTile)*1.5 - Protected Distance.d=HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) - Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) - If Distance>Limit - Debug "delete" - DeleteMapElement(PBMap\MemCache\Images()) - EndIf + Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) + Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) + If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) + LockMutex(PBMap\MemCache\Images()\Mutex) + Debug "delete" + Debug PBMap\MemCache\Images() + FreeImage(PBMap\MemCache\Images()\nImage) + UnlockMutex(PBMap\MemCache\Images()\Mutex) + FreeMutex(PBMap\MemCache\Images()\Mutex) + DeleteMapElement(PBMap\MemCache\Images()) + EndIf Next EndProcedure @@ -984,7 +995,7 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY - ;Fill parameters and signal the drawing thread + ;Fill parameters and send a signal to the drawing thread LockMutex(PBMap\Drawing\Mutex) ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize @@ -992,9 +1003,9 @@ Module PBMap PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) UnlockMutex(PBMap\Drawing\Mutex) + ;Start drawing + SignalSemaphore(PBMap\Drawing\Semaphore) EndIf - ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) @@ -1167,8 +1178,9 @@ CompilerIf #PB_Compiler_IsMainFile EndIf CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1132 -; FirstLine = 1123 +; ExecutableFormat = Console +; CursorPosition = 595 +; FirstLine = 581 ; Folding = --------- ; EnableThread ; EnableXP From 46d4a37c982e70b0c4c44e9feb30cde5eaf349f4 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 25 Aug 2016 11:56:54 +0200 Subject: [PATCH 5/8] Debug, code cleaning, debug infos Map list add was theaded and could lead to crash. --- PBMap.pb | 206 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 107 insertions(+), 99 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index dd6f07f..616e36d 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -25,7 +25,8 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos - Global Verbose = #False + Global Verbose = #True + Global MyDebugLevel = 1 Global Proxy = #False Declare InitPBMap() Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) @@ -73,6 +74,7 @@ Module PBMap PBMapTileY.i PBMapZoom.i nImage.i + key.s GetImageThread.i EndStructure @@ -95,8 +97,8 @@ Module PBMap EndStructure Structure TileThread + *Tile.Tile GetImageThread.i - *Tile.Tile EndStructure Structure ImgMemCach @@ -162,9 +164,9 @@ Module PBMap End EndProcedure - ;Send debug infos to stdout - Procedure MyDebug(msg.s) - If Verbose + ;Send debug infos to stdout (allowing mixed debug infos with curl or other libs) + Procedure MyDebug(msg.s, DbgLevel = 0) + If Verbose And MyDebugLevel >= DbgLevel PrintN(msg) EndIf EndProcedure @@ -417,14 +419,13 @@ Module PBMap EndIf EndProcedure - Procedure.i GetTileFromMem(Zoom.i, XTile.i, YTile.i) - Protected key.s = "Z" + RSet(Str(Zoom), 4, "0") + "X" + RSet(Str(XTile), 8, "0") + "Y" + RSet(Str(YTile), 8, "0") - MyDebug("Check if we have this image in memory") + Procedure.i GetTileFromMem(key.s) + MyDebug("Check if we have this image in memory", 3) If FindMapElement(PBMap\MemCache\Images(), key) - MyDebug("Key : " + key + " found !") + MyDebug("Key : " + key + " found !", 3) ProcedureReturn PBMap\MemCache\Images()\nImage Else - MyDebug("Key : " + key + " not found !") + MyDebug("Key : " + key + " not found !", 3) ProcedureReturn -1 EndIf EndProcedure @@ -434,11 +435,11 @@ Module PBMap If FileSize(CacheFile) > 0 nImage = LoadImage(#PB_Any, CacheFile) If IsImage(nImage) - MyDebug("Loadimage " + CacheFile + " -> Success !") + MyDebug("Loadimage " + CacheFile + " -> Success !", 3) ProcedureReturn nImage EndIf EndIf - MyDebug("Loadimage " + CacheFile + " -> Failed !") + MyDebug("Loadimage " + CacheFile + " -> Failed !", 3) ProcedureReturn -1 EndProcedure @@ -447,29 +448,29 @@ Module PBMap Protected nImage.i = -1 Protected FileHandle.i Protected TileURL.s = PBMap\ServerURL + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" - MyDebug("Check if we have this image on Web") + MyDebug("Check if we have this image on Web", 3) If Proxy FileHandle = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) If FileHandle nImage = GetTileFromHDD(CacheFile) Else - MyDebug("File " + TileURL + " not correctly received with Curl and proxy") + MyDebug("File " + TileURL + " not correctly received with Curl and proxy", 3) EndIf Else *Buffer = ReceiveHTTPMemory(TileURL) ;TODO to thread by using #PB_HTTP_Asynchronous If *Buffer nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer)) If IsImage(nImage) - MyDebug("Load from web " + TileURL + " as Tile nb " + nImage) + MyDebug("Load from web " + TileURL + " as Tile nb " + nImage, 3) SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) FreeMemory(*Buffer) Else - MyDebug("Can't catch image " + TileURL) + MyDebug("Can't catch image " + TileURL, 3) nImage = -1 ;ShowMemoryViewer(*Buffer, MemorySize(*Buffer)) EndIf Else - MyDebug("ReceiveHTTPMemory's buffer is empty") + MyDebug("ReceiveHTTPMemory's buffer is empty", 3) EndIf EndIf ProcedureReturn nImage @@ -477,38 +478,35 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 - Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" Protected Tile.position - ;Adding the image to the cache if possible - AddMapElement(PBMap\MemCache\Images(), key) - PBMap\MemCache\Images(key)\Mutex = CreateMutex() - LockMutex(PBMap\MemCache\Images(key)\Mutex) +; PBMap\MemCache\Images(*Tile\key)\Mutex = CreateMutex() +; LockMutex(PBMap\MemCache\Images(*Tile\key)\Mutex) nImage = GetTileFromHDD(CacheFile) If nImage = -1 nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) If nImage = -1 - MyDebug("Error GetImageThread procedure, image not loaded - " + key) + MyDebug("Error GetImageThread procedure, image not loaded - " + *Tile\key, 3) *Tile\nImage = -1 ProcedureReturn EndIf EndIf - PBMap\MemCache\Images(key)\nImage = nImage - Tile\x=*Tile\PBMapTileX - Tile\y=*Tile\PBMapTiley - XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) - MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") - MyDebug("With the following key : " + key) + PBMap\MemCache\Images(*Tile\key)\nImage = nImage +; Tile\x=*Tile\PBMapTileX +; Tile\y=*Tile\PBMapTiley +; XY2LatLon(@Tile,@PBMap\MemCache\Images(*Tile\key)\Location) + MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache", 3) + MyDebug("With the following key : " + *Tile\key, 3) ;Define this tile image nb *Tile\nImage = nImage - UnlockMutex(PBMap\MemCache\Images(key)\Mutex) +; UnlockMutex(PBMap\MemCache\Images(key)\Mutex) EndProcedure Procedure DrawTile(*Tile.Tile) Protected x = *Tile\Position\x Protected y = *Tile\Position\y - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX)) - MyDebug(" at coords " + Str(x) + "," + Str(y)) + MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + MyDebug(" at coords " + Str(x) + "," + Str(y), 2) MovePathCursor(x, y) DrawVectorImage(ImageID(*Tile\nImage)) EndProcedure @@ -517,8 +515,8 @@ Module PBMap Protected x = *Tile\Position\x Protected y = *Tile\Position\y Protected Text$ = "Loading" - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX)) - MyDebug(" at coords " + Str(x) + "," + Str(y)) + MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + MyDebug(" at coords " + Str(x) + "," + Str(y), 2) BeginVectorLayer() ;MovePathCursor(x, y) VectorSourceColor(RGBA(255, 255, 255, 128)) @@ -538,17 +536,18 @@ Module PBMap Protected ty = Int(*Drawing\Position\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize - MyDebug("Drawing tiles") + MyDebug("Drawing tiles", 2) For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 ;Was quiting the loop if a move occured, giving maybe smoother movement ;If PBMap\Moving ; Break 2 ;EndIf + ;Store parameters in only one memory place, and give it to the thread if needed Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile With *NewTile - ;Keep a track of tiles (especially to free memory) + ;Keep a track of tiles and eventually threads associated (to free memory) AddElement(PBMap\TilesThreads()) PBMap\TilesThreads()\Tile = *NewTile ;New tile parameters @@ -557,78 +556,98 @@ Module PBMap \PBMapTileX = tx + x \PBMapTileY = ty + y \PBMapZoom = PBMap\Zoom + \key = "Z" + RSet(Str(\PBMapZoom), 4, "0") + "X" + RSet(Str(\PBMapTileX), 8, "0") + "Y" + RSet(Str(\PBMapTileY), 8, "0") ;Check if the image exists - \nImage = GetTileFromMem(\PBMapZoom, \PBMapTileX, \PBMapTileY) - If \nImage = -1 - ;If not, load it in the background - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - PBMap\TilesThreads()\GetImageThread = \GetImageThread - MyDebug(" Creating get image thread nb " + Str(\GetImageThread)) + \nImage = GetTileFromMem(\key) + If \nImage = -1 + ;If not, load it in the background + If AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache *only* in this loop + \GetImageThread = CreateThread(@GetImageThread(), *NewTile) + PBMap\TilesThreads()\GetImageThread = \GetImageThread + MyDebug(" Creating get image thread nb " + Str(\GetImageThread), 2) + Else + MyDebug(" Can't add a new image to the map list", 2) + EndIf EndIf If IsImage(\nImage) DrawTile(*NewTile) Else - MyDebug("Image missing") + MyDebug("Image missing", 2) DrawLoading(*NewTile) *Drawing\Dirty = #True ;Signals that this image is missing so we should have to redraw EndIf EndWith Else - MyDebug(" Error, can't create a new tile") + MyDebug(" Error, can't create a new tile", 2) Break 2 EndIf Next Next - ;Free tile memory when the loading thread has finished + ;Free tile memory ;TODO : get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() + ;Check if there's no more loading thread If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 FreeMemory(PBMap\TilesThreads()\Tile) DeleteElement(PBMap\TilesThreads()) EndIf Next - ;-**** Clean Mem Cache - ;TODO in development, by now there's many cache problem as the loading thread could be perturbed - ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Protected MaxNbTile.l - If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) - MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Else - MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize - EndIf - Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - Protected Limit.d=Scale*(MaxNbTile)*1.5 - Debug "Cache cleaning" - ForEach PBMap\MemCache\Images() - Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) - Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) - If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) - LockMutex(PBMap\MemCache\Images()\Mutex) - Debug "delete" - Debug PBMap\MemCache\Images() - FreeImage(PBMap\MemCache\Images()\nImage) - UnlockMutex(PBMap\MemCache\Images()\Mutex) - FreeMutex(PBMap\MemCache\Images()\Mutex) - DeleteMapElement(PBMap\MemCache\Images()) - EndIf - Next +; ;-**** Clean Mem Cache +; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed +; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize +; Protected MaxNbTile.l +; If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) +; MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize +; Else +; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize +; EndIf +; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) +; Protected Limit.d=Scale*(MaxNbTile)*1.5 +; Debug "Cache cleaning" +; ForEach PBMap\MemCache\Images() +; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) +; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) +; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) +; LockMutex(PBMap\MemCache\Images()\Mutex) +; Debug "delete" +; Debug PBMap\MemCache\Images() +; FreeImage(PBMap\MemCache\Images()\nImage) +; UnlockMutex(PBMap\MemCache\Images()\Mutex) +; FreeMutex(PBMap\MemCache\Images()\Mutex) +; DeleteMapElement(PBMap\MemCache\Images()) +; EndIf +; Next EndProcedure - Procedure Pointer(x.i, y.i, color.l = 0) - VectorSourceColor(color) - MovePathCursor(x, y) - AddPathLine(-8, -16, #PB_Path_Relative) - AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) - AddPathLine(-8, 16, #PB_Path_Relative) - ;FillPath(#PB_Path_Preserve) - ;ClipPath(#PB_Path_Preserve) - AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) - VectorSourceColor(color) - FillPath(#PB_Path_Preserve):VectorSourceColor(color);RGBA(0, 0, 0, 255)) - StrokePath(1) + Procedure DrawPointer(*Drawing.DrawingParameters) + If PBMap\CallBackMainPointer > 0 + ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) + CallFunctionFast(PBMap\CallBackMainPointer, *Drawing\CenterX, *Drawing\CenterY) + Else + VectorSourceColor(RGBA($FF, 0, 0, $FF)) + MovePathCursor(*Drawing\CenterX, *Drawing\CenterY) + AddPathLine(-8, -16, #PB_Path_Relative) + AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) + AddPathLine(-8, 16, #PB_Path_Relative) + ;FillPath(#PB_Path_Preserve) + ;ClipPath(#PB_Path_Preserve) + AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) + VectorSourceColor(RGBA($FF, 0, 0, $FF)) + FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA($FF, 0, 0, $FF));RGBA(0, 0, 0, 255)) + StrokePath(1) + EndIf EndProcedure + Procedure DrawScale(*Drawing.DrawingParameters) + ;TODO Add Option and function to display Scale on Map + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(StrD(Scale)) + EndProcedure + Procedure TrackPointer(x.i, y.i,dist.l) Protected color.l color=RGBA(0, 0, 0, 255) @@ -711,7 +730,7 @@ Module PBMap If PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) Else - Pointer(Pixel\X, Pixel\Y, PBMap\Marker()\color) + DrawPointer(*Drawing) EndIf EndIf EndIf @@ -745,20 +764,9 @@ Module PBMap DrawTiles(@Drawing) DrawTrack(@Drawing) DrawMarker(@Drawing) - ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - If PBMap\CallBackMainPointer > 0 - CallFunctionFast(PBMap\CallBackMainPointer, Drawing\CenterX, Drawing\CenterY) - Else - Pointer(Drawing\CenterX, Drawing\CenterY, RGBA($FF, 0, 0, $FF)) - EndIf - ;TODO Add Option and function to display Scale on Map - ;Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - ;VectorFont(FontID(PBMap\Font), 30) - ;VectorSourceColor(RGBA(0, 0, 0, 80)) - ;MovePathCursor(50,50) - ;DrawVectorText(StrD(Scale)) - - ;- Display How Many Image in Cache + DrawPointer(@Drawing) + ;DrawScale(@Drawing) + ;- Display how many images in cache VectorFont(FontID(PBMap\Font), 30) VectorSourceColor(RGBA(0, 0, 0, 80)) MovePathCursor(50,50) @@ -1179,8 +1187,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) ; ExecutableFormat = Console -; CursorPosition = 595 -; FirstLine = 581 +; CursorPosition = 445 +; FirstLine = 442 ; Folding = --------- ; EnableThread ; EnableXP From a4f2ea5bb71b9ff32f4d058c30c268254238a0fb Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 25 Aug 2016 12:52:36 +0200 Subject: [PATCH 6/8] code cleanup --- PBMap.pb | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 616e36d..b069392 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -26,7 +26,7 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos Global Verbose = #True - Global MyDebugLevel = 1 + Global MyDebugLevel = 3 Global Proxy = #False Declare InitPBMap() Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) @@ -103,8 +103,8 @@ Module PBMap Structure ImgMemCach nImage.i - Location.Location - Mutex.i + ;Location.Location + ;Mutex.i EndStructure Structure TileMemCach @@ -184,8 +184,8 @@ Module PBMap Procedure.i CurlReceiveHTTPToFile(URL$, DestFileName$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="") Protected *Buffer, curl.i, Timeout.i, res.i Protected FileHandle.i - MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$) - MyDebug(" to file : " + DestFileName$) + MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$, 3) + MyDebug(" to file : " + DestFileName$, 3) FileHandle = CreateFile(#PB_Any, DestFileName$) If FileHandle And Len(URL$) curl = curl_easy_init() @@ -218,11 +218,11 @@ Module PBMap curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToFileFunction()) res = curl_easy_perform(curl) If res <> #CURLE_OK - MyDebug("CURL problem") + MyDebug("CURL problem", 3) EndIf curl_easy_cleanup(curl) Else - MyDebug("Can't init CURL") + MyDebug("Can't init CURL", 3) EndIf CloseFile(FileHandle) ProcedureReturn FileSize(DestFileName$) @@ -245,7 +245,7 @@ Module PBMap PBMap\Dirty = #False PBMap\Drawing\Mutex = CreateMutex() PBMap\Drawing\Semaphore = CreateSemaphore() - PBMap\EditMarkerIndex = -1 ;<- You must initialize with No Marker selected + PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected" PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) ;-Options PBMap\Options\WheelMouseRelative = #True @@ -289,10 +289,12 @@ Module PBMap EndProcedure Procedure Quit() - ;kill main drawing thread (nicer than KillThread(PBMap\MainDrawingThread)) + ;Ask main drawing thread to stop and wait for it (nicer than KillThread(PBMap\MainDrawingThread)) LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\End = #True - UnlockMutex(PBMap\Drawing\Mutex) + ;KillThread(PBMap\MainDrawingThread) + UnlockMutex(PBMap\Drawing\Mutex) + Repeat : Until Not IsThread(PBMap\MainDrawingThread) ;wait for loading threads to finish nicely ResetList(PBMap\TilesThreads()) While NextElement(PBMap\TilesThreads()) @@ -302,7 +304,7 @@ Module PBMap ResetList( PBMap\TilesThreads()) EndIf Wend - curl_global_cleanup() + curl_global_cleanup() EndProcedure Macro Min(a,b) @@ -547,7 +549,7 @@ Module PBMap Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile With *NewTile - ;Keep a track of tiles and eventually threads associated (to free memory) + ;Keep a track of tiles, and eventually associated threads, to free memory AddElement(PBMap\TilesThreads()) PBMap\TilesThreads()\Tile = *NewTile ;New tile parameters @@ -556,12 +558,12 @@ Module PBMap \PBMapTileX = tx + x \PBMapTileY = ty + y \PBMapZoom = PBMap\Zoom - \key = "Z" + RSet(Str(\PBMapZoom), 4, "0") + "X" + RSet(Str(\PBMapTileX), 8, "0") + "Y" + RSet(Str(\PBMapTileY), 8, "0") + \key = "Z" + RSet(Str(\PBMapZoom), 4, "0") + "X" + RSet(Str(\PBMapTileX), 8, "0") + "Y" + RSet(Str(\PBMapTileY), 8, "0") ;Unique identifier ;Check if the image exists \nImage = GetTileFromMem(\key) If \nImage = -1 ;If not, load it in the background - If AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache *only* in this loop + If AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache, once in this loop \GetImageThread = CreateThread(@GetImageThread(), *NewTile) PBMap\TilesThreads()\GetImageThread = \GetImageThread MyDebug(" Creating get image thread nb " + Str(\GetImageThread), 2) @@ -583,7 +585,7 @@ Module PBMap EndIf Next Next - ;Free tile memory + ;Free tile memory ;TODO : get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() ;Check if there's no more loading thread @@ -617,7 +619,7 @@ Module PBMap ; DeleteMapElement(PBMap\MemCache\Images()) ; EndIf ; Next - + EndProcedure Procedure DrawPointer(*Drawing.DrawingParameters) @@ -771,7 +773,9 @@ Module PBMap VectorSourceColor(RGBA(0, 0, 0, 80)) MovePathCursor(50,50) DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) - StopVectorDrawing() + MovePathCursor(50,60) + DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) + StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while LockMutex(*SharedDrawing\Mutex) ;Be sure that we're not modifying variables while moving (seems not useful, but it is, especially to clean the semaphore) @@ -784,7 +788,7 @@ Module PBMap Repeat : Until TrySemaphore(*SharedDrawing\Semaphore) = 0 EndIf UnlockMutex(*SharedDrawing\Mutex) - Until Drawing\End + Until Drawing\End EndProcedure Procedure Refresh() @@ -1187,8 +1191,6 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) ; ExecutableFormat = Console -; CursorPosition = 445 -; FirstLine = 442 ; Folding = --------- ; EnableThread ; EnableXP From 5fc43503e05c9fe29ab787eec764b769ae83e0c0 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 25 Aug 2016 13:51:34 +0200 Subject: [PATCH 7/8] Better quit handling --- PBMap.pb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index b069392..e94fce6 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -25,7 +25,7 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos - Global Verbose = #True + Global Verbose = #false Global MyDebugLevel = 3 Global Proxy = #False Declare InitPBMap() @@ -292,18 +292,19 @@ Module PBMap ;Ask main drawing thread to stop and wait for it (nicer than KillThread(PBMap\MainDrawingThread)) LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\End = #True - ;KillThread(PBMap\MainDrawingThread) UnlockMutex(PBMap\Drawing\Mutex) Repeat : Until Not IsThread(PBMap\MainDrawingThread) - ;wait for loading threads to finish nicely - ResetList(PBMap\TilesThreads()) - While NextElement(PBMap\TilesThreads()) - If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - FreeMemory(PBMap\TilesThreads()\Tile) - DeleteElement(PBMap\TilesThreads()) - ResetList( PBMap\TilesThreads()) - EndIf - Wend + ;Wait for loading threads to finish nicely + Repeat + ResetList(PBMap\TilesThreads()) + While NextElement(PBMap\TilesThreads()) + If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 + FreeMemory(PBMap\TilesThreads()\Tile) + DeleteElement(PBMap\TilesThreads()) + EndIf + Wend + Delay(20) + Until ListSize(PBMap\TilesThreads()) = 0 curl_global_cleanup() EndProcedure @@ -1191,6 +1192,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) ; ExecutableFormat = Console +; CursorPosition = 305 +; FirstLine = 278 ; Folding = --------- ; EnableThread ; EnableXP From 836e3b70bb13a3d18a69eff0b7fd99196945592d Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 25 Aug 2016 17:55:39 +0200 Subject: [PATCH 8/8] Tracking infamous GetTileFromHDD bug hanging sometimes don't know why. --- PBMap.pb | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index e94fce6..32ba62b 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -25,7 +25,7 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos - Global Verbose = #false + Global Verbose = #False Global MyDebugLevel = 3 Global Proxy = #False Declare InitPBMap() @@ -156,7 +156,9 @@ Module PBMap Options.option ; EndStructure + ;-Global variables Global PBMap.PBMap, Null.i + Global LoadMutexTest = CreateMutex() ;Shows an error msg and terminates the program Procedure Error(msg.s) @@ -168,6 +170,7 @@ Module PBMap Procedure MyDebug(msg.s, DbgLevel = 0) If Verbose And MyDebugLevel >= DbgLevel PrintN(msg) + ;Debug msg EndIf EndProcedure @@ -289,21 +292,33 @@ Module PBMap EndProcedure Procedure Quit() - ;Ask main drawing thread to stop and wait for it (nicer than KillThread(PBMap\MainDrawingThread)) + Protected TimeCounter.q + ;Ask main drawing thread to stop and wait for it for 2 seconds LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\End = #True - UnlockMutex(PBMap\Drawing\Mutex) - Repeat : Until Not IsThread(PBMap\MainDrawingThread) - ;Wait for loading threads to finish nicely + UnlockMutex(PBMap\Drawing\Mutex) + TimeCounter = ElapsedMilliseconds() + Repeat + Delay(100) + If ElapsedMilliseconds() - TimeCounter > 2000 + ;Should not occur + KillThread(PBMap\MainDrawingThread) + EndIf + Until Not IsThread(PBMap\MainDrawingThread) + ;Wait for loading threads to finish nicely. Passed 2 seconds, kills them. + TimeCounter = ElapsedMilliseconds() Repeat ResetList(PBMap\TilesThreads()) While NextElement(PBMap\TilesThreads()) If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 FreeMemory(PBMap\TilesThreads()\Tile) DeleteElement(PBMap\TilesThreads()) + ElseIf ElapsedMilliseconds() - TimeCounter > 2000 + ;Should not occur + KillThread(PBMap\TilesThreads()\GetImageThread) EndIf Wend - Delay(20) + Delay(10) Until ListSize(PBMap\TilesThreads()) = 0 curl_global_cleanup() EndProcedure @@ -434,15 +449,22 @@ Module PBMap EndProcedure Procedure.i GetTileFromHDD(CacheFile.s) - Protected nImage.i + Protected nImage.i + Debug "Loading image " + CacheFile + " ; Size : " + Str(FileSize(CacheFile)) If FileSize(CacheFile) > 0 nImage = LoadImage(#PB_Any, CacheFile) If IsImage(nImage) - MyDebug("Loadimage " + CacheFile + " -> Success !", 3) + Debug "Success loading " + CacheFile + " as nImage " + Str(nImage) + MyDebug("Success loading " + CacheFile + " as nImage " + Str(nImage), 3) ProcedureReturn nImage + Else + Debug "Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !" + MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) EndIf + Else + Debug "Failed loading " + CacheFile + " -> Size = 0" + MyDebug("Failed loading " + CacheFile + " -> Size = 0", 3) EndIf - MyDebug("Loadimage " + CacheFile + " -> Failed !", 3) ProcedureReturn -1 EndProcedure @@ -485,7 +507,9 @@ Module PBMap Protected Tile.position ; PBMap\MemCache\Images(*Tile\key)\Mutex = CreateMutex() ; LockMutex(PBMap\MemCache\Images(*Tile\key)\Mutex) +; LockMutex(LoadMutexTest) nImage = GetTileFromHDD(CacheFile) +; UnlockMutex(LoadMutexTest) If nImage = -1 nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) If nImage = -1 @@ -502,7 +526,7 @@ Module PBMap MyDebug("With the following key : " + *Tile\key, 3) ;Define this tile image nb *Tile\nImage = nImage -; UnlockMutex(PBMap\MemCache\Images(key)\Mutex) + ; UnlockMutex(PBMap\MemCache\Images(key)\Mutex) EndProcedure Procedure DrawTile(*Tile.Tile) @@ -570,6 +594,7 @@ Module PBMap MyDebug(" Creating get image thread nb " + Str(\GetImageThread), 2) Else MyDebug(" Can't add a new image to the map list", 2) + CallDebugger EndIf EndIf If IsImage(\nImage) @@ -774,7 +799,7 @@ Module PBMap VectorSourceColor(RGBA(0, 0, 0, 80)) MovePathCursor(50,50) DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) - MovePathCursor(50,60) + MovePathCursor(50,80) DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) StopVectorDrawing() ;Redraw @@ -1191,9 +1216,8 @@ CompilerIf #PB_Compiler_IsMainFile EndIf CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; ExecutableFormat = Console -; CursorPosition = 305 -; FirstLine = 278 +; CursorPosition = 450 +; FirstLine = 444 ; Folding = --------- ; EnableThread ; EnableXP