terça-feira, 17 de janeiro de 2012

Certa vez fiz um programinha em c, que alocava muito mais memoria do que eu precisava, e como boa programadora java nunca me habituei a desalocar memoria, hoje encontrei em um email antigo o código, vou compartilhar meu erro com vocês ;)

#include <allegro.h>

void init();
void deinit();

struct personagem {
    int x, y;

    float dx, dy;

    int screenLocation;

    BITMAP *sprite;

};



struct personagem ruy;
int imageLine[4];
char *msg = "SONIC";
char *msgMenu = "APERTE ESPACO PARA COMECAR A BRINCADEIRA";
BITMAP * teladefundo;
BITMAP *buffer;
int i = 0;
int h = 0;
int auxH = 0;
long timer = 0;
int sprintColum = 480;
int auxMove = 0;

void doClearScreen() {

    destroy_bitmap(ruy.sprite);
    //destroy_bitmap(teladefundo);
    clear(buffer);
    ruy.sprite = NULL;
    ruy.sprite = load_bitmap("sprite.bmp", NULL);
    teladefundo = load_bitmap("dsf.bmp", NULL);

}

int main() {
    init();
    buffer = create_bitmap(640, 480);
    ruy.x = 92;
    ruy.y = 120;
    ruy.screenLocation = 350;
    ruy.sprite = load_bitmap("sprite.bmp", NULL);
    teladefundo = load_bitmap("dsf.bmp", NULL);
    textout(screen, font, msg, (SCREEN_W / 2)-(text_length(font, msg) / 2), (SCREEN_H / 2)-(text_height(font)), makecol(255, 0, 0));

    while (timer < 500000000) {
        timer++;
        //Pequeno contador para deixar aparecendo o slog do game.
        //UAHUAHUAHUAHAUH
    }


    while (!key[KEY_SPACE]) {
        // masked_blit( teladefundo,buffer,0,0,0,0,600,600);
        i++;
        sprintColum = 120;
        if (i >= 4) {
            i = 0;
        }
        if (ruy.sprite != NULL) {
            doClearScreen();
        }


        draw_sprite(buffer, teladefundo, 20, 30);
        masked_blit(ruy.sprite, buffer, imageLine[i], sprintColum, h, ruy.screenLocation, ruy.x, ruy.y);
        blit(buffer, screen, 0, 0, 0, 0, 640, 480);
    }
    //   masked_blit( ruy.sprite,screen,imageLine[3],480,80,ruy.screenLocation,ruy.x,ruy.y);
    if (key[KEY_SPACE]) {
        draw_sprite(buffer, teladefundo, 20, 30);
        blit(buffer, screen, 0, 0, 0, 0, 640, 480);
        while (!key[KEY_ESC]) {

            if (key[KEY_UP]) {
                sprintColum = 480;
                ruy.screenLocation -= 10;
                i++;
                h +=10;
                auxH++;
                if(i>=4){
                    while (i !=0) {
                        if (ruy.sprite != NULL) {
                            doClearScreen();
                        }
                          h-=10;
                          i--;
                        masked_blit(ruy.sprite, buffer, imageLine[i], sprintColum, h, ruy.screenLocation, ruy.x, ruy.y);
                    }
                  //   ruy.screenLocation=350;
                     masked_blit(ruy.sprite, buffer, imageLine[i], sprintColum, h, ruy.screenLocation, ruy.x, ruy.y);
                     }
           
                if (ruy.sprite != NULL) {
                    doClearScreen();
                }

            }

            if (key[KEY_LEFT]) {
                h -= 10;
                i++;
                sprintColum = 250;
                if (i > 4) {
                    i = 0;
                }

                if (ruy.sprite != NULL) {
                    doClearScreen();
                }

            } else if (key[KEY_RIGHT]) {
                h += 10;
                i++;
                sprintColum = 250;
                if (i > 4) {
                    i = 0;
                }

                if (ruy.sprite != NULL) {
                    doClearScreen();
                }

            }
           draw_sprite(buffer, teladefundo, 20, 30);
           masked_blit(ruy.sprite, buffer, imageLine[i], sprintColum, h, ruy.screenLocation, ruy.x, ruy.y);
           blit(buffer, screen, 0, 0, 0, 0, 640, 480);
        }
    }

    deinit();
    return 0;
}

END_OF_MAIN()



void init() {
    int depth, res;
    allegro_init();
    depth = desktop_color_depth();
    if (depth == 0) depth = 32;
    set_color_depth(depth);
    res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    if (res != 0) {
        allegro_message(allegro_error);
        exit(-1);
    }

    install_timer();
    install_keyboard();
    install_mouse();
    imageLine[0] = 0;
    imageLine[1] = 120;
    imageLine[2] = 240;
    imageLine[3] = 360;
    imageLine[4] = 480;
}

void deinit() {
    clear_keybuf();
    /* add other deinitializations here */
}

Nenhum comentário:

Postar um comentário