Old windows-code does not work on mac

Advanced game related topics
User avatar
thinkitsimple
User
User
Posts: 89
Joined: Mon Aug 13, 2012 6:12 pm
Location: Berlin, Germany
Contact:

Old windows-code does not work on mac

Post by thinkitsimple »

Hi,

i have some old code, which i used on Windows a few years ago. Now i want to go on with this little project, but meanwhile i use a Mac instead of Windows. I looks like there is maybe something Windows-specific code in there, which does not work on my Mac.

What happens if i start the code? The Window is opening but nothing in there, no sprites at all. The sound-file is playing.

Any Mac-user out there who can try my code?

Code: Select all

; Project   : Dungeon
; Filename  : dungeon.pb

AppPath.s = GetPathPart(ProgramFilename())
Enumeration
; Sprites
#Rasen
#Wasser
#Spieler1
#Spieler2
; Sounds
#Musik1
EndEnumeration

#ScreenWidth = 1024
#ScreenHeight = 768
#MapWidth= 15
#MapHeight = 20

; Unterstützung für Bildformat PNG
UsePNGImageDecoder()

; Unterstützung für Soundformat OGG
UseOGGSoundDecoder()

; Sprite und Tastatur initialisieren
If InitSprite() = 0 : MessageRequester("Fehler", "Sprite konnte nicht initialisiert werden!") : End : EndIf 
If InitKeyboard() = 0 : MessageRequester("Fehler", "Tastatur konnte nicht initialisiert werden!") : End : EndIf 

; Geschwindigkeitskonstante 
#SPEED = 2 

; Diese Funktion prüft ob eine Kollision vorliegt 
Procedure Collision(x.f, y.f) 
  Result = 0 
; Gehe alle Tiles durch 
  Restore Map   
  For my = 0 To #MapWidth - 1
    For mx = 0 To #MapHeight - 1
      a.b 
      Read .b a 
