Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added examples/textures_sprite_anim.c #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

IRooc
Copy link

@IRooc IRooc commented Feb 18, 2024

The hardest thing in web still is loading an image and having the width and height available for you in a single function.

For the other part it all works, but if the image initially is not loaded quick enough it will be a wrong sprite offset because it falls back to 256 for width and height.

And yes Tsoding, i used the x = 0 || 256 operator ....

We should credit the Sprite Designer (which is done in the demo but in the source as well)
(c) Scarfy sprite by Eiden Marsal

@IRooc IRooc force-pushed the example/texture_sprite_anim branch 3 times, most recently from 88b9102 to d69676f Compare February 18, 2024 11:53
@IRooc IRooc force-pushed the example/texture_sprite_anim branch from d69676f to 4701690 Compare February 18, 2024 11:54
Comment on lines +301 to +303
let maxWait = 1000;
while(maxWait >= 0)
maxWait--;
Copy link
Contributor

@jkisor jkisor Feb 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tricky -- I think I found a way without waiting. #37
EDIT: Well, this example uses width/height data returned from LoadTexture -- which is yet to be implemented.

Copy link
Author

@IRooc IRooc Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only synchronous option is the deprecated XMLHttpRequest one

function getImageDimensions(url) {
    let xhr = new XMLHttpRequest();
    xhr.overrideMimeType('text/plain; charset=x-user-defined'); //needed to not have unicode just ascii for the header part
    xhr.open('GET', url, false); // False for synchronous request
    xhr.send();
    
    if (xhr.status !== 200) {
        throw new Error('Failed to fetch image: ' + xhr.status);
    }
    
    let data = xhr.responseText;
    if (data.substr(1, 3) === 'PNG') {    
        let ihdr = data.indexOf('IHDR');
        if (ihdr === -1) {
            throw new Error('IHDR chunk not found');
        }
        var uint8 = Uint8Array.from(data.split("").map(x => x.charCodeAt()))
        let width = uint8[ihdr + 4] << 24 | uint8[ihdr + 5] << 16 | uint8[ihdr + 6] << 8 | uint8[ihdr + 7];
        let height = uint8[ihdr + 8] << 24 | uint8[ihdr + 9] << 16 | uint8[ihdr + 10] << 8 | uint8[ihdr + 11];
        return { width, height };
    }
    throw new Error('Not a supported image');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants