Hello, i try to make an class in Cand SDL2 to create 10 bmp like this: ■■■■■■■■■
But it dosnt work right now, it scale one time
Half up the screen ^^ whts wrong
#include <stdio.h>
#include <SDL2/SDL.h>
//this is what we need for the "array of" function,
#include <cstdlib>
#include <sstream>
#define bmp_hight 32
#define bmp_wight 32
const int MAX_BMP = 10;
class theBMP
{
public:
theBMP();
bool isActive;
SDL_Renderer* renderer;
SDL_Surface* Bitmap2;
SDL_Texture* texture2;
const SDL_Rect * dstrect2;
int bmp2_pos_x;
int bmp2_pos_y;
void show(SDL_Renderer *renderer, SDL_Texture *texture2, const SDL_Rect * dstrect2);
private:
};
theBMP arrayofBMP[MAX_BMP];
//_______________________
theBMP::theBMP()
{
bmp2_pos_x;
bmp2_pos_y;
}
//_______________________
void theBMP::show (SDL_Renderer *renderer,
SDL_Texture *texture2,
const SDL_Rect * dstrect2)
{
//now we clone 10x the Position and the bmp
for (int i =0; i<MAX_BMP; i++)
{
if (arrayofBMP[i].isActive == true)
{
SDL_Rect dstrect;
dstrect.x = arrayofBMP[i]. bmp2_pos_x;
dstrect.y = arrayofBMP[i]. bmp2_pos_y;
//draw bitmap2
SDL_RenderCopy(renderer, texture2, NULL, &dstrect);
}
}
}
//_______________________
int main(int argc, char *argv[])
{
SDL_Event event;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError());
return 1;
}
//create Window
SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
//create renderer
SDL_Renderer *renderer =
SDL_CreateRenderer(window, -1, 0);
//create Surface and load Bitmap (background)
SDL_Surface *Bitmap1 =
SDL_LoadBMP("background.bmp");
//create texture from Surface
SDL_Texture *texture1 = SDL_CreateTextureFromSurface(renderer, Bitmap1);
//_____________
//create Surface2 and load Bitmap2 (for the Class)
SDL_Surface *Bitmap2 =
SDL_LoadBMP("bitmap2.bmp");
//create texture2 from Surface2
SDL_Texture *texture2 = SDL_CreateTextureFromSurface(renderer, Bitmap2);
//load the class
theBMP myClass;
// this is the position for the class
SDL_Rect dstrect3 = {0,0,10,10};
//_____________
//GAME LOOP
bool quit = false;
int game_var =2;
while (!quit)
{
SDL_WaitEvent(&event);
//back button on android || x on pc window
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
//draw Bitmap "background" on x, y, size on screebän
SDL_Rect dstrect1= { 0, 0, 2340, 1028 };
SDL_RenderCopy(renderer, texture1, NULL, &dstrect1);
//_____________
if(game_var == 2)
{
for (int i =0; i<MAX_BMP; i++)
{
arrayofBMP[i]. bmp2_pos_x = 100;
arrayofBMP[i]. bmp2_pos_y =500; //original class position
arrayofBMP[i].isActive = true;
}
game_var =1;
}
for (int i =0; i<MAX_BMP; i++)
{
if (arrayofBMP[i].isActive == true)
{
dstrect3.x = arrayofBMP[i]. bmp2_pos_x +64;
dstrect3.y = arrayofBMP[i]. bmp2_pos_y;
SDL_RenderCopy(renderer, texture2, NULL, &dstrect3);
}
}
myClass.show(renderer,texture2, &dstrect3);
//_____________
//draw renderer
SDL_RenderPresent(renderer);
}
//END GAME LOOP
//------------------------
SDL_DestroyTexture(texture1);
SDL_FreeSurface(Bitmap1);
SDL_DestroyTexture(texture2);
SDL_FreeSurface(Bitmap2);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}