You are not logged in.

#1 2008-07-02 16:03:48

grillo
Member

Load PNG from File

I made a small mod to the load texture function to load it from the SD card, you just need to add the following functions to your GRRLIB file (compatible with GRRLIB 3.0.1a)
you have to take in count all the libraries considerations though (max width/lenght 1024... pixels must be multiples of 4... PNGU restrictions... etc)

GRRLIB.c

Code:

u8 * GRRLIB_LoadTextureFromFile(const char *filename) {

   PNGUPROP imgProp;
   IMGCTX ctx;
   void *my_texture;

       ctx = PNGU_SelectImageFromDevice(filename);
        PNGU_GetImageProperties (ctx, &imgProp);
        my_texture = memalign (32, imgProp.imgWidth * imgProp.imgHeight * 4);
        PNGU_DecodeTo4x4RGBA8 (ctx, imgProp.imgWidth, imgProp.imgHeight, my_texture, 255);
        PNGU_ReleaseImageContext (ctx);
        DCFlushRange (my_texture, imgProp.imgWidth * imgProp.imgHeight * 4);
    return my_texture;
}

GRRLIB.h

Code:

u8 * GRRLIB_LoadTextureFromFile(const char *filename);

Here is an example (moded day 5 example):

Code:

/*===========================================
        GRRLIB (GX version) 3.0.1 alpha
        Code     : NoNameNo
        GX hints : RedShade

        Http://grrlib.santo.fr
        Tutorial : Day 5 (Display an Image)
 ============================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <ogcsys.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <fat.h> // <- a must

#include "GRRLIB/GRRLIB.h"

Mtx GXmodelView2D;

int main(){
   u8 *tex_logo;
   float rot=0;
   float alpha=255;

    VIDEO_Init();
    WPAD_Init();
    fatInitDefault (); // <- must be there before loading png

    tex_logo=GRRLIB_LoadTextureFromFile("logo.png"); //<- here it loads the png, be sure to input where the file is stored in your sd

    GRRLIB_InitVideo();
    GRRLIB_Start();

    while(1){
        WPAD_ScanPads();
        u32 wpaddown = WPAD_ButtonsDown(0);
        u32 wpadheld = WPAD_ButtonsHeld(0);

        GRRLIB_FillScreen(0xFF000000);

        GRRLIB_DrawImg(144, 176, 352, 128, tex_logo, rot, 1, 1, alpha );

        GRRLIB_Render();

        if (wpaddown & WPAD_BUTTON_A) exit(0);
        if (wpadheld & WPAD_BUTTON_RIGHT) rot+=2;
        if (wpadheld & WPAD_BUTTON_LEFT) rot-=2;
        if (wpadheld & WPAD_BUTTON_MINUS) if((alpha-=3)<0) alpha=0;
        if (wpadheld & WPAD_BUTTON_PLUS) if((alpha+=3)>255) alpha=255;

    }
    return 0;
}

hope it helps ^^

Last edited by grillo (2008-07-02 16:07:57)

Offline

 

#2 2008-12-30 16:45:32

Crayon
Bad Mother Fucker

Re: Load PNG from File

Just to mention that with the latest libogc it's best to use the full path:

Code:

tex_logo=GRRLIB_LoadTextureFromFile("sd:/logo.png");

Offline

 

#3 2009-01-02 17:18:57

sammystag
Member

Re: Load PNG from File

Just been writing some code to do exactly the same thing. Should have looked here first...
Here's the alternative way of doing it

Code:

u8 * GRRLIB_LoadFileTexture(const char *filename) {

    FILE *fd = fopen(filename, "rb");

    // obtain file size:
    fseek(fd , 0 , SEEK_END);
    long lSize = ftell(fd);
    rewind(fd);

    // read the file
    unsigned char *buffer = (unsigned char*) malloc (sizeof(unsigned char)*lSize);
    fread (buffer, 1, lSize, fd);
    fclose(fd);

    // load data into a texture
    u8 *tex = GRRLIB_LoadTexture(buffer);
    
    free(buffer);
    return tex;
}

Offline

 

Board footer

Powered by FluxBB