Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » OpenLayer cannot load bitmaps

Credits go to Fladimir da Gorf, Thomas Fjellstrom, and Thomas Harte for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
OpenLayer cannot load bitmaps
Vanneto
Member #8,643
May 2007

Well, not exactly true, but mostly. The problem is, my compiler/IDE ( dont know what ) tends to change my paths from this:

bitmap.Load("gfx/image.png");

To something else, as the image wont load. But if I do it like this:

string exe_path = Setup::GetExecutablePath();
bitmap.Load((exe_path + "gfx/image.png").c_str());

It works. Just one problem, if there is a non-ASCI character in the path, for example, ŠČĆŽĐ, the image wont be loaded and the program will fail.

Can this be fixed whitout looking into OpenLayer's code?

Thanks!

In capitalist America bank robs you.

Thomas Harte
Member #33
April 2000
avatar

Quote:

The problem is, my compiler/IDE ( dont know what ) tends to change my paths from this:

It'll be your IDE, either not making the current working directory the same as the directory from which your binary is running. And, of course, the rest of the problem seems to be that OpenLayer only accepts an ASCII string for a path name.

With that in mind, I guess the solution is to find a UNICODE version of the raw-C function setcwd, use it to do something like setcwd(Setup::GetExecutablePath());

I'm not much help beyond that though. Sorry!

Thomas Fjellstrom
Member #476
June 2000
avatar

Also, using the exe path only works as long as you only want to read files, once you want to write to them, make sure your app isn't installed in some system global place with limited permissions (or use the AppData dir..).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

(exe_path + "gfx/image.png").c_str()

That isn't needed (the exe_path thing), since OL always does that for you. But it's true that AppData would be a better idea.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

LordHolNapul
Member #3,619
June 2003
avatar

Wow! Flad inverted the colours! Superb! :)

Thomas Harte
Member #33
April 2000
avatar

Quote:

That isn't needed (the exe_path thing), since OL always does that for you.

Is it safe to assume you're not idiotically trying to do the exact same thing under OS X?

As per Appdata, etc, I don't think that Vanneto has in any way implied that he intends to do more than load these files.

Fladimir da Gorf
Member #1,565
October 2001
avatar

If you use a relative directory, unfortunately it is always using the executable path. When I made that I didn't know it wouldn't work in OS X... But if you provide an absolute path, nothing is made behind the scenes.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Vanneto
Member #8,643
May 2007

Doesent the IDE change this:

bitmap.Load("gfx/bitmap.png");

So that it loads from the project root directory? For example, I have the .exe in bin/Debug. Isnt the root path in ../../ so It would be:

../../gfx/bitmap.png

?

In capitalist America bank robs you.

Fladimir da Gorf
Member #1,565
October 2001
avatar

No, it does exactly what your code does.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Thomas Harte
Member #33
April 2000
avatar

Quote:

If you use a relative directory, unfortunately it is always using the executable path. When I made that I didn't know it wouldn't work in OS X...

It could work, it's just quite against the normal application bundle structure and much less easy to set up as an automated part of the build system in Xcode.

I guess the best solution, if you're looking for one, is to explicitly implement Setup::GetResourcePath(), make it return the same as Setup::GetExecutablePath() under Windows/Linux/whatever but have it return the path to the resources part of the application bundle under OS X, if the application is so packaged.

That tiny bit of code is probably quite trivial to implement, depending on how you're currently jumping between Objective-C and C++ in OpenLayer, and I'd be more than willing to help.

Vanneto
Member #8,643
May 2007

Just one more question, this is probably due to my ununderstanding of C++, but anyway. How come this works:

Bitmap *bmp = new Bitmap("bitmap.png");
bmp->Blit(x, y);
delete bmp;

And this doesent:

Bitmap bmp("bitmap.png");
bmp.Blit(x, y);
bmp = NULL;

(It crashes).

In capitalist America bank robs you.

Evert
Member #794
November 2000
avatar

Quote:

When I made that I didn't know it wouldn't work in OS X...

It's not the normal way to store binaries and their data in UNIX either.

Vanneto
Member #8,643
May 2007

Ok really just one more question. How can I change the way OpenLayer rotates bitmpas? Currently, it rotates the bitmaps around the upper-left corner, and if I rotate my bitmap, it seems higher... Like this:

BEFORE    AFTER
        ___
 __     \  \ 
|  |     __\
|__|
------------------

In allegro, this wasnt a problem. So I dont know why its like this here. Im sure im missing something. Am I?

Thank you.

In capitalist America bank robs you.

Fladimir da Gorf
Member #1,565
October 2001
avatar

SetRotationPivot? Also "bmp = NULL;" shouldn't compile, as NULL is a pointer, but bmp is not.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Vanneto
Member #8,643
May 2007

Yeah it compiled. But it crashed. Removed that bit and it works. Damn I need to watch for this kind of errors!

BTW: Isnt there an easier way then SetRotationPivot? Just seems a lot of work setting it and then back to screen for one Blit(). :P

In capitalist America bank robs you.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

What's so hard about setting it once for each object? Especially if you've got 'Blit' as a method in your class? Just find the object's center , store it in a data member and call SetRotationPivot(center) at the beginning of the Blit function.

