• Welcome to Dark Bit Factory & Gravity.
 

Image Not Drawiing

Started by mike_g, August 09, 2007

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

mike_g

Ok, I'm pretty sure this is a pointer another problem  :-\

I'm coding a demo using SDL. For the text each letter is going to be an object:
struct letter
{
    float x, y;
    char ch;
    struct letter *next;
}; typedef struct letter letter;

letter *NewLetter(letter **head)
{
    letter *newnode;

    if(!(newnode = malloc(sizeof(*newnode))))
        return NULL;
   
    newnode->next=*head;
    *head = newnode;   
    return newnode;
}

When the program starts I create a set of letters from main:
   letter *letterhead=NULL;
   
   InitLetters(30, 100, 4, 3, "SOMETHING IS GOING ON", letterhead);

Heres the function where they are created:
void InitLetters(int x, int y, int space, int randy, char* txt, letter *lh)
{
    int pos=0;
    while(*txt)
    {
        lh=NewLetter(&lh);
        lh->x=x+pos+space+Rand(-randy, randy);
        lh->y=y+Rand(-randy, randy)*3;
        lh->ch=*txt;
        pos+=FONT_WIDTH;
        txt++;
    }           
}
 
In my main loop I then draw each letter:
void DrawLetters(letter *lh)
{
    src.w=FONT_WIDTH;
    src.h=FONT_HEIGHT;
    while(lh != NULL)
    {
        exit(1);
        if(lh->ch > ' ' && lh->ch < '[')
        {
            dest.x=lh->x;
            dest.y=lh->y;
            src.x=((lh->ch-' ')%FONTSET_WIDTH)*FONT_WIDTH;
            src.y=((lh->ch-' ')/FONTSET_WIDTH)*FONT_HEIGHT;         
            SDL_BlitSurface(font, &src, screen, &dest);
        }         
        lh=lh->next;
    }         
}

Now I added 'exit(1)' to the loop in this function so the prog should quit at the first letter, but it dosent. This should mean that none of the letters were actually created in the InitLetters function.
Someone know whats wrong here?

Jim

Yeah, in InitLetters, you need to pass in &letterhead and fix it up from there.  Otherwise you're not modifying letterhead inside NewLetter, you only modifying the copy of it you passed in.

Jim
Challenge Trophies Won:

mike_g

Thanks Jim, that makes sense. I'm still having a bit of a problem getting it to work.

I'm not sure if this is right, but I added another star to where there letterhead is passed in because its a pointer to a pointer. The rest of the lines of code in InitLetters give errors now saying I got incompatible pointer types, but I dont know what to do to sort them out :(
void InitLetters(int x, int y, int space, int randy, char* txt, letter **lh)
{
    int pos=0;
    while(*txt)
    {
        lh=NewLetter(&lh);
        lh->x=x+pos+space+Rand(-randy, randy);
        lh->y=y+Rand(-randy, randy)*3;
        lh->ch=*txt;
        pos+=FONT_WIDTH;
        txt++;
    }           
}

Jim

all the lh become *lh, so
*lh->x = ...

The call to NewLetter becomes
*lh = NewLetter(lh)

Jim
Challenge Trophies Won:

madsravn

The day I understand pointers that well, I think I will be good at C++ :) My idea of pointers is that they are wise to stay away from :)

mike_g

Cheers Jim, how many time have you had to sort out my pointers now? Have some K.

I tried what you said but screwed up passing it into the NewLetter function. As in:
*lh=NewLetter(&lh);

I'll get them tho. Someday soon. Hopefully.    :whack:

Jim

Challenge Trophies Won:

mike_g

Quote*lh=NewLetter(&lh);
Yeah there I was just saying what I was trying to do before that wouldn't work. Got it all fixed thanks, guess maybe the other post was a bit misleading. Cheers  dude :)

Jim

I'm glad it's working :)

Jim
Challenge Trophies Won: