Author Topic: Image Not Drawiing  (Read 5101 times)

0 Members and 1 Guest are viewing this topic.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Image Not Drawiing
« on: August 09, 2007 »
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:
Code: [Select]
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:
Code: [Select]
   letter *letterhead=NULL;
   
   InitLetters(30, 100, 4, 3, "SOMETHING IS GOING ON", letterhead);
Heres the function where they are created:
Code: [Select]
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:
Code: [Select]
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?

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Image Not Drawiing
« Reply #1 on: August 09, 2007 »
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:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Image Not Drawiing
« Reply #2 on: August 09, 2007 »
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 :(
Code: [Select]
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++;
    }           
}

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Image Not Drawiing
« Reply #3 on: August 10, 2007 »
all the lh become *lh, so
*lh->x = ...

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

Jim
Challenge Trophies Won:

Offline madsravn

  • ZX 81
  • *
  • Posts: 13
  • Karma: 0
    • View Profile
Re: Image Not Drawiing
« Reply #4 on: August 10, 2007 »
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 :)

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Image Not Drawiing
« Reply #5 on: August 10, 2007 »
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:
Code: [Select]
*lh=NewLetter(&lh);
I'll get them tho. Someday soon. Hopefully.    :whack:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Image Not Drawiing
« Reply #6 on: August 10, 2007 »
Remove the &
Jim
Challenge Trophies Won:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Image Not Drawiing
« Reply #7 on: August 10, 2007 »
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 :)

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Image Not Drawiing
« Reply #8 on: August 10, 2007 »
I'm glad it's working :)

Jim
Challenge Trophies Won: