Sd2 image but in a class

Hello my name is patrick and
I have a problem: in the
Last i make an “simpel” space shooter
With c and sdl it works fine ad works o Android too
→ i made it with cass

Now sdl2 wants have SDL_RENDERER COPY but first to texture…

My class code works… but no bmp
Whats wrong??



#include <SDL2/SDL.h>

#include <cstdlib>
#include <sstream>

#define cell_height 32
#define cell_width  32

const int MAX_CELLS = 150;

 
class theCell
{
 public: 
 theCell();
 
 bool isActive;
 
 SDL_Renderer* renderer;
 SDL_Surface *cell1;
 SDL_Texture* texture_cell1;
 
 int cell_pos_x; 
 int cell_pos_y;
 
void cell_show(SDL_Renderer *renderer, SDL_Texture *texture_cell1);

 void cell_setting();

 private:
};
theCell arrayofCells[MAX_CELLS];

//________

theCell::theCell()
{
/*posBullet.x;
posBullet.y;
posBullet.w = 10;
posBullet.h = 15;*/
}

//__________

void theCell::cell_show( SDL_Renderer *renderer, SDL_Texture *texture)
{
    SDL_Texture *texture_cell1;

 //------
	/*
  for (int i=0; i<MAX_CELLS; i++)
    {
     if (arrayofCells[i].isActive == true)
     {

      
     }
    }
    */
//-------

    // draw Cell
SDL_Rect dstrect6= { 50, 50, 20, 20 };SDL_RenderCopy(renderer, texture_cell1,    NULL, &dstrect6);
}

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

void theCell::cell_setting()
{

}

//___________________



int main(int argc, char *argv[])
{
  bool quit = false;
  SDL_Event event;
	
	if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
	{
		fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError());
		return 1;
	}

SDL_Window *window = SDL_CreateWindow("SDL2 cases", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);

 SDL_Renderer *renderer =   
 SDL_CreateRenderer(window, -1, 0);

 SDL_Surface *cell1 =  
 SDL_LoadBMP("cell.bmp");

//__________________

SDL_Texture *texture_cell1 = SDL_CreateTextureFromSurface(renderer, cell1);

//_____________


while (!quit)
 {
	SDL_WaitEvent(&event);

		switch (event.type)
		{
		case SDL_QUIT:
			quit = true;
			break;
		}



    // draw Cell

    theCell myCell;
   myCell.cell_show( renderer, texture_cell1);
   

	SDL_RenderPresent(renderer);
	}
	
   //----------
	
	SDL_DestroyTexture(texture_cell1);
	SDL_FreeSurface(cell1);
	
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);

	SDL_Quit();
	return 0;
}

.

In the cell_show method, you redeclare texture_cell1, but ignore the texture argument, where the address of the original texture_cell1 (declared in main) is actually stored. Declaring texture_cell1 in the cell_show method creates a new SDL_Texture * with an uninitialized value.

If you remove that declaration, and replace
SDL_RenderCopy(renderer, texture_cell1, NULL, &dstrect6);
with
SDL_RenderCopy(renderer, texture, NULL, &dstrect6);
does it work then?

1 Like

Yes sir it works
BIG THANKS

1 Like