LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2003, 03:31 PM   #1
ssobeht
Member
 
Registered: Oct 2003
Distribution: Debian Sarge
Posts: 207

Rep: Reputation: 30
mmm... crazy values in C


Well, i'm trying to do a game in C using SDL libs. I had done some things but today, while adding some things to the player file, i discovered something really weird:

Code:
//loads the playah!!
struct player load_playa(char fplaya[MAX_CHAR])
{
    struct player temp;
    FILE *tfile;
    char fname[MAX_CHAR];
    int i,j,nframes,time;
    int tint;


    //loads the player info file
    tfile = fopen(fplaya,"r");

    //speed is:
    fscanf(tfile,"speed=%i\n",&tint);
    temp.speed = (Uint8)tint;
    fscanf(tfile,"jump=%i\n",&tint);
    temp.maxJump = (Uint8)tint;
    printf("speed: %i   max_jump: %i\n",temp.speed,temp.maxJump);

    //until the four sprites are loaded, it does:
    for (j = 0; j <= IDS; j++)
    {
        //checks the number of frames...
        fscanf(tfile,"frames=%i\n",&nframes);

        for (i=0;i<nframes;i++)
        {
            //and loads the pic, time and mask of each frame...

            fscanf(tfile,"pic=%s\n",&fname);
            temp.images[j][i].pic[0] = LoadImg(fname);
            //Generates the surface of the picture looking tio the inverse side.
            temp.images[j][i].pic[1] = ReversePic(temp.images[j][i].pic[0]);

            //sets the transparecy:
            SetTrans(temp.images[j][i].pic[0],0,0);
            SetTrans(temp.images[j][i].pic[1],0,0);

            //timing...
            fscanf(tfile,"time=%i\n",&time);
            temp.images[j][i].time = time;

            //masks...
            fscanf(tfile,"mask=%s\n",&fname);
            temp.images[j][i].rmask = LoadImg(fname);
            temp.images[j][i].lmask = ReversePic(temp.images[j][i].rmask);

        }
        temp.maxFrames[j] = nframes;
    }

    printf("speed: %i   max_jump: %i\n",temp.speed,temp.maxJump);

    return temp;
}
and the output is:
Code:
[ssobeht@NeoTokiO game-project]$ ./lintest
speed: 3   max_jump: 5
speed: 8   max_jump: 1
[ssobeht@NeoTokiO game-project]$
WHY!!! The values are supposed to remain as 3 and 5 but they change!!

you also might need this:
Code:
struct player
{
    struct frame images[IDS][MAX_FRAMES]; //pics id: 0=standing, 1=running, 2=crouch, 3=jump
    Uint8 maxFrames[IDS];
    Uint8 maxJump;
    Uint8 jump;
    Uint8 id;
    Uint8 frame;
    Uint8 speed;
    Uint8 lives;
    int damage;
    SDL_Rect position;
    int x,y;
    int side;
};
This is making me crazy. Plz help
thanx
 
Old 12-11-2003, 04:26 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
dont have time to find exact problem but somewhere in your loops, your writing to your array out of bounds and you hapen to be overwriting speed and maxJump and most likely other variables to. go through it with gdb or in your head and you'll find your going out of bounds somewhere.
 
Old 12-11-2003, 04:41 PM   #3
ssobeht
Member
 
Registered: Oct 2003
Distribution: Debian Sarge
Posts: 207

Original Poster
Rep: Reputation: 30
whats gdb (i'm a bit nebie in C, sorry ). Anyway, i think thats not the reason. This is the file where the function takes the data from:

speed=3
jump=5
frames=1
pic=char/body.bmp
time=0
mask=char/mbody.bmp
frames=2
pic=char/run1.bmp
time=250
mask=char/mrun1.bmp
pic=char/run2.bmp
time=250
mask=char/mrun2.bmp
frames=1
pic=char/crouch.bmp
time=0
mask=char/mcrouch.bmp
frames=1
pic=char/run1.bmp
time=0
mask=char/mrun1.bmp
 
Old 12-11-2003, 04:42 PM   #4
ssobeht
Member
 
Registered: Oct 2003
Distribution: Debian Sarge
Posts: 207

Original Poster
Rep: Reputation: 30
now i now what gdb is (google xD). I'll try it.thanx
 
Old 12-11-2003, 07:10 PM   #5
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Your outer loop should read:

Code:
for (j = 0; j < IDS; j++)
instead of:

Code:
for (j = 0; j <= IDS; j++)
If j = IDS, then you are writing to the memory where 'jump' is located, and everything after it.
 
Old 12-11-2003, 07:31 PM   #6
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
there ya go, i said you were going out of bounds
 
Old 12-12-2003, 06:53 AM   #7
ssobeht
Member
 
Registered: Oct 2003
Distribution: Debian Sarge
Posts: 207

Original Poster
Rep: Reputation: 30
oh, thanx a lot You're all great!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hsync vsync VertRefresh... mmm... ok? mimithebrain Linux - Hardware 8 10-10-2005 03:07 PM
xmodmap for multiple values malo_umoran Slackware 3 03-27-2005 09:39 AM
Sync Values waslit Linux - Software 0 06-05-2004 07:34 AM
mmm instruction help... Beppe83 Linux - Software 1 05-31-2004 07:57 AM
rx/tx values Nauseous *BSD 1 05-26-2004 02:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration