Recent

Author Topic: [SOLVED] Images not found when starting SDL2 game in an other computer  (Read 21616 times)

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Images not found when starting SDL2 game in an other computer
« Reply #15 on: September 10, 2016, 09:48:21 pm »
What is displayed when you write

Code: Pascal  [Select][+][-]
  1. writeln(ExtractFilePath(ParamStr(0)) + 'Resources\image.png');

And don't use 'Resources/image.png' under windows the path is with 'Resources\image.png'

Or better use PathDelim constant-

Code: Pascal  [Select][+][-]
  1. ExtractFilePath(ParamStr(0)) + 'Resources' + PathDelim + 'image.png';
It displayes the normal directory where there is the image.. Yes i use \ instead of / or i use "DirectorySeparator".. Idk why this doesn't works..
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Images not found when starting SDL2 game in an other computer
« Reply #16 on: September 10, 2016, 11:18:17 pm »
Try this: create a small project that loads and show only a picture, using this code for the path:

Code: Pascal  [Select][+][-]
  1. ExtractFilePath(ParamStr(0)) + 'Resources' + PathDelim + 'image.png';

If that project works fine then you maybe can compare it with your current project to see what is wrong, but if that doesn't works is that the basic code is wrong.

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Images not found when starting SDL2 game in an other computer
« Reply #17 on: September 11, 2016, 12:06:57 am »
Ok that's strange but the texture doesn't load images if this command is in a procedure/function.. If i put it in the "main begin end." It works... Ok that's so strange..
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Images not found when starting SDL2 game in an other computer
« Reply #18 on: September 11, 2016, 12:16:59 am »
You can upload a small code that shows that behaviour?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Images not found when starting SDL2 game in an other computer
« Reply #19 on: September 11, 2016, 12:19:04 am »
Ok that's strange but the texture doesn't load images if this command is in a procedure/function.. If i put it in the "main begin end." It works... Ok that's so strange..
We are talking about a small test project here ?

if the case (and in case possible), step through your code with the debugger and inspect each and every element.

There must be something wrong and it is impossible to solve otherwise as i have not seen any code.

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Images not found when starting SDL2 game in an other computer
« Reply #20 on: September 11, 2016, 02:10:28 pm »
Ok here the code:

Code: Pascal  [Select][+][-]
  1. procedure LoadTexture( T : PSDL_Texture; File : String );
  2.   begin
  3.     DirStr := GetCurrentDir + '\Resources\' + File;
  4.     DirPChar := StrAlloc( Lenght( DirStr ) + 1 );
  5.     If StrPCopy( DirPchar, DirStr ) <> DirPChar then HALT;
  6.     T := IMG_LoadTexture( GameRenderer, DirPchar );
  7.     writeln( DirPChar );
  8.   end;
  9.  
  10. Begin
  11.   ...
  12.   LoadTexture( bg, 'bg.png' );
  13.   ...
  14. End.
  15.  

So black screen and no images shown.
Text shown is "C:\Users\Manu\Desktop\DinoLand\DinoLand Project\Resources\bg.png

But..
If this code was not in a procedure.. The image is shown correctly.. Thanks for replies
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Images not found when starting SDL2 game in an other computer
« Reply #21 on: September 11, 2016, 02:19:32 pm »
Maybe it is something about pass parameter by value vs by reference.

Try:

Code: [Select]
procedure LoadTexture(var T : PSDL_Texture; File : String);
http://wiki.freepascal.org/Variable_parameter


Note:
Also, please change the name "File" to something else, perhaps "strFile". Because File is a reserved word.
http://wiki.freepascal.org/File
« Last Edit: September 11, 2016, 02:30:55 pm by Handoko »

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Images not found when starting SDL2 game in an other computer
« Reply #22 on: September 11, 2016, 02:46:29 pm »
Maybe it is something about pass parameter by value vs by reference.

Try:

Code: [Select]
procedure LoadTexture(var T : PSDL_Texture; File : String);
http://wiki.freepascal.org/Variable_parameter


Note:
Also, please change the name "File" to something else, perhaps "strFile". Because File is a reserved word.
http://wiki.freepascal.org/File
I named it "Dir" but i wrote File only here.. Okay i will see.. I hope that will work
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Images not found when starting SDL2 game in an other computer
« Reply #23 on: September 11, 2016, 05:54:08 pm »
Maybe it is something about pass parameter by value vs by reference.
Since TS is expecting the value to be returned that is 100% for sure the culprit.

As a hint to TS: in case you want to return a single value from a subroutine, please use a function and use the result as return value. That makes it more obvious that a value is to be returned.

However (and yet again how conveniently people with problems omit vital information)
Quote
LoadTexture( bg, 'bg.png' );
What type is bg  ?

In case it is TSDL_Texture then you are providing it the wrong parameterg. In case it is PSDL_Texture, then handoko is correct.
« Last Edit: September 11, 2016, 06:00:06 pm by molly »

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Images not found when starting SDL2 game in an other computer
« Reply #24 on: September 11, 2016, 06:13:07 pm »
A good programmer always name his/her items properly at the beginning. It will save lots of trouble in the future especially when the code grows to thousand of lines.

