How can i draw 10x the same image/texture ?

Hello I try to show the same image 10x on my screen

Okey … we need a class …
But it dosnt work (compiled in c4droid)

→ whats wrong ???

Main.cpp

//#####################
// this is written by patrick ratz
//######################

#include <SDL2/SDL.h> 

#include <stdio.h>  
#include <stdlib.h> 

#include <cstdlib>   // this is for the classes 
#include <sstream> // and this for "arrsyof"

#define hWnd_height 48
#define hWnd_width  42


//____ the class ____

const int MAX_hWnd = 10;


class hWnd_Button
{
 public:
 hWnd_Button();
 
 bool isActive;
 
 int hWnd_Button_pos_x;
 int hWnd_Button_pos_y;

 SDL_Renderer* renderer;
 SDL_Surface* surface;
 SDL_Texture* texture;
 
 const SDL_Rect * dstrect;

// show -> lol crazy * ptr but must !!!

void show( SDL_Renderer *renderer, SDL_Texture *texture, const  SDL_Rect * dstrect);

 private:
};
hWnd_Button arrayofPanel[MAX_hWnd];


hWnd_Button::hWnd_Button()
{
  hWnd_Button_pos_x = 50;	
  hWnd_Button_pos_y = 50;
}

// ____________

void hWnd_Button::show (SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect * dstrect)
{
 // we clone 10x the rect to int 
 for (int i =0; i<MAX_hWnd; i++)
 {
  if (arrayofPanel[i].isActive== true)
  {
     SDL_Rect dstrect;

   dstrect.x = arrayofPanel[i].hWnd_Button_pos_x;
     
    dstrect.y = arrayofPanel[i]. hWnd_Button_pos_y;

    //draw bitmap
    SDL_RenderCopy(renderer,texture, NULL, &dstrect); 
  }
 }	
}


//_________MAIN ____________

int main( int argc, char *argv[] )
{
	
  SDL_Event event; 
  SDL_Init(SDL_INIT_EVERYTHING);
 
  SDL_Window *window =  
  SDL_CreateWindow("SDL2 image_Class ", 0, 0, 1024, 768, SDL_WINDOW_SHOWN);
  
  SDL_Renderer *renderer = 
  SDL_CreateRenderer(window, -1, 0);

//--------------

 SDL_Surface *hWnd_up =  
 SDL_LoadBMP("hwnd_blank_up.bmp");
  
 SDL_Texture *texture_hWnd_up =  
 SDL_CreateTextureFromSurface(renderer,hWnd_up);

//----------------

  SDL_Surface *hWnd_down = 
  SDL_LoadBMP("hwnd_blank_down.bmp");
  
  SDL_Texture *texture_hWnd_down = 
  SDL_CreateTextureFromSurface(renderer,hWnd_down);

//-------------
 int hWnd_Button_pos_x = 50;
 int hWnd_Button_pos_y = 50;

  int hwnd_size_x = 48;
  int hwnd_size_y = 42;
    
//--------------
  
  hWnd_Button myClass;
  
  SDL_Rect hWnd_dstrect = { hWnd_Button_pos_x,
hWnd_Button_pos_y,
hwnd_size_x,
hwnd_size_y};
  
//-------------------------------------------------

  int quit = 1;
  int system_var = 2;
  
while(quit == 1) // prgramm loop
{
 
 while (SDL_PollEvent(&event))
 {
  switch (event.type)
  {
   case SDL_QUIT:
   quit = 0;
   break;	
   }
  }
 
 // we clone ONE TIME in a loop 
 // 10x  the class postion
 
 if ( system_var == 2)  
 {
  for (int i =0; i<MAX_hWnd; i++)
  {
   if (arrayofPanel[i].isActive== true)
   {
   arrayofPanel[i].hWnd_Button_pos_x;
   arrayofPanel[i].hWnd_Button_pos_y;
   arrayofPanel[i].isActive = true;
   }  
  }
  system_var == 1;
 } 
 
 
 // we clone 10x rect (main) with class pos.
 for (int i =0; i<MAX_hWnd; i++)
 {
  if (arrayofPanel[i].isActive== true)
  {
   hWnd_dstrect.x = 
   arrayofPanel[i].hWnd_Button_pos_x + 10;
     
    hWnd_dstrect.y = 
    arrayofPanel[i].hWnd_Button_pos_y + 300; 
    
    SDL_RenderCopy(renderer,texture_hWnd_up, NULL, &hWnd_dstrect); 
     
   }
  }


// draw the class 10x ...  but nothing is doing
 myClass.show (renderer, texture_hWnd_up, &hWnd_dstrect); 
 
  
 SDL_RenderPresent(renderer);
} // end of program loop
  
//-------------------------------------------------

  SDL_DestroyTexture(texture_hWnd_up);
  SDL_FreeSurface(hWnd_up);
  
  SDL_DestroyTexture(texture_hWnd_down);
  SDL_FreeSurface(hWnd_down);
  
  
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  
 SDL_Quit();
 return 0;
}

why do we need a class?

“it doesn’t work” → what doesn’t work. describe what did you expect/want and explain what actually happened

indent your code properly, it is hard to follow. congratulations for formatting code properly tho

just write if (arrayofPanel[i].isActive) instead of if (arrayofPanel[i].isActive== true)

you didn’t initialize the members w and h of dstrect in method hWnd_Button::show

the members renderer, surface and texture are useless inside class hWnd_Button. I think the member dstrect is also useless inside it.

make method hWnd_Button::show static. it doesn’t use instance data

move the initialization code protected by system_var outside the infinite loop, put it before the infinite event loop, and remove system_var and all of its usage

this same initialization code never does anything, because member isActive is initialized to false. because it is initialization code, the entire if is useless.

after a few frames your buttons will quickly disappear, because hWnd_dstrect will quickly point outside your window

the call to myClass.show will only use the last update to hWnd_dstrect, hence it will probably draw the image 10 times in the same place

you are including stdlib.h and cstdlib, I think they are the same, prefer the later.

local variable dstrect of method hWnd_Button::show shadows parameter dstrect. do you have warnings enabled? fix all errors and warnings before asking for help. enable with -Wpedantic -Wall -Wextra -Wshadow

Okey okey … i was try to fix all this afternoon :sweat_smile:

Now it works …But dont stop to draw …
with every Pos_x +50

You can help me ?
→ here is my test code


//#####################
// this is written by patrick ratz
//######################

#include <SDL2/SDL.h> 

#include <stdio.h>  
#include <stdlib.h> 

#include <cstdlib>   // this is for the classes 
#include <sstream> // and this for "arrayof"

#define hWnd_Button_height 48
#define hWnd_Button_width  42


//____ the class ____

const int MAX_hWnd = 10;

class hWnd_Button
{
 public:
 hWnd_Button();
 
 bool isActive;
 
 int hWnd_Button_pos_x;
 int hWnd_Button_pos_y;
 
 int hWnd_Button_size_x;
 int hWnd_Button_size_y;

 SDL_Renderer* renderer;
 SDL_Surface* surface;
 SDL_Texture* texture;
 
void show( );

 private:
};
hWnd_Button arrayofPanel[MAX_hWnd];


hWnd_Button::hWnd_Button()
{
 hWnd_Button_pos_x = 0;	
 hWnd_Button_pos_y = 300;
 
//hWnd_Button_size_x = hWnd_Button_height;
 //hWnd_Button_size_y = hWnd_Button_width;
}

// ____________

void hWnd_Button::show ()
{
 /*
 // we clone 10x the rect to int 
 for (int i =0; i<MAX_hWnd; i++)
 {
  if (arrayofPanel[i].isActive== true)
  {
  
    SDL_Rect dstrect;

   dstrect.x = arrayofPanel[i].hWnd_Button_pos_x;
  dstrect.y = arrayofPanel[i]. hWnd_Button_pos_y;
 
// dstrect.h = arrayofPanel[i].hWnd_Button_size_x;     
 //dstrect.w = arrayofPanel[i].hWnd_Button_size_y;

 //draw bitmap
 
//SDL_RenderCopy(renderer,texture, NULL, &dstrect); 
  }
 }	
 */
}


//_________MAIN ____________

int main( int argc, char *argv[] )
{
	
  SDL_Event event; 
  SDL_Init(SDL_INIT_EVERYTHING);
 
  SDL_Window *window =  
  SDL_CreateWindow("SDL2 image_Class ", 0, 0, 1024, 768, SDL_WINDOW_SHOWN);
  
  SDL_Renderer *renderer = 
  SDL_CreateRenderer(window, -1, 0);

//--------------

 SDL_Surface *hWnd_up =  
 SDL_LoadBMP("hwnd_blank_up.bmp");
  
 SDL_Texture *texture_hWnd_up =  
 SDL_CreateTextureFromSurface(renderer,hWnd_up);

//----------------

  SDL_Surface *hWnd_down = 
  SDL_LoadBMP("hwnd_blank_down.bmp");
  
  SDL_Texture *texture_hWnd_down = 
  SDL_CreateTextureFromSurface(renderer,hWnd_down);


//--------------
  
  // load the class + information
hWnd_Button myClass;
 
 
//-------------------------------------------------

  int quit = 1;
  int system_var = 2;
  
while(quit == 1) // prgramm loop
{
 
 while (SDL_PollEvent(&event))
 {
  switch (event.type)
  {
   case SDL_QUIT:
   quit = 0;
   break;	
   }
  }
 
 //---------------
  
  //we clone  ONE TIME ..10x the init !
  if (system_var == 2)
  {
   for (int i =0; i<MAX_hWnd; i++)
   {	
    arrayofPanel[i].hWnd_Button_pos_x  + 50;
   // arrayofPanel[i].hWnd_Button_pos_y = 300;
    
 // arrayofPanel[i].hWnd_Button_size_x = hWnd_Button_height; 
 // arrayofPanel[i].hWnd_Button_size_y =  hWnd_Button_width;
    
     arrayofPanel[i].isActive= true;
    }   
    system_var = 1;
   }
   
   //---------------
  
  //for 10 surfaces
  for (int i =0; i<MAX_hWnd; i++)
  {
   if (arrayofPanel[i].isActive== true)
   {
    SDL_Rect hWnd_dstrect;
 
    hWnd_dstrect.x = arrayofPanel[i].hWnd_Button_pos_x;
    hWnd_dstrect.y= arrayofPanel[i].hWnd_Button_pos_y;
   
   //hWnd_dstrect.h =arrayofPanel[i].hWnd_Button_size_x;
// hWnd_dstrect.w = arrayofPanel[i].hWnd_Button_size_y;
  	
   SDL_RenderCopy(renderer,texture_hWnd_up, NULL, &hWnd_dstrect);   
   }  
  }
  
 SDL_RenderPresent(renderer);
} // end of program loop
  
//-------------------------------------------------

  SDL_DestroyTexture(texture_hWnd_up);
  SDL_FreeSurface(hWnd_up);
  
  SDL_DestroyTexture(texture_hWnd_down);
  SDL_FreeSurface(hWnd_down);
    
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  
 SDL_Quit();
 return 0;
}

Maybe you can describe what it is you’re trying to do, which will make it easier to help you out and give suggestions on how you can achieve it.