Recent

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

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Ok, i starting create my first "full" game using Free Pascal and SDL2 library. The game ever worked in my device Windows 8.1 so i tried to run it on Windows 10 ( an other pc i have ) and it doesn't draws the Images in the screen. Then i added the function that if the texture can't be loaded, the program will stop ( HALT ). and so the game stops when running it on that device. Please Help.. !!
« Last Edit: September 19, 2016, 03:18:09 pm by Manu12x »
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7494
Re: Images not found when starting SDL2 game in an other computer
« Reply #1 on: September 07, 2016, 05:48:47 pm »
Try starting the .EXE from a console, and change to its directory first. See if you notice anything.

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 #2 on: September 07, 2016, 10:07:23 pm »
Try starting the .EXE from a console, and change to its directory first. See if you notice anything.
Ehm i don't understand "exe from a console".. I simply run the exe file in his folder where there is the resources folder.. In my computer it works.. In an other not.. I don' t know why..
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

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 #3 on: September 07, 2016, 10:11:12 pm »
Try starting the .EXE from a console, and change to its directory first. See if you notice anything.
My game is here http:/manueldicriscito.altervista.org/DinoLand.zip
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

lainz

  • Hero Member
  • *****
  • Posts: 3270
    • Lainz
Re: Images not found when starting SDL2 game in an other computer
« Reply #4 on: September 07, 2016, 10:21:46 pm »
What paths are you using in your game?

Handoko

  • Hero Member
  • *****
  • Posts: 3184
  • My goal: build my own game engine using Lazarus
Re: Images not found when starting SDL2 game in an other computer
« Reply #5 on: September 08, 2016, 08:35:52 am »
@Manu12x

I think the same as lainz thought, you didn't set the path to the the texture files correctly. It often happens if you hard code the path. For example:

Code: [Select]
  sdlDinoTexture := IMG_LoadTexture(R, C:\DinoLand\Resources\dino.png');
The code above can be compiled without error. But it is not portable, it usually will cause error if you copy the exe file and run the it from other computers.

There are several solutions to fix the problem:

- Use ParamStr(0);
http://www.freepascal.org/docs-html/rtl/system/paramstr.html

- Use Application.ExeName;
http://www.freepascal.org/docs-html/fcl/custapp/tcustomapplication.exename.html

- Use GetCurrentDir;
http://www.freepascal.org/docs-html/rtl/sysutils/getcurrentdir.html

Example code using ParamStr:
Code: [Select]
var
  sdlDinoTexture: PSDL_Texture;
  sdlEnemyTexture: PSDL_Texture;

procedure LoadAllTextures(R: PSDL_Renderer);
var
  S: string;
begin
  S := ExtractFilePath(ParamStr(0)) + 'Resources' + DirectorySeparator;
  sdlDinoTexture := IMG_LoadTexture(R, S + 'dino.png');
  sdlEnemyTexture := IMG_LoadTexture(R, S + 'enemy.png');
end;
« Last Edit: September 08, 2016, 08:49:49 am by Handoko »

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Images not found when starting SDL2 game in an other computer
« Reply #6 on: September 08, 2016, 08:52:32 am »
Indeed, at least make them so that the user is able to override default values.

Try starting the .EXE from a console, and change to its directory first. See if you notice anything.
Ehm i don't understand "exe from a console".. I simply run the exe file in his folder where there is the resources folder.. In my computer it works.. In an other not.. I don' t know why..
marcov means that you can check things yourself by opening a command prompt (you know that usually black window with a blinking cursor that awaits your commands such as dir, copy, mkdir etc). It sometimes helps if you start your application from inside the directory/folder where the game executable is actually located.

If that actually helped, then you can probably fix that by creating a shortcut that has the startup directory configured correctly (if not then it can be done in code).

afaik that situation is usually the case wehen using relative paths.

But please also have a good look at Handoku's advise, as that accounts for situations where hard-coded paths are being used.
« Last Edit: September 08, 2016, 08:54:42 am by molly »

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 #7 on: September 08, 2016, 11:16:43 am »
@Manu12x

I think the same as lainz thought, you didn't set the path to the the texture files correctly. It often happens if you hard code the path. For example:

Code: [Select]
  sdlDinoTexture := IMG_LoadTexture(R, C:\DinoLand\Resources\dino.png');
The code above can be compiled without error. But it is not portable, it usually will cause error if you copy the exe file and run the it from other computers.

There are several solutions to fix the problem:

- Use ParamStr(0);
http://www.freepascal.org/docs-html/rtl/system/paramstr.html

- Use Application.ExeName;
http://www.freepascal.org/docs-html/fcl/custapp/tcustomapplication.exename.html

- Use GetCurrentDir;
http://www.freepascal.org/docs-html/rtl/sysutils/getcurrentdir.html

Example code using ParamStr:
Code: [Select]
var
  sdlDinoTexture: PSDL_Texture;
  sdlEnemyTexture: PSDL_Texture;

procedure LoadAllTextures(R: PSDL_Renderer);
var
  S: string;
begin
  S := ExtractFilePath(ParamStr(0)) + 'Resources' + DirectorySeparator;
  sdlDinoTexture := IMG_LoadTexture(R, S + 'dino.png');
  sdlEnemyTexture := IMG_LoadTexture(R, S + 'enemy.png');
end;
Ah . no i don't do "C:\...." but i did :
sdlDinoTexture := IMG_LoadTexture( GameRenderer, 'Resources/dino.png');

But ok i will try your code
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

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 #8 on: September 08, 2016, 11:20:04 am »
@Manu12x

I think the same as lainz thought, you didn't set the path to the the texture files correctly. It often happens if you hard code the path. For example:

Code: [Select]
  sdlDinoTexture := IMG_LoadTexture(R, C:\DinoLand\Resources\dino.png');
The code above can be compiled without error. But it is not portable, it usually will cause error if you copy the exe file and run the it from other computers.

There are several solutions to fix the problem:

- Use ParamStr(0);
http://www.freepascal.org/docs-html/rtl/system/paramstr.html

- Use Application.ExeName;
http://www.freepascal.org/docs-html/fcl/custapp/tcustomapplication.exename.html

- Use GetCurrentDir;
http://www.freepascal.org/docs-html/rtl/sysutils/getcurrentdir.html

Example code using ParamStr:
Code: [Select]
var
  sdlDinoTexture: PSDL_Texture;
  sdlEnemyTexture: PSDL_Texture;

procedure LoadAllTextures(R: PSDL_Renderer);
var
  S: string;
begin
  S := ExtractFilePath(ParamStr(0)) + 'Resources' + DirectorySeparator;
  sdlDinoTexture := IMG_LoadTexture(R, S + 'dino.png');
  sdlEnemyTexture := IMG_LoadTexture(R, S + 'enemy.png');
end;
Ah . no i don't do "C:\...." but i did :
sdlDinoTexture := IMG_LoadTexture( GameRenderer, 'Resources/dino.png');

But ok i will try your code
Maybe i should do like i always do with sysutils, assign textfile to a file and so dir is "GetCurrentDir+'\'+'Data/Options.dat'
..?
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

avra

  • Hero Member
  • *****
  • Posts: 1712
    • Additional info
Re: Images not found when starting SDL2 game in an other computer
« Reply #9 on: September 08, 2016, 11:26:49 am »
The game ever worked in my device Windows 8.1 so i tried to run it on Windows 10 ( an other pc i have ) and it doesn't draws the Images in the screen.
Does your game directory on Windows 10 have non-ascii letters in path? Can you try to add code to show problematic path in your game and then compare it to the real path that you can see on your own?
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

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 #10 on: September 08, 2016, 11:38:30 am »
The game ever worked in my device Windows 8.1 so i tried to run it on Windows 10 ( an other pc i have ) and it doesn't draws the Images in the screen.
Does your game directory on Windows 10 have non-ascii letters in path? Can you try to add code to show problematic path in your game and then compare it to the real path that you can see on your own?
I always do this
Code: Pascal  [Select]
  1. Texture := IMG_LoadTexture( GameRenderer, 'Resources/image.png');
  2.  
  3.  
That's wrong? I'll try to fix
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

lainz

  • Hero Member
  • *****
  • Posts: 3270
    • Lainz
Re: Images not found when starting SDL2 game in an other computer
« Reply #11 on: September 08, 2016, 02:24:09 pm »
Try starting the .EXE from a console, and change to its directory first. See if you notice anything.

Doesn't works too.

Maybe i should do like i always do with sysutils, assign textfile to a file and so dir is "GetCurrentDir+'\'+'Data/Options.dat'
..?

I think is better to use ParamStr as Handoko said.

I always do this
Code: Pascal  [Select]
  1. Texture := IMG_LoadTexture( GameRenderer, 'Resources/image.png');
  2.  
  3.  
That's wrong? I'll try to fix

Yes is wrong, else it will be working under Windows 10. Do as Handoko said.

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 #12 on: September 08, 2016, 02:52:34 pm »
Try starting the .EXE from a console, and change to its directory first. See if you notice anything.

Doesn't works too.

Maybe i should do like i always do with sysutils, assign textfile to a file and so dir is "GetCurrentDir+'\'+'Data/Options.dat'
..?

I think is better to use ParamStr as Handoko said.

I always do this
Code: Pascal  [Select]
  1. Texture := IMG_LoadTexture( GameRenderer, 'Resources/image.png');
  2.  
  3.  
That's wrong? I'll try to fix

Yes is wrong, else it will be working under Windows 10. Do as Handoko said.
Ok thanks!
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

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 #13 on: September 10, 2016, 08:21:44 pm »
@Manu12x

I think the same as lainz thought, you didn't set the path to the the texture files correctly. It often happens if you hard code the path. For example:

Code: [Select]
  sdlDinoTexture := IMG_LoadTexture(R, C:\DinoLand\Resources\dino.png');
The code above can be compiled without error. But it is not portable, it usually will cause error if you copy the exe file and run the it from other computers.

There are several solutions to fix the problem:

- Use ParamStr(0);
http://www.freepascal.org/docs-html/rtl/system/paramstr.html

- Use Application.ExeName;
http://www.freepascal.org/docs-html/fcl/custapp/tcustomapplication.exename.html

- Use GetCurrentDir;
http://www.freepascal.org/docs-html/rtl/sysutils/getcurrentdir.html

Example code using ParamStr:
Code: [Select]
var
  sdlDinoTexture: PSDL_Texture;
  sdlEnemyTexture: PSDL_Texture;

procedure LoadAllTextures(R: PSDL_Renderer);
var
  S: string;
begin
  S := ExtractFilePath(ParamStr(0)) + 'Resources' + DirectorySeparator;
  sdlDinoTexture := IMG_LoadTexture(R, S + 'dino.png');
  sdlEnemyTexture := IMG_LoadTexture(R, S + 'enemy.png');
end;
( ExtractFilePath()) Doesn't work.. The game launches but no images in the screen.. Should try GetCurrentDir?
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

lainz

  • Hero Member
  • *****
  • Posts: 3270
    • Lainz
Re: Images not found when starting SDL2 game in an other computer
« Reply #14 on: September 10, 2016, 08:27:55 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';