bg? a very bad name. Also: T, File.

Quote
As a hint to TS: in case you want to return a single value from a subroutine, please use a function and use the result as return value. That makes it more obvious that a value is to be returned.

Originally, I gave example procedure to load all the textures. But he modified it to load a single texture. So yes, it should be better to use a function in this case. The 'better' use case of passing parameter by reference is when the result has more than 1 item.
« Last Edit: September 11, 2016, 06:30:09 pm by Handoko »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Images not found when starting SDL2 game in an other computer
« Reply #25 on: September 11, 2016, 06:29:17 pm »
@Handoko:
Yes, i was just reading it back to see where that routine originated from. Thanks for the heads up.

Ok in that case, the addition of var to that T parameter should be enough as IMG_LoadTexture() return-type is PSDL_Texture.

No idea how that could have worked in the past though  ::)


DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Images not found when starting SDL2 game in an other computer
« Reply #26 on: September 11, 2016, 06:54:54 pm »
@Handoko:
Yes, i was just reading it back to see where that routine originated from. Thanks for the heads up.

Ok in that case, the addition of var to that T parameter should be enough as IMG_LoadTexture() return-type is PSDL_Texture.

No idea how that could have worked in the past though  ::)
Ok.. so.. That's it.

bg (stands for background ) is type of PSDL_Texture and so in the procedure, T ( stands for Texture ) is type of PSDL_Texture, too.
bg.png is a small image 1x1 pixels colored of white.
But.. i don't know the difference between PSDL_Texture and TSDL_Texture but i always write PSDL_Texture.
For this.. I solved with another method..
Code: Pascal  [Select][+][-]
  1.  
  2. bg := IMG_LoadTexture( GameRenderer, FilePath('Resources\bg.png') );
  3.  
  4.  

FilePath() is a function i created :

Code: Pascal  [Select][+][-]
  1. function FilePath( FileName : String ) : PChar;
  2.   var
  3.     DirStr : string;
  4.     DirPChar : PChar;
  5.   begin
  6.     DirStr := GetCurrentDir + '\' + FileName;
  7.     DirPChar := StrAlloc( Length( DirStr ) + 1 );
  8.     if StrPCopy( DirPChar, DirStr ) <> DirPChar then HALT;
  9.     FilePath := DirPChar;
  10.     writeln( DirPChar, ' loaded' );
  11.   end;
  12.  

Should be PChar because syntax is that:

Code: Pascal  [Select][+][-]
  1.  
  2. function IMG_LoadTexture(renderer: PSDL_Renderer; _file: PAnsiChar): PSDL_Texture;
  3.  
  4.  

Tested and so i should do like this..
After fixing this i found another problem loading mp3 files on windows 10 always using FilePath() function. :-[

Code: Pascal  [Select][+][-]
  1. MenuLoop := Mix_LoadMUS( FilePath('Resources\MenuLoop.mp3') );
  2.  

Works on windows 8.1 and doesn't works on windows 10.
Ok so Tuesday there's school.. Computers have windows 7, and i hope for no problems. :o
Thanks again for replies! I appreciate so much them, i will upload my game as soon as possible. ;D
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Images not found when starting SDL2 game in an other computer
« Reply #27 on: September 11, 2016, 07:15:56 pm »
Manu12x, it is good if you have your naming standard. But if you share your codes to others, you need to make it readable for others.

I rarely work in a team and I rarely share my codes. But I always try to name them properly following to common naming standard of others.

For example, identifier started with "T" usually mean "type" of something. Identifier started with "P" is a pointer, just like the one in PSDL_*. I used F, X, Y, Z for looping. But then I change my habit because others usually use i, j, k.

I know you don't know the naming standard of others, but bg is not a good name. These would be more readable: texBackground, texPlayer or sdlBackground, sdlPlayerTexture.

Quote
But.. i don't know the difference between PSDL_Texture and TSDL_Texture but i always write PSDL_Texture.

That's because you haven't learned pointer type variable.

Quote
Works on windows 8.1 and doesn't works on windows 10.
Ok so Tuesday there's school.. Computers have windows 7, and i hope for no problems. :o

Wish you luck.
« Last Edit: September 11, 2016, 07:24:07 pm by Handoko »

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Images not found when starting SDL2 game in an other computer
« Reply #28 on: September 11, 2016, 07:30:34 pm »
in reality.. the texture 'bg' is the single variable with bad name. Texture of dino is named " Dino^.Texture" and so there are much variables like "Dino^.Walking.Up.Step1.Clip"  .
For T i mean Texture and ok i admit i don't know what are the pointers and their utility.. ok i should see. Thanks
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Images not found when starting SDL2 game in an other computer
« Reply #29 on: September 11, 2016, 07:37:07 pm »
You mentioned your FilePath() function doesn't work correctly for playing mp3 files on Windows 10. You may need to start a new tread showing your code and ask others for helps.

I saw some complaints about their codes do not work on Windows 10. But I didn't read their posts (because I don't use Win10). Maybe you can check them for some clues.

 

TinyPortal © 2005-2018