;     Wenn der Tile-Typ 1(#Rasen) ist, so prüfe auf eine Kollision mit diesem Tile... 
      If a = 1
;       ... mit den entsprechenden Tile-Koordinaten(IndexX * TileBreite, IndexY * TileBreite)... 
;       ... und den entsprechenden Spieler-Koordinaten(SpielerX, SpielerY)... 
        If SpriteCollision(#Rasen, Int(x), Int(y), #Spieler1, mx * 50, my * 50) 
;         Wenn ja: gib 1(True) zurück 
          Result = 1 
;         und springe aus beiden Schleifen(Anzahl: 2) raus 
          Break 2 
        EndIf 
      EndIf      
    Next 
  Next 
  
  ProcedureReturn Result 
EndProcedure 

; Screen öffnen
If OpenWindow(0, 0, 0, 340, 285, "Dungeon!", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)

  If OpenWindowedScreen(WindowID(0), 10, 70, 320, 200, 0, 0, 0)
    LoadSprite(#Spieler1, "res/michael_kopf.png")
  Else
    MessageRequester("Error", "Can't open windowed screen!", 0)
    End
  EndIf

EndIf

; Sprites laden
LoadSprite(#Rasen, "res/rasen.png")
LoadSprite(#Wasser,"res/wasser.png")
LoadSprite(#Spieler1,"res/michael_kopf.png")
LoadSprite(#Spieler2,"res/alexandra_kopf.png")

; Spielerposition in der Mitte des Bildschirms 
X.f = #ScreenWidth / 2 
Y.f = #ScreenHeight / 2 

; Framerate auf 30 setzen
SetFrameRate(30)

; Hintergrundmusik laden und endlos abspielen 
If InitSound() = 0 : MessageRequester("Fehler", "Soundwiedergabe konnte nicht initialisiert werden!") : End : EndIf

LoadSound(#Musik1, "res/background.ogg")
PlaySound(#Musik1, 1)

; Hauptschleife 
Repeat 
; Keyboardstatus erneuern und abfragen 
  ExamineKeyboard() 
  
  If KeyboardPushed(#PB_Key_Up)     ; laufe hoch 
    If Collision(X, Y - #SPEED) = 0 
      Y - #SPEED 
    EndIf 
  EndIf 
  If KeyboardPushed(#PB_Key_Down)   ; laufe runter 
    If Collision(X, Y + #SPEED) = 0 
      Y + #SPEED 
    EndIf 
  EndIf 
  If KeyboardPushed(#PB_Key_Left)   ; laufe links 
    If Collision(X - #SPEED, Y) = 0 
      X - #SPEED 
    EndIf 
  EndIf 
  If KeyboardPushed(#PB_Key_Right)  ; laufe rechts 
    If Collision(X + #SPEED, Y) = 0 
      X + #SPEED 
    EndIf 
  EndIf 
  
; Beenden? 
  If KeyboardPushed(#PB_Key_Escape) 
    Quit = 1 
  EndIf 
  
; Screeninhalt löschen und mit Schwarz überdecken 
  ClearScreen(0)
   
; Spieler anzeigen 
	DisplaySprite(#Spieler1, X, Y) 

; Mapdaten durchgehen 
  Restore Map 
  For my = 0 To #MapWidth - 1
    For mx = 0 To #MapHeight - 1
      a.b 
      Read.b a 
      Select a
      	Case 1 ; Rasen
      	  DisplaySprite(#Rasen, mx * 50, my * 50)
      	Case 2 ; Wasser
      	  DisplaySprite(#Wasser, mx * 50, my * 50)
      	Case 3 ; Spieler2
      	  DisplaySprite(#Spieler2, mx * 50, my * 50)
      EndSelect
    Next 
  Next
   
; Gefüllten Backbuffer zum Frontbuffer bringen 
; FlipBuffers() 
  
; Eine kleine Verzögerung um den Prozessor nicht vollständig auszulasten 
  Delay(5) 
Until Quit = 1 
End 

; Mapdaten 
DataSection 
  Map: 
  Data.b 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 
  Data.b 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
  Data.b 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 
  Data.b 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 
  Data.b 1, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 
  Data.b 1, 0, 0, 2, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 
  Data.b 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
  Data.b 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 
  Data.b 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 
  Data.b 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 
  Data.b 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
  Data.b 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
  Data.b 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 
  Data.b 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
  Data.b 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 
EndDataSection
Thanks

Michael
Michael

PureBasic 5.51, macOS 10.12.2 Sierra
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Old windows-code does not work on mac

Post by Danilo »

thinkitsimple wrote:What happens if i start the code? The Window is opening but nothing in there, no sprites at all. The sound-file is playing.
You out-commented FlipBuffers(), so screen buffers do not switch. Also, you need to check
all incoming WindowEvent() in windowed screen mode. Can't test here without the sprites.
User avatar
thinkitsimple
User
User
Posts: 89
Joined: Mon Aug 13, 2012 6:12 pm
Location: Berlin, Germany
Contact:

Re: Old windows-code does not work on mac

Post by thinkitsimple »

Hi Danilo,

found the FlipBuffers() by myself, already tested the code without uncommenting this. Same result, no sprites are shown.

Strange enough, after changing the screen-width and -height of the window, some (rasen, wasser, michael_kopf), but not all (alexandra_kopf) sprites are shown.

How do i check the incoming Window Events?

I have uploaded the sprites and sound to this location: http://www.thinkitsimple.de/public/project/dungeon.zip.

Thanks for helping.
Michael

PureBasic 5.51, macOS 10.12.2 Sierra
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Old windows-code does not work on mac

Post by Danilo »

thinkitsimple wrote:How do i check the incoming Window Events?
Replace the Delay(5) with:

Code: Select all

; Eine kleine Verzögerung um den Prozessor nicht vollständig auszulasten
Delay(5)
Repeat
    event = WindowEvent()
    If event = #PB_Event_CloseWindow
        Quit = 1
    EndIf
Until event = 0
thinkitsimple wrote:Strange enough, after changing the screen-width and -height of the window, some (rasen, wasser, michael_kopf), but not all (alexandra_kopf) sprites are shown.
Map value 3 is outside of the screen here. When I place a 3 somewhere else, it works fine.


EDIT:

Code: Select all

; Project   : Dungeon
; Filename  : dungeon.pb
; Copyright : 2007-2014 Michael Bzdega
; Comment   : A tile-based 2-D Adventure-Game

AppPath.s = GetPathPart(ProgramFilename())
Enumeration
    ; Sprites
    #Rasen
    #Wasser
    #Spieler1
    #Spieler2
    ; Sounds
    #Musik1
EndEnumeration

#ScreenWidth = 800
#ScreenHeight = 600
#MapWidth= 15
#MapHeight = 20

; Unterstützung für Bildformat PNG
UsePNGImageDecoder()

; Unterstützung für Soundformat OGG
UseOGGSoundDecoder()

; Sprite und Tastatur initialisieren
If InitSprite() = 0 : MessageRequester("Fehler", "Sprite konnte nicht initialisiert werden!") : End : EndIf 
If InitKeyboard() = 0 : MessageRequester("Fehler", "Tastatur konnte nicht initialisiert werden!") : End : EndIf 

; Geschwindigkeitskonstante 
#SPEED = 2 

; Diese Funktion prüft ob eine Kollision vorliegt 
Procedure Collision(x.f, y.f) 
    Result = 0 
    ; Gehe alle Tiles durch 
    Restore Map   
    For my = 0 To #MapWidth - 1
        For mx = 0 To #MapHeight - 1
            a.b 
            Read .b a 
            ;     Wenn der Tile-Typ 1(#Rasen) ist, so prüfe auf eine Kollision mit diesem Tile... 
            If a = 1
                ;       ... mit den entsprechenden Tile-Koordinaten(IndexX * TileBreite, IndexY * TileBreite)... 
                ;       ... und den entsprechenden Spieler-Koordinaten(SpielerX, SpielerY)... 
                If SpriteCollision(#Rasen, Int(x), Int(y), #Spieler1, mx * 50, my * 50) 
                    ;         Wenn ja: gib 1 (True) zurück 
                    Result = 1 
                    ;         und springe aus beiden Schleifen (Anzahl: 2) raus 
                    Break 2 
                EndIf 
            EndIf      
        Next 
    Next 
    
    ProcedureReturn Result 
EndProcedure 

; Screen öffnen
If OpenWindow(0, 0, 0, #ScreenWidth, #ScreenHeight, "Dungeon!", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
    
    If OpenWindowedScreen(WindowID(0), 0, 0, #ScreenWidth, #ScreenHeight, 0, 0, 0)
        LoadSprite(#Spieler1, "res/michael_kopf.png")
    Else
        MessageRequester("Error", "Can't open windowed screen!", 0)
        End
    EndIf
    
EndIf

; Sprites laden
LoadSprite(#Rasen, "res/rasen.png")
LoadSprite(#Wasser,"res/wasser.png")
LoadSprite(#Spieler1,"res/michael_kopf.png")
LoadSprite(#Spieler2,"res/alexandra_kopf.png")

; Spielerposition in der Mitte des Bildschirms 
X.f = #ScreenWidth / 2 
Y.f = #ScreenHeight / 2 

; Framerate auf 30 setzen
SetFrameRate(30)

; Hintergrundmusik laden und endlos abspielen 
If InitSound() = 0 : MessageRequester("Fehler", "Soundwiedergabe konnte nicht initialisiert werden!") : End : EndIf

LoadSound(#Musik1, "res/background.ogg")
PlaySound(#Musik1, 1)

; Hauptschleife 
Repeat 
    ; Keyboardstatus erneuern und abfragen 
    ExamineKeyboard() 
    
    If KeyboardPushed(#PB_Key_Up)     ; laufe hoch 
        If Collision(X, Y - #SPEED) = 0 
            Y - #SPEED 
        EndIf 
    EndIf 
    If KeyboardPushed(#PB_Key_Down)   ; laufe runter 
        If Collision(X, Y + #SPEED) = 0 
            Y + #SPEED 
        EndIf 
    EndIf 
    If KeyboardPushed(#PB_Key_Left)   ; laufe links 
        If Collision(X - #SPEED, Y) = 0 
            X - #SPEED 
        EndIf 
    EndIf 
    If KeyboardPushed(#PB_Key_Right)  ; laufe rechts 
        If Collision(X + #SPEED, Y) = 0 
            X + #SPEED 
        EndIf 
    EndIf 
    
    ; Beenden? 
    If KeyboardPushed(#PB_Key_Escape) 
        Quit = 1 
    EndIf 
    
    ; Screeninhalt löschen und mit Schwarz überdecken 
    ClearScreen(0)
    
    ; Spieler anzeigen 
    DisplaySprite(#Spieler1, X, Y) 
    
    ; Mapdaten durchgehen 
    Restore Map 
    For my = 0 To #MapWidth - 1
        For mx = 0 To #MapHeight - 1
            a.b 
            Read.b a 
            Select a
                Case 1 ; Rasen
                    DisplaySprite(#Rasen, mx * 50, my * 50)
                Case 2 ; Wasser
                    DisplaySprite(#Wasser, mx * 50, my * 50)
                Case 3 ; Spieler2
                    DisplaySprite(#Spieler2, mx * 50, my * 50)
            EndSelect
        Next 
    Next
    
    ; Gefüllten Backbuffer zum Frontbuffer bringen 
    FlipBuffers() 
    
    ; Eine kleine Verzögerung um den Prozessor nicht vollständig auszulasten
    Delay(5)
    ; Events abfragen
    Repeat
        event = WindowEvent()
        If event = #PB_Event_CloseWindow
            Quit = 1
        EndIf
    Until event = 0
    
Until Quit = 1 
End 

; Mapdaten 
DataSection 
    Map: 
    Data.b 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 
    Data.b 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
    Data.b 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 
    Data.b 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 
    Data.b 1, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 
    Data.b 1, 0, 0, 2, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 
    Data.b 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
    Data.b 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 
    Data.b 1, 0, 0, 3, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 
    Data.b 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 
    Data.b 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
    Data.b 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
    Data.b 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 
    Data.b 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 
    Data.b 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 
EndDataSection
With the following values you see the whole map:

Code: Select all

#ScreenWidth = 1000
#ScreenHeight = 750
User avatar
thinkitsimple
User
User
Posts: 89
Joined: Mon Aug 13, 2012 6:12 pm
Location: Berlin, Germany
Contact:

Re: Old windows-code does not work on mac

Post by thinkitsimple »

Hi Danilo,

thanks, this works as it should. Very nice, now i can go and make a bit more out this.

Maybe you can help in another small problem?

When i run this in the IDE, all runs fine now. But when i compile it as mac app, the sprites and sound seems not be be included in the app. It opens a black window, no sprites, no music, no error-message.

How can i include these resources within the compiled app? Or is this a matter of path-definition? How do i set the file-pathes, to run the code within the IDE and as compiled app with a res-folder nearby?
Michael

PureBasic 5.51, macOS 10.12.2 Sierra
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Old windows-code does not work on mac

Post by TI-994A »

thinkitsimple wrote:...When i run this in the IDE, all runs fine now. But when i compile it as mac app, the sprites and sound seems not be be included in the app...
Hi thinkitsimple. Just make sure that the res folder which contains the michael_kopf.png, rasen.png, wasser.png, background.ogg, etc. files is in the same directory as the compiled app.
thinkitsimple wrote:How can i include these resources within the compiled app?...
For this, you could either use resource files, or simply include the resources within the application itself, using the CatchImage(), CatchSprite(), CatchMusic() and CatchSound() functions.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Old windows-code does not work on mac

Post by Danilo »

thinkitsimple wrote:When i run this in the IDE, all runs fine now. But when i compile it as mac app, the sprites and sound seems not be be included in the app. It opens a black window, no sprites, no music, no error-message.
Your .app is a directory/package only. Right-click the app and choose "Paketinhalt anzeigen" (german Mac OS X).

Copy your 'res' folder to Dungeon.app/Contents/, so you get Dungeon.app/Contents/res/background.ogg etc.
"Contents" is the main directory within your .app
User avatar
thinkitsimple
User
User
Posts: 89
Joined: Mon Aug 13, 2012 6:12 pm
Location: Berlin, Germany
Contact:

Re: Old windows-code does not work on mac

Post by thinkitsimple »

Danilo wrote:Your .app is a directory/package only. Right-click the app and choose "Paketinhalt anzeigen" (german Mac OS X).

Copy your 'res' folder to Dungeon.app/Contents/, so you get Dungeon.app/Contents/res/background.ogg etc.
"Contents" is the main directory within your .app
Thanks, this solves my problem.
Michael

PureBasic 5.51, macOS 10.12.2 Sierra
Post Reply