dynamisch grossen array übergeben?



  • void cleanArray(int *array,int a,int b)
    {
    	for(int i = 0; i<a; i++)
    	{
    		for(int j = 0; j<b; j++)
    		{
    				array[i][j] = 0x000000;
    		}
    	}
    }
    

    geht nicht weil der array nicht als solch einer angesehen wird?!

    bitte im hilfe, danke!



  • array[i*a + j] = 0x000000;



  • FireFlow schrieb:

    array[i*a + b] = 0x000000;

    void cleanArray(int *array,int a,int b)
    {
    	for(int i = 0; i<a; i++)
    	{
    		for(int j = 0; j<b; j++)
    		{
    				array[i*a + b] = 0x000000;
    		}
    	}
    }
    ...
    ...
    cleanArray(*textArray,SWIDTH,SHEIGHT);
    

    funktioniert nicht, array wir nicht geleert/auf null gesetzt...

    meintest du

    void cleanArray(int *array,int a,int b)
    {
    	for(int i = 0; i<a; i++)
    	{
    		for(int j = 0; j<b; j++)
    		{
    				array[j*a + i] = 0x000000;
    		}
    	}
    }
    

    ? das geht nähmlich 😛



  • Besser wäre evtl.

    array[i*b + j] = 0;
    


  • groovemaster schrieb:

    Besser wäre evtl.

    array[i*b + j] = 0;
    

    warum besser? das ist das gleiche in vertikal oO?



  • pixartist schrieb:

    warum besser?

    Zum einen, weil die CPU evtl. besser cachen kann. Zum anderen, weil der Ausdruck i*b in der innersten Schleife konstant ist und der Compiler somit besser optimieren kann. Dh, tempörares Ergebnis aus der Schleife rausziehen, was pro Durchlauf eine Multiplikation spart.



  • groovemaster schrieb:

    pixartist schrieb:

    warum besser?

    Zum einen, weil die CPU evtl. besser cachen kann. Zum anderen, weil der Ausdruck i*b in der innersten Schleife konstant ist und der Compiler somit besser optimieren kann. Dh, tempörares Ergebnis aus der Schleife rausziehen, was pro Durchlauf eine Multiplikation spart.

    oha..achso, danke



  • hier da gleiche, nur leider machts mir echt kopfzerbrechen:
    ursprüngliche funktion(ging wunderbar)

    void loadimg(SDL_Surface *gSurf, int array[SWIDTH][SHEIGHT],int x, int y, char *path, int width, int height)
    {
    	SDL_Surface *temp = SDL_LoadBMP(path);
    	gSurf = SDL_ConvertSurface(temp, screen->format, SDL_SWSURFACE);
    	SDL_FreeSurface(temp);
    
      // Lock surface if needed
      if (SDL_MUSTLOCK(gSurf))
        if (SDL_LockSurface(gSurf)< 0) 
          return;
    
      int i, j;
      for (i = 0; i < height; i++)
      {
    
        for (j = 0; j < width; j++)
    	{
    		int tileofs = j+(i*width);
    		array[j][i] = ((unsigned int*)gSurf->pixels)[tileofs];
    		tileofs++;
        }
      }
    
      // Unlock if needed
        if (SDL_MUSTLOCK(gSurf)) 
            SDL_UnlockSurface(gSurf);
    }
    

    jetzt das ganze dynamisch:

    void loadimg(SDL_Surface *gSurf, int *array,int x, int y, char *path, int width, int height,int awidth)
    {
    	SDL_Surface *temp = SDL_LoadBMP(path);
    	gSurf = SDL_ConvertSurface(temp, screen->format, SDL_SWSURFACE);
    	SDL_FreeSurface(temp);
    
      // Lock surface if needed
      if (SDL_MUSTLOCK(gSurf))
        if (SDL_LockSurface(gSurf)< 0) 
          return;
    
      int i, j;
      for (i = 0; i < height; i++)
      {
    
        for (j = 0; j < width; j++)
    	{
    		int tileofs = j+(i*width);
    		int aOfs = j+x+((i+y)*awidth);
    		array[aOfs] = ((unsigned int*)gSurf->pixels)[tileofs];
    		tileofs++;
        }
      }
    
      // Unlock if needed
        if (SDL_MUSTLOCK(gSurf)) 
            SDL_UnlockSurface(gSurf);
    }
    

    hier wird das bild total verzerrt! awidth ist sozusagen die "breite" des arrays

    mich wundert auch, dass ich beim debuggen mit der zweiten funktion folgendes sehe:

    aOfs 24
    awidth 25
    i 1
    j 0
    tileofs 25
    width 25
    x 0
    y 0

    0+0+((1+0)*25) = 24 ?????



  • pixartist schrieb:

    hier wird das bild total verzerrt! awidth ist sozusagen die "breite" des arrays

    Was ja korrekt wäre, denn width heisst nunmal Breite. 😃

    Wahrscheinlich liegt dein Problem beim Ausgangsmaterial

    int array[SWIDTH][SHEIGHT]
    

    Die Breite ist idR die niederwertige Dimension, also

    int array[SHEIGHT][SWIDTH]
    

    btw:
    Was soll

    tileofs++;
    

    am Ende der Schleife? Damit wird doch nichts weiter gemacht.



  • danke, habs jetzt

    void loadimg(SDL_Surface *gSurf, int *array, char *path, int width, int height,int aheight,int x = 0, int y = 0)
    {
    	SDL_Surface *temp = SDL_LoadBMP(path);
    	gSurf = SDL_ConvertSurface(temp, screen->format, SDL_SWSURFACE);
    	SDL_FreeSurface(temp);
    
      // Lock surface if needed
      if (SDL_MUSTLOCK(gSurf))
        if (SDL_LockSurface(gSurf)< 0) 
          return;
    
      int i, j;
      for (i = 0; i < height; i++)
      {
    
        for (j = 0; j < width; j++)
    	{
    		int tileofs = j+(i*width);
    		int aOfs = i+y+((j+x)*aheight);
    		array[aOfs] = ((unsigned int*)gSurf->pixels)[tileofs];
        }
      }
    
      // Unlock if needed
        if (SDL_MUSTLOCK(gSurf)) 
            SDL_UnlockSurface(gSurf);
    }
    

    dankeschöööön! 🙂


Anmelden zum Antworten