As far as your second to last post why "new Bitmap("bitmap.png")" works when "bmp("bitmap.png")" doesn't , I don't know. That's really strange since they should both be using the same constructor. What do your Bitmap constructors look like? If you wrote your own constructor then the compiler takes away the default constructor - Bitmap::Bitmap() . I don't know if that has anything to do with it though.

Fladimir da Gorf
Member #1,565
October 2001
avatar

Don't you always rotate the same bitmap around the same point? Then you'll need to set the rotation pivot only once.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Only if it doesn't move. If you mean the center point's relative position from a corner well yeah , that's always the same.

I didn't mean recalculate the center every time the Blit function was called. Just because I said it all in the same sentence doesn't mean I meant do it all right together.

And yes if you have more than one 'Bitmap' object that is being displayed then you'll have to call SetRotationPivot for each one that is rotated otherwise they'll be using one pivot for all of the different 'Bitmap's. :)

Audric
Member #907
January 2001

> Bitmap bmp("bitmap.png"); // It crashes

The most common cause for crash-on-bitmap-loading is when people attempt to read bitmaps before Allegro is initialized(*). For example, if bmp is a global variable, or a member of a static class, or a variable inside the constructor of a static class : The code would then be executed before main(), and nicely crash.

(*) (setting color depth and graphics mode is also a good idea)

Vanneto
Member #8,643
May 2007

Does Transforms::SetRotationPivot() affect only one bitmap? How does it work? BTW, currently I have it like this:

1void Player::draw()
2{
3 // Set the rotation pivot
4 center_x = x + image->Width()/2;
5 center_y = y + image->Height()/2;
6 
7 Transforms::SetRotationPivot(center_x, center_y);
8 
9 static int angle;
10 if(moving == 0) angle = 0;
11 else if(moving == 1) angle = 35;
12 else if(moving == 2) angle = -35;
13 
14 if(moving > 0) image->BlitRotated(x, y, angle);
15 else image->Blit(x, y);
16 moving = 0;
17}

But it still places it above the ground when rotated. Am I doing something wrong?

In capitalist America bank robs you.

Fladimir da Gorf
Member #1,565
October 2001
avatar

Ah, what I mean is to use bmp.SetDefaultPivot. That'll apply only for the specific Bitmap. The pivot in Transforms only applies to Transforms::RotateBy.

Quote:

The most common cause for crash-on-bitmap-loading is when people attempt to read bitmaps before Allegro is initialized(*). For example, if bmp is a global variable, or a member of a static class, or a variable inside the constructor of a static class : The code would then be executed before main(), and nicely crash.

OL has an inner mechanism that delays the loading of global Bitmaps automatically until the graphics mode is set.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Vanneto
Member #8,643
May 2007

Thanks for replying. But this just doesent work:

1void Player::draw()
2{
3 // Set the rotation pivot
4 center_x = x + (image->Width()/2);
5 center_y = y + (image->Height()/2);
6 
7 image->SetDefaultPivot(Vec2D(center_x, center_y));
8 
9 static float angle;
10 if(moving == 0) angle = 0;
11 else if(moving == 1) angle = deg(35.0);
12 else if(moving == 2) angle = deg(-35.0);
13 
14 if(moving > 0) image->BlitRotated(x, y, angle);
15 else image->Blit(x, y);
16 moving = 0;
17}

I attached the binary so you can see what it does. When you get to the black screen, that is the "menu". Just press S to go to the playing part. Move with A and D.

Thanks!

P.S. deg() turns degress in radians. I tried whitout it and it doesent work too.

In capitalist America bank robs you.

Fladimir da Gorf
Member #1,565
October 2001
avatar

At least, take out "x + " and "y + " there, the pivot is only relative to the image itself, not the screen (otherwise, it wouldn't make much sense).

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Vanneto
Member #8,643
May 2007

Im doing this now in the init:

void Player::init(Bitmap *im)
{
    image = im;

    // Set the rotation pivot
    center_x = (image->Width()/2);
    center_y = (image->Height()/2);

    image->SetDefaultPivot(Vec2D(center_x, center_y));
}

And this in the draw() function:

void Player::draw()
{
    static float angle;
    if(moving == 0) angle = 0;
    else if(moving == 1) angle = deg(0.4);
    else if(moving == 2) angle = deg(-0.4);

    if(moving > 0) image->BlitRotated(x, y, angle);
    else image->Blit(x, y);
    moving = 0;
}

But it still gets lifted... Take a look:

Normal:
{"name":"593818","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/62cb871a29fdd5d46f1c8a3650315acf.png","w":1024,"h":768,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/62cb871a29fdd5d46f1c8a3650315acf"}593818

And "running":
{"name":"593819","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/5\/b5bcf7e209aacc0dd7722070b14ae33d.png","w":1024,"h":768,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/5\/b5bcf7e209aacc0dd7722070b14ae33d"}593819

I really would like to use OpenLayer because of its coolness. But I just need to get this fixed.

Thanks!

In capitalist America bank robs you.

Fladimir da Gorf
Member #1,565
October 2001
avatar

OK, which version are you using? 2.0 or 2.1?

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

 1   2 


Go to: