Image Class for beginner

Finaly , after lots of month work !!! - i do it :grin:


IMPORTANT FOR BEGINNER

→ Draw 10x the same texture ←

How?

→ with an image_Class
→ and an array[10]
→ on 10 random Position

..via a for loop in Main :slightly_smiling_face::saluting_face:


THIS IS “MUSS U KNOW” STUFF

Lets go:

write 1 time an "class "

for. all your program processes.. in 2d or 3d..for objects ..events and so on

→ that spend time and lots of Ressources !!

Code:

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

#include <SDL2/SDL.h> 

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

#include <time.h>

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

#define hWnd_Button_height 48
#define hWnd_Button_width  42

 SDL_Surface* image = NULL;
 SDL_Texture* texture_image = NULL;
 
SDL_Rect dstrect;

//____ the class ____

const int MAX_image = 10;

class image_class
{
 public:
 image_class();
 
 bool isActive;
 
 int image_pos_x;
 int image_pos_y;
 
 int image_size_x;
 int image_size_y;
 

 SDL_Renderer* renderer = NULL;
 SDL_Surface* surface = NULL;
 SDL_Texture* texture = NULL;
 
void show( 
SDL_Renderer * renderer,
 SDL_Texture * texture, 
 const SDL_Rect * srcrect,
 const SDL_Rect * dstrect
 );

 private:
};
image_class arrayofPanel[MAX_image];


image_class::image_class()
{
  image_pos_x = 300;	
  image_pos_y= 300;
  
  image_size_x = 96;
  image_size_y = 84;
 
 // image_pos_x + 100;
}

// ____________

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

   dstrect2.w = arrayofPanel[i].image_size_x;
   dstrect2.h= arrayofPanel[i].image_size_y;
   
   dstrect2.x = arrayofPanel[i].image_pos_x + 100;
   
 //draw bitmap
 
SDL_RenderCopy(renderer, texture_image, NULL, &dstrect2); 

//dstrect.x = arrayofPanel[i].image_pos_x + 100;   
  }
 }	
 
}


//_________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);

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

 image =  SDL_LoadBMP("hwnd_blank_up.bmp");
  
 texture_image =  
 SDL_CreateTextureFromSurface(renderer,image);

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

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


//--------------
  
  // load the class + information
image_class 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;	
   }
  }
 
 //---------------
  
  // clone 10x X and Y.... 1 time!!!
 if (system_var == 2)
 {
  	srand(time(NULL));
  	
   for (int i=0; i<MAX_image; i++)
   {
    arrayofPanel[i].image_pos_x = 300;
    arrayofPanel[i].image_pos_y = 300;
   }
  	
    for (int i=0; i<MAX_image; i++)
   {
    arrayofPanel[i].image_pos_x += rand()%800+1;
    
    arrayofPanel[i].image_pos_y += rand()%600+1;
    
   // arrayofPanel[i].image_size_x;
    //arrayofPanel[i].image_size_y;
   
    //arrayofPanel[i].image_pos_x + 10;
   
    arrayofPanel[i].isActive= true;
   
  }   
  
   system_var = 1;
 }
   
 //---------------
   
 //clone 10x surfaces on 10 pos
 
  for (int i=0; i<MAX_image; i++)
  {
   if (arrayofPanel[i].isActive== true)
   {
    
   
 
    dstrect.x = arrayofPanel[i].image_pos_x; 
    dstrect.y = arrayofPanel[i].image_pos_y;

  //  dstrect.h = arrayofPanel[i].image_size_x;
   // dstrect.w = arrayofPanel[i].image_size_y;
   
   
  SDL_RenderCopy(renderer, texture_image, NULL, &dstrect); 
    } 
  }
  
 myClass.show(renderer, texture_image,NULL, &dstrect);

 
  
 SDL_RenderPresent(renderer);
} // end of program loop
  
//-------------------------------------------------

  SDL_DestroyTexture(texture_image);
  SDL_FreeSurface(image);
  
  SDL_DestroyTexture(texture_hWnd_down);
  SDL_FreeSurface(hWnd_down);
    
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  
 SDL_Quit();
 return 0;
}

Dear developers Can u save it in your forum